AdministerServiceImpl.java 3.67 KB
package com.zhongzhi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongzhi.common.constant.Platform;
import com.zhongzhi.common.constant.SmsCode;
import com.zhongzhi.common.exception.HttpException;
import com.zhongzhi.common.utils.JwtUtil;
import com.zhongzhi.dto.administer.AdministerPageDTO;
import com.zhongzhi.dto.administer.LoginDTO;
import com.zhongzhi.model.AdministerDO;
import com.zhongzhi.dao.AdministerDAO;
import com.zhongzhi.model.SmsCodeDO;
import com.zhongzhi.service.AdministerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongzhi.service.PermissionsService;
import com.zhongzhi.service.SmsCodeService;
import com.zhongzhi.vo.administer.AdministerVO;
import com.zhongzhi.vo.permission.PermissionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Autowired
    private SmsCodeService smsCodeService;

    @Autowired
    private PermissionsService permissionsService;

    @Override
    public AdministerVO login(LoginDTO loginDTO) {
        AdministerVO administerVO = new AdministerVO();
        SmsCodeDO smsCodeDO = smsCodeService.getOneByTelePhone(loginDTO.getTelephone(), SmsCode.login, SmsCode.center);
        if (smsCodeDO == null) {
            throw new HttpException(10025);
        }

        if (!smsCodeDO.getCode().equals(loginDTO.getCode())) {
            throw new HttpException(10021);
        }

        smsCodeService.removeById(smsCodeDO.getId());

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

        BeanUtils.copyProperties(administerDO, administerVO);
        String token = JwtUtil.generateToken(administerDO.getId(), Platform.center);
        administerVO.setToken(token);

        List<PermissionVO> list = permissionsService.getPermissionById(administerDO.getId());
        List<PermissionVO> treeList = new ArrayList();
        list.forEach(permissions -> {
            if (permissions.getParentId() == null) {
                treeList.add(permissions);
            }

            list.forEach(p -> {
                if (p.getParentId() != null) {
                    if (p.getParentId().equals(permissions.getId())) {
                        if (permissions.getChildNodeList() == null) {
                            permissions.setChildNodeList(new ArrayList<>());
                        }
                        permissions.getChildNodeList().add(p);
                    }
                }
            });
        });
        administerVO.setPermission(treeList);
        return administerVO;
    }

    @Override
    public IPage getAdministerPage(AdministerPageDTO administerPageDTO) {
        Page page = new Page(administerPageDTO.getPageNo(), administerPageDTO.getPageSize());
        return this.baseMapper.selectPage(page, new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getRole, administerPageDTO.getListItem())
                .like(AdministerDO::getUsername, administerPageDTO.getUsername()));
    }
}