From adf7566f482080d9fff9dd4429f308433a0e2c2f Mon Sep 17 00:00:00 2001 From: 101captain <237651143@qq.com> Date: 星期五, 07 一月 2022 16:20:22 +0800 Subject: [PATCH] 商业街代码提交 --- springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsMerchantServiceImpl.java | 168 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 157 insertions(+), 11 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsMerchantServiceImpl.java b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsMerchantServiceImpl.java index 4594b34..318e000 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsMerchantServiceImpl.java +++ b/springcloud_k8s_panzhihuazhihuishequ/service_community/src/main/java/com/panzhihua/service_community/service/impl/McsMerchantServiceImpl.java @@ -2,20 +2,25 @@ import static java.util.Objects.isNull; import static java.util.Objects.nonNull; +import static org.apache.commons.lang3.StringUtils.isBlank; import java.util.Arrays; import java.util.Date; +import java.util.List; import javax.annotation.Resource; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.panzhihua.service_community.dao.McsConfigDAO; -import com.panzhihua.service_community.dao.McsGameDAO; -import com.panzhihua.service_community.entity.McsConfig; +import com.panzhihua.common.model.dtos.community.microCommercialStreet.LoginWithPhoneDTO; import org.springframework.beans.BeanUtils; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -24,11 +29,15 @@ import com.panzhihua.common.model.dtos.community.microCommercialStreet.DisableOrEnableMcsMerchantDTO; import com.panzhihua.common.model.dtos.community.microCommercialStreet.McsMerchantDTO; import com.panzhihua.common.model.dtos.community.microCommercialStreet.PageMcsMerchantDTO; +import com.panzhihua.common.model.vos.LoginUserInfoVO; import com.panzhihua.common.model.vos.R; import com.panzhihua.common.model.vos.community.microCommercialStreet.McsMerchantVO; import com.panzhihua.common.service.user.UserService; import com.panzhihua.common.utlis.DateUtils; +import com.panzhihua.service_community.dao.McsConfigDAO; +import com.panzhihua.service_community.dao.McsGameDAO; import com.panzhihua.service_community.dao.McsMerchantDAO; +import com.panzhihua.service_community.entity.McsConfig; import com.panzhihua.service_community.entity.McsMerchant; import com.panzhihua.service_community.service.McsMerchantService; @@ -50,6 +59,8 @@ private McsConfigDAO mcsConfigDAO; @Resource private McsGameDAO mcsGameDAO; + @Resource + private StringRedisTemplate stringRedisTemplate; /** * 新增数字商业街商家 @@ -67,6 +78,7 @@ return R.fail("临时商家未设置点亮天数"); } mcsMerchant.setExpireAt(DateUtils.addDay(new Date(), litDays)); + mcsMerchant.setFirstLitAt(new Date()); } McsConfig publishLimitConfig = mcsConfigDAO.selectOne(new QueryWrapper<McsConfig>().lambda().eq(McsConfig::getKey, MERCHANT_PUBLISH_LIMIT_KEY)); mcsMerchant.setPublishLimit(nonNull(publishLimitConfig) ? Integer.parseInt(publishLimitConfig.getValue()) : DEFAULT_PUBLISH_LIMIT); @@ -76,8 +88,8 @@ //添加user R addUserResult = userService.addMcsMerchantUser(mcsMerchantDTO); if (R.isOk(addUserResult)) { - Long merchantUserId = ((Integer) addUserResult.getData()).longValue(); - mcsMerchant.setUserId(merchantUserId); + LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(addUserResult.getData()), LoginUserInfoVO.class); + mcsMerchant.setUserId(loginUserInfoVO.getUserId()); this.baseMapper.updateById(mcsMerchant); } else { throw new ServiceException("406", addUserResult.getMsg()); @@ -96,6 +108,7 @@ @Transactional(rollbackFor = Exception.class) public R putMcsMerchant(McsMerchantDTO mcsMerchantDTO) { McsMerchant mcsMerchant = this.baseMapper.selectById(mcsMerchantDTO.getId()); + Long userId = mcsMerchant.getUserId(); if (isNull(mcsMerchant)) { return R.fail("商家不存在"); } @@ -109,7 +122,7 @@ int result = this.baseMapper.updateById(mcsMerchant); if (result > 0) { //修改商家绑定账户号 - mcsMerchantDTO.setUserId(mcsMerchant.getUserId()); + mcsMerchantDTO.setUserId(userId); R putResult = userService.putMcsMerchantUser(mcsMerchantDTO); if (!R.isOk(putResult)) { throw new ServiceException("406", putResult.getMsg()); @@ -128,10 +141,7 @@ public R getMcsMerchant(Long merchantId) { McsMerchantVO merchantVO = this.baseMapper.getMcsMerchantById(merchantId); if (nonNull(merchantVO)) { - Integer publishLimit = merchantVO.getPublishLimit(); - Integer publishCount = mcsGameDAO.selectPublishCount(merchantVO.getId()); - Integer idleTotal = publishLimit - publishCount; - merchantVO.setIdleTotal(idleTotal > 0 ? idleTotal : 0); + retrieveMerchantSurplusLitDays(merchantVO); } return R.ok(merchantVO); } @@ -164,6 +174,10 @@ page.setSize(pageMcsMerchantDTO.getPageSize()); page.setCurrent(pageMcsMerchantDTO.getPageNum()); IPage<McsMerchantVO> mcsMerchants = this.baseMapper.pageMcsMerchant(page, pageMcsMerchantDTO); + List<McsMerchantVO> records = mcsMerchants.getRecords(); + if (nonNull(records) && !records.isEmpty()) { + records.forEach(e -> retrieveMerchantSurplusLitDays(e)); + } return R.ok(mcsMerchants); } @@ -190,4 +204,136 @@ return R.fail("未知错误"); } } + + /** + * 根据account查询微商业街商家信息 + * @param account + * @return + */ + @Override + public R getMcsMerchantByAccount(String account) { + McsMerchantVO merchantVO = this.baseMapper.getMcsMerchantByAccount(account); + if (isNull(merchantVO)) { + return R.fail("账号不存在"); + } + retrieveMerchantSurplusLitDays(merchantVO); + return R.ok(merchantVO); + } + + /** + * 完成订单更新/新增商家信息 + * + * @param merchantName + * @param configId + * @param userId + * @return + */ + @Override + public R updateOrAddMcsMerchantAfterOrder(String merchantName, Long configId, Long userId) { + McsMerchant mcsMerchant = this.baseMapper.selectOne(new QueryWrapper<McsMerchant>().lambda().eq(McsMerchant::getUserId, userId)); + McsConfig mcsConfig = mcsConfigDAO.selectById(configId); + if (isNull(mcsConfig) || isBlank(mcsConfig.getValue())) { + return R.fail("套餐配置不存在"); + } + JSONObject packageObject = JSONObject.parseObject(mcsConfig.getValue()); + if (!packageObject.containsKey("day")) { + return R.fail("配置有误"); + } + Integer litDays = (Integer)packageObject.get("day"); + Date nowDate = new Date(); + int num; + if (isNull(mcsMerchant)) { + //新增商家 + R<LoginUserInfoVO> userInfoVOR = userService.getUserInfoByUserId(userId.toString()); + if (R.isOk(userInfoVOR) && nonNull(userInfoVOR.getData())) { + LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(userInfoVOR.getData()), LoginUserInfoVO.class); + mcsMerchant = new McsMerchant(); + mcsMerchant.setName(isBlank(merchantName) ? "默认名称" : merchantName); + mcsMerchant.setPhone(loginUserInfoVO.getPhone()); + mcsMerchant.setAccount(loginUserInfoVO.getAccount()); + mcsMerchant.setLevel(1); + mcsMerchant.setExpireAt(DateUtils.addDay(nowDate, litDays)); + mcsMerchant.setFirstLitAt(nowDate); + McsConfig publishLimitConfig = mcsConfigDAO.selectOne(new QueryWrapper<McsConfig>().lambda().eq(McsConfig::getKey, MERCHANT_PUBLISH_LIMIT_KEY)); + mcsMerchant.setPublishLimit(nonNull(publishLimitConfig) ? Integer.parseInt(publishLimitConfig.getValue()) : DEFAULT_PUBLISH_LIMIT); + mcsMerchant.setUserId(userId); + mcsMerchant.setCreatedBy(userId); + mcsMerchant.setUpdatedBy(userId); + num = this.baseMapper.insert(mcsMerchant); + } else { + return R.fail("获取用户信息失败"); + } + } else { + //更新商家 + Date previousExpireAt = mcsMerchant.getExpireAt(); + Date nowExpireAt; + if (isNull(previousExpireAt) || previousExpireAt.before(nowDate)) { + nowExpireAt = DateUtils.addDay(nowDate, litDays); + } else { + nowExpireAt = DateUtils.addDay(previousExpireAt, litDays); + } + McsMerchant mcsMerchant1 = new McsMerchant(); + mcsMerchant1.setId(mcsMerchant.getId()); + mcsMerchant1.setExpireAt(nowExpireAt); + mcsMerchant1.setUpdatedBy(userId); + num = this.baseMapper.updateById(mcsMerchant1); + } + if (num > 0) { + return R.ok(mcsMerchant.getId()); + } else { + return R.fail(); + } + } + + /** + * 验证码登录 + * @param loginDTO + * @return + */ + @Override + public R loginWithPhone(LoginWithPhoneDTO loginDTO) { + String phone = loginDTO.getPhone(); + String redisKey = String.join("::", phone, loginDTO.getCode()); + Boolean hasKey = stringRedisTemplate.hasKey(redisKey); + if (hasKey) { + //通过验证 + R r = userService.getSysUserByPhone(phone, 11); + if (R.isOk(r)) { + if (nonNull(r.getData())) { + //已有用户 + LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(r.getData()), LoginUserInfoVO.class); + stringRedisTemplate.delete(redisKey); + return R.ok(loginUserInfoVO); + } else { + //自动创建用户 + McsMerchantDTO mcsMerchantDTO = new McsMerchantDTO(); + mcsMerchantDTO.setAccount(phone); + mcsMerchantDTO.setPassword("88888888"); + mcsMerchantDTO.setAccountStatus(1); + mcsMerchantDTO.setPhone(phone); + R addUserResult = userService.addMcsMerchantUser(mcsMerchantDTO); + if (R.isOk(addUserResult)) { + LoginUserInfoVO loginUserInfoVO = JSONObject.parseObject(JSONObject.toJSONString(addUserResult.getData()), LoginUserInfoVO.class); + stringRedisTemplate.delete(redisKey); + return R.ok(loginUserInfoVO); + } + return addUserResult; + } + } + return r; + } else { + return R.fail("验证码错误"); + } + } + + private void retrieveMerchantSurplusLitDays(McsMerchantVO merchantVO) { + Integer publishLimit = merchantVO.getPublishLimit(); + Integer publishCount = mcsGameDAO.selectPublishCount(merchantVO.getId()); + Integer idleTotal = publishLimit - publishCount; + merchantVO.setIdleTotal(idleTotal > 0 ? idleTotal : 0); + if (nonNull(merchantVO.getExpireAt())) { + int surplusLitDays = DateUtils.differentDays(new Date(), merchantVO.getExpireAt()); + merchantVO.setSurplusLitDays(surplusLitDays > 0 ? surplusLitDays : 0); + } + } } -- Gitblit v1.7.1