package com.goafanti.common.service;

import com.alibaba.fastjson.JSONObject;
import com.goafanti.common.bo.InputPoverty;
import com.goafanti.common.bo.OutPoverty;
import com.goafanti.common.bo.OutPovertyFlag;
import com.goafanti.common.utils.HttpUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
public class PovertyService {


    private static final String   gx_url="http://fpb.gxxw.cn/index.php?m=check&c=index&a=list_jdlk&name=";
    private static final String   gx_jiance_url="http://fpb.gxxw.cn/index.php?m=check&c=index&a=list_fpjc&name=";


    @Cacheable(value = "selectPoverty#300", key = "'selectPoverty:key='+#id")
    public Map<String,Object> selectPoverty(MultipartFile file,String id) {

        NewExcelUtil<InputPoverty> newExcelUtil=new NewExcelUtil<>(InputPoverty.class);
        List<InputPoverty> list = null;
        try {
            list = newExcelUtil.importExcel(file.getInputStream(),0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<OutPoverty> res=new ArrayList<>();
        Integer totalcount =0;
        Integer effectiveCount =0;
        for (InputPoverty e : list) {
            OutPoverty outPoverty = selectAggravationHttpPoverty(e);
            if (outPoverty.getId()!=null){
                effectiveCount++;
            }
            totalcount++;
            res.add(outPoverty);
        }
        Map<String ,Object> map=new HashMap<>();
        map.put("list",res);
        map.put("totalcount",totalcount);
        map.put("effectiveCount",effectiveCount);
        map.put("id",id);
        return map;
    }
    private OutPoverty selectAggravationHttpPoverty(InputPoverty in){
        OutPoverty outPoverty = selectHttpPoverty(in, 0);
        if (StringUtils.isBlank(outPoverty.getId()))outPoverty=selectHttpPoverty(in,1);
        if (StringUtils.isBlank(outPoverty.getName()))outPoverty.setName(in.getName());
        return outPoverty;
    }

    private OutPoverty selectHttpPoverty(InputPoverty in,Integer type) {
        OutPoverty out=new OutPoverty();
        try {

            String name= StringUtils.deleteWhitespace(in.getName());
            String idcard=in.getIdCard();
            StringBuffer url=new StringBuffer();
            if (type==0)url=url.append(gx_url);
            else url=url.append(gx_jiance_url);
            url=url.append(name).append("&idcard=").append(idcard);
            Document document = Jsoup
                    .connect(url.toString()).get();
            Elements filequery_table = document.getElementsByClass("Filequery_table");
            Elements tr = filequery_table.select("tr");
            if (tr.size()>1){
                Element element = tr.get(1);
                Elements td = element.select("td");
                if (type==0){
                    out.setId(td.get(0).text());
                    out.setCity(td.get(1).text());
                    out.setCounty(td.get(2).text());
                    out.setTownship(td.get(3).text());
                    out.setVillage(td.get(4).text());
                    out.setPeopleSum(td.get(5).text());
                    out.setName(td.get(6).text());
                    out.setRelationship(td.get(7).text());
                    out.setPovertyYear(td.get(8).text());
                }else {
                    out.setId(td.get(0).text());
                    out.setCity(td.get(1).text());
                    out.setCounty(td.get(2).text());
                    out.setTownship(td.get(3).text());
                    out.setVillage(td.get(4).text());
                    out.setName(td.get(5).text());
                    out.setRelationship(td.get(6).text());
                    out.setPeopleSum(td.get(7).text());
                    out.setHouseholdType(td.get(8).text());
                    out.setMonitoringObjectType(td.get(9).text());
                    out.setRiskStatus(td.get(10).text());
                    out.setIdentifyMonitoringTime(td.get(11).text());
                    out.setEliminateMonitoringTime(td.get(12).text());
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out;
    }
    private static final String flag_url="https://cpadis.cpad.gov.cn:1443/cpad_par/app/observeHouse/checkIdentity?loginToken=&userId=&version=";

    @Cacheable(value = "selectPovertytFlag#300", key = "'selectPovertytFlag:key='+#id")
    public Map<String, Object> selectFlag(MultipartFile file, String id) {
        NewExcelUtil<InputPoverty> newExcelUtil=new NewExcelUtil<>(InputPoverty.class);
        List<InputPoverty> list = null;
        try {
            list = newExcelUtil.importExcel(file.getInputStream(),0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<OutPovertyFlag> trueList=new ArrayList<>();
        List<OutPovertyFlag> falseList=new ArrayList<>();
        Map<String, Object> param=new HashMap<>();
        Map<String, Object> res=new HashMap<>();
        Integer totalcount =0;
        Integer effectiveCount =0;
        for (InputPoverty in : list) {
            String name=StringUtils.deleteWhitespace(in.getName());
            String idCode=StringUtils.deleteWhitespace(in.getIdCard());
            param.put("name",name);
            param.put("idCode",idCode);
            JSONObject jsonObject = HttpUtils.httpPostBody(flag_url, param);
            String data = jsonObject.getString("data");
            OutPovertyFlag outPovertyFlag=new OutPovertyFlag();
            outPovertyFlag.setName(name);
            if (StringUtils.equals(data,"true")){
                outPovertyFlag.setPovertyStatus("是");
                trueList.add(outPovertyFlag);
            effectiveCount++;
            }else if (StringUtils.equals(data,"false")){
                outPovertyFlag.setPovertyStatus("否");
                falseList.add(outPovertyFlag);
            }else {
                outPovertyFlag.setPovertyStatus("查询错误");
                falseList.add(outPovertyFlag);
            }
            totalcount++;
        }
        res.put("trueList",trueList);
        res.put("falseList",falseList);
        res.put("totalcount",totalcount);
        res.put("effectiveCount",effectiveCount);
        res.put("id",id);
        return res;
    }
}
