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

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.ZhjcGhjhFghjdzbMapper;
import com.elephant.framework.galaxy.aroad.module.zhjc.mapper.ZhjcGhjhGhjdzbMapper;
import com.elephant.framework.galaxy.aroad.module.zhjc.mapper.ZhjcGhjhMapper;
import com.elephant.framework.galaxy.aroad.module.zhjc.mapper.ZhjcGhjhTzzhMapper;
import com.elephant.framework.galaxy.aroad.module.zhjc.service.ZhjcGhjhService;
import com.elephant.framework.galaxy.aroad.response.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

/**
 * 智慧决策_规划计划
 *
 * @author wangjian
 * @date 2019-05-29 10:45:50
 */
@Service
@AllArgsConstructor
public class ZhjcGhjhServiceImpl extends ServiceImpl<ZhjcGhjhMapper, ZhjcGhjh> implements ZhjcGhjhService {

    final ZhjcGhjhMapper zhjcGhjhMapper;
    final ZhjcGhjhTzzhMapper zhjcGhjhTzzhMapper;
    final ZhjcGhjhFghjdzbMapper zhjcGhjhFghjdzbMapper;
    final ZhjcGhjhGhjdzbMapper zhjcGhjhGhjdzbMapper;

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

    /**
     * 根据项目编码查询项目数据
     */
    public List<ZhjcGhjh> getZhjcGhjhByXmbm(String xmbm) {
        return zhjcGhjhMapper.selectList(Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmbm,xmbm).orderByAsc(ZhjcGhjh::getXmjd));
    }


    /**
     * 获取项目跟踪页面数据
     */
    public IPage<ZhjcGhjh> getGhjhXmgz(Page page, String tbdwdm, String xmlxdm, String jsxzdm, String xmjd) {
        String year = Calendar.getInstance().get(Calendar.YEAR)+"";
        LambdaQueryWrapper<ZhjcGhjh> lambdaQueryWrapper = Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getSfzxzt, "1");
        if(tbdwdm!=null && !"".equals(tbdwdm.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getTbdwdm,tbdwdm);
        }
        if(xmlxdm!=null && !"".equals(xmlxdm.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getXmlxdm,xmlxdm);
        }
        if(jsxzdm!=null && !"".equals(jsxzdm.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getJsxzdm,jsxzdm);
        }
        if(xmjd!=null && !"".equals(xmjd.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getXmjd,xmjd);
        }
        IPage<ZhjcGhjh> pageData = zhjcGhjhMapper.selectPage(page,lambdaQueryWrapper);
        for(ZhjcGhjh zhjcGhjh : pageData.getRecords()) {
            zhjcGhjh.setZsnstz(zhjcGhjhMapper.getTableZsnZJData(zhjcGhjh.getXmid(),"S",year));
            zhjcGhjh.setZsnzytz(zhjcGhjhMapper.getTableZsnZJData(zhjcGhjh.getXmid(),"Z",year));
            zhjcGhjh.setBnstz(zhjcGhjhMapper.getTableBnZJData(zhjcGhjh.getXmid(),"S",year));
            zhjcGhjh.setBnzytz(zhjcGhjhMapper.getTableBnZJData(zhjcGhjh.getXmid(),"Z",year));
        }
        return pageData;
    }

    /**
     * 获取项目跟踪页面坐标数据
     */
    public List<ZhjcGhjh> getGhjhXmgzShape(String tbdwdm, String xmlxdm, String jsxzdm, String xmjd) {
        LambdaQueryWrapper<ZhjcGhjh> lambdaQueryWrapper = Wrappers.<ZhjcGhjh>query().lambda();
        if(tbdwdm!=null && !"".equals(tbdwdm.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getTbdwdm,tbdwdm);
        }
        if(xmlxdm!=null && !"".equals(xmlxdm.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getXmlxdm,xmlxdm);
        }
        if(jsxzdm!=null && !"".equals(jsxzdm.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getJsxzdm,jsxzdm);
        }
        if(xmjd!=null && !"".equals(xmjd.trim())) {
            lambdaQueryWrapper.eq(ZhjcGhjh::getXmjd,xmjd);
        }
        List<ZhjcGhjh> zhjcGhjhs = zhjcGhjhMapper.selectList(lambdaQueryWrapper);

        for(ZhjcGhjh zhjcGhjh : zhjcGhjhs) {
            List shapeList = null;
            if(zhjcGhjh.getXmjd()!=null && "10".equals(zhjcGhjh.getXmjd().trim())) {
                shapeList = zhjcGhjhGhjdzbMapper.selectList(Wrappers.<ZhjcGhjhGhjdzb>query().lambda().eq(ZhjcGhjhGhjdzb::getXmid,zhjcGhjh.getXmid()));
            }else {
                shapeList  = zhjcGhjhFghjdzbMapper.selectList(Wrappers.<ZhjcGhjhFghjdzb>query().lambda().eq(ZhjcGhjhFghjdzb::getXmid,zhjcGhjh.getXmid()));
            }
            List<List<String>> shape1 = new ArrayList<>();
            for(int i=0; i<shapeList.size(); i++) {
                ZhjcGhjhGhjdzb zhjcGhjhGhjdzb = (ZhjcGhjhGhjdzb)shapeList.get(i);
                String shape = zhjcGhjhGhjdzb.getGeomfeature().substring(11,zhjcGhjhGhjdzb.getGeomfeature().length()-1);
                String[] shapes = shape.split(",");
                for(String sha : shapes) {
                    List<String> strs = new ArrayList<>();
                    strs.add(sha.split("\\s+")[0]);
                    strs.add(sha.split("\\s+")[1]);
                    shape1.add(strs);
                }
            }
            zhjcGhjh.setShape(shape1);
        }
        return zhjcGhjhs;
    }


    /**
     * 修改规划计划
     * @return
     */
    public Boolean updateZhjcGhjh(ZhjcGhjh zhjcGhjh) {
        return zhjcGhjhMapper.update(zhjcGhjh,Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmid,zhjcGhjh.getXmid()).eq(ZhjcGhjh::getXmjd,zhjcGhjh.getXmjd()))>0;
    }

    /**
     * 新增规划计划
     */
    public Boolean addZhjcGhjh(ZhjcGhjh zhjcGhjh){
        ZhjcGhjh zhjcGhjh1 = zhjcGhjhMapper.selectOne(Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmid,zhjcGhjh.getXmid()).eq(ZhjcGhjh::getXmjd,zhjcGhjh.getXmjd()).eq(ZhjcGhjh::getXmlxdm,zhjcGhjh.getXmlxdm()));
        zhjcGhjh.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        zhjcGhjh.setSfzxzt("1");

        ZhjcGhjh zhjcGhjh2 = new ZhjcGhjh();
        zhjcGhjh2.setSfzxzt("0");
        zhjcGhjhMapper.update(zhjcGhjh2,Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmbm,zhjcGhjh.getXmbm()));

        int code = zhjcGhjhMapper.insert(zhjcGhjh);
        if(code>0) {
            if(zhjcGhjh1!=null && zhjcGhjh1.getId()!=null && !"".equals(zhjcGhjh1.getId().trim())) {
                zhjcGhjhMapper.delete(Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getId,zhjcGhjh1.getId()));
            }
        }
        return code>0;
    }

    /**
     * 删除规划计划
     */
    public Boolean deleteZhjcGhjh(ZhjcGhjh zhjcGhjh2) {
        ZhjcGhjh zhjcGhjh = zhjcGhjhMapper.selectOne(Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmid,zhjcGhjh2.getXmid()).eq(ZhjcGhjh::getXmjd,zhjcGhjh2.getXmjd()));
        if("".equals(zhjcGhjh.getId().trim()) && zhjcGhjh.getId()==null) {
            return false;
        }
        zhjcGhjhMapper.delete(Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmid,zhjcGhjh2.getXmid()).eq(ZhjcGhjh::getXmjd,zhjcGhjh2.getXmjd()));

        List<ZhjcGhjh> zhjcGhjhs = zhjcGhjhMapper.selectList(Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getXmbm, zhjcGhjh.getXmbm()).orderByDesc(ZhjcGhjh::getXmjd));

        ZhjcGhjh zhjcGhjh3 = new ZhjcGhjh();
        zhjcGhjh3.setSfzxzt("1");
        zhjcGhjhMapper.update(zhjcGhjh3,Wrappers.<ZhjcGhjh>query().lambda().eq(ZhjcGhjh::getId,zhjcGhjhs.get(0).getId()));

        zhjcGhjhTzzhMapper.delete(Wrappers.<ZhjcGhjhTzzh>query().lambda().eq(ZhjcGhjhTzzh::getXmid,zhjcGhjh2.getXmid()).eq(ZhjcGhjhTzzh::getXmjd,zhjcGhjh2.getXmjd()));
        if(zhjcGhjh.getXmjd().trim().startsWith("10")) {
            zhjcGhjhGhjdzbMapper.delete(Wrappers.<ZhjcGhjhGhjdzb>query().lambda().eq(ZhjcGhjhGhjdzb::getXmid,zhjcGhjh2.getXmid()));
            zhjcGhjhFghjdzbMapper.delete(Wrappers.<ZhjcGhjhFghjdzb>query().lambda().eq(ZhjcGhjhFghjdzb::getXmid,zhjcGhjh2.getXmid()));
        }else if(zhjcGhjh.getXmjd().trim().startsWith("21")){
            zhjcGhjhFghjdzbMapper.delete(Wrappers.<ZhjcGhjhFghjdzb>query().lambda().eq(ZhjcGhjhFghjdzb::getXmid,zhjcGhjh2.getXmid()));
        }
        return true;
    }

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

    //获取统计分页一图数据
    public List<ZhjcGhjhTjfxOne> getZhjcGhjhTjfxOneDataByYear(String year) {
        List<ZhjcGhjhTjfxOne> zhjcGhjhTjfxOnes = new ArrayList<>();
        List<ZhjcGhjhTjfxDzxAndXgj> dzx = zhjcGhjhMapper.getGhjhOneData("02",year);
        List<ZhjcGhjhTjfxDzxAndXgj> xgj = zhjcGhjhMapper.getGhjhOneData("01",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 ZhjcGhjhTjfxTwo[] getZhjcGhjhTjfxTwoDataByYear(String year) {
        ZhjcGhjhTjfxTwo[] zhjcGhjhTjfxTwos = new ZhjcGhjhTjfxTwo[2];

        ZhjcGhjhTjfxTwo xgj = new ZhjcGhjhTjfxTwo();
        xgj.setQq(zhjcGhjhMapper.getGhjhTwoData("01","21",year));
        xgj.setGk(zhjcGhjhMapper.getGhjhTwoData("01","22",year));
        xgj.setCs(zhjcGhjhMapper.getGhjhTwoData("01","23",year));
        xgj.setSgt(zhjcGhjhMapper.getGhjhTwoData("01","24",year));
        zhjcGhjhTjfxTwos[0] = xgj;



        ZhjcGhjhTjfxTwo dzx = new ZhjcGhjhTjfxTwo();
        dzx.setGk(zhjcGhjhMapper.getGhjhTwoData("02","22",year));
        dzx.setSgt(zhjcGhjhMapper.getGhjhTwoData("02","24",year));
        dzx.setQq(zhjcGhjhMapper.getGhjhTwoDatatwo(year));
        zhjcGhjhTjfxTwos[1] = dzx;

        return zhjcGhjhTjfxTwos;
    }


    //获取统计分页三图数据
    public List<ZhjcGhjhTjfxThree> getZhjcGhjhTjfxThreeDataByYear(String year) {
        List<ZhjcGhjhTjfxThree> zhjcGhjhTjfxThrees = new ArrayList<>();
        List<ZhjcGhjhTjfxDzxAndXgj> three = zhjcGhjhMapper.getGhjhThreeData(year);
        for(String city : citys) {
            ZhjcGhjhTjfxThree zhjcGhjhTjfxThree = new ZhjcGhjhTjfxThree();
            zhjcGhjhTjfxThree.setCity(city);
            for(ZhjcGhjhTjfxDzxAndXgj zhjcGhjhTjfxDzxAndXgj : three) {
                if(zhjcGhjhTjfxDzxAndXgj.getCity()!=null && zhjcGhjhTjfxDzxAndXgj.getCity().indexOf(city)!=-1) {
                    zhjcGhjhTjfxThree.setNdzytz((new Double(zhjcGhjhTjfxDzxAndXgj.getNumberfour())).intValue());
                    zhjcGhjhTjfxThree.setNdstz((new Double(zhjcGhjhTjfxDzxAndXgj.getNumberfive())).intValue());
                    zhjcGhjhTjfxThree.setZytz(new Double(zhjcGhjhTjfxDzxAndXgj.getNumber()).intValue());
                    zhjcGhjhTjfxThree.setStz(new Double(zhjcGhjhTjfxDzxAndXgj.getNumbertwo()).intValue());
                    zhjcGhjhTjfxThree.setSxztz(new Double(zhjcGhjhTjfxDzxAndXgj.getNumberthree()).intValue());
                    break;
                }
            }
            zhjcGhjhTjfxThrees.add(zhjcGhjhTjfxThree);
        }
        return zhjcGhjhTjfxThrees;
    }


    //获取统计分页四图数据
    public List<ZhjcGhjhTjfxFour> getZhjcGhjhTjfxFourDataByYear(String year) {
        List<ZhjcGhjhTjfxFour> zhjcGhjhTjfxFours = new ArrayList<>();
        List<ZhjcGhjhTjfxDzxAndXgj> dzx = zhjcGhjhMapper.getGhjhFourData(1,"02",year);
        List<ZhjcGhjhTjfxDzxAndXgj> xgj = zhjcGhjhMapper.getGhjhFourData(1,"01",year);
        List<ZhjcGhjhTjfxDzxAndXgj> qt = zhjcGhjhMapper.getGhjhFourData(2,null,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;
    }
}
