package com.red.circle.console.adapter.app.count;


import com.alibaba.excel.EasyExcel;
import com.red.circle.console.app.dto.clienobject.count.ops.CountDailyCurrencyCountGroupCO;
import com.red.circle.console.app.dto.clienobject.count.ops.CountDailyGoldOriginGroupCO;
import com.red.circle.console.app.dto.clienobject.count.ops.CountUserRechargeRankGroupCO;
import com.red.circle.console.app.dto.clienobject.count.ops.CurrencyAnalyzeUserLogGroupCO;
import com.red.circle.console.app.dto.cmd.app.count.ops.GoldOriginProfileExportQry;
import com.red.circle.console.app.dto.cmd.app.count.ops.GoldOriginProfileQry;
import com.red.circle.console.app.excel.model.count.WalletGoldCountModel;
import com.red.circle.console.app.service.app.count.ops.CountCurrencyService;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.wallet.inner.model.dto.WalletGoldCountDTO;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
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;

/**
 * 货币流通统计 前端控制器.
 *
 * @author pengliang
 * @since 2021-07-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/count/currency")
public class CountCurrencyRestController extends BaseController {

  private final CountCurrencyService countCurrencyService;

  /**
   * 每日各平台最近货币情况.
   */
  @GetMapping("/daily/platform")
  public List<CountDailyCurrencyCountGroupCO> listLast30Days() {
    return countCurrencyService.listLast30Days();
  }

  /**
   * 每日各平台最近金币详情情况.
   */
  @GetMapping("/daily/gold-details")
  public List<CountDailyGoldOriginGroupCO> listLast30Days(
      @RequestParam String origin) {
    return countCurrencyService.listLastGoldOrigin30Days(origin);
  }

  /**
   * 每日各平台最近金币详情情况-all.
   */
  @GetMapping("/daily/gold-details/all")
  public List<WalletGoldCountDTO> listGoldOriginProfile(@Validated
  GoldOriginProfileQry qry) {
    return countCurrencyService.listGoldOriginProfile(qry);
  }

  /**
   * 每日各平台最近金币详情情况-all导出excel.
   */
  @GetMapping("/daily/gold-details/excel")
  public void listGoldOriginProfileExcel(HttpServletResponse response,
      @Validated GoldOriginProfileExportQry qry)
      throws IOException {

    List<WalletGoldCountDTO> walletGoldCounts = countCurrencyService.listGoldOriginProfileExport(
        qry);

    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    String fileName = URLEncoder.encode("WalletGoldCount", StandardCharsets.UTF_8)
        .replaceAll("\\+", "%20");
    String sheetName = URLEncoder.encode("WalletGoldCount", StandardCharsets.UTF_8)
        .replaceAll("\\+", "%20");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    EasyExcel.write(response.getOutputStream(), WalletGoldCountModel.class)
        .sheet(sheetName).doWrite(toListWalletGoldCountModel(walletGoldCounts));
  }

  private static List<WalletGoldCountModel> toListWalletGoldCountModel(
      List<WalletGoldCountDTO> walletGoldCounts) {
    if (CollectionUtils.isEmpty(walletGoldCounts)) {
      return CollectionUtils.newArrayList();
    }
    return walletGoldCounts.stream().map(count -> new WalletGoldCountModel()
        .setGroupNum(count.getGroupNum())
        .setType(count.getType())
        .setEventType(count.getEventType())
        .setEventDesc(count.getEventDesc())
        .setAmount(count.getAmount())
        .setPercentage(count.getPercentage())
    ).toList();
  }

  /**
   * 每日用户金币top榜.
   */
  @GetMapping("/daily-user-gold-top")
  public List<CurrencyAnalyzeUserLogGroupCO> listDailyUserGoldTop(
      @RequestParam Integer dateNumber) {
    return countCurrencyService.listDailyUserGoldTop(dateNumber);
  }

  /**
   * 每日用户充值top榜.
   */
  @GetMapping("/daily-user-recharge-top")
  public List<CountUserRechargeRankGroupCO> listDailyUserRechargeTop(
      @RequestParam Integer dateNumber) {
    return countCurrencyService.listDailyUserRechargeTop(dateNumber);
  }

}
