|
|
@@ -1,9 +1,26 @@
|
|
|
package com.usky.system.controller.web;
|
|
|
|
|
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.usky.common.core.exception.BusinessException;
|
|
|
+import com.usky.common.core.bean.ApiResult;
|
|
|
+import com.usky.system.controller.web.page.TableDataInfo;
|
|
|
+import com.usky.system.domain.SysPerson;
|
|
|
+import com.usky.system.domain.SysUserPerson;
|
|
|
+import com.usky.system.service.SysPersonService;
|
|
|
+import com.usky.system.service.SysUserPersonService;
|
|
|
+import com.usky.common.core.util.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
-import org.springframework.stereotype.Controller;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
|
@@ -13,9 +30,158 @@ import org.springframework.stereotype.Controller;
|
|
|
* @author JCB
|
|
|
* @since 2022-08-22
|
|
|
*/
|
|
|
-@Controller
|
|
|
+@RestController
|
|
|
@RequestMapping("/sysPerson")
|
|
|
-public class SysPersonController {
|
|
|
+public class SysPersonController extends BaseController {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private SysPersonService sysPersonService;
|
|
|
+ @Autowired
|
|
|
+ private SysUserPersonService sysUserPersonService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 人员列表(分页)
|
|
|
+ */
|
|
|
+ @GetMapping("/list")
|
|
|
+ public ApiResult<TableDataInfo> list(SysPerson person) {
|
|
|
+ startPage();
|
|
|
+ // 支持 fullName 模糊查询;其他字段保持按传入条件精确匹配
|
|
|
+ SysPerson queryEntity = new SysPerson();
|
|
|
+ if (person != null) {
|
|
|
+ BeanUtils.copyProperties(person, queryEntity);
|
|
|
+ // fullName 用 like 查询,避免 Wrappers.query(entity) 产生 fullName=xxx 的精确条件
|
|
|
+ queryEntity.setFullName(null);
|
|
|
+ }
|
|
|
+ QueryWrapper<SysPerson> queryWrapper = Wrappers.query(queryEntity);
|
|
|
+ queryWrapper.lambda()
|
|
|
+ .like(person != null && StringUtils.isNotBlank(person.getFullName()),
|
|
|
+ SysPerson::getFullName, person.getFullName());
|
|
|
+ List<SysPerson> list = sysPersonService.list(queryWrapper);
|
|
|
+ // 回显每个人员对应的 userId
|
|
|
+ if (list != null && !list.isEmpty()) {
|
|
|
+ List<Integer> personIds = list.stream()
|
|
|
+ .map(SysPerson::getId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ if (!personIds.isEmpty()) {
|
|
|
+ List<SysUserPerson> relations = sysUserPersonService.list(
|
|
|
+ Wrappers.<SysUserPerson>lambdaQuery()
|
|
|
+ .in(SysUserPerson::getPersonId, personIds)
|
|
|
+ );
|
|
|
+ if (relations != null && !relations.isEmpty()) {
|
|
|
+ Map<Integer, Long> personUserMap = relations.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ SysUserPerson::getPersonId,
|
|
|
+ SysUserPerson::getUserId,
|
|
|
+ (u1, u2) -> u1
|
|
|
+ ));
|
|
|
+ list.forEach(p -> {
|
|
|
+ Long userId = personUserMap.get(p.getId());
|
|
|
+ if (userId != null) {
|
|
|
+ p.setUserId(userId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ApiResult.success(getDataTable(list));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据主键获取人员详情
|
|
|
+ */
|
|
|
+ @GetMapping("/{id}")
|
|
|
+ public ApiResult<SysPerson> getInfo(@PathVariable("id") Integer id) {
|
|
|
+ SysPerson person = sysPersonService.getById(id);
|
|
|
+ if (person != null) {
|
|
|
+ SysUserPerson userPerson = sysUserPersonService.getOne(
|
|
|
+ Wrappers.<SysUserPerson>lambdaQuery()
|
|
|
+ .eq(SysUserPerson::getPersonId, id)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+ if (userPerson != null) {
|
|
|
+ person.setUserId(userPerson.getUserId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return ApiResult.success(person);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增人员并绑定用户
|
|
|
+ */
|
|
|
+ @PostMapping
|
|
|
+ @Transactional
|
|
|
+ public ApiResult<Void> add(@RequestBody SysPerson person) {
|
|
|
+ Long userId = person.getUserId();
|
|
|
+ if (Objects.isNull(userId)) {
|
|
|
+ throw new BusinessException("lack necessary param:userId is required!");
|
|
|
+ }
|
|
|
+ // 校验账户是否已被其他人员绑定
|
|
|
+ SysUserPerson existBind = sysUserPersonService.getOne(
|
|
|
+ Wrappers.<SysUserPerson>lambdaQuery()
|
|
|
+ .eq(SysUserPerson::getUserId, userId)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+ if (existBind != null) {
|
|
|
+ throw new BusinessException("此账户已被其它人员绑定,请解绑后重试!");
|
|
|
+ }
|
|
|
+ // 先保存人员信息
|
|
|
+ sysPersonService.save(person);
|
|
|
+ // 新增用户-人员关联
|
|
|
+ SysUserPerson userPerson = new SysUserPerson();
|
|
|
+ userPerson.setUserId(userId);
|
|
|
+ userPerson.setPersonId(person.getId());
|
|
|
+ userPerson.setIsLoginNotify(0);
|
|
|
+ sysUserPersonService.save(userPerson);
|
|
|
+ return ApiResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改人员并更新绑定用户
|
|
|
+ */
|
|
|
+ @PutMapping
|
|
|
+ @Transactional
|
|
|
+ public ApiResult<Void> edit(@RequestBody SysPerson person) {
|
|
|
+ Long userId = person.getUserId();
|
|
|
+ if (Objects.isNull(userId)) {
|
|
|
+ throw new BusinessException("lack necessary param:userId is required!");
|
|
|
+ }
|
|
|
+ // 校验账户是否已被其他人员绑定(排除当前人员本身)
|
|
|
+ SysUserPerson existBind = sysUserPersonService.getOne(
|
|
|
+ Wrappers.<SysUserPerson>lambdaQuery()
|
|
|
+ .eq(SysUserPerson::getUserId, userId)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+ if (existBind != null && !Objects.equals(existBind.getPersonId(), person.getId())) {
|
|
|
+ throw new BusinessException("此账户已被其它人员绑定,请解绑后重试!");
|
|
|
+ }
|
|
|
+ // 更新人员信息
|
|
|
+ sysPersonService.updateById(person);
|
|
|
+ // 先删除原有关联,再按当前传入的 userId 重新绑定
|
|
|
+ QueryWrapper<SysUserPerson> wrapper = Wrappers.query();
|
|
|
+ wrapper.eq("person_id", person.getId());
|
|
|
+ sysUserPersonService.remove(wrapper);
|
|
|
+
|
|
|
+ SysUserPerson userPerson = new SysUserPerson();
|
|
|
+ userPerson.setUserId(userId);
|
|
|
+ userPerson.setPersonId(person.getId());
|
|
|
+ userPerson.setIsLoginNotify(0);
|
|
|
+ sysUserPersonService.save(userPerson);
|
|
|
+ return ApiResult.success();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除人员(支持批量,逗号分隔),并解绑与用户的关联
|
|
|
+ */
|
|
|
+ @DeleteMapping("/{ids}")
|
|
|
+ public ApiResult<Void> remove(@PathVariable("ids") Integer[] ids) {
|
|
|
+ // 先删除关联关系
|
|
|
+ QueryWrapper<SysUserPerson> wrapper = Wrappers.query();
|
|
|
+ wrapper.in("person_id", Arrays.asList(ids));
|
|
|
+ sysUserPersonService.remove(wrapper);
|
|
|
+ // 再删除人员
|
|
|
+ sysPersonService.removeByIds(Arrays.asList(ids));
|
|
|
+ return ApiResult.success();
|
|
|
+ }
|
|
|
}
|
|
|
|