package com.gic.haoban.manage.web.controller;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import com.gic.haoban.manage.api.service.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RRateLimiter;
import org.redisson.api.RateIntervalUnit;
import org.redisson.api.RateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ctrip.framework.apollo.ConfigService;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.HttpClient;
import com.gic.haoban.base.api.common.BasePageInfo;
import com.gic.haoban.common.utils.HaobanResponse;
import com.gic.haoban.manage.api.dto.MaidianLogDTO;
import com.gic.haoban.manage.api.dto.QywxCorpInfoSimpleDTO;
import com.gic.haoban.manage.api.dto.SecretSettingDTO;
import com.gic.haoban.manage.api.dto.StaffDTO;
import com.gic.haoban.manage.api.dto.WxApplicationDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseDTO;
import com.gic.haoban.manage.api.dto.WxEnterpriseQwDTO;
import com.gic.haoban.manage.api.enums.SecretTypeEnum;
import com.gic.haoban.manage.api.service.hm.HmQrcodeApiService;
import com.gic.haoban.manage.api.service.out.SecretSettingApiService;
import com.gic.haoban.manage.web.config.Config;
import com.gic.haoban.manage.web.errCode.HaoBanErrCode;
import com.gic.redis.data.util.GicRateLimiter;
import com.gic.redis.data.util.RedisUtil;
import com.gic.wechat.api.dto.qywx.QywxNewUseridDTO;
import com.gic.wechat.api.service.qywx.QywxCorpApiService;
import com.gic.wechat.api.service.qywx.QywxUserApiService;

/**
 * Created 2020/4/9.
 *
 * @author hua
 */
@RestController
@RequestMapping("/test")
public class TestController extends WebBaseController {


    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    private final ExecutorService pools = Executors.newFixedThreadPool(500);

    @Autowired
    private SecretSettingApiService secretSettingApiService;
    @Autowired
    private TestApiService testApiService;
    @Autowired
    private MaidianLogApiService maidianLogApiService;
    @Autowired
    private WxEnterpriseRelatedApiService wxEnterpriseRelatedApiService;
    @Autowired
    private WxEnterpriseApiService wxEnterpriseApiService;
    @Autowired
    private QywxUserApiService qywxUserApiService;
    @Autowired
    private StaffApiService staffApiService;
    @Autowired
    private WxApplicationApiService wxApplicationApiService;
    @Autowired
    private Config config;
    @Autowired
    private HmQrcodeApiService hmQrcodeApiService;
    @Autowired
    private QywxCorpApiService qywxCorpApiService;
    @Autowired
    private QwFriendApiService qwFriendApiService ;

    @RequestMapping("/qwcode")
    public HaobanResponse qwcode(String enterpriseId, String unionid,String openid,String wxaUnionid ,String code) {
        return this.resultResponse(HaoBanErrCode.ERR_0,code);
    }
    
    @RequestMapping("/pending-check")
    public HaobanResponse pendingChec(String enterpriseId, String unionid,String openid,String wxaUnionid ,String wxaOpenid) {
        return this.resultResponse(HaoBanErrCode.ERR_0,this.qwFriendApiService.pendingIdCheck(enterpriseId, unionid, openid, wxaUnionid,wxaOpenid));
    }
    
    @RequestMapping("/get-pending-times")
    public HaobanResponse getTestTimes(String wxEnterpriseId) {
        return this.resultResponse(HaoBanErrCode.ERR_0,this.qwFriendApiService.getCallTimes(wxEnterpriseId));
    }


