LsUserController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package com.liang.controller;
  2. import com.github.pagehelper.PageHelper;
  3. import com.github.pagehelper.PageInfo;
  4. import com.liang.common.JsonTool;
  5. import com.liang.common.base.BaseResult;
  6. import com.liang.common.utils.DateUtil;
  7. import com.liang.entity.BasePerson;
  8. import com.liang.entity.SysLsUserEntity;
  9. import com.liang.service.BaseAreaService;
  10. import com.liang.service.BaseEducationService;
  11. import com.liang.service.LsUserService;
  12. import io.swagger.annotations.ApiOperation;
  13. import io.swagger.models.auth.In;
  14. import org.apache.commons.io.FilenameUtils;
  15. import org.apache.ibatis.annotations.Delete;
  16. import org.apache.poi.ss.usermodel.Cell;
  17. import org.apache.poi.ss.usermodel.Row;
  18. import org.apache.poi.ss.usermodel.Sheet;
  19. import org.apache.poi.ss.usermodel.Workbook;
  20. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.core.io.ClassPathResource;
  23. import org.springframework.http.HttpHeaders;
  24. import org.springframework.http.HttpStatus;
  25. import org.springframework.http.MediaType;
  26. import org.springframework.http.ResponseEntity;
  27. import org.springframework.stereotype.Controller;
  28. import org.springframework.ui.Model;
  29. import org.springframework.web.bind.annotation.*;
  30. import org.springframework.web.multipart.MultipartFile;
  31. import org.springframework.web.multipart.MultipartHttpServletRequest;
  32. import javax.annotation.Resource;
  33. import javax.xml.transform.Result;
  34. import java.io.IOException;
  35. import java.io.InputStream;
  36. import java.nio.file.Path;
  37. import java.nio.file.Paths;
  38. import java.text.DateFormat;
  39. import java.text.SimpleDateFormat;
  40. import java.time.LocalDate;
  41. import java.time.format.DateTimeFormatter;
  42. import java.util.*;
  43. import static com.liang.common.base.BaseController.RESULT_ROWS;
  44. import static com.liang.common.base.BaseController.RESULT_TOTAL;
  45. /**
  46. * @Author ly
  47. * @Date 2024/4/3 9:42
  48. * @Version 1.0
  49. */
  50. @Controller
  51. @RequestMapping("/lsUser")
  52. public class LsUserController {
  53. @Resource
  54. private LsUserService lsUserService;
  55. @Resource
  56. private BaseEducationService baseEducationService;
  57. // 允许的表格文件扩展名列表
  58. private final List<String> allowedExtensions = Arrays.asList("xls", "xlsx", "csv");
  59. @ApiOperation(value = "页面初始化", notes = "页面初始化")
  60. @RequestMapping(value = "/init", method = RequestMethod.GET)
  61. public String init( Model model) {
  62. List<Map<String, String>> xlList = baseEducationService.getEducationList();
  63. model.addAttribute("xlList", xlList);
  64. return "lsuserinfo/list";
  65. }
  66. /**
  67. *
  68. * @return
  69. */
  70. @ResponseBody
  71. @RequestMapping(value = "/all",method = RequestMethod.GET)
  72. public BaseResult UserAll(@RequestParam("page") Integer pageNum,
  73. @RequestParam("limit") Integer pageSize,
  74. @RequestParam(required = false, defaultValue = "", value = "xm") String xm,
  75. @RequestParam(required = false, defaultValue = "", value = "zt") Integer zt,
  76. @RequestParam(required = false, defaultValue = "", value = "xl") String xl,
  77. @RequestParam(required = false, defaultValue = "", value = "sfzh") String sfzh){
  78. Map<String, String> paraMap = new HashMap<>();
  79. PageHelper.startPage(pageNum, pageSize);
  80. if (zt == null){
  81. paraMap.put("xm", xm);
  82. paraMap.put("xl",xl);
  83. paraMap.put("sfzh",sfzh);
  84. List<SysLsUserEntity> list = lsUserService.UserAll(paraMap);
  85. PageInfo<SysLsUserEntity> pageinfo = new PageInfo<>(list);
  86. // 取出查询结果
  87. List<SysLsUserEntity> rows = pageinfo.getList();
  88. int total = (int) pageinfo.getTotal();
  89. Map<String, Object> result = new HashMap<>();
  90. result.put(RESULT_ROWS, rows);
  91. result.put(RESULT_TOTAL, total);
  92. result.put("list",list);
  93. return BaseResult.success(result);
  94. }
  95. paraMap.put("xm", xm);
  96. paraMap.put("xl",xl);
  97. paraMap.put("zt",String.valueOf(zt));
  98. List<SysLsUserEntity> list = lsUserService.UserAll(paraMap);
  99. PageInfo<SysLsUserEntity> pageinfo = new PageInfo<>(list);
  100. // 取出查询结果
  101. List<SysLsUserEntity> rows = pageinfo.getList();
  102. int total = (int) pageinfo.getTotal();
  103. Map<String, Object> result = new HashMap<>();
  104. result.put(RESULT_ROWS, rows);
  105. result.put(RESULT_TOTAL, total);
  106. result.put("list",list);
  107. return BaseResult.success(result);
  108. }
  109. @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
  110. public String forAdd(Model model) {
  111. List<Map<String, String>> xlList = baseEducationService.getEducationList();
  112. model.addAttribute("xlList", xlList);
  113. return "lsuserinfo/add";
  114. }
  115. @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
  116. public String forEdit(@PathVariable Integer id){
  117. return "lsuserinfo/add";
  118. }
  119. /**
  120. * 保存数据
  121. * @param
  122. * @return
  123. */
  124. @PostMapping("/save")
  125. @ResponseBody
  126. public BaseResult saveOperLog(@RequestBody SysLsUserEntity sysLsUserEntity) {
  127. Map<String, String>map = new HashMap<>();
  128. map.put("sfzh",sysLsUserEntity.getSfzh());
  129. List<SysLsUserEntity> list = lsUserService.getUserxm(map);
  130. if (list.size() == 0) {
  131. SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  132. Date date = new Date(System.currentTimeMillis());
  133. sysLsUserEntity.setCjsj(formatter.format(date));
  134. int num = lsUserService.saveOperLog(sysLsUserEntity);
  135. return BaseResult.success("新增临时人员成功!");
  136. }
  137. return BaseResult.failure("新建临时人员失败,该人员已存在!");
  138. }
  139. @RequestMapping(value="/update/{id}", method = RequestMethod.POST)
  140. @ResponseBody
  141. public BaseResult batchDelete(@RequestBody SysLsUserEntity sysLsUserEntity,@PathVariable Integer id){
  142. sysLsUserEntity.setZt(lsUserService.getUserId(id).getZt());
  143. sysLsUserEntity.setId(id);
  144. sysLsUserEntity.setX(1);
  145. boolean a = lsUserService.upDate(sysLsUserEntity);
  146. if (a != true){
  147. return BaseResult.failure("修改失败");
  148. }
  149. return BaseResult.success("修改成功");
  150. }
  151. /**
  152. * 删除数据
  153. * @param id
  154. * @return
  155. */
  156. @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
  157. @ResponseBody
  158. public BaseResult delete (@PathVariable Integer id){
  159. SysLsUserEntity sysLsUserEntity = lsUserService.getUserId(id);
  160. sysLsUserEntity.setX(0);
  161. boolean flag = lsUserService.upDate(sysLsUserEntity);
  162. if (flag) {
  163. return BaseResult.success("删除成功");
  164. } else {
  165. return BaseResult.failure("删除失败");
  166. }
  167. }
  168. /**
  169. * 修改数据
  170. * @param
  171. * @return
  172. */
  173. @RequestMapping (value = "up/{id}",method = RequestMethod.GET)
  174. public String updates(Model model,@PathVariable Integer id){
  175. SysLsUserEntity sysLsUserEntit = lsUserService.getUserId(id);
  176. model.addAttribute("ls",sysLsUserEntit);
  177. List<Map<String, String>> xlList = baseEducationService.getEducationList();
  178. model.addAttribute("xlList", xlList);
  179. return "lsuserinfo/adds";
  180. }
  181. @RequestMapping(value = "/getId/{id}",method = RequestMethod.GET)
  182. @ResponseBody
  183. public BaseResult getId(Model model,@PathVariable Integer id){
  184. if (id != null){
  185. SysLsUserEntity sysLsUserEntity = lsUserService.getUserId(id);
  186. Map<String, Object> result = new HashMap<>();
  187. result.put(RESULT_ROWS, sysLsUserEntity);
  188. model.addAttribute("sy",sysLsUserEntity);
  189. return BaseResult.success(result);
  190. }
  191. return BaseResult.failure("该人员不存在");
  192. }
  193. @PostMapping("/file")
  194. @ResponseBody
  195. public BaseResult handleFileUpload(@RequestParam("file") MultipartFile file) {
  196. if (file.isEmpty()){
  197. return BaseResult.failure("上传文件异常,上传失败!");
  198. }
  199. List<SysLsUserEntity> lists = new ArrayList<>();
  200. Integer h = 0;
  201. try{
  202. // 验证文件类型
  203. DateUtil dateUtil = new DateUtil();
  204. String fileExtension = FilenameUtils.getExtension(file.getOriginalFilename());
  205. if (!allowedExtensions.contains(fileExtension.toLowerCase())) {
  206. return BaseResult.failure("只允许上传 Excel文件");
  207. }
  208. // 开始解析这个文件
  209. List<List<String>> spreadsheetData = parseSpreadsheet(file.getInputStream(), fileExtension);
  210. System.out.println(spreadsheetData);
  211. spreadsheetData.remove(0);
  212. List<SysLsUserEntity> list = new ArrayList<>();
  213. for (List<String> row : spreadsheetData) {
  214. SysLsUserEntity employee = new SysLsUserEntity();
  215. // 设置员工信息
  216. employee.setXm(row.get(0));
  217. employee.setXb(row.get(1));
  218. employee.setPhone(row.get(2));
  219. employee.setSfzh(row.get(3));
  220. employee.setXl(row.get(4));
  221. employee.setByxy(row.get(5));
  222. employee.setZy(row.get(6));
  223. employee.setQpsj(dateUtil.convertDate(row.get(7)));
  224. employee.setDqsj(dateUtil.convertDate(row.get(8)));
  225. employee.setGznr(row.get(9));
  226. // 将对象添加到列表中
  227. list.add(employee);
  228. }
  229. System.out.println(list);
  230. for(int i = 0; i<list.size();i++){
  231. SysLsUserEntity sysLsUserEntity = new SysLsUserEntity();
  232. // 校验合同起始时间和到期时间是否在三个月 内
  233. if (dateUtil.areDatesWithinThreeMonths(list.get(i).getDqsj(), list.get(i).getQpsj()) !=true) {
  234. sysLsUserEntity.setXm(list.get(i).getXm());
  235. // xmfzr 这个字段存储未能存储原因
  236. sysLsUserEntity.setXmfzr("聘用日期不能大于三个月");
  237. sysLsUserEntity.setSfzh(list.get(i).getSfzh());
  238. sysLsUserEntity.setDqsj(list.get(i).getDqsj());
  239. sysLsUserEntity.setQpsj(list.get(i).getQpsj());
  240. if (list.get(i).getXm().isEmpty() && list.get(i).getSfzh().isEmpty()){
  241. continue;
  242. }
  243. lists.add(sysLsUserEntity);
  244. continue;
  245. }
  246. // 校验当前传入的 身份证号 是否在数据库中存在
  247. if (lsUserService.jy(list.get(i).getSfzh()) != true){
  248. sysLsUserEntity.setXm(list.get(i).getXm());
  249. sysLsUserEntity.setSfzh(list.get(i).getSfzh());
  250. sysLsUserEntity.setDqsj(list.get(i).getDqsj());
  251. sysLsUserEntity.setQpsj(list.get(i).getQpsj());
  252. // xmfzr 这个字段存储未能存储原因
  253. sysLsUserEntity.setXmfzr("该人员身份证已存在");
  254. if (list.get(i).getXm().isEmpty() && list.get(i).getSfzh().isEmpty()){
  255. continue;
  256. }
  257. lists.add(sysLsUserEntity);
  258. continue;
  259. }
  260. Integer mum = lsUserService.saveOperLog(list.get(i));
  261. if (mum > 0){
  262. h++;
  263. continue;
  264. }
  265. }
  266. }catch (Exception a){
  267. a.printStackTrace();
  268. }
  269. // 获取文件名后缀
  270. if (h == 0){
  271. return BaseResult.failure(43,"成员录入失败,人员已存在!");
  272. }
  273. return BaseResult.success("成员录入成功",lists);
  274. }
  275. private List<List<String>> parseSpreadsheet(InputStream inputStream, String fileExtension) throws IOException {
  276. Workbook workbook = null;
  277. if (fileExtension.equalsIgnoreCase("xlsx")) {
  278. workbook = new XSSFWorkbook(inputStream);
  279. } else if (fileExtension.equalsIgnoreCase("xls")) {
  280. throw new IOException("上传的Excel文件格式不支持,请上传xlsx格式文件。");
  281. } else if (fileExtension.equalsIgnoreCase("csv")) {
  282. throw new IOException("上传的文件是CSV格式,当前不支持解析,请上传Excel文件。");
  283. }
  284. List<List<String>> spreadsheetData = new ArrayList<>();
  285. if (workbook != null) {
  286. Sheet sheet = workbook.getSheetAt(0);
  287. Iterator<Row> iterator = sheet.iterator();
  288. while (iterator.hasNext()) {
  289. Row currentRow = iterator.next();
  290. Iterator<Cell> cellIterator = currentRow.iterator();
  291. List<String> rowData = new ArrayList<>();
  292. while (cellIterator.hasNext()) {
  293. Cell currentCell = cellIterator.next();
  294. rowData.add(currentCell.toString());
  295. }
  296. spreadsheetData.add(rowData);
  297. }
  298. workbook.close();
  299. }
  300. return spreadsheetData;
  301. }
  302. }