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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesWorkOrderService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesWorkOrderExtModel;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesWorkOrder;
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.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description :
 * @Reference :
 * @Author :
 * @CreateDate : 2024-05-13 9:06
 * @Modify:
 **/
@RestController
@Api(tags = "工单")
@RequestMapping(MesCommonConstant.MES_YANFEN + "/work-order")
@Slf4j
public class MesWorkOrderController {

    @Autowired
    private IMesWorkOrderService workOrderService;

    @GetMapping("/query")
    @ApiOperation(value = "查询工单")
    public ResultBean queryWorkOrder(MesWorkOrder workOrder) {

        try {

            return ResultBean.success("查询成功").setResultList(workOrderService.queryMesWorkOrderList(workOrder));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("/query-pager")
    @ApiOperation(value = "查询工单")
    public ResultBean queryWorkOrderByPager(MesWorkOrder workOrder, Pager pager) {

        try {
            return ResultBean.success("查询成功").setListPager(workOrderService.queryMesWorkOrderListByPager(workOrder, pager));
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/doProductReport")
    @ApiOperation(value = "生产报工")
    public ResultBean doProductReport(MesWorkOrderExtModel mesWorkOrder) {
        try {
            workOrderService.doProductReport(mesWorkOrder, !StringUtils.isEmpty(mesWorkOrder.getModifyUser()) ? mesWorkOrder.getModifyUser() : AuthUtil.getSessionUser().getUserName());
            return ResultBean.success("报工成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException e) {
            return ResultBean.fail(e).build();
        } catch (Exception e) {
            return ResultBean.fail(e);
        }
    }
}
