package com.xisc.pm.infra.repository.impl;

import com.google.common.collect.Lists;
import com.xisc.pm.domain.entity.PmCmdSmallPlt;
import com.xisc.pm.domain.repository.PmCmdSmallPltRepository;
import com.xisc.pm.infra.mapper.PmCmdSmallPltMapper;
import org.apache.commons.collections.CollectionUtils;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 命令小板表(PmCmdSmallPlt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-08-01 10:25:35
 */
@Component
public class PmCmdSmallPltRepositoryImpl extends BaseRepositoryImpl<PmCmdSmallPlt> implements PmCmdSmallPltRepository {
    @Resource
    private PmCmdSmallPltMapper pmCmdSmallPltMapper;

    @Override
    public List<PmCmdSmallPlt> selectList(PmCmdSmallPlt pmCmdSmallPlt) {
        return pmCmdSmallPltMapper.selectList(pmCmdSmallPlt);
    }

    @Override
    public PmCmdSmallPlt selectByPrimary(Long pmCmdSmallPltId) {
        PmCmdSmallPlt pmCmdSmallPlt = new PmCmdSmallPlt();
        pmCmdSmallPlt.setPmCmdSmallPltId(pmCmdSmallPltId);
        List<PmCmdSmallPlt> pmCmdSmallPlts = pmCmdSmallPltMapper.selectList(pmCmdSmallPlt);
        if (CollectionUtils.isEmpty(pmCmdSmallPlts)) {
            return null;
        }
        return pmCmdSmallPlts.get(0);
    }

    @Override
    public List<PmCmdSmallPlt> selectByPonoSlabList(List<String> ponoSlabList, Long tenantId) {
        if (CollectionUtils.isEmpty(ponoSlabList)) {
            return Collections.emptyList();
        }
        // ponoSlabList 按1000拆分成多个list
        List<List<String>> ponoSlabListList = Lists.partition(ponoSlabList, 1000);
        List<PmCmdSmallPlt> pmCmdSmallPltList = new ArrayList<>(ponoSlabList.size());
        for (List<String> slabList : ponoSlabListList) {
            pmCmdSmallPltList.addAll(this.selectByCondition(Condition.builder(PmCmdSmallPlt.class)
                    .andWhere(Sqls.custom().andIn(PmCmdSmallPlt.FIELD_PONO_SLAB_NO, slabList))
                    .andWhere(Sqls.custom().andEqualTo(PmCmdSmallPlt.FIELD_TENANT_ID, tenantId)).build()));
        }
        return pmCmdSmallPltList;
    }

    @Override
    public List<PmCmdSmallPlt> selectByTmpSlabNoList(List<String> tmpSlabNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmCmdSmallPlt.class)
                .andWhere(Sqls.custom().andIn(PmCmdSmallPlt.FIELD_TMP_SLAB_NO, tmpSlabNoList))
                .andWhere(Sqls.custom().andEqualTo(PmCmdSmallPlt.FIELD_TENANT_ID, tenantId))
                .build());
    }

    @Override
    public List<PmCmdSmallPlt> selectByPonoAndPlateNo(List<String> ponoSlabNoList, List<String> plateNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmCmdSmallPlt.class)
                .andWhere(Sqls.custom().andIn(PmCmdSmallPlt.FIELD_PONO_SLAB_NO, ponoSlabNoList))
                .andWhere(Sqls.custom().andIn(PmCmdSmallPlt.FIELD_PLATE_NO, plateNoList))
                .andWhere(Sqls.custom().andEqualTo(PmCmdSmallPlt.FIELD_TENANT_ID, tenantId))
                .build());
    }
}
