package com.game.business.controller;

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

import com.alibaba.fastjson2.JSONObject;
import com.game.business.domain.AppDealScale;
import com.game.business.domain.AppUser;
import com.game.business.service.IAppDealScaleService;
import com.game.business.service.IAppUserService;
import com.game.business.service.impl.AppDealScaleServiceImpl;
import com.game.business.util.im.TencentCloudImConstant;
import com.game.business.util.im.TencentCloudImUtil;
import com.game.business.vo.ApiUserInfoVo;
import com.game.business.vo.AppAnchorStarVo;
import com.game.common.constant.finance.FinTranType3;
import com.game.common.core.domain.R;
import com.game.common.core.redis.RedisCache;
import org.apache.poi.openxml4j.opc.PackagePart;
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.AppUsersAuth;
import com.game.business.service.IAppUsersAuthService;
import com.game.common.utils.poi.ExcelUtil;
import com.game.common.core.page.TableDataInfo;

/**
 * 主播认证Controller
 * 
 * @author game
 * @date 2024-08-11
 */
@RestController
@RequestMapping("/business/auth")
@Api(value = "AppUsersAuthController", description = "主播认证接口", tags = {"主播认证"})
public class AppUsersAuthController extends BaseController
{
    @Autowired
    private IAppUsersAuthService appUsersAuthService;

    @Autowired
    private IAppUserService appUserService;

    private IAppDealScaleService dealScaleService;
    @Autowired
    private AppDealScaleServiceImpl appDealScaleServiceImpl;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询主播认证列表
     */
    @PreAuthorize("@ss.hasPermi('business:auth:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询主播认证列表", notes = "获取主播认证列表")
    public TableDataInfo<AppUsersAuth> list(AppUsersAuth appUsersAuth)
    {
        startPage();
        List<AppUsersAuth> list = appUsersAuthService.selectAppUsersAuthList(appUsersAuth);
        return getDataTable(list);
    }

    /**
     * 查询主播星级列表
     */
    @GetMapping("/starList")
    @ApiOperation(value = "查询主播星级列表", notes = "查询主播星级列表")
    public R<List<AppAnchorStarVo>> starList(AppUsersAuth appUsersAuth)
    {
        return R.ok(appUsersAuthService.selectAnchorStar());
    }

    /**
     * 导出主播认证列表
     */
    @PreAuthorize("@ss.hasPermi('business:auth:export')")
    @Log(title = "主播认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出主播认证列表", notes = "导出主播认证列表")
    public void export(HttpServletResponse response, AppUsersAuth appUsersAuth)
    {
        List<AppUsersAuth> list = appUsersAuthService.selectAppUsersAuthList(appUsersAuth);
        ExcelUtil<AppUsersAuth> util = new ExcelUtil<AppUsersAuth>(AppUsersAuth.class);
        util.exportExcel(response, list, "主播认证数据");
    }

    /**
     * 获取主播认证详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:auth:query')")
    @GetMapping(value = "/{uid}")
    @ApiOperation(value = "获取主播认证详细信息", notes = "获取主播认证详细信息")
    public R<AppUsersAuth> getInfo(@PathVariable("uid") Long uid)
    {
        return R.ok(appUsersAuthService.selectAppUsersAuthByUid(uid));
    }

    /**
     * 认证审核
     */
    @Log(title = "认证审核", businessType = BusinessType.INSERT)
    @ApiOperation(value = "认证审核", notes = "认证审核")
    @PostMapping("/audit")
    public R audit(@RequestBody AppUsersAuth appUsersAuth)
    {
        AppUsersAuth usersAuth = appUsersAuthService.selectAppUsersAuthByUid(appUsersAuth.getUid());
        if(null == usersAuth || usersAuth.getStatus().longValue() != 1 || (appUsersAuth.getStatus().longValue() != 0 && appUsersAuth.getStatus().longValue() != 2)){
            return R.fail("审核失败，当前状态不可审核");
        }
        AppUsersAuth updateAppUsersAuth = new AppUsersAuth();
        updateAppUsersAuth.setUid(appUsersAuth.getUid());
        //审核通过
        AppUser appUser = appUserService.selectAppUserByUserid(usersAuth.getUid());
        if(null == appUser){
            return R.fail("审核失败，用户不存在");
        }
        AppUser updateAppUser = new AppUser();
        updateAppUser.setUserid(appUser.getUserid());
        if(appUsersAuth.getStatus() == 0){

            updateAppUser.setRole(1L);
            updateAppUser.setIsAnchorAuth(1L);
            updateAppUser.setStarId(appUsersAuth.getStarId());
            updateAppUser.setIsShowHomePage(1L);

            updateAppUsersAuth.setStarId(appUsersAuth.getStarId());
            updateAppUsersAuth.setStatus(appUsersAuth.getStatus());

            // if (FunctionList.WhetherCreateFanGroup()) {
            //     // 主播转盘 主播转盘
            //     appDealScale.setFinTranType(FinTranType3.CONSUM_GAME_ANCHOR_ZHUANPAN);
            //     appDealScale.setDealScale(0);
            //     appDealScaleCrud.Create(appDealScale, true);
            // }

            //创建群聊
            long groupId = (500000000 + appUser.getUserid());
            if(!TencentCloudImUtil.haveGroup(String.valueOf(groupId))){
                TencentCloudImUtil.createGroup(TencentCloudImConstant.GROUP_CHAT_TYPE_PUBLIC, String.valueOf(appUser.getUserid()), String.valueOf(groupId), appUser.getUsername());
            }

            //初始化直播分成
            appDealScaleServiceImpl.init(appUser.getUserid());
        }else if(appUsersAuth.getStatus() == 2){
            //审核不通过
            updateAppUsersAuth.setStatus(appUsersAuth.getStatus());

            updateAppUser.setRole(0L);
            updateAppUser.setIsAnchorAuth(0L);
        }
        updateAppUsersAuth.setReason(appUsersAuth.getReason());
        appUserService.updateAppUser(updateAppUser);
        redisCache.deleteObject("U:UserInfo:".concat(String.valueOf(appUser.getUserid())));
        updateAppUsersAuth.setUptime(new Date());
        appUsersAuthService.updateAppUsersAuth(updateAppUsersAuth);


        return R.ok("审核成功");
    }

    /**
     * 新增主播认证
     */
    @PreAuthorize("@ss.hasPermi('business:auth:add')")
    @Log(title = "主播认证", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增主播认证", notes = "新增主播认证")
    @PostMapping
    public R add(@RequestBody AppUsersAuth appUsersAuth)
    {
        return R.ok(appUsersAuthService.insertAppUsersAuth(appUsersAuth));
    }

    /**
     * 修改主播认证
     */
    @PreAuthorize("@ss.hasPermi('business:auth:edit')")
    @Log(title = "主播认证", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改主播认证", notes = "修改主播认证")
    @PutMapping
    public R edit(@RequestBody AppUsersAuth appUsersAuth)
    {
        return R.ok(appUsersAuthService.updateAppUsersAuth(appUsersAuth));
    }

    /**
     * 删除主播认证
     */
    @PreAuthorize("@ss.hasPermi('business:auth:remove')")
    @Log(title = "主播认证", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除主播认证", notes = "删除主播认证")
	@DeleteMapping("/{uids}")
    public R remove(@PathVariable Long[] uids)
    {
        return R.ok(appUsersAuthService.deleteAppUsersAuthByUids(uids));
    }
}
