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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IJobBookingService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesJobBookingPart;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesPackageExt;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.MesWorkOverModel;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/2/2 15:51
 **/
@RestController
@Api(tags = "组件报工")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN + "/job-booking")
public class JobBookingController {

    @Autowired
    private IJobBookingService jobBookingService;

    @GetMapping("query-work-order")
    @ApiOperation(value = "查询产线工单")
    public ResultBean queryWorkOrder(String workCenterCode, String organizeCode, String workOrderStatus, String partNo, String startTime, String endTime, Pager pager) {
        try {
            return ResultBean.success("查询成功").setListPager(this.jobBookingService.queryWorkOrder(workCenterCode, organizeCode, workOrderStatus, partNo, startTime, endTime, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("default/query-work-order")
    @ApiOperation(value = "查询默认产线工单")
    public ResultBean queryDefaultWorkOrder(String workCenterCode, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultObject(this.jobBookingService.queryDefaultWorkOrder(workCenterCode, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询包装内容
     *
     * @return {@link ResultBean}
     */
    @GetMapping("query")
    @ApiOperation(value = "查询包装内容")
    public ResultBean queryContainerInfo(String workCenterCode, String cnNo, String partNo, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultObject(this.jobBookingService.queryContainerInfo(workCenterCode, cnNo, partNo, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PostMapping("update")
    @ApiOperation(value = "确认按钮")
    public ResultBean updatePackage(@RequestBody MesPackageExt mesPackage) {

        try {
            this.jobBookingService.insertContainerJobBooking(mesPackage);
            return ResultBean.success("确认纸箱成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query-bom")
    @ApiOperation(value = "查询物料")
    public ResultBean queryPartBom(String partNo, String organizeCode) {
        try {
            return ResultBean.success("查询物料").setResultList(this.jobBookingService.queryPartBom(partNo, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @DeleteMapping("delete")
    @ApiOperation(value = "删除")
    public ResultBean deleteJobBookingPart(Long id) {
        try {
            this.jobBookingService.deleteJobBookingPart(id);
            return ResultBean.success("查询成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PostMapping("insert-part")
    @ApiOperation(value = "插入返工物料信息")
    public ResultBean insertJobBookingPart(@RequestBody MesJobBookingPart detail) {
        try {
            return ResultBean.success("修改成功").setResultObject(this.jobBookingService.insertJobBookingPart(detail))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("update-status")
    @ApiOperation(value = "完成包装")
    public ResultBean updateStatus(String workCenterCode, String ctNo, String partNo, String organizeCode) {
        try {

            this.jobBookingService.updateContainer(workCenterCode, ctNo, partNo, organizeCode);
            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @GetMapping("query-part")
    @ApiOperation(value = "插入返工物料信息")
    public ResultBean queryJobBookingPart(MesJobBookingPart detail) {
        try {
            return ResultBean.success("修改成功").setResultList(this.jobBookingService.queryJobBookingPart(detail))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping("submit-job-booking")
    @ApiOperation("提交报工到wms")
    public ResultBean submitJobBooking(@RequestBody List<MesJobBookingPart> jobBookingParts, String userName, String organizeCode) {
        try {
            this.jobBookingService.doMesWorkOver(jobBookingParts, userName, organizeCode);
            return ResultBean.success("修改成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
