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


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwe.scentity.domian.ApsDetailsHazardInvestigationTasksDo;
import com.rongwe.scentity.domian.AspCheckDiscoveryItemDo;
import com.rongwe.scentity.domian.HiddenDangerTrackDo;
import com.rongwei.rwcommon.base.BaseDo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.safecommon.utils.JSCommonUtils;
import com.rongwei.sfcommon.sys.dao.ApsDetailsHazardInvestigationTasksDao;
import com.rongwei.sfcommon.sys.service.ApsDetailsHazardInvestigationTasksService;
import com.rongwei.sfcommon.utils.MlConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class ApsDetailsHazardInvestigationTasksServiceImpl extends ServiceImpl<ApsDetailsHazardInvestigationTasksDao, ApsDetailsHazardInvestigationTasksDo>
        implements ApsDetailsHazardInvestigationTasksService {

    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private AspCheckDiscoveryItemServiceImpl aspCheckDiscoveryItemService;
    @Autowired
    private HiddenDangerTrackServiceImpl hiddenDangerTrackService;
    @Autowired
    private SafeCheckSendNotifyServiceImpl sendNotifyService;
    @Autowired
    private ApsDetailsHazardInvestigationTasksService apsDetailsHazardInvestigationTasksService;


    @Override
    public R hazardInvestigationTaskSave(Map<String, String> parameter) {
        String id = parameter.getOrDefault("id", "");
        if (StringUtils.isBlank(id)) {
            log.error("隐患排查任务ID为空");
            return R.ok();
        }
        // 获取隐患排查任务主表
        ApsDetailsHazardInvestigationTasksDo apsDetailsHazardInvestigationTasksDo = apsDetailsHazardInvestigationTasksService.getById(id);
        if (null == apsDetailsHazardInvestigationTasksDo) {
            log.error("通过id:{}无法获取到隐患排查任务", id);
            return R.ok();
        }

        // 获取所有的隐患排查任务内容
        List<AspCheckDiscoveryItemDo> aspCheckDiscoveryItemDos = aspCheckDiscoveryItemService.list(new LambdaQueryWrapper<AspCheckDiscoveryItemDo>()
                .eq(AspCheckDiscoveryItemDo::getThemecheckitemid, id)
                .eq(AspCheckDiscoveryItemDo::getCreatetask, "0")
                .eq(BaseDo::getDeleted, "0"));
        if (aspCheckDiscoveryItemDos.isEmpty()) {
            log.error("无法通过ID：{}获取到检查项信息", id);
        }
        // 任务生成
        List<HiddenDangerTrackDo> sendNotifyData = generateHiddenDangerTrack(apsDetailsHazardInvestigationTasksDo, aspCheckDiscoveryItemDos);
        // 更新任务状态
        List<String> discoverIds = aspCheckDiscoveryItemDos.stream().map(AspCheckDiscoveryItemDo::getId).collect(Collectors.toList());
        aspCheckDiscoveryItemService.update(new LambdaUpdateWrapper<AspCheckDiscoveryItemDo>()
                .set(AspCheckDiscoveryItemDo::getCreatetask, 1)
                .eq(AspCheckDiscoveryItemDo::getId, discoverIds));
        sendNotifyService.sendDangerTasksNotify(sendNotifyData);
        apsDetailsHazardInvestigationTasksService.update(new LambdaUpdateWrapper<ApsDetailsHazardInvestigationTasksDo>()
                .set(ApsDetailsHazardInvestigationTasksDo::getInvestigationtime, new Date())
                .eq(ApsDetailsHazardInvestigationTasksDo::getState, 1));
        return R.ok();
    }


    /**
     * 生成隐患跟踪任务
     * 每一个检查项生成一个隐患跟踪任务
     *
     * @return
     */
    public List<HiddenDangerTrackDo> generateHiddenDangerTrack(ApsDetailsHazardInvestigationTasksDo apsDetailsHazardInvestigationTasksDo, List<AspCheckDiscoveryItemDo> aspCheckDiscoveryItemDos) {

        List<HiddenDangerTrackDo> addList = new ArrayList<>();
        aspCheckDiscoveryItemDos.forEach(info -> {
            HiddenDangerTrackDo hiddenDangerTrack = new HiddenDangerTrackDo();
            hiddenDangerTrack.setId(SecurityUtil.getUUID());
            hiddenDangerTrack.setTenantid(apsDetailsHazardInvestigationTasksDo.getTenantid());
            //风险分析对象编码
            hiddenDangerTrack.setRiskanalysisobjectcode(apsDetailsHazardInvestigationTasksDo.getTempcode());
            //隐患等级hazardlevel
            hiddenDangerTrack.setHazardlevel(info.getGrade());
            //治理类型
            hiddenDangerTrack.setGovernancetype(info.getGovernancetype());
            //隐患类别
            hiddenDangerTrack.setHiddendangerleibie(info.getClassification());
            //隐患类型
            hiddenDangerTrack.setHiddendangertype(info.getType());
            //隐患来源
            hiddenDangerTrack.setDangersource("1");
            //隐患状态 默认待评级
            hiddenDangerTrack.setHazardstate("3");
            //隐患名称
            hiddenDangerTrack.setHazardname(info.getName());
            //隐患描述
            if (StringUtils.isNotBlank(info.getHazarddescription())) {
                hiddenDangerTrack.setHazarddescription(info.getHazarddescription());
            }
            if (StringUtils.isNotBlank(info.getMeasure())) {
                hiddenDangerTrack.setMeasure(info.getMeasure());
            }
            //整改责任人
            hiddenDangerTrack.setResponsiblepersonforrectification(info.getTrackuser());
            //责任人 id
            hiddenDangerTrack.setTrackuserid(info.getTrackuserid());
            //检查任务id
            hiddenDangerTrack.setCheckid(info.getId());
            JSCommonUtils.setBaseDetail(hiddenDangerTrack, null);
            //隐患图片
            hiddenDangerTrack.setHazardphoto(info.getFileinfo());
            addList.add(hiddenDangerTrack);
        });
        hiddenDangerTrackService.saveBatch(addList);
//        checkItemDo.setSaved("1");
//        themeCheckItemService.saveOrUpdate(checkItemDo);
        return addList;
    }
}
