package com.sifang.modules.kingdee.service;

import com.google.gson.Gson;
import com.jfinal.plugin.activerecord.Record;
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.order.dao.MaterialDeliverDetailDao;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialDeliverOrder;
import com.sifang.modules.supply.setting.entity.CrmSupplyWarehouse;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class SalOutStockService extends BaseService {
    @Autowired
    private MaterialDeliverDetailDao deliverDetailDao;

    // (赊销下单、自主下单出库)
    //新增
    public String saveOrderToKingDee(CrmSupplyMaterialDeliverOrder order,CrmSupplyWarehouse warehouse,String fCustomerID) throws Exception {
        Gson gson = new Gson();
        K3CloudApi client = new K3CloudApi();
        Map<String,Object> data = new HashMap<>();
        data.put("Model",doOrderData(order,warehouse,fCustomerID));
        KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gson.toJson(data), KingDeeConstants.SAL_FORM_ID, gson, client);
        logger.info("saveOrderToKingDee repoResult ={}",gson.toJson(repoResult));
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",repoResult.getId());
        //提交
        KingDeeUtil.kingDeeClitSubmit(gson.toJson(data1),KingDeeConstants.SAL_FORM_ID, gson, client);
        //审核
        KingDeeUtil.kingDeeClitAudit(gson.toJson(data1),KingDeeConstants.SAL_FORM_ID, gson, client);
        return repoResult.getId();
    }

    //修改
    public String editOrderToKingDee(CrmSupplyMaterialDeliverOrder order,CrmSupplyWarehouse warehouse,String fCustomerID) throws Exception {
        Gson gson = new Gson();
        K3CloudApi client = new K3CloudApi();

        //反审核
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",order.getKingDeeId());
        String jsonData = gson.toJson(data1);
        KingDeeUtil.kingDeeClitUnAudit(jsonData,KingDeeConstants.SAL_FORM_ID, gson, client);

        Map<String,Object> data = new HashMap<>();
        data.put("Model",doOrderData(order,warehouse,fCustomerID));
        KingDeeResponseVo repoResult = KingDeeUtil.kingDeeClitSave(gson.toJson(data), KingDeeConstants.SAL_FORM_ID, gson, client);
        logger.info("saveOrderToKingDee repoResult ={}",gson.toJson(repoResult));
        //提交
        KingDeeUtil.kingDeeClitSubmit(gson.toJson(data1),KingDeeConstants.SAL_FORM_ID, gson, client);
        //审核
        KingDeeUtil.kingDeeClitAudit(gson.toJson(data1),KingDeeConstants.SAL_FORM_ID, gson, client);
        return repoResult.getId();
    }

    //反审核
    public void unauditOrderToKingDee(String kingDeeId) throws Exception {
        Gson gson = new Gson();
        K3CloudApi client = new K3CloudApi();
        Map<String,Object> data1 = new HashMap<>();
        data1.put("Ids",kingDeeId);
        String jsonData = gson.toJson(data1);
        KingDeeUtil.kingDeeClitUnAudit(jsonData,KingDeeConstants.SAL_FORM_ID, gson, client);
    }

    public Map<String,Object> doOrderData(CrmSupplyMaterialDeliverOrder order, CrmSupplyWarehouse warehouse,String fCustomerID) {
        Map<String,Object> model = new HashMap<>();
        model.put("FBillTypeID",new FNumberVo());//单据类型
        model.put("FBillNo",order.getNumber());//单据编号
        model.put("FDate", DateUtils.formatYMDDateTime(order.getDeliverTime()));//日期
        model.put("FSaleOrgId",new FNumberVo("R001"));//销售组织
        model.put("FCustomerID",new FNumberVo(fCustomerID));//客户
        model.put("FStockOrgId",new FNumberVo("R001"));//发货组织

        Map<String,Object> SubHeadEntity = new HashMap<>();
        SubHeadEntity.put("FSettleCurrID",new FNumberVo("PRE001"));//结算币别
        SubHeadEntity.put("FSettleOrgID",new FNumberVo("R001"));//结算组织
        model.put("SubHeadEntity",SubHeadEntity);

        List<Map<String,Object>> FEntityList = new ArrayList<>();
        List<Record> details = deliverDetailDao.findDetailsByOrderId(order.getId());
        for (Record detail : details) {
            Map<String,Object> FEntity = new HashMap<>();
            FEntity.put("FMaterialID",new FNumberVo(detail.getStr("code")));//物料编码
            FEntity.put("FUnitID",new FNumberVo(detail.getStr("unit_code")));//库存单位
            FEntity.put("FOwnerTypeID","BD_OwnerOrg");//货主类型    //业务组织
            FEntity.put("FOwnerID",new FNumberVo("R001"));//货主

            FEntity.put("FRealQty",detail.get("out_amount"));//实发数量
           // FEntity.put("FPrice",detail.get("sales_price"));//单价
            FEntity.put("FTaxPrice",detail.get("sales_price"));//含税单价
            FEntity.put("F_QSVH_Amount",detail.get("sales_price"));//销售单价
            FEntity.put("F_QSVH_Amount1",detail.get("purchase_price"));//采购单价


            FEntity.put("FStockID",new FNumberVo(warehouse.getNumber()));//仓库
            FEntity.put("F_QSVH_Base", new FNumberVo("XM0001"));  //项目
            FEntityList.add(FEntity);
        }
        model.put("FEntity",FEntityList);
        return model;
    }

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

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

        model.put("FBillTypeID",new FNumberVo());//单据类型
        model.put("FBillNo","11335");//单据编号
        model.put("FDate", "2023-07-15");//日期
        model.put("FSaleOrgId",new FNumberVo("R001"));//销售组织
        model.put("FCustomerID",new FNumberVo("CUST0001"));//客户
        model.put("FStockOrgId",new FNumberVo("R001"));//发货组织

        Map<String,Object> SubHeadEntity = new HashMap<>();
        SubHeadEntity.put("FSettleCurrID",new FNumberVo("PRE001"));//结算币别
        SubHeadEntity.put("FSettleOrgID",new FNumberVo("R001"));//结算组织
        model.put("SubHeadEntity",SubHeadEntity);

        List<Map<String,Object>> FEntityList = new ArrayList<>();
        List<Record> details = new ArrayList<>();
        Record record=new Record();
        record.set("code", "KZH_001");
        record.set("out_amount", "11");
        record.set("sales_price", "12");
        details.add(record);
        for (Record detail : details) {
            Map<String,Object> FEntity = new HashMap<>();
            FEntity.put("FMaterialID",new FNumberVo(detail.getStr("code")));//物料编码
            FEntity.put("FUnitID",new FNumberVo("8633"));//库存单位
            FEntity.put("FOwnerTypeID","BD_OwnerOrg");//货主类型    //业务组织
            FEntity.put("FOwnerID",new FNumberVo("R001"));//货主

            FEntity.put("FRealQty",detail.get("out_amount"));//实发数量
            FEntity.put("FPrice",detail.get("sales_price"));//单价


            FEntity.put("FStockID",new FNumberVo("CK001"));//仓库
            FEntity.put("F_QSVH_Base", new FNumberVo("XM0001"));  //项目
            FEntityList.add(FEntity);
        }
        model.put("FEntity",FEntityList);

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

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

    }

}
