package com.ruoyi.web.work.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.web.work.api.config.TokenServices;
import com.ruoyi.web.work.api.util.AppUtil;
import com.ruoyi.web.work.domain.BindUser;
import com.ruoyi.web.work.domain.Patient;
import com.ruoyi.web.work.domain.User;
import com.ruoyi.web.work.domain.dto.BindDto;
import com.ruoyi.web.work.mapper.BindUserMapper;
import com.ruoyi.web.work.service.IBindUserService;
import com.ruoyi.web.work.service.IPatientService;
import com.ruoyi.web.work.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.ServerException;
import java.util.List;

/**
 * @author lsw
 * @date 2024-07-26
 */
@Service
public class BindUserServiceImpl extends ServiceImpl<BindUserMapper, BindUser> implements IBindUserService {
    @Autowired
    private BindUserMapper bindUserMapper;

    @Autowired
    IUserService userService;

    @Autowired
    private TokenServices tokenService;

    @Autowired
    private IPatientService patientService;

    @Override
    public List<BindUser> selectList(BindUser bindUser) {
        return bindUserMapper.selectList(bindUser);
    }

    @Transactional
    @Override
    public AjaxResult bind(BindDto dto) throws ServerException {
        Patient patient = bindUserMapper.selectPatient(dto);
        if (patient == null) {
            return AjaxResult.error("预留的信息不存在或输入错误");
        }
        BindUser bindUser = new BindUser();
        bindUser.setPatientId(patient.getId());
        bindUser.setUserId(AppUtil.getUser().getId());
        bindUser.setPatientName(patient.getName());
        bindUser.setRelationship(dto.getRelationship());
        if (bindUserMapper.selectBindUser(bindUser) != null) {
            return AjaxResult.error("你已绑定过该就诊人");
        }
        //如果只有一个就诊人直接设置成当前就诊人
        List<BindUser> bindUserList = bindUserMapper.selectList(new BindUser().setUserId(AppUtil.getUser().getId()));
        if (bindUserList.size() == 0) {
            bindUser.setState(1);
            User user = AppUtil.getUser();
            user.setPatientId(patient.getId());
            user.setPatientName(patient.getName());
            user.setRelationship(dto.getRelationship());
            if (!userService.updateById(user)) {
                throw new ServerException("缓存信息过期,绑定就诊人失败,请重新登录再试");
            }
            tokenService.setLoginUser(user);
        }
        if (!save(bindUser)) {
            throw new ServerException("绑定就诊人失败");
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult remove(Long id) throws ServerException {
        BindUser bindUser = getById(id);
        if (bindUser == null || !bindUser.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("数据不存在或非法操作");
        }
        if (!removeById(id)) {
            throw new ServerException("删除就诊人失败");
        }
        //如果移除的是当前就诊人删除其缓存信息
        if (bindUser.getPatientId().equals(AppUtil.getUser().getPatientId())) {
            User user = AppUtil.getUser();
            user.setPatientName("");
            user.setRelationship("");
            user.setPatientId(0l);
            userService.updateById(user);
            tokenService.setLoginUser(user);
        }
        return AjaxResult.success();
    }

    @Transactional
    @Override
    public AjaxResult change(Long id) throws ServerException {
        BindUser bindUser = getById(id);
        if (bindUser == null || !bindUser.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("数据不存在或非法操作");
        }
        bindUserMapper.changeBindUser(bindUser);
        bindUser.setState(1);
        updateById(bindUser);
        User user = AppUtil.getUser();
        user.setPatientId(bindUser.getPatientId());
        user.setPatientName(bindUser.getPatientName());
        user.setRelationship(bindUser.getRelationship());
        if (!userService.updateById(user)) {
            throw new ServerException("切换就诊人失败");
        }
        tokenService.setLoginUser(userService.getById(AppUtil.getUser().getId()).setToken(AppUtil.getUser().getToken()));
        return AjaxResult.success();
    }

    @Override
    public AjaxResult bindDetail(Long id) {
        BindUser bindUser = getById(id);
        if (bindUser == null || !bindUser.getUserId().equals(AppUtil.getUser().getId())) {
            return AjaxResult.error("数据不存在或非法操作");
        }
        Patient patient = patientService.getById(bindUser.getPatientId());
        return AjaxResult.success(patient);
    }
}
