package org.springblade.modules.api.controller;

/**
 * Author pangqijun
 * Date 2023/2/13
 * Description
 */

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.constant.CommonConstant;
import org.springblade.common.enums.*;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.request.GoodsListRequest;
import org.springblade.modules.mall.entity.CategoryInfo;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.service.ICategoryInfoService;
import org.springblade.modules.mall.service.IGoodsInfoService;
import org.springblade.modules.mall.service.IGoodsParamsService;
import org.springblade.modules.mall.vo.GoodsInfoVO;
import org.springblade.modules.mall.wrapper.GoodsInfoWrapper;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

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

/**
 * Author pangqijun
 * Date 2023/2/13
 * Description
 */
@RestController
@RequestMapping(CommonConstant.API_URL)
@Api(value = "商城模块接口", tags = "05.商城模块接口")
public class ApiGoodsController extends BladeController {

    @Resource
    private ICategoryInfoService categoryInfoService;
    @Resource
    private IGoodsInfoService goodsInfoService;

    @GetMapping("/category/list")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "查询分类列表", notes = "查询根分类，parentId传 0 ")
    public R<List<CategoryInfo>> list(@ApiParam(value = "父分类Id，不传默认为0", defaultValue = "0")
                                      @RequestParam(value = "parentId", required = false) Long parentId) {
        parentId = parentId == null ? 0L : parentId;
        return R.data(categoryInfoService.lambdaQuery().eq(CategoryInfo::getState, YesOrNoEnum.YES.getValue()).eq(CategoryInfo::getParentId, parentId).orderByAsc(CategoryInfo::getSort).list());
    }


    @GetMapping("/goods/page")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "商品分页接口", notes = "传入goodsInfo")
    public R<List<GoodsInfoVO>> list(GoodsListRequest request, Query query) {
        LambdaQueryWrapper<GoodsInfo> wrapper = Wrappers.lambdaQuery(GoodsInfo.class);
        if (StrUtil.isNotBlank(request.getGoodsCategoryId())) {
            CategoryInfo categoryInfo = categoryInfoService.getById(request.getGoodsCategoryId());
            Assert.notNull(categoryInfo, "分类不存在");
            wrapper.likeRight(GoodsInfo::getGoodsCategoryId, request.getGoodsCategoryId());
        }
        if (StrUtil.isNotBlank(request.getGoodsName())) {
            wrapper.like(GoodsInfo::getGoodsName, request.getGoodsName());
        }
        wrapper.orderByAsc(GoodsInfo::getSort);
        wrapper.eq(GoodsInfo::getIsSale, GoodsStateEnum.ON_SALE.getValue());
        wrapper.eq(GoodsInfo::getAuditStatus, AuditStatusEnum.PASS.getValue());
//        wrapper.ge(GoodsInfo::getTotalStock, 1);
        IPage<GoodsInfo> pages = goodsInfoService.page(Condition.getPage(query), wrapper);
        List<GoodsInfoVO> goodsInfoVOS = GoodsInfoWrapper.build().listVO(pages.getRecords());
        for (GoodsInfoVO goodsInfoVO : goodsInfoVOS) {
            goodsInfoVO.setDiscountPrice(goodsInfoVO.getSalePrice());
        }
        return R.data(goodsInfoVOS);
    }

}
