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

import cn.estsh.i3plus.ext.mes.api.base.IAffiliatedStartConfigService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesAffiliatedStartConfig;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesAffiliatedStartConfigRepository;
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.platform.common.util.MesConstWords;
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.sqlpack.MesHqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * @author yumingxing
 * @version 1.0
 * @date 2021/1/29 14:44
 **/
@Service
public class AffiliatedStartConfigService implements IAffiliatedStartConfigService {

    @Autowired
    private MesAffiliatedStartConfigRepository mesAffiliatedStartConfigRepository;
    @Override
    public MesAffiliatedStartConfig insert(MesAffiliatedStartConfig mesAffiliatedStartConfig) {
        ConvertBean.modelInitialize(mesAffiliatedStartConfig, AuthUtil.getSessionUser());
        mesAffiliatedStartConfig.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        return mesAffiliatedStartConfigRepository.insert(mesAffiliatedStartConfig);
    }


    @Override
    public MesAffiliatedStartConfig update(MesAffiliatedStartConfig mesAffiliatedStartConfig) {
        //1、判断单据是否存在
        Long id = mesAffiliatedStartConfig.getId();
        MesAffiliatedStartConfig affiliatedStartConfig = mesAffiliatedStartConfigRepository.getById(id);
        if(ObjectUtils.isEmpty(affiliatedStartConfig)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }
        BeanUtils.copyProperties(mesAffiliatedStartConfig,affiliatedStartConfig);
        ConvertBean.serviceModelUpdate(affiliatedStartConfig, AuthUtil.getSessionUser().getUserName());
        return mesAffiliatedStartConfigRepository.save(affiliatedStartConfig);
    }

    @Override
    public ListPager queryByPager(MesAffiliatedStartConfig mesAffiliatedStartConfig, Pager pager) {
        DdlPackBean packBean = MesHqlPack.getMesAllData(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesAffiliatedStartConfig.getAffiliatedStartClass(),"affiliatedStartClass",packBean);
        DdlPreparedPack.getNumEqualPack(mesAffiliatedStartConfig.getIsValid(),"isValid",packBean);
        DdlPreparedPack.getStringEqualPack(mesAffiliatedStartConfig.getExecuteDesc(),"executeDesc",packBean);
        pager = PagerHelper.getPager(pager, mesAffiliatedStartConfigRepository.findByHqlWhereCount(packBean));
        return new ListPager(mesAffiliatedStartConfigRepository.findByHqlWherePage(packBean, pager), pager);
    }

    @Override
    public int updateIsValidById(long id, Integer status) {
        //1、判断单据是否存在
        MesAffiliatedStartConfig mesAffiliatedStartConfig = mesAffiliatedStartConfigRepository.getById(id);
        if(ObjectUtils.isEmpty(mesAffiliatedStartConfig)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(), "自动配置不存在");
        }

        return mesAffiliatedStartConfigRepository.updateByProperties(new String[]{"id"}, new Object[]{id},
                new String[]{MesConstWords.IS_VALID, "modifyUser", "modifyDatetime"},
                new Object[]{status, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true)});
    }


    /**
     * 异常
     * @param softTypeCode softTypeCode
     * @param errorDetailString 错误消息字符串
     * @param imppExceptionEnumCode 异常代码
     * @param args 参数
     */
    private void throwImppException(String softTypeCode, String errorDetailString, String imppExceptionEnumCode, Object... args) {
        throw ImppExceptionBuilder.newInstance()
                .setSystemID(softTypeCode)
                .setErrorCode(imppExceptionEnumCode)
                .setErrorDetail(errorDetailString,args)
                .build();
    }
}
