package com.njcn.process.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.pms.api.MonitorClient;
import com.njcn.device.pms.pojo.dto.PmsMonitorDTO;
import com.njcn.device.pms.pojo.param.PmsMonitorParam;
import com.njcn.oss.utils.FileStorageUtil;
import com.njcn.process.pojo.po.ThsOverRunLog;
import com.njcn.process.pojo.po.ThsSupervise;
import com.njcn.process.enums.ProgressEnum;
import com.njcn.process.mapper.ThsOverRunLogMapper;
import com.njcn.process.mapper.ThsSuperviseMapper;
import com.njcn.process.pojo.vo.SuperviceRunLogVo;
import com.njcn.process.service.ThsOverRunLogService;
import com.njcn.user.api.DeptFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 告警/预警监测点列表 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2023-03-16
 */
@Service
@Slf4j
public class ThsOverRunLogServiceImpl extends ServiceImpl<ThsOverRunLogMapper, ThsOverRunLog> implements ThsOverRunLogService {
    @Autowired
    private ThsOverRunLogMapper thsOverRunLogMapper;
    @Autowired
    private ThsSuperviseMapper thsSuperviseMapper;
    @Autowired
    private FileStorageUtil fileStorageUtil;
    @Autowired
    private DeptFeignClient deptFeignClient;
    @Autowired
    private MonitorClient monitorClient;

    @Override
    public SuperviceRunLogVo superviseDetail(String supIndex) {
        ThsSupervise thsSupervise = thsSuperviseMapper.selectOne(new LambdaQueryWrapper<ThsSupervise>().eq(ThsSupervise::getSupIndex, supIndex));
        List<ThsOverRunLog> thsOverRunLogs = thsOverRunLogMapper.selectList(new LambdaQueryWrapper<ThsOverRunLog>().eq(ThsOverRunLog::getSupIndex, supIndex));
        if (CollectionUtil.isNotEmpty(thsOverRunLogs)) {
            thsOverRunLogs.forEach(item -> {
                PmsMonitorParam pmsMonitorParam = new PmsMonitorParam();
                pmsMonitorParam.setMonitorIds(Collections.singletonList(item.getLineIndex()));
                HttpResult<List<PmsMonitorDTO>> monitorInfoListByCond = monitorClient.getMonitorInfoListByCond(pmsMonitorParam);
                item.setCompanyName(monitorInfoListByCond.getData().get(0).getOrgName());
                item.setLineIndexName(monitorInfoListByCond.getData().get(0).getName());
                if (StringUtils.isNotBlank(item.getAlarmTicket())) {
                    item.setAlarmTicket(fileStorageUtil.getFileUrl(item.getAlarmTicket()));
                }
                if (StringUtils.isNotBlank(item.getFeedback())) {
                    item.setFeedback(fileStorageUtil.getFileUrl(item.getFeedback()));
                }
                if (StringUtils.isNotBlank(item.getTestReport())) {
                    item.setTestReport(fileStorageUtil.getFileUrl(item.getTestReport()));
                }
                if (StringUtils.isNotBlank(item.getReviseNotice())) {
                    item.setReviseNotice(fileStorageUtil.getFileUrl(item.getReviseNotice()));
                }
                if (StringUtils.isNotBlank(item.getReviseFeedback())) {
                    item.setReviseFeedback(fileStorageUtil.getFileUrl(item.getReviseFeedback()));
                }

            });
        }
        SuperviceRunLogVo superviceRunLogVo = new SuperviceRunLogVo();
        superviceRunLogVo.setThsSupervise(thsSupervise);
        superviceRunLogVo.setOverRunLog(thsOverRunLogs);
        return superviceRunLogVo;
    }

    @Override
    public HttpResult updateSupervise(SuperviceRunLogVo superviseParam) {
        List<ThsOverRunLog> overRunLog = superviseParam.getOverRunLog();
        if (CollectionUtil.isEmpty(overRunLog) ||
                CollectionUtil.isEmpty(overRunLog.stream().map(ThsOverRunLog::getId).collect(Collectors.toList()))) {
            HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "监测点不能为空", null);
        }
        ThsSupervise thsSupervise = superviseParam.getThsSupervise();
        if (StringUtils.isBlank(thsSupervise.getSupIndex())) {
            HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "技术监督id不能为空", null);
        }
        ThsSupervise supervise = thsSuperviseMapper.selectOne(new LambdaQueryWrapper<ThsSupervise>().eq(ThsSupervise::getSupIndex, thsSupervise.getSupIndex()));
        if (!ProgressEnum.START.getCode().equals(supervise.getProgress())) {
            HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, "流程状态为开始才可以更新", null);
        }
        List<ThsOverRunLog> thsOverRunLogs = thsOverRunLogMapper.selectList(new LambdaQueryWrapper<ThsOverRunLog>().eq(ThsOverRunLog::getSupIndex, thsSupervise.getSupIndex()));
        List<String> saveIds = superviseParam.getOverRunLog().stream().map(ThsOverRunLog::getId).collect(Collectors.toList());
        List<String> allIds = thsOverRunLogs.stream().map(ThsOverRunLog::getId).collect(Collectors.toList());
        allIds.removeAll(saveIds);
        thsOverRunLogMapper.delete(new LambdaQueryWrapper<ThsOverRunLog>().in(ThsOverRunLog::getId, allIds));
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, null);
    }
}
