package com.corewing.app.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.corewing.app.dto.website.ProductCategoryRequest;
import com.corewing.app.dto.website.ProductPageRequest;
import com.corewing.app.entity.BizProduct;
import com.corewing.app.entity.BizProductCategory;
import com.corewing.app.mapper.BizProductCategoryMapper;
import com.corewing.app.service.BizProductCategoryService;
import com.corewing.app.service.BizProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BizProductCategroyServiceImpl extends ServiceImpl<BizProductCategoryMapper, BizProductCategory> implements BizProductCategoryService {

    @Resource
    private BizProductCategoryMapper bizProductCategoryMapper;

    @Resource
    private BizProductService bizProductService;

    @Override
    public List<BizProductCategory> list(ProductCategoryRequest productCategoryRequest) {
        LambdaQueryWrapper<BizProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(productCategoryRequest.getSearchKey()), BizProductCategory::getCategoryTitle, productCategoryRequest.getSearchKey());
        List<BizProductCategory> bizProductCategories = bizProductCategoryMapper.selectList(queryWrapper);

        bizProductCategories.forEach(item -> {
            ProductPageRequest  productPageRequest = new ProductPageRequest();
            productPageRequest.setCategoryId(item.getId());
            Page<BizProduct> page = bizProductService.page(productPageRequest);
            item.setProductPage(page);
        });

        return bizProductCategories;
    }
}
