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

import cn.estsh.i3plus.ext.mes.pcn.api.base.bu.IBuFirstOrderService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesFirstOrderDetail;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.MesPcnBaseController;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@Api(tags = "工单首检（改为Q管理）")
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN+ "/bu-ext/first")
public class BuFirstOrderController extends MesPcnBaseController {

    @Autowired
    private IBuFirstOrderService firstOrderService;

    /**
     * 查询单据作业
     *
     * @param serialNumber
     * @param orderNo
     * @param workOrderNo
     * @param organizeCode
     * @param modifyUser
     * @return
     */
    @PutMapping(value = "/check-serial-number/do")
    @ApiOperation(value = "检验条码")
    public ResultBean doCheckSerialNumber(String serialNumber, String orderNo, String workOrderNo, String organizeCode, String modifyUser) {
        try {
            firstOrderService.doCheckSerialNumber(serialNumber, orderNo, workOrderNo, organizeCode, modifyUser);
            return ResultBean.success("检查成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 查询MES_Q管理作业明细
     *
     * @param orderNo
     * @param organizeCode
     * @return
     */
    @GetMapping(value = "/mes-first-order-detail/find")
    @ApiOperation(value = "查询首检单（改为Q管理）明细数据")
    public ResultBean findMesFirstOrderDetail(String orderNo, String organizeCode) {
        try {
            return ResultBean.success("查询成功").setResultList(firstOrderService.findMesFirstOrderDetail(orderNo, organizeCode))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 修改首检明细信息
     *
     * @param mesFirstOrderDetail
     * @return
     */
    @PutMapping(value = "/mes-first-order-detail/update")
    @ApiOperation(value = "修改Q管理明细信息")
    public ResultBean updateMesFirstOrderDetail(@RequestBody MesFirstOrderDetail mesFirstOrderDetail) {
        try {
            String userName = AuthUtil.getSessionUser().getUserName();
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(firstOrderService.updateMesFirstOrderDetail(mesFirstOrderDetail, userName));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
