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

import cn.hutool.core.lang.Assert;
import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
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.meterworkcheckfirstactual.mapper.MeterWorkCheckFirstActualMapper;
import com.steerinfo.meterwork.meterworkcheckfirstactual.model.*;
import com.steerinfo.meterwork.meterworkcheckfirstactual.service.IMeterWorkCheckFirstActualService;
import com.steerinfo.meterwork.meterworkdatacount.model.RfidIdentifyAnalysis;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * MeterWorkCheckFirstActual服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2022-12-23 04:50
 * 类描述
 * 修订历史：
 * 日期：2022-12-23
 * 作者：generator
 * 参考：
 * 描述：MeterWorkCheckFirstActual服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterWorkCheckFirstActualService")
public class MeterWorkCheckFirstActualServiceImpl extends BaseServiceImpl<MeterWorkCheckFirstActual, String> implements IMeterWorkCheckFirstActualService {

    @Autowired(required = false)
    private MeterWorkCheckFirstActualMapper meterWorkCheckFirstActualMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Resource
    private MeterWorkCarActualMapper meterWorkCarActualMapper;

    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Resource
    private MeterWorkRailwayActualMapper meterWorkRailwayActualMapper;

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Override
    protected IBaseMapper<MeterWorkCheckFirstActual, String> getMapper() {
        return meterWorkCheckFirstActualMapper;
    }

    @Override
    public RESTfulResult addModel(MeterWorkCheckFirstActual model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            model.setId(PublicMethod.IdCreate(3));
            long startTime = model.getCheckStartTime().getTime();
            long endTime = model.getCheckEndTime().getTime();
            model.setCheckUsedSeconds(new BigDecimal(Math.abs((startTime - endTime)/1000)));


            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            //计量数据管理员
            HashMap<String, Object> meteringAdministratorMap = new HashMap<>();
            meteringAdministratorMap.put("pBaseCode", "001058");
            List<String> meteringAdministratorModels = comBaseInfoMapper.matterNoByBaseCode(meteringAdministratorMap);
            //计量班长
            HashMap<String, Object> gaugerMonitorMap = new HashMap<>();
            gaugerMonitorMap.put("pBaseCode", "001057");
            List<String> gaugerMonitorModels = comBaseInfoMapper.matterNoByBaseCode(gaugerMonitorMap);
            //计量员
            HashMap<String, Object> gaugerMap = new HashMap<>();
            gaugerMap.put("pBaseCode", "001056");
            List<String> gaugerModels = comBaseInfoMapper.matterNoByBaseCode(gaugerMap);

            boolean flag = false;
            if(model.getTypeNo().equals("001002001")) { //001002001：汽车衡、valueFlag：0（一次数据）、1:（结净数据）
                if(model.getValueFlag().equals("0")) {
                    if(meteringAdministratorModels.size() >= 1) {
                        Long count = meteringAdministratorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkCarActualFirst firstModel = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckMan2No(userId);
                                firstModel.setCheckMan2Name(userName);
                                firstModel.setCheckTime2(new Date());
                                firstModel.setCheckFlag2("2");
                                firstModel.setCheckMemo2(model.getCheckResult());
                                meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerMonitorModels.size() >= 1){
                        Long count = gaugerMonitorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkCarActualFirst firstModel = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckMan1No(userId);
                                firstModel.setCheckMan1Name(userName);
                                firstModel.setCheckTime1(new Date());
                                firstModel.setCheckFlag1("2");
                                firstModel.setCheckMemo1(model.getCheckResult());
                                meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false &&gaugerModels.size() >= 1) {
                        Long count = gaugerModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkCarActualFirst firstModel = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getCheckId());
                            if (firstModel != null) {
                                firstModel.setCheckManNo(userId);
                                firstModel.setCheckManName(userName);
                                firstModel.setCheckTime(new Date());
                                firstModel.setCheckFlag("2");
                                firstModel.setCheckMemo(model.getCheckResult());
                                meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                }else if(model.getValueFlag().equals("1")) {
                    if(meteringAdministratorModels.size() >= 1) {
                        Long count = meteringAdministratorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkCarActual actualModel = meterWorkCarActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckMan2No(userId);
                                actualModel.setCheckMan2Name(userName);
                                actualModel.setCheckTime2(new Date());
                                actualModel.setCheckFlag2("2");
                                actualModel.setCheckMemo2(model.getCheckResult());
                                meterWorkCarActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerMonitorModels.size() >= 1){
                        Long count = gaugerMonitorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkCarActual actualModel = meterWorkCarActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckMan1No(userId);
                                actualModel.setCheckMan1Name(userName);
                                actualModel.setCheckTime1(new Date());
                                actualModel.setCheckFlag1("2");
                                actualModel.setCheckMemo1(model.getCheckResult());
                                meterWorkCarActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerModels.size() >= 1) {
                        Long count = gaugerModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkCarActual actualModel = meterWorkCarActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckManNo(userId);
                                actualModel.setCheckManName(userName);
                                actualModel.setCheckTime(new Date());
                                actualModel.setCheckFlag("2");
                                actualModel.setCheckMemo(model.getCheckResult());
                                meterWorkCarActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                }
            }else if(model.getTypeNo().equals("001019002")||model.getTypeNo().equals("001019004")) {//动态轨道衡、动态铁水衡
                if(model.getValueFlag().equals("0")) {
                    if(meteringAdministratorModels.size() >= 1) {
                        Long count = meteringAdministratorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayReceived firstModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckMan2No(userId);
                                firstModel.setCheckMan2Name(userName);
                                firstModel.setCheckTime2(new Date());
                                firstModel.setCheckFlag2("2");
                                firstModel.setCheckMemo2(model.getCheckResult());
                                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerMonitorModels.size() >= 1){
                        Long count = gaugerMonitorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayReceived firstModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckMan1No(userId);
                                firstModel.setCheckMan1Name(userName);
                                firstModel.setCheckTime1(new Date());
                                firstModel.setCheckFlag1("2");
                                firstModel.setCheckMemo1(model.getCheckResult());
                                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerModels.size() >= 1) {
                        Long count = gaugerModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayReceived firstModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckManNo(userId);
                                firstModel.setCheckManName(userName);
                                firstModel.setCheckTime(new Date());
                                firstModel.setCheckFlag("2");
                                firstModel.setCheckMemo(model.getCheckResult());
                                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                }else if(model.getValueFlag().equals("1")) {

                    if(meteringAdministratorModels.size() >= 1) {
                        Long count = meteringAdministratorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckMan2No(userId);
                                actualModel.setCheckMan2Name(userName);
                                actualModel.setCheckTime2(new Date());
                                actualModel.setCheckFlag2("2");
                                actualModel.setCheckMemo2(model.getCheckResult());
                                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerMonitorModels.size() >= 1){
                        Long count = gaugerMonitorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckMan1No(userId);
                                actualModel.setCheckMan1Name(userName);
                                actualModel.setCheckTime1(new Date());
                                actualModel.setCheckFlag1("2");
                                actualModel.setCheckMemo1(model.getCheckResult());
                                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerModels.size() >= 1) {
                        Long count = gaugerModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckManNo(userId);
                                actualModel.setCheckManName(userName);
                                actualModel.setCheckTime(new Date());
                                actualModel.setCheckFlag("2");
                                actualModel.setCheckMemo(model.getCheckResult());
                                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                }
            }else {
                if(model.getValueFlag().equals("0")) {
                    if(meteringAdministratorModels.size() >= 1) {
                        Long count = meteringAdministratorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActFirst firstModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckMan2No(userId);
                                firstModel.setCheckMan2Name(userName);
                                firstModel.setCheckTime2(new Date());
                                firstModel.setCheckFlag2("2");
                                firstModel.setCheckMemo2(model.getCheckResult());
                                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerMonitorModels.size() >= 1){
                        Long count = gaugerMonitorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActFirst firstModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckMan1No(userId);
                                firstModel.setCheckMan1Name(userName);
                                firstModel.setCheckTime1(new Date());
                                firstModel.setCheckFlag1("2");
                                firstModel.setCheckMemo1(model.getCheckResult());
                                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerModels.size() >= 1) {
                        Long count = gaugerModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActFirst firstModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getCheckId());
                            if(firstModel != null) {
                                firstModel.setCheckManNo(userId);
                                firstModel.setCheckManName(userName);
                                firstModel.setCheckTime(new Date());
                                firstModel.setCheckFlag("2");
                                firstModel.setCheckMemo(model.getCheckResult());
                                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(firstModel);
                            }
                            flag = true;
                        }
                    }
                }else if(model.getValueFlag().equals("1")) {

                    if(meteringAdministratorModels.size() >= 1) {
                        Long count = meteringAdministratorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckMan2No(userId);
                                actualModel.setCheckMan2Name(userName);
                                actualModel.setCheckTime2(new Date());
                                actualModel.setCheckFlag2("2");
                                actualModel.setCheckMemo2(model.getCheckResult());
                                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerMonitorModels.size() >= 1){
                        Long count = gaugerMonitorModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckMan1No(userId);
                                actualModel.setCheckMan1Name(userName);
                                actualModel.setCheckTime1(new Date());
                                actualModel.setCheckFlag1("2");
                                actualModel.setCheckMemo1(model.getCheckResult());
                                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                    if(flag == false && gaugerModels.size() >= 1) {
                        Long count = gaugerModels.stream().filter(item -> item.equals(userId)).count();
                        if(count >= 1) {
                            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(model.getCheckId());
                            if(actualModel != null) {
                                actualModel.setCheckManNo(userId);
                                actualModel.setCheckManName(userName);
                                actualModel.setCheckTime(new Date());
                                actualModel.setCheckFlag("2");
                                actualModel.setCheckMemo(model.getCheckResult());
                                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                            }
                            flag = true;
                        }
                    }
                }
            }
            if(flag == false) {
                rm.setMessage("该"+ userName + "账号不能审核，没有审核权限！！");
                return rm;
            }
            model.setCheckManNo(userId);
            model.setCheckManName(userName);
            meterWorkCheckFirstActualMapper.insert(model);

            rm.setSucceed(true);
            rm.setData(model);
            rm.setMessage("操作成功");
            rm.setCode("200");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }

    @Override
    public List<HashMap> carFirstActualImgDataList(HashMap<String, Object> params) {
        // （001002001：汽车衡、001019001：静态轨道衡、001019002:动态轨道衡、001019003:静态铁水轨道衡、001019004:动态铁水轨道衡、001019005:吊钩秤）
        String typeNo = params.get("typeNo") != null? params.get("typeNo").toString(): "";
        // 0:一次数据、1:净重数据
        String valueFlag = params.get("valueFlag") != null? params.get("valueFlag").toString(): "";
        //站点信息  baseSpotNo、baseSpotName
        List<MeterBaseSpotInfo> spotList = meterWorkCheckFirstActualMapper.spotInfoList(params);
        //一次数据查询
        String startTime = params.get("startTime").toString();
        String endTime = params.get("endTime").toString();
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        List<MeterWorkCarActualFirst> firstModels = meterWorkCheckFirstActualMapper.carFirstDataList(obj);
        List<MeterWorkCarActual> actualModels = meterWorkCheckFirstActualMapper.carActualDataList(obj);
        //一次数据与核查表左连接
        List<FirstActualImgData> firstImgDatamodels = meterWorkCheckFirstActualMapper.carFirstImgDataList(params);
        //净重数据与核查表左连接
        List<FirstActualImgData> ActualImgDatamodels = meterWorkCheckFirstActualMapper.carActualImgDataList(params);
        List<HashMap> mapList = new ArrayList<>();
        if(spotList.size() >= 1) {
            for(MeterBaseSpotInfo spotModel: spotList) {
                long countFirstData = firstModels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                        && StringUtils.isNotEmpty(item.getBaseSpotName())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                        && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                        && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                long countCheckFirstDataYes = 0L;
                long countCheckFirstDataNo = 0L;
                if(firstImgDatamodels.size() >= 1) {
                    countCheckFirstDataNo = firstImgDatamodels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                            && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                            && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                }
                countCheckFirstDataYes = countFirstData - countCheckFirstDataNo;

                long countActualData = actualModels.stream().filter(item -> StringUtils.isNotEmpty(item.getNetSpot3No())
                        && StringUtils.isNotEmpty(item.getNetSpot3Name())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                        && item.getNetSpot3No().equals(spotModel.getBaseSpotNo())
                        && item.getNetSpot3Name().equals(spotModel.getBaseSpotName())).count();;
                long countCheckActualDataYes = 0L;
                long countCheckActualDataNo = 0L;
                if(ActualImgDatamodels.size() >= 1) {
                    countCheckActualDataNo = ActualImgDatamodels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                            && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                            && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                }
                countCheckActualDataYes = countActualData - countCheckActualDataNo;

                HashMap<String, Object> map = new HashMap<>();
                map.put("baseSpotNo", spotModel.getBaseSpotNo());
                map.put("baseSpotName", spotModel.getBaseSpotName());
                map.put("countFirstData", countFirstData);
                map.put("countCheckFirstDataYes", countCheckFirstDataYes);
                map.put("countCheckFirstDataNo", countCheckFirstDataNo);
                map.put("countActualData", countActualData);
                map.put("countCheckActualDataYes", countCheckActualDataYes);
                map.put("countCheckActualDataNo", countCheckActualDataNo);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public PageList<CarActualCountCheckNum> carActualCountCheckNum(HashMap<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<CarActualCountCheckNum> rows = meterWorkCheckFirstActualMapper.carActualCountCheckNum(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<CarFirstCountCheckNum> carFirstCountCheckNum(HashMap<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<CarFirstCountCheckNum> rows = meterWorkCheckFirstActualMapper.carFirstCountCheckNum(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<MeterWorkCheckFirstActualTemp> likeByDesc(HashMap<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("createManNoStr") != null) {
            String[] manNoStr = params.get("createManNoStr").toString().split(",");
            params.put("manNoStr", manNoStr);
        }
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<MeterWorkCheckFirstActualTemp> rows = meterWorkCheckFirstActualMapper.likeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<CarFirstCountCheckNum> carFirstLikeByDesc(HashMap<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<CarFirstCountCheckNum> rows = meterWorkCheckFirstActualMapper.carFirstLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<CarActualCountCheckNum> carActualLikeByDesc(HashMap<String, Object> params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<CarActualCountCheckNum> rows = meterWorkCheckFirstActualMapper.carActualLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<HashMap> railwayFirstImgDataList(HashMap params) {

        // （001002001：汽车衡、001019001：静态轨道衡、001019002:动态轨道衡、001019003:静态铁水轨道衡、001019004:动态铁水轨道衡、001019005:吊钩秤）
        //String typeNo = params.get("typeNo") != null? params.get("typeNo").toString(): "";
        //// 0:一次数据、1:净重数据
        //String valueFlag = params.get("valueFlag") != null? params.get("valueFlag").toString(): "";
        //站点信息  baseSpotNo、baseSpotName
        List<MeterBaseSpotInfo> spotList = meterWorkCheckFirstActualMapper.railwaySpotInfoList(params);
        //一次数据查询
        String startTime = params.get("startTime").toString();
        String endTime = params.get("endTime").toString();
        String railwayTypeNo = params.get("railwayTypeNo").toString();
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        obj.put("railwayTypeNo",railwayTypeNo);
        List<MeterWorkRailwayActFirst> firstModels = meterWorkCheckFirstActualMapper.railwayFirstDataList(obj);
        List<MeterWorkRailwayActual> actualModels = meterWorkCheckFirstActualMapper.railwayActualDataList(obj);
        //一次数据与核查表左连接
        List<RailwayFirstActualImgData> firstImgDatamodels = meterWorkCheckFirstActualMapper.railwayFirstImgDataList(params);
        //净重数据与核查表左连接
        List<RailwayFirstActualImgData> ActualImgDatamodels = meterWorkCheckFirstActualMapper.railwayActualImgDataList(params);
        List<HashMap> mapList = new ArrayList<>();
        if(spotList.size() >= 1) {
            for(MeterBaseSpotInfo spotModel: spotList) {
                long countFirstData = firstModels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                        && StringUtils.isNotEmpty(item.getBaseSpotName())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                        && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                        && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                long countCheckFirstDataYes = 0L;
                long countCheckFirstDataNo = 0L;
                if(firstImgDatamodels.size() >= 1) {
                    countCheckFirstDataNo = firstImgDatamodels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                            && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                            && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                }
                countCheckFirstDataYes = countFirstData - countCheckFirstDataNo;

                long countActualData = actualModels.stream().filter(item -> StringUtils.isNotEmpty(item.getNetSpot3No())
                        && StringUtils.isNotEmpty(item.getNetSpot3Name())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                        && item.getNetSpot3No().equals(spotModel.getBaseSpotNo())
                        && item.getNetSpot3Name().equals(spotModel.getBaseSpotName())).count();;
                long countCheckActualDataYes = 0L;
                long countCheckActualDataNo = 0L;
                if(ActualImgDatamodels.size() >= 1) {
                    countCheckActualDataNo = ActualImgDatamodels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                            && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                            && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                }
                countCheckActualDataYes = countActualData - countCheckActualDataNo;

                HashMap<String, Object> map = new HashMap<>();
                map.put("baseSpotNo", spotModel.getBaseSpotNo());
                map.put("baseSpotName", spotModel.getBaseSpotName());
                map.put("countFirstData", countFirstData);
                map.put("countCheckFirstDataYes", countCheckFirstDataYes);
                map.put("countCheckFirstDataNo", countCheckFirstDataNo);
                map.put("countActualData", countActualData);
                map.put("countCheckActualDataYes", countCheckActualDataYes);
                map.put("countCheckActualDataNo", countCheckActualDataNo);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public PageList<RailwayFirstCountCheckNum> railwayFirstCountCheckNum(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<RailwayFirstCountCheckNum> rows = meterWorkCheckFirstActualMapper.railwayFirstCountCheckNum(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<RailwayFirstCountCheckNum> railwayFirstLikeByDesc(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<RailwayFirstCountCheckNum> rows = meterWorkCheckFirstActualMapper.railwayFirstLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<RailwayActualCountCheckNum> railwayActualCountCheckNum(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<RailwayActualCountCheckNum> rows = meterWorkCheckFirstActualMapper.railwayActualCountCheckNum(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<RailwayActualCountCheckNum> railwayActualLikeByDesc(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<RailwayActualCountCheckNum> rows = meterWorkCheckFirstActualMapper.railwayActualLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<MeterWorkCheckFirstActualRailwayTemp> railwayLikeByDesc(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("createManNoStr") != null) {
            String[] manNoStr = params.get("createManNoStr").toString().split(",");
            params.put("manNoStr", manNoStr);
        }
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<MeterWorkCheckFirstActualRailwayTemp> rows = meterWorkCheckFirstActualMapper.railwayLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<RailwayFirstCountCheckNum> railwayDynamicsFirstCountCheckNum(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<RailwayFirstCountCheckNum> rows = meterWorkCheckFirstActualMapper.railwayDynamicsFirstCountCheckNum(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<RailwayFirstCountCheckNum> railwayDynamicsFirstLikeByDesc(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<RailwayFirstCountCheckNum> rows = meterWorkCheckFirstActualMapper.railwayDynamicsFirstLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<HashMap> railwayDynamicsFirstImgDataList(HashMap params) {
        // （001002001：汽车衡、001019001：静态轨道衡、001019002:动态轨道衡、001019003:静态铁水轨道衡、001019004:动态铁水轨道衡、001019005:吊钩秤）
        //String typeNo = params.get("typeNo") != null? params.get("typeNo").toString(): "";
        //// 0:一次数据、1:净重数据
        //String valueFlag = params.get("valueFlag") != null? params.get("valueFlag").toString(): "";
        //站点信息  baseSpotNo、baseSpotName
        List<MeterBaseSpotInfo> spotList = meterWorkCheckFirstActualMapper.railwayDynamicsSpotInfoList(params);
        //一次数据查询
        String startTime = params.get("startTime").toString();
        String endTime = params.get("endTime").toString();
        String railwayTypeNo = params.get("railwayTypeNo").toString();
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        obj.put("railwayTypeNo",railwayTypeNo);
        List<MeterWorkRailwayReceived> firstModels = meterWorkCheckFirstActualMapper.railwayDynamicsFirstDataList(obj);
        List<MeterWorkRailwayActual> actualModels = meterWorkCheckFirstActualMapper.railwayActualDataList(obj);
        //一次数据与核查表左连接
        List<RailwayFirstActualImgData> firstImgDatamodels = meterWorkCheckFirstActualMapper.railwayDynamicsFirstImgDataList(params);
        //净重数据与核查表左连接
        List<RailwayFirstActualImgData> ActualImgDatamodels = meterWorkCheckFirstActualMapper.railwayActualImgDataList(params);
        List<HashMap> mapList = new ArrayList<>();
        if(spotList.size() >= 1) {
            for(MeterBaseSpotInfo spotModel: spotList) {
                long countFirstData = firstModels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                        && StringUtils.isNotEmpty(item.getBaseSpotName())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                        && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                        && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                long countCheckFirstDataYes = 0L;
                long countCheckFirstDataNo = 0L;
                if(firstImgDatamodels.size() >= 1) {
                    countCheckFirstDataNo = firstImgDatamodels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                            && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                            && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                }
                countCheckFirstDataYes = countFirstData - countCheckFirstDataNo;

                long countActualData = actualModels.stream().filter(item -> StringUtils.isNotEmpty(item.getNetSpot3No())
                        && StringUtils.isNotEmpty(item.getNetSpot3Name())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                        && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                        && item.getNetSpot3No().equals(spotModel.getBaseSpotNo())
                        && item.getNetSpot3Name().equals(spotModel.getBaseSpotName())).count();;
                long countCheckActualDataYes = 0L;
                long countCheckActualDataNo = 0L;
                if(ActualImgDatamodels.size() >= 1) {
                    countCheckActualDataNo = ActualImgDatamodels.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotNo())
                            && StringUtils.isNotEmpty(spotModel.getBaseSpotName())
                            && item.getBaseSpotNo().equals(spotModel.getBaseSpotNo())
                            && item.getBaseSpotName().equals(spotModel.getBaseSpotName())).count();
                }
                countCheckActualDataYes = countActualData - countCheckActualDataNo;

                HashMap<String, Object> map = new HashMap<>();
                map.put("baseSpotNo", spotModel.getBaseSpotNo());
                map.put("baseSpotName", spotModel.getBaseSpotName());
                map.put("countFirstData", countFirstData);
                map.put("countCheckFirstDataYes", countCheckFirstDataYes);
                map.put("countCheckFirstDataNo", countCheckFirstDataNo);
                map.put("countActualData", countActualData);
                map.put("countCheckActualDataYes", countCheckActualDataYes);
                map.put("countCheckActualDataNo", countCheckActualDataNo);
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public PageList<MeterWorkCheckFirstActualRailwayTemp> railwayDynamicsLikeByDesc(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        if(params.get("createManNoStr") != null) {
            String[] manNoStr = params.get("createManNoStr").toString().split(",");
            params.put("manNoStr", manNoStr);
        }
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<MeterWorkCheckFirstActualRailwayTemp> rows = meterWorkCheckFirstActualMapper.railwayDynamicsLikeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }
}
