package org.jeecg.modules.oroqen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.oroqen.entity.OroqenProduct;
import org.jeecg.modules.oroqen.mapper.OroqenProductMapper;
import org.jeecg.modules.oroqen.service.IOroqenProductService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 产品
 * @Author: jeecg-boot
 * @Date: 2025-01-08
 * @Version: V1.0
 */
@Service
public class OroqenProductServiceImpl extends ServiceImpl<OroqenProductMapper, OroqenProduct> implements IOroqenProductService {

    @Override
    public List<OroqenProduct> getProductsByCategoryId(String categoryId) {
        return this.baseMapper.selectByCategoryId(categoryId);
    }

    @Override
    public List<OroqenProduct> getFeaturedProducts(Integer limit) {
        return this.baseMapper.selectFeaturedProducts(limit);
    }

    @Override
    public List<OroqenProduct> getHotProducts(Integer limit) {
        return this.baseMapper.selectHotProducts(limit);
    }

    @Override
    public boolean checkProductCodeExists(String productCode, String excludeId) {
        QueryWrapper<OroqenProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_code", productCode);
        if (excludeId != null) {
            queryWrapper.ne("id", excludeId);
        }
        return this.count(queryWrapper) > 0;
    }

    @Override
    public boolean updateSalesCount(String productId, Integer quantity) {
        return this.baseMapper.updateSalesCount(productId, quantity) > 0;
    }

    @Override
    public boolean updateStock(String productId, Integer quantity) {
        return this.baseMapper.updateStock(productId, quantity) > 0;
    }

    @Override
    public boolean checkStock(String productId, Integer quantity) {
        OroqenProduct product = this.getById(productId);
        if (product == null) {
            return false;
        }
        return product.getStock() != null && product.getStock() >= quantity;
    }
}