DzCardsController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.springframework.security.access.prepost.PreAuthorize;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.util.CollectionUtils;
  33. import org.springframework.web.bind.annotation.*;
  34. import com.ruoyi.common.annotation.Log;
  35. import com.ruoyi.common.core.controller.BaseController;
  36. import com.ruoyi.common.core.domain.AjaxResult;
  37. import com.ruoyi.common.enums.BusinessType;
  38. import com.ruoyi.dz.domain.DzCards;
  39. import com.ruoyi.dz.service.IDzCardsService;
  40. import com.ruoyi.common.utils.poi.ExcelUtil;
  41. import com.ruoyi.common.core.page.TableDataInfo;
  42. /**
  43. * 学习卡Controller
  44. *
  45. * @author ruoyi
  46. * @date 2025-09-12
  47. */
  48. @RestController
  49. @RequestMapping("/dz/cards")
  50. @Api(tags = "后台-学习卡管理")
  51. public class DzCardsController extends BaseController
  52. {
  53. @Autowired
  54. private IDzCardsService dzCardsService;
  55. @Autowired
  56. private IDzAgentService agentService;
  57. @Autowired
  58. private ISysUserService sysUserService;
  59. @Autowired
  60. private SysLoginService sysLoginService;
  61. @Autowired
  62. private SysRegisterService sysRegisterService;
  63. /**
  64. * 查询学习卡列表
  65. */
  66. @PreAuthorize("@ss.hasPermi('dz:cards:list')")
  67. @GetMapping("/list")
  68. @ApiOperation("列表")
  69. public TableDataInfo list(DzCards dzCards)
  70. {
  71. startPage();
  72. List<DzCards> list = dzCardsService.selectDzCardsList(prepare(dzCards));
  73. return getDataTable(list);
  74. }
  75. private DzCards prepare(DzCards dzCards) {
  76. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  77. String userType = sysUser.getUserType();
  78. if(UserTypeEnum.isInstitution(userType)){
  79. dzCards.setDeptId(SecurityUtils.getDeptId());
  80. } else if(UserTypeEnum.isSchool(userType)){
  81. dzCards.setCampusId(sysUser.getUserTypeId());
  82. }
  83. if((UserTypeEnum.isAgent(userType)||UserTypeEnum.isTeacher(userType))&&null==dzCards.getAgentId()){
  84. //代理商及老师只能查看分配给自己的卡
  85. DzAgent agent = agentService.selectDzAgentByUserId(SecurityUtils.getUserId());
  86. if (null!=agent){
  87. if(NumberUtils.isPositive(agent.getParentId())) {
  88. dzCards.setLeafAgentId(agent.getAgentId());
  89. } else {
  90. dzCards.setAgentId(agent.getAgentId());
  91. }
  92. }
  93. }
  94. return dzCards;
  95. }
  96. /**
  97. * 导出学习卡列表
  98. */
  99. @PreAuthorize("@ss.hasPermi('dz:cards:export')")
  100. @Log(title = "学习卡", businessType = BusinessType.EXPORT)
  101. @PostMapping("/export")
  102. public void export(HttpServletResponse response, DzCards dzCards)
  103. {
  104. List<DzCards> list = dzCardsService.selectDzCardsList(prepare(dzCards));
  105. for(DzCards dc : list) {
  106. dc.getDeptId();
  107. }
  108. ExcelUtil<DzCardExport> util = new ExcelUtil<DzCardExport>(DzCardExport.class);
  109. util.exportExcel(response, list.stream().map(t -> {
  110. DzCardExport exp = new DzCardExport();
  111. BeanUtils.copyProperties(t, exp);
  112. return exp;
  113. }).collect(Collectors.toList()), "学习卡数据");
  114. }
  115. /**
  116. * 获取学习卡详细信息
  117. */
  118. @PreAuthorize("@ss.hasPermi('dz:cards:query')")
  119. @GetMapping(value = "/{cardId}")
  120. @ApiOperation("详情")
  121. public AjaxResult getInfo(@PathVariable("cardId") Long cardId)
  122. {
  123. return success(dzCardsService.selectDzCardsByCardId(cardId));
  124. }
  125. /**
  126. * 新增学习卡
  127. */
  128. @PreAuthorize("@ss.hasPermi('dz:cards:add')")
  129. @Log(title = "学习卡", businessType = BusinessType.INSERT)
  130. @PostMapping
  131. public AjaxResult add(@RequestBody DzCards dzCards)
  132. {
  133. return toAjax(dzCardsService.insertDzCards(dzCards));
  134. }
  135. /**
  136. * 修改学习卡
  137. */
  138. @PreAuthorize("@ss.hasPermi('dz:cards:edit')")
  139. @Log(title = "学习卡", businessType = BusinessType.UPDATE)
  140. @PutMapping
  141. public AjaxResult edit(@RequestBody DzCards dzCards)
  142. {
  143. return toAjax(dzCardsService.updateDzCards(dzCards));
  144. }
  145. /**
  146. * 删除学习卡
  147. */
  148. @PreAuthorize("@ss.hasPermi('dz:cards:remove')")
  149. @Log(title = "学习卡", businessType = BusinessType.DELETE)
  150. @DeleteMapping("/{cardIds}")
  151. public AjaxResult remove(@PathVariable Long[] cardIds)
  152. {
  153. return toAjax(dzCardsService.deleteDzCardsByCardIds(cardIds));
  154. }
  155. @Log(title = "制卡", businessType = BusinessType.INSERT)
  156. @PostMapping("/issueCard")
  157. @ApiOperation("制卡")
  158. public AjaxResult issueCard(@ApiParam("机构") Long institutionId, @ApiParam("卡类型") String type, @ApiParam("数量") Integer count)
  159. {
  160. dzCardsService.issueCard(institutionId, getCardType(institutionId, type), count);
  161. return AjaxResult.success();
  162. }
  163. @Log(title = "分配开卡", businessType = BusinessType.INSERT)
  164. @PostMapping("/assignCard")
  165. @ApiOperation("分配即开卡")
  166. public AjaxResult assignCard(@ApiParam("代理商") @RequestParam Long agentId, @ApiParam("卡类型") String type, @ApiParam(value = "卡类型") Integer cardType,
  167. @ApiParam("开始号") @RequestParam String begin, @ApiParam("结束号") @RequestParam String end,
  168. @ApiParam("省份") @RequestParam(required = false) String location,
  169. @ApiParam("考生类型") @RequestParam(required = false) ExamType examType,
  170. @ApiParam("学校") @RequestParam(required = false) Long schoolId,
  171. @ApiParam("有效天数") @RequestParam(required = false) Integer days)
  172. {
  173. SysUser sysUser = SecurityUtils.getLoginUser().getUser();
  174. DzAgent agent = agentService.selectDzAgentByAgentId(agentId);
  175. CardType ct = getCardType(agent.getDeptId(), null != cardType ? cardType.toString() : type);
  176. if(UserTypeEnum.Agent.getVal().equals(sysUser.getUserType())) { // 代理商分配
  177. if (!sysUser.getUserTypeId().equals(agent.getParentId())) {
  178. throw new ValidationException("只能分配给下级代理");
  179. }
  180. dzCardsService.assignCard(sysUser.getUserTypeId(), agentId, ct, begin, end, location, examType, schoolId, days);
  181. } else if(UserTypeEnum.Institution.getVal().equals(sysUser.getUserType())) { // 机构分配卡给自己的下级
  182. if (!sysUser.getDeptId().equals(agent.getDeptId())) {
  183. throw new ValidationException("只能分配给下级代理");
  184. }
  185. dzCardsService.assignCard(NumberUtils.isPositive(agent.getParentId()) ? agent.getParentId() : agentId, agentId, ct, begin, end, location, examType, schoolId, days);
  186. } else { // 平台指定, TODO 暂只支持二级代理
  187. dzCardsService.assignCard(NumberUtils.isPositive(agent.getParentId()) ? agent.getParentId() : agentId, agentId, ct, begin, end, location, examType, schoolId, days);
  188. }
  189. return AjaxResult.success();
  190. }
  191. @Log(title = "直接开卡", businessType = BusinessType.INSERT)
  192. @PostMapping("/openCard")
  193. @ApiOperation("直接开卡")
  194. public AjaxResult openCard(@ApiParam("代理商") Long agentId, @ApiParam("省份") String location, @ApiParam("学校") Long schoolId, @ApiParam("开始号") String begin, @ApiParam("结束号") String end,
  195. @ApiParam("有效天数") @RequestParam(required = false) Integer days)
  196. {
  197. return AjaxResult.error("暂不支持,现在分配即开卡"); // success(dzCardsService.openCard(agentId, location, schoolId, begin, end, days));
  198. }
  199. @Log(title = "分配校区", businessType = BusinessType.INSERT)
  200. @PostMapping("/changeCampus")
  201. @ApiOperation("分配校区")
  202. public AjaxResult changeCampus(@ApiParam("校区") Long campusId, @ApiParam("开始号") String begin, @ApiParam("结束号") String end)
  203. {
  204. return AjaxResult.success(dzCardsService.changeCampus(campusId, begin, end));
  205. }
  206. @Log(title = "修改卡", businessType = BusinessType.UPDATE)
  207. @PostMapping("/changeCard")
  208. @ApiOperation("修改卡 重卡/关卡/支付/续费/结算")
  209. public AjaxResult changeCard(@ApiParam("操作") @RequestParam CardAction action, @ApiParam("卡ID") @RequestParam Long[] cardIds)
  210. {
  211. dzCardsService.changeCard(action, cardIds);
  212. if (CardAction.Close.equals(action) && ArrayUtils.isNotEmpty(cardIds)) {
  213. sysLoginService.resetTokens(sysUserService.selectUserByCardIds(Arrays.asList(cardIds)));
  214. } else if (CardAction.ReOpen.equals(action) && ArrayUtils.isNotEmpty(cardIds)) {
  215. List<SysUser> sysUsers = sysUserService.selectUserByCardIds(Arrays.asList(cardIds));
  216. for (SysUser user : sysUsers) {
  217. if(UserRegStatus.User.equals(user.getRegStatus())) {
  218. SysUser upUser = new SysUser();
  219. upUser.setUserId(user.getUserId());
  220. upUser.setRegStatus(UserRegStatus.Student);
  221. sysUserService.updateUserProfile(upUser);
  222. }
  223. }
  224. sysLoginService.resetTokens(sysUsers);
  225. }
  226. return AjaxResult.success();
  227. }
  228. @Log(title = "修改卡用户信息", businessType = BusinessType.UPDATE)
  229. @PostMapping("/updateCardUser")
  230. @ApiOperation("修改卡用户信息")
  231. public AjaxResult updateCardUser(@RequestBody CardUserBody cardUserBody) {
  232. sysRegisterService.improve(cardUserBody);
  233. return AjaxResult.success();
  234. }
  235. @GetMapping(value = "/cardUser/{cardId}")
  236. @ApiOperation("卡用户详情")
  237. public AjaxResult getCardUser(@PathVariable("cardId") Long cardId)
  238. {
  239. CardUserBody cardUserBody = new CardUserBody();
  240. DzCards dzCards = dzCardsService.selectDzCardsByCardId(cardId);
  241. if(null == dzCards) {
  242. throw new ValidationException("卡号无效");
  243. }
  244. List<SysUser> sysUserList = sysUserService.selectUserByCardIds(Lists.newArrayList(cardId));
  245. if(CollectionUtils.isEmpty(sysUserList)) {
  246. throw new ValidationException("卡用户无效");
  247. }
  248. SysUser sysUser = sysUserList.get(0);
  249. BeanUtils.copyProperties(sysUser, cardUserBody, "password", "schoolId", "classId");
  250. cardUserBody.setMobile(sysUser.getPhonenumber());
  251. if (null != sysUser.getDirectedStudy()) {
  252. cardUserBody.setDirectionStudy(JSONArray.parse(sysUser.getDirectedStudy()));
  253. }
  254. BeanUtils.copyProperties(dzCards, cardUserBody, "password", "nickName", "location", "examType", "endYear");
  255. return success(cardUserBody);
  256. }
  257. private CardType getCardType(Long institutionId, String type) {
  258. if(!NumberUtils.isNumeric(type)) {
  259. return CardType.valueOf(type);
  260. }
  261. if(Integer.parseInt(type) == 9) {
  262. return CardType.Experience;
  263. }
  264. return (null == institutionId || institutionId.equals(101L)) ? CardType.Platform : CardType.Dept;
  265. }
  266. }