package com.ann.demo.service.impl;

import com.ann.demo.entity.filing.MessageSubordinate;
import com.ann.demo.entity.normalEntity.Department;
import com.ann.demo.entity.normalEntity.EMRDictionary;
import com.ann.demo.entity.normalEntity.User;
import com.ann.demo.repository.MessageSubordinateRepository;
import com.ann.demo.repository.UserRepository;
import com.ann.demo.service.DepartmentService;
import com.ann.demo.service.MessageSubordinateService;
import com.ann.demo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Date 2019/7/9 16:43
 * @Created by  ljx
 */
@Service
public class UserServiceImpl implements UserService {

    static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Autowired
    UserRepository userRepository;

    @Autowired
    EMRDictionaryServiceImpl emrDictionaryService;

    @Override
    public void save(User user) {
        //打日志信息
        try {
            // 获取科室  根据科室类型区分临床和非临床，临床的然后有资质的就是医生，没有资质的就是护士
            Integer roleId = 0;
            if (user.getUSER_DEPT() != null) {
                EMRDictionary emrDictionary = emrDictionaryService.findEMRDictionary(user.getUSER_DEPT());
                if (emrDictionary != null) {
                    if (emrDictionary.getCLINIC_ATTR() == 0) {
                        if (user.getEDUCATION_FLAG() == 0) { // 无资质 护士
                            roleId = 1;
                        } else { // 有资质 为 1 医生
                            roleId = 2;
                        }
                    }
                }
            }
            user.setUSER_TYPE(roleId);
            userRepository.save(user);
        } catch (Exception e) {
            logger.error("出错咯！错误信息:" + e);
        }
    }

    @Override
    public void delete(User user) {
        //打日志信息
        try {
            userRepository.delete(user);
        } catch (Exception e) {
            logger.error("出错咯！错误信息:" + e);
        }
    }
}
