package shkd.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.ByteString;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DobeDWUtils {
    //数仓和用友相关的工具类 数仓和用友接口url和appcode在此申明
    //数仓接口的appCode 可能会更换
    public static final String appCode = "AppCode 895894e0-73de-40a9-9b1f-e87fe35a13fd";
    //数仓接口地址基本信息
    public static final String dwUrl = "https://wedc.dobechina.com/webroot/service/publish/7e9d818c-43c0-47cf-ad4c-40c66b92bb34/";
    public static final MediaType MTJSON = MediaType.get("application/json");

    //授权模式,客户端模式为client,密码模式为:password
//    public static final String granttype = "client_credentials";
    //BIP第三方应用id,对应系统中的app_id
    public static final String clientid = "ISC";
    //BIP 第三方应用秘钥,对请求加签使用
    public static final String clientsecret = "cb729cf706934e97aa84";
    //访问的BIP系统的账套code
    public static final String bizcenter = "01";
    //BIP公钥,加解密使用
    public static final String pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8cLD7SZ8W15DF+XZ5+WURaxFP6TBvUG6fabTEh5oV2NFdsowuS88fFUx3rh0KqWQ3VtRRCVjHgGBuPIDakEZ8rn+kDq3BV10+ihJunuqqP/LBSr4Y2JYHj6vqwYw6K3raH4li/sAR02rQdoR922OU2bPxc3BlLTA0yLFqjo/3/Bf8DvYIo/yZ+enK1glK6SYVX7+dLv2a8sWb2MtUDm7JXW14rqwuO9dwlTTBTbrZ3AKubvaE7qXXqbJdYDLGTiLSD2mXJa7vwO+6ZTQJDN4sJUCeFchNMeB2xIjzbV3ZpL75xKPbbRnhANpswiaRN5OhtQcUKK3Y0+TRiihfIc3RQIDAQAB";
    //用友接口IP地址
    public static final String yyip = "106.14.25.83";
    //用友接口端口
//    public static final String yyport = "9999";
    public static final String yyport = "8888";
    //用友付款单新增接口
//    public static final String payUrl = "http://106.14.25.83:9999/nccloud/api/arap/paybill/insertandcommit";
    public static final String payUrl = "http://106.14.25.83:8888/nccloud/api/arap/paybill/insertandcommit";
    //付款金额查询接口
//    public static final String payQueryUrl = "http://106.14.25.83:9999/nccloud/api/sp/spPaybillQuery/querySettleStatus";
    public static final String payQueryUrl = "http://106.14.25.83:8888/nccloud/api/sp/spPaybillQuery/querySettleStatus";

    private static final String jklogEntityName = "qeug_recon_jklog";

    public static boolean isEmpty(String value) {
        return value == null || value.trim().length() <= 0;
    }

    public static String[] getCompanyDeptNumber(String bizDept){
        //根据用款部门的编号获得对应关系表中的财务公司编号和部门编号
        DynamicObject orginfo = BusinessDataServiceHelper.loadSingle("qeug_recon_orgrelation",new QFilter[]{new QFilter("number","=",bizDept)});
        String[] result = new String[3];
        if(orginfo != null){
            result[0] = orginfo.getString("qeug_companynumber");//财务公司编号
            result[1] = orginfo.getString("qeug_deptnumber");//财务部门编号
            result[2] = orginfo.getString("qeug_paynumber");//财务公司付款账户
//            result[2] = "03002635967";
        }
        return result;
    }

    public static void saveLog(String billno,String jkname,String inputs,String outputs,boolean isSuccess,String operation){
        //保存星瀚与第三方接口调用之间的日志记录
        //参数说明:单据编号、接口名称、接口入参、接口返回值、接口执行结果是否成功、操作名称(audit、unaudit等)
        DynamicObject billinfo = BusinessDataServiceHelper.newDynamicObject(jklogEntityName);
        billinfo.set("number",billno);
        billinfo.set("name",jkname);
        billinfo.set("qeug_inputs_tag",inputs);//大文本赋值,标识后加_tag 是内容,不加是文本标题
        billinfo.set("qeug_outputs_tag",outputs);
        billinfo.set("qeug_issuccess",isSuccess);
        billinfo.set("qeug_operation",operation);
        billinfo.set("status","A"); //单据状态默认暂存
        long currUserId = RequestContext.get().getCurrUserId();//当前用户 creator
        billinfo.set("creator",currUserId);
        SaveServiceHelper.save(new DynamicObject[]{billinfo});
    }

    public static String getDateString(Date billDate){
        //创建一个SimpleDateFormat对象,定义目标日期格式
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //格式化Date对象为新的字符串格式
        if(billDate == null){
            return targetFormat.format(new Date());
        }
        return targetFormat.format(billDate);
    }

    public static String getCurrentDateString(){
        //创建一个SimpleDateFormat对象,定义目标日期格式
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd");
        //格式化Date对象为新的字符串格式
        return targetFormat.format(new Date());
    }

    public static int getQueryCount(int totalNum){
        //根据入参 计算总查询次数
        //先判断大小,如果小于固定条数300,则返回1
        if(300 >= totalNum){
            return 1;
        }
        //再取余数,如果余数大于零,则返回1+商
        int ys = totalNum % 300;
        if(ys > 0){
            return totalNum / 300 + 1;
        }else{
            return totalNum / 300;
        }
    }

    public static RequestBody createRequestBody(String params, int pageNum) {
        JSONObject json_body = new JSONObject();
        JSONObject json_detail = new JSONObject();
        json_detail.put("pageSize","300");//一页总条数固定为300
        if("account".equals(params)){
            //如果是成本科目或者会计科目,不进行多次取数,默认设置3000长度,一次取完
            json_detail.put("pageSize","3000");
        }
        json_detail.put("pageNum",pageNum);
        json_body.put("paging",json_detail);
        if("person".equals(params)){
            JSONArray ps = new JSONArray();
            JSONObject psjson = new JSONObject();
            psjson.put("name","entry_type");
            psjson.put("value","");
            ps.add(psjson);

            psjson = new JSONObject();
            psjson.put("name","employ_status");
            psjson.put("value","");
            ps.add(psjson);

            psjson = new JSONObject();
            psjson.put("name","s_date");
            psjson.put("value","");
            ps.add(psjson);

            psjson = new JSONObject();
            psjson.put("name","e_date");
            psjson.put("value","");
            ps.add(psjson);
            json_body.put("params",ps);
        }else if("project".equals(params) || "account".equals(params)){
            JSONArray ps = new JSONArray();
            JSONObject psjson = new JSONObject();
            psjson = new JSONObject();
            psjson.put("name","s_time");
            psjson.put("value","");
            ps.add(psjson);

            psjson = new JSONObject();
            psjson.put("name","e_time");
            psjson.put("value",getDateString(null));
            ps.add(psjson);
            json_body.put("params",ps);
        }else{
            json_body.put("params",new JSONArray());
        }
        //如下写法创建的请求只是json格式,不带charset-utf8,有些接口控制了这两者区别,故需要特别指定
        return RequestBody.create(ByteString.encodeUtf8(json_body.toJSONString()), MTJSON);
    }

    public static String getTestOrgString(){
        JSONObject json_body = new JSONObject();
        json_body.put("totalNum","1");
        JSONObject json_detail = new JSONObject();
        json_detail.put("org_code","test001-01");
        json_detail.put("org_name","test接口修改组织");
        json_detail.put("org_id","1234567890");
        json_detail.put("org_parentid","111110101011");
        json_detail.put("org_shortname","test修改");

        JSONArray array = new JSONArray();
        array.add(json_detail);

//        JSONObject json_detail1 = new JSONObject();
//        json_detail1.put("org_code","test002");
//        json_detail1.put("org_name","test接口新增组织2");
//        json_detail1.put("org_id","1234567891");
//        json_detail1.put("org_parentid","111110101011");
//        json_detail1.put("org_shortname","test2");
//        array.add(json_detail1);

        json_body.put("data",array);
        return json_body.toJSONString();
    }

    public static String getTestAcct(){
        JSONObject json_body = new JSONObject();
        json_body.put("totalNum","1");
        json_body.put("projectnumber","test0667");
        JSONObject json_detail = new JSONObject();
        json_detail.put("accid","100003");
        json_detail.put("parentid","");
        json_detail.put("number","01");
        json_detail.put("name","ziji科目1");
        json_detail.put("longnumber","01");
        json_detail.put("longname","ziji科目1");
        json_detail.put("ciaccountflag","1");
        json_detail.put("taxrate","6");
        json_detail.put("isleaf","1");
        json_detail.put("level","2");

        JSONArray array = new JSONArray();
        array.add(json_detail);

        json_detail = new JSONObject();
        json_detail.put("accid","100004");
        json_detail.put("parentid","100003");
        json_detail.put("number","02");
        json_detail.put("name","ziji科目2");
        json_detail.put("longnumber","02");
        json_detail.put("longname","ziji科目2");
        json_detail.put("ciaccountflag","1");
        json_detail.put("taxrate","3");
        json_detail.put("isleaf","1");
        json_detail.put("level","2");

        array.add(json_detail);

        json_body.put("data",array);
        return json_body.toJSONString();
    }

    public static String getTestProject(){
        JSONObject json_body = new JSONObject();
        json_body.put("totalNum","1");
        JSONObject json_detail = new JSONObject();
        json_detail.put("fbillno","test0667");
        json_detail.put("fbillname","test接口项目");
        json_detail.put("fversionnum","gs006");
        json_detail.put("forgid","111110101011");
        json_detail.put("fisleaf","1");
        json_detail.put("fprojectstageid","拿地阶段");

        JSONArray array = new JSONArray();
        array.add(json_detail);

//        JSONObject json_detail1 = new JSONObject();
//        json_detail1.put("org_code","test002");
//        json_detail1.put("org_name","test接口新增组织2");
//        json_detail1.put("org_id","1234567891");
//        json_detail1.put("org_parentid","111110101011");
//        json_detail1.put("org_shortname","test2");
//        array.add(json_detail1);

        json_body.put("data",array);
        return json_body.toJSONString();
    }

    public static String getTestOrgRelation(){
        JSONObject json_body = new JSONObject();
        json_body.put("totalNum","1");
        JSONObject json_detail = new JSONObject();
        json_detail.put("orgNumber","test0667");
        json_detail.put("orgName","test接口修改组织66");
        json_detail.put("companyNumber","gs006");
        json_detail.put("companyName","接口测试财务公司6");
        json_detail.put("deptNumber","caiwudept2");
        json_detail.put("deptName","财务部门2");

        JSONArray array = new JSONArray();
        array.add(json_detail);

//        JSONObject json_detail1 = new JSONObject();
//        json_detail1.put("org_code","test002");
//        json_detail1.put("org_name","test接口新增组织2");
//        json_detail1.put("org_id","1234567891");
//        json_detail1.put("org_parentid","111110101011");
//        json_detail1.put("org_shortname","test2");
//        array.add(json_detail1);

        json_body.put("data",array);
        return json_body.toJSONString();
    }

    public static String getTestUserString(){
        JSONObject json_body = new JSONObject();
        json_body.put("totalNum","1");
        JSONObject json_detail = new JSONObject();
        json_detail.put("user_id", "1234567800");//人员id
        json_detail.put("user_code", "10000001");//人员编码
        json_detail.put("user_name", "张三三");//姓名
        json_detail.put("username", "用户名");//用户名
        json_detail.put("usertype", "1");//用户类型1:
        json_detail.put("employ_status", "0");//在职状态
        json_detail.put("mobile_phone", "13800000011");//手机号
        json_detail.put("email", "email11@kingdee.com");//电子邮箱
        json_detail.put("idcard", "");//身份证
        json_detail.put("birthday", "1993-8-8");//生日
        json_detail.put("gender", "1");//性别1男 0女
        json_detail.put("department_id", "1234567890");
        json_detail.put("jobposition", "项目经理");

        JSONArray array = new JSONArray();
        array.add(json_detail);

//        JSONObject json_detail1 = new JSONObject();
//        json_detail1.put("org_code","test002");
//        json_detail1.put("org_name","test接口新增组织2");
//        json_detail1.put("org_id","1234567891");
//        json_detail1.put("org_parentid","111110101011");
//        json_detail1.put("org_shortname","test2");
//        array.add(json_detail1);

        json_body.put("data",array);
        return json_body.toJSONString();
    }
}