SmartUserController.java 85 KB

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