|
@@ -2,21 +2,21 @@ package com.anyway.favor.controller;
|
|
|
|
|
|
import com.anyway.favor.model.Person;
|
|
|
import com.anyway.favor.model.User;
|
|
|
+import com.anyway.favor.model.enums.Update;
|
|
|
+import com.anyway.favor.model.vo.PersonVo;
|
|
|
import com.anyway.favor.service.PersonService;
|
|
|
-import com.anyway.util.Couple;
|
|
|
import com.anyway.util.PageQuery;
|
|
|
+import com.anyway.util.Pair;
|
|
|
import com.anyway.util.R;
|
|
|
import com.anyway.util.SessionUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
-import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import javax.validation.constraints.NotBlank;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 人员控制器
|
|
@@ -43,100 +43,42 @@ public class PersonController {
|
|
|
person.setCreateBy(currentUser.getId());
|
|
|
return personService.add(person) ? R.ok() : R.fail("新增失败");
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
- * 跳转添加人员页面
|
|
|
+ * 更新人员
|
|
|
*
|
|
|
+ * @param person
|
|
|
* @return
|
|
|
*/
|
|
|
- @RequestMapping("/toAdd")
|
|
|
- public ModelAndView toAdd(ModelAndView mv) {
|
|
|
+ @PostMapping("/update")
|
|
|
+ public R<Void> update(@Validated({Update.class}) @RequestBody Person person) {
|
|
|
User currentUser = SessionUtils.currentUser();
|
|
|
- Map<String, Object> mapCondition = new HashMap<>();
|
|
|
- mapCondition.put("relatedId", currentUser.getId());
|
|
|
- //母亲列表
|
|
|
- mapCondition.put("gender", "F");
|
|
|
- mapCondition.put("maritalStatus", "1");
|
|
|
- List<Person> motherPersonList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("motherPersonOptionList", Couple.toList(motherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
|
|
|
- //父亲列表
|
|
|
- mapCondition.put("gender", "M");
|
|
|
- List<Person> fatherPersonList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("fatherPersonOptionList", Couple.toList(fatherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
|
|
|
- //配偶列表
|
|
|
- mapCondition.put("gender", null);
|
|
|
- mapCondition.put("maritalStatus", null);
|
|
|
- mapCondition.put("emptyMate", true);
|
|
|
- mapCondition.put("sortName", "marital_status");
|
|
|
- mapCondition.put("sortOrder", "asc");
|
|
|
- List<Person> matePersonList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("matePersonOptionList", Couple.toList(matePersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
|
|
|
- mv.setViewName("/person/edit");
|
|
|
- return mv;
|
|
|
+ person.setModifyBy(currentUser.getId());
|
|
|
+ return personService.update(person) ? R.ok() : R.fail("保存失败");
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
- * 跳转编辑人员页面
|
|
|
+ * 人员详情
|
|
|
*
|
|
|
+ * @param id
|
|
|
* @return
|
|
|
*/
|
|
|
- @RequestMapping("/edit/{id}")
|
|
|
- public ModelAndView edit(@PathVariable Long id) {
|
|
|
- ModelAndView mv = new ModelAndView();
|
|
|
+ @GetMapping("/detail/{id}")
|
|
|
+ public R<PersonVo> detail(@PathVariable Long id) {
|
|
|
Person person = personService.findById(id);
|
|
|
- mv.addObject("person", person);
|
|
|
- mv.setViewName("/person/edit");
|
|
|
-
|
|
|
- User currentUser = SessionUtils.currentUser();
|
|
|
- Map<String, Object> mapCondition = new HashMap<>();
|
|
|
- mapCondition.put("relatedId", currentUser.getId());
|
|
|
- //母亲列表(女且已婚)
|
|
|
- mapCondition.put("gender", "F");
|
|
|
- mapCondition.put("maritalStatus", "1");
|
|
|
- List<Person> motherPersonList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("motherPersonOptionList", Couple.toList(motherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
|
|
|
- //父亲列表(男且已婚)
|
|
|
- mapCondition.put("gender", "M");
|
|
|
- List<Person> fatherPersonList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("fatherPersonOptionList", Couple.toList(fatherPersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
|
|
|
- //配偶列表(男/女且配偶为空且未婚)
|
|
|
- mapCondition.put("gender", "F".equals(person.getGender()) ? "M" : "F");
|
|
|
- mapCondition.put("emptyMate", true);
|
|
|
- mapCondition.put("maritalStatus", "0");
|
|
|
- List<Person> matePersonList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("matePersonOptionList", Couple.toList(matePersonList, e -> e.getCallName() + "(" + e.getName() + ")", e -> String.valueOf(e.getId())));
|
|
|
- return mv;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 更新人员
|
|
|
- *
|
|
|
- * @param person
|
|
|
- * @return
|
|
|
- */
|
|
|
- @ResponseBody
|
|
|
- @RequestMapping("/update")
|
|
|
- public R update(@RequestBody Person person) {
|
|
|
- User currentUser = SessionUtils.currentUser();
|
|
|
- person.setModifyBy(currentUser.getId());
|
|
|
- return personService.update(person) ? R.ok() : R.fail("保存失败");
|
|
|
+ return R.data(this.toPersonVo(person));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 跳转人员列表页面
|
|
|
- *
|
|
|
+ * 转为 PersonVo
|
|
|
+ * @param person
|
|
|
* @return
|
|
|
*/
|
|
|
- @RequestMapping("/toList")
|
|
|
- public ModelAndView toList(@RequestParam Map<String, Object> mapCondition) {
|
|
|
- ModelAndView mv = new ModelAndView();
|
|
|
- //查询和当前登录用户相关的人员
|
|
|
- User currentUser = SessionUtils.currentUser();
|
|
|
- mapCondition.put("relatedId", currentUser.getId());
|
|
|
- List<Person> personList = personService.findByCondition(mapCondition);
|
|
|
- mv.addObject("familyPersonList", personList);
|
|
|
- mv.setViewName("/person/list");
|
|
|
- return mv;
|
|
|
+ private PersonVo toPersonVo(Person person) {
|
|
|
+ PersonVo personVo = new PersonVo(person);
|
|
|
+ personVo.setMatePerson(personService.findById(person.getMateId()));
|
|
|
+ personVo.setFatherPerson(personService.findById(person.getFatherId()));
|
|
|
+ personVo.setMotherPerson(personService.findById(person.getMotherId()));
|
|
|
+ return personVo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -147,13 +89,18 @@ public class PersonController {
|
|
|
*/
|
|
|
@ResponseBody
|
|
|
@RequestMapping("/listPage")
|
|
|
- public R listPage(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
|
|
|
+ public R<List<PersonVo>> listPage(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
|
|
|
User currentUser = SessionUtils.currentUser();
|
|
|
- Map<String, Object> mapCondition = pageQuery.getTerms();
|
|
|
+ Map<String, Object> mapCondition = pageQuery.getTerms(new HashMap<>());
|
|
|
//查询和当前登录用户相关的人员
|
|
|
- mapCondition.put("relatedId", currentUser.getId());
|
|
|
+ mapCondition.put("createBy", currentUser.getId());
|
|
|
List<Person> personList = personService.findPage(pageQuery);
|
|
|
- return R.page(personList, pageQuery.getPage());
|
|
|
+ //设置关联属性
|
|
|
+ List<PersonVo> personVoList = new ArrayList<>();
|
|
|
+ for (Person person : personList) {
|
|
|
+ personVoList.add(this.toPersonVo(person));
|
|
|
+ }
|
|
|
+ return R.page(personVoList, pageQuery.getPage());
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -161,37 +108,32 @@ public class PersonController {
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- @ResponseBody
|
|
|
- @RequestMapping("/listOption")
|
|
|
- public List<Couple> listOption(Person person) {
|
|
|
+ @GetMapping("/listOption")
|
|
|
+ public R<List<Pair<String, Long>>> listOption(@RequestBody PageQuery<Map<String, Object>> pageQuery) {
|
|
|
User currentUser = SessionUtils.currentUser();
|
|
|
- Map<String, Object> mapCondition = new HashMap<>();
|
|
|
- if (!StringUtils.isEmpty(person.getGender())) {
|
|
|
- mapCondition.put("gender", person.getGender());
|
|
|
- }
|
|
|
- if (person.getMaritalStatus() != null) {
|
|
|
- mapCondition.put("maritalStatus", person.getMaritalStatus());
|
|
|
- }
|
|
|
- mapCondition.put("relatedId", currentUser.getId());
|
|
|
- List<Person> personList = personService.findByCondition(mapCondition);
|
|
|
- List<Couple> coupleList = new ArrayList<>();
|
|
|
- for (Person p : personList) {
|
|
|
- coupleList.add(new Couple(p.getId().toString(), p.getCallName() + "("+p.getName()+")"));
|
|
|
- }
|
|
|
- return coupleList;
|
|
|
+ Map<String, Object> mapCondition = pageQuery.getTerms(new HashMap<>());
|
|
|
+ //查询和当前登录用户相关的人员
|
|
|
+ mapCondition.put("createBy", currentUser.getId());
|
|
|
+ List<Person> personList = personService.findPage(pageQuery);
|
|
|
+ List<Pair<String, Long>> personOptionList = Pair.toList(personList, e -> e.getCallName() + "(" + e.getName() + ")", Person::getId);
|
|
|
+ return R.data(personOptionList);
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 删除人员
|
|
|
+ * 批量删除
|
|
|
*
|
|
|
- * @param id
|
|
|
+ * @param ids
|
|
|
* @return
|
|
|
*/
|
|
|
- @ResponseBody
|
|
|
- @RequestMapping("/delete/{id}")
|
|
|
- public R delete(@PathVariable Long id) {
|
|
|
- int c = personService.delete(id);
|
|
|
- return c > 0? R.ok() : R.fail("删除失败");
|
|
|
+ @PostMapping("/delete")
|
|
|
+ public R<Void> delete(@RequestParam(defaultValue = "") @NotBlank(message = "ids不能为空") String ids) {
|
|
|
+ if(!StringUtils.hasText(ids)) {
|
|
|
+ return R.fail("无效的ID");
|
|
|
+ }
|
|
|
+ String[] arr = ids.split(",");
|
|
|
+ List<Long> idList = Arrays.stream(arr).map(Long::valueOf).collect(Collectors.toList());
|
|
|
+ int r = personService.deleteByIds(idList);
|
|
|
+ return r > 0 ? R.ok() : R.fail("删除失败");
|
|
|
}
|
|
|
}
|