|  |  |  | 
|---|
|  |  |  | package com.matrix.system.shopXcx.api.action; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import cn.hutool.core.bean.BeanUtil; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.matrix.biz.bean.BizUser; | 
|---|
|  |  |  | import com.matrix.biz.service.BizUserService; | 
|---|
|  |  |  | import com.matrix.component.redis.RedisUserLoginUtils; | 
|---|
|  |  |  | import com.matrix.component.tools.HttpCurlUtil; | 
|---|
|  |  |  | import com.matrix.component.wechat.externalInterface.weixinUtil.WeixinServiceUtil; | 
|---|
|  |  |  | import com.matrix.core.pojo.AjaxResult; | 
|---|
|  |  |  | import com.matrix.core.tools.LogUtil; | 
|---|
|  |  |  | import com.matrix.core.tools.StringUtils; | 
|---|
|  |  |  | import com.matrix.core.web.BaseAction; | 
|---|
|  |  |  | import com.matrix.system.activity.dao.ActivitySignReceiveRecordDao; | 
|---|
|  |  |  | import com.matrix.system.common.bean.BusParameterSettings; | 
|---|
|  |  |  | import com.matrix.system.common.constance.AppConstance; | 
|---|
|  |  |  | import com.matrix.system.common.dao.BusParameterSettingsDao; | 
|---|
|  |  |  | import com.matrix.system.common.interceptor.HostInterceptor; | 
|---|
|  |  |  | import com.matrix.system.constance.Dictionary; | 
|---|
|  |  |  | import com.matrix.system.fenxiao.constant.FenxiaoSettingConstant; | 
|---|
|  |  |  | import com.matrix.system.fenxiao.dao.ShopSalesmanApplyDao; | 
|---|
|  |  |  | import com.matrix.system.fenxiao.entity.ShopSalesmanApply; | 
|---|
|  |  |  | import com.matrix.system.fenxiao.service.ShopSalesmanApplyService; | 
|---|
|  |  |  | import com.matrix.system.hive.bean.SysVipInfo; | 
|---|
|  |  |  | import com.matrix.system.hive.dao.MoneyCardUseDao; | 
|---|
|  |  |  | import com.matrix.system.hive.dao.SysVipInfoDao; | 
|---|
|  |  |  | import com.matrix.system.hive.service.SysVipInfoService; | 
|---|
|  |  |  | import com.matrix.system.score.dao.ScoreVipDetailDao; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.api.WeChatApiTools; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.api.dto.XcxUserSaveUserInfoDto; | 
|---|
|  |  |  | import com.matrix.system.shopXcx.dao.ShopCouponRecordDao; | 
|---|
|  |  |  | import net.sf.json.JSONObject; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Value; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.Collections; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @author jyy | 
|---|
|  |  |  | 
|---|
|  |  |  | public class WxUserAction extends BaseAction { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private BizUserService bizUserService; | 
|---|
|  |  |  | private SysVipInfoService sysVipInfoService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private SysVipInfoDao sysVipInfoDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ShopSalesmanApplyService shopSalesmanApplyService; | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | WeChatApiTools weChatApiTools; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ScoreVipDetailDao scoreVipDetailDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private WeixinServiceUtil weixinServiceUtil; | 
|---|
|  |  |  | private MoneyCardUseDao moneyCardUseDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | ShopCouponRecordDao shopCouponRecordDao; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | ActivitySignReceiveRecordDao activitySignReceiveRecordDao; | 
|---|
|  |  |  | @Value("${debug}") | 
|---|
|  |  |  | private String isDebug; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | String sessionKey = json.getString("session_key"); | 
|---|
|  |  |  | LogUtil.debug("openId={},sessionKey={}", openId, sessionKey); | 
|---|
|  |  |  | // 查询用户是否存在 | 
|---|
|  |  |  | BizUser bizUser = null; | 
|---|
|  |  |  | SysVipInfo sysVipInfo = null; | 
|---|
|  |  |  | synchronized (this) { | 
|---|
|  |  |  | bizUser = bizUserService.findByOpenId(openId); | 
|---|
|  |  |  | if (bizUser == null) { | 
|---|
|  |  |  | sysVipInfo = sysVipInfoService.findByOpenId(openId); | 
|---|
|  |  |  | if (sysVipInfo == null) { | 
|---|
|  |  |  | // 新增用户 | 
|---|
|  |  |  | bizUser = new BizUser(); | 
|---|
|  |  |  | bizUser.setSessionKey(sessionKey); | 
|---|
|  |  |  | bizUser.setIsSales(BizUser.NOT_SALES); | 
|---|
|  |  |  | bizUser.setOpenId(openId); | 
|---|
|  |  |  | bizUser.setLastLoginTime(new Date()); | 
|---|
|  |  |  | bizUser.setUserType(AppConstance.USER_TYPE_CUSTOMER); | 
|---|
|  |  |  | bizUser.setCompanyId(HostInterceptor.getCompanyId()); | 
|---|
|  |  |  | bizUserService.add(bizUser); | 
|---|
|  |  |  | sysVipInfo = new SysVipInfo(); | 
|---|
|  |  |  | sysVipInfo.setIsSales(SysVipInfo.NOT_SALES); | 
|---|
|  |  |  | sysVipInfo.setOpenId(openId); | 
|---|
|  |  |  | sysVipInfo.setSessionKey(sessionKey); | 
|---|
|  |  |  | sysVipInfo.setCompanyId(HostInterceptor.getCompanyId()); | 
|---|
|  |  |  | sysVipInfo.setArrivalWay("微商城"); | 
|---|
|  |  |  | sysVipInfo.setVipName("微信用户"); | 
|---|
|  |  |  | sysVipInfo.setVipState(Dictionary.VIP_STATE_HY); | 
|---|
|  |  |  | sysVipInfo.setVipType(Dictionary.VIP_TYPE_NOCARD); | 
|---|
|  |  |  | sysVipInfo.setIsDeal(SysVipInfo.UNDEAL_VIP); | 
|---|
|  |  |  | sysVipInfo.setVipNo(openId); | 
|---|
|  |  |  | sysVipInfoService.add(sysVipInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | bizUser.setSessionKey(sessionKey); | 
|---|
|  |  |  | bizUser.setLastLoginTime(new Date()); | 
|---|
|  |  |  | // 更新用户sessionKey | 
|---|
|  |  |  | bizUserService.modifyByModel(bizUser); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | sysVipInfo.setSessionKey(sessionKey); | 
|---|
|  |  |  | sysVipInfoDao.update(sysVipInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 存放redis | 
|---|
|  |  |  | String token = redisUserLoginUtils.saveUserInfo(bizUser); | 
|---|
|  |  |  | sysVipInfo.setLevelCard(null); | 
|---|
|  |  |  | String token = redisUserLoginUtils.saveUserInfo(sysVipInfo); | 
|---|
|  |  |  | LogUtil.info("用户token={}", token); | 
|---|
|  |  |  | res.putInMap("token", token); | 
|---|
|  |  |  | res.putInMap("userInfo", bizUser); | 
|---|
|  |  |  | res.putInMap("userInfo", sysVipInfo); | 
|---|
|  |  |  | res.setStatus(AjaxResult.STATUS_SUCCESS); | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | res.setStatus(AjaxResult.STATUS_FAIL); | 
|---|
|  |  |  | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 查询用户信息 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @RequestMapping(value = "/findUserInfo") | 
|---|
|  |  |  | public @ResponseBody | 
|---|
|  |  |  | AjaxResult findUserInfo() { | 
|---|
|  |  |  | BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class); | 
|---|
|  |  |  | String openId = loginUser.getOpenId(); | 
|---|
|  |  |  | BizUser bizUser = bizUserService.findByOpenId(openId); | 
|---|
|  |  |  | SysVipInfo loginUser = redisUserLoginUtils.getLoginUser(SysVipInfo.class); | 
|---|
|  |  |  | SysVipInfo sysVipInfo = sysVipInfoService.findById(loginUser.getId()); | 
|---|
|  |  |  | sysVipInfo.setPointAll(scoreVipDetailDao.selectUserTotalScore(loginUser.getId())); | 
|---|
|  |  |  | sysVipInfo.setBalance(moneyCardUseDao.selectVipCardTotalMoney(loginUser.getId())); | 
|---|
|  |  |  | AjaxResult res = new AjaxResult(); | 
|---|
|  |  |  | res.putInMap("userInfo", bizUser); | 
|---|
|  |  |  | res.putInMap("couponCount", shopCouponRecordDao.countUnuseCouponRecord(loginUser.getId())); | 
|---|
|  |  |  | res.putInMap("prizeCount",activitySignReceiveRecordDao.getSignAwardReceiveCount(loginUser.getId(),loginUser.getCompanyId())); | 
|---|
|  |  |  | res.putInMap("userInfo", sysVipInfo); | 
|---|
|  |  |  | res.setStatus(AjaxResult.STATUS_SUCCESS); | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | @GetMapping(value = "/findUserInfo/{openId}") | 
|---|
|  |  |  | public @ResponseBody | 
|---|
|  |  |  | AjaxResult findUserInfo(@PathVariable String openId) { | 
|---|
|  |  |  | BizUser bizUser = bizUserService.findByOpenId(openId); | 
|---|
|  |  |  | BizUser bizUserQuery=new BizUser(); | 
|---|
|  |  |  | bizUserQuery.setNickName(bizUser.getNickName()); | 
|---|
|  |  |  | bizUserQuery.setAvatarUrl(bizUser.getAvatarUrl()); | 
|---|
|  |  |  | SysVipInfo sysVipInfo = sysVipInfoService.findByOpenId(openId); | 
|---|
|  |  |  | SysVipInfo bizUserQuery=new SysVipInfo(); | 
|---|
|  |  |  | bizUserQuery.setNickName(sysVipInfo.getNickName()); | 
|---|
|  |  |  | bizUserQuery.setAvatarUrl(sysVipInfo.getAvatarUrl()); | 
|---|
|  |  |  | bizUserQuery.setOpenId(openId); | 
|---|
|  |  |  | return AjaxResult.buildSuccessInstance(bizUserQuery); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 接收用户信息 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @RequestMapping(value = "/saveUserInfo") | 
|---|
|  |  |  | @PostMapping(value = "/saveUserInfo") | 
|---|
|  |  |  | public @ResponseBody | 
|---|
|  |  |  | AjaxResult saveUserInfo(@RequestBody BizUser bizUser) { | 
|---|
|  |  |  | AjaxResult saveUserInfo(@RequestBody XcxUserSaveUserInfoDto xcxUserSaveUserInfoDto) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BizUser loginUser = redisUserLoginUtils.getLoginUser(BizUser.class); | 
|---|
|  |  |  | bizUser.setUserId(loginUser.getUserId()); | 
|---|
|  |  |  | // TODO 赋值操作 | 
|---|
|  |  |  | int i = bizUserService.saveUserInfo(bizUser); | 
|---|
|  |  |  | SysVipInfo loginUser = redisUserLoginUtils.getLoginUser(SysVipInfo.class); | 
|---|
|  |  |  | SysVipInfo sysVipInfo=new SysVipInfo(); | 
|---|
|  |  |  | sysVipInfo.setId(loginUser.getId()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | BeanUtil.copyProperties(xcxUserSaveUserInfoDto,sysVipInfo); | 
|---|
|  |  |  | sysVipInfo.setPhone(xcxUserSaveUserInfoDto.getPhoneNumber()); | 
|---|
|  |  |  | if(StringUtils.isBlank(loginUser.getVipName())||loginUser.getVipName().equals("微信用户")){ | 
|---|
|  |  |  | sysVipInfo.setVipName(xcxUserSaveUserInfoDto.getNickName()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(StringUtils.isBlank(loginUser.getSex())){ | 
|---|
|  |  |  | sysVipInfo.setSex(xcxUserSaveUserInfoDto.getGender()==1?"男":"女"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int i = sysVipInfoDao.update(sysVipInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | checkNeedToBeSalesman(sysVipInfo, loginUser); | 
|---|
|  |  |  | if (i == 0) { | 
|---|
|  |  |  | return new AjaxResult(AjaxResult.STATUS_FAIL, "更新失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(sysVipInfo)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | void checkNeedToBeSalesman(SysVipInfo sysVipInfo, SysVipInfo loginUser) { | 
|---|
|  |  |  | //查询是否需要自动成为分销员 | 
|---|
|  |  |  | if ((loginUser.getIsSales() == null || loginUser.getIsSales() == 2) | 
|---|
|  |  |  | && StringUtils.isNotBlank(bizUser.getNickName())) {//目前还不是分销员 | 
|---|
|  |  |  | && StringUtils.isNotBlank(sysVipInfo.getNickName())) {//目前还不是分销员 | 
|---|
|  |  |  | BusParameterSettings fxSwith = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_SWITCH, HostInterceptor.getCompanyId()); | 
|---|
|  |  |  | if (fxSwith.getParamValue().equals("1")) {//开启分销 | 
|---|
|  |  |  | BusParameterSettings applyWay = busParameterSettingsDao.selectCompanyParamByCode(FenxiaoSettingConstant.FX_APPLY_WAY, loginUser.getCompanyId()); | 
|---|
|  |  |  | if (applyWay.getParamValue().equals(FenxiaoSettingConstant.FX_APPLY_WAY_AUTO)) {//自动成为分销员 | 
|---|
|  |  |  | QueryWrapper<ShopSalesmanApply> queryWrapper = new QueryWrapper<>(); | 
|---|
|  |  |  | queryWrapper.eq("user_id", loginUser.getOpenId()); | 
|---|
|  |  |  | queryWrapper.eq("user_id", loginUser.getId()); | 
|---|
|  |  |  | queryWrapper.eq("apply_status", ShopSalesmanApply.APPLY_STATUS_DSH); | 
|---|
|  |  |  | ShopSalesmanApply shopSalesmanApply = salesmanApplyDao.selectOne(queryWrapper); | 
|---|
|  |  |  | if (shopSalesmanApply == null) {//没有待审核记录 | 
|---|
|  |  |  | shopSalesmanApplyService.applyToBeAnSalesman(loginUser.getOpenId(), null, null, 2); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | shopSalesmanApplyService.applyToBeAnSalesman(loginUser.getId(), null, null, 2); | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | LogUtil.debug(e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (i == 0) { | 
|---|
|  |  |  | return new AjaxResult(AjaxResult.STATUS_FAIL, "更新失败"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return new AjaxResult(AjaxResult.STATUS_SUCCESS, Collections.singletonList(bizUser)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|