SmartUserController.java 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. package com.template.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.qcloud.cos.utils.IOUtils;
  6. import com.seewo.open.sdk.DefaultSeewoClient;
  7. import com.seewo.open.sdk.SeewoClient;
  8. import com.seewo.open.sdk.auth.Account;
  9. import com.template.api.SmartUserControllerAPI;
  10. import com.template.common.utils.*;
  11. import com.template.config.ControlConfig;
  12. import com.template.config.SeewoConfig;
  13. import com.template.model.enumModel.eFileType;
  14. import com.template.model.enumModel.eIdentityStatu;
  15. import com.template.model.enumModel.eLogOff;
  16. import com.template.model.enumModel.eSexStatu;
  17. import com.template.model.pojo.SmartClass;
  18. import com.template.model.pojo.SmartDepartment;
  19. import com.template.model.pojo.SmartGrade;
  20. import com.template.model.pojo.SmartUser;
  21. import com.template.model.request.changeDepartmentRequest;
  22. import com.template.model.request.insertSmartUserRequest;
  23. import com.template.model.request.updateSmartUserRequest;
  24. import com.template.model.request.useridsRequest;
  25. import com.template.model.result.CommonResult;
  26. import com.template.model.result.PageUtils;
  27. import com.template.model.seewo.*;
  28. import com.template.model.vo.*;
  29. import com.template.services.*;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.apache.poi.hssf.usermodel.HSSFRow;
  32. import org.apache.poi.hssf.usermodel.HSSFSheet;
  33. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  34. import org.apache.poi.ss.usermodel.DataFormatter;
  35. import org.apache.poi.ss.usermodel.Row;
  36. import org.apache.poi.ss.usermodel.Sheet;
  37. import org.apache.poi.ss.usermodel.Workbook;
  38. import org.apache.poi.xssf.usermodel.XSSFRow;
  39. import org.apache.poi.xssf.usermodel.XSSFSheet;
  40. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  41. import org.springframework.beans.factory.annotation.Autowired;
  42. import org.springframework.validation.BindingResult;
  43. import org.springframework.web.bind.annotation.RestController;
  44. import org.springframework.web.multipart.MultipartFile;
  45. import javax.annotation.Resource;
  46. import javax.servlet.http.HttpServletResponse;
  47. import java.io.*;
  48. import java.net.URLDecoder;
  49. import java.net.URLEncoder;
  50. import java.nio.charset.Charset;
  51. import java.util.*;
  52. import java.util.stream.Collectors;
  53. import java.util.zip.ZipInputStream;
  54. import static com.template.common.utils.AesTestOne.decrypt;
  55. /**
  56. * <p>
  57. * 前端控制器
  58. * </p>
  59. *
  60. * @author ceshi
  61. * @since 2023-12-04
  62. */
  63. @RestController
  64. public class SmartUserController implements SmartUserControllerAPI {
  65. @Resource
  66. private SeewoConfig seewoConfig;
  67. @Resource
  68. private ControlConfig controlConfig;
  69. @Autowired
  70. private SmartUserService smartUserService;
  71. @Autowired
  72. private SmartGradeService smartGradeService;
  73. @Autowired
  74. private SmartClassService smartClassService;
  75. @Autowired
  76. private SmartUploadService smartUploadService;
  77. @Autowired
  78. private SmartDepartmentService smartDepartmentService;
  79. @Override
  80. public CommonResult logoffAccount(useridsRequest ur, BindingResult bindingResult) {
  81. if (bindingResult.hasErrors()) {
  82. String st = paramUtils.getParamError(bindingResult);
  83. return CommonResult.fail(st);
  84. }
  85. int user = smartUserService.getSmartUserCountByIds(ur.getUserIds());
  86. if (user != ur.getUserIds().size()) {
  87. return CommonResult.fail("用户信息已失效");
  88. }
  89. // for (:) {
  90. //
  91. // }
  92. // user.setIsCancel(eLogOff.Logout.getValue());
  93. //
  94. // int updateResult = smartUserService.updateSmartUser(user);
  95. return 0 > 0 ? CommonResult.ok("注销成功") : CommonResult.fail("注销失败");
  96. }
  97. @Override
  98. public CommonResult changeDepartment(changeDepartmentRequest cdr, BindingResult bindingResult) {
  99. if (bindingResult.hasErrors()) {
  100. String st = paramUtils.getParamError(bindingResult);
  101. return CommonResult.fail(st);
  102. }
  103. List<SmartUser> users = smartUserService.getSmartUserByIds(cdr.getUserIds());
  104. if (users == null) {
  105. return CommonResult.fail("用户信息无效,移动失败!");
  106. }
  107. if (users.size() != cdr.getUserIds().size()) {
  108. return CommonResult.fail("存在无效用户数据,移动失败!");
  109. }
  110. for (SmartUser user : users) {
  111. user.setDepartmentId(cdr.getDepartmentId());
  112. }
  113. boolean result = smartUserService.updateUserBatchById(users);
  114. return result ? CommonResult.ok("移动成功") : CommonResult.fail("移动失败");
  115. }
  116. @Override
  117. public CommonResult importExcelUsers(MultipartFile excelFile, String headImage) throws IOException {
  118. List<SmartUser> result = new ArrayList<>();
  119. //先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
  120. if (excelFile.isEmpty() || excelFile.getSize() == 0) {
  121. return CommonResult.fail("压缩包中的excel文件不能为空");
  122. }
  123. String ContentType = excelFile.getContentType();
  124. InputStream inputStream = excelFile.getInputStream();
  125. //xls格式文件
  126. if (ContentType.equals(eFileType.Xls.getValue())) {
  127. CommonResult<List<SmartUser>> resultData = readXls(inputStream);
  128. if (!resultData.isSuccess()) {
  129. return resultData;
  130. }
  131. result = resultData.getData();
  132. } else if (ContentType.equals(eFileType.Xlsx.getValue())) {
  133. CommonResult<List<SmartUser>> resultData = readXlsx(inputStream);
  134. if (!resultData.isSuccess()) {
  135. return resultData;
  136. }
  137. result = resultData.getData();
  138. } else {
  139. return CommonResult.fail("用户导入只支持Xls、Xlsx");
  140. }
  141. List<String> uploadImages = Arrays.asList(headImage.split(","));
  142. for (SmartUser user : result) {
  143. Optional<String> image = uploadImages == null ? null : uploadImages.stream().filter(e -> e.equals("https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/headImage/" + user.getHeadImage())).findFirst();
  144. if (image != null && image.isPresent()) {
  145. user.setHeadImage(image.get());
  146. }
  147. }
  148. //批量存储用户信息
  149. boolean resultBool = smartUserService.saveBatch(result);
  150. return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
  151. }
  152. /**
  153. * 批量导入用户信息
  154. * 以身份证号作为判断依据 如果存在重复数据就提示存在重复数据
  155. * 开发流程:建议先去拿到excel中的所有数据的身份证号去数据库中找一遍是否存在重复数据
  156. * 如果不存在重复数据就把附件文件夹中的附件文件上传到cos服务器上
  157. * 然后手动拼接一个人脸照片文件地址存入对应的数据中
  158. *
  159. * @param zipFile zip压缩包
  160. * @return
  161. */
  162. @Override
  163. public CommonResult importZipUsers(MultipartFile zipFile) throws IOException {
  164. List<SmartUser> result = new ArrayList<>();
  165. MultipartFile excelFile = null;
  166. List<MultipartFile> multipartFileHashMap = new ArrayList<>();
  167. String excelStr = null;
  168. if (zipFile != null) {
  169. //解压压缩文件并上传文件
  170. ByteArrayOutputStream byteOut = null;
  171. ZipInputStream zipIn = null;
  172. try {
  173. java.util.zip.ZipEntry zipEntry = null;
  174. zipIn = new ZipInputStream(zipFile.getInputStream(), Charset.forName("GBK"));
  175. while ((zipEntry = zipIn.getNextEntry()) != null) {
  176. String fileName = zipEntry.getName();
  177. if (fileName.contains("人脸照片") && (fileName.endsWith(".jpg") || fileName.endsWith(".png") || fileName.endsWith(".jpeg"))) {
  178. byteOut = new ByteArrayOutputStream();
  179. IOUtils.copy(zipIn, byteOut);
  180. InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
  181. MultipartFile multipartFile = FileUtils.getMultipartFile(inputStream, fileName);
  182. multipartFileHashMap.add(multipartFile);
  183. } else if (fileName.contains("人员信息采集表") && (fileName.endsWith(".xlsx") || fileName.endsWith(".xls"))) {
  184. excelStr = fileName.endsWith(".xlsx") ? "xlsx" : "xls";
  185. byteOut = new ByteArrayOutputStream();
  186. IOUtils.copy(zipIn, byteOut);
  187. InputStream inputStream = new ByteArrayInputStream(byteOut.toByteArray());
  188. excelFile = FileUtils.getMultipartFile(inputStream, fileName);
  189. }
  190. }
  191. } catch (Exception e) {
  192. e.printStackTrace();
  193. } finally {
  194. if (byteOut != null) {
  195. byteOut.close();
  196. }
  197. if (zipIn != null) {
  198. zipIn.close();
  199. }
  200. }
  201. }
  202. //先解析excel如果excel不满足格式就提示错误信息 避免提前占用带宽上传头像
  203. if (excelFile.isEmpty() || excelFile.getSize() == 0) {
  204. return CommonResult.fail("压缩包中的excel文件不能为空");
  205. }
  206. String ContentType = excelFile.getContentType();
  207. InputStream inputStream = excelFile.getInputStream();
  208. //xls格式文件
  209. if (excelStr.equals("xls")) {
  210. CommonResult<List<SmartUser>> resultData = readXls(inputStream);
  211. if (!resultData.isSuccess()) {
  212. return resultData;
  213. }
  214. result = resultData.getData();
  215. } else if (excelStr.equals("xlsx")) {
  216. CommonResult<List<SmartUser>> resultData = readXlsx(inputStream);
  217. if (!resultData.isSuccess()) {
  218. return resultData;
  219. }
  220. result = resultData.getData();
  221. } else {
  222. return CommonResult.fail("用户导入只支持Xls、Xlsx");
  223. }
  224. //cos上传 返回图片地址
  225. String uploadResult = smartUploadService.upload(multipartFileHashMap.toArray(new MultipartFile[0]));
  226. List<String> uploadImages = Arrays.asList(uploadResult.split(","));
  227. for (SmartUser user : result) {
  228. Optional<String> headImage = uploadImages == null ? null : uploadImages.stream().filter(e -> e.equals("https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/headImage/" + user.getHeadImage())).findFirst();
  229. if (headImage != null && headImage.isPresent()) {
  230. user.setHeadImage(headImage.get());
  231. }
  232. }
  233. //批量存储用户信息
  234. boolean resultBool = smartUserService.saveBatch(result);
  235. return resultBool ? CommonResult.ok("导入成功") : CommonResult.fail("导入失败");
  236. }
  237. /**
  238. * Xlsx文件读取方法
  239. *
  240. * @param inputStream 文件流
  241. * @return
  242. * @throws IOException
  243. */
  244. private CommonResult<List<SmartUser>> readXlsx(InputStream inputStream) throws IOException {
  245. List<String> phones = new ArrayList<>();
  246. List<String> idCards = new ArrayList<>();
  247. List<String> cardNos = new ArrayList<>();
  248. List<SmartUser> result = new ArrayList<>();
  249. XSSFWorkbook sheets = new XSSFWorkbook(inputStream);
  250. List<SmartDepartment> departments = smartDepartmentService.list(null);
  251. List<SmartGrade> grades = smartGradeService.list(null); //年级
  252. List<SmartClass> classs = smartClassService.list(null);//班级
  253. //读取第一张sheet
  254. XSSFSheet sheetAt = sheets.getSheetAt(0);
  255. DataFormatter dataFormatter = new DataFormatter();
  256. try {
  257. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  258. XSSFRow row = sheetAt.getRow(rowNum);
  259. if (row != null) {
  260. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  261. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  262. //然后poi会根据字符串读取它
  263. //标题 校验
  264. if (rowNum == 0) {
  265. String cardNo = dataFormatter.formatCellValue(row.getCell(0));//学号
  266. if (!cardNo.equals("学号")) {
  267. return CommonResult.fail("导入数据第一列为学号");
  268. }
  269. String name = dataFormatter.formatCellValue(row.getCell(1));//姓名
  270. if (!name.equals("姓名")) {
  271. return CommonResult.fail("导入数据第二列为姓名");
  272. }
  273. String identity = dataFormatter.formatCellValue(row.getCell(2));//身份
  274. if (!identity.equals("身份")) {
  275. return CommonResult.fail("导入数据第三列为身份");
  276. }
  277. String idCard = dataFormatter.formatCellValue(row.getCell(3));//身份证
  278. if (!idCard.equals("身份证")) {
  279. return CommonResult.fail("导入数据第四列为身份证");
  280. }
  281. String sex = dataFormatter.formatCellValue(row.getCell(4));//性别
  282. if (!sex.equals("性别")) {
  283. return CommonResult.fail("导入数据第五列为性别");
  284. }
  285. String department = dataFormatter.formatCellValue(row.getCell(5));//部门
  286. if (!department.equals("部门")) {
  287. return CommonResult.fail("导入数据第六列为部门");
  288. }
  289. String headImage = dataFormatter.formatCellValue(row.getCell(6));//人脸照片
  290. if (!headImage.equals("人脸照片")) {
  291. return CommonResult.fail("导入数据第七列为人脸照片");
  292. }
  293. String dormitoryNumber = dataFormatter.formatCellValue(row.getCell(7));//宿舍号
  294. if (!dormitoryNumber.equals("宿舍号")) {
  295. return CommonResult.fail("导入数据第八列为宿舍号");
  296. }
  297. String grade = dataFormatter.formatCellValue(row.getCell(8));//年级
  298. if (!grade.equals("年级")) {
  299. return CommonResult.fail("导入数据第九列为年级");
  300. }
  301. String schoolClass = dataFormatter.formatCellValue(row.getCell(9));//班级
  302. if (!schoolClass.equals("班级")) {
  303. return CommonResult.fail("导入数据第十列为班级");
  304. }
  305. String phone = dataFormatter.formatCellValue(row.getCell(10));//手机号
  306. if (!phone.equals("手机号")) {
  307. return CommonResult.fail("导入数据第十一列为手机号");
  308. }
  309. String affiliate = dataFormatter.formatCellValue(row.getCell(11));//关联人
  310. if (!affiliate.equals("关联人")) {
  311. return CommonResult.fail("导入数据第十二列为关联人");
  312. }
  313. String title = dataFormatter.formatCellValue(row.getCell(12));//职称
  314. if (!title.equals("职称")) {
  315. return CommonResult.fail("导入数据第十三列为职称");
  316. }
  317. String address = dataFormatter.formatCellValue(row.getCell(13));//家庭住址
  318. if (!address.equals("家庭住址")) {
  319. return CommonResult.fail("导入数据第十四列为家庭住址");
  320. }
  321. String nation = dataFormatter.formatCellValue(row.getCell(14));//民族
  322. if (!nation.equals("民族")) {
  323. return CommonResult.fail("导入数据第十五列为民族");
  324. }
  325. String ofStudent = dataFormatter.formatCellValue(row.getCell(15));//生源地
  326. if (!ofStudent.equals("生源地")) {
  327. return CommonResult.fail("导入数据第十六列为生源地");
  328. }
  329. String graduate = dataFormatter.formatCellValue(row.getCell(16));//毕业学校
  330. if (!graduate.equals("毕业学校")) {
  331. return CommonResult.fail("导入数据第十七列为毕业学校");
  332. }
  333. String duties = dataFormatter.formatCellValue(row.getCell(17));//职务
  334. if (!duties.equals("职务")) {
  335. return CommonResult.fail("导入数据第十八列为职务");
  336. }
  337. } else {
  338. SmartUser user = new SmartUser();
  339. String name = dataFormatter.formatCellValue(row.getCell(1));
  340. if (ObjectUtils.isEmpty(name)) {
  341. return CommonResult.fail("第" + (rowNum + 2) + "条数据的名称不能为空");
  342. }
  343. //身份是否为空判断
  344. String identity = dataFormatter.formatCellValue(row.getCell(2));
  345. if (ObjectUtils.isEmpty(identity)) {
  346. return CommonResult.fail(name + "的身份不能为空");
  347. }
  348. String dormitoryNumber = dataFormatter.formatCellValue(row.getCell(7));//宿舍号
  349. String grade = dataFormatter.formatCellValue(row.getCell(8));//年级
  350. String schoolClass = dataFormatter.formatCellValue(row.getCell(9));//班级
  351. //身份为学生的情况下 宿舍号、年级、班级不能为空
  352. if (eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Student.getValue()) {
  353. if (ObjectUtils.isEmpty(dormitoryNumber)) {
  354. return CommonResult.fail(name + "的宿舍号不能为空");
  355. }
  356. if (ObjectUtils.isEmpty(grade)) {
  357. return CommonResult.fail(name + "的年级不能为空");
  358. }
  359. if (ObjectUtils.isEmpty(schoolClass)) {
  360. return CommonResult.fail(name + "的班级不能为空");
  361. }
  362. }
  363. //手机号重复判断
  364. String phone = dataFormatter.formatCellValue(row.getCell(10));
  365. if (eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Parent.getValue() || eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Teacher.getValue()) {
  366. if (ObjectUtils.isEmpty(phone)) {
  367. return CommonResult.fail(name + "的手机号不能为空");
  368. }
  369. }
  370. if (!ObjectUtils.isEmpty(phone)) {
  371. phones.add(phone);
  372. }
  373. if (phones.stream().distinct().count() != phones.size()) {
  374. return CommonResult.fail("导入的Excel中,手机号:" + phone + "存在重复数据");
  375. }
  376. String affiliate = dataFormatter.formatCellValue(row.getCell(11));//关联人
  377. if (eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Parent.getValue()) {
  378. if (ObjectUtils.isEmpty(affiliate)) {
  379. return CommonResult.fail(name + "的关联人不能为空");
  380. }
  381. }
  382. //性别是否为空判断
  383. String sex = dataFormatter.formatCellValue(row.getCell(4));
  384. if (ObjectUtils.isEmpty(sex)) {
  385. return CommonResult.fail(name + "的性别不能为空");
  386. }
  387. //家庭住址是否为空判断
  388. String address = dataFormatter.formatCellValue(row.getCell(13));
  389. if (ObjectUtils.isEmpty(address)) {
  390. return CommonResult.fail(name + "的家庭住址不能为空");
  391. }
  392. //民族是否为空判断
  393. String nation = dataFormatter.formatCellValue(row.getCell(14));
  394. if (ObjectUtils.isEmpty(nation)) {
  395. return CommonResult.fail(name + "的民族不能为空");
  396. }
  397. //部门是否为空判断
  398. String department = dataFormatter.formatCellValue(row.getCell(5));
  399. if (ObjectUtils.isEmpty(department)) {
  400. return CommonResult.fail(name + "的部门不能为空");
  401. }
  402. int lastIndex = department.lastIndexOf("/");
  403. Integer departmentId = null;
  404. if (lastIndex >= 0) {
  405. Optional<SmartDepartment> departModel = departments.stream().filter(e -> e.getName().equals(department.substring(lastIndex))).findFirst();
  406. if (departModel != null && departModel.isPresent()) {
  407. departmentId = departModel.get().getId();
  408. }
  409. }
  410. //学号重复判断
  411. String cardNo = dataFormatter.formatCellValue(row.getCell(0));
  412. if (!ObjectUtils.isEmpty(cardNo)) {
  413. cardNos.add(cardNo);
  414. }
  415. if (cardNos.stream().distinct().count() != cardNos.size()) {
  416. return CommonResult.fail("导入的Excel中,卡号:" + cardNo + "存在重复数据");
  417. }
  418. //身份证重复判断
  419. String idCard = dataFormatter.formatCellValue(row.getCell(3));
  420. if (!ObjectUtils.isEmpty(idCard)) {
  421. idCards.add(idCard);
  422. }
  423. //List<String> noEmptyIdCards = idCards.stream().filter(e -> !e.equals("") || e != null).collect(Collectors.toList());
  424. if (idCards.stream().distinct().count() != idCards.size()) {
  425. return CommonResult.fail("导入的Excel中,身份证号:" + idCard + "存在重复数据");
  426. }
  427. user.setCardNo(cardNo == null ? "" : cardNo);
  428. user.setName(name == null ? "" : name);
  429. user.setIdentityId(identity == null ? eIdentityStatu.Student.getValue() : eIdentityStatu.integerOf(identity));
  430. user.setIdCard(idCard == null ? "" : idCard);
  431. user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
  432. user.setDepartmentId(departmentId);
  433. String cellImage = dataFormatter.formatCellValue(row.getCell(6));
  434. user.setHeadImage(cellImage);
  435. user.setDormitoryNumber(dormitoryNumber == null ? "" : dormitoryNumber);
  436. user.setGrade(grade == null ? "" : grade);
  437. Optional<SmartGrade> oGrade = grades.stream().filter(e -> e.getName().equals(grade)).findFirst();
  438. if (oGrade != null && oGrade.isPresent()) {
  439. Integer gradeId = oGrade.get().getId();
  440. Optional<SmartClass> oClass = classs.stream().filter(e -> e.getName().equals(schoolClass) && e.getGradeId().equals(gradeId)).findFirst();
  441. if (oClass != null && oClass.isPresent()) {
  442. user.setSchoolClass(oClass.get().getId());
  443. } else {
  444. return CommonResult.fail(name + "的班级数据无效,导入失败");
  445. }
  446. } else {
  447. return CommonResult.fail(name + "的年级数据无效,导入失败");
  448. }
  449. user.setCollege("");
  450. user.setSpeciality("");
  451. user.setCampus("");
  452. user.setPhone(phone == null ? "" : phone);
  453. user.setAffiliate(affiliate == null ? "" : affiliate);
  454. String title = dataFormatter.formatCellValue(row.getCell(12));//职称
  455. user.setTitle(title == null ? "" : title);
  456. user.setAddress(address == null ? "" : address);
  457. user.setNation(nation == null ? "" : nation);
  458. String ofStudent = dataFormatter.formatCellValue(row.getCell(15));//生源地
  459. user.setOfStudent(ofStudent == null ? "" : ofStudent);
  460. String graduate = dataFormatter.formatCellValue(row.getCell(16));//毕业学校
  461. user.setGraduate(graduate == null ? "" : graduate);
  462. String duties = dataFormatter.formatCellValue(row.getCell(17));//职务
  463. user.setDuties(duties == null ? "" : duties);
  464. user.setIsCancel(eLogOff.Unlogout.getValue());
  465. result.add(user);
  466. }
  467. }
  468. }
  469. } catch (Exception e) {
  470. return CommonResult.fail("请按模板格式导入数据");
  471. }
  472. return CommonResult.ok(result);
  473. }
  474. /**
  475. * Xls文件读取方法
  476. *
  477. * @param inputStream 文件流
  478. * @return
  479. * @throws IOException
  480. */
  481. private CommonResult<List<SmartUser>> readXls(InputStream inputStream) throws IOException {
  482. List<String> phones = new ArrayList<>();
  483. List<String> idCards = new ArrayList<>();
  484. List<String> cardNos = new ArrayList<>();
  485. List<SmartUser> result = new ArrayList<>();
  486. HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
  487. List<SmartDepartment> departments = smartDepartmentService.list(null);
  488. List<SmartGrade> grades = smartGradeService.list(null); //年级
  489. List<SmartClass> classs = smartClassService.list(null);//班级
  490. //读取第一张sheet
  491. HSSFSheet sheetAt = sheets.getSheetAt(0);
  492. DataFormatter dataFormatter = new DataFormatter();
  493. try {
  494. for (int rowNum = 0; rowNum < sheetAt.getLastRowNum() + 1; rowNum++) {
  495. HSSFRow row = sheetAt.getRow(rowNum);
  496. if (row != null) {
  497. //使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
  498. //所以先使用setCellType()方法先将该单元格的类型设置为STRING
  499. //然后poi会根据字符串读取它
  500. //标题 校验
  501. if (rowNum == 0) {
  502. String cardNo = dataFormatter.formatCellValue(row.getCell(0));//学号
  503. if (!cardNo.equals("学号")) {
  504. return CommonResult.fail("导入数据第一列为学号");
  505. }
  506. String name = dataFormatter.formatCellValue(row.getCell(1));//姓名
  507. if (!name.equals("姓名")) {
  508. return CommonResult.fail("导入数据第二列为姓名");
  509. }
  510. String identity = dataFormatter.formatCellValue(row.getCell(2));//身份
  511. if (!identity.equals("身份")) {
  512. return CommonResult.fail("导入数据第三列为身份");
  513. }
  514. String idCard = dataFormatter.formatCellValue(row.getCell(3));//身份证
  515. if (!idCard.equals("身份证")) {
  516. return CommonResult.fail("导入数据第四列为身份证");
  517. }
  518. String sex = dataFormatter.formatCellValue(row.getCell(4));//性别
  519. if (!sex.equals("性别")) {
  520. return CommonResult.fail("导入数据第五列为性别");
  521. }
  522. String department = dataFormatter.formatCellValue(row.getCell(5));//部门
  523. if (!department.equals("部门")) {
  524. return CommonResult.fail("导入数据第六列为部门");
  525. }
  526. String headImage = dataFormatter.formatCellValue(row.getCell(6));//人脸照片
  527. if (!headImage.equals("人脸照片")) {
  528. return CommonResult.fail("导入数据第七列为人脸照片");
  529. }
  530. String dormitoryNumber = dataFormatter.formatCellValue(row.getCell(7));//宿舍号
  531. if (!dormitoryNumber.equals("宿舍号")) {
  532. return CommonResult.fail("导入数据第八列为宿舍号");
  533. }
  534. String grade = dataFormatter.formatCellValue(row.getCell(8));//年级
  535. if (!grade.equals("年级")) {
  536. return CommonResult.fail("导入数据第九列为年级");
  537. }
  538. String schoolClass = dataFormatter.formatCellValue(row.getCell(9));//班级
  539. if (!schoolClass.equals("班级")) {
  540. return CommonResult.fail("导入数据第十列为班级");
  541. }
  542. String phone = dataFormatter.formatCellValue(row.getCell(10));//手机号
  543. if (!phone.equals("手机号")) {
  544. return CommonResult.fail("导入数据第十一列为手机号");
  545. }
  546. String affiliate = dataFormatter.formatCellValue(row.getCell(11));//关联人
  547. if (!affiliate.equals("关联人")) {
  548. return CommonResult.fail("导入数据第十二列为关联人");
  549. }
  550. String title = dataFormatter.formatCellValue(row.getCell(12));//职称
  551. if (!title.equals("职称")) {
  552. return CommonResult.fail("导入数据第十三列为职称");
  553. }
  554. String address = dataFormatter.formatCellValue(row.getCell(13));//家庭住址
  555. if (!address.equals("家庭住址")) {
  556. return CommonResult.fail("导入数据第十四列为家庭住址");
  557. }
  558. String nation = dataFormatter.formatCellValue(row.getCell(14));//民族
  559. if (!nation.equals("民族")) {
  560. return CommonResult.fail("导入数据第十五列为民族");
  561. }
  562. String ofStudent = dataFormatter.formatCellValue(row.getCell(15));//生源地
  563. if (!ofStudent.equals("生源地")) {
  564. return CommonResult.fail("导入数据第十六列为生源地");
  565. }
  566. String graduate = dataFormatter.formatCellValue(row.getCell(16));//毕业学校
  567. if (!graduate.equals("毕业学校")) {
  568. return CommonResult.fail("导入数据第十七列为毕业学校");
  569. }
  570. String duties = dataFormatter.formatCellValue(row.getCell(17));//职务
  571. if (!duties.equals("职务")) {
  572. return CommonResult.fail("导入数据第十八列为职务");
  573. }
  574. } else {
  575. SmartUser user = new SmartUser();
  576. String name = dataFormatter.formatCellValue(row.getCell(1));
  577. if (ObjectUtils.isEmpty(name)) {
  578. return CommonResult.fail("第" + (rowNum + 2) + "条数据的名称不能为空");
  579. }
  580. //身份是否为空判断
  581. String identity = dataFormatter.formatCellValue(row.getCell(2));
  582. if (ObjectUtils.isEmpty(identity)) {
  583. return CommonResult.fail(name + "的身份不能为空");
  584. }
  585. String dormitoryNumber = dataFormatter.formatCellValue(row.getCell(7));//宿舍号
  586. String grade = dataFormatter.formatCellValue(row.getCell(8));//年级
  587. String schoolClass = dataFormatter.formatCellValue(row.getCell(9));//班级
  588. //身份为学生的情况下 宿舍号、年级、班级不能为空
  589. if (eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Student.getValue()) {
  590. if (ObjectUtils.isEmpty(dormitoryNumber)) {
  591. return CommonResult.fail(name + "的宿舍号不能为空");
  592. }
  593. if (ObjectUtils.isEmpty(grade)) {
  594. return CommonResult.fail(name + "的年级不能为空");
  595. }
  596. if (ObjectUtils.isEmpty(schoolClass)) {
  597. return CommonResult.fail(name + "的班级不能为空");
  598. }
  599. }
  600. //手机号重复判断
  601. String phone = dataFormatter.formatCellValue(row.getCell(10));
  602. if (eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Parent.getValue() || eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Teacher.getValue()) {
  603. if (ObjectUtils.isEmpty(phone)) {
  604. return CommonResult.fail(name + "的手机号不能为空");
  605. }
  606. }
  607. if (!ObjectUtils.isEmpty(phone)) {
  608. phones.add(phone);
  609. }
  610. if (phones.stream().distinct().count() != phones.size()) {
  611. return CommonResult.fail("导入的Excel中,手机号:" + phone + "存在重复数据");
  612. }
  613. String affiliate = dataFormatter.formatCellValue(row.getCell(11));//关联人
  614. if (eIdentityStatu.integerOf(identity).intValue() == eIdentityStatu.Parent.getValue()) {
  615. if (ObjectUtils.isEmpty(affiliate)) {
  616. return CommonResult.fail(name + "的关联人不能为空");
  617. }
  618. }
  619. //性别是否为空判断
  620. String sex = dataFormatter.formatCellValue(row.getCell(4));
  621. if (ObjectUtils.isEmpty(sex)) {
  622. return CommonResult.fail(name + "的性别不能为空");
  623. }
  624. //家庭住址是否为空判断
  625. String address = dataFormatter.formatCellValue(row.getCell(13));
  626. if (ObjectUtils.isEmpty(address)) {
  627. return CommonResult.fail(name + "的家庭住址不能为空");
  628. }
  629. //民族是否为空判断
  630. String nation = dataFormatter.formatCellValue(row.getCell(14));
  631. if (ObjectUtils.isEmpty(nation)) {
  632. return CommonResult.fail(name + "的民族不能为空");
  633. }
  634. //部门是否为空判断
  635. String department = dataFormatter.formatCellValue(row.getCell(5));
  636. if (ObjectUtils.isEmpty(department)) {
  637. return CommonResult.fail(name + "的部门不能为空");
  638. }
  639. int lastIndex = department.lastIndexOf("/");
  640. Integer departmentId = null;
  641. if (lastIndex >= 0) {
  642. Optional<SmartDepartment> departModel = departments.stream().filter(e -> e.getName().equals(department.substring(lastIndex))).findFirst();
  643. if (departModel != null && departModel.isPresent()) {
  644. departmentId = departModel.get().getId();
  645. }
  646. }
  647. //学号重复判断
  648. String cardNo = dataFormatter.formatCellValue(row.getCell(0));
  649. if (!ObjectUtils.isEmpty(cardNo)) {
  650. cardNos.add(cardNo);
  651. }
  652. if (cardNos.stream().distinct().count() != cardNos.size()) {
  653. return CommonResult.fail("导入的Excel中,卡号:" + cardNo + "存在重复数据");
  654. }
  655. //身份证重复判断
  656. String idCard = dataFormatter.formatCellValue(row.getCell(3));
  657. if (!ObjectUtils.isEmpty(idCard)) {
  658. idCards.add(idCard);
  659. }
  660. //List<String> noEmptyIdCards = idCards.stream().filter(e -> !e.equals("") || e != null).collect(Collectors.toList());
  661. if (idCards.stream().distinct().count() != idCards.size()) {
  662. return CommonResult.fail("导入的Excel中,身份证号:" + idCard + "存在重复数据");
  663. }
  664. user.setCardNo(cardNo == null ? "" : cardNo);
  665. user.setName(name == null ? "" : name);
  666. user.setIdentityId(identity == null ? eIdentityStatu.Student.getValue() : eIdentityStatu.integerOf(identity));
  667. user.setIdCard(idCard == null ? "" : idCard);
  668. user.setSexId(sex == null ? eSexStatu.Man.getValue() : eSexStatu.integerOf(sex));
  669. user.setDepartmentId(departmentId);
  670. String cellImage = dataFormatter.formatCellValue(row.getCell(6));
  671. user.setHeadImage(cellImage);
  672. user.setDormitoryNumber(dormitoryNumber == null ? "" : dormitoryNumber);
  673. user.setGrade(grade == null ? "" : grade);
  674. Optional<SmartGrade> oGrade = grades.stream().filter(e -> e.getName().equals(grade)).findFirst();
  675. if (oGrade != null && oGrade.isPresent()) {
  676. Integer gradeId = oGrade.get().getId();
  677. Optional<SmartClass> oClass = classs.stream().filter(e -> e.getName().equals(schoolClass) && e.getGradeId().equals(gradeId)).findFirst();
  678. if (oClass != null && oClass.isPresent()) {
  679. user.setSchoolClass(oClass.get().getId());
  680. } else {
  681. return CommonResult.fail(name + "的班级数据无效,导入失败");
  682. }
  683. } else {
  684. return CommonResult.fail(name + "的年级数据无效,导入失败");
  685. }
  686. user.setCollege("");
  687. user.setSpeciality("");
  688. user.setCampus("");
  689. user.setPhone(phone == null ? "" : phone);
  690. user.setAffiliate(affiliate == null ? "" : affiliate);
  691. String title = dataFormatter.formatCellValue(row.getCell(12));//职称
  692. user.setTitle(title == null ? "" : title);
  693. user.setAddress(address == null ? "" : address);
  694. user.setNation(nation == null ? "" : nation);
  695. String ofStudent = dataFormatter.formatCellValue(row.getCell(15));//生源地
  696. user.setOfStudent(ofStudent == null ? "" : ofStudent);
  697. String graduate = dataFormatter.formatCellValue(row.getCell(16));//毕业学校
  698. user.setGraduate(graduate == null ? "" : graduate);
  699. String duties = dataFormatter.formatCellValue(row.getCell(17));//职务
  700. user.setDuties(duties == null ? "" : duties);
  701. user.setIsCancel(eLogOff.Unlogout.getValue());
  702. result.add(user);
  703. }
  704. }
  705. }
  706. } catch (Exception e) {
  707. return CommonResult.fail("请按模板格式导入数据");
  708. }
  709. return CommonResult.ok(result);
  710. }
  711. /**
  712. * 新增用户
  713. *
  714. * @param isur 用户数据
  715. * @param bindingResult
  716. * @return
  717. */
  718. @Override
  719. public CommonResult insertSmartUser(insertSmartUserRequest isur, BindingResult bindingResult) throws Exception {
  720. if (bindingResult.hasErrors()) {
  721. String st = paramUtils.getParamError(bindingResult);
  722. return CommonResult.fail(st);
  723. }
  724. //重复性判断
  725. int existCount = smartUserService.querySmartUserByCardNo(isur.getCardNo());
  726. if (existCount > 0) {
  727. return CommonResult.fail("当前学号已存在,请勿重复添加");
  728. }
  729. SmartUser su = new SmartUser();
  730. su.setCardNo(isur.getCardNo());
  731. su.setName(isur.getName());
  732. su.setIdentityId(isur.getIdentityId());
  733. su.setIdCard(isur.getIdCard());
  734. su.setSexId(isur.getSexId());
  735. su.setDepartmentId(isur.getDepartmentId());
  736. su.setHeadImage(isur.getHeadImage());
  737. su.setGrade(isur.getGrade());
  738. su.setCollege(isur.getCollege());
  739. su.setSpeciality(isur.getSpeciality());
  740. su.setSchoolClass(isur.getSchoolClass());
  741. su.setCampus(isur.getCampus());
  742. su.setDormitoryNumber(isur.getDormitoryNumber());
  743. su.setPhone(isur.getPhone());
  744. su.setAffiliate(StringUtils.join(isur.getAffiliate(), ","));
  745. su.setTitle(isur.getTitle());
  746. su.setAddress(isur.getAddress());
  747. su.setNation(isur.getNation());
  748. su.setOfStudent(isur.getOfStudent());
  749. su.setGraduate(isur.getGraduate());
  750. su.setDuties(isur.getDuties());
  751. su.setIsCancel(eLogOff.Unlogout.getValue());
  752. //region 人员信息加入到第三方api
  753. //要将用户数据加入到希沃和百胜中
  754. //希沃和百胜的老师、学生数据添加是不一样的,所以按身份添加
  755. if (isur.getIdentityId().intValue() == eIdentityStatu.Parent.getValue()) {//家长
  756. //拿到被关联学生的信息去获取对应的卡号
  757. //有多个学生就循环学生
  758. if (isur.getAffiliate() == null) {
  759. return CommonResult.fail("被关联人不能为空");
  760. }
  761. if (isur.getAffiliate().size() <= 0) {
  762. return CommonResult.fail("被关联人不能为空");
  763. }
  764. List<SmartUser> studentDatas = smartUserService.getSmartUserIds(isur.getAffiliate());
  765. //region 希沃新增编辑学生家长信息
  766. //学生与家长列表,最大100条
  767. List<ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem> studentParents = new ArrayList<>();
  768. for (SmartUser student : studentDatas) {
  769. ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem students = ParentServiceBatchSaveOrUpdateParentsParam.StudentParentsItem.builder()
  770. .studentCode(student.getCardNo())
  771. .build();
  772. studentParents.add(students);
  773. //家长列表,最多4个
  774. ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem parents = ParentServiceBatchSaveOrUpdateParentsParam.ParentsItem.builder()
  775. .name(isur.getName())
  776. .phone(isur.getPhone())
  777. .index(0)
  778. .build();
  779. students.setParents(java.util.Collections.singletonList(parents));
  780. }
  781. //初始化客户端
  782. SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
  783. ParentServiceBatchSaveOrUpdateParentsParam param = new ParentServiceBatchSaveOrUpdateParentsParam();
  784. //响应体,MimeType为 application/json
  785. ParentServiceBatchSaveOrUpdateParentsParam.RequestBody requestBody = ParentServiceBatchSaveOrUpdateParentsParam.RequestBody.builder()
  786. .build();
  787. param.setRequestBody(requestBody);
  788. //query
  789. ParentServiceBatchSaveOrUpdateParentsParam.Query query = ParentServiceBatchSaveOrUpdateParentsParam.Query.builder()
  790. .appId(seewoConfig.getAppId())
  791. .schoolUid(seewoConfig.getSchoolId())
  792. .build();
  793. requestBody.setQuery(query);
  794. query.setStudentParents(studentParents);
  795. param.setRequestBody(requestBody);
  796. ParentServiceBatchSaveOrUpdateParentsRequest request = new ParentServiceBatchSaveOrUpdateParentsRequest(param);
  797. System.out.println("入参:" + request);
  798. //如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
  799. //request.setServerUrl("https://openapi.test.seewo.com")
  800. //执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
  801. ParentServiceBatchSaveOrUpdateParentsResult result = seewoClient.invoke(request);
  802. System.out.println("出参:" + result);
  803. //endregion
  804. if (result == null) {
  805. return CommonResult.fail("希沃学生家长数据更新失败!");
  806. }
  807. if (!result.getResponseBody().getCode().equals("000000")) {
  808. return CommonResult.fail(result.getResponseBody().getMessage());
  809. }
  810. } else if (isur.getIdentityId().intValue() == eIdentityStatu.Student.getValue()) {//学生
  811. //获取班级Uid
  812. SmartClass classData = smartClassService.getSmartClassById(isur.getSchoolClass());
  813. if (classData == null) {
  814. return CommonResult.fail("班级数据无效,新增失败");
  815. }
  816. //region 希沃新增学生信息
  817. //初始化客户端
  818. SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
  819. StudentServiceBatchSaveClassStudentsParam param = new StudentServiceBatchSaveClassStudentsParam();
  820. //请求体,MimeType为 application/json
  821. StudentServiceBatchSaveClassStudentsParam.JSONRequestBody requestBody = StudentServiceBatchSaveClassStudentsParam.JSONRequestBody.builder()
  822. .build();
  823. param.setRequestBody(requestBody);
  824. //查询条件
  825. StudentServiceBatchSaveClassStudentsParam.StudentSaveQuery query = StudentServiceBatchSaveClassStudentsParam.StudentSaveQuery.builder()
  826. .appId(seewoConfig.getAppId())
  827. .schoolUid(seewoConfig.getSchoolId())
  828. .classUid(classData.getClassUid())
  829. .build();
  830. requestBody.setQuery(query);
  831. // 学生列表
  832. StudentServiceBatchSaveClassStudentsParam.StudentInfo students = StudentServiceBatchSaveClassStudentsParam.StudentInfo.builder()
  833. .studentName(isur.getName())
  834. .studentCode(isur.getCardNo())
  835. .gender(isur.getSexId())
  836. .phone(isur.getPhone() == null ? "" : isur.getPhone())
  837. .build();
  838. query.setStudents(java.util.Collections.singletonList(students));
  839. query.setInPlaceOld(true);// 是否删除旧学生再保存
  840. param.setRequestBody(requestBody);
  841. StudentServiceBatchSaveClassStudentsRequest request = new StudentServiceBatchSaveClassStudentsRequest(param);
  842. System.out.println("入参:" + request);
  843. //如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
  844. //request.setServerUrl("https://openapi.test.seewo.com")
  845. //执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
  846. StudentServiceBatchSaveClassStudentsResult result = seewoClient.invoke(request);
  847. System.out.println("出参:" + result);
  848. if (result == null) {
  849. return CommonResult.fail("希沃学生数据新增失败!");
  850. }
  851. if (!result.getResponseBody().getCode().equals("000000")) {
  852. return CommonResult.fail(result.getResponseBody().getMessage());
  853. }
  854. su.setXwStudentUid(result.getResponseBody().getData().get(0).getUserUid());
  855. //endregion
  856. //region 百胜新增学生信息
  857. String appId = controlConfig.getAppId();
  858. String schoolno = controlConfig.getSchoolCode();
  859. String timestamp = TimeExchange.DateNowTimeStamo();
  860. String appSecret = controlConfig.getAppSecret();
  861. String url = "http://schoolopenapi.szymzh.com/openapi/student/create";
  862. JSONObject jsonobject = new JSONObject();
  863. jsonobject.put("appid", appId);
  864. String str = "{\"student_name\":\"" + isur.getName() + "\",\"classtab_no\":\"" + classData.getBsClassNo() + "\",\"student_number\":\"" + isur.getCardNo() + "\",\"student_sex\":\"" + isur.getSexId() + "\",\"student_photo\":\"" + imageUtils.getBase64Url(isur.getHeadImage()) + "\"}";
  865. String aesStr = URLEncoder.encode(AesTestOne.encrypt(str), "UTF-8");
  866. jsonobject.put("data", aesStr);
  867. jsonobject.put("schoolno", schoolno);
  868. jsonobject.put("timestamp", timestamp);
  869. String md5Str = "appid=" + appId + "&data={\"student_name\":\"" + isur.getName() + "\",\"classtab_no\":\"" + classData.getBsClassNo() + "\",\"student_number\":\"" + isur.getCardNo() + "\",\"student_sex\":\"" + isur.getSexId() + "\",\"student_photo\":\"" + imageUtils.getBase64Url(isur.getHeadImage()) + "\"}" + "&schoolno=" + schoolno + "&timestamp=" + timestamp + "&key=" + appSecret;
  870. String sign = CommonUtil.MD5(md5Str);
  871. //sign签名
  872. jsonobject.put("sign", sign);
  873. //返回的结果中 code为1表示成功
  874. String bsResult = RequestUtils.httpPost(url, jsonobject.toJSONString());
  875. if (bsResult.contains("添加成功")) {
  876. ObjectMapper objectMapper = new ObjectMapper();
  877. BsStudentVo grade = objectMapper.readValue(bsResult, BsStudentVo.class);
  878. // URL解码
  879. String decodedUrl = URLDecoder.decode(grade.getData(), "UTF-8");
  880. BsStudentNoVo studentNo = objectMapper.readValue(decrypt(decodedUrl), BsStudentNoVo.class);
  881. su.setBsStudentNo(studentNo.getStudent_no());
  882. }
  883. //endregion
  884. } else if (isur.getIdentityId().intValue() == eIdentityStatu.Teacher.getValue()) {//老师
  885. //region 希沃添加教师数据
  886. //初始化客户端
  887. SeewoClient seewoClient = new DefaultSeewoClient(new Account(seewoConfig.getAppId(), seewoConfig.getAppSecret()));
  888. TeacherServiceBatchSaveOrUpdateTeacherParam param = new TeacherServiceBatchSaveOrUpdateTeacherParam();
  889. //请求体,MimeType为 application/json
  890. TeacherServiceBatchSaveOrUpdateTeacherParam.JSONRequestBody requestBody = TeacherServiceBatchSaveOrUpdateTeacherParam.JSONRequestBody.builder()
  891. .build();
  892. param.setRequestBody(requestBody);
  893. //老师信息
  894. TeacherServiceBatchSaveOrUpdateTeacherParam.SaveOrUpdateTeacherBatchQuery query = TeacherServiceBatchSaveOrUpdateTeacherParam.SaveOrUpdateTeacherBatchQuery.builder()
  895. .appId(seewoConfig.getAppId())
  896. .schoolUid(seewoConfig.getSchoolId())
  897. .build();
  898. requestBody.setQuery(query);
  899. // 老师列表
  900. TeacherServiceBatchSaveOrUpdateTeacherParam.SaveOrUpdateTeacherQuery teachers = TeacherServiceBatchSaveOrUpdateTeacherParam.SaveOrUpdateTeacherQuery.builder()
  901. .account(isur.getPhone())//用户账号
  902. .name(isur.getName())//用户名字
  903. .accountType("phone")//账号类型 phone:手机号 email:邮箱
  904. .teacherCode("")//教师工号
  905. .photoUrl(isur.getHeadImage())//图片链接
  906. .build();
  907. query.setTeachers(java.util.Collections.singletonList(teachers));
  908. param.setRequestBody(requestBody);
  909. TeacherServiceBatchSaveOrUpdateTeacherRequest request = new TeacherServiceBatchSaveOrUpdateTeacherRequest(param);
  910. System.out.println("入参:" + request);
  911. //如果想要调用沙箱环境,请通过设置 request 对象的 serverUrl 属性,如:
  912. //request.setServerUrl("https://openapi.test.seewo.com")
  913. //执行请求,如果想获取到com.seewo.open.sdk.HttpResponse对象,请调用 seewoClient.execute 方法
  914. TeacherServiceBatchSaveOrUpdateTeacherResult result = seewoClient.invoke(request);
  915. System.out.println("出参:" + result);
  916. if (result == null) {
  917. return CommonResult.fail("希沃学生数据新增失败!");
  918. }
  919. if (!result.getResponseBody().getCode().equals("000000")) {
  920. return CommonResult.fail(result.getResponseBody().getMessage());
  921. }
  922. su.setXwTeacherCode(result.getResponseBody().getData().get(0).getTeacherCode());
  923. //endregion
  924. //region 百胜添加教师数据
  925. SmartDepartment departmentData = smartDepartmentService.getSmartById(isur.getDepartmentId());
  926. if(departmentData == null){
  927. return CommonResult.fail("部门数据无效,新增教师失败");
  928. }
  929. String departmentNo = departmentData.getBsDepartmentNo();//"DT1701845086538710";
  930. String appId = controlConfig.getAppId();
  931. String schoolno = controlConfig.getSchoolCode();
  932. String timestamp = TimeExchange.DateNowTimeStamo();
  933. String appSecret = controlConfig.getAppSecret();
  934. String url = "http://schoolopenapi.szymzh.com/openapi/staff/create";
  935. JSONObject jsonobject = new JSONObject();
  936. jsonobject.put("appid", appId);
  937. String str = "{\"staff_name\":\"" + isur.getName() + "\",\"department_no\":\"" + departmentNo + "\",\"stafft_number\":\"" + isur.getCardNo() + "\",\"staff_phone\":\"" + isur.getPhone() + "\",\"staff_sex\":\"" + isur.getSexId() + "\",\"staff_photo\":\"" + imageUtils.getBase64Url(isur.getHeadImage()) + "\"}";
  938. String aesStr = URLEncoder.encode(AesTestOne.encrypt(str), "UTF-8");
  939. jsonobject.put("data", aesStr);
  940. jsonobject.put("schoolno", schoolno);
  941. jsonobject.put("timestamp", timestamp);
  942. String md5Str = "appid=" + appId + "&data={\"staff_name\":\"" + isur.getName() + "\",\"department_no\":\"" + departmentNo + "\",\"stafft_number\":\"" + isur.getCardNo() + "\",\"staff_phone\":\"" + isur.getPhone() + "\",\"staff_sex\":\"" + isur.getSexId() + "\",\"staff_photo\":\"" + imageUtils.getBase64Url(isur.getHeadImage()) + "\"}" + "&schoolno=" + schoolno + "&timestamp=" + timestamp + "&key=" + appSecret;
  943. String sign = CommonUtil.MD5(md5Str);
  944. //sign签名
  945. jsonobject.put("sign", sign);
  946. //返回的结果中 code为1表示成功
  947. String bsResult = RequestUtils.httpPost(url, jsonobject.toJSONString());
  948. if (bsResult.contains("添加成功")) {
  949. ObjectMapper objectMapper = new ObjectMapper();
  950. BsStaffVo staff = objectMapper.readValue(bsResult, BsStaffVo.class);
  951. // URL解码
  952. String decodedUrl = URLDecoder.decode(staff.getData(), "UTF-8");
  953. BsStaffNoVo staffNo = objectMapper.readValue(decrypt(decodedUrl), BsStaffNoVo.class);
  954. su.setBsStaffCode(staffNo.getStaff_no());
  955. }
  956. //endregion
  957. }
  958. //endregion
  959. //最后都要把数据加入到数据库中
  960. int result = smartUserService.insertSmartUser(su);
  961. //新增用户得将用户信息通过接口推送到希沃、百胜
  962. return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
  963. }
  964. /**
  965. * 更新用户
  966. *
  967. * @param usur 更新用户数据
  968. * @param bindingResult
  969. * @return
  970. */
  971. @Override
  972. public CommonResult updateSmartUserById(updateSmartUserRequest usur, BindingResult bindingResult) {
  973. if (bindingResult.hasErrors()) {
  974. String st = paramUtils.getParamError(bindingResult);
  975. return CommonResult.fail(st);
  976. }
  977. //更新的同时将百胜用户信息同步过去或者同步过来?
  978. SmartUser su = smartUserService.getSmartById(usur.getId());
  979. if (su == null) {
  980. CommonResult.fail("用户数据已失效,修改失败!");
  981. }
  982. su.setCardNo(usur.getCardNo());
  983. su.setName(usur.getName());
  984. su.setIdentityId(usur.getIdentityId());
  985. su.setIdCard(usur.getIdCard());
  986. su.setSexId(usur.getSexId());
  987. su.setDepartmentId(usur.getDepartmentId());
  988. su.setHeadImage(usur.getHeadImage());
  989. su.setGrade(usur.getGrade());
  990. su.setCollege(usur.getCollege());
  991. su.setSpeciality(usur.getSpeciality());
  992. su.setSchoolClass(usur.getSchoolClass());
  993. su.setCampus(usur.getCampus());
  994. su.setDormitoryNumber(usur.getDormitoryNumber());
  995. su.setPhone(usur.getPhone());
  996. su.setAffiliate(StringUtils.join(usur.getAffiliate(), ","));
  997. su.setTitle(usur.getTitle());
  998. su.setAddress(usur.getAddress());
  999. su.setNation(usur.getNation());
  1000. su.setOfStudent(usur.getOfStudent());
  1001. su.setGraduate(usur.getGraduate());
  1002. su.setDuties(usur.getDuties());
  1003. su.setIsCancel(eLogOff.Unlogout.getValue());
  1004. int result = smartUserService.updateSmartUser(su);
  1005. return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
  1006. }
  1007. /**
  1008. * 用户分页数据查询
  1009. *
  1010. * @param currentPage 当前页数
  1011. * @param pageCount 一页数据条数
  1012. * @param departmentId 部门ID
  1013. * @return
  1014. */
  1015. @Override
  1016. public CommonResult queryPageSmartUser(int currentPage, int pageCount, Integer departmentId, String name) {
  1017. //获取该部门下的所有子级部门ID
  1018. List<Integer> childDepartmentIds = new ArrayList<>();
  1019. List<SmartDepartment> departments = smartDepartmentService.list(null);
  1020. childDepartmentIds.add(departmentId);
  1021. QueryDepartmentTreeRecords(departmentId, departments, childDepartmentIds);
  1022. if (departmentId == null) {
  1023. childDepartmentIds = null;
  1024. }
  1025. PageUtils<UserVo> result = smartUserService.querySmartUserPages(currentPage, pageCount, childDepartmentIds, name);
  1026. return CommonResult.ok(result);
  1027. }
  1028. @Override
  1029. public void smartUserExport(HttpServletResponse response, Integer departmentId, String name) {
  1030. //获取该部门下的所有子级部门ID
  1031. List<Integer> childDepartmentIds = new ArrayList<>();
  1032. List<SmartDepartment> departments = smartDepartmentService.list(null);
  1033. childDepartmentIds.add(departmentId);
  1034. QueryDepartmentTreeRecords(departmentId, departments, childDepartmentIds);
  1035. List<SmartUser> users = smartUserService.querySmartUsers(childDepartmentIds, name);
  1036. //导出
  1037. Workbook workbook = new XSSFWorkbook();
  1038. Sheet sheet = workbook.createSheet("用户信息管理");
  1039. Row headerRow = sheet.createRow(0);
  1040. headerRow.createCell(0).setCellValue("序号");
  1041. headerRow.createCell(1).setCellValue("学号");
  1042. headerRow.createCell(2).setCellValue("姓名");
  1043. headerRow.createCell(3).setCellValue("身份");
  1044. headerRow.createCell(4).setCellValue("身份证号");
  1045. headerRow.createCell(5).setCellValue("性别");
  1046. headerRow.createCell(6).setCellValue("部门");
  1047. headerRow.createCell(7).setCellValue("人脸");
  1048. headerRow.createCell(8).setCellValue("年级");
  1049. headerRow.createCell(9).setCellValue("学院");
  1050. headerRow.createCell(10).setCellValue("专业");
  1051. headerRow.createCell(11).setCellValue("班级");
  1052. headerRow.createCell(12).setCellValue("校区");
  1053. headerRow.createCell(13).setCellValue("宿舍号");
  1054. headerRow.createCell(14).setCellValue("手机号");
  1055. headerRow.createCell(15).setCellValue("关联人");
  1056. headerRow.createCell(16).setCellValue("职称");
  1057. headerRow.createCell(17).setCellValue("家庭住址");
  1058. headerRow.createCell(18).setCellValue("民族");
  1059. headerRow.createCell(19).setCellValue("生源地");
  1060. headerRow.createCell(20).setCellValue("毕业学校");
  1061. headerRow.createCell(21).setCellValue("职务");
  1062. for (int i = 0; i < users.size(); i++) {
  1063. SmartUser user = users.get(i);
  1064. Row dataRow = sheet.createRow(i + 1);
  1065. dataRow.createCell(0).setCellValue(i + 1);
  1066. dataRow.createCell(1).setCellValue(user.getCardNo());
  1067. dataRow.createCell(2).setCellValue(user.getName());
  1068. dataRow.createCell(3).setCellValue(eIdentityStatu.stringOf(user.getIdentityId()));
  1069. dataRow.createCell(4).setCellValue(user.getIdCard());
  1070. dataRow.createCell(5).setCellValue(eSexStatu.stringOf(user.getSexId()));
  1071. //获取父级部门ID
  1072. Optional<SmartDepartment> department = departments.stream().filter(e -> e.getId().equals(user.getDepartmentId())).findFirst();
  1073. if (department != null && department.isPresent()) {
  1074. dataRow.createCell(6).setCellValue(QueryParentDepartments(department.get().getParentId(), departments, null));
  1075. }
  1076. dataRow.createCell(7).setCellValue(user.getHeadImage());
  1077. dataRow.createCell(8).setCellValue(user.getGrade());
  1078. dataRow.createCell(9).setCellValue(user.getCollege());
  1079. dataRow.createCell(10).setCellValue(user.getSpeciality());
  1080. dataRow.createCell(11).setCellValue(user.getSchoolClass());
  1081. dataRow.createCell(12).setCellValue(user.getCampus());
  1082. dataRow.createCell(13).setCellValue(user.getDormitoryNumber());
  1083. dataRow.createCell(14).setCellValue(user.getPhone());
  1084. dataRow.createCell(15).setCellValue(user.getAffiliate());
  1085. dataRow.createCell(16).setCellValue(user.getTitle());
  1086. dataRow.createCell(17).setCellValue(user.getAddress());
  1087. dataRow.createCell(18).setCellValue(user.getNation());
  1088. dataRow.createCell(19).setCellValue(user.getOfStudent());
  1089. dataRow.createCell(20).setCellValue(user.getGraduate());
  1090. dataRow.createCell(21).setCellValue(user.getDuties());
  1091. }
  1092. // 将工作簿写入文件
  1093. ExcelUtils.excelDownload(workbook, "用户信息.xlsx", response);
  1094. }
  1095. /**
  1096. * 根据父级ID获取树形数据
  1097. *
  1098. * @param parentID 父级ID
  1099. * @param lists 数据集合
  1100. * @return
  1101. */
  1102. private List<DepartmentTreeVo> QueryDepartmentTreeRecords(Integer parentID, List<SmartDepartment> lists, List<Integer> departmentIds) {
  1103. List<DepartmentTreeVo> newTrees = new ArrayList<>();
  1104. List<SmartDepartment> datas = lists.stream().filter(e -> e.getParentId().equals(parentID)).collect(Collectors.toList());
  1105. for (SmartDepartment data : datas) {
  1106. departmentIds.add(data.getId());
  1107. QueryDepartmentTreeRecords(data.getId(), lists, departmentIds);
  1108. }
  1109. return newTrees;
  1110. }
  1111. /**
  1112. * 根据父级ID获取父级数据
  1113. *
  1114. * @param parentID 子级ID
  1115. * @param lists 数据集合
  1116. * @return
  1117. */
  1118. private String QueryParentDepartments(Integer parentID, List<SmartDepartment> lists, String departmentStr) {
  1119. Optional<SmartDepartment> data = lists.stream().filter(e -> e.getId().equals(parentID)).findFirst();
  1120. if (data != null && data.isPresent()) {
  1121. departmentStr = departmentStr == null ? data.get().getName() : data.get().getName() + "/" + departmentStr;
  1122. QueryParentDepartments(data.get().getParentId(), lists, departmentStr);
  1123. }
  1124. return departmentStr;
  1125. }
  1126. @Override
  1127. public CommonResult deleteSmartUserById(useridsRequest ur, BindingResult bindingResult) {
  1128. if (bindingResult.hasErrors()) {
  1129. String st = paramUtils.getParamError(bindingResult);
  1130. return CommonResult.fail(st);
  1131. }
  1132. int data = smartUserService.getSmartUserCountByIds(ur.getUserIds());
  1133. if (data != ur.getUserIds().size()) {
  1134. return CommonResult.fail("存在无效用户数据,删除失败!");
  1135. }
  1136. int result = smartUserService.deleteSmartUserByIds(ur.getUserIds());
  1137. return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
  1138. }
  1139. @Override
  1140. public CommonResult queryAffiliateUserById(int id) {
  1141. List<AffiliateUserVo> result = smartUserService.queryAffiliateUserById(id);
  1142. return CommonResult.ok(result);
  1143. }
  1144. @Override
  1145. public CommonResult downloadUserExcel() {
  1146. return CommonResult.ok("200", "操作成功", "https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/excelModel/人员信息表.xlsx");
  1147. }
  1148. @Override
  1149. public CommonResult queryStudents(int userId) {
  1150. List<SmartUser> students = new ArrayList<>();
  1151. SmartUser user = smartUserService.getSmartById(userId);
  1152. if (user == null) {
  1153. return CommonResult.fail("用户信息为空,获取学生列表数据失败");
  1154. }
  1155. if (user.getAffiliate() == null) {
  1156. return CommonResult.ok(students);
  1157. }
  1158. List<Integer> affiliateIds = new ArrayList<>();
  1159. List<String> affiliates = Arrays.asList(user.getAffiliate().split(","));
  1160. for (String affiliate : affiliates) {
  1161. affiliateIds.add(Integer.valueOf(affiliate));
  1162. }
  1163. students = smartUserService.getSmartUserByIds(affiliateIds);
  1164. List<ParentOfStudentsVo> result = new ArrayList<>();
  1165. for (SmartUser student : students) {
  1166. ParentOfStudentsVo data = new ParentOfStudentsVo();
  1167. data.setId(student.getId());
  1168. data.setName(student.getName());
  1169. data.setCardNo(student.getCardNo());
  1170. data.setDepartmentId(student.getDepartmentId());
  1171. result.add(data);
  1172. }
  1173. return CommonResult.ok(result);
  1174. }
  1175. public static void main(String[] args) throws Exception {
  1176. //region 百胜新增学生信息
  1177. String image = "https://wanzai-1306339220.cos.ap-shanghai.myqcloud.com/out_www.yalijuda.com_%E7%94%98%E6%98%B1%E6%96%B0%E3%80%9018320846714%E3%80%91_TSZ5EdgPMi%20%281%29.jpg";
  1178. String appId = "sc5efc60f2bd373df9";
  1179. String schoolno = "SL1701743624375793";
  1180. String timestamp = "20240110090422121";//TimeExchange.DateNowTimeStamo();
  1181. String appSecret = "fe0d767a2a394d1b81ccda6fc0ce5ecc";
  1182. String url = "http://schoolopenapi.szymzh.com/openapi/student/create";
  1183. JSONObject jsonobject = new JSONObject();
  1184. jsonobject.put("appid", appId);
  1185. String str = "{\"student_name\":\"" + "李四" + "\",\"classtab_no\":\"" + "CS1704704260801286" + "\",\"student_number\":\"" + "A123457" + "\",\"student_sex\":\"" + 1 + "\",\"student_photo\":\"" + imageUtils.getBase64Url(image) + "\"}";
  1186. String aesStr = URLEncoder.encode(AesTestOne.encrypt(str), "UTF-8");
  1187. jsonobject.put("data", aesStr);
  1188. jsonobject.put("schoolno", schoolno);
  1189. jsonobject.put("timestamp", timestamp);
  1190. String md5Str = "appid=" + appId + "&data={\"student_name\":\"" + "李四" + "\",\"classtab_no\":\"" + "CS1704704260801286" + "\",\"student_number\":\"" + "A123457" + "\",\"student_sex\":\"" + 1 + "\",\"student_photo\":\"" + imageUtils.getBase64Url(image) + "\"}" + "&schoolno=" + schoolno + "&timestamp=" + timestamp + "&key=" + appSecret;
  1191. String sign = CommonUtil.MD5(md5Str);
  1192. //sign签名
  1193. jsonobject.put("sign", sign);
  1194. //返回的结果中 code为1表示成功
  1195. String bsResult = RequestUtils.httpPost(url, jsonobject.toJSONString());
  1196. System.out.println(bsResult);
  1197. if (bsResult.contains("添加成功")) {
  1198. ObjectMapper objectMapper = new ObjectMapper();
  1199. BsStudentVo grade = objectMapper.readValue(bsResult, BsStudentVo.class);
  1200. // URL解码
  1201. String decodedUrl = URLDecoder.decode(grade.getData(), "UTF-8");
  1202. BsStudentNoVo studentNo = objectMapper.readValue(decrypt(decodedUrl), BsStudentNoVo.class);
  1203. String sdsd = "";
  1204. }
  1205. //endregion
  1206. }
  1207. }