package com.seer.teach.mall.app.good.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mall.entity.GoodsEntity;
import com.seer.teach.mall.convert.GoodsConvert;
import com.seer.teach.mall.request.*;
import com.seer.teach.mall.response.GoodSpuDetailResp;
import com.seer.teach.mall.app.good.service.IAppGoodsService;
import com.seer.teach.mall.app.good.service.IAppGoodsSpecificationService;
import com.seer.teach.mall.response.GoodsResp;
import com.seer.teach.mall.response.GoodsSpecificationResp;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/app/user/goods")
@RestController
@Tag(name = "APP - 商品")
@Validated
@RequiredArgsConstructor
@Slf4j
@DecryptionAnnotation
@EncryptionAnnotation
public class AppGoodsController {

    private final IAppGoodsService goodsService;

    private final IAppGoodsSpecificationService goodsSpecificationService;

    @PostMapping("/category/list")
    @SaCheckPermission("app:goods:categoryList")
    @Operation(summary = "根据商品类型查询")
    @LogPrint
    public ResultBean<PageListBean<GoodsResp>> getPageListByCategoryId(@RequestBody @Validated CategoryTypeReq params) {
        IPage<GoodsEntity> page = goodsService.getPageListByCategoryId(params);
        PageListBean<GoodsResp> pageListBean = PageConverterUtils.convertPageListBean(page, GoodsConvert.INSTANCE::convertList);
        return ResultBean.success(pageListBean);
    }

    @PostMapping("/paged")
    @SaCheckPermission("app:goods:paged")
    @Operation(summary = "商品分页查询")
    @LogPrint
    public ResultBean<PageListBean<GoodsResp>> getPageList(@RequestBody @Validated GoodSearchReq params) {
        return ResultBean.success(goodsService.getPageList(params));
    }

    @PostMapping("/goods-details")
    @SaCheckPermission("app:goods:details")
    @Operation(summary = "商品详情页查询")
    @LogPrint
    public ResultBean<GoodSpuDetailResp> getGoodsDetails(@RequestBody @Validated GoodsDetailsReq params) {
        return ResultBean.success(goodsService.getGoodsDetails(params));
    }

    @PostMapping("/sku-list")
    @SaCheckPermission("app:goods:specification")
    @Operation(summary = "商品规格查询")
    @LogPrint
    public ResultBean<List<GoodsSpecificationResp>> getGoodsSkuList(@RequestBody @Validated GoodsSpecificationQueryReq params){
        return ResultBean.success(goodsSpecificationService.getGoodsSpecification(params));
    }

    @PostMapping("/search")
    @SaCheckPermission("app:goods:search")
    @Operation(summary = "商品查询")
    @LogPrint
    public ResultBean<PageListBean<GoodsResp>> searchGoods(@RequestBody @Validated GoodSearchReq params){
        IPage<GoodsEntity> page = goodsService.searchGoods(params);
        PageListBean<GoodsResp> pageListBean = PageConverterUtils.convertPageListBean(page, GoodsConvert.INSTANCE::convertList);
        return ResultBean.success(pageListBean);
    }
}