bdeba37a0fa8289e2528d9660a8483674d393e89.svn-base 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package cn.com.goldenwater.dcproj.controller.ducha;
  2. import cn.com.goldenwater.core.web.BaseController;
  3. import cn.com.goldenwater.core.web.BaseResponse;
  4. import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers;
  5. import cn.com.goldenwater.dcproj.service.BisInspAllRlationPersService;
  6. import cn.com.goldenwater.dcproj.target.Authority;
  7. import cn.com.goldenwater.dcproj.util.ReadExcelUtil;
  8. import cn.com.goldenwater.dcproj.utils.impexcel.ExpAndImpUtil;
  9. import cn.com.goldenwater.id.util.UuidUtil;
  10. import io.swagger.annotations.Api;
  11. import io.swagger.annotations.ApiOperation;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.web.bind.annotation.*;
  19. import org.springframework.web.multipart.MultipartFile;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.file.Paths;
  24. import java.util.*;
  25. /**
  26. * @author zhaohg
  27. * @date 2019-2-23
  28. */
  29. @Api(value = "新用户管理", tags = "006新用户管理")
  30. @RestController
  31. @RequestMapping("/bis/insp")
  32. public class BisInspAllRlationPersBatchController extends BaseController {
  33. private Logger logger = LoggerFactory.getLogger(getClass());
  34. @Value("${export.templatePath}")
  35. private String templatePath;
  36. @Autowired
  37. private BisInspAllRlationPersService bisInspAllRlationPersService;
  38. @Authority(roles = "1")
  39. @ApiOperation("获取导入模板")
  40. @GetMapping(value = "/getExl")
  41. public BaseResponse getExl(HttpServletResponse response) {
  42. String path = templatePath + File.separator + "person_list.xls";
  43. try {
  44. ExpAndImpUtil.downloadFile(response, path, "用户导入表");
  45. } catch (Exception e) {
  46. logger.error(e.getMessage(), e);
  47. }
  48. return buildSuccessResponse();
  49. }
  50. @ApiOperation("上传 xls 导入数据")
  51. @RequestMapping(value = "/impExl", method = RequestMethod.POST)
  52. public BaseResponse<Map<String, Object>> impExl(@RequestParam("file") MultipartFile multfile,
  53. @RequestParam(value = "orgId", required = false) String orgId) throws IOException, InvalidFormatException {
  54. // 获取文件名
  55. String fileName = multfile.getOriginalFilename();
  56. // 获取文件后缀
  57. String prefix = fileName.substring(fileName.lastIndexOf("."));
  58. // 用uuid作为文件名,防止生成的临时文件重复
  59. String uuid = UuidUtil.uuid();
  60. File file = File.createTempFile(uuid, prefix);
  61. // MultipartFile to File
  62. multfile.transferTo(Paths.get(file.getPath()));
  63. Map<String, Object> map = new HashMap<>(2);
  64. int shu = 0;
  65. int success = 0;
  66. Map<String, String> failReason = new HashMap<>();
  67. if (StringUtils.isBlank(orgId)) {
  68. orgId = getCurrentOrgId();
  69. }
  70. String[][] data = ReadExcelUtil.getData(file, 1);
  71. if (data.length > 0) {
  72. for (String[] datum : data) {
  73. if (StringUtils.isBlank(datum[3])) {
  74. continue;
  75. }
  76. BisInspAllRlationPers pers = new BisInspAllRlationPers();
  77. try {
  78. pers.setOrgId(orgId);
  79. pers.setPersName(datum[1]);
  80. pers.setSex(getSex(datum[2]));
  81. pers.setMobilenumb(datum[3]);
  82. pers.setPersType(getPersType(datum[4]));
  83. pers.setDpnm(datum[5]);
  84. pers.setAdmDuty(datum[6]);
  85. pers.setDppost(datum[7]);
  86. pers.setPwd(getPwd(datum[8], datum[3]));
  87. bisInspAllRlationPersService.createUser(pers);
  88. success++;
  89. } catch (Exception e) {
  90. logger.error(e.getMessage(), e);
  91. shu++;
  92. failReason.put(pers.getPersName() + "[" + pers.getMobilenumb() + "]", e.getMessage());
  93. }
  94. }
  95. }
  96. map.put("false", shu);
  97. map.put("success", success);
  98. map.put("failReason", failReason);
  99. return buildSuccessResponse(map);
  100. }
  101. /**
  102. * 获取性别
  103. *
  104. * @param value 数据
  105. * @return 1:男 2:女
  106. */
  107. private String getSex(String value) {
  108. Set<String> set = new HashSet<>(Arrays.asList("1", "2"));
  109. if (set.contains(value) || StringUtils.isBlank(value)) {
  110. return value;
  111. }
  112. if (value.contains("男")) {
  113. return "1";
  114. } else if (value.contains("女")) {
  115. return "2";
  116. } else {
  117. return null;
  118. }
  119. }
  120. /**
  121. * 获取人员类型
  122. *
  123. * @param value 数据
  124. * @return 1.管理员 2.机构领导 3.督察人员 4.稽查人员
  125. */
  126. private String getPersType(String value) {
  127. Set<String> set = new HashSet<>(Arrays.asList("1", "2", "3", "4"));
  128. if (set.contains(value)) {
  129. return value;
  130. }
  131. if (StringUtils.isBlank(value)) {
  132. return "3";
  133. }
  134. if (value.contains("管理")) {
  135. return "1";
  136. } else if (value.contains("机构")) {
  137. return "2";
  138. } else if (value.contains("督查")) {
  139. return "3";
  140. } else if (value.contains("稽察")) {
  141. return "4";
  142. } else {
  143. return "3";
  144. }
  145. }
  146. /**
  147. * 获取密码: 默认密码是手机号码
  148. *
  149. * @param value 数据
  150. * @return 1.管理员 2.机构领导 3.督察人员 4.稽查人员
  151. */
  152. private String getPwd(String value, String phone) {
  153. if (StringUtils.isBlank(value)) {
  154. return phone;
  155. }
  156. return value.trim();
  157. }
  158. }