package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.entity.PointsProduct;
import com.zhentao.service.PointsProductService;
import com.zhentao.mapper.PointsProductMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
* @author 联想
* @description 针对表【points_product】的数据库操作Service实现
* @createDate 2025-12-11 10:49:42
*/
@Slf4j
@Service
public class PointsProductServiceImpl extends ServiceImpl<PointsProductMapper, PointsProduct>
    implements PointsProductService{
    
    @Override
    public Page<PointsProduct> getProductList(Integer page, Integer size, String keyword, Integer status, Integer category) {
        Page<PointsProduct> pageParam = new Page<>(page, size);
        QueryWrapper<PointsProduct> wrapper = new QueryWrapper<>();
        
        // 关键词搜索（商品名称、描述）
        if (StringUtils.hasText(keyword)) {
            wrapper.and(w -> w.like("name", keyword).or().like("description", keyword));
        }
        
        // 状态筛选（0-下架 1-上架）
        if (status != null) {
            wrapper.eq("status", status);
        }
        
        // 分类筛选（1-实物 2-虚拟）
        if (category != null) {
            wrapper.eq("category", category);
        }
        
        // 按排序字段和创建时间排序
        wrapper.orderByAsc("sort_order").orderByDesc("create_time");
        
        return this.page(pageParam, wrapper);
    }
    
    @Override
    public PointsProduct getProductById(Long id) {
        return this.getById(id);
    }
    
    @Override
    public boolean createProduct(PointsProduct product) {
        try {
            // 设置默认值
            if (product.getStatus() == null) {
                product.setStatus(0); // 默认下架
            }
            if (product.getIsRecommend() == null) {
                product.setIsRecommend(0); // 默认不推荐
            }
            if (product.getSortOrder() == null) {
                product.setSortOrder(0);
            }
            if (product.getStock() == null) {
                product.setStock(0);
            }
            
            Date now = new Date();
            product.setCreateTime(now);
            product.setUpdateTime(now);
            
            return this.save(product);
        } catch (Exception e) {
            log.error("创建积分商品失败", e);
            return false;
        }
    }
    
    @Override
    public boolean updateProduct(PointsProduct product) {
        try {
            product.setUpdateTime(new Date());
            return this.updateById(product);
        } catch (Exception e) {
            log.error("更新积分商品失败", e);
            return false;
        }
    }
    
    @Override
    public boolean deleteProduct(Long id) {
        try {
            return this.removeById(id);
        } catch (Exception e) {
            log.error("删除积分商品失败", e);
            return false;
        }
    }
    
    @Override
    public boolean updateStatus(Long id, Integer status) {
        try {
            PointsProduct product = new PointsProduct();
            product.setId(id);
            product.setStatus(status);
            product.setUpdateTime(new Date());
            
            return this.updateById(product);
        } catch (Exception e) {
            log.error("更新积分商品状态失败", e);
            return false;
        }
    }
}




