package com.fs.hospital.service.impl;

import java.util.List;

import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.HospitalMember;
import com.fs.hospital.vo.HospitalMemberIntegralLogsListHVO;
import com.fs.hospital.mapper.HospitalMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalMemberIntegralLogsMapper;
import com.fs.hospital.domain.HospitalMemberIntegralLogs;
import com.fs.hospital.service.IHospitalMemberIntegralLogsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 积分记录Service业务层处理
 *
 * @author fs
 * @date 2024-03-04
 */
@Service
public class HospitalMemberIntegralLogsServiceImpl implements IHospitalMemberIntegralLogsService
{
    @Autowired
    private HospitalMemberIntegralLogsMapper hospitalMemberIntegralLogsMapper;
    @Autowired
    HospitalMemberMapper hospitalMemberMapper;
    /**
     * 查询积分记录
     *
     * @param id 积分记录主键
     * @return 积分记录
     */
    @Override
    public HospitalMemberIntegralLogs selectHospitalMemberIntegralLogsById(Long id)
    {
        return hospitalMemberIntegralLogsMapper.selectHospitalMemberIntegralLogsById(id);
    }

    /**
     * 查询积分记录列表
     *
     * @param hospitalMemberIntegralLogs 积分记录
     * @return 积分记录
     */
    @Override
    public List<HospitalMemberIntegralLogs> selectHospitalMemberIntegralLogsList(HospitalMemberIntegralLogs hospitalMemberIntegralLogs)
    {
        return hospitalMemberIntegralLogsMapper.selectHospitalMemberIntegralLogsList(hospitalMemberIntegralLogs);
    }

    /**
     * 新增积分记录
     *
     * @param hospitalMemberIntegralLogs 积分记录
     * @return 结果
     */
    @Override
    public int insertHospitalMemberIntegralLogs(HospitalMemberIntegralLogs hospitalMemberIntegralLogs)
    {
        hospitalMemberIntegralLogs.setCreateTime(DateUtils.getNowDate());
        return hospitalMemberIntegralLogsMapper.insertHospitalMemberIntegralLogs(hospitalMemberIntegralLogs);
    }

    /**
     * 修改积分记录
     *
     * @param hospitalMemberIntegralLogs 积分记录
     * @return 结果
     */
    @Override
    public int updateHospitalMemberIntegralLogs(HospitalMemberIntegralLogs hospitalMemberIntegralLogs)
    {
        return hospitalMemberIntegralLogsMapper.updateHospitalMemberIntegralLogs(hospitalMemberIntegralLogs);
    }

    /**
     * 批量删除积分记录
     *
     * @param ids 需要删除的积分记录主键
     * @return 结果
     */
    @Override
    public int deleteHospitalMemberIntegralLogsByIds(Long[] ids)
    {
        return hospitalMemberIntegralLogsMapper.deleteHospitalMemberIntegralLogsByIds(ids);
    }

    /**
     * 删除积分记录信息
     *
     * @param id 积分记录主键
     * @return 结果
     */
    @Override
    public int deleteHospitalMemberIntegralLogsById(Long id)
    {
        return hospitalMemberIntegralLogsMapper.deleteHospitalMemberIntegralLogsById(id);
    }

    @Override
    public List<HospitalMemberIntegralLogsListHVO> selectHospitalMemberIntegralLogsListPVO(HospitalMemberIntegralLogs hospitalMemberIntegralLogs) {
        return hospitalMemberIntegralLogsMapper.selectHospitalMemberIntegralLogsListPVO(hospitalMemberIntegralLogs);
    }
    @Transactional
    @Override
    public int addHospitalMemberIntegral(HospitalMemberIntegralLogs hospitalMemberIntegralLogs) {

        HospitalMember hospitalMember = hospitalMemberMapper.selectHospitalMemberByMemberId(hospitalMemberIntegralLogs.getMemberId());
        if (hospitalMember==null){
            return 0;
        }
            Long integral = hospitalMember.getIntegral();
            HospitalMember member = new HospitalMember();
            member.setMemberId(hospitalMember.getMemberId());
            member.setIntegral(integral+hospitalMemberIntegralLogs.getIntegral());
            hospitalMemberMapper.updateHospitalMember(member);
            HospitalMemberIntegralLogs logs = new HospitalMemberIntegralLogs();
            logs.setIntegral(hospitalMemberIntegralLogs.getIntegral());
            logs.setRemark(hospitalMemberIntegralLogs.getRemark());
            logs.setMemberId(hospitalMemberIntegralLogs.getMemberId());
            logs.setBalance(member.getIntegral());
            logs.setLogType(1);
            logs.setHospitalId(hospitalMemberIntegralLogs.getHospitalId());
            logs.setCreateTime(DateUtils.getNowDate());
            logs.setHospitalUserId(hospitalMemberIntegralLogs.getHospitalUserId());
           return hospitalMemberIntegralLogsMapper.insertHospitalMemberIntegralLogs(logs);
    }


    @Transactional
    public int subHospitalMemberIntegral(HospitalMemberIntegralLogs hospitalMemberIntegralLogs) {

        HospitalMember hospitalMember = hospitalMemberMapper.selectHospitalMemberByMemberId(hospitalMemberIntegralLogs.getMemberId());
        if (hospitalMember==null){
            return 0;
        }
        Long integral = hospitalMember.getIntegral();
        HospitalMember member = new HospitalMember();
        member.setMemberId(hospitalMember.getMemberId());
        member.setIntegral(integral-hospitalMemberIntegralLogs.getIntegral());
        if (integral-hospitalMemberIntegralLogs.getIntegral()<0){
            throw new CustomException("剩余积分不能小于0");
        }
        hospitalMemberMapper.updateHospitalMember(member);
        HospitalMemberIntegralLogs logs = new HospitalMemberIntegralLogs();
        logs.setIntegral(0-hospitalMemberIntegralLogs.getIntegral());
        logs.setRemark(hospitalMemberIntegralLogs.getRemark());
        logs.setMemberId(hospitalMemberIntegralLogs.getMemberId());
        logs.setHospitalId(hospitalMemberIntegralLogs.getHospitalId());
        logs.setBalance(member.getIntegral());
        logs.setLogType(2);
        logs.setCreateTime(DateUtils.getNowDate());
        logs.setHospitalUserId(hospitalMemberIntegralLogs.getHospitalUserId());
        return hospitalMemberIntegralLogsMapper.insertHospitalMemberIntegralLogs(logs);
    }
}
