package com.zt.plat.module.base.dal.dao.materialproperties;

import cn.hutool.core.util.StrUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesPageReqVO;
import com.zt.plat.module.base.controller.admin.materialproperties.vo.MaterialPropertiesSimplePageReqVO;
import com.zt.plat.module.base.dal.dataobject.materialproperties.MaterialPropertiesDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 物料属性 Mapper
 *
 * @author 后台管理-1
 */
@Mapper
public interface MaterialPropertiesMapper extends BaseMapperX<MaterialPropertiesDO> {

    default PageResult<MaterialPropertiesDO> selectPage(MaterialPropertiesPageReqVO reqVO) {
    LambdaQueryWrapperX<MaterialPropertiesDO> query = new LambdaQueryWrapperX<MaterialPropertiesDO>()
        .eqIfPresent(MaterialPropertiesDO::getCode, reqVO.getCode())
        .likeIfPresent(MaterialPropertiesDO::getName, reqVO.getName())
        .eqIfPresent(MaterialPropertiesDO::getUnitQuantityId, reqVO.getUnitQuantityId())
        .eqIfPresent(MaterialPropertiesDO::getDictionaryDataValue, reqVO.getDictionaryDataValue())
        .eqIfPresent(MaterialPropertiesDO::getDataType, reqVO.getDataType())
        .eqIfPresent(MaterialPropertiesDO::getRemark, reqVO.getRemark())
        .betweenIfPresent(MaterialPropertiesDO::getCreateTime, reqVO.getCreateTime());
        if (reqVO.getDeptId() == null) {
            query.isNull(MaterialPropertiesDO::getDeptId);
        } else {
            query.eq(MaterialPropertiesDO::getDeptId, reqVO.getDeptId());
        }
        if (StrUtil.isNotBlank(reqVO.getKeyword())) {
            query.and(w -> w.like(MaterialPropertiesDO::getCode, reqVO.getKeyword())
                    .or()
                    .like(MaterialPropertiesDO::getName, reqVO.getKeyword()));
        }
        query.orderByDesc(MaterialPropertiesDO::getId);
        return selectPage(reqVO, query);
    }

    default PageResult<MaterialPropertiesDO> selectSimplePage(MaterialPropertiesSimplePageReqVO reqVO) {
        LambdaQueryWrapperX<MaterialPropertiesDO> query = new LambdaQueryWrapperX<MaterialPropertiesDO>()
        .eqIfPresent(MaterialPropertiesDO::getDictionaryDataValue, reqVO.getDictionaryDataValue());
        if (StrUtil.isNotBlank(reqVO.getKeyword())) {
            query.and(w -> w.like(MaterialPropertiesDO::getCode, reqVO.getKeyword())
                    .or()
                    .like(MaterialPropertiesDO::getName, reqVO.getKeyword()));
        }
        if (reqVO.getDeptId() == null) {
            query.isNull(MaterialPropertiesDO::getDeptId);
        } else {
            query.eq(MaterialPropertiesDO::getDeptId, reqVO.getDeptId());
        }
        query.orderByAsc(MaterialPropertiesDO::getCode);
        return selectPage(reqVO, query);
    }

    default List<MaterialPropertiesDO> selectByCodes(Collection<String> codes) {
        if (codes == null || codes.isEmpty()) {
            return Collections.emptyList();
        }
        return selectList(new LambdaQueryWrapperX<MaterialPropertiesDO>()
                .in(MaterialPropertiesDO::getCode, codes));
    }

}