package com.fs.hospital.service.impl;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.utils.DateUtils;
import com.fs.hospital.domain.HospitalPatient;
import com.fs.hospital.mapper.HospitalPatientMapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.mapper.HospitalMemberMapper;
import com.fs.hospital.domain.HospitalMember;
import com.fs.hospital.service.IHospitalMemberService;

/**
 * 诊所会员Service业务层处理
 *
 * @author fs
 * @date 2024-03-04
 */
@Service
public class HospitalMemberServiceImpl implements IHospitalMemberService
{
    @Autowired
    private HospitalMemberMapper hospitalMemberMapper;
    @Autowired
    private HospitalPatientMapper hospitalPatientMapper;

    /**
     * 查询诊所会员
     *
     * @param memberId 诊所会员主键
     * @return 诊所会员
     */
    @Override
    public HospitalMember selectHospitalMemberByMemberId(Long memberId)
    {
        return hospitalMemberMapper.selectHospitalMemberByMemberId(memberId);
    }

    /**
     * 查询诊所会员列表
     *
     * @param hospitalMember 诊所会员
     * @return 诊所会员
     */
    @Override
    public List<HospitalMember> selectHospitalMemberList(HospitalMember hospitalMember)
    {
        return hospitalMemberMapper.selectHospitalMemberList(hospitalMember);
    }

    /**
     * 新增诊所会员
     *
     * @param hospitalMember 诊所会员
     * @return 结果
     */
    @Override
    public R insertHospitalMember(HospitalMember hospitalMember)
    {
        hospitalMember.setCreateTime(DateUtils.getNowDate());
        HospitalMember hospitalMember1 = hospitalMemberMapper.selectHospitalMemberByPatientId(hospitalMember.getPatientId());
        if (hospitalMember1!=null){
            return  R.error("该患者已绑定会员");
        }
        if (hospitalMemberMapper.insertHospitalMember(hospitalMember)>0){
            HospitalPatient patient = new HospitalPatient();
            patient.setPatientId(hospitalMember.getPatientId());
            patient.setMemberId(hospitalMember.getMemberId());
            hospitalPatientMapper.updateHospitalPatient(patient);
            return R.ok();
        }else {
           return R.error("创建失败");
        }

    }

    /**
     * 修改诊所会员
     *
     * @param hospitalMember 诊所会员
     * @return 结果
     */
    @Override
    public int updateHospitalMember(HospitalMember hospitalMember)
    {
        hospitalMember.setUpdateTime(DateUtils.getNowDate());
        return hospitalMemberMapper.updateHospitalMember(hospitalMember);
    }

    /**
     * 批量删除诊所会员
     *
     * @param memberIds 需要删除的诊所会员主键
     * @return 结果
     */
    @Override
    public int deleteHospitalMemberByMemberIds(Long[] memberIds)
    {
        return hospitalMemberMapper.deleteHospitalMemberByMemberIds(memberIds);
    }

    /**
     * 删除诊所会员信息
     *
     * @param memberId 诊所会员主键
     * @return 结果
     */
    @Override
    public int deleteHospitalMemberByMemberId(Long memberId)
    {
        return hospitalMemberMapper.deleteHospitalMemberByMemberId(memberId);
    }
}
