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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rongwe.scentity.domian.ApsSpecialInspectionTaskDo;
import com.rongwe.scentity.domian.AspCheckDiscoveryItemDo;
import com.rongwe.scentity.domian.HiddenDangerTrackDo;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.rwcommon.base.BaseDo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SecurityUtil;
import com.rongwei.safecommon.utils.JSCommonUtils;
import com.rongwei.sfcommon.sys.service.SpecialInspectionService;
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.List;

/**
 * SpecialInspectionServiceImpl class
 *
 * @author XH
 * @date 2024/08/20
 */
@Service
public class SpecialInspectionServiceImpl implements SpecialInspectionService {
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private ApsSpecialInspectionTaskZibiaoServiceImpl apsSpecialInspectionTaskZibiaoService;
    @Autowired
    private ApsSpecialInspectionTaskServiceImpl apsSpecialInspectionTaskService;
    @Autowired
    private AspCheckDiscoveryItemServiceImpl aspCheckDiscoveryItemService;
    @Autowired
    private HiddenDangerTrackServiceImpl hiddenDangerTrackService;

    /**
     * 专项检查生成隐患任务
     *
     * @param id
     * @return
     */
    @Override
    public R createHiddenDangerTrack(String id) {
        log.info("开始根据专项检查任务：{}生成隐患任务", id);
        ApsSpecialInspectionTaskDo specialInspectionTaskDo = apsSpecialInspectionTaskService.getById(id);
        JSCommonUtils.parameterCheck(() -> specialInspectionTaskDo == null, "无法找到对应的专项检查任务",
                "根据id:{}无法找到对应的专项检查任务", id);
        // 获取改专项检查任务对应的隐患任务
        List<AspCheckDiscoveryItemDo> checkDiscoveryItemDos = aspCheckDiscoveryItemService.list(new LambdaQueryWrapper<AspCheckDiscoveryItemDo>().eq(BaseDo::getDeleted, "0")
                .eq(AspCheckDiscoveryItemDo::getThemecheckitemid, id));
        JSCommonUtils.parameterCheck(checkDiscoveryItemDos::isEmpty, "暂无隐患任务需要生成",
                "");

        List<HiddenDangerTrackDo> saveList = new ArrayList<>();
        HiddenDangerTrackDo hiddenDangerTrackDo;
        SysUserVo currentUser = JSCommonUtils.getCurrentUser();
        for (AspCheckDiscoveryItemDo checkDiscoveryItemDo : checkDiscoveryItemDos) {
            hiddenDangerTrackDo = new HiddenDangerTrackDo();
            hiddenDangerTrackDo.setId(SecurityUtil.getUUID());
            JSCommonUtils.setBaseDetail(hiddenDangerTrackDo, currentUser);
            hiddenDangerTrackDo.setRiskanalysisobjectcode(checkDiscoveryItemDo.getRiskunitcode());
            hiddenDangerTrackDo.setHazardlevel(checkDiscoveryItemDo.getGrade());
            hiddenDangerTrackDo.setDangersource("10");
            hiddenDangerTrackDo.setGovernancetype(checkDiscoveryItemDo.getGovernancetype());
            hiddenDangerTrackDo.setHiddendangertype(checkDiscoveryItemDo.getType());
            hiddenDangerTrackDo.setHazardstate("3");
            hiddenDangerTrackDo.setHazardname(checkDiscoveryItemDo.getName());
            hiddenDangerTrackDo.setHazarddescription(checkDiscoveryItemDo.getHazarddescription());
            hiddenDangerTrackDo.setHazardphoto(checkDiscoveryItemDo.getFileinfo());
            hiddenDangerTrackDo.setResponsiblepersonforrectification(checkDiscoveryItemDo.getTrackuser());
            hiddenDangerTrackDo.setTrackuserid(checkDiscoveryItemDo.getTrackuserid());
            hiddenDangerTrackDo.setTenantid(specialInspectionTaskDo.getTenantid());
            JSCommonUtils.setBaseDetail(hiddenDangerTrackDo,currentUser);
            saveList.add(hiddenDangerTrackDo);
        }
        JSCommonUtils.batchChunk(saveList, hiddenDangerTrackService::saveBatch, 100);
        return R.ok();
    }
}
