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

import com.google.common.collect.Lists;
import com.xisc.pm.domain.entity.PmCmdLargePlt;
import com.xisc.pm.domain.repository.PmCmdLargePltRepository;
import com.xisc.pm.infra.mapper.PmCmdLargePltMapper;
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;

/**
 * 命令大板表(PmCmdLargePlt)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-08-01 10:24:39
 */
@Component
public class PmCmdLargePltRepositoryImpl extends BaseRepositoryImpl<PmCmdLargePlt> implements PmCmdLargePltRepository {
    @Resource
    private PmCmdLargePltMapper pmCmdLargePltMapper;

    @Override
    public List<PmCmdLargePlt> selectList(PmCmdLargePlt pmCmdLargePlt) {
        return pmCmdLargePltMapper.selectList(pmCmdLargePlt);
    }

    @Override
    public PmCmdLargePlt selectByPrimary(Long pmCmdLargePltId) {
        PmCmdLargePlt pmCmdLargePlt = new PmCmdLargePlt();
        pmCmdLargePlt.setPmCmdLargePltId(pmCmdLargePltId);
        List<PmCmdLargePlt> pmCmdLargePlts = pmCmdLargePltMapper.selectList(pmCmdLargePlt);
        if (CollectionUtils.isEmpty(pmCmdLargePlts)) {
            return null;
        }
        return pmCmdLargePlts.get(0);
    }

    @Override
    public List<PmCmdLargePlt> 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<PmCmdLargePlt> pmCmdLargePltList = new ArrayList<>(ponoSlabList.size());
        //Oracle
        for (List<String> slabList : ponoSlabListList) {
            pmCmdLargePltList.addAll(this.selectByCondition(Condition.builder(PmCmdLargePlt.class)
                    .andWhere(Sqls.custom().andIn(PmCmdLargePlt.FIELD_PONO_SLAB_NO, slabList))
                    .andWhere(Sqls.custom().andEqualTo(PmCmdLargePlt.FIELD_TENANT_ID, tenantId))
                    .build()));
        }
        return pmCmdLargePltList;
    }

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

    @Override
    public List<PmCmdLargePlt> selectBySlabNoList(List<String> slabNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmCmdLargePlt.class)
                .andWhere(Sqls.custom().andIn(PmCmdLargePlt.FIELD_SLAB_NO, slabNoList))
                .andWhere(Sqls.custom().andEqualTo(PmCmdLargePlt.FIELD_TENANT_ID, tenantId))
                .build());
    }

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