package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.ApiResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mall.domain.MallOrderOrder;
import com.ruoyi.mall.mapper.MallCombineGoodsMapper;
import com.ruoyi.mall.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 拼团
 *
 * @author chang
 * @date 2021-11-25
 */
@RestController
@RequestMapping("/api/combine")
@ApiOperation("拼团")
public class ApiCombineController {

    @Resource
    private IMallCombineGoodsService mallCombineGoodsService;
    @Resource
    private IMallCombineOrderLinkService mallCombineOrderLinkService;


    /**
     * 拼团商品列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @return ok
     */
    @GetMapping("/getCombineGoodsList")
    public ApiResult getCombineGoodsList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "100", required = false) Integer pageSize,
                                         @RequestParam Map<String, Object> params) {

        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> list = mallCombineGoodsService.getCombineGoodsList(params);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
            return ApiResult.ok().put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("拼团商品列表异常");
        }
    }

    /**
     * 待拼团列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param params   goodsId 商品ID
     * @return ok
     */
    @GetMapping("/getWaitCombineList")
    public ApiResult getWaitCombineList(@RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "100", required = false) Integer pageSize,
                                        @RequestParam Map<String, Object> params) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<Map<String, Object>> list = mallCombineOrderLinkService.getWaitCombineList(params);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo(list);
            return ApiResult.ok().put("data", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("待拼团列表异常");
        }
    }

    /**
     * 待拼团详情
     *
     * @param params   linkId 待拼团订单ID
     * @return ok
     */
    @GetMapping("/getWaitCombineInfo")
    public ApiResult getWaitCombineInfo(@RequestParam Map<String, Object> params) {
        if (!params.containsKey("linkId") || StringUtils.isBlank(params.get("linkId").toString())) {
            return ApiResult.error("请选择待拼团详情");
        }
        try {
            return ApiResult.ok().put("data", mallCombineOrderLinkService.getWaitCombineInfo(params));
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("待拼团详情异常");
        }

    }

}
