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.
 
 
 
 
 

284 lines
11 KiB

  1. package com.simple.controller;
  2. import java.text.NumberFormat;
  3. import java.util.ArrayList;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. import java.util.List;
  7. import org.apache.commons.lang3.StringUtils;
  8. import org.apache.log4j.Logger;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.ModelAttribute;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestBody;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RestController;
  16. import com.alibaba.fastjson.JSON;
  17. import com.alibaba.fastjson.JSONObject;
  18. import com.github.pagehelper.PageInfo;
  19. import com.simple.common.Result;
  20. import com.simple.common.ResultData;
  21. import com.simple.domain.dto.WxCuerBasicInfoDto;
  22. import com.simple.domain.po.WxCUser;
  23. import com.simple.domain.po.WxCUserBasicInfo;
  24. import com.simple.domain.po.WxCUserTags;
  25. import com.simple.domain.po.WxCoupon;
  26. import com.simple.domain.po.WxCouponOrder;
  27. import com.simple.domain.po.WxTags;
  28. import com.simple.domain.vo.UserStructureVo;
  29. import com.simple.enums.EnumAgeInfo;
  30. import com.simple.service.WxCUserBasicInfoService;
  31. import com.simple.service.WxCUserService;
  32. import com.simple.service.WxCUserTagsService;
  33. import com.simple.service.WxCouponOrderService;
  34. import com.simple.service.WxCouponService;
  35. import com.simple.service.WxTagsService;
  36. import io.swagger.annotations.Api;
  37. import io.swagger.annotations.ApiImplicitParam;
  38. import io.swagger.annotations.ApiImplicitParams;
  39. import io.swagger.annotations.ApiOperation;
  40. @RestController
  41. @RequestMapping("wxCUserBasicInfo")
  42. @Api(description="会员管理相关接口")
  43. public class WxCUserBasicInfoController extends BaseController
  44. {
  45. @Autowired
  46. private WxCUserBasicInfoService wxCUserBasicInfoService;
  47. @Autowired
  48. private WxCUserTagsService wxCUserTagsService;
  49. @Autowired
  50. private WxTagsService wxTagsService;
  51. @Autowired
  52. private WxCUserService wxCUserService;
  53. @Autowired
  54. private WxCouponOrderService wxCouponOrderService;
  55. @Autowired
  56. private WxCouponService wxCouponService;
  57. private Logger logger = Logger.getLogger(WxCUserBasicInfoController.class);
  58. @ApiOperation("分页列表接口")
  59. @GetMapping("list")
  60. @ApiImplicitParams({
  61. @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
  62. @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
  63. public ResultData list(@ModelAttribute WxCuerBasicInfoDto wxCUserBasicInfo,Integer pageNum, Integer pageSize) {
  64. if (null == wxCUserBasicInfo) wxCUserBasicInfo = new WxCuerBasicInfoDto();
  65. String tenantId = getTenantId();
  66. wxCUserBasicInfo.setTenantId(tenantId);
  67. PageInfo<WxCUserBasicInfo> page = wxCUserBasicInfoService.list(wxCUserBasicInfo, pageNum, pageSize);
  68. if(page.getSize()==0 && StringUtils.isNotBlank(wxCUserBasicInfo.getPhone())
  69. && wxCUserBasicInfo.getEndTime()==null && wxCUserBasicInfo.getStartTime()==null
  70. && StringUtils.isBlank(wxCUserBasicInfo.getName())
  71. ) {//当只有手机号查询并且查不到数据 ,新增
  72. WxCUser cUser = new WxCUser();
  73. cUser.setTenantId(tenantId);
  74. cUser.setPhone(wxCUserBasicInfo.getPhone());
  75. PageInfo<WxCUser> cUsers = wxCUserService.listAsPage(cUser, 1, 1);
  76. if(cUsers.getSize()>0) {
  77. createUserBasicInfo(cUsers.getList().get(0));
  78. page = wxCUserBasicInfoService.list(wxCUserBasicInfo, pageNum, pageSize);
  79. }
  80. }
  81. return new ResultData(page);
  82. }
  83. private void createUserBasicInfo(WxCUser wxCUser) {
  84. WxCUserBasicInfo wxCUserBasicInfo =new WxCUserBasicInfo();
  85. wxCUserBasicInfo.setCUserId(wxCUser.getId());
  86. wxCUserBasicInfo.setPhone(wxCUser.getPhone());
  87. wxCUserBasicInfo.setTenantId(wxCUser.getTenantId());
  88. wxCUserBasicInfo.setNickName(wxCUser.getNickName());
  89. wxCUserBasicInfoService.saveOrUpdate(wxCUserBasicInfo);
  90. }
  91. // @ApiOperation("新增接口")
  92. // @PostMapping("add")
  93. // public ResultData add(@RequestBody WxCUserBasicInfo wxCUserBasicInfo) {
  94. // //Assert.notNull(wxCUserBasicInfo.getName(), "角色名不能为空");
  95. // //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
  96. // wxCUserBasicInfoService.saveOrUpdate(wxCUserBasicInfo);
  97. // return new ResultData();
  98. // }
  99. @ApiOperation("根据id更新接口")
  100. @PostMapping("update")
  101. public ResultData update(@RequestBody WxCUserBasicInfo wxCUserBasicInfo) {
  102. WxCUserBasicInfo info = wxCUserBasicInfoService.getById(wxCUserBasicInfo.getId());
  103. wxCUserBasicInfo.setTenantId(getTenantId());
  104. if(StringUtils.isNotBlank(wxCUserBasicInfo.getTagIds())) {
  105. WxCUserTags record =new WxCUserTags();
  106. record.setUserId(info.getCUserId());
  107. record.setTenantId(getTenantId());
  108. PageInfo<WxCUserTags> page = wxCUserTagsService.listAsPage(record, 1, 1);
  109. if(page.getSize()>0) {
  110. WxCUserTags t = page.getList().get(0);
  111. record.setId(t.getId());
  112. }
  113. String tags = wxCUserBasicInfo.getTagIds();
  114. List<Long> tagIdList = new ArrayList<>();
  115. for(String t:tags.split(",")) {
  116. tagIdList.add(Long.valueOf(t));
  117. }
  118. record.setTags(JSON.toJSONString(tagIdList));
  119. wxCUserTagsService.saveOrUpdate(record);
  120. wxCUserBasicInfo.setTagId(record.getId());
  121. }
  122. wxCUserBasicInfoService.saveOrUpdate(wxCUserBasicInfo);
  123. return new ResultData();
  124. }
  125. @ApiOperation("根据id删除接口")
  126. @GetMapping("/del")
  127. @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
  128. public ResultData delete(Long id) {
  129. wxCUserBasicInfoService.deleteById(id);
  130. return new ResultData(Result.SUCCESS, "删除成功", null);
  131. }
  132. @ApiOperation("根据id查询接口")
  133. @GetMapping("/findById")
  134. @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
  135. public ResultData findById(Long id) {
  136. WxCUserBasicInfo info = wxCUserBasicInfoService.getById(id);
  137. if(info.getTagId()!=null) {
  138. WxCUserTags uTag = wxCUserTagsService.getById(info.getTagId());
  139. if(StringUtils.isNotBlank(uTag.getTags())) {
  140. List<Long> ids = JSONObject.parseArray(uTag.getTags(),Long.class);
  141. WxTags wxTags =new WxTags();
  142. wxTags.setIds(ids);
  143. PageInfo<WxTags> page = wxTagsService.listAsPage(wxTags, 1, 5000);
  144. String tagNames="";
  145. String tagIds="";
  146. List<Long> tagIdList = new ArrayList<>();
  147. for(WxTags wt:page.getList()) {
  148. tagNames+=wt.getName()+"/";
  149. tagIds+=wt.getId()+",";
  150. tagIdList.add(wt.getId());
  151. }
  152. if(StringUtils.isNotBlank(tagNames)) {
  153. info.setTagNames(tagNames.substring(0,tagNames.length()-1));
  154. }
  155. if(StringUtils.isNoneBlank(tagIds)) {
  156. info.setTagIds(tagIds.substring(0,tagIds.length()-1));
  157. }
  158. long count = wxCUserTagsService.findCountByTag(tagIdList);
  159. info.setCount(count);
  160. }
  161. }
  162. return new ResultData(Result.SUCCESS,"查询成功",info);
  163. }
  164. @ApiOperation("根据userId查询交易记录接口")
  165. @GetMapping("/findOrderCouponByUserId")
  166. @ApiImplicitParam(name="userId",value="userId",dataType="Long", paramType = "query",required=true)
  167. public ResultData findOrderCouponByUserId(Long userId,Integer pageNum, Integer pageSize) {
  168. WxCouponOrder corder = new WxCouponOrder();
  169. corder.setCUserId(userId);
  170. corder.setTenantId(getTenantId());
  171. PageInfo<WxCouponOrder> page = wxCouponOrderService.listAsPage(corder, pageNum, pageSize);
  172. if(page.getSize()>0) {
  173. List<WxCouponOrder> list = page.getList();
  174. for(WxCouponOrder c:list) {
  175. WxCoupon coupon = wxCouponService.getById(c.getCouponId());
  176. c.setCouponName(coupon.getTitle());
  177. c.setSalePrice(coupon.getPrice());
  178. }
  179. }
  180. return new ResultData(Result.SUCCESS,"查询成功",page);
  181. }
  182. @ApiOperation("查询会员性别结构")
  183. @GetMapping("/findUserSexStructure")
  184. public ResultData findUserSexStructure(
  185. Date startTime,Date endTime
  186. ) {
  187. WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
  188. dto.setStartTime(startTime);
  189. dto.setEndTime(endTime);
  190. //保密
  191. dto.setSex(0);
  192. long secrecy = getCount(dto);
  193. dto.setSex(1);
  194. long boy = getCount(dto);
  195. dto.setSex(2);
  196. long girl=getCount(dto);
  197. Long all =secrecy+boy+girl;
  198. List<UserStructureVo> vos = new ArrayList<>();
  199. vos.add(getVo(boy, all, "男"));
  200. vos.add(getVo(girl, all, "女"));
  201. vos.add(getVo(secrecy, all, "保密"));
  202. return new ResultData(vos);
  203. }
  204. @ApiOperation("查询会员年龄结构")
  205. @GetMapping("/findUserAgeStructure")
  206. public ResultData findUserAgeStructure() {
  207. long all =wxCUserBasicInfoService.findCountByAge(new WxCuerBasicInfoDto());
  208. List<UserStructureVo> vos = new ArrayList<>();
  209. Calendar c = Calendar.getInstance();
  210. for(EnumAgeInfo a:EnumAgeInfo.values()) {
  211. c.clear();
  212. c.setTime(new Date());
  213. c.set(Calendar.HOUR_OF_DAY, 0);
  214. c.set(Calendar.MINUTE,0);
  215. c.set(Calendar.SECOND,0);
  216. long count = getCountByAge(a, c);
  217. vos.add(getVo(count, all, a.getDesc()));
  218. }
  219. return new ResultData();
  220. }
  221. private long getCountByAge(EnumAgeInfo a,Calendar c) {
  222. WxCuerBasicInfoDto dto =new WxCuerBasicInfoDto();
  223. c.add(Calendar.YEAR, -a.getEnd());
  224. Date startTime = c.getTime();
  225. c.clear();
  226. c.setTime(startTime);
  227. c.add(Calendar.YEAR,a.getEnd()-a.getStart());
  228. Date endTime = c.getTime();
  229. dto.setStartTime(startTime);
  230. dto.setEndTime(endTime);
  231. return wxCUserBasicInfoService.findCountByAge(dto);
  232. }
  233. //通过性别获取数量
  234. private long getCount(WxCuerBasicInfoDto dto) {
  235. //TODO 考虑性能问题,暂不处理少量重复问题,后续可考虑大数据处理
  236. return wxCUserBasicInfoService.findCountBySex(dto)+
  237. wxCUserService.findCountBySex(dto);
  238. }
  239. private UserStructureVo getVo(long count,long all,String name) {
  240. UserStructureVo vo = new UserStructureVo();
  241. vo.setName(name);
  242. vo.setCount(count+"");
  243. NumberFormat nf = NumberFormat.getPercentInstance();
  244. nf.setMinimumFractionDigits(2);//控制保留小数点后几位,2:表示保留2位小数点
  245. if(all>0) {
  246. vo.setPercentage(nf.format(new Long(count).doubleValue()/new Long(all).doubleValue()));
  247. }else {
  248. vo.setPercentage("0.00%");
  249. }
  250. return vo;
  251. }
  252. }