package com.hzs.system.board.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.utils.BigDecimalUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.system.board.service.IBoardService;
import com.hzs.system.board.vo.*;
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.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页看板 控制器
 */
@RestController
@RequestMapping("/manage/board")
public class BoardController extends BaseController {

    @Autowired
    private IBoardService iBoardService;

    /**
     * 会员汇总数据
     *
     * @return
     */
    @GetMapping("/member-summary")
    public AjaxResult memberSummary() {
        // 会员汇总数据
        BoardMemberVO boardMemberVO = iBoardService.getMemberBoard();
        boardMemberVO.setMemberTodayLevelList(new ArrayList<>(6));
        boardMemberVO.setMemberYesterdayLevelList(new ArrayList<>(6));
        boardMemberVO.setMemberMonthLevelList(new ArrayList<>(6));

        // 会员各等级数据
        List<BoardMemberLevelVO> levelList = iBoardService.listMemberLevelBoard();

        for (BoardMemberLevelVO boardMemberLevelVO : levelList) {
            if (boardMemberLevelVO.getSort() == 0) {
                // 今日
                boardMemberVO.getMemberTodayLevelList().add(boardMemberLevelVO);
            } else if (boardMemberLevelVO.getSort() == 1) {
                // 昨日
                boardMemberVO.getMemberYesterdayLevelList().add(boardMemberLevelVO);
            } else {
                // 本月
                boardMemberVO.getMemberMonthLevelList().add(boardMemberLevelVO);
            }
        }
        return AjaxResult.success(boardMemberVO);
    }

    /**
     * 日业绩数据
     *
     * @return
     */
    @GetMapping("/day-achieve")
    public AjaxResult dayAchieve() {
        // 返回数据VO
        BoardDayAchieveVO resultVO = new BoardDayAchieveVO();

        List<BoardAchieveVO> dayAchieveList = iBoardService.getDayAchieve();
        if (CollectionUtil.isNotEmpty(dayAchieveList)) {
            for (BoardAchieveVO boardAchieveVO : dayAchieveList) {
                if (boardAchieveVO.getSort() == 0) {
                    resultVO.setToday(BoardAchieveVO.builder()
                            .firstAchieve(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getFirstAchieve()))
                            .firstAmount(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getFirstAmount()))
                            .repAchieve(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getRepAchieve()))
                            .repAmount(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getRepAmount()))
                            .build());
                } else {
                    resultVO.setYesterday(BoardAchieveVO.builder()
                            .firstAchieve(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getFirstAchieve()))
                            .firstAmount(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getFirstAmount()))
                            .repAchieve(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getRepAchieve()))
                            .repAmount(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getRepAmount()))
                            .build());
                }
            }
        }

        return AjaxResult.success(resultVO);
    }

    /**
     * 月业绩数据
     *
     * @return
     */
    @GetMapping("/month-achieve")
    public AjaxResult monthAchieve() {
        BoardAchieveVO boardAchieveVO = iBoardService.getMonthAchieve();
        boardAchieveVO.setFirstAchieve(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getFirstAchieve()));
        boardAchieveVO.setFirstAmount(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getFirstAmount()));
        boardAchieveVO.setRepAchieve(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getRepAchieve()));
        boardAchieveVO.setRepAmount(BigDecimalUtil.handleBig10000Down(boardAchieveVO.getRepAmount()));
        return AjaxResult.success(boardAchieveVO);
    }

    /**
     * 充值汇总数据
     *
     * @return
     */
    @GetMapping("/recharge-summary")
    public AjaxResult rechargeSummary() {
        BoardRechargeVO boardRechargeVO = new BoardRechargeVO();
        boardRechargeVO.setRechargeTodayList(new ArrayList<>(6));
        boardRechargeVO.setRechargeYesterdayLevelList(new ArrayList<>(6));
        boardRechargeVO.setRechargeMonthLevelList(new ArrayList<>(6));

        // 会员汇总数据
        List<BoardRechargeAccountVO> rechargeList = iBoardService.listAccountRechargeBoard();

        for (BoardRechargeAccountVO boardMemberVO : rechargeList) {
            // 充值金额按万处理
            boardMemberVO.setRechargeAmount(BigDecimalUtil.handleBig10000Down(boardMemberVO.getRechargeAmount()));
            if (boardMemberVO.getSort() == 0) {
                // 今日
                boardRechargeVO.getRechargeTodayList().add(boardMemberVO);
            } else if (boardMemberVO.getSort() == 1) {
                // 昨日
                boardRechargeVO.getRechargeYesterdayLevelList().add(boardMemberVO);
            } else {
                // 本月
                boardRechargeVO.getRechargeMonthLevelList().add(boardMemberVO);
            }
        }
        return AjaxResult.success(boardRechargeVO);
    }

}
