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

import com.xisc.pm.domain.entity.PmCompositeOrderRel;
import com.xisc.pm.domain.repository.PmCompositeOrderRelRepository;
import com.xisc.pm.infra.mapper.PmCompositeOrderRelMapper;
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;

/**
 * 复合板成品合同与基材合同对应关系表(PmCompositeOrderRel)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-09-25 09:20:38
 */
@Component
public class PmCompositeOrderRelRepositoryImpl extends BaseRepositoryImpl<PmCompositeOrderRel> implements PmCompositeOrderRelRepository {
    @Resource
    private PmCompositeOrderRelMapper pmCompositeOrderRelMapper;

    @Override
    public List<PmCompositeOrderRel> selectList(PmCompositeOrderRel pmCompositeOrderRel) {
        return pmCompositeOrderRelMapper.selectList(pmCompositeOrderRel);
    }

    @Override
    public PmCompositeOrderRel selectByPrimary(Long relId) {
        PmCompositeOrderRel pmCompositeOrderRel = new PmCompositeOrderRel();
        pmCompositeOrderRel.setRelId(relId);
        List<PmCompositeOrderRel> pmCompositeOrderRels = pmCompositeOrderRelMapper.selectList(pmCompositeOrderRel);
        if (CollectionUtils.isEmpty(pmCompositeOrderRels)) {
            return null;
        }
        return pmCompositeOrderRels.get(0);
    }

    @Override
    public List<PmCompositeOrderRel>  selectByOrderList(List<String> orderNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmCompositeOrderRel.class)
                        .andWhere(Sqls.custom().andIn(PmCompositeOrderRel.FIELD_BASE_ORDER_NO, orderNoList))
                        .andWhere(Sqls.custom().andEqualTo(PmCompositeOrderRel.FIELD_TENANT_ID, tenantId))
                .build());
    }

    @Override
    public List<PmCompositeOrderRel> selectByProductAndOrderList(String productOrderNo, List<String> orderNoList, Long tenantId) {
        return this.selectByCondition(Condition.builder(PmCompositeOrderRel.class)
                .andWhere(Sqls.custom().andIn(PmCompositeOrderRel.FIELD_BASE_ORDER_NO, orderNoList))
                .andWhere(Sqls.custom().andEqualTo(PmCompositeOrderRel.FIELD_PRODUCT_ORDER_NO, productOrderNo))
                .andWhere(Sqls.custom().andEqualTo(PmCompositeOrderRel.FIELD_TENANT_ID, tenantId))
                .build());
    }
}
