/* * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * Neither the name of the dreamlu.net developer nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * Author: Chill 庄骞 (smallchill@163.com) */ package org.springblade.modules.grid.service.impl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springblade.core.tool.utils.BeanUtil; import org.springblade.modules.grid.entity.GridEntity; import org.springblade.modules.grid.entity.GridmanEntity; import org.springblade.modules.grid.excel.GridmanExcel; import org.springblade.modules.grid.service.IGridService; import org.springblade.modules.grid.vo.GridmanVO; import org.springblade.modules.grid.mapper.GridmanMapper; import org.springblade.modules.grid.service.IGridmanService; import org.springblade.modules.system.entity.Dept; import org.springblade.modules.system.entity.Region; import org.springblade.modules.system.entity.User; import org.springblade.modules.system.service.IRegionService; import org.springblade.modules.system.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.core.metadata.IPage; import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Objects; /** * 网格员表 服务实现类 * * @author BladeX * @since 2023-11-27 */ @Service public class GridmanServiceImpl extends ServiceImpl implements IGridmanService { @Autowired private IUserService userService; @Autowired private IGridService gridService; @Autowired private IRegionService regionService; @Override public IPage selectGridmanPage(IPage page, GridmanVO gridman) { return page.setRecords(baseMapper.selectGridmanPage(page, gridman)); } /** * 网格员表 自定义新增或修改 * @param gridman * @return */ @Override @Transactional(rollbackFor = Exception.class) public boolean saveOrUpdateGridman(GridmanEntity gridman) { boolean flag = false; // 修改 if (null!=gridman.getId()) { // 更新网格员信息 flag = updateById(gridman); }else { // 新增 // 先判断用户表中是否已存在该用户,如果已存在则不新增,需要更新角色 QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("is_deleted", 0).eq("account", gridman.getMobile()); List list = userService.list(wrapper); // 更新用户,查询是否需要更新角色 if (list.size() > 0) { // 默认取出第一个 User user = list.get(0); // 判断角色是否包好网格员的角色,如果没有则加入网格员的角色,有则不更改 if (!user.getRoleId().contains("1717429261910528001")) { user.setRoleId(user.getRoleId() + "," + "1717429261910528001"); // 更新用户信息 userService.updateById(user); } //插入网格员信息 gridman.setUserId(user.getId()); flag = save(gridman); } else { saveUser(gridman); // 插入网格员信息 flag = save(gridman); } } // 返回 return flag; } /** * 用户新增 * @param gridman */ public void saveUser(GridmanEntity gridman) { // 新增用户 User userInfo = new User(); // 设置机构 GridEntity gridEntity = gridService.getById(gridman.getGridId()); if (null != gridEntity) { userInfo.setDeptId(gridEntity.getDeptId().toString()); } // 设置账号信息 userInfo.setAccount(gridman.getMobile()); userInfo.setPhone(gridman.getMobile()); userInfo.setName(gridman.getGridmanName()); userInfo.setRealName(gridman.getGridmanName()); // 设置网格员角色 userInfo.setRoleId("1717429261910528001"); // 设置密码,默认密码为 123456 userInfo.setPassword("123456"); // 插入用户 userService.submit(userInfo); // 设置网格信息 gridman.setUserId(userInfo.getId()); } /** * 网格员导入 * @param data * @param isCovered */ @Override public void importGridman(List data, Boolean isCovered) { // 遍历 for (GridmanExcel gridmanExcel : data) { GridmanEntity gridmanEntity = Objects.requireNonNull(BeanUtil.copy(gridmanExcel, GridmanEntity.class)); // 先查询当前网格社区对应的机构id QueryWrapper regionWrapper = new QueryWrapper<>(); regionWrapper.eq("name",gridmanExcel.getCommunityName()); Region region = regionService.getOne(regionWrapper); // 通过社区名称和网格名称获取网格id QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("is_deleted",0) .eq("community_code",region.getCode()) .eq("grid_name",gridmanExcel.getGridName()); GridEntity gridEntity = gridService.getOne(queryWrapper); // 设置网格id gridmanEntity.setGridId(gridEntity.getId()); // 通过手机号查询网格员id QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("is_deleted",0).eq("mobile",gridmanEntity.getMobile()); GridmanEntity one = getOne(wrapper); if (one!=null){ gridmanEntity.setId(one.getId()); } // 插入网格员 saveOrUpdateGridman(gridmanEntity); } } }