package com.fs.hospital.service.impl;

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

import com.fs.common.annotation.DataScope;
import com.fs.common.utils.StringUtils;

import com.fs.hospital.domain.*;
import com.fs.hospital.mapper.*;
import com.fs.hospital.vo.HospitalUserListHVO;
import com.fs.hospital.vo.HospitalUserHVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.hospital.service.IHospitalUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 员工信息Service业务层处理
 *
 * @author fs
 * @date 2024-02-26
 */
@Service
public class HospitalUserServiceImpl implements IHospitalUserService
{
    @Autowired
    private HospitalUserMapper hospitalUserMapper;

    @Autowired
    private HospitalUserPostMapper userPostMapper;

    @Autowired
    private HospitalUserRoleMapper userRoleMapper;
    @Autowired
    private HospitalRoleMapper roleMapper;

    @Autowired
    private HospitalPostMapper postMapper;

    /**
     * 查询员工信息
     *
     * @param userId 员工信息主键
     * @return 员工信息
     */
    @Override
    public HospitalUser selectHospitalUserByUserId(Long userId)
    {
        return hospitalUserMapper.selectHospitalUserByUserIdVO(userId);
    }

    /**
     * 查询员工信息列表
     *
     * @param hospitalUser 员工信息
     * @return 员工信息
     */
    @Override
    public List<HospitalUser> selectHospitalUserList(HospitalUser hospitalUser)
    {
        return hospitalUserMapper.selectHospitalUserList(hospitalUser);
    }

    /**
     * 新增员工信息
     *
     * @param hospitalUser 员工信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHospitalUser(HospitalUser hospitalUser)
    {
        // 新增用户信息
        int rows = hospitalUserMapper.insertHospitalUser(hospitalUser);
        // 新增用户岗位关联
        insertUserPost(hospitalUser);
        // 新增用户与角色管理
        insertUserRole(hospitalUser);
        return rows;
    }
    /**
     * 新增用户岗位信息
     *
     * @param user 用户对象
     */
    public void insertUserPost(HospitalUser user)
    {
        Long[] posts = user.getPostIds();
        if (StringUtils.isNotNull(posts))
        {
            // 新增用户与岗位管理
            List<HospitalUserPost> list = new ArrayList<HospitalUserPost>();
            for (Long postId : posts)
            {
                HospitalUserPost up = new HospitalUserPost();
                up.setUserId(user.getUserId());
                up.setPostId(postId);
                list.add(up);
            }
            if (list.size() > 0)
            {
                userPostMapper.batchUserPost(list);
            }
        }
    }
    /**
     * 新增用户角色信息
     *
     * @param user 用户对象
     */
    public void insertUserRole(HospitalUser user)
    {
        Long[] roles = user.getRoleIds();
        if (StringUtils.isNotNull(roles))
        {
            // 新增用户与角色管理
            List<HospitalUserRole> list = new ArrayList<HospitalUserRole>();
            for (Long roleId : roles)
            {
                HospitalUserRole ur = new HospitalUserRole();
                ur.setUserId(user.getUserId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0)
            {
                userRoleMapper.batchUserRole(list);
            }
        }
    }

    /**
     * 修改员工信息
     *
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHospitalUser(HospitalUser user)
    {
        Long userId = user.getUserId();
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        // 删除用户与岗位关联
        userPostMapper.deleteUserPostByUserId(userId);
        // 新增用户与岗位管理
        insertUserPost(user);
        return hospitalUserMapper.updateHospitalUser(user);
    }

    /**
     * 批量删除员工信息
     *
     * @param userIds 需要删除的员工信息主键
     * @return 结果
     */
    @Override
    public int deleteHospitalUserByUserIds(Long[] userIds)
    {
        return hospitalUserMapper.deleteHospitalUserByUserIds(userIds);
    }

    /**
     * 删除员工信息信息
     *
     * @param userId 员工信息主键
     * @return 结果
     */
    @Override
    public int deleteHospitalUserByUserId(Long userId)
    {
        return hospitalUserMapper.deleteHospitalUserByUserId(userId);
    }

    @Override
    public HospitalUser selectUserByUserName(String username) {
        return hospitalUserMapper.selectHospitalUserByUserName(username);
    }

    @Override
    public String checkUserNameUnique(String userName) {
        return hospitalUserMapper.checkUserNameUnique(userName)>0?"1":"0";
    }

    @Override
    @DataScope(deptAlias = "u",userAlias = "u")
    public List<HospitalUser> getUserListByDeptId(HospitalUser user) {
        return hospitalUserMapper.getUserListByDeptId(user);
    }

    @Override
    public Integer selectHospitalUserCountByHospitalId(Long hospitalId) {
        return hospitalUserMapper.selectHospitalUserCountByHospitalId(hospitalId);
    }

    @Override
    public int resetUserPwdByUserId(Long userId, String encryptPassword) {
        return hospitalUserMapper.resetUserPwdByUserId(userId,encryptPassword);
    }

    @Override
    public List<HospitalUserHVO> selectMyUserList(HospitalUser user) {
        return hospitalUserMapper.selectMyUserList(user);
    }

    @Override
    public String selectUserRoleGroup(String username) {
        List<HospitalRole> list = roleMapper.selectRolesByUserName(username);
        StringBuffer idsStr = new StringBuffer();
        for (HospitalRole role : list)
        {
            idsStr.append(role.getRoleName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString()))
        {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }

    @Override
    public String selectUserPostGroup(String username) {
        List<HospitalPost> list = postMapper.selectPostsByUserName(username);
        StringBuffer idsStr = new StringBuffer();
        for (HospitalPost post : list)
        {
            idsStr.append(post.getPostName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString()))
        {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }

    @Override
    public int updateUserProfile(HospitalUser user) {
        return hospitalUserMapper.updateHospitalUser(user);
    }

    @Override
    public int resetUserPwd(String userName, String encryptPassword) {
        return hospitalUserMapper.resetUserPwd(userName, encryptPassword);
    }

    @Override
    public int updateUserAvatar(String username, String avatar) {
        return hospitalUserMapper.updateUserAvatar(username, avatar) ;
    }

    @Override
    public List<HospitalUserListHVO> selectHospitalUserListVO(HospitalUser user) {
        return hospitalUserMapper.selectHospitalUserListVO(user);
    }
}
