package com.jzq;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jzq.common.ResultInfo;
import com.jzq.common.bean.sign.SignatoryReq;
import com.jzq.common.http.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import java.io.File;
import java.util.Date;
import java.util.IdentityHashMap;
import java.util.Map;
/**
 * 
 * - 项目名称 : 后台服务*
- 文件名称 : JzqHttpApiTest*
- 创建时间 : 2019/8/23 9:14*
- 描 述 :  用于jzq的api服请求
 * 
*
 * @author yfx
 * @title 用于jzq的api服请求
 */
@Slf4j
public class JzqHttpApiTool {
    /**
     * 君子签测试环境key和接口地址:
     * appKey:dcb4bd535a09df3c
     * appSecret:b87c346edcb4bd535a09df3ca8c45d9a
     * services_url:https://api.sandbox.junziqian.com
     * 开发文档: https://s.junziqian.com/api_doc/index.html
     */
    private static String SERVICE_URL="https://api.sandbox.junziqian.com";
    private static  String APP_KEY="dcb4bd535a09df3c";
    private static  String APP_SECRET="b87c346edcb4bd535a09df3ca8c45d9a";
    public JzqHttpApiTool(String url,String appKey,String appSecret){
        SERVICE_URL = url;
        APP_SECRET = appSecret;
        APP_KEY = appKey;
        initParams();
    }
    //请求的body内参数
    private static  Map bodyParams;
    public void initParams(){
        long ts=System.currentTimeMillis();
        String nonce=DigestUtils.md5Hex(System.currentTimeMillis()+"");
        String sign=DigestUtils.sha256Hex("nonce"+nonce+"ts"+ts+"app_key"+APP_KEY+"app_secret"+APP_SECRET);
        bodyParams=new IdentityHashMap<>();
        bodyParams.put("ts",ts);
        bodyParams.put("app_key",APP_KEY);
        bodyParams.put("sign",sign);
        bodyParams.put("nonce",nonce);//这只只是为了生成一个随机值
    }
    /**
     * 1.ping服务
     */
    public boolean ping(){
        initParams();
        try {
            Map  params=bodyParams;
            String url=SERVICE_URL+"/v2/ping";
            //所有参数装入了body中
            String str= HttpClientUtils.init().getPost(url,null,params,false);
            log.info("返回结果为:"+str);
            ResultInfo ri= JSONObject.parseObject(str,ResultInfo.class);
            return ri.isSuccess();
        }catch (Exception e){
        }
        return false;
    }
    /**
     * 发起签约 (君子签后台配置模版ID发起)
     * @param fullname
     * @param creditCode
     * @param legalName
     * @param email
     * @param businessimg
     * @param notifyUrl
     * @return
     */
    public boolean organizationCreate (String fullname,String creditCode,String legalName,String email,File  businessimg,String notifyUrl){
        try {
            initParams();
            Map  params=bodyParams;
            String url=SERVICE_URL+"/v2/user/organizationCreate";
            params.put("name",fullname);
            params.put("identificationType",1);
            params.put("organizationRegNo",creditCode);
            params.put("organizationType",0);
            params.put("organizationCode",creditCode);
            params.put("organizationRegImg",businessimg);
            params.put("notifyUrl",notifyUrl);
            params.put("legalName",legalName);
            params.put("emailOrMobile",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  true;
            }
            System.out.println(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 发起签约 (君子签后台配置模版ID发起)
     * @param fullname
     * @param creditCode
     * @param legalName
     * @param email
     * @param businessimg
     * @param notifyUrl
     * @return
     */
    public boolean organizationReApply(String fullname,String creditCode,String legalName,String email,File  businessimg,String notifyUrl){
        try {
            initParams();
            Map  params=bodyParams;
            String url=SERVICE_URL+"/v2/user/organizationReapply";
            params.put("name",fullname);
            params.put("identificationType",1);
            params.put("organizationRegNo",creditCode);
            params.put("organizationType",0);
            params.put("organizationCode",creditCode);
            params.put("organizationRegImg",businessimg);
            params.put("notifyUrl",notifyUrl);
            params.put("legalName",legalName);
            params.put("emailOrMobile",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  true;
            }
            System.out.println(str);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 查询企业签约状态 审核状态,0正在申请1通过2驳回
     * @param email
     * @return
     */
    public int  organizationAuditStatus (String email){
        try {
            initParams();
            Map  params=bodyParams;
            String url=SERVICE_URL+"/v2/user/organizationAuditStatus";
            params.put("emailOrMobile",email);
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getJSONObject("data").getIntValue("status");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  -1;//查询失败
    }
    /**
     * 发起签约
     * @param name
     * @param file
     * @param fullname
     * @param creditCoe
     * @param email
     * @param postionJson
     */
    public String applySign(String name,File file,String fullname,String creditCoe,String email,String postionJson){
      try {
          String url=SERVICE_URL+"/v2/sign/applySign";
          initParams();
          Map  params=bodyParams;
          params.put("contractName",name); //合同名称
          params.put("serverCa",1); //使用云证书
          params.put("file",file);
          params.put("dealType",5); //指定合同文件签署方式 5 为部分自动签
          params.put("positionType",0); //指定通过表单域方式设置签字位置
          params.put("fileType",0);
          params.put("needQifengSign",1);
          JSONArray signatories=new JSONArray();
          SignatoryReq sReq=new SignatoryReq();
          sReq.setFullName(fullname); //企业姓名
          sReq.setIdentityType(11); //证件类型
          sReq.setIdentityCard(creditCoe);//营业执照号
          sReq.setEmail(email); //在君子签注册认证的邮箱
//        sReq.setChapteJson("[{\"page\":0,\"chaptes\":[{\"offsetX\":0.12,\"offsetY\":0.23}]},{\"page\":1,\"chaptes\":[{\"offsetX\":0.45,\"offsetY\":0.67}]}]");
          sReq.setChapteJson(postionJson);
          sReq.setNoNeedVerify(1);
          signatories.add(sReq);
          params.put("signatories",signatories.toJSONString());
          System.out.println(signatories.toJSONString());
          String str= HttpClientUtils.init().getPost(url,null,params,true);
          System.out.println(str);
          JSONObject json = JSONObject.parseObject(str);
          if(json!=null && json.getBoolean("success")){
              return  json.getString("data");
          }
      }catch (Exception e){
      }
      return null;
    }
    /**
     * 获取签署链接地址
     * @param applyNo
     * @param name
     * @param creditCode
     */
    public String signLink(String applyNo,String name,String creditCode) {
        try {
            initParams();
            Map params = bodyParams;
            String url = SERVICE_URL + "/v2/sign/link";
            params.put("applyNo",applyNo); //发起合同签署接口返回的APL编号
            params.put("fullName",name); //发起合同签署接口需要手动签署对象的姓名
            params.put("identityCard",creditCode); //发起合同签署接口中需要手动签署对象的证件号
            params.put("identityType",11); //证件类型,个人1 ,企业11
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getString("data");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    /**
     * 获取签署链接地址
     * @param applyNo
     */
    public String  linkFile(String applyNo) {
        try {
            initParams();
            Map params = bodyParams;
            String url = SERVICE_URL + "/v2/sign/linkFile";
            //构建请求参数
            params.put("applyNo",applyNo); //发起合同签署接口返回的APL编号
            String str= HttpClientUtils.init().getPost(url,null,params,true);
            System.out.println(str);
            JSONObject json = JSONObject.parseObject(str);
            if(json!=null && json.getBoolean("success")){
                return  json.getString("data");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
}