package com.rongwei.sfcommon.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rongwe.scentity.domian.CheckItemsDo;
import com.rongwe.scentity.domian.PointCheckDo;
import com.rongwe.scentity.vo.CheckItemsTypeVo;
import com.rongwei.safecommon.utils.CXCommonUtils;
import com.rongwei.sfcommon.sys.dao.CheckItemsDao;
import com.rongwei.sfcommon.sys.service.CheckItemsService;
import com.rongwei.sfcommon.sys.service.PointCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

@Service("checkItemsService")
public class CheckItemsServiceImpl extends ServiceImpl<CheckItemsDao, CheckItemsDo> implements CheckItemsService {

    @Autowired
    private PointCheckService pointCheckService;

    @Autowired
    private CheckItemsDao checkItemsDao;

    /**
     * 设备列表删除验证是否存在点检记录
     *
     * @param ids
     */
    public String deleteValidate(List<String> ids) {
        String warnMes = null;
        List<String> retWarn = new ArrayList<>();
        if (ids != null && ids.size() > 0) {
            for (String id : ids) {
                CheckItemsDo checkItemsDo = this.getById(id);
                Map<String, Object> pcMap = new HashMap<>();
                pcMap.put("CHECKITEMID", id);
                List<PointCheckDo> pointCheckDos = (List<PointCheckDo>) pointCheckService.listByMap(pcMap);
                if (pointCheckDos != null && pointCheckDos.size() > 0) {
                    retWarn.add("设备编号：" + checkItemsDo.getCheckitemcode() + "；设备名称：" + checkItemsDo.getCheckitemname());
                } else {
                    this.removeById(id);
                }
            }
        }
        if (retWarn != null && retWarn.size() > 0) {
            warnMes = String.join("\n", retWarn);
            warnMes = warnMes + "\n" + "以上设备已存在点检记录不可删除；";
        }
        return warnMes;
    }

    /**
     * 备用设备列表
     *
     * @return
     */
    @Override
    public IPage<CheckItemsDo> spareList(Page page) {

        return checkItemsDao.spareList(page);
    }

    @Override
    public void reportEquMessage() {
        //查询所有需要提醒的设备
        List<CheckItemsTypeVo> list = this.baseMapper.selectNeedReportEqu();
        //查询设备的工厂，三个工厂的设备管理员角色下的员工ID
        Set<String> tenantIds = list.stream().map(CheckItemsDo::getTenantid).collect(Collectors.toSet());
        Map<String, List<String>> equManager = new HashMap<>();
        tenantIds.forEach(tenantId -> {
            List<String> userIdList = this.baseMapper.selectUserByTenantId(tenantId);
            equManager.put(tenantId, userIdList);
        });

        //根据设备的所属工厂给自己工厂的设备管理员发送消息
        list.forEach(item -> {
            if (ObjectUtil.isNotEmpty(item.getTenantid())) {
                String typeName = ObjectUtil.isNotEmpty(item.getTypeName()) ? item.getTypeName() : "";
                String checkItemCode = ObjectUtil.isNotEmpty(item.getCheckitemcode()) ? item.getCheckitemcode() : "";
                String checkItemName = ObjectUtil.isNotEmpty(item.getCheckitemname()) ? item.getCheckitemname() : "";
                String nextCheckDate = ObjectUtil.isNotEmpty(item.getNextcheckdate()) ? DateUtil.format(item.getNextcheckdate(), "yyy-MM-dd") : "";


                //发送消息
                CXCommonUtils.sendNotify("特种设备提醒",
                        "【设备年检提醒】，您好，有如下设备即将年检到期，请及时处理：" +
                                "设备类型：【" + typeName + "】，设备编号：【" + checkItemCode + "】，设备名称：【" + checkItemName + "】，年检日期：【" + nextCheckDate + "】",
                        null,
                        equManager.get(item.getTenantid()),
                        item.getId(),
                        "specialequipment");
            }
        });


    }

}
