ZhjDatasSynServiceImpl.java 15.6 KB
package com.pashanhoo.zhj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pashanhoo.zhj.entity.RegBusBdcqzsdjxxDo;
import com.pashanhoo.zhj.entity.SynZhjInfoDo;
import com.pashanhoo.zhj.mapper.RegBusBdcqzsdjxxMapper;
import com.pashanhoo.zhj.mapper.SynZhjInfoMapper;
import com.pashanhoo.zhj.service.ZhjDatasSynService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 案卷基本信息 服务实现类
 * </p>
 *
 * @author
 * @since 2021-11-05
 */
@Service
public class ZhjDatasSynServiceImpl extends ServiceImpl<RegBusBdcqzsdjxxMapper, RegBusBdcqzsdjxxDo> implements ZhjDatasSynService {

    private static Logger logger = LoggerFactory.getLogger(ZhjDatasSynServiceImpl.class);
    @Autowired
    private SynZhjInfoMapper synZhjInfoMapper;
    @Autowired
    private RegBusBdcqzsdjxxMapper regBusBdcqzsdjxxMapper;
    //智慧局内网ip
    private String zhjurl="http://172.16.56.10:7050";
    //认证令牌
    private  String appkey="3cd832c411238faa1df46d532a21bfcd";
    private String appSecret="ee7612083f63c2ce0d016c2aefd0a8d2";

    public void send_bdcYwInfo() {
       List<SynZhjInfoDo> selectAllListInfo=synZhjInfoMapper.selectAllInfo();
       if(selectAllListInfo!=null && selectAllListInfo.size()>0){
           final List<RegBusBdcqzsdjxxDo> zsdjInfoList=regBusBdcqzsdjxxMapper.getZsdjInfoList(selectAllListInfo);
           if(zsdjInfoList!=null && zsdjInfoList.size()>0){
               String url=zhjurl+"/dclp/hzapi/token?appKey="+appkey+"&appSecret="+appSecret;
               Map map1=doGetForNew(url);
               if(map1!=null && (Integer)map1.get("code")==200){
                   final Map map2= (Map) map1.get("data");
                   final String token= (String) map2.get("token");
                   //组装宗地基本信息
                   new Thread(new Runnable() {
                       @Override
                       public void run() {
                           System.out.println("===============当前线程是:"+Thread.currentThread().getName());
                           synZdInfo(zsdjInfoList,token);
                       }
                   }).start();

                    //组装户信息
                   new Thread(new Runnable() {
                       @Override
                       public void run() {
                           System.out.println("===============当前线程是:"+Thread.currentThread().getName());
                           synHInfo(zsdjInfoList,token);
                       }
                   }).start();

                   //组装权属状态
                   new Thread(new Runnable() {
                       @Override
                       public void run() {
                           System.out.println("===============当前线程是:"+Thread.currentThread().getName());
                           synQSZTInfo(zsdjInfoList,token);
                       }
                   }).start();
               }
               }
           }
       }

    /**
     *根据业务通过swagger接口手动推送宗地基本信息
     * @param ywh
     * @param
     */
    public void synZdInfoByywh(String ywh){
        List<RegBusBdcqzsdjxxDo> zsdjInfoList =regBusBdcqzsdjxxMapper.getZsdjInfoListByYwh(ywh);
        if(zsdjInfoList!=null && zsdjInfoList.size()>0){
            String url=zhjurl+"/dclp/hzapi/token?appKey="+appkey+"&appSecret="+appSecret;
            Map map1=doGetForNew(url);
            if(map1!=null && (Integer)map1.get("code")==200){
                Map map2= (Map) map1.get("data");
                String token= (String) map2.get("token");
                synZdInfo(zsdjInfoList,token);
            }
        }
    }

