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.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.utils.StringUtil;
import com.sifang.modules.supply.setting.entity.CrmSupplySupplier;
import com.sifang.modules.supply.stock.entity.SupplyMaterial;
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 SupplierService extends BaseService {

    //新增
    public KingDeeResponseVo saveSupplierToKingDee(CrmSupplySupplier supplier) throws Exception {
        Map<String,Object> data = new HashMap<>();
        data.put("Model",doSupplierData(supplier));
        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 updateSupplierToKingDee(CrmSupplySupplier supplier) throws Exception {
        Gson gson = new Gson();
        K3CloudApi cloudApi=new K3CloudApi();
        //反审核
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",supplier.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(supplier));
        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 void forbidSupplierToKingDee(CrmSupplySupplier supplier) throws Exception {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("Ids",supplier.getKingDeeId());
        RepoResult repoResult = KingDeeUtil.kingDeeClitStatusOperation(gson.toJson(data),KingDeeConstants.SUPPLIER_FORM_ID,KingDeeConstants.STATUS_FORBID, gson, new K3CloudApi());
        logger.info("forbidSupplierToKingDee repoResult ={}",gson.toJson(repoResult));
    }

    //反禁用
    public void enableSupplierToKingDee(CrmSupplySupplier supplier) throws Exception {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("Ids",supplier.getKingDeeId());
        RepoResult repoResult = KingDeeUtil.kingDeeClitStatusOperation(gson.toJson(data),KingDeeConstants.SUPPLIER_FORM_ID,KingDeeConstants.STATUS_ENABLE, gson, new K3CloudApi());
        logger.info("enableSupplierToKingDee repoResult ={}",gson.toJson(repoResult));
    }

    public Map<String,Object> doSupplierData(CrmSupplySupplier supplier) {
        Map<String,Object> model = new HashMap<>();
       if (StringUtil.isNotBlank(supplier.getKingDeeId())) {
            model.put("FSupplierId",supplier.getKingDeeId());
        }
        model.put("FCreateOrgId",new HashMap<>().put("FNumber","R001"));//创建组织
        model.put("FUseOrgId",new HashMap<>().put("FNumber","R001"));//创建组织
        model.put("FName",supplier.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",supplier.getRemarks());
        List<Object> FBankInfo=new ArrayList<>();
        Map<String,Object> fBankInfo=new HashMap<>();
        fBankInfo.put("FBankCode", supplier.getBankCode());  //银行账号
        fBankInfo.put("FOpenBankName", supplier.getBankName());   //开户银行
        fBankInfo.put("FBankCurrencyId", new FNumberVo("PRE001"));  //币别
        FBankInfo.add(fBankInfo);
        model.put("FBankInfo",FBankInfo);
        return model;
    }


    public static void main(String[] args) {
        try {
            Gson gson = new Gson();
            K3CloudApi client = new K3CloudApi();

            CrmSupplySupplier supplier = new CrmSupplySupplier();
            supplier.setName("再来个供应商");
            supplier.setCreateBy("22");
            supplier.setRemarks("测试下");


            Map<String,Object> data = new HashMap<>();
            Map<String,Object> model = new HashMap<>();
          /*  if (StringUtil.isNotBlank(supplier.getKingDeeId())) {
                model.put("FSupplierId",supplier.getKingDeeId());
            }*/
            model.put("FCreateOrgId",new HashMap<>().put("FNumber",supplier.getCreateBy()));
            model.put("FUseOrgId",new HashMap<>());
            model.put("FName",supplier.getName());
            Map<String,Object> FFinanceInfo = new HashMap<>();
            FFinanceInfo.put("FPayCurrencyId",new HashMap<>().put("FNumber",new HashMap<>()));
            model.put("FFinanceInfo",FFinanceInfo);
            model.put("FDescription",supplier.getRemarks());

            List<Object> FBankInfo=new ArrayList<>();
            Map<String,Object> fBankInfo=new HashMap<>();
            fBankInfo.put("FBankCode", "8888888888888888888");  //银行账号
            fBankInfo.put("FOpenBankName", "中国银行");   //开户银行
            fBankInfo.put("FBankCurrencyId", new FNumberVo("PRE001"));  //币别
            FBankInfo.add(fBankInfo);
            model.put("FBankInfo",FBankInfo);

            data.put("Model",model);

            KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gson.toJson(data), KingDeeConstants.SUPPLIER_FORM_ID, gson, client);
            System.out.println("saveSupplierToKingDee repoResult ="+gson.toJson(repoResult));
            Map<String,Object> data1 = new HashMap<>();
            data1.put("Ids",repoResult.getId());
            //提交
            KingDeeUtil.kingDeeClitSubmit(gson.toJson(data1),KingDeeConstants.SUPPLIER_FORM_ID, gson, new K3CloudApi());
            //审核
            KingDeeUtil.kingDeeClitAudit(gson.toJson(data1),KingDeeConstants.SUPPLIER_FORM_ID, gson, new K3CloudApi());


            //禁用
//            Map<String,Object> data = new HashMap<>();
//            data.put("Ids","125286");
//            RepoResult repoResult = KingDeeUtil.kingDeeClitStatusOperation(gson.toJson(data),KingDeeConstants.SUPPLIER_FORM_ID,KingDeeConstants.STATUS_FORBID, gson, new K3CloudApi());


            //反禁用
//            Map<String,Object> data = new HashMap<>();
//            data.put("Ids","125286");
//            RepoResult repoResult = KingDeeUtil.kingDeeClitStatusOperation(gson.toJson(data),KingDeeConstants.SUPPLIER_FORM_ID,KingDeeConstants.STATUS_ENABLE, gson, new K3CloudApi());

            //查看
//            Map<String, Object> data = new HashMap<>();
//            data.put("Id", "125286");
//            String json = gson.toJson(data);
//            RepoResult repoResult =KingDeeUtil.kingDeeClitDetail(json,KingDeeConstants.SUPPLIER_FORM_ID,gson,client);
            System.out.println("saveSupplierToKingDee repoResult ="+gson.toJson(repoResult));
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

}
