package com.game.business.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import com.game.business.vo.AppRechargeConfigVo;
import com.game.common.annotation.DataSource;
import com.game.common.enums.DataSourceType;
import com.game.common.utils.DateUtils;
import com.game.common.utils.StringUtils;
import com.game.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.game.business.mapper.AppUsersChargeMapper;
import com.game.business.domain.AppUsersCharge;
import com.game.business.service.IAppUsersChargeService;

/**
 * 充值记录Service业务层处理
 *
 * @author game
 * @date 2024-06-18
 */
@Service
public class AppUsersChargeServiceImpl extends ServiceImpl<AppUsersChargeMapper, AppUsersCharge> implements IAppUsersChargeService {
    @Autowired
    private AppUsersChargeMapper appUsersChargeMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询充值记录
     *
     * @param id 充值记录主键
     * @return 充值记录
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public AppUsersCharge selectAppUsersChargeById(Long id) {
        return appUsersChargeMapper.selectAppUsersChargeById(id);
    }

    /**
     * 查询充值记录列表
     *
     * @param appUsersCharge 充值记录
     * @return 充值记录
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<AppUsersCharge> selectAppUsersChargeList(AppUsersCharge appUsersCharge) {
        return appUsersChargeMapper.selectAppUsersChargeList(appUsersCharge);
    }

    /**
     * 新增充值记录
     *
     * @param appUsersCharge 充值记录
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int insertAppUsersCharge(AppUsersCharge appUsersCharge) {
       appUsersChargeMapper.getNewId();
       appUsersCharge.setId(appUsersChargeMapper.getMaxId());
       appUsersCharge.setUpdateTime(new Date());

       return appUsersChargeMapper.insertAppUsersCharge(appUsersCharge);
    }

    /**
     * 修改充值记录
     *
     * @param appUsersCharge 充值记录
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int updateAppUsersCharge(AppUsersCharge appUsersCharge) {
        return appUsersChargeMapper.updateAppUsersCharge(appUsersCharge);
    }

    /**
     * 批量删除充值记录
     *
     * @param ids 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int deleteAppUsersChargeByIds(Long[] ids) {
        return appUsersChargeMapper.deleteAppUsersChargeByIds(ids);
    }

    /**
     * 删除充值记录信息
     *
     * @param id 充值记录主键
     * @return 结果
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public int deleteAppUsersChargeById(Long id) {
        return appUsersChargeMapper.deleteAppUsersChargeById(id);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<AppUsersCharge> getChargeList(Long userId, String beginTime, String endTime) {
        LambdaQueryWrapper<AppUsersCharge> queryWrapper = new LambdaQueryWrapper<>();
        if(null != userId) {
            queryWrapper.eq(AppUsersCharge::getUid, userId);
        }
        queryWrapper.between(AppUsersCharge::getAddtime,beginTime,endTime);
        queryWrapper.eq(AppUsersCharge::getIsDelete,0);
        queryWrapper.eq(AppUsersCharge::getStatus,"1");
        queryWrapper.eq(AppUsersCharge::getCoinType,1);
        queryWrapper.notIn(AppUsersCharge::getType,new Integer[]{6,7});
        return appUsersChargeMapper.selectList(queryWrapper);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public double isChargeToday(Long userId,int coinType) {
        Date now = new Date();
        LambdaQueryWrapper<AppUsersCharge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUsersCharge::getUid,userId);
        queryWrapper.eq(AppUsersCharge::getStatus,1);
        queryWrapper.eq(AppUsersCharge::getCoinType,coinType);
        queryWrapper.ge(AppUsersCharge::getUpdateTime, DateUtil.format(now,"yyyy-MM-dd").concat(" 00:00:00"));
        queryWrapper.le(AppUsersCharge::getUpdateTime, DateUtil.format(now,"yyyy-MM-dd").concat(" 23:59:59"));
        List<AppUsersCharge> appUsersCharges = appUsersChargeMapper.selectList(queryWrapper);
        double money = 0;
        if(null != appUsersCharges && appUsersCharges.size() > 0){
            money = appUsersCharges.stream().mapToDouble(e->null==e.getCoinGive()?0.00:e.getCoinGive().doubleValue()).sum();
        }
        return money;
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public AppUsersCharge selectByOrderNo(String orderNo) {
        LambdaQueryWrapper<AppUsersCharge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AppUsersCharge::getOrderno,orderNo);
        return appUsersChargeMapper.selectOne(queryWrapper);
    }

    @Override
    @DataSource(DataSourceType.SLAVE)
    public Double sumCharge(Long userId) {
        return appUsersChargeMapper.sumCharge(userId);
    }

    @Override
    public AppRechargeConfigVo getRechargeConfig() {
        AppRechargeConfigVo configVo = new AppRechargeConfigVo();
        String config = configService.selectConfigByKey("app_recharge_config");
        if(StringUtils.isNotBlank(config)){
            String[] arr = config.split(",");
            configVo.setFirstSend(Double.valueOf(arr[0]));
            configVo.setSend(Double.valueOf(arr[1]));
            configVo.setMaxSend(Double.valueOf(arr[2]));
        }
        return configVo;
    }
}