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

import cn.estsh.i3plus.ext.mes.api.base.IDataObjectExtService;
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.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesDataObject;
import cn.estsh.i3plus.pojo.mes.repository.MesDataObjectRepository;
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;
import org.springframework.util.StringUtils;

import java.util.List;

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

    @Autowired
    private MesDataObjectRepository dataObjectRepository;

    @Override
    public ListPager<MesDataObject> queryDataObjectByPager(MesDataObject dataObject, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getDataObject(dataObject);
        pager = PagerHelper.getPager(pager, dataObjectRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(dataObject.orderBy());
        List<MesDataObject> dataObjectList = dataObjectRepository.findByHqlWherePage(hqlPack, pager);
        return new ListPager(dataObjectList, pager);
    }

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

    @Override
    public MesDataObject updateDataObject(MesDataObject dataObject) {
        MesDataObject dataObjectDb = dataObjectRepository.getById(dataObject.getId());
        if (dataObjectDb == null) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("对象代码[%s]记录不存在", dataObject.getObjectCode())
                    .build();
        }

        dataObjectDb.setObjectCode(dataObject.getObjectCode());
        dataObjectDb.setObjectName(dataObject.getObjectName());
        ConvertBean.serviceModelUpdate(dataObjectDb, dataObject.getModifyUser());
        return dataObjectRepository.save(dataObjectDb);
    }

    @Override
    public int updateDataObjectValidById(Long id, Integer status, String organizeCode, String userName) {
        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值【%s】未被定义", status)
                    .build();
        }
        MesDataObject dataObjectDb = dataObjectRepository.getById(id);
        if (dataObjectDb == null || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == dataObjectDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", id)
                    .build();
        }
        return dataObjectRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{id},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, userName, TimeTool.getNowTime(true)});
    }

    @Override
    public List<MesDataObject> queryDataObjectList(String organizeCode) {
        if (StringUtils.isEmpty(organizeCode)) {
            return null;
        }
        return dataObjectRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.IS_VALID},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue()});
    }

}
