|
- package com.simple.controller;
-
- import java.text.NumberFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
-
- import org.apache.commons.lang3.StringUtils;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import com.github.pagehelper.PageInfo;
- import com.simple.common.Result;
- import com.simple.common.ResultData;
- import com.simple.domain.dto.WxCuerBasicInfoDto;
- import com.simple.domain.po.WxCUser;
- import com.simple.domain.po.WxCUserBasicInfo;
- import com.simple.domain.po.WxCUserTags;
- import com.simple.domain.po.WxCoupon;
- import com.simple.domain.po.WxCouponOrder;
- import com.simple.domain.po.WxTags;
- import com.simple.domain.vo.UserStructureVo;
- import com.simple.enums.EnumAgeInfo;
- import com.simple.service.WxCUserBasicInfoService;
- import com.simple.service.WxCUserService;
- import com.simple.service.WxCUserTagsService;
- import com.simple.service.WxCouponOrderService;
- import com.simple.service.WxCouponService;
- import com.simple.service.WxTagsService;
-
- import io.swagger.annotations.Api;
- import io.swagger.annotations.ApiImplicitParam;
- import io.swagger.annotations.ApiImplicitParams;
- import io.swagger.annotations.ApiOperation;
-
- @RestController
- @RequestMapping("wxCUserBasicInfo")
- @Api(description="会员管理相关接口")
- public class WxCUserBasicInfoController extends BaseController
- {
- @Autowired
- private WxCUserBasicInfoService wxCUserBasicInfoService;
-
- @Autowired
- private WxCUserTagsService wxCUserTagsService;
-
- @Autowired
- private WxTagsService wxTagsService;
-
- @Autowired
- private WxCUserService wxCUserService;
-
- @Autowired
- private WxCouponOrderService wxCouponOrderService;
-
- @Autowired
- private WxCouponService wxCouponService;
-
- private Logger logger = Logger.getLogger(WxCUserBasicInfoController.class);
-
- @ApiOperation("分页列表接口")
- @GetMapping("list")
- @ApiImplicitParams({
- @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
- @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
- public ResultData list(@ModelAttribute WxCuerBasicInfoDto wxCUserBasicInfo,Integer pageNum, Integer pageSize) {
- if (null == wxCUserBasicInfo) wxCUserBasicInfo = new WxCuerBasicInfoDto();
- String tenantId = getTenantId();
- wxCUserBasicInfo.setTenantId(tenantId);
- PageInfo<WxCUserBasicInfo> page = wxCUserBasicInfoService.list(wxCUserBasicInfo, pageNum, pageSize);
- if(page.getSize()==0 && StringUtils.isNotBlank(wxCUserBasicInfo.getPhone())
- && wxCUserBasicInfo.getEndTime()==null && wxCUserBasicInfo.getStartTime()==null
- && StringUtils.isBlank(wxCUserBasicInfo.getName())
- ) {//当只有手机号查询并且查不到数据 ,新增
- WxCUser cUser = new WxCUser();
- cUser.setTenantId(tenantId);
- cUser.setPhone(wxCUserBasicInfo.getPhone());
- PageInfo<WxCUser> cUsers = wxCUserService.listAsPage(cUser, 1, 1);
- if(cUsers.getSize()>0) {
- createUserBasicInfo(cUsers.getList().get(0));
- page = wxCUserBasicInfoService.list(wxCUserBasicInfo, pageNum, pageSize);
- }
- }
- return new ResultData(page);
- }
-
-
- private void createUserBasicInfo(WxCUser wxCUser) {
- WxCUserBasicInfo wxCUserBasicInfo =new WxCUserBasicInfo();
- wxCUserBasicInfo.setCUserId(wxCUser.getId());
- wxCUserBasicInfo.setPhone(wxCUser.getPhone());
- wxCUserBasicInfo.setTenantId(wxCUser.getTenantId());
- wxCUserBasicInfo.setNickName(wxCUser.getNickName());
- wxCUserBasicInfoService.saveOrUpdate(wxCUserBasicInfo);
-
- }
-
- // @ApiOperation("新增接口")
- // @PostMapping("add")
- // public ResultData add(@RequestBody WxCUserBasicInfo wxCUserBasicInfo) {
- // //Assert.notNull(wxCUserBasicInfo.getName(), "角色名不能为空");
- // //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
- // wxCUserBasicInfoService.saveOrUpdate(wxCUserBasicInfo);
- // return new ResultData();
- // }
-
- @ApiOperation("根据id更新接口")
- @PostMapping("update")
- public ResultData update(@RequestBody WxCUserBasicInfo wxCUserBasicInfo) {
- WxCUserBasicInfo info = wxCUserBasicInfoService.getById(wxCUserBasicInfo.getId());
- wxCUserBasicInfo.setTenantId(getTenantId());
- if(StringUtils.isNotBlank(wxCUserBasicInfo.getTagIds())) {
- WxCUserTags record =new WxCUserTags();
- record.setUserId(info.getCUserId());
- record.setTenantId(getTenantId());
- PageInfo<WxCUserTags> page = wxCUserTagsService.listAsPage(record, 1, 1);
- if(page.getSize()>0) {
- WxCUserTags t = page.getList().get(0);
- record.setId(t.getId());
- }
- String tags = wxCUserBasicInfo.getTagIds();
- List<Long> tagIdList = new ArrayList<>();
- for(String t:tags.split(",")) {
- tagIdList.add(Long.valueOf(t));
- }
- record.setTags(JSON.toJSONString(tagIdList));
- wxCUserTagsService.saveOrUpdate(record);
- wxCUserBasicInfo.setTagId(record.getId());
- }
- wxCUserBasicInfoService.saveOrUpdate(wxCUserBasicInfo);
- return new ResultData();
- }
-
- @ApiOperation("根据id删除接口")
- @GetMapping("/del")
- @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
- public ResultData delete(Long id) {
- wxCUserBasicInfoService.deleteById(id);
- return new ResultData(Result.SUCCESS, "删除成功", null);
- }
-
- @ApiOperation("根据id查询接口")
- @GetMapping("/findById")
- @ApiImplicitParam(name="id",value="id",dataType="Long", paramType = "query",required=true)
- public ResultData findById(Long id) {
- WxCUserBasicInfo info = wxCUserBasicInfoService.getById(id);
- if(info.getTagId()!=null) {
- WxCUserTags uTag = wxCUserTagsService.getById(info.getTagId());
- if(StringUtils.isNotBlank(uTag.getTags())) {
- List<Long> ids = JSONObject.parseArray(uTag.getTags(),Long.class);
- WxTags wxTags =new WxTags();
- wxTags.setIds(ids);
- PageInfo<WxTags> page = wxTagsService.listAsPage(wxTags, 1, 5000);
- String tagNames="";
- String tagIds="";
- List<Long> tagIdList = new ArrayList<>();
- for(WxTags wt:page.getList()) {
- tagNames+=wt.getName()+"/";
- tagIds+=wt.getId()+",";
- tagIdList.add(wt.getId());
- }
- if(StringUtils.isNotBlank(tagNames)) {
- info.setTagNames(tagNames.substring(0,tagNames.length()-1));
- }
- if(StringUtils.isNoneBlank(tagIds)) {
- info.setTagIds(tagIds.substring(0,tagIds.length()-1));
- }
- long count = wxCUserTagsService.findCountByTag(tagIdList);
- info.setCount(count);
- }
- }
- return new ResultData(Result.SUCCESS,"查询成功",info);
- }
-
- @ApiOperation("根据userId查询交易记录接口")
- @GetMapping("/findOrderCouponByUserId")
- @ApiImplicitParam(name="userId",value="userId",dataType="Long", paramType = "query",required=true)
- public ResultData findOrderCouponByUserId(Long userId,Integer pageNum, Integer pageSize) {
- WxCouponOrder corder = new WxCouponOrder();
- corder.setCUserId(userId);
- corder.setTenantId(getTenantId());
- PageInfo<WxCouponOrder> page = wxCouponOrderService.listAsPage(corder, pageNum, pageSize);
- if(page.getSize()>0) {
- List<WxCouponOrder> list = page.getList();
- for(WxCouponOrder c:list) {
- WxCoupon coupon = wxCouponService.getById(c.getCouponId());
- c.setCouponName(coupon.getTitle());
- c.setSalePrice(coupon.getPrice());
- }
- }
- return new ResultData(Result.SUCCESS,"查询成功",page);
- }
-
- @ApiOperation("查询会员性别结构")
- @GetMapping("/findUserSexStructure")
- public ResultData findUserSexStructure(
- Date startTime,Date endTime
- ) {
- WxCuerBasicInfoDto dto = new WxCuerBasicInfoDto();
- dto.setStartTime(startTime);
- dto.setEndTime(endTime);
- //保密
- dto.setSex(0);
- long secrecy = getCount(dto);
- dto.setSex(1);
- long boy = getCount(dto);
- dto.setSex(2);
- long girl=getCount(dto);
- Long all =secrecy+boy+girl;
- List<UserStructureVo> vos = new ArrayList<>();
- vos.add(getVo(boy, all, "男"));
- vos.add(getVo(girl, all, "女"));
- vos.add(getVo(secrecy, all, "保密"));
- return new ResultData(vos);
- }
-
- @ApiOperation("查询会员年龄结构")
- @GetMapping("/findUserAgeStructure")
- public ResultData findUserAgeStructure() {
-
- long all =wxCUserBasicInfoService.findCountByAge(new WxCuerBasicInfoDto());
- List<UserStructureVo> vos = new ArrayList<>();
- Calendar c = Calendar.getInstance();
- for(EnumAgeInfo a:EnumAgeInfo.values()) {
- c.clear();
- c.setTime(new Date());
- c.set(Calendar.HOUR_OF_DAY, 0);
- c.set(Calendar.MINUTE,0);
- c.set(Calendar.SECOND,0);
- long count = getCountByAge(a, c);
- vos.add(getVo(count, all, a.getDesc()));
- }
-
-
- return new ResultData();
- }
-
-
- private long getCountByAge(EnumAgeInfo a,Calendar c) {
- WxCuerBasicInfoDto dto =new WxCuerBasicInfoDto();
- c.add(Calendar.YEAR, -a.getEnd());
- Date startTime = c.getTime();
- c.clear();
- c.setTime(startTime);
- c.add(Calendar.YEAR,a.getEnd()-a.getStart());
- Date endTime = c.getTime();
- dto.setStartTime(startTime);
- dto.setEndTime(endTime);
- return wxCUserBasicInfoService.findCountByAge(dto);
- }
-
-
- //通过性别获取数量
- private long getCount(WxCuerBasicInfoDto dto) {
- //TODO 考虑性能问题,暂不处理少量重复问题,后续可考虑大数据处理
- return wxCUserBasicInfoService.findCountBySex(dto)+
- wxCUserService.findCountBySex(dto);
- }
-
- private UserStructureVo getVo(long count,long all,String name) {
- UserStructureVo vo = new UserStructureVo();
- vo.setName(name);
- vo.setCount(count+"");
- NumberFormat nf = NumberFormat.getPercentInstance();
- nf.setMinimumFractionDigits(2);//控制保留小数点后几位,2:表示保留2位小数点
- if(all>0) {
- vo.setPercentage(nf.format(new Long(count).doubleValue()/new Long(all).doubleValue()));
- }else {
- vo.setPercentage("0.00%");
- }
- return vo;
- }
-
- }
|