package com.sifang.modules.kingdee.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.kingdee.bos.webapi.entity.RepoResult;
import com.kingdee.bos.webapi.entity.RepoStatus;
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.stock.entity.SupplyMaterial;
import com.sifang.modules.sys.common.service.BaseService;
import org.springframework.stereotype.Service;

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

@Service
public class MateriService extends BaseService {

    //新增
    public String saveMateriToKingDee(SupplyMaterial material) throws Exception {
        Map<String,Object> data = new HashMap<>();
        data.put("Model",doMateriData(material));
        Gson gson = new Gson();
        String jsonStr=gson.toJson(data);
        K3CloudApi k3CloudApi=new K3CloudApi();
        logger.info(">>>>>>>>>>>>>>>>>saveMateriToKingDee jsonStr:{}",jsonStr);
        KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(jsonStr, KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        logger.info("saveMateriToKingDee repoResult ={}",gson.toJson(repoResult));
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",repoResult.getId());

        //提交
        KingDeeUtil.kingDeeClitSubmit(gson.toJson(data1),KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        //审核
        KingDeeUtil.kingDeeClitAudit(gson.toJson(data1),KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        return repoResult.getId();
    }

    //编辑
    public void updateMateriToKingDee(SupplyMaterial material) throws Exception {
        Gson gson = new Gson();
        K3CloudApi k3CloudApi=new K3CloudApi();
        //反审核
        Map<String, Object> data1 = new HashMap<>();
        data1.put("Ids", material.getKingDeeId());

        String json1 = gson.toJson(data1);
        logger.info(">>>>>>>>>>>>>>>>updateMateriToKingDee  jsonData:{}",json1);
        KingDeeUtil.kingDeeClitUnAudit(json1,KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);

        Map<String,Object> data = new HashMap<>();
        data.put("Model",doMateriData(material));
        KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gson.toJson(data), KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        logger.info("updateMateriToKingDee repoResult ={}",gson.toJson(repoResult));

        //提交
        KingDeeUtil.kingDeeClitSubmit(json1,KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        //审核
        KingDeeUtil.kingDeeClitAudit(json1,KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
    }
    /**
     * 批量保存
    */
    public String saveBatchMateriToKingDee(List<Map<String,Object>> list) throws Exception {
        Map<String,Object> data = new HashMap<>();
        data.put("Model",list);
        Gson gson = new Gson();
        String jsonStr=gson.toJson(data);
        K3CloudApi k3CloudApi=new K3CloudApi();
        logger.info(">>>>>>>>>>>>>>>>>saveMateriToKingDee jsonStr:{}",jsonStr);
        KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(jsonStr, KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        logger.info("saveMateriToKingDee repoResult ={}",gson.toJson(repoResult));
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",repoResult.getId());

        //提交
       // KingDeeUtil.kingDeeClitSubmit(gson.toJson(data1),KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        //审核
      //  KingDeeUtil.kingDeeClitAudit(gson.toJson(data1),KingDeeConstants.MATERIAL_FORM_ID, gson, k3CloudApi);
        return repoResult.getId();
    }



    //禁用
    public void forbidMateriToKingDee(SupplyMaterial material) throws Exception {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("Ids",material.getKingDeeId());
        RepoResult repoResult = KingDeeUtil.kingDeeClitStatusOperation(gson.toJson(data),KingDeeConstants.MATERIAL_FORM_ID,KingDeeConstants.STATUS_FORBID, gson, new K3CloudApi());
        logger.info("forbidMateriToKingDee repoResult ={}",gson.toJson(repoResult));
    }

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

    public Map<String,Object> doMateriData(SupplyMaterial material) {
        Map<String,Object> model = new HashMap<>();
       if (StringUtil.isNotBlank(material.getKingDeeId())) {
            model.put("FMATERIALID",material.getKingDeeId());
        }
        model.put("FCreateOrgId",new FNumberVo("R001"));//创建组织
        model.put("FUseOrgId",new FNumberVo("R001"));//使用组织
        model.put("FNumber",material.getCode());//编码
        model.put("FName",material.getMaterialName());//名称
        model.put("FSpecification",material.getMaterialSpecs());//规格型号
        model.put("FDescription",material.getRemarks());//描述
        model.put("FMaterialGroup",new FNumberVo(""));//物料分组
        model.put("FIsHandleReserve",false);
        model.put("SubHeadEntity",getSubHeadEntity(material));
        model.put("SubHeadEntity1",getSubHeadEntity1(material));
        model.put("SubHeadEntity3",getSubHeadEntity3(material));
       // model.put("FBarCodeEntity_CMK",getFBarCodeEntity(material));
//        model.put("FBarCodeEntity_CMK",new FNumberVo(""));
        return model;
    }

    public Map<String,Object> getSubHeadEntity(SupplyMaterial material){
        Map<String,Object> subHeadEntity = new HashMap<>();
        subHeadEntity.put("FBARCODE",material.getCode());//条码
        subHeadEntity.put("FErpClsID",material.getMaterialProperties());//物料属性
        subHeadEntity.put("FFeatureItem","1");//特征件子项
        subHeadEntity.put("FCategoryID",new FNumberVo("CHLB01_SYS"));//存货类别
        subHeadEntity.put("FBaseUnitId",new FNumberVo("Pcs"));//基本单位
        subHeadEntity.put("FSuite","0");//套件
        return subHeadEntity;
    }
    public Map<String,Object> getSubHeadEntity1(SupplyMaterial material){
        Map<String,Object> subHeadEntity = new HashMap<>();
        subHeadEntity.put("FStoreUnitID",new FNumberVo(material.getUnitCode()));//库存单位
//        subHeadEntity.put("FAuxUnitID",new FNumberVo(material.getUnitCode()));//辅助单位
        subHeadEntity.put("FUnitConvertDir","1");//换算方向
        subHeadEntity.put("FCurrencyId",new FNumberVo("PRE001"));//币别
        subHeadEntity.put("FSNManageType","1");//业务范围
        subHeadEntity.put("FSNGenerateTime","1");//序列号生成时机
        return subHeadEntity;
    }

    public Map<String,Object> getSubHeadEntity3(SupplyMaterial material){
        Map<String,Object> subHeadEntity = new HashMap<>();
        subHeadEntity.put("FPurchaseUnitId",new FNumberVo(material.getUnitCode()));//采购单位
        subHeadEntity.put("FPurchasePriceUnitId",new FNumberVo(material.getUnitCode()));//采购计价单位
        subHeadEntity.put("FQuotaType","1");//配额方式
        return subHeadEntity;
    }

    public Map<String,Object> getFBarCodeEntity(SupplyMaterial material){
        Map<String,Object> cmk = new HashMap<>();
        cmk.put("FCodeType_CMK","");//条形码类型
        Map<String,Object> map = Maps.newHashMap();
        map.put("FNUMBER",material.getUnitCode());
        cmk.put("FUnitId_CMK",map);//计量单位
        return cmk;
    }

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

            SupplyMaterial material = new SupplyMaterial();
            material.setCode("25555");
            material.setMaterialName("耳机配件");
            material.setMaterialSpecs("11");
            material.setCreateBy("我");
            material.setRemarks("耳机的配件");
            material.setMaterialAttribute("0");

            Map<String,Object> data = new HashMap<>();
            List<Map<String,Object>> list = Lists.newArrayList();
            Map<String,Object> model = new HashMap<>();
            model.put("FCreateOrgId",new FNumberVo("R001"));
            model.put("FUseOrgId",new FNumberVo("R001"));
            model.put("FNumber",material.getCode());
            model.put("FName",material.getMaterialName());
            model.put("FSpecification",material.getMaterialSpecs());
            model.put("FDescription",material.getRemarks());
            model.put("FMaterialGroup",new FNumberVo(""));
            model.put("FIsHandleReserve",false);
            Map<String,Object> subHeadEntity = new HashMap<>();
            subHeadEntity.put("FBARCODE",material.getCode());
            subHeadEntity.put("FErpClsID",material.getMaterialProperties());
           // subHeadEntity.put("FErpClsID","1");//物料属性
            subHeadEntity.put("FFeatureItem","1");//特征件子项
            subHeadEntity.put("FCategoryID",new FNumberVo("CHLB01_SYS"));//存货类别
            subHeadEntity.put("FBaseUnitId",new FNumberVo("Pcs"));//基本单位
            subHeadEntity.put("FSuite","0");//套件
            model.put("SubHeadEntity",subHeadEntity);
            Map<String,Object> subHeadEntity1 = new HashMap<>();
            subHeadEntity1.put("FStoreUnitID",new FNumberVo(material.getUnitCode()));
            subHeadEntity1.put("FUnitConvertDir","1");//换算方向
            subHeadEntity1.put("FCurrencyId",new FNumberVo("PRE001"));//币别
            subHeadEntity1.put("FSNManageType","1");//业务范围
            subHeadEntity1.put("FSNGenerateTime","1");//序列号生成时机
            model.put("SubHeadEntity1",subHeadEntity1);
            Map<String,Object> subHeadEntity3 = new HashMap<>();
            subHeadEntity3.put("FPurchaseUnitId",new FNumberVo(material.getUnitCode()));//采购单位
            subHeadEntity3.put("FPurchasePriceUnitId",new FNumberVo(material.getUnitCode()));//采购计价单位
            subHeadEntity3.put("FQuotaType","1");//配额方式
            model.put("SubHeadEntity3",subHeadEntity3);
            Map<String,Object> cmk = Maps.newHashMap();

           // model.put("FBarCodeEntity_CMK",cmk.put("",""));
            list.add(model);
            data.put("Model",gson.toJson(list));
            KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gson.toJson(data), KingDeeConstants.MATERIAL_FORM_ID, gson, new K3CloudApi());
           System.out.println(repoResult);
            Map<String,Object> data1 = new HashMap<>();
        /*    data1.put("Ids",repoResult.getId());
            //提交
            KingDeeUtil.kingDeeClitSubmit(gson.toJson(data1),KingDeeConstants.MATERIAL_FORM_ID, gson, new K3CloudApi());
            //审核
            KingDeeUtil.kingDeeClitAudit(gson.toJson(data1),KingDeeConstants.MATERIAL_FORM_ID, gson, new K3CloudApi());

*/
            //禁用
//            Map<String,Object> data = new HashMap<>();
//            data.put("Ids","125286");
//            RepoResult repoResult = KingDeeUtil.kingDeeClitStatusOperation(gson.toJson(data),KingDeeConstants.MATERIAL_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.MATERIAL_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.MATERIAL_FORM_ID,gson,client);

        } catch (Exception e) {
            e.printStackTrace();
        }



    }


}
