package com.ruoyi.app.controller.doubleDefense;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.JWTUtil;
import com.ruoyi.doubledefense.domain.CheckTaskInventoryRecord;
import com.ruoyi.doubledefense.dto.CheckTaskInventoryStatisticsDTO;
import com.ruoyi.doubledefense.dto.HazardStatisticsDTO;
import com.ruoyi.doubledefense.service.ICheckTaskInventoryRecordService;
import com.ruoyi.doubledefense.service.IDangerService;
import com.ruoyi.doubledefense.service.IHazardService;
import com.ruoyi.system.domain.AppLogin;
import com.ruoyi.system.dto.ContractorUserDTO;
import com.ruoyi.system.service.IAppLoginService;
import com.ruoyi.system.service.IContractorUserService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Objects;

/**
 * APP首页统计
 */
@Api(tags = "APP首页统计-风险分级管理")
@RestController
@RequestMapping("/app/doubleDefense/homepage")
public class DoubleDefenseHomePageAppController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IAppLoginService appLoginService;
    @Autowired
    private IContractorUserService contractorUserService;

    @Autowired
    private IHazardService hazardService;
    @Autowired
    private ICheckTaskInventoryRecordService checkTaskInventoryRecordService;

    @ApiOperation("风险管理统计：无参数，响应参数参考->Parameters")
//    @ApiResponses(HazardStatisticsDTO.class)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "type", value = "审核类型：1驳回，2通过", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "dangerId", value = "隐患主键", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "remark", value = "备注：驳回原因或审核通过备注", dataType = "String", dataTypeClass = String.class)
//    })
    @GetMapping("/riskStatistics")
    public AjaxResult riskStatistics(HazardStatisticsDTO statisticsDTO) {
        //用户昵称
        String nickName = null;
        //用户类型：1内部，2承包商
        if ("1".equals(JWTUtil.getAppUserType())) {
            //设置人员姓名
            SysUser sysUser = sysUserService.selectUserById(Long.parseLong(JWTUtil.getPCUserId()));
            if (Objects.nonNull(sysUser)) {
                nickName = sysUser.getNickName();
            }
        } else {
            AppLogin appLogin = appLoginService.selectAppLoginById(JWTUtil.getUserId());
            if (Objects.nonNull(appLogin)) {
                ContractorUserDTO contractorUserDTO = contractorUserService.selectContractorUserById(appLogin.getContractorUserId());
                if (Objects.nonNull(contractorUserDTO)) {
                    nickName = contractorUserDTO.getCompellation();
                }
            }
        }
        //管控对象（生产装置）
        //分析单元
        //风险事件
        //管控措施
        //排查要求（排查项）

        //岗位要求（无）
        //重大危险源（无）

        HazardStatisticsDTO result = hazardService.riskStatistics();

        return AjaxResult.success(result);
//        return AjaxResult.success("接口待开发……【" + nickName + "】");
    }


    @ApiOperation("巡检完成率：响应参数参考->Parameters")
    @GetMapping("/checkTaskInventoryStatistics")
    public AjaxResult checkTaskInventoryStatistics(CheckTaskInventoryStatisticsDTO checkTaskInventoryStatisticsDTO) {
      /*  //用户昵称
        String nickName = null;
        //用户类型：1内部，2承包商
        if ("1".equals(JWTUtil.getAppUserType())) {
            //设置人员姓名
            SysUser sysUser = sysUserService.selectUserById(Long.parseLong(JWTUtil.getPCUserId()));
            if (Objects.nonNull(sysUser)) {
                nickName = sysUser.getNickName();
            }
        } else {
            AppLogin appLogin = appLoginService.selectAppLoginById(JWTUtil.getUserId());
            if (Objects.nonNull(appLogin)) {
                ContractorUserDTO contractorUserDTO = contractorUserService.selectContractorUserById(appLogin.getContractorUserId());
                if (Objects.nonNull(contractorUserDTO)) {
                    nickName = contractorUserDTO.getCompellation();
                }
            }
        }*/

        //往前多少天，默认APP给正数，传递给方法时转换成负数，往前减天数
        Integer lastDays = checkTaskInventoryStatisticsDTO.getLastDays();
        if (lastDays == null) {
            return AjaxResult.error("参数错误，请传递需要统计的天数！");
        }
        // AND tcti.start_date <![CDATA[<=]]>  #{startDateLt}
        // AND tcti.start_date >= #{startDateGt}
        Date today = DateUtils.getLastDay(0, 23, 59, 59, 999);
        Date lastDay = DateUtils.getLastDay(lastDays > 0 ? -lastDays : lastDays, 0, 0, 0, 0);


        CheckTaskInventoryRecord checkTaskInventoryRecord = new CheckTaskInventoryRecord();
        checkTaskInventoryRecord.setStartDateGt(lastDay);//N天前的00:00:00
        checkTaskInventoryRecord.setStartDateLt(today);//今天 23:59:59:59
        CheckTaskInventoryStatisticsDTO result = checkTaskInventoryRecordService.checkTaskInventoryStatistics(checkTaskInventoryRecord);
        result.setLastDays(lastDays);
        return AjaxResult.success(result);
//        return AjaxResult.success("接口待开发……【" + nickName + "】");
    }

}
