DzCardsController.java 16 KB

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