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

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.zhjc.entity.*;
import com.elephant.framework.galaxy.aroad.module.zhjc.mapper.ZhjcGhjhGhxmMapper;
import com.elephant.framework.galaxy.aroad.module.zhjc.service.ZhjcGhjhGhxmService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * 智慧决策—项目决策-规划项目
 *
 * @author wangjian
 * @date 2019-05-05 16:10:42
 */
@Service
@AllArgsConstructor
public class ZhjcGhjhGhxmServiceImpl extends ServiceImpl<ZhjcGhjhGhxmMapper, ZhjcGhjhGhxm> implements ZhjcGhjhGhxmService {

    private final ZhjcGhjhGhxmMapper zhjcGhjhGhxmMapper;
    private static String[] citys = new String[]{"济南","青岛","淄博","枣庄","东营","烟台","潍坊","济宁","泰安","威海","日照","滨州","德州","聊城"
            ,"临沂","菏泽","莱芜"};

    /**
     * 根据统计年份查询规划计划统计分析页面数据
     * @param year
     * @return
     */
    public ZhjcGhjhTjfx getZhjcGhjhTjfxDataByYear(String year) {
        ZhjcGhjhTjfx zhjcGhjhTjfx = new ZhjcGhjhTjfx();
        zhjcGhjhTjfx.setZhjcGhjhTjfxOnes( getZhjcGhjhTjfxOneDataByYear(year));
        zhjcGhjhTjfx.setZhjcGhjhTjfxThrees(getZhjcGhjhTjfxThreeDataByYear(year));
        zhjcGhjhTjfx.setZhjcGhjhTjfxFours(getZhjcGhjhTjfxFourDataByYear(year));
        return zhjcGhjhTjfx;
    }

    /**
     * 分页查询规划项目
     * @param zhjcGhjhGhxm
     * @return
     */
     public IPage<ZhjcGhjhGhxm> getZhjcGhjhXmgzDataByPage(Page page, ZhjcGhjhGhxm zhjcGhjhGhxm) {

         LambdaQueryWrapper<ZhjcGhjhGhxm> lambdaQueryWrapper = Wrappers.<ZhjcGhjhGhxm>query().lambda();

         if(zhjcGhjhGhxm.getCity()!=null && !"".equals(zhjcGhjhGhxm.getCity())) {
             lambdaQueryWrapper = lambdaQueryWrapper.like(ZhjcGhjhGhxm::getCity, zhjcGhjhGhxm.getCity());
         }
         if(zhjcGhjhGhxm.getJsdj()!=null && !"".equals(zhjcGhjhGhxm.getJsdj())) {
             lambdaQueryWrapper = lambdaQueryWrapper.like(ZhjcGhjhGhxm::getJsdj, zhjcGhjhGhxm.getJsdj());
         }
         if(zhjcGhjhGhxm.getJzqk()!=null && !"".equals(zhjcGhjhGhxm.getJzqk())) {
             lambdaQueryWrapper = lambdaQueryWrapper.eq(ZhjcGhjhGhxm::getJzqk, zhjcGhjhGhxm.getJzqk());
         }
         if(zhjcGhjhGhxm.getJsxz()!=null && !"".equals(zhjcGhjhGhxm.getJsxz())) {
             lambdaQueryWrapper = lambdaQueryWrapper.eq(ZhjcGhjhGhxm::getJsxz, zhjcGhjhGhxm.getJsxz());
         }

         return zhjcGhjhGhxmMapper.selectPage(page, lambdaQueryWrapper);
     }


