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

import cn.estsh.i3plus.ext.mes.api.base.IDataObjectExtService;
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.i3plus.pojo.mes.bean.MesDataObject;
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 SxDataObjectController extends MesBaseController {

    @Autowired
    private IDataObjectExtService dataObjectExtService;

    @GetMapping(value = "/data-object-ext/query")
    @ApiOperation(value = "按条件分页查询数据对象信息信息")
    public ResultBean queryDataObjectByPager(MesDataObject dataObject, Pager pager) {
        try {
            dataObject.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            return ResultBean.success("操作成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setListPager(dataObjectExtService.queryDataObjectByPager(dataObject, pager));
        } catch (ImppBusiException busExcep) {
            return ResultBean.fail(busExcep);
        } catch (Exception e) {
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @PostMapping(value = "/data-object-ext/insert")
    @ApiOperation(value = "新增数据对象信息信息")
    public ResultBean insertDataObject(MesDataObject dataObject) {
        try {
            ValidatorBean.beginValid(dataObject)
                    .notNull(MesExtConstWords.OBJECT_CODE, dataObject.getObjectCode())
                    .notNull(MesExtConstWords.OBJECT_NAME, dataObject.getObjectName());

            dataObject.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            dataObject.setCreateUser(AuthUtil.getSessionUser().getUserName());
            dataObjectExtService.insertDataObject(dataObject);

            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 = "/data-object-ext/update")
    @ApiOperation(value = "更新数据对象信息信息")
    public ResultBean updateDataObject(MesDataObject dataObject) {
        try {
            ValidatorBean.beginValid(dataObject)
                    .notNull(MesExtConstWords.ID, dataObject.getId())
                    .notNull(MesExtConstWords.OBJECT_CODE, dataObject.getObjectCode())
                    .notNull(MesExtConstWords.OBJECT_NAME, dataObject.getObjectName());

            dataObject.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
            dataObject.setModifyUser(AuthUtil.getSessionUser().getUserName());
            dataObjectExtService.updateDataObject(dataObject);
            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 = "/data-object-ext/status-update")
    @ApiOperation(value = "根据id启用或禁用数据对象信息信息")
    public ResultBean updateDataObjectValidById(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();
            }
            dataObjectExtService.updateDataObjectValidById(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);
        }
    }

}
