package com.elephant.framework.galaxy.aroad.module.zhjg.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.elephant.framework.galaxy.aroad.module.zhjg.entity.*;
import com.elephant.framework.galaxy.aroad.module.zhjg.mapper.ZhjgLzglCxysxkMapper;
import com.elephant.framework.galaxy.aroad.module.zhjg.service.ZhjgLzglCxysxkService;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 超限运输许可表
 *
 * @author wangjian
 * @date 2019-03-27 17:46:24
 */
@Service
@AllArgsConstructor
public class ZhjgLzglCxysxkServiceImpl extends ServiceImpl<ZhjgLzglCxysxkMapper, ZhjgLzglCxysxk> implements ZhjgLzglCxysxkService {

    private final ZhjgLzglCxysxkMapper ZhjgLzglCxysxkMapper;
    /**
     * 通过时间查询该时间段内的数据
     * @param startDate
     * @param endDate
     * @return
     */
    public List<ZhjgLzglCxysxk> findZhjgLzglCxysxkByDate(String startDate, String endDate){
        return ZhjgLzglCxysxkMapper.selectList(Wrappers.<ZhjgLzglCxysxk>query().lambda().
                ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate).eq(ZhjgLzglCxysxk::getIfks,2));
    }

    /**
     *  根据许可单号查询超限许可信息是否已存在
     * @param xkdh
     * @return
     */
    public Integer getZhjgLzglCxysxkByXkdh(String xkdh){
        return ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getXkdh,xkdh));
    }

    /**
     * 超限许可许可公示分页查询
     *
     * @param page 分页对象
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @param xkdh 许可单号
     * @param cph 车牌号
     * @return
     */
    public  IPage<List<ZhjgLzglCxysxk>> selectCxysXkgsByCurrentDate(Page page, String startDate, String endDate, String xkdh, String cph) {
        return ZhjgLzglCxysxkMapper.selectCxysXkgsByCurrentDate(page,startDate,endDate,xkdh,cph);
    }

    /**
     * 查询跨省发证审批数量
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return 数量
     */
    public Integer[] selectCountKsFzSpByDate(String startDate, String endDate) {
        Integer[] fzspSl = new Integer[2];
        fzspSl[0] = ZhjgLzglCxysxkMapper.selectCountKsFzSpByDate(null,startDate,endDate);

        System.out.println("fzspSl[0] :"+fzspSl[0]);
        fzspSl[1] = ZhjgLzglCxysxkMapper.selectCountKsFzSpByDate("37",startDate,endDate);
        System.out.println("fzspSl[1] :"+fzspSl[1]);

        return fzspSl;
    }

    /**
     * 超限运输数据分析页面统计数据
     * @param startLong
     * @param endLong
     * @param startWidth
     * @param endWidth
     * @param startHeight
     * @param endHeight
     * @param startWeight
     * @param endWeight
     * @param startDate
     * @param endDate
     * @return
     */
    public CxysDataAnalysis selectCxysSjfx(Double startLong,Double endLong,
                                          Double startWidth,Double endWidth,
                                          Double startHeight,Double endHeight,
                                          Double startWeight,Double endWeight,
                                          String startDate,String endDate) {
        CxysDataAnalysis cxysSjfx = new CxysDataAnalysis();
        cxysSjfx.setLongBeyond(selectBeyondLong(startLong,endLong,startDate,endDate));
        cxysSjfx.setHeightBeyond(selectBeyondHeight(startHeight,endHeight,startDate,endDate));
        cxysSjfx.setWeightBeyond(selectBeyondWeight(startWeight,endWeight,startDate,endDate));
        cxysSjfx.setWidthBeyond(selectBeyondWidth(startWidth,endWidth,startDate,endDate));
        cxysSjfx.setCxysSizeBeyond(selectSizeBeyond(startDate,endDate));
        cxysSjfx.setCxysSsspspTj(dealWithSsspSpData(startDate,endDate));
        return cxysSjfx;
    }

    /**
     * 超限运输综合展示页面
     * @param startDate
     * @param endDate
     * @return
     */
    public ShowCxysTotal seleCxysZhzs(String startDate,String endDate) {
        ShowCxysTotal showCxysTotal = new ShowCxysTotal();

        Integer pieUnder = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .le(ZhjgLzglCxysxk::getChzz,49));
        Integer pieRange = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getChzz,49).lt(ZhjgLzglCxysxk::getChzz,100));
        Integer pieOn = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .ge(ZhjgLzglCxysxk::getChzz,100));
        Integer longOn = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getCd,28));
        Integer longUnder = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .le(ZhjgLzglCxysxk::getCd,28));
        Integer widthOn = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getKd,3.75));
        Integer widthUnder = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .le(ZhjgLzglCxysxk::getKd,3.75));
        Integer heightOn = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getGd,4.5));
        Integer heightUnder = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .le(ZhjgLzglCxysxk::getGd,4.5));
        showCxysTotal.setPieOn(pieOn);
        showCxysTotal.setPieUnder(pieUnder);
        showCxysTotal.setPieRange(pieRange);
        showCxysTotal.setLongOn(longOn);
        showCxysTotal.setLongUnder(longUnder);
        showCxysTotal.setWidthOn(widthOn);
        showCxysTotal.setWidthUnder(widthUnder);
        showCxysTotal.setHeightOn(heightOn);
        showCxysTotal.setHeightUnder(heightUnder);
        showCxysTotal.setCxysSsspspTj(dealWithSsspSpData(startDate,endDate));
        return showCxysTotal;
    }


    /**
     * 查询超长
     * @param start 开始长度
     * @param end 结束长度
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public CxysBeyondFormat selectBeyondLong(Double start, Double end,String startDate, String endDate) {
        CxysBeyondFormat longNumber = new CxysBeyondFormat();
        int dataNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .ge(ZhjgLzglCxysxk::getCd,start).le(ZhjgLzglCxysxk::getCd,end));

        int otherNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .lt(ZhjgLzglCxysxk::getCd,start))+
                ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                        .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                        .gt(ZhjgLzglCxysxk::getCd,end));

        longNumber.setData(dataNumber);
        longNumber.setOtherData(otherNumber);
        return longNumber;
    }

    /**
     * 查询超宽
     * @param start 开始长度
     * @param end 结束长度
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public CxysBeyondFormat selectBeyondWidth(Double start, Double end,String startDate, String endDate) {
        CxysBeyondFormat widthNumber = new CxysBeyondFormat();
        int dataNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .ge(ZhjgLzglCxysxk::getKd,start).le(ZhjgLzglCxysxk::getKd,end));

        int otherNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .lt(ZhjgLzglCxysxk::getKd,start))+
                ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                        .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                        .gt(ZhjgLzglCxysxk::getKd,end));

        widthNumber.setData(dataNumber);
        widthNumber.setOtherData(otherNumber);
        return widthNumber;
    }

    /**
     * 查询超高
     * @param start 开始长度
     * @param end 结束长度
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public CxysBeyondFormat selectBeyondHeight(Double start, Double end,String startDate, String endDate) {
        CxysBeyondFormat heightNumber = new CxysBeyondFormat();
        int dataNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .ge(ZhjgLzglCxysxk::getGd,start).le(ZhjgLzglCxysxk::getGd,end));
        int otherNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .lt(ZhjgLzglCxysxk::getGd,start))+
                ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                        .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                        .gt(ZhjgLzglCxysxk::getGd,end));
        heightNumber.setData(dataNumber);
        heightNumber.setOtherData(otherNumber);
        return heightNumber;
    }

    /**
     * 查询超重
     * @param start 开始长度
     * @param end 结束长度
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public CxysBeyondFormat selectBeyondWeight(Double start, Double end,String startDate, String endDate) {
        CxysBeyondFormat weightNumber = new CxysBeyondFormat();
        int dataNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .ge(ZhjgLzglCxysxk::getChzz,start).le(ZhjgLzglCxysxk::getChzz,end));
        int otherNumber = ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .lt(ZhjgLzglCxysxk::getChzz,start))+
                ZhjgLzglCxysxkMapper.selectCount( Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                        .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                        .gt(ZhjgLzglCxysxk::getChzz,end));
        weightNumber.setData(dataNumber);
        weightNumber.setOtherData(otherNumber);
        return weightNumber;
    }


    /**
     * 超限运输省市尺寸超限分析
     * @param startDate 开始时间
     * @param endDate 结束时间
     * @return
     */
    public CxysSizeBeyondAnalysis selectSizeBeyond(String startDate, String endDate) {
        CxysSizeBeyondAnalysis cxysSizeBeyondAnalysis = new CxysSizeBeyondAnalysis();
        int dcc = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getCd,28));
        int dck = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getKd,3.75));
        int dcg = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getGd,4.5));
        int cck = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getCd,28).gt(ZhjgLzglCxysxk::getKd,3.75));
        int ccg = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getCd,28).gt(ZhjgLzglCxysxk::getGd,4.5));
        int ckg = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getKd,3.75).gt(ZhjgLzglCxysxk::getGd,4.5));
        int cckg = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .gt(ZhjgLzglCxysxk::getCd,28).gt(ZhjgLzglCxysxk::getGd,4.5).gt(ZhjgLzglCxysxk::getKd,3.75));
        int qt = ZhjgLzglCxysxkMapper.selectCount(Wrappers.<ZhjgLzglCxysxk>query().lambda().eq(ZhjgLzglCxysxk::getIfks,2)
                .ge(ZhjgLzglCxysxk::getRq,startDate).le(ZhjgLzglCxysxk::getRq,endDate)
                .le(ZhjgLzglCxysxk::getCd,28).le(ZhjgLzglCxysxk::getGd,4.5).le(ZhjgLzglCxysxk::getKd,3.75));
        cxysSizeBeyondAnalysis.setDcc(dcc);
        cxysSizeBeyondAnalysis.setDck(dck);
        cxysSizeBeyondAnalysis.setDcg(dcg);
        cxysSizeBeyondAnalysis.setCck(cck);
        cxysSizeBeyondAnalysis.setCcg(ccg);
        cxysSizeBeyondAnalysis.setCkg(ckg);
        cxysSizeBeyondAnalysis.setCckg(cckg);
        cxysSizeBeyondAnalysis.setQt(qt);
        return cxysSizeBeyondAnalysis;
    }

    /**
     * 查询省市受理审批数量
     * @param startDate
     * @param endDate
     * @return
     */
    public List<Map> dealWithSsspSpData(String startDate,String endDate) {
        List<Map> slspData = new ArrayList<Map>();
        String areaNames = "莱芜,菏泽,临沂,聊城,德州,滨州,日照,威海,泰安,济宁,潍坊,烟台,东营,枣庄,淄博,青岛,济南,省厅公路局,";
        for(int i=0; i<areaNames.split(",").length; i++){
            String areaName = areaNames.split(",")[i];
            Map<String,Object> map = new HashMap<String,Object>();
            map.put("日期",areaName);
            map.put("受理数目累计",0);
            map.put("审批数量累计",0);
            slspData.add(map);
        }

        List<ZhjgLzglCxysxk> zhjgLzglCxysxks = findZhjgLzglCxysxkByDate(startDate,endDate);

        for(ZhjgLzglCxysxk zhjgLzglCxysxk : zhjgLzglCxysxks){
            if("10".equals(zhjgLzglCxysxk.getDwjb().trim())) {
                Integer spsm = Integer.parseInt(slspData.get(17).get("审批数量累计").toString());
                if (slspData.get(17).containsKey("审批数量累计")){
                    //如果存在，可以直接put新的键值对。新的键值会自动覆盖之前的。
                    slspData.get(17).put("审批数量累计",spsm+1);
                }
            }
            if(zhjgLzglCxysxk.getSldw().trim().indexOf("山东省交通运输厅公路局")!=-1) {
                Integer slsm = Integer.parseInt(slspData.get(17).get("受理数目累计").toString());
                if (slspData.get(17).containsKey("受理数目累计")){
                    //如果存在，可以直接put新的键值对。新的键值会自动覆盖之前的。
                    slspData.get(17).put("受理数目累计",slsm+1);
                }
            }

            for(Map map : slspData) {
                String area = map.get("日期").toString();

                if(zhjgLzglCxysxk.getSldw().trim().indexOf(area)!=-1) {
                    Integer slsm = Integer.parseInt(map.get("受理数目累计").toString());
                    if (map.containsKey("受理数目累计")){
                        //如果存在，可以直接put新的键值对。新的键值会自动覆盖之前的。
                        map.put("受理数目累计",slsm+1);
                    }
                }
                if(!"10".equals(zhjgLzglCxysxk.getDwjb())) {
                    if(zhjgLzglCxysxk.getSldw().trim().indexOf(area)!=-1) {
                        Integer spsm = Integer.parseInt(map.get("审批数量累计").toString());
                        if (slspData.get(17).containsKey("审批数量累计")){
                            //如果存在，可以直接put新的键值对。新的键值会自动覆盖之前的。
                            map.put("审批数量累计",spsm+1);
                        }
                    }
                }
            }
        }

        return slspData;
    }
}
