package cn.estsh.i3plus.ext.mes.pcn.apiservice.controller.busi;

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesEnumExtService;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesEnumDetailService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesEnumDetail;
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.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Description : 枚举明细配置
 * @Reference :
 * @Author : junsheng.li
 * @CreateDate 2024/6/9 19:27
 * @Modify:
 **/
@RestController
@RequestMapping(MesCommonConstant.MES_YANFEN + "/mesEnumDetail")
public class MesEnumDetailController {
    @Autowired
    private IMesEnumExtService enumService;

    @Autowired
    private IMesEnumDetailService mesEnumDetailService;

    @GetMapping("/find")
    @ApiOperation(value = "查询包装明细")
    public ResultBean findMesEnumDetail(MesEnumDetail mesEnumDetail) {
        try {
            ValidatorBean.checkNotNull(mesEnumDetail.getEnumCode(), "枚举代码不能为空");
            mesEnumDetail.setOrganizeCode(!StringUtils.isEmpty(mesEnumDetail.getOrganizeCode())?mesEnumDetail.getOrganizeCode():AuthUtil.getOrganizeCode());
            List<MesEnumDetail> mesEnumDetailList = mesEnumDetailService.findMesEnumDetail(mesEnumDetail);
            return ResultBean.success("查询成功").setResultList(mesEnumDetailList);
        } catch (ImppBusiException imppException) {
            return ResultBean.fail(imppException);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value = "/enumlist")
    @ApiOperation(value = "获取MES系统枚举")
    public ResultBean enumlistByOrg(@RequestParam(name = "enumName") String enumName, @RequestParam(name = "organizeCode", required = false) String organizeCode) {
        try {
            organizeCode = org.springframework.util.StringUtils.isEmpty(organizeCode) ? AuthUtil.getOrganize().getOrganizeCode() : organizeCode;
            Map<String, Object> map = enumService.doGetMesEnumByEnumName(organizeCode, enumName);
            return ResultBean.success("查询成功")
                    .setResultObject(map)
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode());
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
