package com.ruoyi.web.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.core.domain.model.LoginUser;
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.CheckTaskInventoryStatisticsPCDTO;
import com.ruoyi.doubledefense.dto.DangerStatisticsPCDTO;
import com.ruoyi.doubledefense.dto.DeviceStatisticsPCDTO;
import com.ruoyi.doubledefense.service.ICheckTaskInventoryRecordService;
import com.ruoyi.doubledefense.service.IDangerService;
import com.ruoyi.doubledefense.service.IDeviceService;
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 lombok.extern.slf4j.Slf4j;
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;

@Api(tags = "PC首页统计-双控")
@RestController
@RequestMapping("/pc/doubledefense/task/homepage")
@Slf4j
public class DoubleDefenseHomePageController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IAppLoginService appLoginService;
    @Autowired
    private IContractorUserService contractorUserService;
    @Autowired
    private IDangerService dangerService;
    @Autowired
    private ICheckTaskInventoryRecordService checkTaskInventoryRecordService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private IHazardService hazardService;


    @ApiOperation("双重预防机制")
    @GetMapping("/dangerStatistics")
    public AjaxResult dangerStatistics() {
        /*//用户昵称
        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();
                }
            }
        }*/
        DangerStatisticsPCDTO data = dangerService.dangerStatisticsPC(new Date());
        return AjaxResult.success(data);
    }

    @ApiOperation("巡检任务清单统计")
    @GetMapping("/checkTaskInventoryStatisticsPC")
    public AjaxResult checkTaskInventoryStatisticsPC(Integer lastDays) {
        /*//用户昵称
        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();
                }
            }
        }
*/
        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

        CheckTaskInventoryStatisticsPCDTO data = checkTaskInventoryRecordService.checkTaskInventoryStatisticsPC(checkTaskInventoryRecord);
        data.setLastDays(lastDays);
        return AjaxResult.success(data);
    }

    @ApiOperation("设备管理")
    @GetMapping("/deviceStatistics")
    public AjaxResult deviceStatistics(Integer lastDays) {
        /*//用户昵称
        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();
                }
            }
        }*/
        LoginUser loginUser = getLoginUser();
        String companyCode = loginUser.getCompanyCode();
        DeviceStatisticsPCDTO data = deviceService.deviceStatistics(companyCode);
        return AjaxResult.success(data);
    }

}
