package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.StoreProductDelicacies;
import shop.alien.mapper.StoreProductDelicaciesMapper;
import shop.alien.store.service.StoreProductDelicaciesService;

import java.util.List;

/**
 * 美食商品表 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class StoreProductDelicaciesServiceImpl extends ServiceImpl<StoreProductDelicaciesMapper, StoreProductDelicacies> implements StoreProductDelicaciesService {

    @Override
    public IPage<StoreProductDelicacies> getPage(int pageNum, int pageSize, String name, String category, String extGroup, Integer status, Integer extId) {
        LambdaQueryWrapper<StoreProductDelicacies> queryWrapper = new LambdaQueryWrapper<>();

        // 名称模糊查询
        if (StringUtils.hasText(name)) {
            queryWrapper.like(StoreProductDelicacies::getName, name);
        }

        // 类别查询
        if (StringUtils.hasText(category)) {
            queryWrapper.eq(StoreProductDelicacies::getCategory, category);
        }

        // 菜品分组查询
        if (StringUtils.hasText(extGroup)) {
            queryWrapper.eq(StoreProductDelicacies::getExtGroup, extGroup);
        }

        // 状态查询
        if (status != null) {
            queryWrapper.eq(StoreProductDelicacies::getStatus, status);
        }

        // 商品表主键查询
        if (extId != null) {
            queryWrapper.eq(StoreProductDelicacies::getExtId, extId);
        }

        // 按创建时间倒序
        queryWrapper.orderByDesc(StoreProductDelicacies::getCreatedTime);

        return baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    @Override
    public StoreProductDelicacies getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean saveDelicacies(StoreProductDelicacies delicacies) {
        // 设置默认值
        if (delicacies.getUnit() == null || delicacies.getUnit().isEmpty()) {
            delicacies.setUnit("份");
        }
        if (delicacies.getQuantity() == null) {
            delicacies.setQuantity(0);
        }
        if (delicacies.getStatus() == null) {
            delicacies.setStatus(1);
        }
        if (delicacies.getCreatedUserId() == null) {
            delicacies.setCreatedUserId(0);
        }
        if (delicacies.getUpdatedUserId() == null) {
            delicacies.setUpdatedUserId(0);
        }

        return baseMapper.insert(delicacies) > 0;
    }

    @Override
    public boolean updateDelicacies(StoreProductDelicacies delicacies) {
        if (delicacies.getId() == null) {
            log.error("更新美食商品失败：ID不能为空");
            return false;
        }

        // 设置更新人
        if (delicacies.getUpdatedUserId() == null) {
            delicacies.setUpdatedUserId(0);
        }

        return baseMapper.updateById(delicacies) > 0;
    }

    @Override
    public boolean deleteDelicacies(Integer id) {
        // 逻辑删除，MyBatis-Plus会自动处理
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteBatch(List<Integer> ids) {
        if (ids == null || ids.isEmpty()) {
            log.error("批量删除美食商品失败：ID列表不能为空");
            return false;
        }
        // 逻辑删除，MyBatis-Plus会自动处理
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public boolean updateStatus(Integer id, Integer status) {
        if (id == null) {
            log.error("更新美食商品状态失败：ID不能为空");
            return false;
        }
        if (status == null || (status != 0 && status != 1)) {
            log.error("更新美食商品状态失败：状态值无效，只能为0或1");
            return false;
        }

        LambdaUpdateWrapper<StoreProductDelicacies> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreProductDelicacies::getId, id)
                .set(StoreProductDelicacies::getStatus, status);

        return baseMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public List<StoreProductDelicacies> getListByExtId(Integer extId) {
        if (extId == null) {
            return null;
        }
        LambdaQueryWrapper<StoreProductDelicacies> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreProductDelicacies::getExtId, extId)
                .orderByDesc(StoreProductDelicacies::getCreatedTime);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<StoreProductDelicacies> getListByCategory(String category) {
        if (!StringUtils.hasText(category)) {
            return null;
        }
        LambdaQueryWrapper<StoreProductDelicacies> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreProductDelicacies::getCategory, category)
                .orderByDesc(StoreProductDelicacies::getCreatedTime);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<StoreProductDelicacies> getListByExtGroup(String extGroup) {
        if (!StringUtils.hasText(extGroup)) {
            return null;
        }
        LambdaQueryWrapper<StoreProductDelicacies> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StoreProductDelicacies::getExtGroup, extGroup)
                .orderByDesc(StoreProductDelicacies::getCreatedTime);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<StoreProductDelicacies> getByExtId(Integer id) {
        return this.lambdaQuery().eq(StoreProductDelicacies::getExtId, id).list();
    }
}

