package com.ruoyi.web.controller.mall;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.RestResponse;
import com.ruoyi.course.domain.req.ReqSearchClaTime;
import com.ruoyi.course.domain.time.RespBusinessClaTimeCalendar;
import com.ruoyi.course.service.ScClaTimeService;
import com.ruoyi.mall.domain.Product;
import com.ruoyi.mall.domain.query.ProductQuery;
import com.ruoyi.mall.domain.vo.AppProductVO;
import com.ruoyi.mall.domain.vo.ProductDetailVO;
import com.ruoyi.mall.service.ProductCategoryService;
import com.ruoyi.mall.service.impl.ProductServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户地址 前端控制器
 * </p>
 *
 * @author xn
 * @since 2022-09-29
 */
@Api(tags = "app商品信息")
@RestController
@RequestMapping("/api/product")
public class    ProductController {

    @Autowired
    private ProductCategoryService categoryService;
    @Autowired
    private ProductServiceImpl productService;

    @Autowired
    private ScClaTimeService scClaTimeService;

    /**
     * 商品页分类list
     * @return
     */
    @ApiOperation("商品类别")
    @PostMapping("/categoryList")
    public  RestResponse getAddressList(@RequestBody Map params){
        return new RestResponse().setSuccess(true).setData(categoryService.getProductCategorys(params));
    }

    /**
     * 商品搜索
     * @param query {categoryId：分类；search：名称；} null：全部商品
     * @param page
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Page<AppProductVO>> queryGoodByPage(@RequestBody ProductQuery query, Pageable page) {
        List<Product> pageRes = productService.selectList(query, page);
        if (ObjectUtil.isNotEmpty(pageRes)){
            return  ResponseEntity.ok(new PageImpl<>(BeanUtil.copyToList(pageRes,AppProductVO.class), page, ((com.github.pagehelper.Page) pageRes).getTotal()));
        }
        return ResponseEntity.ok(new PageImpl<>(new ArrayList<>(), page, 0));
    }

    /**
     * 商品详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public ResponseEntity<ProductDetailVO> queryDetail(@PathVariable Long id) {
        ProductDetailVO detail = productService.queryDetail(id);
        return ResponseEntity.ok(detail);
    }

    /**
     * 商品详情-课程表
     * @param id
     * @return
     */
    @GetMapping("/detail/calendar/{id}")
    public ResponseEntity<RespBusinessClaTimeCalendar> queryDetailCalendar(@PathVariable Long id) {
        ReqSearchClaTime reqSearchClaTime=new ReqSearchClaTime();
        HashSet<Long> courseIds= new HashSet<>();
        reqSearchClaTime.setCourseId(id);
        RespBusinessClaTimeCalendar c =scClaTimeService.searchListForCalendar(reqSearchClaTime);
        return ResponseEntity.ok(c);
    }

    /**
     *
     * 达人好物及精品课程
     * @return
     */
    @PostMapping("/getGoodStuff")
    public ResponseEntity<Map> getGoodStuff(@RequestBody Map map) {
        Map map1 = productService.selectByGoodStuff(map.get("storeId").toString());
        return ResponseEntity.ok(map1);
    }



}

