DzCardsController.java 16 KB

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