package com.sifang.modules.kingdee.service;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.kingdee.bos.webapi.entity.RepoResult;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
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.StringUtil;
import com.sifang.modules.supply.setting.entity.CrmSupplySupplier;
import com.sifang.modules.sys.common.service.BaseService;
import org.springframework.stereotype.Service;

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

// 代理商推入金蝶也是推入供应商
@Service
public class KingDeeTraderService extends BaseService {

    //保存代理商
     public KingDeeResponseVo saveTrader(CrmTrader trader) throws Exception {
         Map<String,Object> data = new HashMap<>();
         data.put("Model",doSupplierData(trader));
         Gson gson = new Gson();
         K3CloudApi cloudApi=new K3CloudApi();
         String gsonStr=gson.toJson(data);
         KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gsonStr, KingDeeConstants.SUPPLIER_FORM_ID, gson,cloudApi);
         logger.info("saveSupplierToKingDee repoResult ={}",gson.toJson(repoResult));
         Map<String,Object> data1 = new HashMap<>();
         data1.put("Ids",repoResult.getId());
         String str2=gson.toJson(data1);
         //提交
         KingDeeUtil.kingDeeClitSubmit(str2,KingDeeConstants.SUPPLIER_FORM_ID, gson,cloudApi);
         //审核
         KingDeeUtil.kingDeeClitAudit(str2,KingDeeConstants.SUPPLIER_FORM_ID, gson,cloudApi);

         return repoResult;
     }

     //编辑代理商
    public void editTrader(CrmTrader trader) throws Exception {
        logger.info(">>>>>>>>>>>>>>>>推给金蝶 editTrader   traderId:{}",trader.getId());
        Gson gson = new Gson();
        K3CloudApi cloudApi=new K3CloudApi();
        //反审核
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",trader.getKingDeeId());
        String jsonData = gson.toJson(data1);
        KingDeeUtil.kingDeeClitUnAudit(jsonData,KingDeeConstants.SUPPLIER_FORM_ID, gson, cloudApi);
        Map<String,Object> data = new HashMap<>();
        data.put("Model",doSupplierData(trader));
        String gsonStr=gson.toJson(data);
        KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gsonStr, KingDeeConstants.SUPPLIER_FORM_ID, gson, cloudApi);
        logger.info("updateSupplierToKingDee repoResult ={}",gson.toJson(repoResult));

        //提交
        KingDeeUtil.kingDeeClitSubmit(jsonData,KingDeeConstants.SUPPLIER_FORM_ID, gson, cloudApi);
        //审核
        KingDeeUtil.kingDeeClitAudit(jsonData,KingDeeConstants.SUPPLIER_FORM_ID, gson, cloudApi);
    }


    public Map<String,Object> doSupplierData(CrmTrader trader) {
        Map<String,Object> model = new HashMap<>();
        if (StringUtil.isNotBlank(trader.getKingDeeId())) {
            model.put("FSupplierId",trader.getKingDeeId());
        }
        model.put("FCreateOrgId",new HashMap<>().put("FNumber","R001"));//创建组织
        model.put("FUseOrgId",new HashMap<>().put("FNumber","R001"));//创建组织
        model.put("FName",trader.getName());//名称
        Map<String,Object> FFinanceInfo = new HashMap<>();
        FFinanceInfo.put("FPayCurrencyId",new HashMap<>().put("FNumber",new HashMap<>().put("FNumber","PRE001")));//结算币别
        model.put("FFinanceInfo",FFinanceInfo);
        model.put("FDescription",trader.getRemarks());
        List<Object> FBankInfo=new ArrayList<>();
        Map<String,Object> fBankInfo=new HashMap<>();
        fBankInfo.put("FBankCode", trader.getBankCode());  //银行账号
        fBankInfo.put("FOpenBankName", trader.getBankName());   //开户银行
        fBankInfo.put("FBankCurrencyId", new FNumberVo("PRE001"));  //币别
        FBankInfo.add(fBankInfo);
        model.put("FBankInfo",FBankInfo);
        return model;
    }


     public static void main(String[] arr) throws Exception {

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

         model.put("FCreateOrgId",new HashMap<>().put("FNumber","R001"));//创建组织
         model.put("FUseOrgId",new HashMap<>().put("FNumber","R001"));//创建组织
         model.put("FName","你好代理商");//名称
         Map<String,Object> FFinanceInfo = new HashMap<>();
         FFinanceInfo.put("FPayCurrencyId",new HashMap<>().put("FNumber",new HashMap<>().put("FNumber","PRE001")));//结算币别
         model.put("FFinanceInfo",FFinanceInfo);
         model.put("FDescription","这个代理商有点问题哦!");
         List<Object> FBankInfo=new ArrayList<>();
         Map<String,Object> fBankInfo=new HashMap<>();
         fBankInfo.put("FBankCode", "787867678676574676767676767");  //银行账号
         fBankInfo.put("FOpenBankName", "中国银行");   //开户银行
         fBankInfo.put("FBankCurrencyId", new FNumberVo("PRE001"));  //币别
         FBankInfo.add(fBankInfo);
         model.put("FBankInfo",FBankInfo);

         Map<String,Object> data = new HashMap<>();
         data.put("Model",model);
         Gson gson = new Gson();
         K3CloudApi cloudApi=new K3CloudApi();
         String gsonStr=gson.toJson(data);
         KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gsonStr, KingDeeConstants.SUPPLIER_FORM_ID, gson,cloudApi);

         Map<String,Object> data1 = new HashMap<>();
         data1.put("Ids",repoResult.getId());
         String str2=gson.toJson(data1);
         //提交
         KingDeeUtil.kingDeeClitSubmit(str2,KingDeeConstants.SUPPLIER_FORM_ID, gson,cloudApi);
         //审核
         KingDeeUtil.kingDeeClitAudit(str2,KingDeeConstants.SUPPLIER_FORM_ID, gson,cloudApi);

     }
    
}
