package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.entity.VipPackage;
import com.zhentao.mapper.VipPackageMapper;
import com.zhentao.service.VipPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * VIP套餐服务实现类
 */
@Slf4j
@Service
public class VipPackageServiceImpl implements VipPackageService {
    
    private final VipPackageMapper vipPackageMapper;
    
    public VipPackageServiceImpl(VipPackageMapper vipPackageMapper) {
        this.vipPackageMapper = vipPackageMapper;
    }
    
    @Override
    public Page<VipPackage> getPackageList(Integer page, Integer size, String keyword, Integer status) {
        Page<VipPackage> pageParam = new Page<>(page, size);
        QueryWrapper<VipPackage> wrapper = new QueryWrapper<>();
        
        // 关键词搜索
        if (StringUtils.hasText(keyword)) {
            wrapper.like("package_name", keyword);
        }
        
        // 状态筛选
        if (status != null) {
            wrapper.eq("status", status);
        }
        
        // 按排序字段和创建时间排序
        wrapper.orderByAsc("sort_order").orderByDesc("create_time");
        
        return vipPackageMapper.selectPage(pageParam, wrapper);
    }
    
    @Override
    public List<VipPackage> getAllActivePackages() {
        QueryWrapper<VipPackage> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 1);
        wrapper.orderByAsc("sort_order");
        return vipPackageMapper.selectList(wrapper);
    }
    
    @Override
    public VipPackage getPackageById(Long id) {
        return vipPackageMapper.selectById(id);
    }
    
    @Override
    public boolean createPackage(VipPackage vipPackage) {
        try {
            if (vipPackage.getStatus() == null) {
                vipPackage.setStatus(1);
            }
            if (vipPackage.getIsRecommend() == null) {
                vipPackage.setIsRecommend(0);
            }
            if (vipPackage.getSortOrder() == null) {
                vipPackage.setSortOrder(0);
            }
            
            vipPackage.setCreateTime(LocalDateTime.now());
            vipPackage.setUpdateTime(LocalDateTime.now());
            
            return vipPackageMapper.insert(vipPackage) > 0;
        } catch (Exception e) {
            log.error("创建VIP套餐失败", e);
            return false;
        }
    }
    
    @Override
    public boolean updatePackage(VipPackage vipPackage) {
        try {
            vipPackage.setUpdateTime(LocalDateTime.now());
            return vipPackageMapper.updateById(vipPackage) > 0;
        } catch (Exception e) {
            log.error("更新VIP套餐失败", e);
            return false;
        }
    }
    
    @Override
    public boolean deletePackage(Long id) {
        try {
            return vipPackageMapper.deleteById(id) > 0;
        } catch (Exception e) {
            log.error("删除VIP套餐失败", e);
            return false;
        }
    }
    
    @Override
    public boolean batchDeletePackage(List<Long> ids) {
        try {
            return vipPackageMapper.deleteBatchIds(ids) > 0;
        } catch (Exception e) {
            log.error("批量删除VIP套餐失败", e);
            return false;
        }
    }
    
    @Override
    public boolean updateStatus(Long id, Integer status) {
        try {
            VipPackage vipPackage = new VipPackage();
            vipPackage.setPackageId(id);
            vipPackage.setStatus(status);
            vipPackage.setUpdateTime(LocalDateTime.now());
            
            return vipPackageMapper.updateById(vipPackage) > 0;
        } catch (Exception e) {
            log.error("更新VIP套餐状态失败", e);
            return false;
        }
    }
}

