|  |  |  | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.moral.api.config.mybatis.wrapper.NullFilterWrapper; | 
|---|
|  |  |  | import com.moral.api.entity.Group; | 
|---|
|  |  |  | import com.moral.api.entity.Organization; | 
|---|
|  |  |  | import com.moral.api.entity.User; | 
|---|
|  |  |  | import com.moral.api.mapper.OrganizationMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.UserMapper; | 
|---|
|  |  |  | import com.moral.api.entity.UserGroup; | 
|---|
|  |  |  | import com.moral.api.mapper.*; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.user.UserDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.user.UserQueryDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.form.user.UserDeleteForm; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.moral.api.pojo.form.user.UserUpdateForm; | 
|---|
|  |  |  | import com.moral.api.service.UserService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.util.CompareFieldUtils; | 
|---|
|  |  |  | import com.moral.api.util.LogUtils; | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import com.moral.pojo.CompareFieldResult; | 
|---|
|  |  |  | import com.moral.util.ConvertUtils; | 
|---|
|  |  |  | import com.moral.util.DateUtils; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.logging.SimpleFormatter; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * <p> | 
|---|
|  |  |  | 
|---|
|  |  |  | UserMapper userMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | OrganizationMapper organizationMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | GroupMapper groupMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | UserGroupMapper userGroupMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | GroupMenuMapper groupMenuMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public UserQueryDTO queryUsers(UserQueryForm form) { | 
|---|
|  |  |  | 
|---|
|  |  |  | //更新 | 
|---|
|  |  |  | userMapper.updateById(user); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取更新后的用户 | 
|---|
|  |  |  | user = userMapper.selectById(user.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | updatelog(oldUser,user); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //封装返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public UserDTO deleteUser(UserDeleteForm form) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | UserDTO dto = new UserDTO(); | 
|---|
|  |  |  | //取参 | 
|---|
|  |  |  | Integer id = form.getId(); | 
|---|
|  |  |  | //查询要删除的用户 用于插入日志 | 
|---|
|  |  |  | User oldUser = new User(); | 
|---|
|  |  |  | oldUser.setIsDelete(Constants.NOT_DELETE); | 
|---|
|  |  |  | oldUser.setId(id); | 
|---|
|  |  |  | QueryWrapper<User> oldWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | oldWrapper.setEntity(oldUser); | 
|---|
|  |  |  | oldUser = userMapper.selectOne(oldWrapper); | 
|---|
|  |  |  | if(ObjectUtils.isEmpty(oldUser)){ | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.USER_NOT_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.USER_NOT_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //逻辑删除,删除账号以及该组织下的所有账号 | 
|---|
|  |  |  | UpdateWrapper<User> deleteWrapper = new UpdateWrapper<>(); | 
|---|
|  |  |  | deleteWrapper.eq("organization_id",oldUser.getOrganizationId()); | 
|---|
|  |  |  | deleteWrapper.set("is_delete",Constants.DELETE); | 
|---|
|  |  |  | userMapper.update(null,deleteWrapper); | 
|---|
|  |  |  | //返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<User> wrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper.setEntity(existUser); | 
|---|
|  |  |  | User existUserResult = userMapper.selectOne(wrapper); | 
|---|
|  |  |  | if(!ObjectUtils.isEmpty(existUserResult)){ | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(existUserResult)) { | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.USER_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.USER_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<User> organizationUserWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | organizationUserWrapper.setEntity(organizationUser); | 
|---|
|  |  |  | organizationUser = userMapper.selectOne(organizationUserWrapper); | 
|---|
|  |  |  | if(!ObjectUtils.isEmpty(organizationUser)){ | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(organizationUser)) { | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.ORGANIZATION_USER_EXIST.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.ORGANIZATION_USER_EXIST.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //插入 | 
|---|
|  |  |  | //插入用户表 | 
|---|
|  |  |  | userMapper.insert(user); | 
|---|
|  |  |  | //创建组织admin角色 | 
|---|
|  |  |  | Group group = new Group(); | 
|---|
|  |  |  | group.setOrganizationId(organizationId); | 
|---|
|  |  |  | group.setGroupName("admin"); | 
|---|
|  |  |  | groupMapper.insert(group); | 
|---|
|  |  |  | //插入账号角色关联表 | 
|---|
|  |  |  | UserGroup userGroup = new UserGroup(); | 
|---|
|  |  |  | userGroup.setUserId(user.getId()); | 
|---|
|  |  |  | userGroup.setGroupId(group.getId()); | 
|---|
|  |  |  | userGroup.setOrganizationId(organizationId); | 
|---|
|  |  |  | userGroupMapper.insert(userGroup); | 
|---|
|  |  |  | //更新组织 | 
|---|
|  |  |  | Organization organization = new Organization(); | 
|---|
|  |  |  | organization.setAdminUserId(user.getId()); | 
|---|
|  |  |  | organization.setId(organizationId); | 
|---|
|  |  |  | organizationMapper.updateById(organization); | 
|---|
|  |  |  | //操作插入日志 | 
|---|
|  |  |  | insertLog(user,organizationId); | 
|---|
|  |  |  | //封装返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public UserDTO deleteUsersByOrganizationId(Integer organizationId) { | 
|---|
|  |  |  | //创建返回对象 | 
|---|
|  |  |  | UserDTO dto = new UserDTO(); | 
|---|
|  |  |  | //逻辑删除,删除账号以及该组织下的所有账号 | 
|---|
|  |  |  | UpdateWrapper<User> deleteUserChildrenWrapper = new UpdateWrapper<>(); | 
|---|
|  |  |  | deleteUserChildrenWrapper.eq("organization_id", organizationId); | 
|---|
|  |  |  | deleteUserChildrenWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | userMapper.update(null, deleteUserChildrenWrapper); | 
|---|
|  |  |  | //逻辑删除,groupMenu | 
|---|
|  |  |  | UpdateWrapper deleteGroupMenuWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteGroupMenuWrapper.eq("organization_id", organizationId); | 
|---|
|  |  |  | deleteGroupMenuWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | groupMenuMapper.update(null, deleteGroupMenuWrapper); | 
|---|
|  |  |  | //逻辑删除,userGroup | 
|---|
|  |  |  | UpdateWrapper deleteUserGroupWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteUserGroupWrapper.eq("organization_id", organizationId); | 
|---|
|  |  |  | deleteUserGroupWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | userGroupMapper.update(null, deleteUserGroupWrapper); | 
|---|
|  |  |  | //逻辑删除,group | 
|---|
|  |  |  | UpdateWrapper deleteGroupWrapper = new UpdateWrapper(); | 
|---|
|  |  |  | deleteGroupWrapper.eq("organization_id", organizationId); | 
|---|
|  |  |  | deleteGroupWrapper.set("is_delete", Constants.DELETE); | 
|---|
|  |  |  | groupMapper.update(null, deleteGroupWrapper); | 
|---|
|  |  |  | //返回结果 | 
|---|
|  |  |  | dto.setCode(ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return dto; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 插入操作插入日志 | 
|---|
|  |  |  | * @Param: [user, organizationId] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/8/24 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void insertLog(User user,Integer organizationId) { | 
|---|
|  |  |  | StringBuilder content = new StringBuilder("添加了前台用户;"); | 
|---|
|  |  |  | content.append("角色:admin;"); | 
|---|
|  |  |  | //查询添加admin用户的组织 | 
|---|
|  |  |  | Organization organization = organizationMapper.selectById(organizationId); | 
|---|
|  |  |  | content.append("组织:"+organization.getName()+";"); | 
|---|
|  |  |  | content.append("账号:"+user.getAccount()+";"); | 
|---|
|  |  |  | content.append("用户名称:"+user.getUserName()+";"); | 
|---|
|  |  |  | content.append("过期时间:"+ DateUtils.dateToDateString(user.getExpireTime(),"yyyy-MM-dd")+";"); | 
|---|
|  |  |  | if(user.getEmail()!=null) | 
|---|
|  |  |  | content.append("邮箱:"+user.getEmail()+";"); | 
|---|
|  |  |  | if(user.getMobile()!=null) | 
|---|
|  |  |  | content.append("手机:"+user.getMobile()+";"); | 
|---|
|  |  |  | if(user.getWechat()!=null) | 
|---|
|  |  |  | content.append("微信:"+user.getWechat()+";"); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(content.toString(), Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @Description: 更新操作插入日志 | 
|---|
|  |  |  | * @Param: [oldUser, newUser] | 
|---|
|  |  |  | * @return: void | 
|---|
|  |  |  | * @Author: 陈凯裕 | 
|---|
|  |  |  | * @Date: 2021/8/24 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void updatelog(User oldUser,User newUser){ | 
|---|
|  |  |  | List<CompareFieldResult> results = CompareFieldUtils.compare(User.class, oldUser, newUser); | 
|---|
|  |  |  | StringBuilder content = new StringBuilder("修改了前台用户;"); | 
|---|
|  |  |  | content.append("所属组织:"+organizationMapper.selectById(oldUser.getOrganizationId()).getName()+";"); | 
|---|
|  |  |  | //密码特殊处理 | 
|---|
|  |  |  | if(newUser.getPassword()!=null) | 
|---|
|  |  |  | content.append("修改了密码;"); | 
|---|
|  |  |  | String resultContent = CompareFieldUtils.resultsConvertContent(results,content.toString()); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(resultContent, Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|