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

import cn.estsh.i3plus.ext.mes.api.base.ep.IEpPartCheckExtService;
import cn.estsh.i3plus.ext.mes.pojo.model.ep.EpPartCheckExtModel;
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.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.enumutil.MesEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.MesPcnEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.DdlPreparedPack;
import cn.estsh.i3plus.pojo.mes.bean.MesPartCheck;
import cn.estsh.i3plus.pojo.mes.repository.MesPartCheckRepository;
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.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @DESCRIPTION:
 * @USER: xinwang.yi
 * @DATE: 2021-12-27 15:00
 */
@Service
public class EpPartCheckExtService implements IEpPartCheckExtService {

    @Autowired
    private MesPartCheckRepository partCheckRepository;

    @Override
    public ListPager<EpPartCheckExtModel> queryMesPartCheckByPager(MesPartCheck partCheck, Pager pager) {
        List<EpPartCheckExtModel> resultList = new ArrayList<>();
        DdlPackBean hqlPack = MesHqlPack.getMesPartCheck(partCheck, AuthUtil.getOrganize().getOrganizeCode());
        pager = PagerHelper.getPager(pager, partCheckRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(partCheck.orderBy());
        List<MesPartCheck> partCheckList = partCheckRepository.findByHqlWherePage(hqlPack , pager);
        if (!CollectionUtils.isEmpty(partCheckList)) {
            for (MesPartCheck check : partCheckList) {
                if (null == check) {
                    continue;
                }
                EpPartCheckExtModel result = new EpPartCheckExtModel();
                BeanUtils.copyProperties(check, result);
                result.setDataTypeName(MesExtEnumUtil.PART_CHECK_DATA_TYPE.valueOfDescription(check.getDataType()));
                resultList.add(result);
            }
        }
        return new ListPager(resultList, pager);
    }

    @Override
    public MesPartCheck insertMesPartCheck(MesPartCheck partCheck) {
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(partCheck.getOrganizeCode());
        if (!StringUtils.isEmpty(partCheck.getPartNo())) {
            DdlPreparedPack.getStringEqualPack(partCheck.getPartNo(), MesExtConstWords.PART_NO, packBean);
        } else {
            DdlPreparedPack.getStringNullPack(MesExtConstWords.PART_NO, packBean);
        }
        DdlPreparedPack.getStringEqualPack(partCheck.getWorkCenterCode(), MesExtConstWords.WORK_CENTER_CODE, packBean);
        MesPartCheck partCheckDb = partCheckRepository.getByProperty(packBean);
        if (null != partCheckDb){
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("记录已存在")
                    .build();
        }
        ConvertBean.serviceModelInitialize(partCheck, partCheck.getCreateUser());
        return partCheckRepository.insert(partCheck);
    }

    @Override
    public MesPartCheck updateMesPartCheck(MesPartCheck partCheck) {

        MesPartCheck partCheckDb = partCheckRepository.getById(partCheck.getId());
        if (null == partCheckDb ||
                (null != partCheckDb && CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue() == partCheckDb.getIsDeleted())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("ID为[%s]的记录不存在", partCheck.getId())
                    .build();
        }

        partCheckDb.setCheckSpelExpress(partCheck.getCheckSpelExpress());
        partCheckDb.setTypeSpelExpress(partCheck.getTypeSpelExpress());
        partCheckDb.setRecordNumSpelExpress(partCheck.getRecordNumSpelExpress());
        partCheckDb.setLimitSpelExpress(partCheck.getLimitSpelExpress());
        partCheckDb.setRecordNumDesc(partCheck.getRecordNumDesc());
        partCheckDb.setDataType(partCheck.getDataType());
        ConvertBean.serviceModelUpdate(partCheckDb, partCheck.getModifyUser());
        return partCheckRepository.save(partCheckDb);
    }

    @Override
    public int updateMesPartCheckStatusById(Long id, int status, String userName) {

        return partCheckRepository.updateByProperties(new String[]{MesExtConstWords.ID},new Object[]{id},
                new String[]{MesConstWords.IS_VALID, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME},
                new Object[]{status, userName, TimeTool.getNowTime(true)});
    }
}
