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

import cn.estsh.i3plus.ext.mes.api.base.IPrintPartParameterExtService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPrintPartParameterExt;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesShiftExt;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.util.CommonConstWords;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.impp.framework.base.controller.BaseController;
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 cn.estsh.impp.framework.boot.util.ValidatorBean;
import com.netflix.discovery.converters.Auto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-bu
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2022-01-11 17:17
 */
@RestController
@Api(tags = "物料打印配置参数接口")
@RequestMapping(CommonConstWords.BASE_URL_MES + "/print-part-parameter")
public class PrintPartParameterExtController extends MesBaseController {

    @Autowired
    private IPrintPartParameterExtService printPartParameterExtService;

    @GetMapping(value = "/query")
    @ApiOperation(value = "按条件分页查询打印所需物料拓展信息")
    public ResultBean queryShiftByPager(MesPrintPartParameterExt printPartParameterExt, Pager pager) {
        try {
            printPartParameterExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(printPartParameterExtService.queryPrintPartParameterExtByPager(printPartParameterExt, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "新增打印所需物料拓展信息")
    public ResultBean insertShift(MesPrintPartParameterExt printPartParameterExt) {
        try {
            ValidatorBean.beginValid(printPartParameterExt)
                    .notNull(MesExtConstWords.NET_WEIGHT, printPartParameterExt.getNetWeight())
                    .notNull(MesExtConstWords.ROUGH_WEIGHT, printPartParameterExt.getRoughWeight())
                    .notNull(MesExtConstWords.PACKAGE_SIZE, printPartParameterExt.getPackageSize())
                    .notNull(MesExtConstWords.PART_NO, printPartParameterExt.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, printPartParameterExt.getPartName());

            printPartParameterExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            printPartParameterExt.setCreateUser(AuthUtil.getSessionUser().getUserName());
            printPartParameterExtService.insertPrintPartParameterExt(printPartParameterExt);

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

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新打印所需物料拓展信息")
    public ResultBean updateShift(MesPrintPartParameterExt printPartParameterExt) {
        try {
            ValidatorBean.beginValid(printPartParameterExt)
                    .notNull(MesExtConstWords.NET_WEIGHT, printPartParameterExt.getNetWeight())
                    .notNull(MesExtConstWords.ROUGH_WEIGHT, printPartParameterExt.getRoughWeight())
                    .notNull(MesExtConstWords.PACKAGE_SIZE, printPartParameterExt.getPackageSize())
                    .notNull(MesExtConstWords.PART_NO, printPartParameterExt.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, printPartParameterExt.getPartName());

            printPartParameterExt.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            printPartParameterExt.setModifyUser(AuthUtil.getSessionUser().getUserName());
            printPartParameterExtService.updatePrintPartParameterExt(printPartParameterExt);
            return ResultBean.success("修改成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PutMapping(value = "/status-update")
    @ApiOperation(value = "根据id启用或禁用打印所需物料拓展信息")
    public ResultBean updateShiftValidById(Long id, Integer status) {
        try {
            if (StringUtils.isEmpty(id)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            printPartParameterExtService.updatePrintPartParameterExtValidById(id, status, 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);
        }
    }
}
