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

import cn.estsh.i3plus.ext.mes.api.base.ep.IHappenStationService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesHappenStation;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesHappenStationRepository;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
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.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @PROJECT_NAME: i3plus-mes-panasonic-dev
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-16 10:32
 */
@Service
public class HappenStationService implements IHappenStationService {

    @Autowired
    private MesHappenStationRepository happenStationRepository;

    @Override
    public MesHappenStation insert(MesHappenStation happenStation) {
        MesHappenStation happenStationMeta = happenStationRepository.getByProperty(new String[]{MesExtConstWords.WORK_CENTER_CODE, MesExtConstWords.WORK_CELL_CODE}, new Object[]{happenStation.getWorkCenterCode(), happenStation.getWorkCellCode()});
        if(happenStationMeta != null){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(), ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode(), "已存在该工位发生岗位");
        }
        ConvertBean.modelInitialize(happenStation, AuthUtil.getSessionUser());
        happenStation.setOrganizeCode(AuthUtil.getOrganize().getOrganizeCode());
        happenStation.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return happenStationRepository.insert(happenStation);
    }

    @Override
    public MesHappenStation update(MesHappenStation happenStation) {
        MesHappenStation happenStationMeta = happenStationRepository.getById(happenStation.getId());
        if(happenStationMeta == null){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(),ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(),"不存在该工位发生岗位");
        }

        ConvertBean.modelUpdate(happenStationMeta,AuthUtil.getSessionUser());
        happenStationMeta.setHappenStation(happenStation.getHappenStation());
        happenStationMeta.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        return happenStationRepository.save(happenStationMeta);
    }

    @Override
    public ListPager queryByPager(MesHappenStation happenStation, Pager pager) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(AuthUtil.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(happenStation.getWorkCenterCode(),MesExtConstWords.WORK_CENTER_CODE,packBean);
        DdlPreparedPack.getStringEqualPack(happenStation.getWorkCellCode(),MesExtConstWords.WORK_CELL_CODE,packBean);
        DdlPreparedPack.getStringRightLikerPack(happenStation.getHappenStation(),MesExtConstWords.HAPPEN_STATION,packBean);
        pager = PagerHelper.getPager(pager,happenStationRepository.findByHqlWhereCount(packBean));
        return new ListPager(happenStationRepository.findByHqlWherePage(packBean,pager),pager);
    }

    @Override
    public int updateIsValidById(long id, Integer status) {
        MesHappenStation happenStationMeta = happenStationRepository.getById(id);
        if(StringUtils.isEmpty(happenStationMeta)){
            throwImppException(CommonEnumUtil.SOFT_TYPE.MES.getCode(),ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode(),"没有该记录");
        }

        return happenStationRepository.updateByProperties(new String[]{MesExtConstWords.ID}, new Object[]{MesExtConstWords.ID},
                new String[]{MesExtConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME, MesExtConstWords.SYSTEM_SYNC_STATUS},
                new Object[]{status, AuthUtil.getSessionUser().getUserName(), TimeTool.getNowTime(true), MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue()});
    }

    /**
     * 异常
     * @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();
    }
}
