package com.ym.mec.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysRole;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.EmployeeDto;
import com.ym.mec.biz.dal.dto.EmployeeLevelDto;
import com.ym.mec.biz.dal.dto.SysUserDto;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.EmployeeOperateEnum;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.dal.enums.ParamEnum;
import com.ym.mec.biz.dal.page.EmployeeQueryInfo;
import com.ym.mec.biz.dal.page.UserBasicQueryInfo;
import com.ym.mec.biz.service.*;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.entity.ImGroupModel;
import com.ym.mec.common.entity.ImResult;
import com.ym.mec.common.entity.ImUserModel;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.WrapperUtil;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.common.tenant.TenantContextHolder;
import com.ym.mec.im.ImFeignService;
import com.ym.mec.im.entity.GroupMember;
import com.ym.mec.im.entity.GroupModel;
import com.ym.mec.util.collection.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.FileNotFoundException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Integer, Employee> implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private ImFeignService imFeignService;
    @Autowired
    private ImGroupMemberService imGroupMemberService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private SysUserCashAccountDao sysUserCashAccountDao;
    @Autowired
    private ClassGroupDao classGroupDao;
    @Autowired
    private ImUserFriendService imUserFriendService;
    @Autowired
    private OrganizationDao organizationDao;
    @Autowired
    private ImGroupDao imGroupDao;
    @Autowired
    private CooperationOrganDao cooperationOrganDao;
    @Autowired
    private OaUserService oaUserService;
    @Autowired
    private TenantInfoService tenantInfoService;
    @Autowired
    private SysEmployeePositionService employeePositionService;
    @Autowired
    private SysEmployeePositionDao employeePositionDao;
    @Autowired
    private SysUserTenantService sysUserTenantService;
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public BaseDAO<Integer, Employee> getDAO() {
        return employeeDao;
    }

    public static void checkTenantId(Integer tenantId, Integer targetTenantId) {
        //如果机构不一致，不允许跨机构注册
        if (tenantId != null && tenantId != 0 && !tenantId.equals(targetTenantId)) {
            throw new LockedException("用户已注册");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Employee employee) {
        RBucket<String> bucket = redissonClient.getBucket("add_employee_lock" + employee.getPhone());
        if (!bucket.trySet(employee.getPhone(), 1l, TimeUnit.SECONDS)) {
            throw new BizException("请勿重复提交");
        }
        SysUser user = teacherDao.getUserWithPhone(employee.getPhone());
        Integer tenantId = employee.getTenantId();
        if (user != null && user.getId() != null) {
            checkTenantId(tenantId, user.getTenantId());
            Employee employee1 = employeeDao.get(user.getId());
            if (employee1 == null) {
                if (StringUtils.isEmpty(user.getPassword())) {
                    // 修改 gym + 联系人手机后4位
                    int phoneStrLen = employee.getPhone().length();
                    employee.setPassword(new BCryptPasswordEncoder().encode("gym" + employee.getPhone().substring(phoneStrLen - 4, phoneStrLen)));
                    // employee.setPassword(new BCryptPasswordEncoder().encode(ParamEnum.INIT_PASSWORD.getCode().toString()));
                }
                employee.setUserId(user.getId());
                employee.setId(user.getId());
                employeeDao.insert(employee);
                //新增用户角色
                employeeDao.batchAddEmployeeRole(user.getId(), employee.getRoleIds(), tenantId);
                //新增用户职位
                employeePositionService.batchAddPosition(employee.getId(), employee.getPositionIds(), tenantId);
                employee.setUserType(user.getUserType() + ",SYSTEM");
                teacherDao.updateUser(employee);
                return;
            } else {
                throw new BizException("员工已存在");
            }
        }
        if (StringUtils.isBlank(employee.getPassword())) {
            int phoneStrLen = employee.getPhone().length();
            employee.setPassword(new BCryptPasswordEncoder().encode("gym" + employee.getPhone().substring(phoneStrLen - 4, phoneStrLen)));
        }
        employee.setUserType("SYSTEM");
        teacherDao.addSysUser(employee);
        employee.setUserId(employee.getId());
        employeeDao.insert(employee);
        //新增用户角色
        employeeDao.batchAddEmployeeRole(employee.getId(), employee.getRoleIds(), tenantId);
        //新增用户职位
        employeePositionService.batchAddPosition(employee.getId(), employee.getPositionIds(), tenantId);
        //添加用户现金账户
        sysUserCashAccountDao.insert(new SysUserCashAccount(employee.getId(), tenantId, "CNY"));
        ImResult imResult = imFeignService.register(new ImUserModel(employee.getId().toString(), employee.getUsername(), null));
        employee.setImToken(imResult.getToken());
        teacherDao.updateUser(employee);
        if (user != null) {
            employee.setAvatar(user.getAvatar());
        }
        Integer userId = employee.getId();
        if(Objects.nonNull(employee.getCreateTenantUserId())){
            userId = employee.getCreateTenantUserId();
        }
        //添加平台用户和机构的关系
        addUserTenant(employee.getTenantIds(), tenantId, userId);
        //添加到OA
        oaUserService.addOaUser(employee);
    }

    /**
     * 添加平台用户和机构的关系
     *
     * @param tenantIds    机构id集合
     * @param userTenantId 用户当前机构id
     * @param userId       用户id
     */
    private void addUserTenant(List<Integer> tenantIds, Integer userTenantId, Integer userId) {
        if (Objects.nonNull(userTenantId) && userTenantId == -1) {
            if (CollectionUtils.isEmpty(tenantIds)) {
                throw new BizException("平台账号必须指定一个机构！");
            }
            Date now = new Date();
            tenantIds.forEach(t -> {
                SysUserTenant userTenant = new SysUserTenant();
                userTenant.setUserId(userId);
                userTenant.setTenantId(t);
                userTenant.setCreatedTime(now);
                try {
                    sysUserTenantService.save(userTenant);
                } catch (DuplicateKeyException ignored) {
                }
            });
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEmployee(Employee employee) {
        Employee employee1 = employeeDao.get(employee.getId());
        if (null == employee1) {
            throw new BizException("员工信息不存在");
        }
        employee.setUserId(employee.getId());
        SysUser sysUser = teacherDao.getUserWithPhone(employee.getPhone());
        if (StringUtils.isNotEmpty(employee.getPhone())) {
            if (sysUser != null && !sysUser.getId().equals(employee.getUserId())) {
                throw new BizException("手机号已存在");
            }
            if (sysUser == null) {
                sysUser = sysUserFeignService.queryUserInfo();
            }
        }

        // 同步更新机构手机号 如果它存在  138xxx -> 159xxx
        SysUser user = teacherDao.getUser(employee.getId());
        if (!Objects.equals(user.getPhone(), employee.getPhone())) {
            tenantInfoService.updatePhone(employee.getPhone(), user.getPhone());
        }

        employeeDao.update(employee);
        //新增用户角色
        Integer tenantId = TenantContextHolder.getTenantId();
        if(employee.getRoleIds() != null && employee.getRoleIds().size() > 0){
            //删除当前用户角色
            employeeDao.delEmployeeRole(employee.getId());
            employeeDao.batchAddEmployeeRole(employee.getId(), employee.getRoleIds(), tenantId);
        }
        if(Objects.nonNull(employee.getPositionIds()) && employee.getPositionIds().size() > 0){
            //删除用户职位
            employeePositionService.delPosition(employee.getId(), tenantId);
            //添加职位
            employeePositionService.batchAddPosition(employee.getId(), employee.getPositionIds(), tenantId);
        }
        teacherDao.updateUser(employee);
        imGroupDao.updateNickname(employee.getUserId(), employee.getRealName());
        imGroupDao.updateUserFriendNickname(employee.getUserId(), employee.getRealName());
        imFeignService.update(new ImUserModel(employee.getUserId().toString(), employee.getRealName(), sysUser.getAvatar()));

        employee.setAvatar(sysUser.getAvatar());
        //更新机构信息
        TenantInfo tenantInfo = tenantInfoService.getOne(new WrapperUtil<TenantInfo>()
                .queryWrapper().eq("user_id_", employee.getId()));
        if (Objects.nonNull(tenantInfo) && !Objects.equals(tenantInfo.getPhone(), employee.getPhone())) {
            tenantInfo.setPhone(employee.getPhone());
            tenantInfo.setUpdatedTime(new Date());
            tenantInfo.setUpdatedBy(sysUser.getId());
            tenantInfoService.updateById(tenantInfo);
        }
        //更新用户与机构的关系
        sysUserTenantService.deleteByUserId(employee.getId());
        addUserTenant(employee.getTenantIds(), employee.getTenantId(), employee.getId());
        ExecutorService executor = Executors.newCachedThreadPool();
        CompletableFuture.runAsync(()->{
            try {
                //更新OA信息
                oaUserService.updateOaUser(employee);
            } catch (Exception e) {
                e.printStackTrace();
            }
        },executor);
    }

    @Override
    public PageInfo queryEmployByOrganId(EmployeeQueryInfo queryInfo) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null) {
            throw new BizException("获取用户信息失败");
        }
        PageInfo<EmployeeDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);
        List<EmployeeDto> dataList = null;
        int count = employeeDao.queryEmployByOrganIdCount(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = employeeDao.queryEmployByOrganId(params);
            Integer tenantId = TenantContextHolder.getTenantId();
            List<Integer> userIds = dataList.stream().map(e -> e.getId()).collect(Collectors.toList());
            Map<Integer, String> positionMap = MapUtil.convertMybatisMap(employeePositionDao.queryPositionMap(userIds, tenantId));
            Map<Integer, String> positionNameMap = MapUtil.convertMybatisMap(employeePositionDao.queryPositionNameMap(userIds, tenantId));
            List<Integer> allOrganIds = new ArrayList<>();
            for (EmployeeDto employeeDto : dataList) {
                employeeDto.setPositionIds(positionMap.get(employeeDto.getId()));
                employeeDto.setPositionNames(positionNameMap.get(employeeDto.getId()));
                //平台账户查询所拥有的机构
                if(Objects.nonNull(queryInfo.getTenantId()) && queryInfo.getTenantId() == -1){
                    employeeDto.setUserTenantList(sysUserTenantService.queryUserTenant(employeeDto.getId()));
                }
                if (StringUtils.isBlank(employeeDto.getOrganIdStr())) {
                    continue;
                }
                List<Integer> organIds = Arrays.stream(employeeDto.getOrganIdStr().split(",")).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
                allOrganIds.addAll(organIds);
            }
            if (!CollectionUtils.isEmpty(allOrganIds)) {
                List<Organization> allOrgans = organizationDao.findOrgans(allOrganIds);
                for (EmployeeDto employeeDto : dataList) {
                    if (StringUtils.isBlank(employeeDto.getOrganIdStr())) {
                        continue;
                    }
                    List<Integer> organIds = Arrays.stream(employeeDto.getOrganIdStr().split(",")).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
                    List<Organization> organs = allOrgans.stream().filter(organ -> organIds.contains(organ.getId())).collect(Collectors.toList());
                    List<String> organNames = organs.stream().map(Organization::getName).collect(Collectors.toList());
                    employeeDto.setOrganNameList(organNames);
                    employeeDto.setOrganIdList(organIds.stream().mapToLong(Long::valueOf).boxed().collect(Collectors.toList()));
                }
            }
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void employeeOperate(Integer employeeId, EmployeeOperateEnum operate) {
        Employee employee = employeeDao.get(employeeId);
        SysUser sysUser = teacherDao.getUser(employeeId);
        switch (operate) {
            case RESET_PASSWORD:
                employeeDao.updatePassword(employeeId, new BCryptPasswordEncoder().encode(ParamEnum.INIT_PASSWORD.getCode().toString()));
                break;
            case LOCK_UNLOCK:
                if (employee != null && employee.getDemissionDate() != null && sysUser.getLockFlag() == 1) {
                    throw new BizException("操作失败：离职员工不允许解冻");
                }
                employeeDao.updateUserLockStatus(employeeId);
                if (sysUser.getUserType().contains("TEACHER")) {
                    sysUserFeignService.exitByPhone("teacher", sysUser.getPhone());
                } else if (sysUser.getUserType().contains("SYSTEM")) {
                    sysUserFeignService.exitByPhone("system", sysUser.getPhone());
                }
                break;
            case DEMISSION:
                employeeDao.updateUserLock(employeeId, 1);
                employeeDao.updateUserDemissionDate(employeeId);
                sysUserFeignService.exitByPhone("system", sysUser.getPhone());

                //更新OA信息
                oaUserService.delOaUser(employee);
                break;
            case REINSTATE:
                //离职，如果有关联课程组教务老师，
                employeeDao.updateUserDemissionDate(employeeId);

                //更新OA信息
                oaUserService.reInsertOaUser(employee);
                break;
        }
    }

    @Override
    public HashMap<String, Integer> hasCourseGroupRelation(Integer employeeId) {
        Integer hasCourseGroupRelation = employeeDao.hasCourseGroupRelation(employeeId);
        Integer hasCourseSchedule = employeeDao.hasCourseSchedule(employeeId);
        HashMap<String, Integer> map = new HashMap<>(2);
        map.put("hasCourseGroupRelation", hasCourseGroupRelation);
        map.put("hasCourseSchedule", hasCourseSchedule);
        return map;
    }

    @Override
    public void updateEducationTeacherId(Integer currentUserId, Integer targetUserId) {
        employeeDao.updateEducationTeacherId(currentUserId, targetUserId);
    }

    @Override
    public Map<String, Object> levelDetail(Integer userId) {
        Map<String, Object> resultMap = new HashMap<>(4);
        //维修技师所在分部列表
        List<Integer> repair = employeeDao.queryRepairOrganList(userId);
        resultMap.put("repair", repair);
        //教务老师所在分部列表
        List<Integer> education = employeeDao.queryEducationOrganList(userId);
        resultMap.put("education", education);
        //运营主管所在分部列表
        List<Integer> teamTeacher = employeeDao.queryTeamTeacherOrganList(userId);
        resultMap.put("teamTeacher", teamTeacher);
        //乐队指导所在分部列表
        List<Integer> director = employeeDao.queryDirectorOrganList(userId);
        resultMap.put("director", director);
        return resultMap;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void level(List<EmployeeLevelDto> employeeLevelDtos) {
        Integer levelUserId = employeeLevelDtos.get(0).getLevelUserId();
        List<ImGroupModel> educationGroupModels = new ArrayList<>();
        //获取用户需要加入的群组列表
        Map<String, List<EmployeeLevelDto>> collect = employeeLevelDtos.stream().collect(Collectors.groupingBy(e -> e.getRoleName()));
        Set<String> roleNames = collect.keySet();
        Set<Integer> userIds = new HashSet<>();
        for (String roleName : roleNames) {
            List<EmployeeLevelDto> levelDtos = collect.get(roleName);
            Map<Integer, List<EmployeeLevelDto>> organIdMap = levelDtos.stream().collect(Collectors.groupingBy(e -> e.getOrganId()));
            Set<Integer> organIds = organIdMap.keySet();
            for (Integer organId : organIds) {
                List<EmployeeLevelDto> levelDtoList = organIdMap.get(organId);
                for (EmployeeLevelDto employeeLevelDto : levelDtoList) {
                    if (employeeLevelDto.getLevelUserId() == employeeLevelDto.getTransferUserId()) {
                        throw new BizException("交接人校验失败,请重新填写");
                    }
                    userIds.add(employeeLevelDto.getTransferUserId());
                    //获取关联的预报名的群聊
                    educationGroupModels.addAll(classGroupDao.queryApplyGroups(employeeLevelDto.getTransferUserId(), levelUserId, organId));
                    switch (roleName) {
                        case "education":
                            //替换当前分部合作单位关联的乐团主管
                            cooperationOrganDao.batchUpdateEduId(employeeLevelDto.getTransferUserId(), levelUserId, organId);
                            //教务老师所需加入的群组
                            educationGroupModels.addAll(classGroupDao.queryEducationGroups(employeeLevelDto.getTransferUserId(), levelUserId, organId));
                            break;
                        case "teamTeacher":
                            //运营主管所需加入的群组
                            educationGroupModels.addAll(classGroupDao.queryTeamTeacherGroups(employeeLevelDto.getTransferUserId(), levelUserId, organId));
                            break;
                        case "director":
                            //乐队指导所需加入的群组
                            educationGroupModels.addAll(classGroupDao.queryDirectorGroups(employeeLevelDto.getTransferUserId(), levelUserId, organId));
                            break;
                    }
                }
            }
        }
        Date date = new Date();
        if (educationGroupModels.size() > 0) {
            Map<Integer, String> realNameMap = MapUtil.convertIntegerMap(teacherDao.queryNameByIdList(new ArrayList<>(userIds)));
            //❤️用户加群
            List<com.ym.mec.biz.dal.entity.ImGroupMember> imGroupMemberList = new ArrayList<>();
            List<GroupModel> groupModelList = new ArrayList<>();
            for (ImGroupModel imGroupModel : educationGroupModels) {
                String userId = imGroupModel.getMemberList().get(0).getId();
                com.ym.mec.biz.dal.entity.ImGroupMember imGroupMember = new com.ym.mec.biz.dal.entity.ImGroupMember();
                imGroupMember.setCreateTime(date);
                imGroupMember.setImGroupId(imGroupModel.getId());
                imGroupMember.setIsAdmin(false);
                imGroupMember.setRoleType(imGroupModel.getUserRole());
                imGroupMember.setUpdateTime(date);
                imGroupMember.setUserId(Integer.parseInt(userId));
                imGroupMember.setNickname(realNameMap.get(imGroupMember.getUserId()));
                imGroupMemberList.add(imGroupMember);
                GroupMember groupMember = new GroupMember(userId, imGroupModel.getId());
                GroupModel groupModel = new GroupModel(imGroupModel.getId(), new GroupMember[]{groupMember}, imGroupModel.getName());
                groupModelList.add(groupModel);
            }
            imGroupMemberService.batchInsert(imGroupMemberList);
            imFeignService.groupBatchJoin(groupModelList);
        }
        //原有员工退群
        List<ImGroupModel> groupModels = classGroupDao.queryTeacherQuitGroups(levelUserId);
        //原有员工退预报名群
        groupModels.addAll(classGroupDao.queryQuitApplyGroups(levelUserId));
        if (groupModels.size() > 0) {
            List<com.ym.mec.biz.dal.entity.ImGroupMember> imGroupMemberList = new ArrayList<>();
            List<GroupModel> groupModelList = new ArrayList<>();
            for (ImGroupModel imGroupModel : groupModels) {
                com.ym.mec.biz.dal.entity.ImGroupMember imGroupMember = new com.ym.mec.biz.dal.entity.ImGroupMember();
                imGroupMember.setImGroupId(imGroupModel.getId());
                imGroupMember.setUserId(Integer.parseInt(imGroupModel.getMemberList().get(0).getId()));
                imGroupMemberList.add(imGroupMember);

                GroupMember groupMember = new GroupMember(imGroupMember.getUserId().toString(), imGroupModel.getId());
                GroupModel groupModel = new GroupModel(imGroupModel.getId(), new GroupMember[]{groupMember}, imGroupModel.getName());
                groupModelList.add(groupModel);
            }
            imGroupMemberService.batchDeleteByGroupIdAndUserId(imGroupMemberList);
            imFeignService.groupBatchQuit(groupModelList);
        }
        //更换教务老师等数据
        employeeDao.employeeLevel(employeeLevelDtos);
        //新增离职时间
        employeeDao.updateUserDemissionDate(levelUserId);
        //冻结用户
        employeeDao.updateUserLock(levelUserId, 1);
        ExecutorService executor = Executors.newCachedThreadPool();
        CompletableFuture<Boolean> refreshUserFriend = CompletableFuture.supplyAsync(() -> {
            //获取用户担任教务老师、维修技师的所有乐团列表
            List<String> musicGroupIds = employeeDao.queryMusicGroupIdByUserId(levelUserId);
            //获取用户担任教务老师的所有网管课列表
            List<String> practiceGroupIds = employeeDao.queryPracticeGroupIdByUserId(levelUserId);
            //获取用户担任教务老师的所有VIP课列表
            List<String> vipGroupIds = employeeDao.queryVipGroupIdByUserId(levelUserId);
            boolean refreshFlag = false;
            if (musicGroupIds.size() > 0) {
                for (String musicGroupId : musicGroupIds) {
                    imUserFriendService.refreshGroupImUserFriend(musicGroupId, GroupType.MUSIC);
                }
                refreshFlag = true;
            }
            if (practiceGroupIds.size() > 0) {
                for (String musicGroupId : practiceGroupIds) {
                    imUserFriendService.refreshGroupImUserFriend(musicGroupId, GroupType.PRACTICE);
                }
                refreshFlag = true;
            }
            if (vipGroupIds.size() > 0) {
                for (String musicGroupId : vipGroupIds) {
                    imUserFriendService.refreshGroupImUserFriend(musicGroupId, GroupType.VIP);
                }
                refreshFlag = true;
            }
            return refreshFlag;
        }, executor);
        Boolean join = refreshUserFriend.join();
        if (join) {
            refreshUserFriend.thenRun(() -> {
                //更新群成员数量
                imGroupDao.updateMemberNum();
                //删除重复的群成员
                imGroupMemberService.delRepeat();
            });
        }
        executor.shutdown();
    }

    @Override
    public List<Integer> queryUserRole(Integer sysUserId) {
        return employeeDao.queryUserRole(sysUserId);
    }

    /**
     * 批量添加角色和菜单关系
     *
     * @param roleId  角色id
     * @param menuIds 菜单集合
     */
    @Override
    public int batchInsertRoleMenu(Integer roleId, List<Integer> menuIds, Integer tenantId) {
        return employeeDao.batchInsertRoleMenu(roleId, menuIds, tenantId);
    }

    /**
     * 添加角色信息
     *
     * @param sysRole
     */
    @Override
    public int insertSysRole(SysRole sysRole) {
        return employeeDao.insertSysRole(sysRole);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void simpleUpdate(Employee employee) {
        SysUser user = teacherDao.getUser(employee.getId());
        //为了处理平台账户修改个人信息
        employee.setTenantId(user.getTenantId());
        employee.setUserId(employee.getId());
        employee.setUpdateTime(new Date());
        if (StringUtils.isNotEmpty(employee.getPhone())) {
            SysUser sysUser = teacherDao.getUserWithPhone(employee.getPhone());
            if (sysUser != null && !sysUser.getId().equals(employee.getUserId())) {
                throw new BizException("手机号已存在");
            }
            teacherDao.updateUser(employee);
            //同步修改机构手机号
            TenantInfo tenantInfo = tenantInfoService.getOne(new QueryWrapper<TenantInfo>()
                    .eq("user_id_", employee.getUserId()));
            if (Objects.nonNull(tenantInfo)) {
                tenantInfo.setPhone(employee.getPhone());
                tenantInfoService.updateById(tenantInfo);
            }
        }
        employeeDao.update(employee);
    }

    @Override
    public PageInfo<SysUserDto> queryEmployeeBasicInfo(UserBasicQueryInfo queryInfo) {
        PageInfo<SysUserDto> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<>();
        MapUtil.populateMap(params, queryInfo);

        List<SysUserDto> dataList = null;
        int count = employeeDao.countEmployeeBasicInfo(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = employeeDao.queryEmployeeBasicInfo(params);
        }
        if (count == 0) {
            dataList = new ArrayList<>();
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resetPassword(Integer userId) {
        SysUser user = Optional.ofNullable(teacherDao.getUser(userId)).orElseThrow(() -> new BizException("用户不存在"));
        //默认密码是gym+手机号后四位
        StringBuffer gym = new StringBuffer("gym").append(user.getPhone().substring(7));
        employeeDao.updatePassword(userId, new BCryptPasswordEncoder().encode(gym.toString()));
    }
}
