package com.red.circle.other.adapter.app.gift;

import com.red.circle.common.business.core.ControllerRedisKeyConstant;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.common.business.dto.cmd.app.AppUserIdCmd;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.app.dto.clientobject.gift.GiftConfigCO;
import com.red.circle.other.app.dto.clientobject.gift.GiftConfigGroupByTabCO;
import com.red.circle.other.app.dto.clientobject.gift.GiftWallCO;
import com.red.circle.other.app.dto.clientobject.gift.PremiumGiftWallCO;
import com.red.circle.other.app.dto.clientobject.gift.UserGiftBackpackCO;
import com.red.circle.other.app.dto.cmd.gift.GiveAwayGiftBackpackCmd;
import com.red.circle.other.app.dto.cmd.gift.GiveAwayGiftBatchCmd;
import com.red.circle.other.app.service.gift.GiftService;
import com.red.circle.tool.core.json.JacksonUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import com.red.circle.other.infra.annotation.RemoveUserActionCache;
import com.red.circle.other.infra.database.rds.enums.OtherConfigEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 礼物 前端控制器.
 * </p>
 *
 * @author pengliang on 2020-06-01
 * @eo.api-type http
 * @eo.groupName 礼物管理
 * @eo.path /gift
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/gift", produces = MediaType.APPLICATION_JSON_VALUE)
public class GiftRestController extends BaseController {

  private final GiftService giftService;

  /**
   * 用户礼物背包.
   *
   * @eo.name 用户礼物背包.
   * @eo.url /backpack
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/backpack")
  public List<UserGiftBackpackCO> listUserGiftBackpack(AppExtCommand cmd) {
    return giftService.listUserGiftBackpack(cmd);
  }

  /**
   * 赠送背包礼物.
   *
   * @eo.name 赠送背包礼物.
   * @eo.url /backpack/give-away
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/backpack/give-away")
  public void giveAwayGiftBackpack(@RequestBody @Validated GiveAwayGiftBackpackCmd cmd) {
    giftService.giveAwayGiftBackpack(cmd);
  }

  /**
   * 获取礼物列表.
   *
   * @eo.name 获取礼物列表.
   * @eo.url /list
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/list")
  public List<GiftConfigCO> list(AppExtCommand cmd) {
    return giftService.listSysOriginEffectiveAllGift(cmd);
  }

  /**
   * 获取礼物列表-分组.
   *
   * @eo.name 获取礼物列表-分组.
   * @eo.url /group-tab
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/group-tab")
  public List<GiftConfigGroupByTabCO> giftV2(AppExtCommand cmd) {
    return GiftConfigGroupByTabCO
        .toGiftConfigGroupByTab(giftService.listSysOriginEffectiveAllGift(cmd), Objects::nonNull);
  }

  /**
   * 获取礼物墙列表.
   *
   * @eo.name 获取礼物墙列表.
   * @eo.url /wall
   * @eo.method get
   * @eo.request-type formdata
   */
  @Cacheable(value = ControllerRedisKeyConstant.GIFT_WALL, key = "#cmd.getUserId()", unless = "#result == null || #result.isEmpty() || #result.size() < 10")
  @GetMapping("/wall")
  public List<GiftWallCO> giftWall(AppUserIdCmd cmd) {
    return giftService.listUserGiftWall(cmd);
  }


  /**
   * 赠送-礼物.
   *
   * @eo.name 赠送-礼物.
   * @eo.url /batch
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/batch")
  public BigDecimal giveAwayGiftBatch(@RequestBody @Validated GiveAwayGiftBatchCmd cmd) {
    log.warn("giveAwayGiftBatch:{}",JacksonUtils.toJson(cmd));
    return giftService.giveAwayGiftBatch(cmd);
  }

  /**
   * 赠送-幸运礼物.
   *
   * @eo.name 赠送-幸运礼物.
   * @eo.url /give/lucky-gift
   * @eo.method post
   * @eo.request-type json
   */
  @PostMapping("/give/lucky-gift")
  public BigDecimal giveLuckyGift(@RequestBody @Validated GiveAwayGiftBatchCmd cmd) {
    return giftService.giveLuckyGift(cmd);
  }

  /**
   * 获取公共高价值礼物墙列表.
   *
   * @eo.name 获取公共高价值礼物墙列表.
   * @eo.url /premium/gift/wall
   * @eo.method get
   * @eo.request-type formdata
   */
  @GetMapping("/premium/gift/wall")
  public List<PremiumGiftWallCO> premiumGiftWall(AppExtCommand cmd) {
    return giftService.listPremiumGiftWallCO(cmd);
  }


  /**
   * 赠送-祝福礼物.
   */
  @PostMapping("/give/blessings/gift")
  public BigDecimal giveBlessingsGift(@RequestBody @Validated GiveAwayGiftBatchCmd cmd) {

    return giftService.giveBlessingsGift(cmd);
  }

}

