package cn.cecep.talroad.data.analyse.task.service;

import cn.cecep.talroad.domain.AmProblemFi;
import cn.cecep.talroad.domain.analysis.SRaSelfMonitoringData;
import cn.cecep.talroad.mapper.AmProblemFiMapper;
import cn.cecep.talroad.mapper.analysis.SRaSelfMonitoringDataMapper;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class OutletMonitorGasTaskService implements TaskService {



    @Autowired
    private AmProblemFiMapper amProblemFiMapper;

    @Autowired
    private SRaSelfMonitoringDataMapper sRaSelfMonitoringDataMapper;


    @Override
    public Object mainProcess(Object o) {
        Timestamp startDate = new Timestamp(1687273132000L);
        Timestamp endDate = new Timestamp(1688055248000L);
//        LambdaQueryWrapper<SRaSelfMonitoringData> lambdaQuery = Wrappers.lambdaQuery(SRaSelfMonitoringData.class);
//        lambdaQuery.ge(SRaSelfMonitoringData::getCreateTime, startDate);
//        lambdaQuery.le(SRaSelfMonitoringData::getCreateTime, endDate);
        List<SRaSelfMonitoringData> monitoringDataList = sRaSelfMonitoringDataMapper.queryList(startDate,endDate);
        Map<String, List<SRaSelfMonitoringData>> listMap = monitoringDataList.stream().filter(e -> "1".equals(e.getExceedingStandard()))
                .collect(Collectors.groupingBy(SRaSelfMonitoringData::getFactoryId));
        List<AmProblemFi> fis = encapsulatedData(listMap);

        if (CollectionUtil.isEmpty(fis)){
            return true;
        }
        amProblemFiMapper.insertAmProblemFis(fis);
        return true;
    }

    private List<AmProblemFi> encapsulatedData(Map<String, List<SRaSelfMonitoringData>> listMap) {
        ArrayList<AmProblemFi> amProblemFis = new ArrayList<>();
        listMap.forEach((k,v)->{
            AmProblemFi amProblemFi = new AmProblemFi();
            amProblemFi.setFactoryId(k);
            String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
            amProblemFi.setId(uuid);
            amProblemFi.setProblemSources("1");
            StringBuffer msgs = new StringBuffer();
            v.forEach(e->{
                String msg = getMessage(e);
                msgs.append(msg).append("/");
            });
            amProblemFi.setProblemDetails(msgs.toString());
            amProblemFi.setType(1);
            amProblemFis.add(amProblemFi);
        });
        return amProblemFis;
    }


    protected String getMessage(SRaSelfMonitoringData data){
        return String.format("%s-%s%s手工监测%s浓度为%s，超过许可浓度限值%s",
                data.getFactoryName(),
                data.getMonitoringName(),
                new SimpleDateFormat("yyyy年MM月dd日").format(data.getMonitorTime()),
                data.getPollutantsName(),
                data.getActualConcentration(),
                data.getUpperValue()+"-"+data.getLowerValue());
    }


}
