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

import cn.estsh.i3plus.ext.mes.api.base.IMesPartCheckDetailService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPersonCheckDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkOrderExt;
import cn.estsh.i3plus.ext.mes.pojo.model.PartCheckDetailModel;
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.base.controller.MesBaseController;
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.*;

import java.util.List;

/**
 * @Author: xiangming.liao
 * @CreateDate: 2021/01/18 13:33 下午
 * @Description:
 **/
@RestController
@Api(tags = "物料点检接口")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/part-check")
public class PartCheckDetailController extends MesBaseController {
    @Autowired
    private IMesPartCheckDetailService mesPartCheckDetailService;

    @GetMapping("part-check/query")
    @ApiOperation(value = "按条件分页查询物料点检单明细")
    public ResultBean queryMesPartCheckDetail(MesPartCheckDetail detail, Pager pager) {

        try {
            detail.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setListPager(this.mesPartCheckDetailService.queryMesPartCheckDetail(detail, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


    @PutMapping(value = "/part-check/by-id/update")
    @ApiOperation(value = "点击物料点检查询报表中的品质确认和班组确认按钮")
    public ResultBean updatePartCheckById(@RequestBody List<PartCheckDetailModel> partCheckDetailList) {
        try {
            mesPartCheckDetailService.updatePartCheckById(partCheckDetailList, AuthUtil.getSessionUser().getUserName());

            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 班组确认
     *
     * @param ids
     * @param shiftGroupCheck
     * @return
     */
    @PutMapping("part-check/shift-group-check/update")
    @ApiOperation(value = "班组确认")
    public ResultBean shiftGroupCheckUpdate(String[] ids, Integer shiftGroupCheck) {
        try {
            return ResultBean.success("班组确认成功").setResultObject(this.mesPartCheckDetailService.updateShiftGroupCheck(ids, shiftGroupCheck))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 品质确认
     *
     * @param ids
     * @param qualityCheck
     * @return
     */
    @PutMapping("part-check/quality-check/update")
    @ApiOperation(value = "品质确认")
    public ResultBean qualityCheckUpdate(String[] ids, Integer qualityCheck) {
        try {
            return ResultBean.success("品质确认成功").setResultObject(this.mesPartCheckDetailService.updateQualityCheck(ids, qualityCheck))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 物料点检作业
     * @param orderNo
     * @param orderStatus
     * @param itemPartNo
     * @return
     */
    @PutMapping("part-check/order-status-check/update")
    @ApiOperation(value = "单据状态修改成功")
    public ResultBean orderStatusCheckUpdate(String orderNo, Integer orderStatus, String itemPartNo) {
        try {
            return ResultBean.success("单据状态修改成功").setResultObject(this.mesPartCheckDetailService.orderStatusCheckUpdate(orderNo, orderStatus, itemPartNo))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
