LsUserController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. Map<String, String> paraMap = new HashMap<>();
  78. PageHelper.startPage(pageNum, pageSize);
  79. if (zt == null){
  80. paraMap.put("xm", xm);
  81. paraMap.put("xl",xl);
  82. List<SysLsUserEntity> list = lsUserService.UserAll(paraMap);
  83. PageInfo<SysLsUserEntity> pageinfo = new PageInfo<>(list);
  84. // 取出查询结果
  85. List<SysLsUserEntity> rows = pageinfo.getList();
  86. int total = (int) pageinfo.getTotal();
  87. Map<String, Object> result = new HashMap<>();
  88. result.put(RESULT_ROWS, rows);
  89. result.put(RESULT_TOTAL, total);
  90. result.put("list",list);
  91. return BaseResult.success(result);
  92. }
  93. paraMap.put("xm", xm);
  94. paraMap.put("xl",xl);
  95. paraMap.put("zt",String.valueOf(zt));
  96. List<SysLsUserEntity> list = lsUserService.UserAll(paraMap);
  97. PageInfo<SysLsUserEntity> pageinfo = new PageInfo<>(list);
  98. // 取出查询结果
  99. List<SysLsUserEntity> rows = pageinfo.getList();
  100. int total = (int) pageinfo.getTotal();
  101. Map<String, Object> result = new HashMap<>();
  102. result.put(RESULT_ROWS, rows);
  103. result.put(RESULT_TOTAL, total);
  104. result.put("list",list);
  105. return BaseResult.success(result);
  106. }
  107. @RequestMapping(value = "/forAdd", method = RequestMethod.GET)
  108. public String forAdd(Model model) {
  109. List<Map<String, String>> xlList = baseEducationService.getEducationList();
  110. model.addAttribute("xlList", xlList);
  111. return "lsuserinfo/add";
  112. }
  113. @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
  114. public String forEdit(@PathVariable Integer id){
  115. return "lsuserinfo/add";
  116. }
  117. /**
  118. * 保存数据
  119. * @param
  120. * @return
  121. */
  122. @PostMapping("/save")
  123. @ResponseBody
  124. public BaseResult saveOperLog(@RequestBody SysLsUserEntity sysLsUserEntity) {
  125. Map<String, String>map = new HashMap<>();
  126. map.put("sfzh",sysLsUserEntity.getSfzh());
  127. List<SysLsUserEntity> list = lsUserService.getUserxm(map);
  128. if (list.size() == 0) {
  129. SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  130. Date date = new Date(System.currentTimeMillis());
  131. sysLsUserEntity.setCjsj(formatter.format(date));
  132. int num = lsUserService.saveOperLog(sysLsUserEntity);
  133. return BaseResult.success("新增临时人员成功!");
  134. }
  135. return BaseResult.failure("新建临时人员失败,该人员已存在!");
  136. }
  137. @RequestMapping(value="/update/{id}", method = RequestMethod.POST)
  138. @ResponseBody
  139. public BaseResult batchDelete(@RequestBody SysLsUserEntity sysLsUserEntity,@PathVariable Integer id){
  140. sysLsUserEntity.setZt(lsUserService.getUserId(id).getZt());
  141. sysLsUserEntity.setId(id);
  142. sysLsUserEntity.setX(1);
  143. boolean a = lsUserService.upDate(sysLsUserEntity);
  144. if (a != true){
  145. return BaseResult.failure("修改失败");
  146. }
  147. return BaseResult.success("修改成功");
  148. }
  149. /**
  150. * 删除数据
  151. * @param id
  152. * @return
  153. */
  154. @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
  155. @ResponseBody
  156. public BaseResult delete (@PathVariable Integer id){
  157. SysLsUserEntity sysLsUserEntity = lsUserService.getUserId(id);
  158. sysLsUserEntity.setX(0);
  159. boolean flag = lsUserService.upDate(sysLsUserEntity);
  160. if (flag) {
  161. return BaseResult.success("删除成功");
  162. } else {
  163. return BaseResult.failure("删除失败");
  164. }
  165. }
  166. /**
  167. * 修改数据
  168. * @param
  169. * @return
  170. */
  171. @RequestMapping (value = "up/{id}",method = RequestMethod.GET)
  172. public String updates(Model model,@PathVariable Integer id){
  173. SysLsUserEntity sysLsUserEntit = lsUserService.getUserId(id);
  174. model.addAttribute("ls",sysLsUserEntit);
  175. List<Map<String, String>> xlList = baseEducationService.getEducationList();
  176. model.addAttribute("xlList", xlList);
  177. return "lsuserinfo/adds";
  178. }
  179. @RequestMapping(value = "/getId/{id}",method = RequestMethod.GET)
  180. @ResponseBody
  181. public BaseResult getId(Model model,@PathVariable Integer id){
  182. if (id != null){
  183. SysLsUserEntity sysLsUserEntity = lsUserService.getUserId(id);
  184. Map<String, Object> result = new HashMap<>();
  185. result.put(RESULT_ROWS, sysLsUserEntity);
  186. model.addAttribute("sy",sysLsUserEntity);
  187. return BaseResult.success(result);
  188. }
  189. return BaseResult.failure("该人员不存在");
  190. }
  191. @PostMapping("/file")
  192. @ResponseBody
  193. public BaseResult handleFileUpload(@RequestParam("file") MultipartFile file) {
  194. if (file.isEmpty()){
  195. return BaseResult.failure("上传文件异常,上传失败!");
  196. }
  197. List<SysLsUserEntity> lists = new ArrayList<>();
  198. Integer h = 0;
  199. try{
  200. // 验证文件类型
  201. DateUtil dateUtil = new DateUtil();
  202. String fileExtension = FilenameUtils.getExtension(file.getOriginalFilename());
  203. if (!allowedExtensions.contains(fileExtension.toLowerCase())) {
  204. return BaseResult.failure("只允许上传 Excel文件");
  205. }
  206. // 开始解析这个文件
  207. List<List<String>> spreadsheetData = parseSpreadsheet(file.getInputStream(), fileExtension);
  208. System.out.println(spreadsheetData);
  209. spreadsheetData.remove(0);
  210. List<SysLsUserEntity> list = new ArrayList<>();
  211. for (List<String> row : spreadsheetData) {
  212. SysLsUserEntity employee = new SysLsUserEntity();
  213. // 设置员工信息
  214. employee.setXm(row.get(0));
  215. employee.setXb(row.get(1));
  216. employee.setPhone(row.get(2));
  217. employee.setSfzh(row.get(3));
  218. employee.setXl(row.get(4));
  219. employee.setByxy(row.get(5));
  220. employee.setZy(row.get(6));
  221. employee.setQpsj(dateUtil.convertDate(row.get(7)));
  222. employee.setDqsj(dateUtil.convertDate(row.get(8)));
  223. employee.setGznr(row.get(9));
  224. // 将对象添加到列表中
  225. list.add(employee);
  226. }
  227. System.out.println(list);
  228. for(int i = 0; i<list.size();i++){
  229. SysLsUserEntity sysLsUserEntity = new SysLsUserEntity();
  230. // 校验合同起始时间和到期时间是否在三个月 内
  231. if (dateUtil.areDatesWithinThreeMonths(list.get(i).getDqsj(), list.get(i).getQpsj()) !=true) {
  232. sysLsUserEntity.setXm(list.get(i).getXm());
  233. // xmfzr 这个字段存储未能存储原因
  234. sysLsUserEntity.setXmfzr("聘用日期不能大于三个月");
  235. sysLsUserEntity.setSfzh(list.get(i).getSfzh());
  236. sysLsUserEntity.setDqsj(list.get(i).getDqsj());
  237. sysLsUserEntity.setQpsj(list.get(i).getQpsj());
  238. if (list.get(i).getXm().isEmpty() && list.get(i).getSfzh().isEmpty()){
  239. continue;
  240. }
  241. lists.add(sysLsUserEntity);
  242. continue;
  243. }
  244. // 校验当前传入的 身份证号 是否在数据库中存在
  245. if (lsUserService.jy(list.get(i).getSfzh()) != true){
  246. sysLsUserEntity.setXm(list.get(i).getXm());
  247. sysLsUserEntity.setSfzh(list.get(i).getSfzh());
  248. sysLsUserEntity.setDqsj(list.get(i).getDqsj());
  249. sysLsUserEntity.setQpsj(list.get(i).getQpsj());
  250. // xmfzr 这个字段存储未能存储原因
  251. sysLsUserEntity.setXmfzr("该人员身份证已存在");
  252. if (list.get(i).getXm().isEmpty() && list.get(i).getSfzh().isEmpty()){
  253. continue;
  254. }
  255. lists.add(sysLsUserEntity);
  256. continue;
  257. }
  258. Integer mum = lsUserService.saveOperLog(list.get(i));
  259. if (mum > 0){
  260. h++;
  261. continue;
  262. }
  263. }
  264. }catch (Exception a){
  265. a.printStackTrace();
  266. }
  267. // 获取文件名后缀
  268. if (h == 0){
  269. return BaseResult.failure(43,"成员录入失败,人员已存在!");
  270. }
  271. return BaseResult.success("成员录入成功",lists);
  272. }
  273. private List<List<String>> parseSpreadsheet(InputStream inputStream, String fileExtension) throws IOException {
  274. Workbook workbook = null;
  275. if (fileExtension.equalsIgnoreCase("xlsx")) {
  276. workbook = new XSSFWorkbook(inputStream);
  277. } else if (fileExtension.equalsIgnoreCase("xls")) {
  278. throw new IOException("上传的Excel文件格式不支持,请上传xlsx格式文件。");
  279. } else if (fileExtension.equalsIgnoreCase("csv")) {
  280. throw new IOException("上传的文件是CSV格式,当前不支持解析,请上传Excel文件。");
  281. }
  282. List<List<String>> spreadsheetData = new ArrayList<>();
  283. if (workbook != null) {
  284. Sheet sheet = workbook.getSheetAt(0);
  285. Iterator<Row> iterator = sheet.iterator();
  286. while (iterator.hasNext()) {
  287. Row currentRow = iterator.next();
  288. Iterator<Cell> cellIterator = currentRow.iterator();
  289. List<String> rowData = new ArrayList<>();
  290. while (cellIterator.hasNext()) {
  291. Cell currentCell = cellIterator.next();
  292. rowData.add(currentCell.toString());
  293. }
  294. spreadsheetData.add(rowData);
  295. }
  296. workbook.close();
  297. }
  298. return spreadsheetData;
  299. }
  300. }