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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxPartCategoryPlanRateService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesPartCategoryPlanRate;
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.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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES)
@Api(tags = "产品类别计划比例控制层")
public class SxPartCategoryPlanRateController extends MesBaseController {

    @Autowired
    private ISxPartCategoryPlanRateService partCategoryPlanRateService;

    @GetMapping(value = "/sx-ext/part-category-plan-rate/query")
    @ApiOperation(value = "按条件分页查询产品类别计划比例信息")
    public ResultBean queryPartCategoryPlanRateByPager(MesPartCategoryPlanRate partCategoryPlanRate, Pager pager) {
        try {
            partCategoryPlanRate.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(partCategoryPlanRateService.queryPartCategoryPlanRateByPager(partCategoryPlanRate, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-ext/part-category-plan-rate/query-list")
    @ApiOperation(value = "按条件查询产品类别计划比例信息")
    public ResultBean queryPartCategoryPlanRateList(MesPartCategoryPlanRate partCategoryPlanRate) {
        try {
            partCategoryPlanRate.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(partCategoryPlanRateService.queryPartCategoryPlanRateList(partCategoryPlanRate));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/sx-ext/part-category-plan-rate/insert")
    @ApiOperation(value = "新增产品类别计划比例信息")
    public ResultBean insertPartCategoryPlanRate(MesPartCategoryPlanRate partCategoryPlanRate) {
        try {
            ValidatorBean.beginValid(partCategoryPlanRate)
                    .notNull(MesExtConstWords.CATEGORY_CODE, partCategoryPlanRate.getCategoryCode())
                    .notNull(MesExtConstWords.CATEGORY_NAME, partCategoryPlanRate.getCategoryName())
                    .notNull(MesExtConstWords.PLAN_RATE, partCategoryPlanRate.getPlanRate());

            partCategoryPlanRate.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            partCategoryPlanRate.setCreateUser(AuthUtil.getSessionUser().getUserName());
            partCategoryPlanRateService.insertPartCategoryPlanRate(partCategoryPlanRate);

            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 = "/sx-ext/part-category-plan-rate/update")
    @ApiOperation(value = "更新产品类别计划比例信息")
    public ResultBean updatePartCategoryPlanRate(MesPartCategoryPlanRate partCategoryPlanRate) {
        try {
            ValidatorBean.beginValid(partCategoryPlanRate)
                    .notNull(MesExtConstWords.ID, partCategoryPlanRate.getIdStr())
                    .notNull(MesExtConstWords.CATEGORY_CODE, partCategoryPlanRate.getCategoryCode())
                    .notNull(MesExtConstWords.CATEGORY_NAME, partCategoryPlanRate.getCategoryName())
                    .notNull(MesExtConstWords.PLAN_RATE, partCategoryPlanRate.getPlanRate());

            partCategoryPlanRate.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            partCategoryPlanRate.setModifyUser(AuthUtil.getSessionUser().getUserName());
            partCategoryPlanRateService.updatePartCategoryPlanRate(partCategoryPlanRate);
            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 = "/sx-ext/part-category-plan-rate/status-update")
    @ApiOperation(value = "根据id启用或禁用产品类别计划比例信息")
    public ResultBean updatePartCategoryPlanRateValidById(String idStr, Integer status) {
        try {
            if (StringUtils.isEmpty(idStr)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            partCategoryPlanRateService.updatePartCategoryPlanRateValidById(idStr, 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);
        }
    }

    @DeleteMapping(value = "/sx-ext/part-category-plan-rate/delete")
    @ApiOperation(value = "根据id删除产品类别计划比例信息")
    public ResultBean deletePartCategoryPlanRateById(String idStr) {
        try {
            if (StringUtils.isEmpty(idStr)) {
                throw ImppExceptionBuilder.newInstance()
                        .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                        .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                        .setErrorDetail("请选择需要操作的资源。")
                        .build();
            }
            partCategoryPlanRateService.deletePartCategoryPlanRateById(idStr, 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);
        }
    }

    @GetMapping(value = "/sx-ext/part-category-plan-rate/query-all-list")
    @ApiOperation(value = "获取所有产品类别计划比例信息")
    public ResultBean queryAllPartCategoryPlanRateList(@RequestParam("organizeCode") String organizeCode) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(partCategoryPlanRateService.queryAllPartCategoryPlanRateList(organizeCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/sx-ext/part-category-plan-rate/check-is-exist")
    @ApiOperation(value = "获取所有产品类别计划比例信息")
    public ResultBean checkPartCategoryPlanRateIsExist(@RequestParam("organizeCode") String organizeCode, @RequestParam("categoryCode") String categoryCode) {
        try {
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(partCategoryPlanRateService.checkPartCategoryPlanRateIsExist(organizeCode, categoryCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
