package com.game.business.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.List;

import com.game.business.service.IAppUserService;
import com.game.business.util.im.TencentCloudImUtil;
import com.game.business.vo.ApiUserInfoVo;
import com.game.common.constant.finance.FinTranType3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.game.business.mapper.AppDealScaleMapper;
import com.game.business.domain.AppDealScale;
import com.game.business.service.IAppDealScaleService;
import com.game.common.annotation.DataSource;
import com.game.common.enums.DataSourceType;

/**
 * 主播分成比例Service业务层处理
 *
 * @author game
 * @date 2024-08-11
 */
@Service
public class AppDealScaleServiceImpl extends ServiceImpl<AppDealScaleMapper, AppDealScale> implements IAppDealScaleService {
    @Autowired
    private AppDealScaleMapper appDealScaleMapper;

    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询主播分成比例
     *
     * @param id 主播分成比例主键
     * @return 主播分成比例
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public AppDealScale selectAppDealScaleById(Long id) {
        return appDealScaleMapper.selectAppDealScaleById(id);
    }

    /**
     * 查询主播分成比例列表
     *
     * @param appDealScale 主播分成比例
     * @return 主播分成比例
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<AppDealScale> selectAppDealScaleList(AppDealScale appDealScale) {
        return appDealScaleMapper.selectAppDealScaleList(appDealScale);
    }

    /**
     * 新增主播分成比例
     *
     * @param appDealScale 主播分成比例
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int insertAppDealScale(AppDealScale appDealScale) {
        appDealScaleMapper.updateId();
        Long id = appDealScaleMapper.getNextId();
        appDealScale.setId(id);
        return appDealScaleMapper.insertAppDealScale(appDealScale);
    }

    /**
     * 修改主播分成比例
     *
     * @param appDealScale 主播分成比例
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int updateAppDealScale(AppDealScale appDealScale) {
        return appDealScaleMapper.updateAppDealScale(appDealScale);
    }

    /**
     * 批量删除主播分成比例
     *
     * @param ids 需要删除的主播分成比例主键
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int deleteAppDealScaleByIds(Long[] ids) {
        return appDealScaleMapper.deleteAppDealScaleByIds(ids);
    }

    /**
     * 删除主播分成比例信息
     *
     * @param id 主播分成比例主键
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int deleteAppDealScaleById(Long id) {
        return appDealScaleMapper.deleteAppDealScaleById(id);
    }

    @Async("asyncExecutor")
    @Override
    @DataSource(DataSourceType.SLAVE)
    public void init(Long userId){
        //初始化分成比例
        // 获取默认百分比
        AppDealScale appDealScale = new AppDealScale();
        appDealScale.setDealScale(BigDecimal.valueOf(0.6));
        appDealScale.setUid(userId);
        appDealScale.setPlanId(0L);
        appDealScale.setIsTrue(0L);

        // 打赏 其它类型打赏的分成比例
        appDealScale.setTranType2(1L);
        appDealScale.setTranType3(1L);
        this.insertAppDealScale(appDealScale);

        // 打赏 直播房间打赏
        appDealScale.setFinTranType(FinTranType3.CONSUM_LIVE_ROOM_REWARD);
        this.insertAppDealScale(appDealScale);

        // 发送弹幕
        appDealScale.setFinTranType(FinTranType3.CONSUM_PT_TEXT_BARRAGE);
        this.insertAppDealScale(appDealScale);

        // 房间收益 计时房间
        appDealScale.setFinTranType(FinTranType3.CONSUM_ROOM_TIME);
        this.insertAppDealScale(appDealScale);

        // 房间收益 付费房间
        appDealScale.setFinTranType(FinTranType3.CONSUM_ROOM_PAY);
        this.insertAppDealScale(appDealScale);

        // 购买贵宾席
        appDealScale.setFinTranType(FinTranType3.CONSUM_BUY_VIPSEATS);
        this.insertAppDealScale(appDealScale);

        // 打赏 通话打赏
        appDealScale.setFinTranType(FinTranType3.CONSUM_O2O_CALL_REWARD);
        this.insertAppDealScale(appDealScale);

        // 付费通话 视频通话
        appDealScale.setFinTranType(FinTranType3.CONSUM_PAY_O2O_VIDEO);
        this.insertAppDealScale(appDealScale);

        // 付费通话 语音通话
        appDealScale.setFinTranType(FinTranType3.CONSUM_PAY_O2O_VOICE);
        this.insertAppDealScale(appDealScale);

        // 查看联系方式 查看手机号
        appDealScale.setFinTranType(FinTranType3.CONSUM_WATCH_MOBILE);
        this.insertAppDealScale(appDealScale);

        // 查看联系方式 查看微信号
        appDealScale.setFinTranType(FinTranType3.CONSUM_WATCH_WECHAT);
        this.insertAppDealScale(appDealScale);

        // 购买svip
        // appDealScale.setFinTranType(FinTranType3.CONSUM_BUY_SVIP);
        // appDealScaleCrud.Create(appDealScale, true);

        // 付费直播回放 直播回放 -- 暂时没有，先注释掉
        // appDealScale.setFinTranType(FinTranType3.CONSUM_PAY_COIN_LIVE_REPLAY);
        // appDealScaleCrud.Create(appDealScale, true);

        // 打赏 短视频打赏
        appDealScale.setFinTranType(FinTranType3.CONSUM_NEWS_REWARD);
        this.insertAppDealScale(appDealScale);

        // 私密视图 私密视频
        appDealScale.setFinTranType(FinTranType3.CONSUM_PRIVATE_VIDEO);
        this.insertAppDealScale(appDealScale);

        // 私密视图 私密图片
        appDealScale.setFinTranType(FinTranType3.CONSUM_PRIVATE_IMG);
        this.insertAppDealScale(appDealScale);

        // 打赏 聊天打赏
        appDealScale.setFinTranType(FinTranType3.CONSUM_CHAT_REWARD);
        this.insertAppDealScale(appDealScale);

        // 打赏 个人主页打赏
        appDealScale.setFinTranType(FinTranType3.CONSUM_PERSONAL_HOME_PAGE_REWARD);
        this.insertAppDealScale(appDealScale);

        // 打赏 幸运礼物
        appDealScale.setFinTranType(FinTranType3.CONSUM_LUCKY_GIFT_REWARD);
        this.insertAppDealScale(appDealScale);

        // 付费私信
        appDealScale.setFinTranType(FinTranType3.CONSUM_CHAT_CHARGES);
        this.insertAppDealScale(appDealScale);

        // 守护
        appDealScale.setFinTranType(FinTranType3.CONSUM_GUARD);
        this.insertAppDealScale(appDealScale);

        // 加入粉丝团
        appDealScale.setFinTranType(FinTranType3.CONSUM_FANS_TEAM);
        this.insertAppDealScale(appDealScale);

        // // 购买贵族
        // appDealScale.setFinTranType(FinTranType3.CONSUM_BUY_NOBLE);
        // appDealScaleCrud.Create(appDealScale, true);
        // 寻觅订单完结
        appDealScale.setFinTranType(FinTranType3.SEEK_ORDER_FINISH);
        this.insertAppDealScale(appDealScale);

        ApiUserInfoVo apiUserInfoVo = appUserService.getUserInfo(userId);
        //发送im通知
        JSONObject paramJson = new JSONObject();
        paramJson.put("type", "AnchorMsgSender");
        paramJson.put("subType", "onAnchorAuthUser");
        paramJson.put("content", JSONObject.toJSONString(apiUserInfoVo));
        TencentCloudImUtil.sendMsg(1,null,String.valueOf(userId),JSONObject.toJSONString(paramJson),0);
    }
}