package com.sifang.modules.kingdee.service;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.SuccessEntity;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.sifang.modules.account.entity.CrmTraderAccountSettleDetail;
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.sunshine.SystemSetting.utils.BusinessException;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class KingDeePayableService extends BaseService {


    //保存--电站结算的时候
    public String kingDeeSavePayable(CrmTraderAccountSettleDetail settleDetail, CrmTrader trader) throws Exception {
        logger.info(">>>>>>>>>>>kingDeeSavePayable 推入金蝶系统 settleDetail:{}",settleDetail.getId());
        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", settleDetail.getNumber());  //单据编号
        model.put("FDATE", DateUtils.formatDate(settleDetail.getCreateTime(), "yyyy-MM-dd"));   //申请日期
        model.put("FENDDATE_H", DateUtils.getDate()); //到期日
        model.put("FDOCUMENTSTATUS", ""); //单据状态
        model.put("FBUSINESSTYPE", "结算"); //业务类型

        model.put("FSUPPLIERID", new FNumberVo(trader.getKingDeeNumber()));  //供应商
        model.put("FCURRENCYID", new FNumberVo("PRE001"));  //币别
        model.put("FPAYORGID", new FNumberVo("R001"));  //付款组织
        model.put("FSETTLEORGID", new FNumberVo("R001"));  //结算组织

        model.put("FCancelStatus", "");  //作废状态


        Map<String,Object> FsubHeadSuppiler=new HashMap<>();
        FsubHeadSuppiler.put("FChargeId", new FNumberVo(trader.getKingDeeNumber()));//收款方
        model.put("FsubHeadSuppiler",FsubHeadSuppiler);

        List<Object> list=new ArrayList<>();
        Map<String,Object> FEntityPlan=new HashMap<>();
        FEntityPlan.put("FPAYAMOUNTFOR", settleDetail.getAmount()); //应付款金额
        FEntityPlan.put("FPAYRATE", 100); //应付比例
        list.add(FEntityPlan);
        model.put("FEntityPlan", list);

        List<Object> list2=new ArrayList<>();
        Map<String,Object> FEntityDetail=new HashMap<>();
        FEntityDetail.put("FMATERIALID", new FNumberVo("FW_FW_NO"));  //物料编码
        FEntityDetail.put("FPRICEUNITID", new FNumberVo("bwx001"));    //计价单位
        FEntityDetail.put("FPriceQty",1);//计价数量
        FEntityDetail.put("FTaxPrice", settleDetail.getAmount()); //含税单价
        FEntityDetail.put("FNoTaxAmountFor_D",settleDetail.getAmount()); //不含税金额
        FEntityDetail.put("FTAXAMOUNTFOR_D",0); //税额
        FEntityDetail.put("FALLAMOUNTFOR_D", settleDetail.getAmount()); //价税合计
        FEntityDetail.put("F_QSVH_Assistant1", new FNumberVo("01"));  //  应付类型
        FEntityDetail.put("F_QSVH_Base1",  new FNumberVo("XM0001"));  // 项目
        FEntityDetail.put("F_QSVH_Assistant",  new FNumberVo("01"));  // 区域
        list2.add(FEntityDetail);
        model.put("FEntityDetail",list2);

        data.put("Model", model);

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

        K3CloudApi client = new K3CloudApi();
        //保存
        KingDeeResponseVo result = KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.PAY_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.PAY_FORM_ID,gson,client);
        //审核
        KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.PAY_FORM_ID,gson,client);
        return id;
    }

    //下推
    public String pushDown(String KingIds) throws Exception {
        logger.info("下推 pushDown  KingIds :{}",KingIds);
        K3CloudApi client = new K3CloudApi();
        Gson gson = new Gson();
        //先调用采购单下推
        logger.info("开始......");
        long begin = System.currentTimeMillis();

        Map<String,Object> mapxia = Maps.newHashMap();
        mapxia.put("Ids", KingIds);
        mapxia.put("IsEnableDefaultRule",true);
        mapxia.put("TargetFormId",KingDeeConstants.PAYAPPLY_FORM_ID);
        mapxia.put("IsDraftWhenSaveFail",true);
        ArrayList<SuccessEntity>  repons= KingDeeUtil.kingDeeClitPush(gson.toJson(mapxia),KingDeeConstants.PAY_FORM_ID,gson,client);
        logger.info("下推 pushDown  repons :{}",repons);
        long end = System.currentTimeMillis();
        logger.info("下推结束，共耗时：[" + (end-begin) / 1000 + "]秒");
        if(repons== null){
            throw new BusinessException("同步金蝶数据失败，请刷新再试");
        }
        String xiatuiId = repons.get(0).getId();
        return xiatuiId;
    }


    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", "55556");  //单据编号
        model.put("FDATE", "2023-08-10");   //申请日期
        model.put("FENDDATE_H", DateUtils.getDate()); //到期日
        model.put("FDOCUMENTSTATUS", ""); //单据状态
        model.put("FBUSINESSTYPE", "结算");  //业务类型

        model.put("FSUPPLIERID", new FNumberVo("R001"));  //供应商
        model.put("FCURRENCYID", new FNumberVo("PRE001"));  //币别
        model.put("FPAYORGID", new FNumberVo("R001"));  //付款组织
        model.put("FSETTLEORGID", new FNumberVo("R001"));  //结算组织

        model.put("FCancelStatus", "");  //作废状态


        Map<String,Object> FsubHeadSuppiler=new HashMap<>();
        FsubHeadSuppiler.put("FChargeId", new FNumberVo("CUST0001"));//收款方
        model.put("FsubHeadSuppiler",FsubHeadSuppiler);
        String date=DateUtils.getDate();
        List<Object> list=new ArrayList<>();
        Map<String,Object> FEntityPlan=new HashMap<>();
        FEntityPlan.put("FPAYAMOUNTFOR", "288"); //应付款金额
        FEntityPlan.put("FPAYRATE", 100); //应付比例
        list.add(FEntityPlan);
        model.put("FEntityPlan", list);

        List<Object> list2=new ArrayList<>();
        Map<String,Object> FEntityDetail=new HashMap<>();
        FEntityDetail.put("FMATERIALID", new FNumberVo("ZZ-01-1114"));  //物料编码
        FEntityDetail.put("FPRICEUNITID", new FNumberVo("jian001"));    //计价单位
        FEntityDetail.put("FPriceQty",1);//计价数量
        FEntityDetail.put("FTaxPrice", 3); //含税单价
        FEntityDetail.put("FNoTaxAmountFor_D", "288"); //不含税金额
        FEntityDetail.put("FTAXAMOUNTFOR_D",0); //税额
        FEntityDetail.put("FALLAMOUNTFOR_D", "288"); //价税合计
        FEntityDetail.put("F_QSVH_Assistant1", new FNumberVo("01"));  //  应付类型
        FEntityDetail.put("F_QSVH_Base1",  new FNumberVo("XM0001"));  // 项目
        FEntityDetail.put("F_QSVH_Assistant",  new FNumberVo("01"));  // 区域
        list2.add(FEntityDetail);
        model.put("FEntityDetail",list2);


        data.put("Model", model);

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

        K3CloudApi client = new K3CloudApi();
        //保存
        KingDeeResponseVo result = KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.PAY_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.PAY_FORM_ID,gson,client);
        //审核
        KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.PAY_FORM_ID,gson,client);*/

        K3CloudApi client = new K3CloudApi();
        Gson gson = new Gson();
        //先调用采购单下推
        long begin = System.currentTimeMillis();

        Map<String,Object> mapxia = Maps.newHashMap();
        mapxia.put("Ids", "100067");
        mapxia.put("IsEnableDefaultRule",true);
        mapxia.put("TargetFormId",KingDeeConstants.PAYAPPLY_FORM_ID);
        mapxia.put("IsDraftWhenSaveFail",true);
        ArrayList<SuccessEntity>  repons= KingDeeUtil.kingDeeClitPush(gson.toJson(mapxia),KingDeeConstants.PAY_FORM_ID,gson,client);
    }









}
