| | |
| | | package com.panzhihua.service_community.service.impl; |
| | | |
| | | 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.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; |
| | |
| | | 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; |
| | | |
| | |
| | | @Service("mcsMerchantService") |
| | | public class McsMerchantServiceImpl extends ServiceImpl<McsMerchantDAO, McsMerchant> implements McsMerchantService { |
| | | |
| | | private static final String MERCHANT_PUBLISH_LIMIT_KEY = "MERCHANT_PUBLISH_LIMIT"; |
| | | private static final int DEFAULT_PUBLISH_LIMIT = 2; |
| | | |
| | | @Resource |
| | | private UserService userService; |
| | | @Resource |
| | | private McsConfigDAO mcsConfigDAO; |
| | | @Resource |
| | | private McsGameDAO mcsGameDAO; |
| | | @Resource |
| | | private StringRedisTemplate stringRedisTemplate; |
| | | |
| | | /** |
| | | * 新增数字商业街商家 |
| | |
| | | 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); |
| | | mcsMerchant.setUserId(0L); |
| | | int insertResult = this.baseMapper.insert(mcsMerchant); |
| | | if (insertResult > 0) { |
| | | //添加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()); |
| | |
| | | @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("商家不存在"); |
| | | } |
| | | if (mcsMerchantDTO.getLevel().equals(1)) { |
| | | Integer litDays = mcsMerchantDTO.getLitDays(); |
| | | if (isNull(litDays)) { |
| | | return R.fail("临时商家未设置点亮天数"); |
| | | if (nonNull(litDays)) { |
| | | mcsMerchant.setExpireAt(DateUtils.addDay(new Date(), litDays)); |
| | | } |
| | | mcsMerchant.setExpireAt(DateUtils.addDay(new Date(), litDays)); |
| | | } |
| | | BeanUtils.copyProperties(mcsMerchantDTO, mcsMerchant); |
| | | 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()); |
| | |
| | | */ |
| | | @Override |
| | | public R getMcsMerchant(Long merchantId) { |
| | | return R.ok(this.baseMapper.getMcsMerchantById(merchantId)); |
| | | McsMerchantVO merchantVO = this.baseMapper.getMcsMerchantById(merchantId); |
| | | if (nonNull(merchantVO)) { |
| | | retrieveMerchantSurplusLitDays(merchantVO); |
| | | } |
| | | return R.ok(merchantVO); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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); |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |