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

import com.rabbitmq.client.Return;
import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
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.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcarmetertime.model.*;
import com.steerinfo.meterwork.meterworkcarmetertime.mapper.MeterWorkCarMeterTimeMapper;
import com.steerinfo.meterwork.meterworkcarmetertime.service.IMeterWorkCarMeterTimeService;
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 javax.annotation.Resource;
import java.util.*;
import java.math.BigDecimal;

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

    @Resource
    private MeterWorkCarMeterTimeMapper meterWorkCarMeterTimeMapper;

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Override
    protected IBaseMapper<MeterWorkCarMeterTime, String> getMapper() {
        return meterWorkCarMeterTimeMapper;
    }

    @Override
    public List<GroupTimeVo> getGroupByTime(HashMap meterWorkCarMeterTime) {
        return meterWorkCarMeterTimeMapper.selectGroupByTime(meterWorkCarMeterTime);
    }

    @Override
    public List<PointDetailsVo> getPointDetails(PointDetailsBo pointDetailsBo) {

        return meterWorkCarMeterTimeMapper.selectPointDetails(pointDetailsBo);
    }

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

    @Override
    public List<Map<String, Object>> getCarMeterTimeAndCarFirstCount(HashMap parmas) {
        //汇总站点
        List<CarMeterTimeAndCarFirst> spotGroupBy = meterWorkCarMeterTimeMapper.getSpotGroupBy(parmas);
        //查询班组
        //计量一班
        HashMap<String, Object> hashMap1=new HashMap<>();
        hashMap1.put("pBaseCode","001060001");
        List<String> list1 = comBaseInfoMapper.matterNoByBaseCode(hashMap1);
        //计量二班
        HashMap<String, Object> hashMap2=new HashMap<>();
        hashMap2.put("pBaseCode","001060002");
        List<String> list2 = comBaseInfoMapper.matterNoByBaseCode(hashMap2);
        //计量三班
        HashMap<String, Object> hashMap3=new HashMap<>();
        hashMap3.put("pBaseCode","001060003");
        List<String> list3 = comBaseInfoMapper.matterNoByBaseCode(hashMap3);
        //计量四班
        HashMap<String, Object> hashMap4=new HashMap<>();
        hashMap4.put("pBaseCode","001060004");
        List<String> list4 = comBaseInfoMapper.matterNoByBaseCode(hashMap4);

        List<CarMeterTimeAndCarFirst> lists = meterWorkCarMeterTimeMapper.getCarMeterTimeAndCarFirstCount(parmas);

        lists.stream().filter(item->list1.contains(item.getCreateManNo()));

        List<Map<String, Object>> listMessage=new ArrayList<>();

        if (lists==null||list1.size()==0) {
            return listMessage;
        }

        for (CarMeterTimeAndCarFirst carMeterTimeAndCarFirst : spotGroupBy) {
            //总车数
            long count = lists.stream().filter(item -> StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())).count();


            //计量一班总车数
            long sumCount1 = (list1==null||list1.size()==0)?0: lists.stream().filter(item -> list1.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                            &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                            && StringUtils.isNotEmpty(item.getBaseSpotName())
                            && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                            && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                            && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                            && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())).count();
            //计量二班总车数
            long sumCount2 = (list1==null||list1.size()==0)?0: lists.stream().filter(item -> list2.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())).count();
            //计量三班总车数
            long sumCount3 = (list1==null||list1.size()==0)?0: lists.stream().filter(item -> list3.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())).count();
            //计量四班总车数
            long sumCount4 = (list1==null||list1.size()==0)?0: lists.stream().filter(item -> list4.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())).count();


            //查询每个站点包含计量一班车数总时间小于60秒
            //查询计量一班的明细
            //parmas.put("createManNo",list1);
            //List<CarMeterTimeAndCarFirst> lists1 = meterWorkCarMeterTimeMapper.carMeterTimeAndCarFirstBySpot(parmas);
            long count1 = (list1==null||list1.size()==0)?0: lists.stream().filter(item -> list1.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())<=60).count();
            //查询计量二班的明细
            //parmas.put("createManNo",list2);
            //List<CarMeterTimeAndCarFirst> lists2 = meterWorkCarMeterTimeMapper.carMeterTimeAndCarFirstBySpot(parmas);
            long count2 = (list2==null||list2.size()==0)?0: lists.stream().filter(item -> list2.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())<=60).count();
            //查询计量三班的明细
            //parmas.put("createManNo",list3);
            //List<CarMeterTimeAndCarFirst> lists3 = meterWorkCarMeterTimeMapper.carMeterTimeAndCarFirstBySpot(parmas);
            long count3 = (list3==null||list3.size()==0)?0: lists.stream().filter(item -> list3.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())<=60).count();
            //查询计量四班的明细
            //parmas.put("createManNo",list4);
            //List<CarMeterTimeAndCarFirst> lists4 = meterWorkCarMeterTimeMapper.carMeterTimeAndCarFirstBySpot(parmas);
            long count4 = (list4==null||list4.size()==0)?0: lists.stream().filter(item -> list4.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())<=60).count();

            //总车数
            long sumCount = count1+count2+count3+count4;
            //60s及时率
            String v =  String.format("%.2f",(sumCount* 1.0) / (count * 1.0)*100) ;


            //查询每个站点包含计量一班车数总时间大于=60秒小于120
            //查询计量一班的明细
            long count21 = (list1==null||list1.size()==0)?0: lists.stream().filter(item ->list1.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>60&&Integer.parseInt(item.getTotalNum())<=120).count();
            //查询计量二班的明细
            long count22 = (list2==null||list2.size()==0)?0: lists.stream().filter(item ->list2.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>60&&Integer.parseInt(item.getTotalNum())<=120).count();
            //查询计量三班的明细
            long count23 = (list3==null||list3.size()==0)?0: lists.stream().filter(item ->list3.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>60&&Integer.parseInt(item.getTotalNum())<=120).count();
            //查询计量四班的明细
            long count24 = (list4==null||list4.size()==0)?0: lists.stream().filter(item ->list4.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>60&&Integer.parseInt(item.getTotalNum())<=120).count();
            //总车数
            long sum2Count = count21+count22+count23+count24;
            String v2 =  String.format("%.2f",(sum2Count* 1.0) / (count * 1.0)*100);

            //查询每个站点包含计量一班车数总时间大于=60秒小于120
            //查询计量一班的明细
            long count31 = (list1==null||list1.size()==0)?0: lists.stream().filter(item -> list1.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>120).count();
            //查询计量二班的明细
            long count32 = (list2==null||list2.size()==0)?0: lists.stream().filter(item -> list2.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>120).count();
            //查询计量三班的明细
            long count33 = (list3==null||list3.size()==0)?0: lists.stream().filter(item -> list3.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>120).count();
            //查询计量四班的明细
            long count34 = (list4==null||list4.size()==0)?0: lists.stream().filter(item -> list4.contains(item.getCreateManNo())//判断班组中的人是否存在于班组中
                    &&StringUtils.isNotEmpty(item.getBaseSpotNo())
                    && StringUtils.isNotEmpty(item.getBaseSpotName())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && StringUtils.isNotEmpty(carMeterTimeAndCarFirst.getBaseSpotName())
                    && item.getBaseSpotNo().equals(carMeterTimeAndCarFirst.getBaseSpotNo())
                    && item.getBaseSpotName().equals(carMeterTimeAndCarFirst.getBaseSpotName())
                    &&Integer.parseInt(item.getTotalNum())>120).count();
            //总车数
            long sum3Count = count31+count32+count33+count34;
            String v3 = String.format("%.2f",count==0?0:(sum3Count* 1.0) / (count * 1.0)*100) ;

            //一班<60及时率
            String V11 = String.format("%.2f",sumCount1==0?0:(count1* 1.0) / (sumCount1 * 1.0)*100) ;
            //二班<60及时率
            String V12 = String.format("%.2f",sumCount2==0?0:(count2* 1.0) / (sumCount2 * 1.0)*100) ;
            //三班<60及时率
            String V13 = String.format("%.2f",sumCount3==0?0:(count3* 1.0) / (sumCount3 * 1.0)*100) ;
            //四班<60及时率
            String V14 = String.format("%.2f",sumCount4==0?0:(count4* 1.0) / (sumCount4 * 1.0)*100) ;


            //一班120-60及时率
            String V21 = String.format("%.2f",sumCount1==0?0:(count21* 1.0) / (sumCount1 * 1.0)*100) ;
            //二班120-60及时率
            String V22 = String.format("%.2f",sumCount2==0?0:(count22* 1.0) / (sumCount2 * 1.0)*100) ;
            //三班120-60及时率
            String V23 = String.format("%.2f",sumCount3==0?0:(count23* 1.0) / (sumCount3 * 1.0)*100) ;
            //四班120-60及时率
            String V24 = String.format("%.2f",sumCount4==0?0:(count24* 1.0) / (sumCount4 * 1.0)*100) ;


            //一班>120及时率
            String V31 = String.format("%.2f",sumCount1==0?0:(count31* 1.0) / (sumCount1 * 1.0)*100) ;
            //二班>120及时率
            String V32 = String.format("%.2f",sumCount2==0?0:(count32* 1.0) / (sumCount2 * 1.0)*100) ;
            //三班>120及时率
            String V33 = String.format("%.2f",sumCount3==0?0:(count33* 1.0) / (sumCount3 * 1.0)*100) ;
            //四班>120及时率
            String V34 = String.format("%.2f",sumCount4==0?0:(count34* 1.0) / (sumCount4 * 1.0)*100) ;

            if(sumCount1!=(count1+count21+count31)){
                System.out.println("1:"+carMeterTimeAndCarFirst.getBaseSpotName());
            }

            if(sumCount2!=(count2+count22+count32)){
                System.out.println("2:"+carMeterTimeAndCarFirst.getBaseSpotName());
            }

            if(sumCount3!=(count3+count23+count33)){
                System.out.println("3:"+carMeterTimeAndCarFirst.getBaseSpotName());
            }

            if(sumCount4!=(count4+count24+count34)){
                System.out.println("4:"+carMeterTimeAndCarFirst.getBaseSpotName());
            }

            HashMap<String,Object> map =new HashMap<>();
            map.put("baseSpotNo",carMeterTimeAndCarFirst.getBaseSpotNo());
            map.put("baseSpotName",carMeterTimeAndCarFirst.getBaseSpotName());
            map.put("count",count);
            map.put("count1",count1);
            map.put("count2",count2);
            map.put("count3",count3);
            map.put("count4",count4);
            map.put("count21",count21);
            map.put("count22",count22);
            map.put("count23",count23);
            map.put("count24",count24);
            map.put("count31",count31);
            map.put("count32",count32);
            map.put("count33",count33);
            map.put("count34",count34);

            map.put("sumCount1",sumCount1);
            map.put("sumCount2",sumCount2);
            map.put("sumCount3",sumCount3);
            map.put("sumCount4",sumCount4);
            map.put("sumCountSum",sumCount1+sumCount2+sumCount3+sumCount4);

            map.put("sumCount",sumCount);
            map.put("sum2Count",sum2Count);
            map.put("sum3Count",sum3Count);

            map.put("V11",V11);
            map.put("V12",V12);
            map.put("V13",V13);
            map.put("V14",V14);
            map.put("V21",V21);
            map.put("V22",V22);
            map.put("V23",V23);
            map.put("V24",V24);
            map.put("V31",V31);
            map.put("V32",V32);
            map.put("V33",V33);
            map.put("V34",V34);

            map.put("v",v);
            map.put("v2",v2);
            map.put("v3",v3);
            listMessage.add(map);
        }

        //查询班组
        return listMessage;
    }

}
