package com.seer.teach.mall.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 com.seer.teach.common.PageListBean;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mall.entity.CategoryEntity;
import com.seer.teach.mall.response.CategoryResp;
import com.seer.teach.mall.mapper.CategoryMapper;
import com.seer.teach.mall.service.ICategoryService;
import com.seer.teach.mall.convert.CategoryConvert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  商品类别服务实现类
 * </p>
 *
 * @author Captain
 * @since 2025-06-10
 */
@Slf4j
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements ICategoryService {


    @Override
    public PageListBean<CategoryResp> queryPageList(Integer pageNum, Integer pageSize,Integer showIndex, Integer isShow) {
        log.info("查询商品类别列表");
        IPage<CategoryEntity> pageParams = new Page(pageNum, pageSize);
        IPage<CategoryEntity> iPage = super.page(pageParams, new LambdaQueryWrapper<>(CategoryEntity.class)
                .eq(Objects.nonNull(showIndex),CategoryEntity::getShowIndex, 1)
                .eq(Objects.nonNull(isShow),CategoryEntity::getIsShow, 1)
                .orderByAsc(CategoryEntity::getSortOrder));
        return PageConverterUtils.convertPageListBean(iPage, CategoryConvert.INSTANCE::list);
    }

    @Override
    public boolean updateIsShowStatus(Integer id, Integer isShow) {
        return update(new LambdaUpdateWrapper<>(CategoryEntity.class).set(CategoryEntity::getIsShow, isShow).eq(CategoryEntity::getId, id));
    }

    @Override
    public boolean updateShowIndexStatus(Integer id, Integer showIndex) {
        return  update(new LambdaUpdateWrapper<>(CategoryEntity.class).set(CategoryEntity::getShowIndex, showIndex).eq(CategoryEntity::getId, id));
    }
}