You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

181 lines
7.1 KiB

  1. package com.simple.controller;
  2. import com.simple.domain.po.WxCouponChannel;
  3. import com.simple.service.WxCouponChannelService;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.apache.log4j.Logger;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.GetMapping;
  8. import org.springframework.web.bind.annotation.ModelAttribute;
  9. import org.springframework.web.bind.annotation.PostMapping;
  10. import org.springframework.web.bind.annotation.RequestBody;
  11. import org.springframework.web.bind.annotation.RequestMapping;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import com.alibaba.fastjson.JSON;
  14. import com.github.pagehelper.PageInfo;
  15. import com.simple.common.Result;
  16. import com.simple.common.ResultData;
  17. import com.simple.domain.dto.WxCounponDto;
  18. import com.simple.domain.po.WxCoupon;
  19. import com.simple.domain.po.WxMerchant;
  20. import com.simple.service.WxCouponService;
  21. import com.simple.service.WxMerchantService;
  22. import io.swagger.annotations.Api;
  23. import io.swagger.annotations.ApiImplicitParam;
  24. import io.swagger.annotations.ApiImplicitParams;
  25. import io.swagger.annotations.ApiOperation;
  26. import java.util.ArrayList;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.Map;
  30. import java.util.stream.Collectors;
  31. @RestController
  32. @RequestMapping("wxCoupon")
  33. @Api(description="优惠券接口")
  34. public class WxCouponController extends BaseController
  35. {
  36. @Autowired
  37. private WxCouponService wxCouponService;
  38. @Autowired
  39. private WxMerchantService wxMerchantService;
  40. @Autowired
  41. private WxCouponChannelService wxCouponChannelService;
  42. private Logger logger = Logger.getLogger(WxCouponController.class);
  43. @ApiOperation("分页列表接口")
  44. @GetMapping("list")
  45. @ApiImplicitParams({
  46. @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
  47. @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
  48. public ResultData list(@ModelAttribute WxCoupon wxCoupon,Integer pageNum, Integer pageSize) {
  49. if (null == wxCoupon) wxCoupon = new WxCoupon();
  50. wxCoupon.setTenantId(getTenantId());
  51. PageInfo<WxCoupon> page = null;
  52. if (wxCoupon.getStatus() != null && wxCoupon.getStatus() == -1) {
  53. page = wxCouponService.findEnableList(wxCoupon, pageNum, pageSize);
  54. }else {
  55. page = wxCouponService.listAsPage(wxCoupon, pageNum, pageSize);
  56. }
  57. List<WxCoupon> wxCouponList = page.getList();
  58. if(wxCouponList.isEmpty()){
  59. return new ResultData(page);
  60. }
  61. List<Long> ids = wxCouponList.stream().map(p->p.getId()).collect(Collectors.toList());
  62. WxCouponChannel wxCouponChannel = new WxCouponChannel();
  63. wxCouponChannel.setTenantId(getTenantId());
  64. wxCouponChannel.setCouponIds(ids);
  65. wxCouponChannel.setStatus(0);
  66. //上架状态
  67. List<WxCouponChannel> list = wxCouponChannelService.listAsPage(wxCouponChannel,1,5).getList();
  68. if(!list.isEmpty()){
  69. Map<Long, List<WxCouponChannel>> groupBy = list.stream().collect(Collectors.groupingBy(WxCouponChannel::getCouponId));
  70. for (WxCoupon temp:wxCouponList) {
  71. if(groupBy.get(temp.getId())!=null){
  72. List<Integer> channels=new ArrayList<>();
  73. for (WxCouponChannel tempchannel:groupBy.get(temp.getId())) {
  74. if(!channels.contains(tempchannel.getTargetAd())) {
  75. channels.add(tempchannel.getTargetAd());
  76. }
  77. }
  78. String sss = JSON.toJSONString(channels);
  79. temp.setChannels(sss);
  80. }else{
  81. temp.setChannels("");
  82. }
  83. }
  84. }else{
  85. for (WxCoupon temp:wxCouponList) {
  86. temp.setChannels("");
  87. }
  88. }
  89. return new ResultData(page);
  90. }
  91. @ApiOperation("新增接口")
  92. @PostMapping("add")
  93. public ResultData add(@RequestBody WxCoupon wxCoupon) {
  94. //Assert.notNull(wxCoupon.getName(), "角色名不能为空");
  95. //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  96. if(StringUtils.isNotEmpty(wxCoupon.getSalePriceStr())){
  97. wxCoupon.setSalePrice((int)Double.parseDouble(wxCoupon.getSalePriceStr())*100);
  98. }
  99. if(StringUtils.isNotEmpty(wxCoupon.getUsePriceStr())){
  100. wxCoupon.setUsePrice((int)Double.parseDouble(wxCoupon.getUsePriceStr())*100);
  101. }
  102. if(StringUtils.isNotEmpty(wxCoupon.getPriceStr())){
  103. wxCoupon.setPrice((int)Double.parseDouble(wxCoupon.getPriceStr())*100);
  104. }
  105. if(StringUtils.isNotBlank(wxCoupon.getBusiness())) {
  106. String[] arys = wxCoupon.getBusiness().split(",");
  107. wxCoupon.setBusiness(JSON.toJSONString(arys));
  108. }
  109. wxCoupon.setTenantId(getUser().getTenantId());
  110. wxCoupon.setChannels("");
  111. Long id = wxCouponService.saveOrUpdate(wxCoupon);
  112. return new ResultData(id);
  113. }
  114. @ApiOperation("根据id更新接口")
  115. @PostMapping("update")
  116. public ResultData update(@RequestBody WxCoupon wxCoupon) {
  117. if(wxCoupon.getId()==null) {
  118. return new ResultData(ResultData.ERROR,"缺少id");
  119. }
  120. if(StringUtils.isNotBlank(wxCoupon.getBusiness())) {
  121. String[] arys = wxCoupon.getBusiness().split(",");
  122. wxCoupon.setBusiness(JSON.toJSONString(arys));
  123. }
  124. return wxCouponService.updateCoupon(wxCoupon);
  125. }
  126. @ApiOperation("根据id删除接口")
  127. @GetMapping("/del")
  128. @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
  129. public ResultData delete(Long id) {
  130. wxCouponService.deleteById(id);
  131. return new ResultData(Result.SUCCESS, "删除成功", null);
  132. }
  133. @ApiOperation("根据id查询接口")
  134. @GetMapping("/findById")
  135. @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
  136. public ResultData findById(Long id) {
  137. WxCoupon c = wxCouponService.getById(id);
  138. WxCounponDto dto = new WxCounponDto();
  139. org.springframework.beans.BeanUtils.copyProperties(c, dto);
  140. WxMerchant merchant = wxMerchantService.getById(c.getMerchantId());
  141. dto.setWxMerchant(merchant);
  142. return new ResultData(Result.SUCCESS,"查询成功",dto);
  143. }
  144. @ApiOperation("分页列表接口")
  145. @GetMapping("send/list")
  146. @ApiImplicitParams({
  147. @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
  148. @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
  149. public ResultData sendList(@ModelAttribute WxCoupon wxCoupon,Integer pageNum, Integer pageSize) {
  150. if (null == wxCoupon) wxCoupon = new WxCoupon();
  151. wxCoupon.setTenantId(getTenantId());
  152. wxCoupon.setStatus(0);
  153. PageInfo<WxCoupon> page = null;
  154. page = wxCouponService.findCanSendList(wxCoupon, pageNum, pageSize);
  155. return new ResultData(page);
  156. }
  157. }