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

import com.xisc.pm.api.dto.PmPdSlabSplitDTO;
import com.xisc.pm.domain.entity.PmPdSlab;
import com.xisc.pm.domain.repository.PmPdSlabRepository;
import com.xisc.pm.infra.mapper.PmPdSlabMapper;
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.List;

/**
 * 组板板坯表(PmPdSlab)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-07-31 16:30:31
 */
@Component
public class PmPdSlabRepositoryImpl extends BaseRepositoryImpl<PmPdSlab> implements PmPdSlabRepository {
    @Resource
    private PmPdSlabMapper pmPdSlabMapper;

    @Override
    public List<PmPdSlab> selectList(PmPdSlab pmPdSlab) {
        return pmPdSlabMapper.selectList(pmPdSlab);
    }

    @Override
    public PmPdSlab selectByPrimary(Long pdSlabId) {
        PmPdSlab pmPdSlab = new PmPdSlab();
        pmPdSlab.setPmPdSlabId(pdSlabId);
        List<PmPdSlab> pmPdSlabs = pmPdSlabMapper.selectList(pmPdSlab);
        if (CollectionUtils.isEmpty(pmPdSlabs)) {
            return null;
        }
        return pmPdSlabs.get(0);
    }

    @Override
    public PmPdSlabSplitDTO selectMmSlabBySlabId(Long slabId, Long organizationId) {
        return pmPdSlabMapper.selectMmSlabBySlabId(slabId, organizationId);
    }
    
    @Override
    public List<PmPdSlabSplitDTO> selectMmSlabBySlabNoList(List<String> slabNoList, Long organizationId) {
        return this.pmPdSlabMapper.selectMmSlabBySlabNoList(slabNoList, organizationId);
    }

    @Override
    public List<PmPdSlab> selectListBySlabNo(String slabNo, Long organizationId) {
        return this
                .selectByCondition(Condition.builder(PmPdSlab.class)
                        .andWhere(Sqls.custom().andEqualTo(PmPdSlab.FIELD_TENANT_ID, organizationId)
                                .andEqualTo(PmPdSlab.FIELD_SLAB_NO, slabNo))
                        .orderByAsc(PmPdSlab.FIELD_SLAB_DIV_NO).build());
    }

    @Override
    public PmPdSlab selectByVirtualSlabNo(String virtualSlabNo, Long organizationId) {
        List<PmPdSlab> pmPdSlabs = this
                .selectByCondition(Condition.builder(PmPdSlab.class)
                        .andWhere(Sqls.custom().andEqualTo(PmPdSlab.FIELD_TENANT_ID, organizationId)
                                .andEqualTo(PmPdSlab.FIELD_VIRTUAL_SLAB_NO, virtualSlabNo))
                        .orderByAsc(PmPdSlab.FIELD_SLAB_DIV_NO).build());
        if(pmPdSlabs != null && !pmPdSlabs.isEmpty()) {
            return pmPdSlabs.get(0);
        }
        return null;
    }

    @Override
    public List<PmPdSlab> selectByVirtualSlabNoList(List<String> virtualSlabNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmPdSlab.class)
                .andWhere(Sqls.custom().andEqualTo(PmPdSlab.FIELD_TENANT_ID, tenantId)
                        .andIn(PmPdSlab.FIELD_VIRTUAL_SLAB_NO, virtualSlabNoList)).build());
    }

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