package com.cpop.mall.business.controller.mini;

import com.alibaba.fastjson.JSONObject;
import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.mall.business.bo.ProductPageBo;
import com.cpop.mall.business.service.CarouselService;
import com.cpop.mall.business.service.ProductService;
import com.cpop.mall.business.vo.*;
import com.cpop.system.business.entity.Store;
import com.cpop.system.business.service.StoreService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

import static com.cpop.mall.business.entity.table.CarouselTableDef.CAROUSEL;

/**
 * 商城-商品表 控制层。
 *
 * @author DB
 * @since 2023-10-23
 */
@RestController
@Api(tags = "小程序商城-商品管理")
@RequestMapping("/mini/product")
public class MiniProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CarouselService carouselService;

    /**
     * @descriptions 分页查询商城-商品
     * @author DB
     * @date 2023/10/23 11:56
     * @param bo 分页参数
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getMiniProductPage")
    @ApiOperation("分页查询商城-商品")
    public R<Page<MiniProductPageVo>> getMiniProductPage(@ApiParam("分页参数") ProductPageBo bo) {
        Page<MiniProductPageVo> page = productService.getMiniProductPage(bo);
        return R.ok(page);
    }

    /**
     * @descriptions 查询商城同类型随机商品
     * @author DB
     * @date 2023/11/13 16:24
     * @param productType 商品类型
     * @param id id
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.vo.MiniProductPageVo>>
     */
    @GetMapping("/getSameTypeRandomProduct")
    @ApiOperation("查询商城同类型随机商品")
    public R<Page<MiniProductPageVo>> getSameTypeRandomProduct(@RequestParam("productType") @ApiParam(value = "商品类型", required = true) Integer productType, @RequestParam("id") @ApiParam(value = "商品id", required = true) String id) {
        Page<MiniProductPageVo> page = productService.getSameTypeRandomProduct(id, productType);
        return R.ok(page);
    }

    /**
     * @descriptions 下单需要选择店铺/校区
     * @author DB
     * @date 2023/10/23 11:56
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getStoreList")
    @ApiOperation("下单需要选择店铺/校区")
    public R<List<StoreListVo>> getStoreList() {
        List<StoreListVo> list = productService.getStoreList();
        return R.ok(list);
    }

    /**
     * @descriptions 查询商品评价分页列表
     * @author DB
     * @date 2023/10/23 11:56
     * @param productId 商品id
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getOrderEvaluatePage/{productId}")
    @ApiOperation("查询商品评价分页列表")
    public R<Page<ProductEvaluateVo>> getOrderEvaluatePage(@PathVariable @ApiParam("商品id") String productId) {
        Page<ProductEvaluateVo> page = productService.getOrderEvaluatePage(productId);
        return R.ok(page);
    }

    /**
     * @descriptions 根据校区ids获取校区信息
     * @author DB
     * @date 2023/11/02 17:11
     * @param storeIds 校区id列表
     * @return: com.cpop.core.base.R<java.util.List<com.cpop.mall.business.vo.StoreListVo>>
     */
    @GetMapping("/getStoreByIds")
    @ApiOperation("根据校区ids获取校区信息")
    public R<List<StoreListVo>> getMiniProductPage(@RequestParam("storeIds") String storeIds) {
        List<Store> stores = SpringUtils.getBean(StoreService.class).listByIds(Arrays.asList(storeIds.split(",")));
        List<StoreListVo> list = BeanUtils.mapToList(stores, StoreListVo.class);
        return R.ok(list);
    }

    /**
     * @descriptions 获取商品详情
     * @author DB
     * @date 2023/11/06 11:55
     * @param id 商品id
     * @return: com.cpop.core.base.R<com.cpop.mall.business.vo.ProductInfoVo>
     */
    @GetMapping("/getProductInfo/{id}")
    @ApiOperation("获取商品详情")
    public R<ProductInfoVo> getProductInfo(@PathVariable("id") @ApiParam(value = "商品id", required = true) String id) {
        ProductInfoVo info = productService.getProductInfo(id);
        return R.ok(info);
    }

    /**
     * 获取商城轮播图
     * @author DB
     * @since 2024/1/15
     * @return R<List<CarouselListVo>>
     */
    @GetMapping("/getCarouselList")
    @ApiOperation("获取商城轮播图")
    public R<List<CarouselListVo>> getCarouselList() {
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        List<CarouselListVo> vos = carouselService.queryChain().where(CAROUSEL.BRAND_ID.eq(loginUserInfo.getString("brandId"))
                .and(CAROUSEL.UP_STATUS.eq(true))).orderBy(CAROUSEL.ORDER_NO.asc()).listAs(CarouselListVo.class);
        return R.ok(vos);
    }
}
