package com.fs.hospital.service.impl;

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

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.HospitalMember;
import com.fs.hospital.domain.HospitalMemberBill;
import com.fs.hospital.mapper.HospitalMemberBillMapper;
import com.fs.hospital.mapper.HospitalMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalMemberRechargeMapper;
import com.fs.hospital.domain.HospitalMemberRecharge;
import com.fs.hospital.service.IHospitalMemberRechargeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户充值Service业务层处理
 *
 * @author fs
 * @date 2024-03-04
 */
@Service
public class HospitalMemberRechargeServiceImpl implements IHospitalMemberRechargeService
{
    @Autowired
    private HospitalMemberRechargeMapper hospitalMemberRechargeMapper;
    @Autowired
    private HospitalMemberBillMapper hospitalMemberBillMapper;
    @Autowired
    private HospitalMemberMapper hospitalMemberMapper;

    /**
     * 查询用户充值
     *
     * @param rechargeId 用户充值主键
     * @return 用户充值
     */
    @Override
    public HospitalMemberRecharge selectHospitalMemberRechargeByRechargeId(Long rechargeId)
    {
        return hospitalMemberRechargeMapper.selectHospitalMemberRechargeByRechargeId(rechargeId);
    }

    /**
     * 查询用户充值列表
     *
     * @param hospitalMemberRecharge 用户充值
     * @return 用户充值
     */
    @Override
    public List<HospitalMemberRecharge> selectHospitalMemberRechargeList(HospitalMemberRecharge hospitalMemberRecharge)
    {
        return hospitalMemberRechargeMapper.selectHospitalMemberRechargeList(hospitalMemberRecharge);
    }

    /**
     * 新增用户充值
     *
     * @param hospitalMemberRecharge 用户充值
     * @return 结果
     */
    @Override
    @Transactional
    public R insertHospitalMemberRecharge(HospitalMemberRecharge hospitalMemberRecharge)
    {
        HospitalMember hospitalMember = hospitalMemberMapper.selectHospitalMemberByMemberId(hospitalMemberRecharge.getMemberId());
        if (hospitalMember!=null){
            hospitalMemberRecharge.setCreateTime(DateUtils.getNowDate());
            if (hospitalMemberRechargeMapper.insertHospitalMemberRecharge(hospitalMemberRecharge)>0){
                hospitalMember.setBalance(hospitalMember.getBalance().add(hospitalMemberRecharge.getMoney()));
                hospitalMemberMapper.updateHospitalMember(hospitalMember);
                HospitalMemberBill bill = new HospitalMemberBill();
                bill.setMoney(hospitalMemberRecharge.getMoney());
                bill.setMemberId(hospitalMember.getMemberId());
                bill.setHospitalId(hospitalMember.getHospitalId());
                bill.setBillType(1);
                bill.setRemark(hospitalMemberRecharge.getRemark());
                bill.setTitle("会员充值入账");
                bill.setBalance(hospitalMember.getBalance());
                bill.setBusinessId(hospitalMemberRecharge.getRechargeId().toString());
                bill.setCreateTime(new Date());
                hospitalMemberBillMapper.insertHospitalMemberBill(bill);
                return R.ok();
            }
        }
        return R.error();
    }

    /**
     * 修改用户充值
     *
     * @param hospitalMemberRecharge 用户充值
     * @return 结果
     */
    @Override
    public int updateHospitalMemberRecharge(HospitalMemberRecharge hospitalMemberRecharge)
    {
        return hospitalMemberRechargeMapper.updateHospitalMemberRecharge(hospitalMemberRecharge);
    }

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

    /**
     * 删除用户充值信息
     *
     * @param rechargeId 用户充值主键
     * @return 结果
     */
    @Override
    public int deleteHospitalMemberRechargeByRechargeId(Long rechargeId)
    {
        return hospitalMemberRechargeMapper.deleteHospitalMemberRechargeByRechargeId(rechargeId);
    }
}
