
package com.tmerclub.cloud.group.controller.app;

import com.tmerclub.cloud.api.group.vo.GroupOrderVO;
import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.group.service.GroupActivityService;
import com.tmerclub.cloud.group.service.GroupOrderService;
import com.tmerclub.cloud.group.service.GroupTeamService;
import com.tmerclub.cloud.group.vo.app.AppGroupActivityVO;
import com.tmerclub.cloud.group.vo.app.AppGroupTeamInfoVO;
import com.tmerclub.cloud.group.vo.app.AppGroupTeamVO;
import com.tmerclub.cloud.group.vo.mongo.MongoAppGroupUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
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.List;
import java.util.Objects;

/**
 * 拼团团队表
 *
 * @author YXF
 * @date 2021-03-20 10:39:32
 */
@RestController("appGroupTeamController")
@RequestMapping("/groupTeam")
@Tag(name = "app-拼团团队表")
public class GroupTeamController {

    @Autowired
    private GroupTeamService groupTeamService;

    @Autowired
    private GroupActivityService groupActivityService;

    @Autowired
    private GroupOrderService groupOrderService;

    @DubboReference
    private SpuFeignClient spuFeignClient;

    @SuppressWarnings("checkstyle:LineLength")
    @GetMapping("/info")
    @Operation(summary = "拼团详情", description = "拼团详情,至少携带一个参数,查看拼团单（团队）信息")
    public ServerResponseEntity<AppGroupTeamInfoVO> teamInfo(
            @Parameter(description = "拼团团队ID") Long groupTeamId,
            @Parameter(description = "订单id") Long orderId, @Parameter(description = "商品id") Long spuId) {
        // 通过订单编号获取团ID
        if (Objects.nonNull(orderId)) {
            GroupOrderVO groupOrder = groupOrderService.getByOrderId(orderId);
            if (Objects.nonNull(groupOrder) && Objects.nonNull(groupOrder.getGroupTeamId())) {
                groupTeamId = groupOrder.getGroupTeamId();
            }
        }

        AppGroupTeamVO groupTeam = groupTeamService.getAppGroupTeam(groupTeamId);

        // 获取参团的用户列表
        List<MongoAppGroupUserVO> groupUserList = groupOrderService.listApiGroupUserDto(groupTeamId);
        // 获取活动信息
        AppGroupActivityVO appGroupActivityVoInfo = groupActivityService.getAppGroupActivityByGroupActivityId(groupTeam.getGroupActivityId());

        GroupOrderVO groupOrder = groupOrderService.getUserGroupOrderByGroupTeamId(groupTeamId);

        ServerResponseEntity<SpuVO> spuResponse = spuFeignClient.getById(spuId);
        if (!spuResponse.isSuccess()) {
            return ServerResponseEntity.transform(spuResponse);
        }
        SpuVO spuVO = spuResponse.getData();
        appGroupActivityVoInfo.setJoinGroupTeamId(groupTeamId);

        // 拼装拼团详情信息
        AppGroupTeamInfoVO infoVo = new AppGroupTeamInfoVO();
        infoVo.setGroupTeam(groupTeam);
        infoVo.setGroupUserList(groupUserList);
        infoVo.setGroupActivity(appGroupActivityVoInfo);
        infoVo.setSpuName(spuVO.getName());
        infoVo.setPriceFee(spuVO.getPriceFee());
        infoVo.setMainImgUrl(spuVO.getMainImgUrl());
        infoVo.setSellingPoint(spuVO.getSellingPoint());
        infoVo.setSpuMold(spuVO.getSpuMold());
        infoVo.setIsRefund(spuVO.getIsRefund());
        if (groupOrder != null) {
            infoVo.setOrderId(groupOrder.getOrderId());
        }
        return ServerResponseEntity.success(infoVo);
    }

    @GetMapping("/joinGroupUsers")
    @Operation(summary = "参团的用户列表", description = "参团的用户列表")
    public ServerResponseEntity<List<MongoAppGroupUserVO>> joinGroupUsers(
            @Parameter(description = "拼团团队ID", required = true) @RequestParam("groupTeamId") Long groupTeamId) {
        // 获取参团的用户列表
        List<MongoAppGroupUserVO> groupUserList = groupOrderService.listApiGroupUserDto(groupTeamId);
        return ServerResponseEntity.success(groupUserList);
    }
}
