package com.game.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.IdUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.game.business.domain.AppUser;
import com.game.business.domain.FinTranRecord;
import com.game.business.service.IAppUserService;
import com.game.business.task.AppUserCountTask;
import com.game.common.constant.AppSceneType;
import com.game.common.constant.finance.FinTranAddedInfo;
import com.game.common.constant.finance.FinTranType1;
import com.game.common.constant.finance.FinTranType3;
import com.game.common.constant.finance.TranCurrencyType;
import com.game.common.core.domain.R;
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 com.game.common.annotation.DataSource;
import com.game.common.enums.DataSourceType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.game.business.domain.AppUsersCashrecord;
import com.game.business.service.IAppUsersCashrecordService;
import com.game.common.utils.poi.ExcelUtil;
import com.game.common.core.page.TableDataInfo;

/**
 * 提现记录Controller
 * 
 * @author game
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/business/cashrecord")
@Api(value = "AppUsersCashrecordController", description = "提现记录接口", tags = {"提现记录"})
public class AppUsersCashrecordController extends BaseController
{
    @Autowired
    private IAppUsersCashrecordService appUsersCashrecordService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private AppUserCountTask appUserCountTask;

    /**
     * 查询提现记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:cashrecord:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询提现记录列表", notes = "获取提现记录列表")
    public TableDataInfo list(AppUsersCashrecord appUsersCashrecord)
    {
        startPage();
        List<AppUsersCashrecord> list = appUsersCashrecordService.selectAppUsersCashrecordList(appUsersCashrecord);
        return getDataTable(list);
    }

    /**
     * 导出提现记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:cashrecord:export')")
    @Log(title = "提现记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出提现记录列表", notes = "导出提现记录列表")
    public void export(HttpServletResponse response, AppUsersCashrecord appUsersCashrecord)
    {
        List<AppUsersCashrecord> list = appUsersCashrecordService.selectAppUsersCashrecordList(appUsersCashrecord);
        ExcelUtil<AppUsersCashrecord> util = new ExcelUtil<AppUsersCashrecord>(AppUsersCashrecord.class);
        util.exportExcel(response, list, "提现记录数据");
    }

    /**
     * 获取提现记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:cashrecord:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取提现记录详细信息", notes = "获取提现记录详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appUsersCashrecordService.selectAppUsersCashrecordById(id));
    }

    /**
     * 新增提现记录
     */
    @PreAuthorize("@ss.hasPermi('business:cashrecord:add')")
    @Log(title = "提现记录", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增提现记录", notes = "新增提现记录")
    @PostMapping
    public AjaxResult add(@RequestBody AppUsersCashrecord appUsersCashrecord)
    {
        return toAjax(appUsersCashrecordService.insertAppUsersCashrecord(appUsersCashrecord));
    }

    /**
     * 修改提现记录
     */
    @PreAuthorize("@ss.hasPermi('business:cashrecord:edit')")
    @Log(title = "提现记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改提现记录", notes = "修改提现记录")
    @PutMapping
    @DSTransactional
    public R<String> edit(@RequestBody AppUsersCashrecord appUsersCashrecord) throws Exception {
        AppUsersCashrecord cashrecord = appUsersCashrecordService.selectAppUsersCashrecordById(appUsersCashrecord.getId());
        if(null == cashrecord){
            return R.fail("审核失败，记录不存在");
        }
        if(cashrecord.getStatus().intValue() != 0){
            return R.fail("审核失败，当前状态不可审核");
        }
        AppUser appUser = appUserService.selectAppUserByUserid(cashrecord.getUid());
        if(null == appUser){
            return R.fail("审核失败，用户不存在");
        }
        AppUsersCashrecord update = new AppUsersCashrecord();
        update.setId(cashrecord.getId());
        update.setStatus(appUsersCashrecord.getStatus());
        update.setReason(appUsersCashrecord.getReason());
        int rest = appUsersCashrecordService.updateAppUsersCashrecord(update);
        if(rest < 1){
            return R.fail("审核失败");
        }
        if(2 == appUsersCashrecord.getStatus().intValue()){

            //审核驳回 回退用户金额
            //流水 余额
            FinTranAddedInfo addedInfo = FinTranAddedInfo.createTranInfo(appUser.getUserid(), 0, 0, AppSceneType.Scene_None, "");
            FinTranRecord tran = FinTranRecord.initFinTranRecordSomeParams(addedInfo, FinTranType3.CASH_DIAMOND_INCOME_BACK, FinTranType1.U_Income_Coin_Balance, appUser);
            tran.setDiamondCoinChange(cashrecord.getVotes().doubleValue());
            tran.setAfterDiamondCoin(appUser.getDiamondCoin());
            tran.setCurrencyType(TranCurrencyType.Balance.getType());
            tran.setTranGroupId(IdUtil.getSnowflakeNextId());
            tran.setRemarks("提现审核失败：回退金额");
            appUserService.updateUserAmount(tran);
            appUserCountTask.updateWithdrawAble(appUser.getUserid());
        }
        return R.ok("审核成功");
    }

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