AdministerServiceImpl.java 13.8 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meishu.common.exception.HttpException;
import com.meishu.dto.administer.*;
import com.meishu.mapper.*;
import com.meishu.model.*;
import com.meishu.service.AdministerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.*;
import com.meishu.vo.administer.AdministerPermissionVO;
import com.meishu.vo.administer.LoginVO;
import com.meishu.vo.administer.PermissionsVO;
import com.meishu.vo.wechat.AccessTokenVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 中心管理账户表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-04-25
 */
@Service
public class AdministerServiceImpl extends ServiceImpl<AdministerMapper, AdministerDO> implements AdministerService {

    @Autowired
    private SmsCodeMapper smsCodeMapper;

    @Autowired
    private AdministerPermissionMapper administerPermissionMapper;

    @Autowired
    private PermissionsDictMapper permissionsDictMapper;

    @Autowired
    private AdministerDepartmentMappingMapper administerDepartmentMappingMapper;

    @Autowired
    private AdministerSubjectMappingMapper administerSubjectMappingMapper;

    @Autowired
    private RoleAdministerMappingMapper roleAdministerMappingMapper;

    @Autowired
    private SubjectTreeMapper subjectTreeMapper;

    @Autowired
    private SubjectDictMapper subjectDictMapper;

    @Autowired
    private ClassesDictMapper classesDictMapper;

    public String sendMsg(AdministerDO administerDO){


        //验证是否是白名单用户
        Integer count = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getTelephone, administerDO.getTelephone()));
        if (count == 0) {
            throw new HttpException(10011);
        }

        String verifyCode = RandomUtil.getRandomCode(6);
        String param = "{ \"code\":\"" + verifyCode + "\"}";

        //插入短信验证码
        smsCodeMapper.delete(new QueryWrapper<SmsCodeDO>()
                .lambda()
                .eq(SmsCodeDO::getTelephone,administerDO.getTelephone()));

        SmsCodeDO smsCodeDO = new SmsCodeDO();
        smsCodeDO.setTelephone(administerDO.getTelephone());
        smsCodeDO.setCode(verifyCode);
        smsCodeDO.setCreateDate(LocalDateTime.now());
        smsCodeMapper.insert(smsCodeDO);
        return SMSUtils.sendVerifySMS(ConstantUtils.TEMPLATE_CODE, smsCodeDO.getTelephone(), param);

    }

    public LoginVO login(SmsCodeDO smsCodeDO){

        LoginVO loginVO = new LoginVO();
        Integer count = smsCodeMapper.selectCount(new QueryWrapper<SmsCodeDO>()
                .lambda()
                .eq(SmsCodeDO::getTelephone, smsCodeDO.getTelephone())
                .eq(SmsCodeDO::getCode, smsCodeDO.getCode())
                .gt(SmsCodeDO::getCreateDate, LocalDateTime.now().minusMinutes(10L))
        );

        List<String> admin = Arrays.asList("18111111111","18122222222","18133333333","18144444444","18155555555","18166666666","18177777777","18188888888","18199999999","18100000000");
        if (admin.contains(smsCodeDO.getTelephone())){
            if ("123456".equals(smsCodeDO.getCode())){
                AdministerDO administerDO = this.baseMapper.selectOne(new QueryWrapper<AdministerDO>()
                        .lambda()
                        .eq(AdministerDO::getTelephone, smsCodeDO.getTelephone()));
                if (administerDO == null) {
                    throw new HttpException(10013);
                }
                String token = JwtUtil.generateToken(administerDO.getId(), ConstantUtils.ADMINISTER_TERMINATE);
                loginVO.setToken(token);
                return loginVO;
            }else {
                throw new HttpException(10003);
            }
        }

        if (count > 0) {
            AdministerDO administerDO = this.baseMapper.selectOne(new QueryWrapper<AdministerDO>()
                    .lambda()
                    .eq(AdministerDO::getTelephone, smsCodeDO.getTelephone()));
            if (administerDO == null) {
                throw new HttpException(10013);
            }

            String token = JwtUtil.generateToken(administerDO.getId(), ConstantUtils.ADMINISTER_TERMINATE);
            loginVO.setToken(token);
            return loginVO;
        } else {
            throw new HttpException(10003);
        }
    }

    public  LoginVO passwordLogin(AdministerDO administerDO){

        LoginVO loginVO = new LoginVO();
        AdministerDO administerDO1 = this.baseMapper.selectOne(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getTelephone, administerDO.getTelephone())
        .eq(AdministerDO::getPassword,administerDO.getPassword()));
        if (administerDO1 == null) {
            throw new HttpException(10004);
        }

        String token = JwtUtil.generateToken(administerDO1.getId(), ConstantUtils.ADMINISTER_TERMINATE);
        loginVO.setToken(token);
        return loginVO;

    }

    public AdministerPermissionVO getPermissions() {

        AdministerPermissionVO administerPermissionVO = new AdministerPermissionVO();
        AdministerDO administerDO = (AdministerDO) Localstorage.getUser();
//        AdministerDO administerDO = this.baseMapper.selectById(41);
        if (administerDO == null) {
            throw new HttpException(10012);
        }
        BeanUtils.copyProperties(administerDO, administerPermissionVO);

        //判断是否是超级管理员
        List<RoleAdministerMappingDO> roleAdministerMappingDOS = roleAdministerMappingMapper.selectList(new QueryWrapper<RoleAdministerMappingDO>()
        .lambda()
        .eq(RoleAdministerMappingDO::getAdministerId,administerDO.getId()));

        Boolean flag = false;

        for (RoleAdministerMappingDO roleAdministerMappingDO : roleAdministerMappingDOS){
            if (roleAdministerMappingDO.getRoleId()==1L){
                flag = true;
            }
        }
        administerPermissionVO.setIsRole(flag);
        List<PermissionsVO> list = administerPermissionMapper.getPermissions(administerDO.getId());
        List<PermissionsVO> treeList = new ArrayList();
        if (list != null) {
            list.forEach(permission -> {
                if (permission.getParentId() == null) {
                    treeList.add(permission);
                }

                list.forEach(p -> {
                    if (null != p.getParentId() && p.getParentId().equals(permission.getId())) {
                        if (permission.getChildren() == null) {
                            permission.setChildren(new ArrayList<>());
                        }
                        permission.getChildren().add(p);
                    }
                });
            });
        }
        administerPermissionVO.setPermissions(treeList);
        return administerPermissionVO;
    }

    public  List<PermissionsDictDO> getParentPermission(){
        return permissionsDictMapper.selectList(new QueryWrapper<PermissionsDictDO>()
        .lambda()
        .isNull(PermissionsDictDO::getParentId));
    }

    @Transactional(rollbackFor = Exception.class)
    public String addAdminister(AddAdministerDTO addAdministerDTO){

        //老师手机号不能重复
        Integer count = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
        .lambda()
        .eq(AdministerDO::getTelephone,addAdministerDTO.getTelephone()));

        if (count>0){
            throw new HttpException(60001);
        }

        AdministerDO administerDO = new AdministerDO();
        BeanUtils.copyProperties(addAdministerDTO,administerDO);
        administerDO.setStatus("1");
        this.baseMapper.insert(administerDO);

