package com.sifang.modules.kingdee.service;

import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.RepoResult;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.sifang.modules.flowpath.entity.CashOutApply;
import com.sifang.modules.kingdee.constants.KingDeeConstants;
import com.sifang.modules.kingdee.util.KingDeeUtil;
import com.sifang.modules.kingdee.vo.FNumberVo;
import com.sifang.modules.kingdee.vo.KingDeeResponseVo;
import com.sifang.modules.sunshine.SystemSetting.entity.CrmTrader;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class KingDeePayService extends BaseService {


    //保存
    public String kingDeeSaveCashOut(CashOutApply cashOutApply, CrmTrader trader,String FID) throws Exception {

        logger.info(">>>>>>>>>>>kingDeeSaveCashOut 推入金蝶系统 cashOutApply:{}",cashOutApply.getId());
        Map<String,Object> data=new HashMap<>();

        data.put("ValidateFlag", false);  //是否验证数据合法性标志
        data.put("NumberSearch",false);  //是否用编码搜索基础资料

        Map<String,Object> model=new HashMap<>();
        model.put("FID", FID);
        model.put("FBILLTYPEID", new FNumberVo());  //单据类型
        model.put("FBillNo", cashOutApply.getApplyNumber());  //单据编号
        model.put("FDATE", DateUtils.formatDate(cashOutApply.getCreateTime(), "yyyy-MM-dd"));   //申请日期
        model.put("FCONTACTUNITTYPE", "BD_Supplier");  //往来单位类型
        model.put("FCONTACTUNIT", new FNumberVo(trader.getKingDeeNumber()));  //往来单位
        model.put("FRECTUNITTYPE", "BD_Supplier");   //收款单位类型
        model.put("FRECTUNIT", new FNumberVo(trader.getKingDeeNumber()));  //收款单位
        model.put("FCURRENCYID", new FNumberVo("PRE001"));  //币别
        model.put("FPAYORGID", new FNumberVo("R001"));  //付款组织
        model.put("FSETTLEORGID", new FNumberVo("R001"));  //结算组织
        model.put("FDOCUMENTSTATUS", "");    //单据状态

        model.put("FCANCELSTATUS", "");  //作废状态
        model.put("FAPPLYORGID", new FNumberVo("R001"));  //申请组织
        model.put("FSETTLECUR", new FNumberVo("PRE001"));  //结算币别

        String date=DateUtils.getDate();
        List<Object> list=new ArrayList<>();
        Map<String,Object> FPAYAPPLYENTRY=new HashMap<>();
        FPAYAPPLYENTRY.put("FENDDATE", date);  // 到期日
        FPAYAPPLYENTRY.put("FENDDATE", date);   //期望付款日期
        FPAYAPPLYENTRY.put("FAPPLYAMOUNTFOR", cashOutApply.getOutCash()); //申请付款金额
        list.add(FPAYAPPLYENTRY);
        model.put("FPAYAPPLYENTRY", list);

        data.put("Model", model);

        Gson gson = new Gson();
        String json = gson.toJson(data);

        K3CloudApi client = new K3CloudApi();
        //保存
        KingDeeResponseVo result = KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        String id=result.getId();

        Map<String, Object> data2 = new HashMap<>();
        data2.put("Ids", id);
        String json2 = gson.toJson(data2);
        //提交
        KingDeeUtil.kingDeeClitSubmit(json2, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        //审核
        KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        return id;
    }

    //修改
    public String kingDeeEditCashOut(CashOutApply cashOutApply) throws Exception {

        logger.info(">>>>>>>>>>>kingDeeEditCashOut 推入金蝶系统 cashOutApply:{}",cashOutApply.getId());
        Map<String, Object> data1 = new HashMap<>();
        data1.put("Ids", cashOutApply.getKingDeeId());

        Gson gson = new Gson();
        K3CloudApi client = new K3CloudApi();
        String json1 = gson.toJson(data1);
        //反审核
        KingDeeUtil.kingDeeClitUnAudit(json1,KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);

        Map<String,Object> data=new HashMap<>();

        data.put("ValidateFlag", false);  //是否验证数据合法性标志
        data.put("NumberSearch",false);  //是否用编码搜索基础资料

        Map<String,Object> model=new HashMap<>();
        model.put("FBILLTYPEID", new FNumberVo(""));  //单据类型
        model.put("FBillNo", cashOutApply.getApplyNumber());  //单据编号
        model.put("FDATE", DateUtils.formatDate(cashOutApply.getCreateTime(), "yyyy-MM-dd"));   //申请日期
        model.put("FCONTACTUNITTYPE", "");  //往来单位类型
        model.put("FCONTACTUNIT", new FNumberVo(""));  //往来单位
        model.put("FRECTUNITTYPE", "");   //收款单位类型
        model.put("FRECTUNIT", new FNumberVo(""));  //收款单位
        model.put("FCURRENCYID", new FNumberVo(""));  //币别
        model.put("FPAYORGID", new FNumberVo(""));  //付款组织
        model.put("FSETTLEORGID", new FNumberVo(""));  //结算组织
        model.put("FDOCUMENTSTATUS", "0");    //单据状态

        model.put("FID", cashOutApply.getKingDeeId());

        data.put("Model", model);


        String json = gson.toJson(data);


        //保存
        KingDeeResponseVo result = KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        String id=result.getId();

        Map<String, Object> data2 = new HashMap<>();
        data2.put("Ids", id);
        String json2 = gson.toJson(data2);
        //提交
        KingDeeUtil.kingDeeClitSubmit(json2, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        //审核
        KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        return id;
    }

    public static void main(String[] arr) throws Exception {
        Map<String,Object> data=new HashMap<>();

        data.put("ValidateFlag", false);  //是否验证数据合法性标志
        data.put("NumberSearch",false);  //是否用编码搜索基础资料

        Map<String,Object> model=new HashMap<>();
        model.put("FBILLTYPEID", new FNumberVo());  //单据类型
        model.put("FBillNo", "202307667");  //单据编号
        model.put("FDATE", "2023-07-05");   //申请日期
        model.put("FCONTACTUNITTYPE", "BD_Customer");  //往来单位类型
        model.put("FCONTACTUNIT", new FNumberVo("CUST0001"));  //往来单位
        model.put("FRECTUNITTYPE", "BD_Customer");   //收款单位类型
        model.put("FRECTUNIT", new FNumberVo("CUST0001"));  //收款单位
        model.put("FCURRENCYID", new FNumberVo("PRE001"));  //币别
        model.put("FPAYORGID", new FNumberVo("R001"));  //付款组织
        model.put("FSETTLEORGID", new FNumberVo("R001"));  //结算组织
        model.put("FDOCUMENTSTATUS", "");    //单据状态

        model.put("FCANCELSTATUS", "");  //作废状态
        model.put("FAPPLYORGID", new FNumberVo("R001"));  //申请组织
        model.put("FSETTLECUR", new FNumberVo("PRE001"));  //结算币别

        List<Object> list=new ArrayList<>();
        Map<String,Object> FPAYAPPLYENTRY=new HashMap<>();
        FPAYAPPLYENTRY.put("FENDDATE", "2023-07-15");  // 到期日
        FPAYAPPLYENTRY.put("FENDDATE", "2023-07-15");   //期望付款日期
        FPAYAPPLYENTRY.put("FAPPLYAMOUNTFOR", 234); //申请付款金额
        list.add(FPAYAPPLYENTRY);
        model.put("FPAYAPPLYENTRY", list);

        data.put("Model", model);

        Gson gson = new Gson();
        String json = gson.toJson(data);

        K3CloudApi client = new K3CloudApi();
        //保存
        KingDeeResponseVo result = KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        String id=result.getId();

        Map<String, Object> data2 = new HashMap<>();
        data2.put("Ids", id);
        String json2 = gson.toJson(data2);
        //提交
        KingDeeUtil.kingDeeClitSubmit(json2, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);
        //审核
        KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.PAYAPPLY_FORM_ID,gson,client);

    }
}
