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

import cn.estsh.i3plus.ext.mes.pcn.api.busi.ep.ISxWorkCenterMovtrnCfgService;
import cn.estsh.i3plus.ext.mes.pcn.pojo.bean.MesWorkCenterMovtrnCfg;
import cn.estsh.i3plus.ext.mes.pcn.pojo.model.ep.WorkCenterMovtrnCfgModel;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
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.MesPcnBaseController;
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 oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author: wangjie
 * @CreateDate: 2021/01/19 15:37
 * @Description:
 **/
@RestController
@RequestMapping(CommonConstWords.BASE_URL_MES_PCN)
@Api(tags = "生产线立库配置控制层")
public class SxWorkCenterMovtrnCfgController extends MesPcnBaseController {

    @Autowired
    private ISxWorkCenterMovtrnCfgService workCenterMovtrnCfgService;

    @GetMapping(value = "/ext/work-center-movtrn-cfg/query")
    @ApiOperation(value = "按条件分页查询生产线立库配置信息")
    public ResultBean queryWorkCenterMovtrnCfgByPager(MesWorkCenterMovtrnCfg workCenterMovtrnCfg, Pager pager) {
        try {
            ValidatorBean.beginValid(workCenterMovtrnCfg)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, workCenterMovtrnCfg.getOrganizeCode());
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(workCenterMovtrnCfgService.queryWorkCenterMovtrnCfgByPager(workCenterMovtrnCfg, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/ext/work-center-movtrn-cfg/save")
    @ApiOperation(value = "保存生产线立库配置信息")
    public ResultBean saveWorkCenterMovtrnCfg(WorkCenterMovtrnCfgModel workCenterMovtrnCfg) {
        try {
            ValidatorBean.beginValid(workCenterMovtrnCfg)
                    .notNull(MesPcnExtConstWords.ORGANIZE_CODE, workCenterMovtrnCfg.getOrganizeCode())
                    .notNull(MesPcnExtConstWords.MODIFY_USER, workCenterMovtrnCfg.getModifyUser())
                    .notNull(MesPcnExtConstWords.WORK_CENTER_CODE, workCenterMovtrnCfg.getWorkCenterCode())
                    .notNull(MesPcnExtConstWords.STATUS, workCenterMovtrnCfg.getStatus());

            List<WorkCenterMovtrnCfgModel> workCenterMovtrnCfgList = new ArrayList<>();
            workCenterMovtrnCfgList.add(workCenterMovtrnCfg);
            workCenterMovtrnCfgService.saveWorkCenterMovtrnCfg(workCenterMovtrnCfgList, CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());

            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 = "/ext/work-center-movtrn-cfg/query-by-center")
    @ApiOperation(value = "根据生产线代码查询生产线立库配置信息")
    public ResultBean queryWorkCenterMovtrnCfgByCenter(@RequestBody Map<String, Object> params) {
        try {
            String organizeCode = (String) params.get(MesPcnExtConstWords.ORGANIZE_CODE);
            String workCenterCode = (String) params.get(MesPcnExtConstWords.WORK_CENTER_CODE);
            return ResultBean.success("查询成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(workCenterMovtrnCfgService.queryWorkCenterMovtrnCfgByWorkCenterCode(organizeCode, workCenterCode));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

}
