/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.groupbuy.api.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.dto.OrderRefundDto;
import com.yami.shop.common.response.ServerResponse;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.groupbuy.comment.api.dto.*;
import com.yami.shop.groupbuy.comment.enums.ActivityStatusEnum;
import com.yami.shop.groupbuy.comment.enums.TeamStatusEnum;
import com.yami.shop.groupbuy.comment.model.GroupTeam;
import com.yami.shop.groupbuy.comment.service.GroupActivityService;
import com.yami.shop.groupbuy.comment.service.GroupProdService;
import com.yami.shop.groupbuy.comment.service.GroupSkuService;
import com.yami.shop.groupbuy.comment.service.GroupTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
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 java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 拼团商品接口
 *
 * @author 小懒虫
 * @date 2019/8/30 9:44
 */
@RestController
@RequestMapping("/groupProd")
@Api(tags = "拼团商品接口")
@AllArgsConstructor
public class GroupProdController {

    private final GroupSkuService groupSkuService;
    private final GroupTeamService groupTeamService;
    private final GroupProdService groupProdService;
    private final GroupActivityService groupActivityService;

    @GetMapping("/info")
    @ApiOperation(value = "拼团活动详情", notes = "获取对应商品的拼团活动信息")
    public ResponseEntity<ServerResponse<ApiGroupActivityDto>> activityInfo(
            @ApiParam(value = "物品ID", required = true) @RequestParam("prodId") Long prodId,
            @ApiParam(value = "拼团活动ID", required = true) @RequestParam("groupActivityId") Long groupActivityId) {

        // 查询拼团活动信息
        ApiGroupActivityDto info = groupActivityService.getApiGroupActivityInfo(groupActivityId, prodId);

        // [getStatus 2:未启用]
        if (info == null || info.getStatus() == 2) {
//            ("当前商品没有参加该拼团活动");
            return ResponseEntity.ok(ServerResponseEntity.fail("当前商品没有参加该拼团活动"));
        }

        if (info.getIsPreheat() != 1 && info.getActivityStatus().equals(ActivityStatusEnum.NOT_STARTED.value())) {
            return ResponseEntity.ok(ServerResponseEntity.fail("当前拼团活动未开始"));
        }

        if (info.getActivityStatus().equals(ActivityStatusEnum.FINISHED.value())) {
            return ResponseEntity.ok(ServerResponseEntity.fail("当前拼团活动已结束"));
        }

        if (info.getActivityStatus().equals(ActivityStatusEnum.EXPIRED.value())) {
            return ResponseEntity.ok(ServerResponseEntity.fail("当前拼团活动已失效"));
        }

        ApiJoinGroupTeamDto joinTeam = groupTeamService.getJoinGroupTeamHasMaxNum(groupActivityId, prodId);
        if (joinTeam != null) {
            GroupTeam groupTeam = groupTeamService.getById(joinTeam.getJoinGroupTeamId());
            if (Objects.equals(groupTeam.getStatus(), TeamStatusEnum.IN_GROUP.value())) {
                info.setJoinGroupTeamId(joinTeam.getJoinGroupTeamId());
            }
            info.setProdCount(joinTeam.getProdCount());
        }

        // 获取SKU列表
        List<ApiGroupSkuDto> apiGroupSkuDtoList = groupSkuService.getApiByGroupActivityIdAndProdId(info.getGroupActivityId(), prodId);
        info.setGroupSkuList(apiGroupSkuDtoList);

        // 获取商品活动最低价
        List<Double> actPrices = apiGroupSkuDtoList.stream().map(ApiGroupSkuDto::getActPrice).collect(Collectors.toList());
        info.setActPrice(Collections.min(actPrices));

        return ResponseEntity.ok(ServerResponseEntity.success(info));
    }

    @GetMapping("/joinGroupList")
    @ApiOperation(value = "可加入的团列表", notes = "只显示最近n个团列表(默认10)")
    public ResponseEntity<List<ApiGroupTeamDto>> joinGroupList(
            @ApiParam(value = "物品ID", required = true) @RequestParam("prodId") Long prodId,
            @ApiParam(value = "拼团活动ID", required = true) @RequestParam("groupActivityId") Long groupActivityId,
            @ApiParam(value = "显示数量（默认10）") @RequestParam(value = "showSize", defaultValue = "10") Integer showSize) {
        List<ApiGroupTeamDto> list = groupTeamService.listJoinGroup(groupActivityId, prodId, showSize);
        return ResponseEntity.ok(list);
    }

    @GetMapping("/page")
    @ApiOperation(value = "拼团商品列表", notes = "拼团商品列表（分页列表）")
    public ResponseEntity<IPage<ApiGroupProdDto>> getGroupProdPage(PageParam<OrderRefundDto> page) {
        IPage<ApiGroupProdDto> prodPage = groupProdService.getApiGroupProdPage(page);
        return ResponseEntity.ok(prodPage);
    }

    @GetMapping("/indexList")
    @ApiOperation(value = "首页中的团购商品列表", notes = "获取最新前5条团购商品数据")
    public ResponseEntity<List<ApiGroupProdDto>> indexList() {
        PageParam<OrderRefundDto> page = new PageParam<>();
        page.setSearchCount(false);
        page.setCurrent(1L);
        page.setSize(5);
        page.setTotal(5);
        IPage<ApiGroupProdDto> prodPage = groupProdService.getApiGroupProdPage(page);
        return ResponseEntity.ok(prodPage.getRecords());
    }
}
