package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesShippingKanbanCfgService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesShippingKanbanCfgModel;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 龙兴发运看板
 * @Author: jason.niu
 * @Date: 2025/3/25 18:16
 * @Modify:
 */
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mesShippingKanbanCfg")
public class MesShippingKanbanCfgController {
    @Autowired
    private IMesShippingKanbanCfgService shippingKanbanCfgService;

    @GetMapping("/query")
    @ApiOperation(value = "查询发运看板配置项")
    public ResultBean queryShippingKanbanCfg(String organizeCode, String shippingGroupCode) {
        try {
            organizeCode = !StringUtils.isEmpty(organizeCode) ? organizeCode : AuthUtil.getOrganize().getOrganizeCode();
            return ResultBean.success("查询成功").setResultObject(shippingKanbanCfgService.queryShippingKanbanCfg(organizeCode, shippingGroupCode));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存发运看板配置项")
    public ResultBean saveShippingKanbanCfg(@RequestBody MesShippingKanbanCfgModel request) {
        try {
            String organizeCode = !StringUtils.isEmpty(request.getOrganizeCode()) ? request.getOrganizeCode() : AuthUtil.getOrganize().getOrganizeCode();
            shippingKanbanCfgService.doSaveShippingKanbanCfg(request, organizeCode, request.getUsername());
            return ResultBean.success("保存成功");
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/queryContext")
    @ApiOperation(value = "查询发运看板内容")
    public ResultBean queryShippingKanbanContext(String organizeCode, String shippingGroupCode) {
        try {
            organizeCode = !StringUtils.isEmpty(organizeCode) ? organizeCode : AuthUtil.getOrganize().getOrganizeCode();
            return ResultBean.success("查询成功").setResultObject(shippingKanbanCfgService.queryShippingKanbanContext(organizeCode, shippingGroupCode));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            e.printStackTrace();
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/queryCustQtyByTimeChartData")
    @ApiOperation(value = "查询库存趋势折线图数据")
    public ResultBean queryCustQtyByTimeChartData(String organizeCode, String shippingGroupCode) {
        try {
            organizeCode = !StringUtils.isEmpty(organizeCode) ? organizeCode : AuthUtil.getOrganize().getOrganizeCode();
            return ResultBean.success("查询成功").setResultList(shippingKanbanCfgService.queryCustQtyByTimeChartData(organizeCode, shippingGroupCode));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            e.printStackTrace();
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
