package com.elephant.framework.galaxy.aroad.module.zhjs.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.common.entity.KeyValues;
import com.elephant.framework.galaxy.aroad.module.common.getInterfaceData.HttpClient;
import com.elephant.framework.galaxy.aroad.module.zhjs.entity.*;
import com.elephant.framework.galaxy.aroad.module.zhjs.mapper.ZhjsGcxmMapper;
import com.elephant.framework.galaxy.aroad.module.zhjs.service.ZhjsGcxmService;
import com.elephant.framework.galaxy.aroad.module.zhjs.service.ZhjsTokenService;
import lombok.AllArgsConstructor;
import net.sf.json.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 工程项目信息表

 *
 * @author wangjian
 * @date 2019-04-08 18:14:32
 */
@Service
@AllArgsConstructor
public class ZhjsGcxmServiceImpl extends ServiceImpl<ZhjsGcxmMapper, ZhjsGcxm> implements ZhjsGcxmService {

    final ZhjsGcxmMapper zhjsGcxmMapper;
    final HttpClient httpClient;
    final ZhjsTokenService zhjsTokenService;

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

    /**
     * 根据项目ID查询项目情况
     * @param xmid
     * @return
     */
    public ZhjsXmqk getZhjsXmqkByXmid(String xmid) {
        return zhjsGcxmMapper.getZhjsXmqkByXmid(xmid);
    }

    //获取token
    public String getTokenData() {
        ZhjsToken zhjsToken = zhjsTokenService.getOne(Wrappers.query());
        String token = zhjsToken.getToken();

        while(true) {
            JSONObject jsonObject = JSONObject.fromObject(httpClient.client(zhjsToken.getToken()));
            if("1".equals(jsonObject.getString("code"))) {
                zhjsToken.setToken(JSONObject.fromObject(jsonObject.getString("data")).getString("token"));
            }else if("107".equals(jsonObject.getString("code"))) {
                if(!token.equals(zhjsToken.getToken())) {
                    zhjsTokenService.setZhjsToken(token,zhjsToken);
                    System.out.println("更新koken成功，值为："+zhjsToken.getToken());
                }
                break;
            }
        }
        return zhjsToken.getToken();
    }

    /**
     * 通过项目Id获取汇总明细
     * @return
     */
    public String getHzmxByProId(String token, String proId) {
        RestTemplate rest =new RestTemplate();
        String json = "{'pro_id':'"+proId+"'}";
        String url = "http://118.24.7.48:8088/getSummaryListTestInterfaces.action?access_token="+token+"&key={json}";
        ResponseEntity<String> res= rest.getForEntity(url,String.class,json);


        return  JSONObject.fromObject(res.getBody()).getString("data");
    }

    /**
     * 查询综合展示图一数据
     */
    public List<ZhjsZhzsOne> getZhjsZhzsOneDatas() {
        List<ZhjsZhzsOne> zhjsZhzsOnes = new ArrayList<>();

        List<KeyValues> xujxm = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(null, 1);
        List<KeyValues> xijxm = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(null, 2);
        List<KeyValues> wgxm = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(null, 3);

        for(String city : citys) {
            ZhjsZhzsOne zhjsZhzsOne = new ZhjsZhzsOne();
            zhjsZhzsOne.setCity(city);

            for(KeyValues keyValues : xujxm) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsOne.setXujxm(keyValues.getNumber());
                }
            }
            for(KeyValues keyValues : xijxm) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsOne.setXijxm(keyValues.getNumber());
                }
            }
            for(KeyValues keyValues : wgxm) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsOne.setWgxm(keyValues.getNumber());
                }
            }
            zhjsZhzsOnes.add(zhjsZhzsOne);
        }

        return zhjsZhzsOnes;
    }

    /**
     * 查询综合展示图二数据
     */
    public List<KeyValues> getZhjsZhzsTwoDatas(String city) {
        List<KeyValues> keyValues = new ArrayList<>();

        keyValues.add(new KeyValues("瓶颈续建", zhjsGcxmMapper.getZhjsZhzsTwo(city,1,1)));
        keyValues.add(new KeyValues("瓶颈新建", zhjsGcxmMapper.getZhjsZhzsTwo(city,1,2)));
        keyValues.add(new KeyValues("瓶颈项目", zhjsGcxmMapper.getZhjsZhzsTwo(city,1,null)));
        keyValues.add(new KeyValues("穿城续建", zhjsGcxmMapper.getZhjsZhzsTwo(city,2,1)));
        keyValues.add(new KeyValues("穿城新建", zhjsGcxmMapper.getZhjsZhzsTwo(city,2,2)));
        keyValues.add(new KeyValues("穿城项目", zhjsGcxmMapper.getZhjsZhzsTwo(city,2,null)));

        return keyValues;
    }

    /**
     * 查询综合展示图三数据
     */
    public List<ZhjsZhzsThree> getZhjsZhzsThreeDatas() {
        List<ZhjsZhzsThree> zhjsZhzsThrees = new ArrayList<>();

        List<KeyValues> ccxuj = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(2,1);
        List<KeyValues> ccxij = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(2,2);
        List<KeyValues> pjxuj = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(1,1);
        List<KeyValues> pjxij = zhjsGcxmMapper.getZhjsZhzsThreeAndOne(1,2);

        for(String city : citys) {
            ZhjsZhzsThree zhjsZhzsThree = new ZhjsZhzsThree();
            zhjsZhzsThree.setCity(city);
            for(KeyValues keyValues : ccxuj) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsThree.setCcxuj(keyValues.getNumber());
                    break;
                }
            }
            for(KeyValues keyValues : ccxij) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsThree.setCcxij(keyValues.getNumber());
                    break;
                }
            }
            for(KeyValues keyValues : pjxuj) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsThree.setPjxuj(keyValues.getNumber());
                    break;
                }
            }
            for(KeyValues keyValues : pjxij) {
                if(keyValues.getName().indexOf(city)>-1) {
                    zhjsZhzsThree.setPjxij(keyValues.getNumber());
                    break;
                }
            }
            zhjsZhzsThrees.add(zhjsZhzsThree);
        }
        return zhjsZhzsThrees;
    }

    /**
     * 根据项目名称分页模糊查询数据
     */
    public IPage<ZhjsGcxm> getZhjsGcxmPageByProName(Page page, String proname) {
        return zhjsGcxmMapper.selectPage(page, Wrappers.<ZhjsGcxm>query().lambda().like(ZhjsGcxm::getProname, proname));
    }


    /**
     * 查询项目信息
     */
    public List<ZhjsGcxmShape> getGcxmAndShape(Integer attribute,Integer invmodel) {
        return zhjsGcxmMapper.getGcxmAndShape(attribute,invmodel);
    }
}