package com.huaxu.service; import com.baomidou.mybatisplus.core.metadata.IPage; import com.huaxu.common.ToolUtil; import com.huaxu.dao.UserMapper; import com.huaxu.entity.UserEntity; import com.huaxu.entity.UserRoleEntity; import com.huaxu.entity.UserTagEntity; import com.huaxu.model.LoginUser; import com.huaxu.model.ProgramItem; import com.huaxu.util.UserUtil; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import javax.annotation.Resource; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.Arrays; /** * * 用户Service接口 * @author: WYY * @date 2020-10-22 17:35 */ @Service public class UserService extends ServiceImpl { @Resource private UserMapper userMapper; @Resource private UserTagService userTagService; @Resource private UserRoleService userRoleService; /** * 自定义分页查询,含关联实体对像 */ public IPage findPage(IPage page, UserEntity userEntity) { LoginUser currentUser = UserUtil.getCurrentUser(); userEntity.setTenantId(currentUser.getTenantId()); userEntity.setProgramItems(currentUser.getProgramItemList()); userEntity.setUserType(currentUser.getType()); Page userPage = userMapper.findPage(page, userEntity); return userPage; } /** * 查列表 */ public List findList(UserEntity userEntity) { LoginUser currentUser = UserUtil.getCurrentUser(); userEntity.setTenantId(currentUser.getTenantId()); return userMapper.findList(userEntity); } /** * 批量删除 */ @Transactional(rollbackFor = Exception.class) public boolean delUserByIds(Long[] ids) { for (Long id : ids) { //批量删除附件 } return this.removeByIds(Arrays.asList(ids)); } public boolean checkMobileUnique(String phone) { LoginUser currentUser = UserUtil.getCurrentUser(); int count = this.count(new QueryWrapper().eq("phone", phone).eq("tenant_id",currentUser.getTenantId())); if (count > 0) { return true; } return false; } public boolean checkMobileUnique(UserEntity user) { LoginUser currentUser = UserUtil.getCurrentUser(); Long userId = ToolUtil.isEmpty(user.getId()) ? -1L : user.getId(); UserEntity info = this.getOne(new QueryWrapper().eq("phone", user.getPhone()).eq("tenant_id",currentUser.getTenantId())); if (ToolUtil.isNotEmpty(info) && !info.getId().equals(userId)) { return true; } return false; } /** * 单个删除 */ public boolean delUserById(Long id) { //todo 移除附件 return this.removeById(id); } /** * 保存 */ public boolean addUser(UserEntity user) { if (this.save(user)) { Long pkId = user.getId(); //添加用户标签 if (user.getUserTags() != null) { for (Long item : user.getUserTags()) { UserTagEntity userTagEntity = new UserTagEntity(); userTagEntity.setCreateBy(user.getCreateBy()); userTagEntity.setUpdateBy(user.getUpdateBy()); userTagEntity.setDateCreate(user.getDateCreate()); userTagEntity.setDateUpdate(user.getDateUpdate()); userTagEntity.setStatus(1); userTagEntity.setTagId(Long.valueOf(item)); userTagEntity.setUserId(pkId); userTagService.addUserTag(userTagEntity); } } //添加角色 if (user.getRoleId() != null) { UserRoleEntity userRoleEntity = new UserRoleEntity(); userRoleEntity.setCreateBy(user.getCreateBy()); userRoleEntity.setUpdateBy(user.getUpdateBy()); userRoleEntity.setDateCreate(user.getDateCreate()); userRoleEntity.setDateUpdate(user.getDateUpdate()); userRoleEntity.setStatus(1); userRoleEntity.setRoleId(Long.valueOf(user.getRoleId())); userRoleEntity.setUserId(pkId); userRoleService.addUserRole(userRoleEntity); } return true; } return false; } /** * 修改根居ID */ public boolean updateUserById(UserEntity user) { if (this.updateById(user)) { //更新关联附件信息 Long pkId = user.getId(); //添加用户标签 if (user.getUserTags() != null && user.getUserTags().size() > 0) { //先删除之前的标签 List userTagEntitys = new ArrayList<>(); UserTagEntity userTagDelete = new UserTagEntity(); userTagDelete.setUserId(pkId); userTagEntitys = userTagService.findList(userTagDelete); Long[] ids = new Long[userTagEntitys.size()]; for (int i = 0; i < userTagEntitys.size(); i++) { ids[i] = userTagEntitys.get(i).getId(); } userTagService.delUserTagByIds(ids); //新增标签 for (Long item : user.getUserTags()) { UserTagEntity userTagEntity = new UserTagEntity(); userTagEntity.setCreateBy(user.getCreateBy()); userTagEntity.setUpdateBy(user.getUpdateBy()); userTagEntity.setDateCreate(user.getDateCreate()); userTagEntity.setDateUpdate(user.getDateUpdate()); userTagEntity.setStatus(1); userTagEntity.setTagId(Long.valueOf(item)); userTagEntity.setUserId(pkId); userTagService.addUserTag(userTagEntity); } } //修改角色 if (user.getRoleId() != null) { List userRoleEntities = new ArrayList<>(); UserRoleEntity userRoleEntity = new UserRoleEntity(); userRoleEntity.setUserId(pkId); userRoleEntities = userRoleService.findList(userRoleEntity); if (userRoleEntities.size() > 0) { Long userRoleId = userRoleEntities.get(0).getId(); userRoleService.delUserRoleById(userRoleId); } //新增角色 UserRoleEntity userRole = new UserRoleEntity(); userRole.setCreateBy(user.getCreateBy()); userRole.setUpdateBy(user.getUpdateBy()); userRole.setDateCreate(user.getDateCreate()); userRole.setDateUpdate(user.getDateUpdate()); userRole.setStatus(1); userRole.setRoleId(Long.valueOf(user.getRoleId())); userRole.setUserId(pkId); userRoleService.addUserRole(userRole); } return true; } return false; } /** * 根居ID获取对象 */ public UserEntity findUserById(Long id) { List userTagEntitys = new ArrayList<>(); UserTagEntity userTags = new UserTagEntity(); userTags.setUserId(id); userTagEntitys = userTagService.findList(userTags); List ids = new ArrayList<>(); for (int i = 0; i < userTagEntitys.size(); i++) { ids.add(userTagEntitys.get(i).getTagId()); } UserEntity userEntity = userMapper.findUserById(id); userEntity.setUserTags(ids); return userEntity; } }