    @RequestMapping("/test-dubbo")
    public HaobanResponse dubbo(@RequestParam(defaultValue = "2000") Integer n, @RequestParam(defaultValue = "100") String t) {
        int i = n;
        while (i-- > 0) {
            int finalI = i;
            pools.execute(new Runnable() {
                @Override
                public void run() {
                    long start = System.currentTimeMillis();
                    testApiService.checkThread(finalI + "", Long.valueOf(t));
                    logger.info("时间:{}", System.currentTimeMillis() - start);
                }
            });
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    @RequestMapping("/lock")
    public HaobanResponse lock(String name, Long time, int ty) {
        RedisUtil.lock(name, time, TimeUnit.SECONDS, 0L);
        if (ty == 0) {
            if (RedisUtil.isLocked(name)) {
                RedisUtil.unlock(name);
            }
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    @RequestMapping("/maidian-log")
    public HaobanResponse maidian(String hbversion, BasePageInfo pageInfo) {
        Page<MaidianLogDTO> page = maidianLogApiService.queryList(hbversion, pageInfo);
        return resultResponse(HaoBanErrCode.ERR_1, page);
    }

    @RequestMapping("/deal-slef-qywx-sys-department")
    public HaobanResponse dealSys1(String corpid, String secretVal, String dataId) {
        testApiService.listSelfQywxUser(corpid, secretVal, dataId);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    @RequestMapping("/deal-all-qywx-sys-department")
    public HaobanResponse dealSys2(String corpid, String secretVal, String dataId) {
        testApiService.listAllQywxUser(corpid, secretVal, dataId);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    @RequestMapping("/test-limiter")
    public HaobanResponse testLimiter(String key, String eid) {
        if (StringUtils.isAnyBlank(key, eid)) {
            return resultResponse(HaoBanErrCode.ERR_5);
        }
        GicRateLimiter instance = GicRateLimiter.getInstance("haoban-manage3-web");
        Long acquire = instance.acquire(key, eid);
        if (acquire == 0L) {
            return resultResponse(HaoBanErrCode.ERR_13);
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    @RequestMapping("/init-history")
    public HaobanResponse initHistory(String wxEnterpriseId) {
        wxEnterpriseRelatedApiService.initHistoryEnterprise(wxEnterpriseId);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 获取到分表到哪个表内
     *
     * @param tableId 分表id
     * @param count   分表数量
     * @return
     */
    @RequestMapping("/test-table")
    public HaobanResponse testTable(String tableId, Integer count) {
        if (StringUtils.isBlank(tableId)) {
            return resultResponse(HaoBanErrCode.ERR_5);
        }
        if (count == null) {
            return resultResponse(HaoBanErrCode.ERR_5);
        }
        int i = tableId.hashCode() % count;
        return resultResponse(HaoBanErrCode.ERR_1, i);
    }

    /**
     * 设置迁移完成
     *
     * @param corpid
     * @param serviceCorpid
     * @return
     */
    @RequestMapping("/finishExternalUseridMigration")
    public HaobanResponse finishExternalUseridMigration(String corpid, String serviceCorpid) {
        boolean flag = qywxUserApiService.finishExternalUseridMigration(corpid, serviceCorpid);
        if (flag) {
            WxEnterpriseDTO wxEnterpriseDTO = wxEnterpriseApiService.getEnterpriseBycorpId(corpid);
            if (wxEnterpriseDTO == null) {
                return resultResponse(HaoBanErrCode.ERR_10012);
            }
            wxEnterpriseApiService.agreeExternalUseridFlag(wxEnterpriseDTO.getWxEnterpriseId(), 1);
            return resultResponse(HaoBanErrCode.ERR_1);
        } else {
            return resultResponse(HaoBanErrCode.ERR_0);
        }
    }

    /**
     * 测试授权回调
     *
     * @param param
     * @return
     */
    @RequestMapping("/callbackReviceEnterprise")
    public HaobanResponse callbackReviceEnterprise(String param) {
        QywxCorpInfoSimpleDTO qywxCorpInfoSimpleDTO = JSON.parseObject(param, QywxCorpInfoSimpleDTO.class);
        wxEnterpriseApiService.callbackReviceEnterprise(qywxCorpInfoSimpleDTO);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 后台预留批量处理wx_user_id to wx_open_user_id
     *
     * @param corpid
     * @return
     */
    @RequestMapping("/useridToOpenuserid")
    public HaobanResponse useridToOpenuserid(String corpid) {
        WxApplicationDTO wxApplicationDTO = wxApplicationApiService.selectByCorpid(corpid);
        String wxEnterpriseId = wxApplicationDTO.getWxEnterpriseId();
        String siteId = wxApplicationDTO.getSiteId();
        // 限流器
        RRateLimiter rateLimiter = RedisUtil.getRedisClient().getRateLimiter("haoban:manage3:updateWxOpenUserId:limit");
        // 每1秒产生5个令牌
        rateLimiter.trySetRate(RateType.OVERALL, 5, 1, RateIntervalUnit.SECONDS);
        int pageNum = 1;
        while (true) {
            rateLimiter.acquire();
            //批量处理wx_user_id to wx_open_user_id
            Page<StaffDTO> staffDTOPageInfo = staffApiService.listUserIdByWxEnterpriseId(wxEnterpriseId, pageNum, 1000);
            List<StaffDTO> staffDTOS = staffDTOPageInfo.getResult();
            if (CollectionUtils.isEmpty(staffDTOS)) {
                break;
            }
            pageNum++;
            logger.info("useridToOpenuserid请求：corpid：{}", corpid);
            List<QywxNewUseridDTO> qywxNewUseridDTOS = qywxUserApiService.useridToOpenuserid(corpid, siteId, staffDTOS.stream().map(StaffDTO::getWxUserId).collect(Collectors.toList()));
            if (CollectionUtils.isNotEmpty(qywxNewUseridDTOS)) {
                for (QywxNewUseridDTO qywxNewUseridDTO : qywxNewUseridDTOS) {
                    staffApiService.updateOpenUserIdsByUserId(wxEnterpriseId, qywxNewUseridDTO.getUserid(), qywxNewUseridDTO.getOpen_userid());
                }
            }
        }
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 后台单个处理wx_user_id to wx_open_user_id
     *
     * @param corpid
     * @param staffId
     * @return
     */
    @RequestMapping("/useridToOpenuseridByStaffId")
    public HaobanResponse useridToOpenuseridByStaffId(String corpid, String staffId) {
        StaffDTO staffDTO = staffApiService.selectById(staffId);
        if (staffDTO == null) {
            return resultResponse(HaoBanErrCode.ERR_8);
        }
        String wxUserId = staffDTO.getWxUserId();
        String openUserid = getOpenUserid(wxUserId, corpid);
        staffApiService.updateOpenUserIdsByUserId(staffDTO.getWxEnterpriseId(), wxUserId, openUserid);
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 获取openuserid
     *
     * @param userId
     * @param corpid
     * @return
     */
    private String getOpenUserid(String userId, String corpid) {
        WxApplicationDTO wxApplicationDTO = wxApplicationApiService.selectByCorpid(corpid);
        List<QywxNewUseridDTO> qywxNewUseridDTOS = qywxUserApiService.useridToOpenuserid(corpid, wxApplicationDTO.getSiteId(), Collections.singletonList(userId));
        if (CollectionUtils.isEmpty(qywxNewUseridDTOS)) {
            return "";
        }
        return qywxNewUseridDTOS.get(0).getOpen_userid();
    }

    /**
     * 代开发完成升级
     * @param corpid
     * @param agentid
     * @param openidType
     * @return
     */
    @RequestMapping("/finishOpenidMigration")
    public HaobanResponse finishOpenidMigration(String corpid, String agentid, String openidType) {
        String[] openidTypeSplit = openidType.split(",");
        boolean flag = qywxUserApiService.finishOpenidMigration(corpid, config.getCorpid(), agentid, openidTypeSplit);
        return resultResponse(HaoBanErrCode.ERR_1, flag);
    }

    /**
     * 转自建userid
     * @param corpid
     * @param secret
     * @param userIds
     * @return
     */
    @RequestMapping("/getSelfUseridToOpenuserid")
    public HaobanResponse getSelfUseridToOpenuserid(String corpid, String secret, String userIds) {
        String[] split = userIds.split(",");
        List<QywxNewUseridDTO> list = qywxUserApiService.getSelfUseridToOpenuserid(corpid, secret, Arrays.asList(split));
        return resultResponse(HaoBanErrCode.ERR_1, list);
    }

    /**
     * 生成活码
     * @return
     */
    @RequestMapping("/checkStaffOpenContact")
    public HaobanResponse checkStaffOpenContact() {
        hmQrcodeApiService.checkStaffOpenContact("-1");
        return resultResponse(HaoBanErrCode.ERR_1);
    }

    /**
     * 生成活码单企业
     * @param wxEnterpriseId
     * @param enterpriseId
     * @return
     */
    @RequestMapping("/checkStaffOpenContactByWxEnterpriseId")
    public HaobanResponse checkStaffOpenContactByWxEnterpriseId(String wxEnterpriseId,String enterpriseId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("wxEnterpriseId",wxEnterpriseId);
        jsonObject.put("enterpriseId",enterpriseId);
        hmQrcodeApiService.checkStaffOpenContactByWxEnterpriseId(jsonObject.toJSONString());
        return resultResponse(HaoBanErrCode.ERR_1);
    }
    
    
    @RequestMapping("test-qw-post-fws")
    public HaobanResponse testQwPost(String url , String corpid , String json) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getProviderAccessToken(corpid) ;
        logger.info("toekn={},corpId={} , url={} , json={}",token, corpid ,(url + token) , json);
        Map<String, Object> map = HttpClient.getWinxinResByJson(url + token ,json);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    @RequestMapping("test-qw-get-fws")
    public HaobanResponse testQwGet(String url , String corpId) {
        if(isProd()) {
            return null ;
        }
        String token =qywxCorpApiService.getProviderAccessToken(corpId) ;
        logger.info("toekn={},appid={} , url={} , json={}",token,corpId ,(url + token));
        Map<String, Object> map = HttpClient.getHttpByGet(url + token);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    
    @RequestMapping("test-qw-post-3")
    public HaobanResponse testQwPost3(String url , String corpid , String json) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getCorpAccessToken(corpid,config.getWxSuiteid()) ;
        logger.info("toekn={},corpId={} , url={} , json={}",token, corpid ,(url + token) , json);
        Map<String, Object> map = HttpClient.getWinxinResByJson(url + token ,json);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    
    @RequestMapping("test-qw-post-suite")
    public HaobanResponse testQwPostSuite(String url , String corpid , String json) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getSuiteAccessToken(config.getWxSuiteid()) ;
        logger.info("toekn={},corpId={} , url={} , json={}",token, corpid ,(url + token) , json);
        Map<String, Object> map = HttpClient.getWinxinResByJson(url + token ,json);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    
    @RequestMapping("test-qw-token")
    public HaobanResponse testQwPost3(String corpid ,  String secret) {
        if(StringUtils.isBlank(secret)) {
            String token = qywxCorpApiService.getCorpAccessToken(corpid,config.getWxSuiteid()) ;
            return resultResponse(HaoBanErrCode.ERR_0, token) ;
        }else {
            String token = qywxCorpApiService.getToken(corpid, secret) ;
            return resultResponse(HaoBanErrCode.ERR_0, token) ;
        }
    }
    
    @RequestMapping("test-qw-get-3")
    public HaobanResponse testQwGet3(String url , String corpid) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getCorpAccessToken(corpid,config.getWxSuiteid()) ;
        logger.info("toekn={},appid={} , url={} , json={}",token,corpid ,(url + token));
        Map<String, Object> map = HttpClient.getHttpByGet(url + token);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    
    @RequestMapping("test-qw-get-suite")
    public HaobanResponse testQwGetSuite(String url , String corpid) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getSuiteAccessToken(config.getWxSuiteid()) ;
        logger.info("toekn={},appid={} , url={} , json={}",token,corpid ,(url + token));
        Map<String, Object> map = HttpClient.getHttpByGet(url + token);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    @RequestMapping("test-qw-post-dk")
    public HaobanResponse testQwPostdk(String url , String corpid , String secret, String json) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getToken(corpid, secret) ;
        logger.info("toekn={},corpId={} , url={} , json={}",token, corpid ,(url + token) , json);
        Map<String, Object> map = HttpClient.getWinxinResByJson(url + token ,json);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    @RequestMapping("test-qw-get-dk")
    public HaobanResponse testQwGetdk(String url , String corpid , String secret) {
        if(isProd()) {
            return null ;
        }
        String token = qywxCorpApiService.getToken(corpid, secret) ;
        logger.info("toekn={},appid={} , url={} , json={}",token,corpid ,(url + token));
        Map<String, Object> map = HttpClient.getHttpByGet(url + token);
        return resultResponse(HaoBanErrCode.ERR_0, map) ;
    }
    
    private boolean isProd() {
        com.ctrip.framework.apollo.Config config =  ConfigService.getConfig("COMMON.gic-properties");
        String env = config.getProperty("environment.value", "");
        boolean isProd = "prod".equals(env);
        return false ;
    }
    
    @RequestMapping("test-get-qwinfo")
    public Object getQwInfo(String wxEnterpriseId) {
        if(StringUtils.isEmpty(wxEnterpriseId)) {
            return "参数无" ;
        }
        WxEnterpriseQwDTO qwDTO = this.wxEnterpriseApiService.getQwInfo(wxEnterpriseId) ;
        if(null == qwDTO) {
            return "企业不存在" ;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("企微加密说明", "1(全明文) 2(全密文) 3(第三方密文，代开明文) 4(第三方明文，代开密文)") ;
        map.put("当前企业加密方式", getCorpid(qwDTO)) ;
        map.put("企微信息", qwDTO) ;
        
        return map ;
    }
    
    
    private String getCorpid(WxEnterpriseQwDTO wxEnterpriseDTO) {
        int wxType =  wxEnterpriseDTO.getWxSecurityType() ;
        String desc = " -- " ;
        if(wxType==1) {
            desc = "（全部明文）" ;
        }
        if(wxType==2) {
            desc =  "（全部密文）" ;
        }
        if(wxType==3) {
            desc =  "（第三方密文，代开明文）" ;
        }
        if(wxType==4) {
            desc =  "（第三方明文，代开密文）" ;
        }
        return Arrays.asList(wxEnterpriseDTO.getThirdCorpid()+"(第三方)",wxEnterpriseDTO.getDkCorpid()+"(代开发)").stream().filter(str ->  StringUtils.isNotBlank(str)).collect(Collectors.joining(" / ")) + desc;
    }
    
    @RequestMapping("test-bq")
    public Object bq(String wxEnterpriseId) throws UnsupportedEncodingException {
        WxEnterpriseDTO dto = this.wxEnterpriseApiService.getOne(wxEnterpriseId) ;
        if(null == dto) {
            return "wu" ;
        }
        List<String> list = new ArrayList<>();
        String url = dto.getUrlHost().split("#")[0] ;
        String cop = dto.getWxCorpid() ;
        SecretSettingDTO s =  this.secretSettingApiService.getSecretSetting(wxEnterpriseId, SecretTypeEnum.SELF_APP.getVal()) ;
        
        String token = this.qywxCorpApiService.getToken(cop, s.getSecretVal()) ;
        String ip =  dto.getUrlHost().split("#")[1] ;

        String selfUrl = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token=" + token;
        url = url+"/api-qywx-self/qywx/self-post?ip=" + ip + "&url="
                + URLEncoder.encode(selfUrl, "utf-8");
        Map<String, Object> map1 = httpsRequest(url, "GET", null);
        System.out.println(JSONObject.toJSONString(map1));
        String errcode = map1.get("errcode").toString();
        if (!"0".equals(errcode)) {
            return map1;
        }
        JSONArray arr = JSONArray.parseArray(map1.get("tag_group").toString()) ;
        
        for(int i=0;i<arr.size();i++) {
            
            JSONObject obj = arr.getJSONObject(i) ;
//            System.out.println(obj.toJSONString());
            String groupName = obj.getString("group_name") ;
            String groupId = obj.getString("group_id") ;
            String where = " where qywx_group_name ='"+groupName+"' and wx_enterprise_id='"+wxEnterpriseId+"' and status_flag = 1  ; " ;
            //System.out.println("select * from tab_haoban_qywx_tag " + where );
            String str = ("update tab_haoban_qywx_tag set qywx_group_key = '"+groupId+"'" + where  );
            list.add(str) ;
        }
        
        
        list.add("-- ****************************************************** ");
        list.add("UPDATE `tab_haoban_qywx_tag_item` a LEFT JOIN `tab_haoban_qywx_tag` b ON a.qywx_tag_id = b.`qywx_tag_id`   SET a.qywx_group_key = b.`qywx_group_key` WHERE a.status_flag = 1 and  a.wx_enterprise_id = '"+wxEnterpriseId+"' ; ");
        list.add("-- ****************************************************** ");
        for(int i=0;i<arr.size();i++) {
            JSONObject obj = arr.getJSONObject(i) ;
            String groupId = obj.getString("group_id") ;
            JSONArray tagArr = obj.getJSONArray("tag") ;
            for(int j=0;j<tagArr.size();j++) {
                JSONObject tagObj = tagArr.getJSONObject(j) ;
                String tagName = tagObj.getString("name") ;
                String tagId = tagObj.getString("id") ;
                String tagWhere = " where qywx_group_key = '"+groupId+"' and qywx_tag_name ='"+tagName+"' and wx_enterprise_id='"+wxEnterpriseId+"' and status_flag = 1  ; " ;
                //System.out.println("select * from tab_haoban_qywx_tag_item " + tagWhere );
                list.add("update tab_haoban_qywx_tag_item set qywx_tag_key = '"+tagId+"'" + tagWhere  );
            }
        }
        
        return list ;
    }
    
    
    public static Map<String, Object> httpsRequest(String requestUrl, String requestMethod, String outputStr) {
        try {
            URL url = new URL(requestUrl);
            URLConnection conn =  url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求方式（GET/POST）
           // conn.setRequestMethod(requestMethod);
            // 当outputStr不为null时向输出流写数据
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                // 注意编码格式
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            // 从输入流读取返回内容
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            // 释放资源
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
           // conn.disconnect();
            String retStr = buffer.toString();
            Map<String, Object> map = (Map<String, Object>) com.alibaba.fastjson.JSON.parse(retStr);
            logger.info(JSON.toJSONString(map));
            return map;
        } catch (ConnectException ce) {
            System.out.println(ce);
        } catch (Exception e) {
            System.out.println(e);
        }
        return null;
    }

    @RequestMapping("iptest")
    @ResponseBody
    public Object getQwInfo(HttpServletRequest req) {
        String ip = getIpAddr(req) ;
        logger.info("企微test请求ip={}",ip);
        JSONObject json = new JSONObject() ;
        json.put("errcode","0");
        json.put("errmsg",ip) ;
        return json ;
    }

    public static String getIpAddr(HttpServletRequest request){
        String ip = request.getHeader("X-Forwarded-For-Pound") ;
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
          return ip;
        }
        ip = request.getHeader("X-Real-IP");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个IP值，第一个为真实IP。
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        } else {
            return request.getRemoteAddr();
        }
    }

    public static String httpRequest(String requestUrl, String requestMethod, String outputStr, int timeOut) {
        if (timeOut == 0) {
            timeOut = 1000;
        }
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setReadTimeout(timeOut);
            conn.setRequestMethod(requestMethod);
            if (null != outputStr) {
                OutputStream outputStream = conn.getOutputStream();
                outputStream.write(outputStr.getBytes("UTF-8"));
                outputStream.close();
            }
            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            StringBuilder buffer = new StringBuilder();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            inputStream.close();
            inputStream = null;
            conn.disconnect();
            return buffer.toString();
        } catch (ConnectException ce) {
            ce.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("self-check")
    @ResponseBody
    public Object self(String ips){
        if(StringUtils.isEmpty(ips)) {
            ips = "121.36.247.198,60.204.155.186,123.60.35.52,124.70.140.207,121.37.130.214,122.9.64.47,116.63.51.145,122.9.68.94,122.9.190.110" ;
        }
        String[] arr = ips.split(",") ;
        Map<String,String> map = new HashMap<>() ;
        for(String ip : arr) {
            String s = httpRequest("http://"+ip+":8990/api-qywx-self/index.html","GET",null,3000) ;
            logger.info("s={},{}",ip,s);
            map.put(ip,s) ;
        }
        return map ;
    }
}
