DzCardsController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.ruoyi.web.controller.dz;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5. import javax.servlet.http.HttpServletResponse;
  6. import javax.validation.ValidationException;
  7. import com.alibaba.fastjson2.JSONArray;
  8. import com.alibaba.fastjson2.JSONObject;
  9. import com.google.common.collect.Lists;
  10. import com.ruoyi.common.core.domain.entity.SysUser;
  11. import com.ruoyi.common.enums.ExamType;
  12. import com.ruoyi.common.enums.UserRegStatus;
  13. import com.ruoyi.common.utils.NumberUtils;
  14. import com.ruoyi.common.utils.SecurityUtils;
  15. import com.ruoyi.common.utils.bean.BeanUtils;
  16. import com.ruoyi.dz.domain.DzAgent;
  17. import com.ruoyi.dz.service.IDzAgentService;
  18. import com.ruoyi.enums.CardAction;
  19. import com.ruoyi.enums.CardType;
  20. import com.ruoyi.enums.UserTypeEnum;
  21. import com.ruoyi.system.service.ISysUserService;
  22. import com.ruoyi.web.domain.CardUserBody;
  23. import com.ruoyi.web.domain.DzCardExport;
  24. import com.ruoyi.web.service.SysLoginService;
  25. import com.ruoyi.web.service.SysRegisterService;
  26. import io.swagger.annotations.Api;
  27. import io.swagger.annotations.ApiOperation;
  28. import io.swagger.annotations.ApiParam;
  29. import org.apache.commons.lang3.ArrayUtils;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.springframework.security.access.prepost.PreAuthorize;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.util.CollectionUtils;
  34. import org.springframework.web.bind.annotation.*;
  35. import com.ruoyi.common.annotation.Log;
  36. import com.ruoyi.common.core.controller.BaseController;
  37. import com.ruoyi.common.core.domain.AjaxResult;
  38. import com.ruoyi.common.enums.BusinessType;
  39. import com.ruoyi.dz.domain.DzCards;
  40. import com.ruoyi.dz.service.IDzCardsService;
  41. import com.ruoyi.common.utils.poi.ExcelUtil;
  42. import com.ruoyi.common.core.page.TableDataInfo;
  43. /**
  44. * 学习卡Controller
  45. *
  46. * @author ruoyi
  47. * @date 2025-09-12
  48. */
  49. @RestController
  50. @RequestMapping("/dz/cards")
  51. @Api(tags = "后台-学习卡管理")
  52. public class DzCardsController extends BaseController
  53. {
  54. @Autowired
  55. private IDzCardsService dzCardsService;
  56. @Autowired
  57. private IDzAgentService agentService;
  58. @Autowired
  59. private ISysUserService sysUserService;
  60. @Autowired
  61. private SysLoginService sysLoginService;
  62. @Autowired
  63. private SysRegisterService sysRegisterService;
  64. /**
  65. * 查询学习卡列表
  66. */
  67. @PreAuthorize("@ss.hasPermi('dz:cards:list')")
  68. @GetMapping("/list")
  69. @ApiOperation("列表")
  70. public TableDataInfo list(DzCards dzCards)
  71. {
  72. prepare(dzCards);
  73. startPage();
  74. List<DzCards> list = dzCardsService.selectDzCardsList2(dzCards);
  75. return getDataTable(list);
  76. }
  77. private DzCards prepare(DzCards dzCards) {
  78. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  79. String userType = sysUser.getUserType();
  80. if (UserTypeEnum.isInstitution(userType)) {
  81. dzCards.setDeptId(SecurityUtils.getDeptId());
  82. } else if (UserTypeEnum.isSchool(userType)) {
  83. dzCards.setCampusId(sysUser.getUserTypeId());
  84. } else if (UserTypeEnum.isTeacher(userType)) {
  85. dzCards.setTeacherId(sysUser.getUserTypeId());
  86. }
  87. if ((UserTypeEnum.isAgent(userType)) && null == dzCards.getAgentId()) {
  88. //代理商及老师只能查看分配给自己的卡
  89. DzAgent agent = agentService.selectDzAgentByUserId(SecurityUtils.getUserId());
  90. if (null != agent) {
  91. if (NumberUtils.isPositive(agent.getParentId())) {
  92. dzCards.setLeafAgentId(agent.getAgentId());
  93. } else {
  94. dzCards.setAgentId(agent.getAgentId());
  95. }
  96. }
  97. }
  98. boolean beginBlank = StringUtils.isBlank(dzCards.getBegin());
  99. if (beginBlank && StringUtils.isNotBlank(dzCards.getEnd())) {
  100. dzCards.setBegin(dzCards.getEnd());
  101. } else if (!beginBlank) {
  102. dzCards.setEnd(dzCards.getBegin());
  103. }
  104. return dzCards;
  105. }
  106. /**
  107. * 导出学习卡列表
  108. */
  109. @PreAuthorize("@ss.hasPermi('dz:cards:export')")
  110. @Log(title = "学习卡", businessType = BusinessType.EXPORT)
  111. @PostMapping("/export")
  112. public void export(HttpServletResponse response, DzCards dzCards)
  113. {
  114. List<DzCards> list = dzCardsService.selectDzCardsList(prepare(dzCards));
  115. ExcelUtil<DzCardExport> util = new ExcelUtil<DzCardExport>(DzCardExport.class);
  116. util.exportExcel(response, list.stream().map(t -> {
  117. DzCardExport exp = new DzCardExport();
  118. BeanUtils.copyProperties(t, exp);
  119. return exp;
  120. }).collect(Collectors.toList()), "学习卡数据");
  121. }
  122. /**
  123. * 获取学习卡详细信息
  124. */
  125. @PreAuthorize("@ss.hasPermi('dz:cards:query')")
  126. @GetMapping(value = "/{cardId}")
  127. @ApiOperation("详情")
  128. public AjaxResult getInfo(@PathVariable("cardId") Long cardId)
  129. {
  130. return success(dzCardsService.selectDzCardsByCardId(cardId));
  131. }
  132. /**
  133. * 新增学习卡
  134. */
  135. @PreAuthorize("@ss.hasPermi('dz:cards:add')")
  136. @Log(title = "学习卡", businessType = BusinessType.INSERT)
  137. @PostMapping
  138. public AjaxResult add(@RequestBody DzCards dzCards)
  139. {
  140. return toAjax(dzCardsService.insertDzCards(dzCards));
  141. }
  142. /**
  143. * 修改学习卡
  144. */
  145. @PreAuthorize("@ss.hasPermi('dz:cards:edit')")
  146. @Log(title = "学习卡", businessType = BusinessType.UPDATE)
  147. @PutMapping
  148. public AjaxResult edit(@RequestBody DzCards dzCards)
  149. {
  150. return toAjax(dzCardsService.updateDzCards(dzCards));
  151. }
  152. /**
  153. * 删除学习卡
  154. */
  155. @PreAuthorize("@ss.hasPermi('dz:cards:remove')")
  156. @Log(title = "学习卡", businessType = BusinessType.DELETE)
  157. @DeleteMapping("/{cardIds}")
  158. public AjaxResult remove(@PathVariable Long[] cardIds)
  159. {
  160. return toAjax(dzCardsService.deleteDzCardsByCardIds(cardIds));
  161. }
  162. @Log(title = "制卡", businessType = BusinessType.INSERT)
  163. @PostMapping("/issueCard")
  164. @ApiOperation("制卡")
  165. @PreAuthorize("@ss.hasPermi('dz:cards:issue')")
  166. public AjaxResult issueCard(@ApiParam("机构") Long institutionId, @ApiParam("卡类型") String type, @ApiParam("数量") Integer count) {
  167. dzCardsService.issueCard(institutionId, getCardType(institutionId, type), count);
  168. return AjaxResult.success();
  169. }
  170. @Log(title = "分配开卡", businessType = BusinessType.INSERT)
  171. @PostMapping("/assignCard")
  172. @ApiOperation("分配即开卡")
  173. @PreAuthorize("@ss.hasPermi('dz:cards:assign')")
  174. public AjaxResult assignCard(@ApiParam("代理商") @RequestParam Long agentId, @ApiParam("卡类型") String type, @ApiParam(value = "卡类型") Integer cardType,
  175. @ApiParam("开始号") @RequestParam String begin, @ApiParam("结束号") @RequestParam String end,
  176. @ApiParam("省份") @RequestParam(required = false) String location,
  177. @ApiParam("考生类型") @RequestParam(required = false) ExamType examType,
  178. @ApiParam("学校") @RequestParam(required = false) Long schoolId,
  179. @ApiParam("有效天数") @RequestParam(required = false) Integer days)
  180. {
  181. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  182. DzAgent agent = agentService.selectDzAgentByAgentId(agentId);
  183. CardType ct = getCardType(agent.getDeptId(), null != cardType ? cardType.toString() : type);
  184. if(UserTypeEnum.Agent.getVal().equals(sysUser.getUserType())) { // 代理商分配
  185. if (!sysUser.getUserTypeId().equals(agent.getParentId())) {
  186. throw new ValidationException("只能分配给下级代理");
  187. }
  188. dzCardsService.assignCard(sysUser.getUserTypeId(), agentId, ct, begin, end, location, examType, schoolId, days);
  189. } else if(UserTypeEnum.Institution.getVal().equals(sysUser.getUserType())) { // 机构分配卡给自己的下级
  190. if (!sysUser.getDeptId().equals(agent.getDeptId())) {
  191. throw new ValidationException("只能分配给下级代理");
  192. }
  193. dzCardsService.assignCard(NumberUtils.isPositive(agent.getParentId()) ? agent.getParentId() : agentId, agentId, ct, begin, end, location, examType, schoolId, days);
  194. } else { // 平台指定, TODO 暂只支持二级代理
  195. dzCardsService.assignCard(NumberUtils.isPositive(agent.getParentId()) ? agent.getParentId() : agentId, agentId, ct, begin, end, location, examType, schoolId, days);
  196. }
  197. return AjaxResult.success();
  198. }
  199. @Log(title = "直接开卡", businessType = BusinessType.INSERT)
  200. @PostMapping("/openCard")
  201. @ApiOperation("直接开卡")
  202. public AjaxResult openCard(@ApiParam("代理商") Long agentId, @ApiParam("省份") String location, @ApiParam("学校") Long schoolId, @ApiParam("开始号") String begin, @ApiParam("结束号") String end,
  203. @ApiParam("有效天数") @RequestParam(required = false) Integer days)
  204. {
  205. return AjaxResult.error("暂不支持,现在分配即开卡"); // success(dzCardsService.openCard(agentId, location, schoolId, begin, end, days));
  206. }
  207. @Log(title = "分配校区", businessType = BusinessType.INSERT)
  208. @PostMapping("/changeCampus")
  209. @ApiOperation("分配校区")
  210. @PreAuthorize("@ss.hasPermi('dz:cards:associateCampus')")
  211. public AjaxResult changeCampus(@ApiParam("校区") Long campusId, @ApiParam("校区班级") Long campusClassId, @ApiParam("开始号") String begin, @ApiParam("结束号") String end) {
  212. return AjaxResult.success(dzCardsService.changeCampus(campusId, campusClassId, begin, end));
  213. }
  214. @Log(title = "支付", businessType = BusinessType.UPDATE)
  215. @PostMapping("/change/pay")
  216. @ApiOperation("支付")
  217. public AjaxResult changePay(@ApiParam("卡ID") @RequestParam Long[] cardIds) {
  218. return changeCard(CardAction.Pay, cardIds);
  219. }
  220. @Log(title = "续费", businessType = BusinessType.UPDATE)
  221. @PostMapping("/change/renew")
  222. @ApiOperation("续费")
  223. public AjaxResult changeRenew(@ApiParam("卡ID") @RequestParam Long[] cardIds) {
  224. return changeCard(CardAction.Renew, cardIds);
  225. }
  226. @Log(title = "结算", businessType = BusinessType.UPDATE)
  227. @PostMapping("/change/settlement")
  228. @ApiOperation("结算")
  229. public AjaxResult changeSettlement(@ApiParam("卡ID") @RequestParam Long[] cardIds) {
  230. return changeCard(CardAction.Settlement, cardIds);
  231. }
  232. @Log(title = "重卡", businessType = BusinessType.UPDATE)
  233. @PostMapping("/change/reopen")
  234. @ApiOperation("重卡")
  235. public AjaxResult changeReopen(@ApiParam("卡ID") @RequestParam Long[] cardIds) {
  236. return changeCard(CardAction.ReOpen, cardIds);
  237. }
  238. @Log(title = "退费", businessType = BusinessType.UPDATE)
  239. @PostMapping("/change/refund")
  240. @ApiOperation("退费")
  241. public AjaxResult changeRefund(@ApiParam("卡ID") @RequestParam Long[] cardIds) {
  242. return changeCard(CardAction.Refund, cardIds);
  243. }
  244. @Log(title = "关卡", businessType = BusinessType.UPDATE)
  245. @PostMapping("/change/close")
  246. @ApiOperation("关卡")
  247. public AjaxResult changeClose(@ApiParam("卡ID") @RequestParam Long[] cardIds) {
  248. return changeCard(CardAction.Close, cardIds);
  249. }
  250. @Log(title = "修改卡", businessType = BusinessType.UPDATE)
  251. @PostMapping("/changeCard")
  252. @ApiOperation("修改卡 重卡/关卡/支付/续费/结算")
  253. public AjaxResult changeCard(@ApiParam("操作") @RequestParam CardAction action, @ApiParam("卡ID") @RequestParam Long[] cardIds)
  254. {
  255. dzCardsService.changeCard(action, cardIds);
  256. if ((CardAction.Close.equals(action) || CardAction.Refund.equals(action)) && ArrayUtils.isNotEmpty(cardIds)) {
  257. sysLoginService.resetTokens(sysUserService.selectUserByCardIds(Arrays.asList(cardIds)));
  258. } else if (CardAction.ReOpen.equals(action) && ArrayUtils.isNotEmpty(cardIds)) {
  259. List<SysUser> sysUsers = sysUserService.selectUserByCardIds(Arrays.asList(cardIds));
  260. for (SysUser user : sysUsers) {
  261. if(UserRegStatus.User.equals(user.getRegStatus())) {
  262. SysUser upUser = new SysUser();
  263. upUser.setUserId(user.getUserId());
  264. upUser.setRegStatus(UserRegStatus.Student);
  265. sysUserService.updateUserProfile(upUser);
  266. }
  267. }
  268. sysLoginService.resetTokens(sysUsers);
  269. }
  270. return AjaxResult.success();
  271. }
  272. @Log(title = "修改卡用户信息", businessType = BusinessType.UPDATE)
  273. @PostMapping("/updateCardUser")
  274. @ApiOperation("修改卡用户信息")
  275. @PreAuthorize("@ss.hasPermi('dz:cards:updateuser')")
  276. public AjaxResult updateCardUser(@RequestBody CardUserBody cardUserBody) {
  277. sysRegisterService.improve(cardUserBody);
  278. return AjaxResult.success();
  279. }
  280. @GetMapping(value = "/cardUser/{cardId}")
  281. @ApiOperation("卡用户详情")
  282. @PreAuthorize("@ss.hasPermi('dz:cards:updateuser')")
  283. public AjaxResult getCardUser(@PathVariable("cardId") Long cardId)
  284. {
  285. CardUserBody cardUserBody = new CardUserBody();
  286. DzCards dzCards = dzCardsService.selectDzCardsByCardId(cardId);
  287. if(null == dzCards) {
  288. throw new ValidationException("卡号无效");
  289. }
  290. List<SysUser> sysUserList = sysUserService.selectUserByCardIds(Lists.newArrayList(cardId));
  291. if(CollectionUtils.isEmpty(sysUserList)) {
  292. throw new ValidationException("卡用户无效");
  293. }
  294. SysUser sysUser = sysUserList.get(0);
  295. BeanUtils.copyProperties(sysUser, cardUserBody, "password", "schoolId", "classId");
  296. cardUserBody.setMobile(sysUser.getPhonenumber());
  297. if (null != sysUser.getDirectedStudy()) {
  298. cardUserBody.setDirectionStudy(JSONArray.parse(sysUser.getDirectedStudy()));
  299. }
  300. BeanUtils.copyProperties(dzCards, cardUserBody, "password", "nickName", "location", "examType", "endYear");
  301. cardUserBody.setCampusSchoolId(dzCards.getCampusId());
  302. return success(cardUserBody);
  303. }
  304. /**
  305. * 统计学习卡数据
  306. */
  307. @PreAuthorize("@ss.hasPermi('dz:cards:list')")
  308. @GetMapping("/statistics")
  309. @ApiOperation("统计学习卡数据")
  310. public AjaxResult statistics(@RequestParam(required = false) Long deptId,
  311. @RequestParam(required = false) Long agentId,
  312. @RequestParam(required = false) String openTimeBegin,
  313. @RequestParam(required = false) String openTimeEnd)
  314. {
  315. java.util.Map<String, Object> params = new java.util.HashMap<>();
  316. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  317. Long campusId = null;
  318. Long teacherId = null;
  319. if (UserTypeEnum.isAgent(sysUser.getUserType())) {
  320. agentId = sysUser.getUserTypeId();
  321. } else if (UserTypeEnum.isSchool(sysUser.getUserType())) {
  322. campusId = sysUser.getUserTypeId();
  323. } else if (UserTypeEnum.isTeacher(sysUser.getUserType())) {
  324. teacherId = sysUser.getUserTypeId();
  325. } else if(!UserTypeEnum.isSys(sysUser.getUserType())) {
  326. deptId = sysUser.getDeptId();
  327. }
  328. if (deptId != null) {
  329. params.put("deptId", deptId);
  330. }
  331. if (agentId != null) {
  332. params.put("agentId", agentId);
  333. }
  334. if (campusId != null) {
  335. params.put("campusId", campusId);
  336. }
  337. if (teacherId != null) {
  338. params.put("teacherId", teacherId);
  339. }
  340. if (openTimeBegin != null && !openTimeBegin.isEmpty()) {
  341. params.put("openTimeBegin", openTimeBegin);
  342. }
  343. if (openTimeEnd != null && !openTimeEnd.isEmpty()) {
  344. params.put("openTimeEnd", openTimeEnd);
  345. }
  346. List<com.ruoyi.dz.dto.CardStatisticsDTO> list = dzCardsService.statisticCards(params);
  347. return success(list);
  348. }
  349. private CardType getCardType(Long institutionId, String type) {
  350. if(!NumberUtils.isNumeric(type)) {
  351. return CardType.valueOf(type);
  352. }
  353. if(Integer.parseInt(type) == 9) {
  354. return CardType.Experience;
  355. }
  356. return (null == institutionId || institutionId.equals(101L)) ? CardType.Platform : CardType.Dept;
  357. }
  358. /**
  359. * 统计详情 - 根据统计类型查询学习卡列表
  360. */
  361. @PreAuthorize("@ss.hasPermi('dz:cards:list')")
  362. @GetMapping("/statisticsDetail")
  363. @ApiOperation("统计详情 - 查询学习卡列表")
  364. public TableDataInfo statisticsDetail(@RequestParam String statisticsType,
  365. @RequestParam Long agentId)
  366. {
  367. DzCards dzCards = new DzCards();
  368. dzCards.setStatisticsType(statisticsType);
  369. dzCards.setAgentId(agentId);
  370. startPage();
  371. List<DzCards> list = dzCardsService.selectDzCardsList(prepare(dzCards));
  372. return getDataTable(list);
  373. }
  374. }