package com.fastbee.rechargecard.service.impl;

import java.util.List;

import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.rechargecard.domain.UserRechargeCards;
import com.fastbee.rechargecard.domain.dto.RechargecardUser;
import com.fastbee.rechargecard.domain.dto.WeChatRecharge;
import com.fastbee.rechargecard.mapper.UserRechargeCardsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.rechargecard.mapper.NgUserRechargeRecordsMapper;
import com.fastbee.rechargecard.domain.NgUserRechargeRecords;
import com.fastbee.rechargecard.service.INgUserRechargeRecordsService;

/**
 * 用户充值记录Service业务层处理
 *
 * @author kerwincui
 * @date 2024-12-19
 */
@Service
public class NgUserRechargeRecordsServiceImpl implements INgUserRechargeRecordsService
{
    @Autowired
    private NgUserRechargeRecordsMapper ngUserRechargeRecordsMapper;
    @Autowired
    private UserRechargeCardsMapper userRechargeCardsMapper;

    @Override
    public NgUserRechargeRecords SelectRechargeRecodeByRechargeCode(String rechargeCode) {
        return ngUserRechargeRecordsMapper.SelectRechargeRecodeByRechargeCode(rechargeCode);
    }

    @Override
    public int insertNgUserRechargeRecordsWeChat(WeChatRecharge rechargecardUser) {
        // 根据id查询用户充值卡信息
        UserRechargeCards info = userRechargeCardsMapper.selectUserRechargeCardsByCardnumber(String.valueOf(rechargecardUser.getCardnumber()));
        if (info == null) {
            // 如果没有找到对应的记录，返回0或者一个错误码
            throw  new ServiceException("卡号不存在！");
        }
        NgUserRechargeRecords ngUserRechargeRecords=new NgUserRechargeRecords();
        if(info.getUserId()!=null)
        {
            ngUserRechargeRecords.setUserId(info.getUserId());
        }
        if(info.getUserName()!=null)
        {
            ngUserRechargeRecords.setUserName(info.getUserName());
        }
        ngUserRechargeRecords.setRechargeTime(DateUtils.getNowDate());
        ngUserRechargeRecords.setBalance(rechargecardUser.getAmount());
        ngUserRechargeRecords.setAmount(rechargecardUser.getAmount());
        ngUserRechargeRecords.setType(1);//微信
        ngUserRechargeRecords.setStatus(0);//生成订单
        ngUserRechargeRecords.setAreaCode(String.valueOf(rechargecardUser.getAreacode()));
        ngUserRechargeRecords.setCardNumber(String.valueOf(rechargecardUser.getCardnumber()));
        ngUserRechargeRecords.setRechargeCode(rechargecardUser.getRechargeCode());
        ngUserRechargeRecords.setSerialNumber(rechargecardUser.getDeviceNumber());
        return ngUserRechargeRecordsMapper.insertNgUserRechargeRecords(ngUserRechargeRecords);
    }

    /**
     * 查询用户充值记录
     *
     * @param
     * @return 用户充值记录
     */
    @Override
    public List<NgUserRechargeRecords> selectNgUserRechargeRecordsByCardNumber(NgUserRechargeRecords ngUserRechargeRecords)
    {
        return ngUserRechargeRecordsMapper.selectNgUserRechargeRecordsByCardNumber(ngUserRechargeRecords);
    }

    /**
     * 查询充值机充值记录
     *
     * @param
     * @return 用户充值记录
     */
    @Override
    public List<NgUserRechargeRecords> selectNgUserRechargeRecordsBySerialNumber(NgUserRechargeRecords ngUserRechargeRecords)
    {
        return ngUserRechargeRecordsMapper.selectNgUserRechargeRecordsBySerialNumber(ngUserRechargeRecords);
    }

    /**
     * 查询用户充值记录列表
     *
     * @param ngUserRechargeRecords 用户充值记录
     * @return 用户充值记录
     */
    @Override
    public List<NgUserRechargeRecords> selectNgUserRechargeRecordsList(NgUserRechargeRecords ngUserRechargeRecords)
    {
        return ngUserRechargeRecordsMapper.selectNgUserRechargeRecordsList(ngUserRechargeRecords);
    }

    /**
     * 新增用户充值记录
     *
     * @param ngUserRechargeRecords 用户充值记录
     * @return 结果
     */
    @Override
    public int insertNgUserRechargeRecords(NgUserRechargeRecords ngUserRechargeRecords)
    {
        ngUserRechargeRecords.setCreateTime(DateUtils.getNowDate());
        ngUserRechargeRecordsMapper.insertNgUserRechargeRecords(ngUserRechargeRecords);

        return ngUserRechargeRecordsMapper.insertNgUserRechargeRecords(ngUserRechargeRecords);
    }

    /**
     * 修改用户充值记录
     *
     * @param ngUserRechargeRecords 用户充值记录
     * @return 结果
     */
    @Override
    public int updateNgUserRechargeRecords(NgUserRechargeRecords ngUserRechargeRecords)
    {
        ngUserRechargeRecords.setUpdateTime(DateUtils.getNowDate());
        return ngUserRechargeRecordsMapper.updateNgUserRechargeRecords(ngUserRechargeRecords);
    }

    /**
     * 批量删除用户充值记录
     *
     * @param ids 需要删除的用户充值记录主键
     * @return 结果
     */
    @Override
    public int deleteNgUserRechargeRecordsByIds(Long[] ids)
    {
        return ngUserRechargeRecordsMapper.deleteNgUserRechargeRecordsByIds(ids);
    }

    /**
     * 删除用户充值记录信息
     *
     * @param id 用户充值记录主键
     * @return 结果
     */
    @Override
    public int deleteNgUserRechargeRecordsById(Long id)
    {
        return ngUserRechargeRecordsMapper.deleteNgUserRechargeRecordsById(id);
    }
}
