package com.ruoyi.care.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.care.domain.CareCompanies;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.care.mapper.CareNursesMapper;
import com.ruoyi.care.domain.CareNurses;
import com.ruoyi.care.service.ICareNursesService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理人员Service业务层处理
 * 
 * @author qy
 * @date 2024-11-26
 */
@Service
public class CareNursesServiceImpl implements ICareNursesService 
{
    @Autowired
    private CareNursesMapper careNursesMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    /**
     * 查询护理人员
     * 
     * @param nurseId 护理人员主键
     * @return 护理人员
     */
    @Override
    public CareNurses selectCareNursesByNurseId(Long nurseId)
    {
        return careNursesMapper.selectCareNursesByNurseId(nurseId);
    }

    /**
     * 查询护理人员列表
     * 
     * @param careNurses 护理人员
     * @return 护理人员
     */
    @Override
    public List<CareNurses> selectCareNursesList(CareNurses careNurses)
    {
        return careNursesMapper.selectCareNursesList(careNurses);
    }

    /**
     * 新增护理人员
     * 
     * @param careNurses 护理人员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCareNurses(CareNurses careNurses)
    {
        //新增用户信息
        SysUser user = new SysUser();
        user.setUserName(careNurses.getLoginName());
        user.setNickName(careNurses.getName());
        if(StringUtils.isNotEmpty(careNurses.getPassword())){
            user.setPassword(SecurityUtils.encryptPassword(careNurses.getPassword()));
        }
        user.setUserType("02");//护理人员
        userMapper.insertUser(user);

        // 新增护理人员信息
        careNurses.setCreateTime(DateUtils.getNowDate());
        careNurses.setUserId(user.getUserId());
        int rows =careNursesMapper.insertCareNurses(careNurses);


        //获取护理人员权限nurse
        SysRole role=roleMapper.checkRoleKeyUnique("nurse");
        // 新增用户与角色管理
        insertUserRole(user.getUserId(), role.getRoleId());
        careNurses.setCreateTime(DateUtils.getNowDate());
        return rows;
    }
    /**
     * 新增用户角色信息
     *
     * @param userId 用户ID
     * @param roleId 角色组
     */
    public void insertUserRole(Long userId,Long roleId)
    {
        if (StringUtils.isNotEmpty(roleId.toString()))
        {
            // 新增用户与角色管理
            List<SysUserRole> list = new ArrayList<SysUserRole>(0);
            SysUserRole ur = new SysUserRole();
            ur.setUserId(userId);
            ur.setRoleId(roleId);
            list.add(ur);
            userRoleMapper.batchUserRole(list);
        }
    }
    /**
     * 修改护理人员
     * 
     * @param careNurses 护理人员
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCareNurses(CareNurses careNurses)
    {
        Long userId = careNurses.getUserId();
        // 修改用户信息
        SysUser user=userMapper.selectUserById(userId);
        user.setUserName(careNurses.getLoginName());
        user.setNickName(careNurses.getName());
        if(StringUtils.isNotEmpty(careNurses.getPassword())){
            user.setPassword(SecurityUtils.encryptPassword(careNurses.getPassword()));
        }
        userMapper.updateUser(user);

        careNurses.setUpdateTime(DateUtils.getNowDate());
        return careNursesMapper.updateCareNurses(careNurses);
    }

    /**
     * 批量删除护理人员
     * 
     * @param nurseIds 需要删除的护理人员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCareNursesByNurseIds(Long[] nurseIds)
    {
        Long[] userIds=new Long[nurseIds.length];
        for (int i = 0; i < nurseIds.length; i++) {
            Long nurseId = nurseIds[i];
            CareNurses careNurses=careNursesMapper.selectCareNursesByNurseId(nurseId);
            userIds[i]=careNurses.getUserId();
        }
        // 删除用户与角色关联
        userRoleMapper.deleteUserRole(userIds);
        // 删除用户
        userMapper.deleteUserByIds(userIds);
        return careNursesMapper.deleteCareNursesByNurseIds(nurseIds);
    }

    /**
     * 删除护理人员信息
     * 
     * @param nurseId 护理人员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCareNursesByNurseId(Long nurseId)
    {
        CareNurses careNurses=careNursesMapper.selectCareNursesByNurseId(nurseId);
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(careNurses.getUserId());
        // 删除用户
        userMapper.deleteUserById(careNurses.getUserId());

        return careNursesMapper.deleteCareNursesByNurseId(nurseId);
    }
}