//        //多个校区
//        List<Long> campusIds = addAdministerDTO.getCampusIds();
//        for (Long lg : campusIds){
//            CampusAdministerMappingDO campusAdministerMappingDO = new CampusAdministerMappingDO();
//            campusAdministerMappingDO.setAdministerId(administerDO.getId());
//            campusAdministerMappingDO.setCampusId(lg);
//            campusAdministerMappingMapper.insert(campusAdministerMappingDO);
//        }

        //多个部门
        List<Long> departmentIds = addAdministerDTO.getDepartmentIds();
        for (Long lg : departmentIds){
            AdministerDepartmentMappingDO administerDepartmentMappingDO = new AdministerDepartmentMappingDO();
            administerDepartmentMappingDO.setAdministerId(administerDO.getId());
            administerDepartmentMappingDO.setDepartmentId(lg);
            administerDepartmentMappingMapper.insert(administerDepartmentMappingDO);
        }

        //多个科目
        List<Long> subjectIds = addAdministerDTO.getSubjectIds();
        for (Long lg : subjectIds){
            AdministerSubjectMappingDO administerSubjectMappingDO = new AdministerSubjectMappingDO();
            administerSubjectMappingDO.setAdministerId(administerDO.getId());
            administerSubjectMappingDO.setSubjectId(lg);
            administerSubjectMappingMapper.insert(administerSubjectMappingDO);
        }

        //角色
        List<Long> roleIds = addAdministerDTO.getRoleIds();
        for (Long lg : roleIds){
            RoleAdministerMappingDO roleAdministerMappingDO = new RoleAdministerMappingDO();
            roleAdministerMappingDO.setAdministerId(administerDO.getId());
            roleAdministerMappingDO.setRoleId(lg);
            roleAdministerMappingMapper.insert(roleAdministerMappingDO);
        }

        return ConstantUtils.ADD_SUCCESS;
    }

    public List<PermissionsDictDO> getPermissionTree(){
        List<PermissionsDictDO> list = permissionsDictMapper.selectList(null);
        List<PermissionsDictDO> treeList = new ArrayList();
        if (list != null) {
            list.forEach(permission -> {
                if (permission.getParentId() == null) {
                    treeList.add(permission);
                }

                list.forEach(p -> {
                    if (null != p.getParentId() && p.getParentId().equals(permission.getId())) {
                        if (permission.getChildren() == null) {
                            permission.setChildren(new ArrayList<>());
                        }
                        permission.getChildren().add(p);
                    }
                });
            });
        }
        return treeList;
    }

    public String changeStatus(ChangeStatusDTO changeStatusDTO){
        AdministerDO administerDO = new AdministerDO();
        administerDO.setStatus(changeStatusDTO.getStatus());
        this.baseMapper.update(administerDO,new QueryWrapper<AdministerDO>()
        .lambda()
        .in(AdministerDO::getId,changeStatusDTO.getIds()));
//        this.baseMapper.updateById(administerDO);
        return ConstantUtils.SUCCESS_UPDATE;
    }

    @Transactional(rollbackFor = Exception.class)
    public  String updateAdminister(UpdateAdministerDTO updateAdministerDTO){

        AdministerDO administerDO = new AdministerDO();
        BeanUtils.copyProperties(updateAdministerDTO,administerDO);
        this.baseMapper.updateById(administerDO);

        if (null!=updateAdministerDTO.getDepartmentIds()){
            administerDepartmentMappingMapper.delete(new QueryWrapper<AdministerDepartmentMappingDO>()
                    .lambda()
                    .eq(AdministerDepartmentMappingDO::getAdministerId,updateAdministerDTO.getId()));

            List<Long> departmentIds = updateAdministerDTO.getDepartmentIds();
            for (Long lg : departmentIds){
                AdministerDepartmentMappingDO administerDepartmentMappingDO = new AdministerDepartmentMappingDO();
                administerDepartmentMappingDO.setDepartmentId(lg);
                administerDepartmentMappingDO.setAdministerId(updateAdministerDTO.getId());
                administerDepartmentMappingMapper.insert(administerDepartmentMappingDO);
            }
        }

        if (null!=updateAdministerDTO.getSubjectIds()){

            administerSubjectMappingMapper.delete(new QueryWrapper<AdministerSubjectMappingDO>()
            .lambda()
            .eq(AdministerSubjectMappingDO::getAdministerId,updateAdministerDTO.getId()));
            //多个科目
            List<Long> subjectIds = updateAdministerDTO.getSubjectIds();
            for (Long lg : subjectIds){
                AdministerSubjectMappingDO administerSubjectMappingDO = new AdministerSubjectMappingDO();
                administerSubjectMappingDO.setAdministerId(administerDO.getId());
                administerSubjectMappingDO.setSubjectId(lg);
                administerSubjectMappingMapper.insert(administerSubjectMappingDO);
            }
        }

        if (null!=updateAdministerDTO.getRoleIds()){
            roleAdministerMappingMapper.delete(new QueryWrapper<RoleAdministerMappingDO>()
            .lambda()
            .eq(RoleAdministerMappingDO::getAdministerId,updateAdministerDTO.getId()));
            List<Long> roleIds = updateAdministerDTO.getRoleIds();
            for (Long lg : roleIds){
                RoleAdministerMappingDO roleAdministerMappingDO = new RoleAdministerMappingDO();
                roleAdministerMappingDO.setAdministerId(updateAdministerDTO.getId());
                roleAdministerMappingDO.setRoleId(lg);
                roleAdministerMappingMapper.insert(roleAdministerMappingDO);
            }

        }
        return ConstantUtils.SUCCESS_UPDATE;
    }

    public List<AdministerDO> getAllAdminister(){
        return this.baseMapper.selectList(null);
    }


}