package cn.iocoder.yudao.module.erp.dal.mysql.product;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * ERP 产品 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ErpProductMapper extends BaseMapperX<ErpProductDO> {

    default PageResult<ErpProductDO> selectPage(ErpProductPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpProductDO>()
                .likeIfPresent(ErpProductDO::getName, reqVO.getName())
                .eqIfPresent(ErpProductDO::getCategoryId, reqVO.getCategoryId())
                .betweenIfPresent(ErpProductDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ErpProductDO::getId));
    }

    default PageResult<ErpProductDO> selectProductCodeExist(ErpProductPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpProductDO>()
                .eqIfPresent(ErpProductDO::getName, reqVO.getName())
                .eqIfPresent(ErpProductDO::getBarCode, reqVO.getCode())
                .orderByAsc(ErpProductDO::getId));
    }

    default Boolean selectProductExist(ErpProductPageReqVO reqVO) {
        LambdaQueryWrapperX<ErpProductDO> queryWrapper = (LambdaQueryWrapperX<ErpProductDO>) new LambdaQueryWrapperX<ErpProductDO>()
                .orderByAsc(ErpProductDO::getId);
        if (StringUtils.hasText(reqVO.getName()) && StringUtils.hasText(reqVO.getStandard())) {
            // 组合查询：name 和 standard 必须同时匹配
            queryWrapper.eq(ErpProductDO::getName, reqVO.getName())
                    .eq(ErpProductDO::getStandard, reqVO.getStandard());
            return !selectPage(reqVO, queryWrapper).getList().isEmpty();
        }
        return false;
    }

    default Long selectCountByCategoryId(Long categoryId) {
        return selectCount(ErpProductDO::getCategoryId, categoryId);
    }

    default Long selectCountByUnitId(Long unitId) {
        return selectCount(ErpProductDO::getUnitId, unitId);
    }

    default List<ErpProductDO> selectListByStatus(Integer status) {
        return selectList(ErpProductDO::getStatus, status);
    }
    default List<ErpProductDO> selectListByCategoryId(Integer categoryId) {
        return selectList(ErpProductDO::getStatus, CommonStatusEnum.ENABLE.getStatus(),
                ErpProductDO::getCategoryId, categoryId);
    }
    /**查询不同类型的产品**/
    default List<ErpProductDO> selectByCategorys(List<Integer> categoryIds){
        return selectList(new QueryWrapper<ErpProductDO>()
                .eq("status", 0)
                .in("category_id" ,categoryIds));
    }

    default List<ErpProductDO> selectListByNameIn(Collection<String> names) {
        return selectList(new LambdaQueryWrapperX<ErpProductDO>().in(ErpProductDO::getName, names));
    }


}