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

import cn.estsh.i3plus.ext.mes.pcn.api.base.IMesContainerSnService;
import cn.estsh.i3plus.ext.mes.pcn.apiservice.util.MesPcnException;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
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.base.util.StringUtil;
import cn.estsh.i3plus.pojo.mes.bean.MesContainerSn;
import cn.estsh.i3plus.pojo.mes.repository.MesContainerSnRepository;
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 java.util.List;

/**
 * @Description : 容器条码-业务方法实现
 * @Reference :
 * @Author : logic
 * @CreateDate : 2025/6/16 10:44
 * @Modify:
 **/
@Service
@Slf4j
public class MesContainerSnServiceImpl implements IMesContainerSnService {

    @Autowired
    private MesContainerSnRepository mesContainerSnRepository;

    @Override
    public ListPager<MesContainerSn> queryMesContainerSnByPager(MesContainerSn mesContainerSn, Pager pager) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(mesContainerSn.getOrganizeCode());
        DdlPreparedPack.getStringEqualPack(mesContainerSn.getContainerTypeCode(), "containerTypeCode", ddlPackBean);
        DdlPreparedPack.getStringEqualPack(mesContainerSn.getContainerSn(), "containerSn", ddlPackBean);
        pager = PagerHelper.getPager(pager, mesContainerSnRepository.findByHqlWhereCount(ddlPackBean));
        List<MesContainerSn> containerSnList = mesContainerSnRepository.findByHqlWherePage(ddlPackBean, pager);
        return new ListPager<>(containerSnList, pager);
    }

    @Override
    public void checkContainerSnIsExist(String containerSn, String organizeCode) {
        DdlPackBean ddlPackBean = DdlPackBean.getDdlPackBean(organizeCode);
        DdlPreparedPack.getStringEqualPack(containerSn, "containerSn", ddlPackBean);
        if (!mesContainerSnRepository.isExitByHql(ddlPackBean)) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES_PCN.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("容器条码【%s】不存在!", containerSn)
                    .build();
        }
    }

    @Override
    public MesContainerSn getContainerSn(String organizeCode, String containerSn) {
        if (StringUtil.isEmptyAndNull(organizeCode) || StringUtil.isEmptyAndNull(containerSn)) {
            return null;
        }
        return mesContainerSnRepository.getByProperty(
                new String[]{MesPcnExtConstWords.ORGANIZE_CODE, MesPcnExtConstWords.IS_DELETED, MesPcnExtConstWords.IS_VALID, "containerSn"},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), CommonEnumUtil.IS_VAILD.VAILD.getValue(), containerSn});
    }

    @Override
    public MesContainerSn checkContainerSn(String organizeCode, String containerSn) {
        MesContainerSn mesContainerSn = getContainerSn(organizeCode, containerSn);
        if (mesContainerSn == null) {
            MesPcnException.throwMesBusiException("容器条码【%s】信息不存在", containerSn);
        }
        return mesContainerSn;
    }
}
