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.supply.setting.entity.CrmSupplyWarehouse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

@Service
public class WarehouseService {

    Logger logger = LoggerFactory.getLogger(getClass());

    //仓库保存
    public KingDeeResponseVo saveWarehouse(CrmSupplyWarehouse warehouse) throws Exception {
        logger.info(">>>>>>>>>>>推送金蝶系统 saveWarehouse");
        Map<String, Object> data = new HashMap<>();

        Map<String, Object> model = new HashMap<>();
        model.put("FName", warehouse.getName());    //名称
        model.put("FStockProperty", warehouse.getType());    //仓库属性
        model.put("FAddress", warehouse.getProvince()+warehouse.getCity()+warehouse.getArea()+warehouse.getAddress());    //仓库地址
        /*model.put("FPrincipal",warehouse.getPersonInCharge());  //仓库负责人*/
        model.put("FTel", warehouse.getMobile());  // 联系电话
        model.put("FDescription", warehouse.getRemarks()); //描述
        model.put("FStockStatusType", "0"); //库存状态类型
        model.put("FDefStockStatusId",new FNumberVo("KCZT01_SYS"));
        model.put("FLocListFormatter", "3");  //仓位维度数据列表显示格式

        data.put("Model", model);
        Gson gson = new Gson();
        String json = gson.toJson(data);

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

    //仓库编辑
    public void editWarehouse(CrmSupplyWarehouse warehouse) throws Exception {
        logger.info(">>>>>>>>>>>推送金蝶系统 editWarehouse warehouseId:{}",warehouse.getId());
        Map<String, Object> data1 = new HashMap<>();
        data1.put("Ids", warehouse.getKingDeeId());

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

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

        Map<String,Object> model=new HashMap<>();
        model.put("FName", warehouse.getName());    //名称
        model.put("FStockProperty", warehouse.getType());    //仓库属性
        model.put("FAddress", warehouse.getProvince()+warehouse.getCity()+warehouse.getArea()+warehouse.getAddress());    //仓库地址

       /* model.put("FPrincipal", warehouse.getPersonInCharge());  //仓库负责人*/
        model.put("FTel", warehouse.getMobile());  // 联系电话
        model.put("FDescription", warehouse.getRemarks()); //描述
        model.put("FStockStatusType", "0"); //库存状态类型
        model.put("FDefStockStatusId",new FNumberVo("KCZT01_SYS"));
        model.put("FLocListFormatter", "3");  //仓位维度数据列表显示格式
        model.put("FStockId", warehouse.getKingDeeId());
        data.put("Model", model);

        String json=gson.toJson(data);
        //保存
        KingDeeResponseVo result= KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

        //提交
        KingDeeUtil.kingDeeClitSubmit(json1, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);
        //审核
        KingDeeUtil.kingDeeClitAudit(json1, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);
    }




    //禁用
    public void hiddenWarehouse(String id) throws Exception {
        //禁用
        Map<String, Object> data = new HashMap<>();
        data.put("Ids", id);
        Gson gson = new Gson();
        String json = gson.toJson(data);
        logger.info(">>>>>>>>>>>>>>>>hiddenWarehouse  json:{}",json);
        K3CloudApi client = new K3CloudApi();
        RepoResult result = KingDeeUtil.kingDeeClitStatusOperation(json, KingDeeConstants.WAREHOUSE_FORM_ID, KingDeeConstants.STATUS_FORBID,gson,client);
    }

    //启用
    public void useWarehouse(String id) throws Exception {
        //禁用
        Map<String, Object> data = new HashMap<>();
        data.put("Ids", id);
        Gson gson = new Gson();
        String json = gson.toJson(data);
        logger.info(">>>>>>>>>>>>>>>>useWarehouse  json:{}",json);
        K3CloudApi client = new K3CloudApi();
        RepoResult result = KingDeeUtil.kingDeeClitStatusOperation(json, KingDeeConstants.WAREHOUSE_FORM_ID, KingDeeConstants.STATUS_ENABLE,gson,client);
    }


    public static void main(String arr[]) throws Exception {
        Gson gson = new Gson();
        K3CloudApi client = new K3CloudApi();
       // 保存
       Map<String,Object> data=new HashMap<>();

        Map<String,Object> model=new HashMap<>();
        model.put("FStockId", "114519");    //名称
        model.put("FName", "新金蝶仓库");    //名称
        model.put("FNumber", "CK009");
        model.put("FStockProperty", "1");    //仓库属性
        model.put("FAddress", "安徽省合肥市瑶海区快快快");    //仓库地址
       /* model.put("FPrincipal", "将军");  //仓库负责人*/
        model.put("FTel", "13244665588");  // 联系电话
        model.put("FDescription", "这个仓库都是备件"); //描述
        model.put("FStockStatusType", "0"); //库存状态类型
        model.put("FDefStockStatusId",new FNumberVo("KCZT01_SYS"));
        model.put("FLocListFormatter","3");  //仓位维度数据列表显示格式

        data.put("Model", model);
        String json=gson.toJson(data);
        System.out.println(json);
        try {
            KingDeeResponseVo result= KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);
            String id=result.getId();
            Map<String, Object> data2 = new HashMap<>();
            System.out.println(result.getNumber());
            data2.put("Ids", id);
            String json2 = gson.toJson(data2);
            System.out.println(json2);
            KingDeeUtil.kingDeeClitSubmit(json2, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

//            KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

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

     /*   //提交
        Map<String, Object> data2 = new HashMap<>();
        data2.put("Ids","103620");
        String json2 = gson.toJson(data2);
        //提交
        KingDeeUtil.kingDeeClitSubmit(json2, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);*/

      /*  //禁用
        Map<String, Object> data = new HashMap<>();
        data.put("Ids", "125262");
        Gson gson = new Gson();
        String json = gson.toJson(data);
        System.out.println(json);
        RepoResult result = KingDeeUtil.kingDeeClitStatusOperation(json, KingDeeConstants.WAREHOUSE_FORM_ID, KingDeeConstants.STATUS_ENABLE);*/

        //查看
     /*   Map<String, Object> data = new HashMap<>();
        data.put("Number", "CK002");
        String json = gson.toJson(data);
        RepoResult result =KingDeeUtil.kingDeeClitDetail(json,KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);
*/
        //编辑
     /*   Map<String, Object> data1 = new HashMap<>();
        data1.put("Numbers", "CK002");

        String json1 = gson.toJson(data1);
        KingDeeUtil.kingDeeClitUnAudit(json1,KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

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

        Map<String,Object> model=new HashMap<>();
        model.put("FName", "夕阳西下仓");    //名称
        model.put("FStockProperty", "1");    //仓库属性
        model.put("FAddress", "南京");    //仓库地址
        model.put("FTel", "18817523228");  // 联系电话
        model.put("FDescription", "这个夕阳西下仓仓库有修改"); //描述
        model.put("FStockStatusType", "正常"); //库存状态类型
        model.put("FLocListFormatter","3");  //仓位维度数据列表显示格式
        model.put("FNumber", "CK002");
        data.put("Model", model);

        String json=gson.toJson(data);
        System.out.println(json);
        try {
            RepoResult result= KingDeeUtil.kingDeeClitSave(json, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

            Map<String, Object> data2 = new HashMap<>();
            data2.put("Numbers", "CK002");
            String json2 = gson.toJson(data2);
            System.out.println(json2);
            KingDeeUtil.kingDeeClitSubmit(json2, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

            KingDeeUtil.kingDeeClitAudit(json2, KingDeeConstants.WAREHOUSE_FORM_ID,gson,client);

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



    }
}
