package com.hw.jindie.controller;

import com.alibaba.fastjson2.JSONObject;
import com.hw.common.core.web.domain.AjaxResult;
import com.hw.common.log.annotation.Log;
import com.hw.common.log.enums.BusinessType;
import com.hw.jindie.service.IKingdeeErpSyncService;
import com.hw.jindie.service.impl.KingdeeErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @ProjectName:HwMes
 * @Author:xins
 * @Date:2024-01-11 17:26
 * @Version:1.0
 */
@RestController
@RequestMapping("/jindie")
public class KingdeeErpSyncController {

    @Autowired
    private IKingdeeErpSyncService kingdeeErpSyncService;

    @Autowired
    private KingdeeErpService kingdeeErpService;

    /**
     * 新增原材料入库记录
     */
//    @RequiresPermissions("wms:mobile:addrawinstock")
    @Log(title = "物料信息", businessType = BusinessType.INSERT)
    @GetMapping(("/syncMaterialInfo"))
    public AjaxResult syncMaterialInfo() {
        try {
            return AjaxResult.success(kingdeeErpSyncService.syncMaterialInfoFromErp());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /**
     * 单一采购申请单保存->提交->审核
     *
     * @param params {"FMaterialId": "040200050162","FMaterialDesc": "UPVC直管","FReqQty": 20}
     * @return {"msg": "{"IsSuccess": true,"Errors": [],"SuccessEntitys": [{"Id": "154898","Number": "CGSQ010785","DIndex": 0}]}", "code": 200}
     */
    @PostMapping(("/singleSavePurchaseRequisition"))
    public AjaxResult singleSavePurchaseRequisition(@RequestBody String params) {
        try {
            return AjaxResult.success(kingdeeErpService.singleSavePurchaseRequisition(params));
        } catch (Exception e) {
            return AjaxResult.error(e.toString());
        }
    }

    /**
     * 采购入库单保存->提交->审核
     *
     * @param params {"FMaterialId": "02030002","FMaterialDesc": "推进式搅拌机",
     * 	"FAuxPropId": "①【搅拌机】380V--BLD12搅拌机功率3kw，速比23（转速65rpm） ②【搅拌杆】搅拌杆长度3200mm，轴径57 ③【叶轮】叶轮直径650，二叶双层 ④【材质】碳钢衬胶⑤【机架】TJA2机架特别备注：①湘潭电机二级能效；②衬胶材质③电压:三相380V/50Hz; 电机防护/绝缘/温升:IP56/F/B",
     * 	"FUnitID": "tai","FStockId": "CK002","FSrcBillNo": "JYCG-2023-5184","FRealQty": 4.0000,"FDate": "2024-04-10 00:00:00"}
     * @return {"msg": "{"IsSuccess":true,"Errors":[],"SuccessEntitys":[{"Id":"106249","Number":"CGRK00779","DIndex":0}]}","code": 200
     * }
     */
    @PostMapping(("/savePurchaseStorage"))
    public AjaxResult savePurchaseStorage(@RequestBody String params) {
        try {
            return AjaxResult.success(kingdeeErpService.savePurchaseStorage(params));
        } catch (Exception e) {
            return AjaxResult.error(e.toString());
        }
    }

    /**
     * 其他出库单保存->提交->审核
     * @param params {"FMaterialId": "01150020","FDate": "2024-04-12 00:00:00","FUnitID": "zhi1","FStockId": "CK052","FAuxPropId": "304不锈钢滤帽","FQty": 4.0000,"FPrice": 20.3}
     * @return {"msg": "{\"IsSuccess\":true,\"Errors\":[],\"SuccessEntitys\":[{\"Id\":\"116470\",\"Number\":\"QTCK004153\",\"DIndex\":0}]}","code": 200}
     */
    @PostMapping(("/saveOtherOutStorage"))
    public AjaxResult saveOtherOutStorage(@RequestBody String params) {
        try {
            return AjaxResult.success(kingdeeErpService.saveOtherOutStorage(params));
        } catch (Exception e) {
            return AjaxResult.error(e.toString());
        }
    }


    /**
     * 其他入库单保存->提交->审核
     * @param params {"FMaterialId": "01150020","FDate": "2024-04-12 00:00:00","FUnitID": "zhi1","FStockId": "CK052","FAuxPropId": "304不锈钢滤帽","FQty": 4.0000,"FPrice": 20.3}
     * @return  { "msg": "{\"IsSuccess\":true,\"Errors\":[],\"SuccessEntitys\":[{\"Id\":\"100418\",\"Number\":\"QTRK000352\",\"DIndex\":0}]}", "code": 200}
     */
    @PostMapping(("/saveOtherInStorage"))
    public AjaxResult saveOtherInStorage(@RequestBody String params) {
        try {
            return AjaxResult.success(kingdeeErpService.saveOtherInStorage(params));
        } catch (Exception e) {
            return AjaxResult.error(e.toString());
        }
    }

    /**
     * ERP通用保存接口
     *
     * @param params
     * @param formId
     * @return
     */
    @PostMapping(("/genericSaveInterface/{formId}"))
    public String genericSaveInterface(@RequestBody JSONObject params, @PathVariable("formId") String formId) {
        try {
            return kingdeeErpService.genericSaveInterface(params, formId);
        } catch (Exception e) {
            return e.toString();
        }
    }

    /**
     * ERP通用提交接口
     *
     * @param params
     * @return
     */
    @PostMapping(("/genericCommitInterface"))
    public AjaxResult genericCommitInterface(@RequestBody JSONObject params) {
        try {
            return AjaxResult.success(kingdeeErpService.genericCommitInterface(params));
        } catch (Exception e) {
            return AjaxResult.error(e.toString());
        }
    }

    /**
     * ERP通用审核接口
     *
     * @param params
     * @return
     */
    @PostMapping(("/genericAuditInterface"))
    public AjaxResult genericAuditInterface(@RequestBody JSONObject params) {
        try {
            return AjaxResult.success(kingdeeErpService.genericAuditInterface(params));
        } catch (Exception e) {
            return AjaxResult.error(e.toString());
        }
    }

    /**
     * ERP通用查看接口
     *
     * @param params
     * @return
     */
    @PostMapping(("/genericViewInterface"))
    public String genericViewInterface(@RequestBody JSONObject params) {
        try {
            return kingdeeErpService.genericViewInterface(params);
        } catch (Exception e) {
            return e.toString();
        }
    }

    /**
     * 测试组装实体
     *
     * @param params
     * @return
     */
    @PostMapping(("/testAssemblyEntity"))
    public String testAssemblyEntity(@RequestBody String params) {
        try {
            return kingdeeErpService.testAssemblyEntity(params);
        } catch (Exception e) {
            return e.toString();
        }
    }

}
