package beilv.userbill.service.impl;

import beilv.common.core.text.Convert;
import beilv.common.utils.DateUtils;
import beilv.system.domain.SysMember;
import beilv.system.domain.dto.SysMemberDTO;
import beilv.system.mapper.SysMemberMapper;
import beilv.userbill.domain.SysUserBill;
import beilv.userbill.mapper.SysUserBillMapper;
import beilv.userbill.service.ISysUserBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户积分流水Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-31
 */
@Service
public class SysUserBillServiceImpl implements ISysUserBillService {
    @Autowired
    private SysUserBillMapper sysUserBillMapper;
    @Autowired
    private SysMemberMapper sysMemberMapper;

    /**
     * 查询用户积分流水
     *
     * @param id 用户积分流水主键
     * @return 用户积分流水
     */
    @Override
    public SysUserBill selectSysUserBillById(Long id) {
        return sysUserBillMapper.selectSysUserBillById(id);
    }

    /**
     * 查询用户积分流水列表
     *
     * @param sysUserBill 用户积分流水
     * @return 用户积分流水
     */
    @Override
    public List<SysUserBill> selectSysUserBillList(SysUserBill sysUserBill) {
        return sysUserBillMapper.selectSysUserBillList(sysUserBill);
    }

    /**
     * 新增用户积分流水
     *
     * @param sysUserBill 用户积分流水
     * @return 结果
     */
    @Override
    public int insertSysUserBill(SysUserBill sysUserBill) {
        sysUserBill.setCreateTime(DateUtils.getNowDate());
        return sysUserBillMapper.insertSysUserBill(sysUserBill);
    }

    /**
     * 修改用户积分流水
     *
     * @param sysUserBill 用户积分流水
     * @return 结果
     */
    @Override
    public int updateSysUserBill(SysUserBill sysUserBill) {
        sysUserBill.setUpdateTime(DateUtils.getNowDate());
        return sysUserBillMapper.updateSysUserBill(sysUserBill);
    }

    /**
     * 批量删除用户积分流水
     *
     * @param ids 需要删除的用户积分流水主键
     * @return 结果
     */
    @Override
    public int deleteSysUserBillByIds(String ids) {
        return sysUserBillMapper.deleteSysUserBillByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除用户积分流水信息
     *
     * @param id 用户积分流水主键
     * @return 结果
     */
    @Override
    public int deleteSysUserBillById(Long id) {
        return sysUserBillMapper.deleteSysUserBillById(id);
    }

    /**
     * 增加收入流水
     *
     * @param userId  userId
     * @param title   账单标题
     * @param number  明细数字
     * @param balance 剩余
     * @param remark  备注
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void income(Long userId, String title, BigDecimal number, BigDecimal balance, String remark) {
        SysUserBill userBill = SysUserBill.builder()
                .userId(userId)
                .title(title)
                .number(number)
                .balance(balance)
                .pm(1)
                .remark(remark)
                .build();
        sysUserBillMapper.insertSysUserBill(userBill);
    }

    /**
     * 增加支出流水
     *
     * @param userId  userId
     * @param title   账单标题
     * @param number  明细数字
     * @param balance 剩余
     * @param remark  备注
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void expend(Long userId, String title, BigDecimal number, BigDecimal balance, String remark) {
        SysUserBill userBill = SysUserBill.builder()
                .userId(userId)
                .title(title)
                .number(number)
                .balance(balance)
                .pm(0)
                .remark(remark)
                .build();
        sysUserBillMapper.insertSysUserBill(userBill);
    }


    @Override
    public int exchange(SysMemberDTO dto) {
        SysMember sysMember = sysMemberMapper.selectSysMemberById(dto.getId());
        if (sysMember.getIntegral().compareTo(dto.getIntegral()) < 0) {
            throw new RuntimeException("积分不足");
        }
        BigDecimal surplusIntegral = sysMember.getIntegral().subtract(dto.getIntegral());
        expend(dto.getId(), "会员兑换", dto.getIntegral(), surplusIntegral, "本次消耗" + dto.getIntegral() + "积分," + "兑换" + dto.getGiftName() + "成功。");
        sysMember.setIntegral(surplusIntegral);
        return sysMemberMapper.updateSysMember(sysMember);
    }

    @Override
    public List<SysUserBill> listByUserId(Long userId) {
        return sysUserBillMapper.listByUserId(userId);
    }

}
