package cn.iocoder.yudao.module.pms.job.qhse;

import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pms.constant.PmsConstants;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.cert.vo.IotMeasureCertPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.measure.vo.IotMeasureBookPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.DeptUtil;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.cert.IotMeasureCertMapper;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.measure.IotMeasureBookMapper;
import cn.iocoder.yudao.module.pms.message.PmsMessage;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.PostApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.permission.PermissionApi;
import cn.iocoder.yudao.module.system.api.permission.RoleApi;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import cn.iocoder.yudao.module.system.service.permission.RoleService;
import com.alibaba.excel.util.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class BookAndCertWeekJob implements JobHandler {
    private final IotMeasureBookMapper iotMeasureBookMapper;
    private final DeptApi deptApi;
    private final DeptService deptService;
    private final PmsMessage pmsMessage;
    private final AdminUserApi adminUserApi;
    private final PostApi postApi;
    private final RoleApi roleApi;
    private final RoleService roleService;
    private final PermissionApi permissionApi;
    private final IotMeasureCertMapper iotMeasureCertMapper;
    private final DeptUtil deptUtil;

    public BookAndCertWeekJob(IotMeasureBookMapper iotMeasureBookMapper, DeptApi deptApi, DeptService deptService, PmsMessage pmsMessage, AdminUserApi adminUserApi, PostApi postApi, RoleApi roleApi, RoleService roleService, PermissionApi permissionApi, IotMeasureCertMapper iotMeasureCertMapper, DeptUtil deptUtil) {
        this.iotMeasureBookMapper = iotMeasureBookMapper;
        this.deptApi = deptApi;
        this.deptService = deptService;
        this.pmsMessage = pmsMessage;
        this.adminUserApi = adminUserApi;
        this.postApi = postApi;
        this.roleApi = roleApi;
        this.roleService = roleService;
        this.permissionApi = permissionApi;
        this.iotMeasureCertMapper = iotMeasureCertMapper;
        this.deptUtil = deptUtil;
    }

    @Override
    @TenantIgnore
    @DataPermission(enable = false)
    public String execute(String param) throws Exception {
        //获取项目部
        List<DeptRespDTO> collect = deptApi.getDeptList().stream().filter(e -> "2".equals(e.getType())).collect(Collectors.toList());
        collect.forEach(dept -> {
            Set<Long> ids = new HashSet<>();
            if (Objects.nonNull(dept.getId())) {
                ids = deptService.getChildDeptIdListFromCache(dept.getId());
                ids.add(dept.getId());
            }
            IotMeasureBookPageReqVO reqVO = new IotMeasureBookPageReqVO();
            reqVO.setAlertWarn(true);
            //90天预警
            long warn = iotMeasureBookMapper.selectCountByDeptAndExpireAndWarn(reqVO, ids);
            reqVO.setAlertWarn(null);
            reqVO.setExpired(true);
            Long expired = iotMeasureBookMapper.selectCountByDeptAndExpireAndWarn(reqVO, ids);
            //获取证书的预警及过期数量
            IotMeasureCertPageReqVO reqVO1 = new IotMeasureCertPageReqVO();
            reqVO1.setAlertWarn(true);
            Long warnCert = iotMeasureCertMapper.selectCountByDeptAndExpireAndWarn(reqVO1, ids);
            reqVO1.setAlertWarn(null);
            reqVO1.setExpired(true);
            Long expiredCert = iotMeasureCertMapper.selectCountByDeptAndExpireAndWarn(reqVO1, ids);
            if (warn>0||expired>0||warnCert>0||expiredCert>0) {
                List<AdminUserRespDTO> userListByDeptId = adminUserApi.getUserListByDeptId(dept.getId());
                List<Long> userIds = roleApi.getRoleUserIds("QHSE项目部");
                //给项目部的角色为QHSE项目部的人员发送消息，项目部的过期及预警都要发送消息
                userListByDeptId.stream().filter(e -> userIds.contains(e.getId())).forEach(adminUser -> {
//                    pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+",过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+",过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, adminUser.getId(), adminUser.getMobile());
                    pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+",过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, 140L, "13255461093");
                });
                //发送给各公司的QHSE负责人
                String companyCode = deptUtil.getCompanyCode(dept.getId());
                if ("rh".equals(companyCode)){
                    List<AdminUserRespDTO> rh = adminUserApi.getUserListByDeptId(157L);
                    List<Long> rhIds = roleApi.getRoleUserIds("QHSE安全副总监");
                    rh.stream().filter(e -> rhIds.contains(e.getId())).forEach(adminUser -> {
                        String nickname = adminUser.getNickname();
                        if (StringUtils.isNotBlank(nickname)) {
                            if ("翟纪全".equals(nickname)||"刘学刚".equals(nickname)) {
                                pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+"，过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, adminUser.getId(), adminUser.getMobile());
                            }
                        }
//                        pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+",过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, 140L, "13255461093");
                    });
                } else if ("ry".equals(companyCode)){
                    List<AdminUserRespDTO> ry = adminUserApi.getUserListByDeptId(158L);
                    List<Long> ryIds = roleApi.getRoleUserIds("QHSE安全副总监");
                    ry.stream().filter(e -> ryIds.contains(e.getId())).forEach(adminUser -> {
                        String nickname = adminUser.getNickname();
                        if (StringUtils.isNotBlank(nickname)) {
                            if ("李东斌".equals(nickname)||"李强".equals(nickname)) {
                                pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+"，过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, adminUser.getId(), adminUser.getMobile());
                            }
                        }
//                        pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+",过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, 140L, "13255461093");
                    });
                } else if ("rd".equals(companyCode)){
                    List<AdminUserRespDTO> rd = adminUserApi.getUserListByDeptId(163L);
                    List<Long> rdIds = roleApi.getRoleUserIds("QHSE安全副总监");
                    rd.stream().filter(e -> rdIds.contains(e.getId())).forEach(adminUser -> {
                        String nickname = adminUser.getNickname();
                        if (StringUtils.isNotBlank(nickname)) {
                            if ("曲猛".equals(nickname)||"陈习荣".equals(nickname)) {
                                pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+"，过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, adminUser.getId(), adminUser.getMobile());
                            }
                        }
//                        pmsMessage.sendMessage(dept.getId(), dept.getName()+"计量器具台账90天预警数量为："+warn+",过期数量为："+expired+"。资质证书60天预警数量为："+warnCert+"，过期数量为："+expiredCert+"。请登录QHSE系统进行查看。", PmsConstants.WEEK_ALARM_MESSAGE, 140L, "13255461093");
                    });
                }
            }
        });
        //瑞恒
        Set<Long> ids = new HashSet<>();
        IotMeasureBookPageReqVO reqVO = new IotMeasureBookPageReqVO();
        reqVO.setExpired(true);
        ids = deptService.getChildDeptIdListFromCache(157L);
        ids.add(157L);
        Long expired = iotMeasureBookMapper.selectCountByDeptAndExpireAndWarn(reqVO, ids);
        IotMeasureCertPageReqVO certPageReqVO = new IotMeasureCertPageReqVO();
        certPageReqVO.setExpired(true);
        Long certExpired = iotMeasureCertMapper.selectCountByDeptAndExpireAndWarn(certPageReqVO, ids);
        pmsMessage.sendMessage(10L, "瑞恒兴域计量器具过期数量为："+expired+"，资质证书过期数量为："+certExpired+"。请登录QHSE系统进行查看。",PmsConstants.WEEK_ALARM_MESSAGE,
                873L,"15710003711");
        //瑞鹰
        Set<Long> ryids = new HashSet<>();
        IotMeasureBookPageReqVO ryreqVO = new IotMeasureBookPageReqVO();
        ryreqVO.setExpired(true);
        ryids = deptService.getChildDeptIdListFromCache(158L);
        ryids.add(158L);
        Long ryexpired = iotMeasureBookMapper.selectCountByDeptAndExpireAndWarn(ryreqVO, ryids);
        IotMeasureCertPageReqVO rycertPageReqVO = new IotMeasureCertPageReqVO();
        rycertPageReqVO.setExpired(true);
        Long rycertExpired = iotMeasureCertMapper.selectCountByDeptAndExpireAndWarn(rycertPageReqVO, ryids);
        pmsMessage.sendMessage(10L, "瑞鹰国际计量器具过期数量为："+ryexpired+"，资质证书过期数量为："+rycertExpired+"。请登录QHSE系统进行查看。",PmsConstants.WEEK_ALARM_MESSAGE,
                873L,"15710003711");
        //瑞都
        Set<Long> rdids = new HashSet<>();
        IotMeasureBookPageReqVO rdreqVO = new IotMeasureBookPageReqVO();
        rdreqVO.setExpired(true);
        rdids = deptService.getChildDeptIdListFromCache(163L);
        rdids.add(163L);
        Long rdexpired = iotMeasureBookMapper.selectCountByDeptAndExpireAndWarn(rdreqVO, rdids);
        IotMeasureCertPageReqVO rdcertPageReqVO = new IotMeasureCertPageReqVO();
        rdcertPageReqVO.setExpired(true);
        Long rdcertExpired = iotMeasureCertMapper.selectCountByDeptAndExpireAndWarn(rdcertPageReqVO, rdids);
        pmsMessage.sendMessage(10L, "四川瑞都计量器具过期数量为："+rdexpired+"，资质证书过期数量为："+rdcertExpired+"。请登录QHSE系统进行查看。",PmsConstants.WEEK_ALARM_MESSAGE,
                873L,"15710003711");
        return "";
    }
}
