package cn.cecep.talroad.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.cecep.talroad.common.core.exception.ServiceException;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.util.DateUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.cecep.talroad.domain.PcFactory;
import cn.cecep.talroad.domain.PcOutlet;
import cn.cecep.talroad.domain.PcPollutant;
import cn.cecep.talroad.mapper.PcFactoryMapper;
import cn.cecep.talroad.mapper.PcOutletMapper;
import cn.cecep.talroad.mapper.PcPollutantMapper;
import cn.cecep.talroad.vo.AmProblemFiRatioImportVo;
import cn.cecep.talroad.vo.AmProblemFiRatioVo;
import cn.cecep.talroad.vo.PcPollutantVo;
import cn.cecep.talroad.vo.query.AmProblemFiRatioQuery;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cecep.talroad.domain.AmProblemFiRatio;
import cn.cecep.talroad.mapper.AmProblemFiRatioMapper;
import cn.cecep.talroad.service.IAmProblemFiRatioService;




/**
 * 预警报警信息-预警比例定制Service业务层处理
 *
 * @author szhpt
 * @date 2023-06-27*/
@Service
public class AmProblemFiRatioServiceImpl extends ServiceImpl<AmProblemFiRatioMapper,AmProblemFiRatio> implements IAmProblemFiRatioService
{
    @Autowired
    private AmProblemFiRatioMapper amProblemFiRatioMapper;

    @Autowired
    private PcFactoryMapper pcFactoryMapper;

    @Autowired
    private PcOutletMapper pcOutletMapper;

    @Autowired
    private PcPollutantMapper pcPollutantMapper;
    /**
     * 查询预警报警信息-预警比例定制列表
     *
     * @param amProblemFiRatio 预警报警信息-预警比例定制
     * @return 预警报警信息-预警比例定制
     */
    @Override
    public List<AmProblemFiRatioVo> selectAmProblemFiRatioList(AmProblemFiRatioQuery amProblemFiRatio)
    {
        return amProblemFiRatioMapper.selectAmProblemFiRatioList(amProblemFiRatio);
    }

    @Override
    public List<AmProblemFiRatioVo> pageList(Page<AmProblemFiRatioVo> page,AmProblemFiRatioQuery amProblemFiRatio) {
        List<AmProblemFiRatioVo> list = new ArrayList<>();
        if(page != null){
            list = amProblemFiRatioMapper.pageList(page,amProblemFiRatio);
            page.setRecords(list);
        }
        return list;
    }

    @Override
    public List<PcPollutantVo> pcPollutantList() {
        return amProblemFiRatioMapper.pcPollutantList();
    }

    @Override
    public String importAmProblemFiRatioList(List<AmProblemFiRatioImportVo> amProblemFiRatioList) {
        if (CollectionUtil.isEmpty(amProblemFiRatioList)) {
            throw new ServiceException("导入数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        QueryWrapper<PcFactory> factoryQueryWrapper = new QueryWrapper<>();
        QueryWrapper<PcOutlet> pcOutletQueryWrapper = new QueryWrapper<>();
        QueryWrapper<PcPollutant> pcPollutantQueryWrapper = new QueryWrapper<>();
        QueryWrapper<AmProblemFiRatio> amProblemFiRatioQueryWrapper = new QueryWrapper<>();
        for (AmProblemFiRatioImportVo amProblemFiRatioVo : amProblemFiRatioList) {
            String factoryName = amProblemFiRatioVo.getFactoryName();
            String monitoringPointName = amProblemFiRatioVo.getMonitoringPointName();
            String pollName = amProblemFiRatioVo.getPollName();
            String problemRatio = amProblemFiRatioVo.getProblemRatio();
            try{
                if(StringUtils.isEmpty(factoryName)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、企业名称不能为空");
                    continue;
                }
                if(StringUtils.isEmpty(monitoringPointName)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、监测点位名称不能为空");
                    continue;
                }
                if(StringUtils.isEmpty(pollName)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、污染物名称不能为空");
                    continue;
                }
                if(StringUtils.isEmpty(problemRatio)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、预警比例不能为空");
                    continue;
                }

                //判断企业是否存在
                factoryQueryWrapper.clear();
                factoryQueryWrapper.eq("factory_name",factoryName).last("limit 1");
                PcFactory pcFactory = pcFactoryMapper.selectOne(factoryQueryWrapper);
                if(ObjectUtil.isNull(pcFactory)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、企业名称:"+factoryName+"不存在");
                    continue;
                }else{
                    amProblemFiRatioVo.setFactoryId(pcFactory.getId());
                }

                //判断监测点位名称是否存在
                pcOutletQueryWrapper.clear();
                pcOutletQueryWrapper.eq("monitoring_point_name",monitoringPointName).last("limit 1");
                PcOutlet pcOutlet = pcOutletMapper.selectOne(pcOutletQueryWrapper);
                if(ObjectUtil.isNull(pcOutlet)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、监测点位名称:"+monitoringPointName+"不存在");
                    continue;
                }else{
                    amProblemFiRatioVo.setStationId(pcOutlet.getId());
                }

                //判断污染物名称是否存在
                pcPollutantQueryWrapper.clear();
                pcPollutantQueryWrapper.eq("poll_name",pollName).last("limit 1");
                PcPollutant pcPollutant = pcPollutantMapper.selectOne(pcPollutantQueryWrapper);
                if(ObjectUtil.isNull(pcPollutant)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、污染物名称:"+pollName+"不存在");
                    continue;
                }else{
                    amProblemFiRatioVo.setPollId(pcPollutant.getId());
                }

                //判断数据是否存在
                amProblemFiRatioQueryWrapper.clear();
                amProblemFiRatioQueryWrapper.eq("factory_id",pcFactory.getId())
                        .eq("station_id",pcOutlet.getId())
                        .eq("poll_id",pcPollutant.getId()).last("limit 1");
                AmProblemFiRatio amProblemFiRatio = amProblemFiRatioMapper.selectOne(amProblemFiRatioQueryWrapper);
                if(ObjectUtil.isNotNull(amProblemFiRatio)){
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、该条数据已经存在");
                    continue;
                }else{
                    amProblemFiRatioVo.setPollId(pcPollutant.getId());
                }
                //插入
                AmProblemFiRatio amProblemFiRatioBean = new AmProblemFiRatio();
                BeanUtil.copyProperties(amProblemFiRatioVo,amProblemFiRatioBean);
                amProblemFiRatioBean.setCreateTime(new Date());
                amProblemFiRatioBean.setUpdateTime(new Date());
                this.save(amProblemFiRatioBean);
                successNum++;
                successMsg.append("<br/>" + successNum + "、预警比例定制导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "、预警比例定制导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString() + successMsg.insert(0,"<br/> 导入成功！共 " + successNum + " 条，数据如下：").toString()  );
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public AmProblemFiRatioVo selectAmProblemFiRatio(Long id) {
        return baseMapper.selectAmProblemFiRatio(id);
    }

}
