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

import cn.estsh.i3plus.ext.mes.api.base.IWeighManagerService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWeighManager;
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.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 yumingxing
 * @version 1.0
 * @date 2021/2/2 15:51
 **/
@RestController
@Api(tags = "称重管理")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/weigh-manager")
public class WeighManageController {

    @Autowired
    private IWeighManagerService weighManagerService;

    @GetMapping("detail")
    @ApiOperation(value = "查询详情")
    public ResultBean queryDetail(MesWeighManager detail) {
        try {
            return ResultBean.success("查询成功").setResultList(this.weighManagerService.detail(detail))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping("query")
    @ApiOperation(value = "条件分页")
    public ResultBean queryMesPartCheckDetail(MesWeighManager weighManager, Pager pager) {
        try {
            weighManager.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setListPager(this.weighManagerService.queryByPager(weighManager, pager))
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 班组确认
     *
     * @param details
     * @return
     */
    @PutMapping("/shift-group-check/update")
    @ApiOperation(value = "班组确认")
    public ResultBean shiftGroupCheckUpdate(@RequestBody List<MesWeighManager> details) {
        try {
            this.weighManagerService.updateShiftGroupCheck(details);
            return ResultBean.success("班组确认成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    /**
     * 品质确认
     *
     * @param details
     * @return
     */
    @PutMapping("/quality-check/update")
    @ApiOperation(value = "品质确认")
    public ResultBean qualityCheckUpdate(@RequestBody List<MesWeighManager> details) {
        try {
            this.weighManagerService.updateQualityCheck(details);
            return ResultBean.success("品质确认成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }


}