    //获取统计分页一图数据
    public List<ZhjcGhjhTjfxOne> getZhjcGhjhTjfxOneDataByYear(String year) {
        List<ZhjcGhjhTjfxOne> zhjcGhjhTjfxOnes = new ArrayList<>();
        List<ZhjcGhjhTjfxDzxAndXgj> dzx = zhjcGhjhGhxmMapper.getZhjcGhjhTjfxOneDzxAndXgjByYear("大中修",year);
        List<ZhjcGhjhTjfxDzxAndXgj> xgj = zhjcGhjhGhxmMapper.getZhjcGhjhTjfxOneDzxAndXgjByYear("新改建",year);

        for(String city : citys) {
            ZhjcGhjhTjfxOne zhjcGhjhTjfxOne = new ZhjcGhjhTjfxOne();
            zhjcGhjhTjfxOne.setCity(city);

            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : dzx) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city) > -1) {
                    zhjcGhjhTjfxOne.setDzx((new Double(zhjcGhjhTjfxDzxAndXgj.getNumber())).intValue());
                    break;
                }
            }
            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : xgj) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city)>-1) {
                    zhjcGhjhTjfxOne.setXgj((new Double(zhjcGhjhTjfxDzxAndXgj.getNumber())).intValue());
                    break;
                }
            }

            zhjcGhjhTjfxOnes.add(zhjcGhjhTjfxOne);
        }

        return zhjcGhjhTjfxOnes;
    }

    //获取统计分页三图数据
    public List<ZhjcGhjhTjfxThree> getZhjcGhjhTjfxThreeDataByYear(String year) {
        List<ZhjcGhjhTjfxThree> zhjcGhjhTjfxThrees = new ArrayList<>();

        List<ZhjcGhjhTjfxDzxAndXgj> zhjcGhjhTjfxDzxAndXgjs = zhjcGhjhGhxmMapper.getZhjcGhjhTjfxThreeDataByYear(year);

        for(String city : citys) {
            ZhjcGhjhTjfxThree zhjcGhjhTjfxThree = new ZhjcGhjhTjfxThree();
            zhjcGhjhTjfxThree.setCity(city);
            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : zhjcGhjhTjfxDzxAndXgjs) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity()!=null && zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city)!=-1) {
                    zhjcGhjhTjfxThree.setNdzytz((new Double(zhjcGhjhTjfxDzxAndXgj.getNumber())).intValue());
                    zhjcGhjhTjfxThree.setNdstz((new Double(zhjcGhjhTjfxDzxAndXgj.getNumbertwo())).intValue());
                    zhjcGhjhTjfxThree.setZytz(0);
                    zhjcGhjhTjfxThree.setStz(new Double(zhjcGhjhTjfxDzxAndXgj.getNumberthree()).intValue());
                    zhjcGhjhTjfxThree.setSxztz(new Double(zhjcGhjhTjfxDzxAndXgj.getNumberfour()).intValue());
                    break;
                }
            }
            zhjcGhjhTjfxThrees.add(zhjcGhjhTjfxThree);
        }


        return zhjcGhjhTjfxThrees;
    }

    //获取统计分页四图数据
    public List<ZhjcGhjhTjfxFour> getZhjcGhjhTjfxFourDataByYear(String year) {

        List<ZhjcGhjhTjfxFour> zhjcGhjhTjfxFours = new ArrayList<>();

        List<ZhjcGhjhTjfxDzxAndXgj> dzx = zhjcGhjhGhxmMapper.getZhjcGhjhTjfxFourByYear("大中修",year);
        List<ZhjcGhjhTjfxDzxAndXgj> xgj = zhjcGhjhGhxmMapper.getZhjcGhjhTjfxFourByYear("新改建",year);
        List<ZhjcGhjhTjfxDzxAndXgj> qt = zhjcGhjhGhxmMapper.getZhjcGhjhTjfxFourQtByYear(year);

        for(String city: citys) {
            ZhjcGhjhTjfxFour zhjcGhjhTjfxFour = new ZhjcGhjhTjfxFour();
            zhjcGhjhTjfxFour.setCity(city);
            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : dzx) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city)>-1) {
                    zhjcGhjhTjfxFour.setDzx(zhjcGhjhTjfxDzxAndXgj.getNumber());
                    break;
                }
            }
            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : xgj) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city)>-1) {
                    zhjcGhjhTjfxFour.setXgj(zhjcGhjhTjfxDzxAndXgj.getNumber());
                    break;
                }
            }
            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : qt) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city)>-1) {
                    zhjcGhjhTjfxFour.setQt(zhjcGhjhTjfxDzxAndXgj.getNumber());
                    break;
                }
            }

            zhjcGhjhTjfxFours.add(zhjcGhjhTjfxFour);
        }

        return zhjcGhjhTjfxFours;
    }
}
