| | |
| | | package com.ycl.platform.service.impl; |
| | | |
| | | import com.ycl.platform.domain.entity.YwPeople; |
| | | import com.ycl.platform.domain.entity.YwUnit; |
| | | import com.ycl.platform.mapper.YwPeopleMapper; |
| | | import com.ycl.platform.mapper.YwUnitMapper; |
| | | import com.ycl.platform.service.YwPeopleService; |
| | | import com.ycl.system.Result; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | |
| | | import java.util.List; |
| | | |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.ycl.system.entity.SysUser; |
| | | import com.ycl.system.mapper.SysUserMapper; |
| | | import com.ycl.system.page.PageUtil; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.Assert; |
| | | |
| | | import java.util.Objects; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | /** |
| | | * 运维人员 服务实现类 |
| | |
| | | public class YwPeopleServiceImpl extends ServiceImpl<YwPeopleMapper, YwPeople> implements YwPeopleService { |
| | | |
| | | private final YwPeopleMapper ywPeopleMapper; |
| | | private final YwUnitMapper ywUnitMapper; |
| | | private final PasswordEncoder passwordEncoder; |
| | | private final SysUserMapper sysUserMapper; |
| | | |
| | | /** |
| | | * 添加 |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result add(YwPeopleForm form) { |
| | | // 为运维人员添加账号 |
| | | SysUser sysUser = new SysUser(); |
| | | fullUser(sysUser, form); |
| | | sysUserMapper.insertUser(sysUser); |
| | | |
| | | YwPeople entity = YwPeopleForm.getEntityByForm(form, null); |
| | | entity.setUserId(sysUser.getUserId()); |
| | | entity.setAddWay("manual"); |
| | | if(baseMapper.insert(entity) > 0) { |
| | | return Result.ok("添加成功"); |
| | | } |
| | | return Result.error("添加失败"); |
| | | } |
| | | |
| | | /** |
| | | * 赋值user |
| | | * @param sysUser |
| | | * @param form |
| | | */ |
| | | public void fullUser(SysUser sysUser, YwPeopleForm form) { |
| | | sysUser.setUserName(form.getYwPersonAccount()); |
| | | sysUser.setPassword(passwordEncoder.encode("123456")); |
| | | sysUser.setStatus("0"); |
| | | sysUser.setDelFlag("0"); |
| | | sysUser.setFirstLogin((short) 0); |
| | | sysUser.setNickName(form.getYwPersonName()); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result update(YwPeopleForm form) { |
| | | |
| | | YwPeople entity = baseMapper.selectById(form.getId()); |
| | | |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | |
| | | SysUser sysUser = sysUserMapper.selectUserById(entity.getUserId()); |
| | | sysUser.setUserName(form.getYwPersonAccount()); |
| | | sysUser.setNickName(form.getYwPersonName()); |
| | | sysUserMapper.updateUser(sysUser); |
| | | |
| | | BeanUtils.copyProperties(form, entity); |
| | | if (baseMapper.updateById(entity) > 0) { |
| | | return Result.ok("修改成功"); |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result remove(List<String> ids) { |
| | | if(baseMapper.deleteBatchIds(ids) > 0) { |
| | | return Result.ok("删除成功"); |
| | | } |
| | | return Result.error("删除失败"); |
| | | List<YwPeople> list = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .select(YwPeople::getUserId) |
| | | .in(YwPeople::getId, ids) |
| | | .list(); |
| | | List<Long> userIds = list.stream().map(YwPeople::getUserId).collect(Collectors.toList()); |
| | | baseMapper.deleteBatchIds(ids); |
| | | sysUserMapper.deleteUserByIds((Long[]) userIds.toArray()); |
| | | return Result.ok("删除成功"); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public Result removeById(String id) { |
| | | if(baseMapper.deleteById(id) > 0) { |
| | | return Result.ok("删除成功"); |
| | | } |
| | | return Result.error("删除失败"); |
| | | YwPeople ywPeople = baseMapper.selectById(id); |
| | | sysUserMapper.deleteUserById(ywPeople.getUserId()); |
| | | baseMapper.deleteById(id); |
| | | return Result.ok("删除成功"); |
| | | } |
| | | |
| | | /** |
| | |
| | | public Result page(YwPeopleQuery query) { |
| | | |
| | | IPage<YwPeople> page = new LambdaQueryChainWrapper<>(baseMapper) |
| | | .eq(StringUtils.hasText(query.getYwPersonCode()), YwPeople::getYwPersonCode, query.getYwPersonCode()) |
| | | .like(StringUtils.hasText(query.getYwPersonName()), YwPeople::getYwPersonName, query.getYwPersonName()) |
| | | .eq(Objects.nonNull(query.getBelongUnit()), YwPeople::getBelongUnit, query.getBelongUnit()) |
| | | .orderByDesc(YwPeople::getCreateTime) |
| | | .page(PageUtil.getPage(query, YwPeople.class)); |
| | | |
| | | List<YwPeopleVO> vos = page.getRecords().stream() |
| | | .map( |
| | | entity -> YwPeopleVO.getVoByEntity(entity, null) |
| | | entity -> { |
| | | YwPeopleVO vo = YwPeopleVO.getVoByEntity(entity, null); |
| | | YwUnit unit = new LambdaQueryChainWrapper<>(ywUnitMapper) |
| | | .eq(YwUnit::getId, vo.getBelongUnit()) |
| | | .select(YwUnit::getUnitName) |
| | | .one(); |
| | | if (Objects.nonNull(unit)) { |
| | | vo.setBelongUnitName(unit.getUnitName()); |
| | | } |
| | | return vo; |
| | | } |
| | | ) |
| | | .collect(Collectors.toList()); |
| | | return Result.ok().data(vos).total(page.getTotal()); |