package com.steerinfo.meterwork.meterworkparammap.service.impl;

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.meterwork.meterworkcaractual.mapper.MeterWorkCarActualMapper;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkmonitor.mapper.MeterWorkMonitorMapper;
import com.steerinfo.meterwork.meterworkmonitor.model.MeterWorkMonitor;
import com.steerinfo.meterwork.meterworkparammap.controller.MeterWorkParamMap;
import com.steerinfo.meterwork.meterworkparammap.mapper.MeterWorkParamMapMapper;
import com.steerinfo.meterwork.meterworkparammap.service.IMeterWorkParamMapService;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.StringUtils;
import io.micrometer.core.instrument.Meter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("MeterWorkParamService")
public class MeterWorkParamMapService implements IMeterWorkParamMapService {

    @Resource
    private MeterWorkParamMapMapper meterWorkParamMapMapper;

    @Resource
    private MeterWorkMonitorMapper meterWorkMonitorMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Resource
    private MeterWorkCarActualMapper meterWorkCarActualMapper;

    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Override
    public RESTfulResult combinationByParams(HashMap<String, Object> params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            String baseSpotNo = params.get("baseSpotNo") == null ? "": params.get("baseSpotNo").toString();
            String carNo = params.get("carNo") == null ? "": params.get("carNo").toString();
            String predictionNo = params.get("predicitonNo") == null ? "": params.get("predicitonNo").toString();
            HashMap<String, Object> paramMap = new HashMap<>();

            if(StringUtils.isNotEmpty(baseSpotNo)) {
                //根据称点查询监控信息
                HashMap<String, Object> spotInfoMap = new HashMap<>();
                spotInfoMap.put("pointNo", baseSpotNo);
                List<MeterWorkMonitor> spotInfoList = meterWorkMonitorMapper.selectByParameters(spotInfoMap);
                if(spotInfoList.size() >= 1){
                    RESTfulResult rmSpot = new RESTfulResult();
                    rmSpot.setData(spotInfoList);
                    paramMap.put("spotInfoList", rmSpot);
                }else {
                    paramMap.put("spotInfoList", null);
                }
            }else {
                paramMap.put("spotInfoList", null);
            }

            if(StringUtils.isNotEmpty(carNo)) {
                //计量实绩
                HashMap<String, Object> actualInfoMap = new HashMap<>();
                actualInfoMap.put("carNo", carNo);
//                List<MeterWorkCarActual> actualInfoList = meterWorkCarActualMapper.selectByMap(actualInfoMap);
                List<MeterWorkCarActual> actualInfoList = meterWorkCarActualMapper.selectByMapRe(actualInfoMap);
                if(actualInfoList.size() >= 1){
                    RESTfulResult rmActual = new RESTfulResult();
                    rmActual.setData(actualInfoList);
                    paramMap.put("actualInfoList", rmActual);
                }else {
                    paramMap.put("actualInfoList", null);
                }

                // 根据车号查询委托表中未使用和使用中的委托
                // 状态（0：未配车；1：未使用；2：已使用；3：已作废；4：正使用）
                HashMap<String, Object> scaleInfoMap = new HashMap<>();
                scaleInfoMap.put("carNo", carNo);
                List<PreTrackScale> scaleInfoList = preTrackScaleMapper.selectBy(scaleInfoMap);
                if(scaleInfoList.size() >= 1){
                    RESTfulResult rmScale = new RESTfulResult();
                    rmScale.setData(scaleInfoList);
                    paramMap.put("scaleInfoList", rmScale);
                }else {
                    paramMap.put("scaleInfoList", null);
                }

                // VALUE_FLAG 状态（0=作废，1=有效(已匹配)，2=结净，3=未匹配）
                // 根据车号查询最近的一次计量数据
                List<MeterWorkCarActualFirst> firstInfoList = meterWorkCarActualFirstMapper.selectByCarNo(carNo);
                if(firstInfoList.size() >= 1){
                    RESTfulResult rmFirst = new RESTfulResult();
                    rmFirst.setData(firstInfoList);
                    paramMap.put("firstInfoList", rmFirst);
                }else {
                    paramMap.put("firstInfoList", null);
                }
            }else {
                paramMap.put("actualInfoList", null);
                paramMap.put("scaleInfoList", null);
                paramMap.put("firstInfoList", null);
            }

            if(StringUtils.isNotEmpty(predictionNo)) {
                // 签发时间比较
                // 通过上次结净记录，找到该委托的签发时间
                HashMap<String, Object> scaleNewInfoMap = new HashMap<>();
                scaleNewInfoMap.put("predictionNo", predictionNo);
                List<PreTrackScale> scaleNewInfoList = preTrackScaleMapper.selectBy(scaleNewInfoMap);
                if(scaleNewInfoList.size() >= 1){
                    RESTfulResult rmScaleNew = new RESTfulResult();
                    rmScaleNew.setData(scaleNewInfoList);
                    paramMap.put("scaleNewInfoList", rmScaleNew);
                }else {
                    paramMap.put("scaleNewInfoList", null);
                }
            }else {
                paramMap.put("scaleNewInfoList", null);
            }
            rm.setSucceed(true);
            rm.setCode("200");
            rm.setData(paramMap);
            rm.setMessage("操作成功");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，" + e.getMessage());
        }
        return rm;
    }
}
