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

import cn.estsh.i3plus.ext.mes.api.base.bu.IMesProductPrintTypeService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProductPrintType;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
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.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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xinwang.yi
 * @CreateDate: 2022/01/03 10:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES + "/ext")
@Api(tags = "产品打印类型控制层")
public class MesProductPrintTypeController extends MesBaseController {

    @Autowired
    private IMesProductPrintTypeService productPrintTypeService;

    @GetMapping(value = "/product-print-type/query")
    @ApiOperation(value = "按条件分页查询产品打印类型信息")
    public ResultBean queryProductPrintTypeByPager(MesProductPrintType productPrintType, Pager pager) {
        try {
            productPrintType.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(productPrintTypeService.queryProductPrintTypeByPager(productPrintType, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/product-print-type/insert")
    @ApiOperation(value = "新增产品打印类型信息")
    public ResultBean insertProductPrintType(MesProductPrintType productPrintType) {
        try {
            ValidatorBean.beginValid(productPrintType)
                    .notNull(MesExtConstWords.PART_NO, productPrintType.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, productPrintType.getPartName())
                    .notNull(MesExtConstWords.PRINT_TYPE, productPrintType.getPrintType())
                    .notNull(MesExtConstWords.PRINT_MODE, productPrintType.getPrintMode());

            productPrintType.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            productPrintType.setCreateUser(AuthUtil.getSessionUser().getUserName());
            productPrintTypeService.insertProductPrintType(productPrintType);

            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 = "/product-print-type/update")
    @ApiOperation(value = "更新产品打印类型信息")
    public ResultBean updateProductPrintType(MesProductPrintType productPrintType) {
        try {
            ValidatorBean.beginValid(productPrintType)
                    .notNull(MesExtConstWords.ID, productPrintType.getId())
                    .notNull(MesExtConstWords.PART_NO, productPrintType.getPartNo())
                    .notNull(MesExtConstWords.PART_NAME, productPrintType.getPartName())
                    .notNull(MesExtConstWords.PRINT_TYPE, productPrintType.getPrintType())
                    .notNull(MesExtConstWords.PRINT_MODE, productPrintType.getPrintMode());

            productPrintType.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            productPrintType.setModifyUser(AuthUtil.getSessionUser().getUserName());
            productPrintTypeService.updateProductPrintType(productPrintType);
            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 = "/product-print-type/status-update")
    @ApiOperation(value = "根据id启用或禁用产品打印类型信息")
    public ResultBean updateProductPrintTypeValidById(String[] id, Integer status) {
        try {
            ValidatorBean.checkNotNull(id, "请选择需要操作的资源");
            id = ConvertBean.modelSafeArrayNumber(id, true);
            if (id.length == 0) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.EMS.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            productPrintTypeService.updateProductPrintTypeValidById(id, status, AuthUtil.getOrganize().getOrganizeCode(), 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);
        }
    }

}
