SubstationServiceImpl.java 4.05 KB
package com.diligrp.rider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.entity.Substation;
import com.diligrp.rider.mapper.SubstationMapper;
import com.diligrp.rider.service.SubstationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

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

@Service
@RequiredArgsConstructor
public class SubstationServiceImpl implements SubstationService {

    private final SubstationMapper substationMapper;

    @Override
    public List<Substation> list(String keyword) {
        LambdaQueryWrapper<Substation> wrapper = new LambdaQueryWrapper<Substation>()
                .orderByDesc(Substation::getId);
        if (keyword != null && !keyword.isBlank()) {
            wrapper.like(Substation::getUserLogin, keyword)
                    .or().like(Substation::getUserNickname, keyword)
                    .or().like(Substation::getMobile, keyword);
        }
        return substationMapper.selectList(wrapper);
    }

    @Override
    public void add(Substation substation) {
        if (substation.getCityId() == null || substation.getCityId() < 1) {
            throw new BizException("请选择管理城市");
        }

        Long loginExists = substationMapper.selectCount(new LambdaQueryWrapper<Substation>()
                .eq(Substation::getUserLogin, substation.getUserLogin()));
        if (loginExists > 0) throw new BizException("账号已存在,请更换");

        substation.setUserPass(encryptPass(substation.getUserPass()));
        substation.setUserStatus(1);
        substation.setCreateTime(System.currentTimeMillis() / 1000);
        substationMapper.insert(substation);
    }

    @Override
    public void edit(Substation substation) {
        Substation existing = substationMapper.selectById(substation.getId());
        if (existing == null) throw new BizException("分站管理员不存在");
        // 密码为空则不更新
        if (substation.getUserPass() == null || substation.getUserPass().isBlank()) {
            substation.setUserPass(null);
        } else {
            substation.setUserPass(encryptPass(substation.getUserPass()));
        }
        substationMapper.updateById(substation);
    }

    @Override
    public void ban(Long id) {
        substationMapper.update(null, new LambdaUpdateWrapper<Substation>()
                .eq(Substation::getId, id).set(Substation::getUserStatus, 0));
    }

    @Override
    public void cancelBan(Long id) {
        substationMapper.update(null, new LambdaUpdateWrapper<Substation>()
                .eq(Substation::getId, id).set(Substation::getUserStatus, 1));
    }

    @Override
    public void del(Long id) {
        substationMapper.deleteById(id);
    }

    @Override
    public Substation getByCityId(Long cityId) {
        return substationMapper.selectOne(new LambdaQueryWrapper<Substation>()
                .eq(Substation::getCityId, cityId).last("LIMIT 1"));
    }

    @Override
    public void changePassword(Long substationId, String oldPassword, String newPassword) {
        Substation sub = substationMapper.selectById(substationId);
        if (sub == null) throw new BizException("账号不存在");
        if (!encryptPass(oldPassword).equals(sub.getUserPass())) {
            throw new BizException("原密码不正确");
        }
        if (encryptPass(newPassword).equals(sub.getUserPass())) {
            throw new BizException("新密码不能与原密码相同");
        }
        substationMapper.update(null, new com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper<Substation>()
                .eq(Substation::getId, substationId)
                .set(Substation::getUserPass, encryptPass(newPassword)));
    }

    private String encryptPass(String pass) {
        return DigestUtils.md5DigestAsHex(pass.getBytes(StandardCharsets.UTF_8));
    }
}