package com.sifang.modules.kingdee.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.kingdee.bos.webapi.entity.RepoResult;
import com.kingdee.bos.webapi.entity.RepoRet;
import com.kingdee.bos.webapi.entity.RepoStatus;
import com.kingdee.bos.webapi.entity.SuccessEntity;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.sifang.modules.kingdee.constants.KingDeeConstants;
import com.sifang.modules.kingdee.vo.KingDeeResponseVo;
import com.sifang.modules.kingdee.vo.RepoRetVo;
import com.sifang.modules.sunshine.SystemSetting.utils.BusinessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 金蝶接口
 */
public class KingDeeUtil {

    public  static Logger logger= LoggerFactory.getLogger(KingDeeUtil.class);


    //保存
    public static KingDeeResponseVo kingDeeClitSave(String jsonData, String formId, Gson gson, K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitSave  jsonData:{},formId:{}",jsonData,formId);
        //调用接口
        String resultJson = client.save(formId,jsonData);
        logger.info("kingDeeClitSave接口返回结果:resultJson {}",resultJson);
        //对返回结果进行解析和校验
        RepoRetVo repoRet = gson.fromJson(resultJson, RepoRetVo.class);
        if (repoRet.isSuccessfully()) {
            logger.info("kingDeeClitSave接口返回结果: {}", gson.toJson(repoRet.getResult()));
            return repoRet.getResult();
        } else {
            logger.info("kingDeeClitSave接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    //提交
    public static void kingDeeClitSubmit(String jsonData, String formId,Gson gson,K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitSubmit  jsonData:{},formId:{}",jsonData,formId);
        //调用接口
        String resultJson = client.submit(formId,jsonData);
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
        if (repoRet.isSuccessfully()) {
            logger.info("kingDeeClitSubmit接口返回结果:{}", gson.toJson(repoRet.getResult()));

        } else {
            logger.info("kingDeeClitSubmit接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    //审核
    public static void kingDeeClitAudit(String jsonData, String formId,Gson gson,K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitAudit  jsonData:{},formId:{}",jsonData,formId);
        String resultJson = client.audit(formId,jsonData);
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
        if (repoRet.isSuccessfully()) {
            logger.info("kingDeeClitAudit接口返回结果:{}", gson.toJson(repoRet.getResult()));

        } else {
            logger.info("kingDeeClitAudit接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    //反审核
    public static void kingDeeClitUnAudit(String jsonData, String formId,Gson gson,K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitUnAudit  jsonData:{},formId:{}",jsonData,formId);
        String resultJson = client.unAudit(formId,jsonData);
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
        if (repoRet.isSuccessfully()) {
            logger.info("kingDeeClitUnAudit 接口返回结果:{}", gson.toJson(repoRet.getResult()));

        } else {
            logger.info("kingDeeClitUnAudit 接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }


    //删除
    public static RepoResult kingDeeClitDelete(String jsonData, String formId,Gson gson,K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitDelete  jsonData:{},formId:{}",jsonData,formId);
       /* K3CloudApi client = new K3CloudApi();
        //调用接口*/
        String resultJson = client.delete(formId,jsonData);
       /* //用于记录结果
        Gson gson = new Gson();*/
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
        if (repoRet.isSuccessfully()) {
            logger.info("kingDeeClitDelete接口返回结果:{}", gson.toJson(repoRet.getResult()));
            return repoRet.getResult();
        } else {
            logger.info("kingDeeClitDelete接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    //禁用Forbid/反禁用Enable
    public static RepoResult kingDeeClitStatusOperation(String jsonData, String formId,String operateNumber,Gson gson,K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitStatusOperation  jsonData:{},formId:{}",jsonData,formId);
        //调用接口
        String resultJson = client.excuteOperation(formId,operateNumber,jsonData);
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
       logger.info(">>>>>>>>>>>>kingDeeClitStatusOperation>repoRet:{}",repoRet.getResult().getResponseStatus());
        if (repoRet.isSuccessfully()) {
            logger.info("kingDeeClitStatusOperation接口返回结果: {}", gson.toJson(repoRet.getResult()));
            return repoRet.getResult();
        } else {
            logger.info("kingDeeClitStatusOperation接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+ gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    //查看
    public static JsonArray kingDeeClitDetail(String jsonData, String formId,Gson gson,K3CloudApi client) throws Exception {
        logger.info(">>>>>>>>>>>>>kingDeeClitDetail  jsonData:{},formId:{}",jsonData,formId);
        //调用接口
        String resultJson = client.view(formId,jsonData);
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
        logger.info(">>>>>>>>>>>>>kingDeeClitDetail  resultJson:{}",resultJson);
        if (repoRet.isSuccessfully()) {
            JsonObject gsofn = gson.fromJson(resultJson, JsonObject.class);
            JsonObject resu = gsofn.getAsJsonObject("Result");
            JsonObject resu2 = resu.getAsJsonObject("Result");
            JsonArray array = gson.fromJson(resu2.get("InStockEntry"), JsonArray.class);
            logger.info("kingDeeClitDetail接口返回结果:{}", gson.toJson(repoRet.getResult()));
            return array;
        } else {
            logger.info("kingDeeClitDetail接口返回结果: " + gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new IllegalAccessException("推送金蝶消息有误，错误信息："+gson.toJson(repoRet.getResult().getResponseStatus()));
        }
    }

    //PRE001
    public static ArrayList<SuccessEntity>  kingDeeClitPush(String jsonData, String formId, Gson gson, K3CloudApi client) throws Exception {

        String resultJson = client.push(formId,jsonData);
        logger.info("请求报文 -------------  {}",jsonData);
        //对返回结果进行解析和校验
        RepoRet repoRet = gson.fromJson(resultJson, RepoRet.class);
        if (repoRet.getResult().getResponseStatus().isIsSuccess()) {
            logger.info(">>>>>>>>>>>>>kingDeeClitPush 接口返回结果:{}", gson.toJson(repoRet.getResult()));
            return repoRet.getResult().getResponseStatus().getSuccessEntitys();
        } else {
            logger.info(">>>>>>>>>>>>>kingDeeClitPush 接口返回错误结果:{}" ,gson.toJson(repoRet.getResult().getResponseStatus()));
            throw new BusinessException("下推失败");
        }
    }

    public static List<String> getNumberList(String number){
        List<String> list=new ArrayList<>();
        list.add(number);
        return list;
    }

    public static void main(String[] arr) throws Exception {
         String json="{\"Ids\":\"103707\"}";
         Gson gson=new Gson();
        K3CloudApi cloudApi=new K3CloudApi();
        kingDeeClitSubmit(json,KingDeeConstants.TRADER_FORM_ID,gson,cloudApi);
        kingDeeClitAudit(json, KingDeeConstants.TRADER_FORM_ID,gson,cloudApi);
    }
}
