package com.ruoyi.app.controller.doubleDefense;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.doubledefense.domain.CheckTaskInventory;
import com.ruoyi.doubledefense.domain.CheckTaskInventoryRecord;
import com.ruoyi.doubledefense.dto.DangerDTO;
import com.ruoyi.doubledefense.dto.res.*;
import com.ruoyi.doubledefense.service.ICheckTaskInventoryRecordService;
import com.ruoyi.doubledefense.service.ICheckTaskInventoryService;
import com.ruoyi.doubledefense.service.IDangerService;
import com.ruoyi.doubledefense.service.impl.CheckTaskInventoryServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 巡查任务Controller
 *
 * @author ruoyi
 * @date 2023-08-03
 */
@Api(tags = "APP巡查任务打卡相关接口")
@RestController
@RequestMapping("/app/checkTask")
public class CheckTaskForAppController extends BaseController {

    @Autowired
    private ICheckTaskInventoryService iCheckTaskInventoryService;

    @Autowired
    private ICheckTaskInventoryRecordService checkTaskInventoryRecordService;


    /**
     * 1、APP巡检任务-区域未维度（第一层）
     * 注意：根据隐患排查要求绑定的岗位下人员可见的任务反向关联对应的区域
     *
     * @return
     */
    @ApiOperation("查询用户巡检清单列表（区域纬度）")
    @GetMapping("/areaList")
    public TableDataInfo list() {
        startPage();
        List<CheckTaskListDTOForApp> list = iCheckTaskInventoryService.getUserCheckTaskListForApp();
//        Long userId = SecurityUtils.getUserId();
//        System.out.println("userId = " + userId);
        return getDataTable(list);
    }


    //    @ApiOperation("查询区域下巡检清单列表")
//    @ApiImplicitParam(name = "type", value = "0未排查，1已排查", dataType = "Integer", dataTypeClass = Integer.class)
//    public List<CheckTaskInventoryResultDTO> getAreaCheckTaskList(Integer type)

    /**
     * 2、APP巡检任务-区域未维度-分析单元维度（第二层）
     * 通过区域ID查询分下分析单元下的所有任务（不区分任务状态）
     *
     * @param areaId
     * @return
     */
    @ApiOperation("查询区域下巡检清单列表（分析单元纬度）")
    @ApiImplicitParam(name = "areaId", value = "区域ID", dataType = "long", dataTypeClass = Long.class)
    @GetMapping("/getAreaCheckTaskList")
    public TableDataInfo getAreaCheckTaskList(Long areaId) {
        /*startPage();
        List<Danger> list = dangerService.selectDangerList(danger);
        return getDataTable(list);*/
//        return Arrays.asList(new CheckTaskInventoryResultDTO());
        startPage();
        List<CheckTasRiskUnitkListDTOForApp> list = iCheckTaskInventoryService.getUserCheckTaskRiskUnitListForApp(areaId);
        return getDataTable(list);
    }

    /**
     * 2、APP巡检任务-区域未维度-分析单元维度-巡检任务列表（第三层）
     *
     * @param riskUnitId
     * @return
     */
    //    @ApiOperation("查询巡检任务所属分析单元的详情")
//    @ApiImplicitParam(name = "checkTaskInventoryId", value = "巡检清单ID", dataType = "String", dataTypeClass = String.class)
//    @GetMapping("/getRiskUnitByCheckTaskInventoryId")
    @ApiOperation("查询分析单元下巡检任务清单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "riskUnitId", value = "所属风险单元UUID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "排查状态：0未排查，1已排查", dataType = "String", dataTypeClass = String.class)
    })
    @GetMapping("/getCheckTaskInventoryListByRistUnitId")
    public TableDataInfo getCheckTaskInventoryListByRistUnitId(String riskUnitId, Integer status) {
//        List<CheckTaskInventory> list = null;
        //待巡检 不分页：清单表
        if (status != null) {

            //已完成
            if (status == 0) {
                // 开始时间正序
                List<CheckTaskInventory> list = iCheckTaskInventoryService.selectCheckTaskInventoryListByRistUnitId(riskUnitId);
                return getDataTable(list);
            }
            //已巡检、漏检 分页：记录表
            else if (status == 1 || status == 2) {
//            else if (status == 1) {
                // 开始时间倒叙
                startPage();
                List<CheckTaskInventoryRecord> recordList = checkTaskInventoryRecordService.selectCheckTaskInventoryRecordListByRistUnitId(riskUnitId, status);
                return getDataTable(recordList);
            }
        }
        return getDataTable(new ArrayList<>());
//        return Arrays.asList(new RiskUnitResultDTO());
    }


    /**
     * APP巡检打卡（NFC）传回区域ID给服务端，服务端再通过当前登录人的PcUserId查对应的岗位->通过岗位再查询待巡检的任务->将待巡检任务批量的巡检完成
     *
     * @param areaCode 企业编码
     * @return
     */
    @ApiOperation("巡检打卡")
    @PostMapping("/register")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "areaCode", value = "区域编码", dataType = "String", dataTypeClass = String.class)
    })
    public AjaxResult register(String areaCode) {

        logger.info("【巡检打卡】本次打卡的区域ID areaCode=" + areaCode);
        String msg = "";
        boolean flag = false;
        try {
            flag = iCheckTaskInventoryService.register(areaCode);
        } catch (Exception e) {
            msg = e.getMessage();
            logger.info("巡检打卡失败：{}", msg);
            e.printStackTrace();
        }
        if (!flag) {
            return error(msg);
        } else {
            return success(1);
        }
    }


}
