package com.ruoyi.web.v2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.entity.PayRecord;
import com.ruoyi.web.v2.entity.UserAccount;
import com.ruoyi.web.v2.entity.request.UserAccountRequest;
import com.ruoyi.web.v2.mapper.PayRecordMapper;
import com.ruoyi.web.v2.mapper.UserAccountMapper;
import com.ruoyi.web.v2.service.IUserAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.v2.utils.AccountUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-12-25
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements IUserAccountService {
    @Autowired
    private PayRecordMapper payRecordMapper;

    @Override
    public AjaxResult getListEventByPage(UserAccountRequest userAccount) {

        return AjaxResult.success(this.page(new Page<UserAccount>( userAccount.getPageNum(),userAccount.getPageSize()),new QueryWrapper<UserAccount>()
                .like(StringUtils.isNotEmpty(userAccount.getUserId()), "user_id", userAccount.getUserId())
                .like(StringUtils.isNotEmpty(userAccount.getDeptName()),"dept_ame",userAccount.getDeptName())));
    }

    @Override
    public AjaxResult getList(UserAccountRequest userAccount) {
        return AjaxResult.success(this.list(new QueryWrapper<UserAccount>()
                .like(StringUtils.isNotEmpty(userAccount.getUserId()), "user_id", userAccount.getUserId())
                .like(StringUtils.isNotEmpty(userAccount.getDeptName()),"dept_ame",userAccount.getDeptName())));
    }

    @Override
    public UserAccount getByUserId(String userId) {
        return this.getOne(new QueryWrapper<UserAccount>().eq("user_id",userId));
    }

    @Override
    public PayRecord updatePayAmount(PayRecord payRecord) {
        //添加余额
        String payAmount = payRecord.getPayAmount();
        if (StringUtils.isEmpty(payAmount)){
            throw new IllegalArgumentException("充值金额为空");
        }

        UserAccount byUserId = this.getByUserId(payRecord.getUserId());
        if (payRecord.getPeyType() == 0){
            byUserId.setAccountBalance(AccountUtil.sumPerDoubleString(byUserId.getAccountBalance() , payAmount));
        }else if (payRecord.getPeyType() == 1){
            byUserId.setExperienceBalance(AccountUtil.sumPerDoubleString(byUserId.getExperienceBalance() , payAmount));
        }
        this.updateById(byUserId);

        payRecord.setAccountBalance(byUserId.getAccountBalance());

        payRecord.setExperienceBalance(byUserId.getExperienceBalance());
        return payRecord;

    }
}
