package com.zt.plat.module.qms.business.config.dal.mapper;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.framework.tenant.core.context.TenantContextHolder;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.module.qms.business.config.controller.vo.*;
import com.zt.plat.module.qms.business.config.dal.dataobject.BaseSampleDO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigEntrustSourceSampleTypeDO;
import com.zt.plat.module.qms.enums.QmsCommonConstant;

import cn.hutool.core.util.ObjectUtil;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 样品大类管理 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface BaseSampleMapper extends BaseMapperX<BaseSampleDO> {

    default PageResult<BaseSampleDO> selectPage(BaseSamplePageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<BaseSampleDO>()
                .likeIfPresent(BaseSampleDO::getName, reqVO.getName())
                .eqIfPresent(BaseSampleDO::getMaterialId, reqVO.getMaterialId())
                .eqIfPresent(BaseSampleDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(BaseSampleDO::getMaterialName, reqVO.getMaterialName())
                .eqIfPresent(BaseSampleDO::getDictionaryBusinessId, reqVO.getDictionaryBusinessId())
                .eqIfPresent(BaseSampleDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .betweenIfPresent(BaseSampleDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(BaseSampleDO::getRemark, reqVO.getRemark())
                .orderByDesc(BaseSampleDO::getId));
    }
    
    default List<BaseSampleRespVO> selectList(BaseSamplePageReqVO reqVO) {
    	LambdaQueryWrapperX<BaseSampleDO> queryWrapperX = new LambdaQueryWrapperX<BaseSampleDO>()
        .likeIfPresent(BaseSampleDO::getName, reqVO.getName())
        .eqIfPresent(BaseSampleDO::getMaterialId, reqVO.getMaterialId())
        .eqIfPresent(BaseSampleDO::getMaterialCode, reqVO.getMaterialCode())
        .likeIfPresent(BaseSampleDO::getMaterialName, reqVO.getMaterialName())
        .eqIfPresent(BaseSampleDO::getDictionaryBusinessId, reqVO.getDictionaryBusinessId())
        .eqIfPresent(BaseSampleDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
        .betweenIfPresent(BaseSampleDO::getCreateTime, reqVO.getCreateTime())
        .eqIfPresent(BaseSampleDO::getRemark, reqVO.getRemark());
    	if (ObjectUtil.isNotEmpty(reqVO.getConfigEntrustSourceId())) {
        	queryWrapperX.inSql(BaseSampleDO::getDictionaryBusinessId, "SELECT tcesst.DIC_BSN_ID FROM T_CFG_ENTT_SRC_SMP_TP tcesst WHERE tcesst.DELETED = 0 AND tcesst.CFG_ENTT_SRC_ID = " + reqVO.getConfigEntrustSourceId());
		} else if (ObjectUtil.isNotEmpty(reqVO.getConfigEntrustSourceKey())) {
			queryWrapperX.inSql(BaseSampleDO::getDictionaryBusinessId, "SELECT tcesst.DIC_BSN_ID FROM T_CFG_ENTT_SRC_SMP_TP tcesst LEFT JOIN T_CFG_ENTT_SRC tces ON tcesst.CFG_ENTT_SRC_ID = tces.ID WHERE tcesst.DELETED = 0 AND tces.DELETED = 0 AND tces.KY = '" + reqVO.getConfigEntrustSourceKey() + "'");
		}
    	
    	if (QmsCommonConstant.ENTRUST_INSPECTION.equals(reqVO.getEntrustType())) {
			queryWrapperX.inSql(BaseSampleDO::getDictionaryBusinessId, "SELECT tcesst.DIC_BSN_ID FROM T_CFG_ENTT_SRC_SMP_TP tcesst LEFT JOIN T_CFG_ENTT_SRC tces ON tcesst.CFG_ENTT_SRC_ID = tces.ID WHERE tcesst.DELETED = 0 AND tces.DELETED = 0 AND tces.ENTT_TP = '" + reqVO.getEntrustType() + "'");
		}

    	queryWrapperX.orderByDesc(BaseSampleDO::getId);
    	List<BaseSampleDO> list = selectList(queryWrapperX);
    	return BeanUtils.toBean(list, BaseSampleRespVO.class);
    }

    List<BaseSampleDO> getBaseSampleListWithAssayStandardCount();

}