package com.tashow.cloud.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tashow.cloud.common.exception.ErrorCode;
import com.tashow.cloud.productapi.api.product.dto.CategoryDO;
import com.tashow.cloud.productapi.api.product.dto.ProdPropValueDO;
import com.tashow.cloud.product.mapper.CategoryMapper;
import com.tashow.cloud.product.mapper.ProdPropValueMapper;
import com.tashow.cloud.product.service.CategoryService;
import com.tashow.cloud.productapi.api.product.dto.CategoryDto;
import com.tashow.cloud.productapi.api.product.vo.CategoryPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.CategorySaveReqVO;
import com.tashow.cloud.productapi.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.util.object.BeanUtils;


import static com.tashow.cloud.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 产品类目 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO>  implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDO> categoryList(Integer grade, Long categoryId,String categoryName, Integer status) {
        List<CategoryDO> categoryMenuList =categoryMapper.selectCategoryList(grade, categoryId,categoryName, status,1L);
        return categoryMenuList;
    }

    @Override
    public Long createCategory(CategorySaveReqVO createReqVO) {
        // 插入
        CategoryDO category = BeanUtils.toBean(createReqVO, CategoryDO.class);
        category.setShopId(1L);
        categoryMapper.insert(category);
        // 返回
        return category.getCategoryId();
    }

    @Override
    public void updateCategory(CategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCategoryExists(updateReqVO.getCategoryId());
        // 更新
        CategoryDO updateObj = BeanUtils.toBean(updateReqVO, CategoryDO.class);
        categoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategory(Long id) {
        // 校验存在
        validateCategoryExists(id);
        // 删除
        categoryMapper.deleteById(id);
    }

    private void validateCategoryExists(Long id) {
        if (categoryMapper.selectById(id) == null) {
            throw exception(ErrorCodeConstants.CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public CategoryDO getCategory(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public PageResult<CategoryDO> getCategoryPage(CategoryPageReqVO pageReqVO) {
        return null;
    }


}