VipCodeController.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.miaxis.pc.controller.vip;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.miaxis.common.constant.Constants;
  4. import java.util.List;
  5. import java.util.Arrays;
  6. import java.util.Random;
  7. import com.miaxis.vip.dto.QueryVipCodeListDTO;
  8. import io.swagger.annotations.*;
  9. import com.miaxis.common.core.domain.Response;
  10. import org.springframework.security.access.prepost.PreAuthorize;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.web.bind.annotation.GetMapping;
  13. import org.springframework.web.bind.annotation.PostMapping;
  14. import org.springframework.web.bind.annotation.PutMapping;
  15. import org.springframework.web.bind.annotation.DeleteMapping;
  16. import org.springframework.web.bind.annotation.PathVariable;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import org.springframework.web.bind.annotation.ModelAttribute;
  21. import com.miaxis.common.annotation.Log;
  22. import com.miaxis.common.core.controller.BaseController;
  23. import com.miaxis.common.enums.BusinessTypeEnum;
  24. import com.miaxis.vip.domain.VipCode;
  25. import com.miaxis.vip.service.IVipCodeService;
  26. import com.miaxis.common.utils.poi.ExcelUtil;
  27. import com.miaxis.common.core.page.ResponsePageInfo;
  28. /**
  29. * 【激活码】Controller
  30. *
  31. * @author miaxis
  32. * @date 2021-08-18
  33. */
  34. @RestController
  35. @RequestMapping("/vip/code")
  36. @Api(tags={"【pc-激活码】"})
  37. public class VipCodeController extends BaseController{
  38. @Autowired
  39. private IVipCodeService vipCodeService;
  40. /**
  41. * 查询激活码列表
  42. */
  43. @PreAuthorize("@ss.hasPermi('vip:code:list')")
  44. @GetMapping("/list")
  45. @ApiOperation("查询激活码列表")
  46. @ApiImplicitParams({
  47. @ApiImplicitParam(name = "pageNum",value = "当前页码" ,dataType = "int", paramType = "query", required = false),
  48. @ApiImplicitParam(name = "pageSize",value = "每页数据量" , dataType = "int", paramType = "query", required = false),
  49. })
  50. public ResponsePageInfo<VipCode> list(@ModelAttribute QueryVipCodeListDTO dto){
  51. startPage();
  52. List<VipCode> list = vipCodeService.selectVipCodeList(dto);
  53. return toResponsePageInfo(list);
  54. }
  55. /**
  56. * 导出激活码列表
  57. */
  58. @PreAuthorize("@ss.hasPermi('vip:code:export')")
  59. @Log(title = "激活码", businessType = BusinessTypeEnum.EXPORT)
  60. @GetMapping("/export")
  61. @ApiOperation("导出激活码列表Excel")
  62. public Response<String> export(@ModelAttribute QueryVipCodeListDTO queryVipCodeListDTO){
  63. List<VipCode> list = vipCodeService.selectVipCodeList(queryVipCodeListDTO);
  64. ExcelUtil<VipCode> util = new ExcelUtil<VipCode>(VipCode.class);
  65. return util.exportExcel(list, "code");
  66. }
  67. /**
  68. * 获取激活码详细信息
  69. */
  70. @PreAuthorize("@ss.hasPermi('vip:code:query')")
  71. @GetMapping(value = "/{id}")
  72. @ApiOperation("获取激活码详细信息")
  73. public Response<VipCode> getInfo(
  74. @ApiParam(name = "id", value = "激活码参数", required = true)
  75. @PathVariable("id") Long id
  76. ){
  77. return Response.success(vipCodeService.getById(id));
  78. }
  79. /**
  80. * 新增激活码
  81. */
  82. @PreAuthorize("@ss.hasPermi('vip:code:add')")
  83. @Log(title = "激活码", businessType = BusinessTypeEnum.INSERT)
  84. @PostMapping
  85. @ApiOperation("新增激活码")
  86. public Response<String> add(){
  87. VipCode vipCode = new VipCode();
  88. String rVipCode = null ;
  89. do {
  90. rVipCode = randomVipcode();
  91. }while (isRepeat(rVipCode));
  92. vipCode.setVipCode(rVipCode);
  93. vipCodeService.save(vipCode);
  94. return Response.success(rVipCode);
  95. }
  96. private boolean isRepeat(String rVipCode) {
  97. QueryWrapper<VipCode> wrapper = new QueryWrapper<VipCode>();
  98. wrapper.eq("vip_code",rVipCode);
  99. VipCode one = vipCodeService.getOne(wrapper);
  100. return one!= null;
  101. }
  102. private String randomVipcode() {
  103. String result = "";
  104. Random random = new Random();
  105. for(int i =0 ;i <11 ;i ++){
  106. result+=random.nextInt(10);
  107. }
  108. return result;
  109. }
  110. /**
  111. * 修改激活码
  112. */
  113. @PreAuthorize("@ss.hasPermi('vip:code:edit')")
  114. @Log(title = "激活码", businessType = BusinessTypeEnum.UPDATE)
  115. @PutMapping
  116. @ApiOperation("修改激活码")
  117. public Response<Integer> edit(@RequestBody VipCode vipCode){
  118. return toResponse(vipCodeService.updateById(vipCode) ? 1 : 0);
  119. }
  120. /**
  121. * 删除激活码
  122. */
  123. @PreAuthorize("@ss.hasPermi('vip:code:remove')")
  124. @Log(title = "激活码", businessType = BusinessTypeEnum.DELETE)
  125. @DeleteMapping("/{ids}")
  126. @ApiOperation("删除激活码")
  127. public Response<Integer> remove(
  128. @ApiParam(name = "ids", value = "激活码ids参数", required = true)
  129. @PathVariable Long[] ids
  130. ){
  131. return toResponse(vipCodeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
  132. }
  133. }