package com.vctgo.system.service.impl;

import java.nio.charset.StandardCharsets;
import java.util.*;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vctgo.common.core.constant.CacheConstants;
import com.vctgo.common.core.constant.HttpStatus;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.utils.DateUtils;
import com.vctgo.common.core.utils.StrUtils;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.mybatisplus.util.TenantUtils;
import com.vctgo.common.redis.service.RedisService;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysRole;
import com.vctgo.system.api.domain.SysTenant;
import com.vctgo.system.api.domain.SysUser;
import com.vctgo.system.api.domain.SysUserTempalte;
import com.vctgo.system.api.domain.vo.SysTeachStaffVo;
import com.vctgo.system.api.model.LoginUser;
import com.vctgo.system.domain.*;
import com.vctgo.system.mapper.*;
import com.vctgo.system.service.ISysReClassUserService;
import com.vctgo.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vctgo.system.service.ISysTeachStaffService;
import com.vctgo.system.util.ConstantUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * 幼儿园教职工信息Service业务层处理
 *
 * @author caesar
 * @date 2024-03-27
 */
@Slf4j
@Service
public class SysTeachStaffServiceImpl implements ISysTeachStaffService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysTeachStaffMapper sysTeachStaffMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private RedisService redisService;

    @Autowired
    private ISysReClassUserService sysReClassUserService;
    @Autowired
    private SysTenantMapper sysTenantMapper;

    @Autowired
    private SysBranchKindergartenMapper sysBranchKindergartenMapper;

    @Autowired
    private SysReGardenBranchMapper sysReGardenBranchMapper;

    @Autowired
    private SysPostMapper sysPostMapper;


    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private SysPlatformTeachStaffMapper sysPlatformTeachStaffMapper;
    /**
     * 查询幼儿园教职工信息
     *
     * @param userId 幼儿园教职工用户Id
     * @return 幼儿园教职工信息
     */
    @Override
    public SysTeachStaff selectSysTeachStaffById(String userId) {
        return sysTeachStaffMapper.selectByUserId(userId);
    }

    /**
     * 查询幼儿园教职工信息列表-分页
     *
     * @param sysTeachStaff 幼儿园教职工信息
     * @return 幼儿园教职工信息
     */
    @Override
    public IPage<SysTeachStaff> selectSysTeachStaffPage(SysTeachStaff sysTeachStaff) {
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L));

        return sysTeachStaffMapper.selectSysTeachStaffList(mpPage, sysTeachStaff);
    }

    /**
     * 查询幼儿园教职工信息列表
     *
     * @param sysTeachStaff 幼儿园教职工信息
     * @return 幼儿园教职工信息
     */
    @Override
    public List<SysTeachStaff> selectSysTeachStaffList(SysTeachStaff sysTeachStaff) {
        return sysTeachStaffMapper.selectSysTeachStaffList(sysTeachStaff);
    }

    /**
     * 新增幼儿园教职工信息
     *
     * @param sysTeachStaff 幼儿园教职工信息
     * @return 结果
     */
    @Override
    public int insertSysTeachStaff(SysTeachStaff sysTeachStaff) {

        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getDelFlag, ConstantUtils.IS_DELETED_0);
        sysUserWrapper.eq(SysUser::getPhonenumber, sysTeachStaff.getPhonenumber());
        List<SysUser> sysUserList = sysUserMapper.selectList(sysUserWrapper);
        if (CollectionUtils.isNotEmpty(sysUserList)) {
            throw new SecurityException("该手机号已存在，请核对后再试");
        }

        // 幼儿园端新增用户
        SysUser sysUser = new SysUser();
        sysUser.setDelFlag(ConstantUtils.IS_DELETED_0);
        sysUser.setUserName(sysTeachStaff.getPhonenumber());
        sysUser.setNickName(sysTeachStaff.getUserName());
        sysUser.setPhonenumber(sysTeachStaff.getPhonenumber());
        sysUser.setCreateById(SecurityUtils.getUserId());
        sysUser.setCreateTime(DateUtils.getNowDate());
        sysUser.setDeptId(sysTeachStaff.getDeptId());
        sysUser.setSex(sysTeachStaff.getSex());
        sysUser.setEmail(sysTeachStaff.getEmail());
        sysUser.setUserStatus(sysTeachStaff.getUserStatus());
        sysUser.setAccountStatus(ConstantUtils.ACCOUNT_STATUS_2);
        sysUser.setTenantId(SecurityUtils.getTenantId());
        sysUserMapper.insert(sysUser);

        // 新增加用户附属信息
        sysTeachStaff.setUserId(sysUser.getUserId());
        return sysTeachStaffMapper.insert(sysTeachStaff);
    }

    /**
     * 修改幼儿园教职工信息
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public AjaxResult checkSysTeachUserStatus(Long userId) {

        SysReClassUser sysReClassUser = new SysReClassUser();
        sysReClassUser.setBindUserId(userId);
        List<SysReClassUser> reClassUserList = sysReClassUserService.selectSysReClassUserList(sysReClassUser);
        //需求要求解除班级时提示，人为解绑，而不是离职时自动解绑班级。
        if (CollectionUtils.isNotEmpty(reClassUserList)) {
            throw new ServiceException("该教职工已绑定班级，不可离职");
        }

        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUser::getDelFlag, ConstantUtils.IS_DELETED_0);
        sysUserWrapper.eq(SysUser::getUserId, userId);
        SysUser sysUser = sysUserMapper.selectOne(sysUserWrapper);

        // 账户状态 0=正常,1=停用,2=未启用
        String accountStatus = sysUser.getAccountStatus();

        // 密码，通过密码不为空的方式，来判断当前这个用户开通了账号
        String password = sysUser.getPassword();
        if (ConstantUtils.ACCOUNT_STATUS_0.equals(accountStatus) && StringUtils.hasLength(password)) {
            return AjaxResult.success("离职前需禁用该教职工已开通账号，是否确认禁用账号？", "1");
        }
        return AjaxResult.success();
    }

    /**
     * 修改幼儿园教职工信息
     *
     * @param sysTeachStaff 幼儿园教职工信息
     * @return 结果
     */
    @Override
    public int updateSysTeachStaff(SysTeachStaff sysTeachStaff) {

        /**
         * 保存幼儿园教职工的基本信息
         *
         */
        LambdaQueryWrapper<SysUser> sysUserWrapper = new LambdaQueryWrapper<>();
        sysUserWrapper.eq(SysUserTempalte::getDelFlag, ConstantUtils.IS_DELETED_0);
        sysUserWrapper.eq(SysUserTempalte::getPhonenumber, sysTeachStaff.getPhonenumber());
        SysUser sysUserTmp = sysUserMapper.selectOne(sysUserWrapper);
        if (Objects.nonNull(sysUserTmp) && sysUserTmp.getUserId().longValue() != sysTeachStaff.getUserId().longValue()) {
            throw new SecurityException("手机号已被使用");
        }

        boolean isDeleteRedis = false;
        SysUser sysUser = sysUserMapper.selectById(sysTeachStaff.getUserId());
        if ((sysUser.getDeptId().longValue() != sysTeachStaff.getDeptId().longValue())
                || (!sysUser.getNickName().equals(sysTeachStaff.getUserName()))
                || (!sysUser.getPhonenumber().equals(sysTeachStaff.getPhonenumber()))
                || (!sysUser.getUserStatus().equals(sysTeachStaff.getUserStatus()))) {
            isDeleteRedis = true;
        }

        /**
         * 修改幼儿园教职工的基本信息
         */
        if ("1".equals(sysTeachStaff.getUserStatus())) {
            //离职时，将用户账户更改为禁用状态
            sysUser.setAccountStatus(ConstantUtils.ACCOUNT_STATUS_1);
        }
        sysUser.setUpdateById(SecurityUtils.getUserId());
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUser.setUserName(sysTeachStaff.getPhonenumber());
        sysUser.setNickName(sysTeachStaff.getUserName());
        sysUser.setPhonenumber(sysTeachStaff.getPhonenumber());
        sysUser.setSex(sysTeachStaff.getSex());
        sysUser.setEmail(sysTeachStaff.getEmail());
        sysUser.setUserStatus(sysTeachStaff.getUserStatus());
        sysUser.setDeptId(sysTeachStaff.getDeptId());
        if (StringUtils.hasLength(sysUser.getUserAccount())) {
            sysUser.setUserAccount(sysTeachStaff.getPhonenumber());
        }
        sysUserMapper.updateUser(sysUser);

        sysTeachStaff.setUpdateById(SecurityUtils.getUserId());
        sysTeachStaff.setUpdateTime(DateUtils.getNowDate());
        int result = Objects.nonNull(sysTeachStaff.getId()) ? sysTeachStaffMapper.updateById(sysTeachStaff) : sysTeachStaffMapper.insert(sysTeachStaff);

        if (isDeleteRedis) {
            // 修改用户，将其登录信息删掉
            List<String> keyList = new ArrayList<>();
            Collection<String> keys = redisService.keys(CacheConstants.LOGIN_TOKEN_KEY + "*");
            for (String key : keys) {
                LoginUser onlineUser = redisService.getCacheObject(key);
                if (Objects.nonNull(onlineUser) && sysTeachStaff.getUserId().longValue() == onlineUser.getUserid().longValue()) {
                    keyList.add(key);
                }
            }
            redisService.deleteObject(keyList);
        }
        return result;
    }

    /**
     * 逻辑删除幼儿园教职工信息
     *
     * @param userId 需要删除的幼儿园教职工信息主键
     * @return 结果
     */
    @Override
    public int logicalDeleteSysTeachStaffByIds(Long userId) {

        // 删除用户信息
        userService.deleteUserByIds(new Long[]{userId});
//        sysUserMapper.deleteUserById(userId);

        // 删除用户附带信息
        SysTeachStaff sysTeachStaff = new SysTeachStaff();
        sysTeachStaff.setUpdateById(SecurityUtils.getUserId());
        sysTeachStaff.setUpdateBy(SecurityUtils.getUsername());
        sysTeachStaff.setUpdateTime(new Date());
        sysTeachStaff.setIsDeleted(ConstantUtils.IS_DELETED_1);
        return sysTeachStaffMapper.logicalDeleteSysTeachStaffByIds(new Long[]{userId}, sysTeachStaff);
    }

    /**
     * 批量删除幼儿园教职工信息
     *
     * @param ids 需要删除的幼儿园教职工信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTeachStaffByIds(Long[] ids) {
        return sysTeachStaffMapper.deleteSysTeachStaffByIds(ids);
    }

    /**
     * 删除幼儿园教职工信息信息
     *
     * @param id 幼儿园教职工信息主键
     * @return 结果
     */
    @Override
    public int deleteSysTeachStaffById(Long id) {
        return sysTeachStaffMapper.deleteById(id);
    }

    @Override
    public Long countUserPostById(Long postId) {
        return sysTeachStaffMapper.selectCount(new QueryWrapper<SysTeachStaff>().lambda()
                .or().eq(SysTeachStaff::getMainPost, postId)
                .or().eq(SysTeachStaff::getOtherPost, postId)
                .eq(SysTeachStaff::getIsDeleted, ConstantUtils.IS_DELETED_0)
        );
    }

    @Override
    public SysTeachStaffVo getLocationId(Long userId) {
        LambdaQueryWrapper<SysTeachStaff> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysTeachStaff::getUserId, userId);
        SysTeachStaff sysTeachStaff = sysTeachStaffMapper.selectOne(wrapper);
        SysTeachStaffVo sysTeachStaffVo = new SysTeachStaffVo();
        sysTeachStaffVo.setLocationId(sysTeachStaff.getKindergartenId());
        return sysTeachStaffVo;
    }

    @Override
    @Transactional
    public int updateStaffByKinderId(SysTeachStaffVo sysTeachStaffVo) {
        //查询租户
        LambdaQueryWrapper<SysTenant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysTenant::getBranchId, sysTeachStaffVo.getLocationId());
        SysTenant sysTenant= sysTenantMapper.selectOne(wrapper);
        LambdaQueryWrapper<SysBranchKindergarten> branchWrapper = new LambdaQueryWrapper<>();
        branchWrapper.eq(SysBranchKindergarten::getId, sysTeachStaffVo.getLocationId());
        SysBranchKindergarten sysBranchKindergarten = sysBranchKindergartenMapper.selectOne(branchWrapper);
        SysUser sysUser = sysUserMapper.selectById(sysTeachStaffVo.getUserId());
        boolean iscreate=false;
        Long tenantId=null;
        SysTenant addSysTenant =new SysTenant();
        if(sysTenant==null){
            //创建默认租户
            addSysTenant.setBranchId(sysBranchKindergarten.getId());
            String randomPassword = SecurityUtils.randomPassword(8);//随机密码8位
            String password = SecurityUtils.encryptPassword(randomPassword);
            addSysTenant.setPassword(password);
            addSysTenant.setStatus(1);
            addSysTenant.setTenantName(sysBranchKindergarten.getKindergartenName());
            if(sysBranchKindergarten.getKindergartenType().equals("1")){
                addSysTenant.setTenantType(3);
            }else{
                addSysTenant.setGardenId(sysBranchKindergarten.getGardenId());
                addSysTenant.setTenantType(2);
            }
            addSysTenant.setUserName(SecurityUtils.getUsername());
            addSysTenant.setUserPhone(sysUser.getPhonenumber());
            addSysTenant.setSourceType(2);
            insertSysTenant(addSysTenant,sysBranchKindergarten);
            iscreate=true;
            tenantId=addSysTenant.getId();
        }else{
            tenantId=sysTenant.getId();

        }
        //回填账户相关信息 租户id
        //用户性别（0男 1女 2未知）
        String sex="0";
        if(StrUtils.isNotEmpty(sysTeachStaffVo.getSexValue()) && "女".equals(sysTeachStaffVo.getSexValue())){
            sex="1";
        }
        if(sysUser!=null){
            sysUser.setSex(sex);
            sysUser.setUpdateBy(SecurityUtils.getUsername());
            sysUser.setUpdateById(SecurityUtils.getUserId());
            sysUser.setUpdateTime(new Date());
            if(iscreate){
                sysUser.setUserType("00");
                sysUser.setRemark(addSysTenant.getTenantName() + "管理员");
            }
            sysUser.setTenantId(tenantId);

            //重新设置验证状态
            if(sysUser.getSourceType()==2 && sysUser.getVerifyStatus()!=1){
                sysUser.setVerifyStatus(0);
            }
            sysUser.setNickName(sysTeachStaffVo.getNameValue());
            sysUserMapper.updateUser(sysUser);
        }
        //回填教职工表的幼儿园信息
        SecurityUtils.setTemporaryIgnoreTables(Arrays.asList("sys_teach_staff"));

        LambdaQueryWrapper<SysTeachStaff> staffWrapper = new LambdaQueryWrapper<>();
        staffWrapper.eq(SysTeachStaff::getUserId, sysTeachStaffVo.getUserId());
        SysTeachStaff sysTeachStaff = sysTeachStaffMapper.selectOne(staffWrapper);
        SecurityUtils.removeTemporaryIgnoreTables(Arrays.asList("sys_teach_staff"));
        if(sysTeachStaff != null){
            if(sysBranchKindergarten!=null){
                sysTeachStaff.setKindergartenType(sysBranchKindergarten.getKindergartenType());
            }
            sysTeachStaff.setKindergartenId(sysTeachStaffVo.getLocationId());
            sysTeachStaff.setSex(sex);
            String remark="";
            if(StrUtils.isNotEmpty(sysTeachStaffVo.getPostValue())){
                remark += sysTeachStaffVo.getPostValue()+";";
            }
            if(StrUtils.isNotEmpty(sysTeachStaffVo.getProfessionalValue())){
                remark += sysTeachStaffVo.getProfessionalValue()+";";
            }
            if(StrUtils.isNotEmpty(sysTeachStaffVo.getCertificateNumberValue())){
                remark += "资格证编号"+sysTeachStaffVo.getCertificateNumberValue()+";";
            }
            sysTeachStaff.setRemark(remark);
            sysTeachStaff.setUpdateBy(SecurityUtils.getUsername());
            sysTeachStaff.setUpdateById(SecurityUtils.getUserId());
            sysTeachStaff.setUpdateTime(new Date());
            sysTeachStaff.setUserName(sysTeachStaffVo.getNameValue());
            sysTeachStaff.setTenantId(tenantId);
            sysTeachStaffMapper.updateSysTeachStaff(sysTeachStaff);
        }
        //新增平台端教职工信息
        if(sysUser!=null){
            insertPlatSysTeachStaff(sysUser,sysTeachStaff);
        }
        return 0;
    }

    public void insertSysTenant(SysTenant sysTenant,SysBranchKindergarten sysBranchKindergarten)
    {
        Long maxTenantId = sysTenantMapper.getMaxTenantId();
        sysTenant.setId(maxTenantId == null ? 100L : maxTenantId+1);
        //存在集团的幼儿园租户，上级集团Id和幼儿园Id均需要传值
        if(sysTenant.getTenantType()==2 && (StrUtils.isEmpty(sysTenant.getGardenId())|| StrUtils.isEmpty(sysTenant.getBranchId()))){
            throw new IllegalArgumentException("参数有误，请联系管理员!");
        }
        //根据集团id查询集团的租户id
//        SysTenant sysGroupTenant = new SysTenant();
//        sysGroupTenant.setTenantType(Integer.valueOf(ConstantUtils.TENANT_TYPE_1));
//        sysGroupTenant.setGardenId(sysTenant.getGardenId());
//        List<SysTenant> sysGroupTenantList = sysTenantMapper.selectSysTenantList(sysGroupTenant);
//        //一个集团只允许有一个租户管理员账号，且必须先建立集团账号，才可以建立幼儿园账号
//        if(sysTenant.getTenantType()==2 && CollectionUtils.isEmpty(sysGroupTenantList)){
//           throw new IllegalArgumentException("当前租户集团管理员账号未开通，请先开通集团租户账号!");
//        }
//        //一个集团只允许有一个租户管理员账号
//        if(sysTenant.getGardenId() != null && sysGroupTenantList.size()>1){
//            throw new IllegalArgumentException("当前租户集团账号有误，请联系管理员!");
//        }
        //创建租户
        sysTenantMapper.insert(sysTenant);
//        sysTenant.getParams().put("parentId", sysGroupTenantList.get(0).getId());
//        createBranchReTenantData(sysTenant);


        //租户创建完成后 开始创建相关基础数据
        TenantUtils.execute(sysTenant.getId(), () -> {
            //创建默认岗位--岗位默认为主班教师、配班教师、保育员
            if(sysTenant.getTenantType() == 1 || sysTenant.getTenantType() == 3){
                createPost(sysTenant);
            }
            //创建默认角色--角色默认为租户名称+管理员
            Long roleid = createRole(sysTenant);
            //回填默认账号
            updateUser(sysTenant, roleid,sysBranchKindergarten);
        });

        if (redisService.hasKey("tenantList")) {
            redisService.setCacheList("tenantList", sysTenant);
        } else {
            List<SysTenant> tenantList = sysTenantMapper.selectSysTenantList(sysTenant);
            tenantList.add(sysTenant);
            redisService.setCacheList("tenantList", tenantList);
        }

    }

    private int createBranchReTenantData(SysTenant sysTenant) {
        SysReGardenBranch sysReGardenBranch = new SysReGardenBranch();
        sysReGardenBranch.setId(sysTenant.getId());
        if(sysTenant.getTenantType() == 2){
            sysReGardenBranch.setParentId((Long)sysTenant.getParams().get("parentId"));
        }else{
        }
        sysReGardenBranch.setDeptType(ConstantUtils.DEPT_TYPE_2);
        return sysReGardenBranchMapper.insert(sysReGardenBranch);
    }

    private void createPost(SysTenant sysTenant) {
        Long tenantId = sysTenant.getId();
        List<SysPost> postList = new ArrayList<>(3);
        SysPost zbjsPost = new SysPost();
        SysPost pbjsPost = new SysPost();
        SysPost byyPost = new SysPost();
        zbjsPost.setPostCode("zbjs").setPostName("主班教师").setIsSysPost("Y").setPostSort("0");
        zbjsPost.setTenantId(tenantId);
        pbjsPost.setPostCode("pbjs").setPostName("配班教师").setIsSysPost("Y").setPostSort("0");
        pbjsPost.setTenantId(tenantId);
        byyPost.setPostCode("byy").setPostName("保育员").setIsSysPost("Y").setPostSort("0");
        byyPost.setTenantId(tenantId);
        postList.add(zbjsPost);
        postList.add(pbjsPost);
        postList.add(byyPost);
        sysPostMapper.insertBatch(postList);
    }

    private Long createRole(SysTenant sysTenant) {
        // 创建角色
        SysRole role = new SysRole();
        role.setRoleName(sysTenant.getTenantName()+"管理员").setRoleKey("admin")
                .setRoleSort("1").setDataScope("1").setMenuCheckStrictly(true).setDeptCheckStrictly(true);
        role.setCreateBy(sysTenant.getUserName());
        role.setRemark("租户管理员");
        role.setAdminRole(true);
        role.setIsDefault("1");
        role.setTenantId(sysTenant.getId());
        sysRoleMapper.insert(role);
        //根据租户套餐ids查出套餐编码塞入角色-菜单表
//        createRoleMenu(sysTenant,role);
        return role.getRoleId();
    }

    private void updateUser(SysTenant sysTenant, Long roleId,SysBranchKindergarten sysBranchKindergarten) {
        //更新用户
        //创建默认租户
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
//        sysUser.setUserType("00");
//        sysUser.setRemark(sysTenant.getTenantName() + "管理员");
//        sysUser.setTenantId(sysTenant.getId());
//        sysUserMapper.updateUser(sysUser);
        //回填教职工表的幼儿园信息
//        LambdaQueryWrapper<SysTeachStaff> staffWrapper = new LambdaQueryWrapper<>();
//        staffWrapper.eq(SysTeachStaff::getUserId, sysUser.getUserId());
//        SysTeachStaff sysTeachStaff = sysTeachStaffMapper.selectOne(staffWrapper);
//        if(sysTeachStaff != null){
//            sysTeachStaff.setKindergartenId(sysBranchKindergarten.getId());
//            sysTeachStaff.setKindergartenType(sysBranchKindergarten.getKindergartenType());
//            sysTeachStaff.setTenantId(sysTenant.getId());
//            sysTeachStaffMapper.update(sysTeachStaff);
//        }
        userRoleMapper.insert(new SysUserRole().setRoleId(roleId).setUserId(sysUser.getUserId()).setTenantId(sysTenant.getId()));
    }

    public void insertPlatSysTeachStaff(SysUser sysUser,SysTeachStaff sysTeachStaff) {
        SysPlatformTeachStaff sysPlatformTeachStaff =new SysPlatformTeachStaff();
        SecurityUtils.setTemporaryIgnoreTables(Arrays.asList("sys_platform_teach_staff"));

        sysPlatformTeachStaff.setCreateById(SecurityUtils.getUserId());

        // 判断当前手机号是否存在
        String phone = sysUser.getPhonenumber();
        LambdaQueryWrapper<SysPlatformTeachStaff> sysTeachStaffWrapper = new LambdaQueryWrapper<>();
        sysTeachStaffWrapper.eq(SysPlatformTeachStaff::getIsDeleted, ConstantUtils.IS_DELETED_0);
        sysTeachStaffWrapper.eq(SysPlatformTeachStaff::getPhone, phone);
        List<SysPlatformTeachStaff> sysPlatformTeachStaffList = sysPlatformTeachStaffMapper.selectList(sysTeachStaffWrapper);
        if(CollectionUtils.isNotEmpty(sysPlatformTeachStaffList)){
            SysPlatformTeachStaff updateVo = sysPlatformTeachStaffList.get(0);
            //重新设置验证状态
            if(updateVo.getInformationSource().equals("4") && updateVo.getVerifyStatus()!=1 ){
                updateVo.setSex(sysUser.getSex());
                updateVo.setKindergartenId(sysTeachStaff.getKindergartenId());
                updateVo.setUserName(sysUser.getUserName());
                updateVo.setKindergartenType(sysTeachStaff.getKindergartenType());
                updateVo.setUpdateBy(SecurityUtils.getUsername());
                updateVo.setUpdateById(SecurityUtils.getUserId());
                updateVo.setUpdateTime(new Date());
                updateVo.setVerifyStatus(0);
                if(sysTeachStaff != null){
                    updateVo.setRemark(sysTeachStaff.getRemark());
                }
                sysPlatformTeachStaffMapper.updateSysPlatTeachStaff(updateVo);
            }
        }else{
            sysPlatformTeachStaff.setKindergartenId(sysTeachStaff.getKindergartenId());
            sysPlatformTeachStaff.setKindergartenType(sysTeachStaff.getKindergartenType());
            sysPlatformTeachStaff.setInformationSource(ConstantUtils.STAFF_INFO_SOURCE_VOTE);
            sysPlatformTeachStaff.setVerifyStatus(0);
            sysPlatformTeachStaff.setUserName(sysUser.getUserName());
            sysPlatformTeachStaff.setPhone(phone);
            sysPlatformTeachStaff.setCreateById(SecurityUtils.getUserId());
            sysPlatformTeachStaff.setCreateBy(SecurityUtils.getUsername());
            sysPlatformTeachStaff.setCreateTime(DateUtils.getNowDate());
            sysPlatformTeachStaff.setTenantId(9999L);
            sysPlatformTeachStaffMapper.insert(sysPlatformTeachStaff);
        }

        SecurityUtils.removeTemporaryIgnoreTables(Arrays.asList("sys_platform_teach_staff"));

    }
}