    /**
     * 给智慧局推送宗地基本信息
     * @param zsdjInfoList
     * @param token
     */
    public void synZdInfo(List<RegBusBdcqzsdjxxDo> zsdjInfoList,String token)  {
        try{
            if(zsdjInfoList!=null && zsdjInfoList.size()>0){
                for (int i = 0; i < zsdjInfoList.size(); i++) {
                    RegBusBdcqzsdjxxDo busBdcqzsdjxxDo= zsdjInfoList.get(i);
                    List<Map> zdjglist = regBusBdcqzsdjxxMapper.getZdInfoByZddm(busBdcqzsdjxxDo);
                    if(zdjglist!=null && zdjglist.size()>0){
                        for (int j = 0; j < zdjglist.size(); j++) {
                            Map map=zdjglist.get(j);
                            StringBuilder params = new StringBuilder();
                            params.append("?").append("DJQDM=").append(map.get("DJQDM")).append("&")
                                    .append("DJQMC=").append(URLEncoder.encode((String) map.get("DJQMC"),"UTF-8")).append("&")
                                    .append("DJZQDM=").append(map.get("DJZQDM")).append("&")
                                    .append("DJZQMC=").append(URLEncoder.encode((String) map.get("DJZQMC"),"UTF-8")).append("&")
                                    .append("BDCDYH=").append(map.get("BDCDYH")).append("&")
                                    .append("ZL=").append(URLEncoder.encode((String) map.get("ZL"),"UTF-8")).append("&")
                                    .append("ZDMJ=").append(map.get("ZDMJ")).append("&")
                                    .append("YT=").append(URLEncoder.encode((String) map.get("YT"),"UTF-8"));
                            String url1=zhjurl+"/dclp/hzapi/ZDJGMS"+params.toString();
                            Integer code=doPostForNew(url1,"", token);
                            if(code==200){
                                synZhjInfoMapper.updatezdzt(busBdcqzsdjxxDo.getYwh());
                            }
                        }
                    }

                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
    /**
     * 根据业务通过swagger接口手动推送户信息
     * @param ywh
     * @param
     */
    public void synHInfoByYwh(String ywh){
        List<RegBusBdcqzsdjxxDo> zsdjInfoList =regBusBdcqzsdjxxMapper.getZsdjInfoListByYwh(ywh);
        if(zsdjInfoList!=null && zsdjInfoList.size()>0){
            String url=zhjurl+"/dclp/hzapi/token?appKey="+appkey+"&appSecret="+appSecret;
            Map map1=doGetForNew(url);
            if(map1!=null && (Integer)map1.get("code")==200){
                Map map2= (Map) map1.get("data");
                String token= (String) map2.get("token");
                synHInfo(zsdjInfoList,token);
            }
        }
    }
    /**
     * 给智慧局推送户基本信息
     * @param zsdjInfoList
     * @param token
     */
    public void synHInfo(List<RegBusBdcqzsdjxxDo> zsdjInfoList,String token){
        try{
            if(zsdjInfoList!=null && zsdjInfoList.size()>0){
                for (int i = 0; i < zsdjInfoList.size(); i++) {
                    RegBusBdcqzsdjxxDo busBdcqzsdjxxDo= zsdjInfoList.get(i);
                    List<Map> hlist = regBusBdcqzsdjxxMapper.getHQSInfoByZddm(busBdcqzsdjxxDo);
                    if(hlist!=null && hlist.size()>0){
                        for (int j = 0; j < hlist.size(); j++) {
                            Map map=hlist.get(j);
                            StringBuilder params = new StringBuilder();
                            params.append("?").append("BDCDYH=").append(map.get("BDCDYH")).append("&")
                                    .append("ZRZH=").append(map.get("ZRZH")).append("&")
                                    .append("CH=").append(map.get("CH")).append("&")
                                    .append("ZL=").append(URLEncoder.encode((String) map.get("ZL"),"UTF-8")).append("&")
                                    .append("SJCS=").append(map.get("SJCS")).append("&")
                                    .append("SHBW=").append(URLEncoder.encode((String) map.get("SHBW"),"UTF-8")).append("&")
                                    .append("HX=").append(map.get("HX")).append("&")
                                    .append("HXJG=").append(map.get("HXJG")).append("&")
                                    .append("FWYT1=").append(map.get("FWYT1")).append("&")
                                    .append("SCJZMJ=").append(map.get("SCJZMJ")).append("&")
                                    .append("SCTNJZMJ=").append(map.get("SCTNJZMJ")).append("&")
                                    .append("SCDXBFJZMJ=").append(map.get("SCDXBFJZMJ")).append("&")
                                    .append("FTTDMJ=").append(map.get("FTTDMJ")).append("&")
                                    .append("FWLX=").append(map.get("FWLX")).append("&")
                                    .append("FWXZ=").append(map.get("FWXZ")).append("&")
                                    .append("QLLX=").append(map.get("QLLX")).append("&")
                                    .append("DJLX=").append(map.get("DJLX")).append("&")
                                    .append("SYQMJ=").append(map.get("SYQMJ")).append("&")
                                    .append("SYQQSSJ=").append(map.get("SYQQSSJ")).append("&")
                                    .append("SYQJSSJ=").append(map.get("SYQJSSJ")).append("&")
                                    .append("BDCQZH=").append(URLEncoder.encode((String) map.get("BDCQZH"),"UTF-8")).append("&")
                                    .append("QXDM=").append(map.get("QXDM")).append("&")
                                    .append("DJSJ=").append(map.get("DJSJ")).append("&")
                                    .append("QSZT=").append(map.get("QSZT")).append("&")
                                    .append("ZT=").append(map.get("ZT"));

                            String url1=zhjurl+"/dclp/hzapi/HQSJGMS"+params.toString();
                            Integer code=doPostForNew(url1,"", token);
                            if(code==200){
                                synZhjInfoMapper.updatezdzt(busBdcqzsdjxxDo.getYwh());
                            }
                        }
                    }
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }

    }

    /**
     * 给智慧局推送权属状态基本信息
     * @param ywh
     * @param
     */
    public void synQSZTInfoByYwh(String ywh){
        List<RegBusBdcqzsdjxxDo> zsdjInfoList =regBusBdcqzsdjxxMapper.getZsdjInfoListByYwh(ywh);
        if(zsdjInfoList!=null && zsdjInfoList.size()>0){
            String url=zhjurl+"/dclp/hzapi/token?appKey="+appkey+"&appSecret="+appSecret;
            Map map1=doGetForNew(url);
            if(map1!=null && (Integer)map1.get("code")==200){
                Map map2= (Map) map1.get("data");
                String token= (String) map2.get("token");
                synQSZTInfo(zsdjInfoList,token);
            }
        }
    }
    /**
     * 给智慧局推送权属状态基本信息
     * @param zsdjInfoList
     * @param token
     */
    public void synQSZTInfo(List<RegBusBdcqzsdjxxDo> zsdjInfoList,String token){
        if(zsdjInfoList!=null && zsdjInfoList.size()>0){
            for (int i = 0; i < zsdjInfoList.size(); i++) {
                RegBusBdcqzsdjxxDo busBdcqzsdjxxDo= zsdjInfoList.get(i);
                List<Map> qsztList = regBusBdcqzsdjxxMapper.getQSZTInfoByZddm(busBdcqzsdjxxDo);
                if(qsztList!=null && qsztList.size()>0){
                    for (int j = 0; j < qsztList.size(); j++) {
                        Map map=qsztList.get(j);
                        StringBuilder params = new StringBuilder();
                        params.append("?").append("BDCDYH=").append(map.get("BDCDYH")).append("&")
                                .append("DYQSZT=").append(map.get("DYQSZT")).append("&")
                                .append("YYQSZT=").append(map.get("YYQSZT")).append("&")
                                .append("CFQSZT=").append(map.get("CFQSZT")).append("&")
                                .append("YGQSZT=").append(map.get("YGQSZT"));
                        String url1=zhjurl+"/dclp/hzapi/QSZT"+params.toString();
                        Integer code=doPostForNew(url1,"", token);
                        if(code==200){
                            synZhjInfoMapper.updatezdzt(busBdcqzsdjxxDo.getYwh());
                        }
                    }
                }
            }
        }
    }

    public  Map doGetForNew(String url) {
        logger.info("http request url:" + url);
        BufferedReader in = null;
        String result = "";
        Map jsonObject=new HashMap();
        try{
            URL url1=new URL(url);
            HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setRequestProperty("Charset", "utf-8");
            conn.setDoInput(true);
            conn.setDoOutput(false);
            //x-qys-signature生成方式:Md5(AppToken + AppSecret + timestamp),获取32位小写值
            conn.connect();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            jsonObject=JSONObject.parseObject(result,Map.class);
            logger.info("http request back:" + jsonObject);
        }catch (Exception ex){
            logger.info("发送 POST 请求出现异常!" + ex);
            ex.printStackTrace();
        }finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return jsonObject;
    }


    public  Integer doPostForNew(String url, String params,String token) {
        logger.info("http request url:" + url);
        logger.info("http request params:" + params);
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        Map jsonObject=new HashMap();
        Integer code=200;
        try{
            URL url1=new URL(url);
            HttpURLConnection conn= (HttpURLConnection) url1.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
            conn.setRequestProperty("X-Access-Token", token);
            out=new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"utf-8"));
            out.print(params);
            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            jsonObject=JSONObject.parseObject(result,Map.class);
            logger.info("http request back:" + jsonObject);
            code= (Integer) jsonObject.get("code");
        }catch (Exception ex){
            logger.info("发送 POST 请求出现异常!" + ex);
            ex.printStackTrace();
            code=-1;
        }finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return code;
    }



}