package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base.ep;

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpObjectCfgExtService;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
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.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesObjectCfg;
import cn.estsh.i3plus.pojo.mes.repository.MesObjectCfgRepository;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class EpObjectCfgExtService implements IEpObjectCfgExtService {

    @Autowired
    private MesObjectCfgRepository objectCfgRepository;

    @Override
    public MesObjectCfg insertObjectCfg(MesObjectCfg objectCfg) {
        DdlPackBean packBean = MesExtHqlPack.getAllBaseData(objectCfg.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(objectCfg.getObjectCode(), MesExtConstWords.OBJECT_CODE, packBean);
        DdlPreparedPack.getStringEqualPack(objectCfg.getFieldCode(), MesExtConstWords.FIELD_CODE, packBean);
        MesObjectCfg objectCfgDb = objectCfgRepository.getByProperty(packBean);
        if (null != objectCfgDb) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("对象代码[%s]字段代码[%s]记录已存在", objectCfg.getObjectCode(), objectCfg.getFieldCode())
                    .build();
        }
        ConvertBean.serviceModelInitialize(objectCfg, objectCfg.getCreateUser());
        return objectCfgRepository.insert(objectCfg);
    }

    @Override
    public MesObjectCfg updateObjectCfg(MesObjectCfg objectCfg) {
        MesObjectCfg objectCfgDb = objectCfgRepository.getById(objectCfg.getId());
        if (objectCfgDb == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("对象代码[%s]字段代码[%s]记录不存在", objectCfg.getObjectCode(), objectCfg.getFieldCode())
                    .build();
        }

        objectCfgDb.setFieldName(objectCfg.getFieldName());
        objectCfgDb.setFieldType(objectCfg.getFieldType());
        objectCfgDb.setFieldLength(objectCfg.getFieldLength());
        objectCfgDb.setShowSeq(objectCfg.getShowSeq());
        objectCfgDb.setIsShow(objectCfg.getIsShow());
        objectCfgDb.setIsSave(objectCfg.getIsSave());
        ConvertBean.serviceModelUpdate(objectCfgDb, objectCfg.getModifyUser());
        return objectCfgRepository.save(objectCfgDb);
    }

}
