package com.game.business.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.game.business.dto.FinTranDto;
import com.game.business.dto.FinTranRecordDTO;
import com.game.business.service.IAppGameBettingService;
import com.game.business.service.IAppUserService;
import com.game.business.vo.FinTranVo;
import com.game.common.core.domain.HttpRetArr;
import com.game.common.core.domain.HttpRetPageArr;
import com.game.common.core.domain.R;
import com.game.common.utils.MessageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.game.common.annotation.Log;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import com.game.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.game.business.domain.FinTranRecord;
import com.game.business.service.IFinTranRecordService;
import com.game.common.utils.poi.ExcelUtil;
import com.game.common.core.page.TableDataInfo;

/**
 * 消费记录Controller
 * 
 * @author game
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/business/tran_record")
@Api(value = "FinTranRecordController", description = "消费记录接口", tags = {"消费记录"})
public class FinTranRecordController extends BaseController
{
    @Autowired
    private IFinTranRecordService finTranRecordService;

    @Autowired
    private IAppGameBettingService appGameBettingService;

    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询消费记录列表", notes = "获取消费记录列表")
    public TableDataInfo list(FinTranRecord finTranRecord)
    {
        startPage();
        List<FinTranRecord> list = finTranRecordService.selectFinTranRecordList(finTranRecord);
        return getDataTable(list);
    }

    /**
     * 查询消费统计
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:count')")
    @GetMapping("/count")
    @ApiOperation(value = "查询消费记录统计", notes = "查询消费记录统计")
    public R count(FinTranRecordDTO finTranRecord)
    {
        JSONObject jsonObject = finTranRecordService.selecFinTranRecordCount(finTranRecord);

        // 用户总统计（游戏投注）
        Map<String, BigDecimal> bettingAmountSum = appGameBettingService.getBettingAmountSum(finTranRecord.getUserId());
        jsonObject.put("bettingAmountSum", bettingAmountSum);

        // 根据时间查询投注
        Map<String, BigDecimal> bettingAmountDateSum = appGameBettingService.getBettingAmountByDateSum(finTranRecord);
        jsonObject.put("bettingAmountDateSum", bettingAmountDateSum);

        // 用户总统计（金币、可兑换金币、剩余余额）
        Map<String, BigDecimal> userCount = appUserService.getUserCount(finTranRecord.getUserId());
        jsonObject.put("userCount", userCount);

        //金币兑换余额
        Map<String,Double> exchange = finTranRecordService.selectExChangeSum(finTranRecord);
        jsonObject.put("exchange", exchange);
        return R.ok(jsonObject);
    }

    /**
     * 导出消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:export')")
    @Log(title = "消费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出消费记录列表", notes = "导出消费记录列表")
    public void export(HttpServletResponse response, FinTranRecord finTranRecord)
    {
        List<FinTranRecord> list = finTranRecordService.selectFinTranRecordList(finTranRecord);
        ExcelUtil<FinTranRecord> util = new ExcelUtil<FinTranRecord>(FinTranRecord.class);
        util.exportExcel(response, list, "消费记录数据");
    }

    /**
     * 获取消费记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取消费记录详细信息", notes = "获取消费记录详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(finTranRecordService.selectFinTranRecordById(id));
    }

    /**
     * 新增消费记录
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:add')")
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增消费记录", notes = "新增消费记录")
    @PostMapping
    public AjaxResult add(@RequestBody FinTranRecord finTranRecord)
    {
        return toAjax(finTranRecordService.insertFinTranRecord(finTranRecord));
    }

    /**
     * 修改消费记录
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:edit')")
    @Log(title = "消费记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改消费记录", notes = "修改消费记录")
    @PutMapping
    public AjaxResult edit(@RequestBody FinTranRecord finTranRecord)
    {
        return toAjax(finTranRecordService.updateFinTranRecord(finTranRecord));
    }

    /**
     * 删除消费记录
     */
    @PreAuthorize("@ss.hasPermi('business:tran_record:remove')")
    @Log(title = "消费记录", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除消费记录", notes = "删除消费记录")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(finTranRecordService.deleteFinTranRecordByIds(ids));
    }

    /**
     * 查询消费记录列表
     */
    @PostMapping("/selectTranList")
    @ApiOperation(value = "查询直播收益、邀请奖励列表", notes = "查询直播收益、邀请奖励列表")
    public HttpRetPageArr<FinTranVo> selectTranList(@RequestBody FinTranDto finTranDto)
    {
        startPage();
        List<FinTranVo> list = finTranRecordService.selectTranList(finTranDto);
        return HttpRetPageArr.success(MessageUtils.message("11022"),getDataTable(list));
    }
}
