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

import cn.estsh.i3plus.ext.mes.api.base.IObjectCfgExtService;
import cn.estsh.i3plus.ext.mes.pojo.model.ObjectCfgExtModel;
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.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.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

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

    @Autowired
    private MesObjectCfgRepository objectCfgRepository;

    @Override
    public ListPager<ObjectCfgExtModel> queryObjectCfgByPager(MesObjectCfg objectCfg, Pager pager) {
        List<ObjectCfgExtModel> modelList = new ArrayList<>();
        DdlPackBean hqlPack = MesExtHqlPack.getObjectCfg(objectCfg);
        pager = PagerHelper.getPager(pager, objectCfgRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(objectCfg.orderBy());
        List<MesObjectCfg> objectCfgList = objectCfgRepository.findByHqlWherePage(hqlPack, pager);
        if (!CollectionUtils.isEmpty(objectCfgList)) {
            for (MesObjectCfg item : objectCfgList) {
                if (null == item) {
                    continue;
                }
                ObjectCfgExtModel model = new ObjectCfgExtModel();
                BeanUtils.copyProperties(item, model);
                model.setIsShowName(CommonEnumUtil.TRUE_OR_FALSE.valueOfDescription(item.getIsShow()));
                modelList.add(model);
            }
        }
        return new ListPager(modelList, pager);
    }

    @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);
        DdlPreparedPack.getStringEqualPack(objectCfg.getPlcCode(), MesExtConstWords.PLC_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]PLC代码[%s]记录已存在", objectCfg.getObjectCode(), objectCfg.getFieldCode(), objectCfg.getPlcCode())
                    .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]PLC代码[%s]记录不存在", objectCfg.getObjectCode(), objectCfg.getFieldCode(), objectCfg.getPlcCode())
                    .build();
        }

        objectCfgDb.setFieldName(objectCfg.getFieldName());
        objectCfgDb.setShowSeq(objectCfg.getShowSeq());
        objectCfgDb.setIsShow(objectCfg.getIsShow());
        ConvertBean.serviceModelUpdate(objectCfgDb, objectCfg.getModifyUser());
        return objectCfgRepository.save(objectCfgDb);
    }

    @Override
    public int updateObjectCfgValidById(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();
        }
        MesObjectCfg objectCfgDb = objectCfgRepository.getById(id);
        if (objectCfgDb == null || CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == objectCfgDb.getIsDeleted()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID[%s]记录不存在", id)
                    .build();
        }
        return objectCfgRepository.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)});
    }

}
