package com.yeshi.fanli.service.impl.user; import java.util.Date; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Service; import com.yeshi.fanli.dao.user.UserConvertLinkTemplateDao; import com.yeshi.fanli.entity.bus.user.UserConvertLinkTemplate; import com.yeshi.fanli.service.inter.user.UserConvertLinkTemplateService; import com.yeshi.fanli.service.inter.user.UserInfoExtraService; import com.yeshi.fanli.service.inter.user.vip.UserVIPInfoService; import com.yeshi.fanli.util.StringUtil; @Service public class UserConvertLinkTemplateServiceImpl implements UserConvertLinkTemplateService { @Resource private UserConvertLinkTemplateDao userConvertLinkTemplateDao; @Resource private UserVIPInfoService userVIPInfoService; @Resource private UserInfoExtraService userInfoExtraService; /** * * @Title: countByUid * @Description: 根据用户ID统计数量 * @param uid * @return * long 返回类型 * @throws */ private long countByUid(Long uid) { Query query = new Query(); query.addCriteria(Criteria.where("uid").is(uid)); return userConvertLinkTemplateDao.count(query); } /** * * @Title: verifyTemplate * @Description: 验证模板内容是否正确 * @param template * @param uid * @throws Exception * void 返回类型 * @throws */ private void verifyTemplate(String template, Long uid) throws Exception { if (StringUtil.isNullOrEmpty(template)) throw new Exception("模板内容不能为空"); if (template.length() > 140) { throw new Exception("最长为140个字"); } if (template.contains("[邀请码]")) { String inviteCode = userInfoExtraService.getInviteCodeByUid(uid); if (StringUtil.isNullOrEmpty(inviteCode)) { throw new Exception("用户未激活,模板本不能出现[邀请码]"); } } } @Override public List listByUid(Long uid) { Query query = new Query(); query.addCriteria(Criteria.where("uid").is(uid).and("createTime").lte(new Date(System.currentTimeMillis()+1000*60*60*24L))); List templateList = userConvertLinkTemplateDao.findList(query); if (templateList != null) { String inviteCode = null; for (UserConvertLinkTemplate userConvertLinkTemplate : templateList) { // 替换邀请码 if (userConvertLinkTemplate.getTemplate().contains("[邀请码]")) { if (inviteCode == null) inviteCode = userInfoExtraService.getInviteCodeByUid(uid); userConvertLinkTemplate .setContent(userConvertLinkTemplate.getTemplate().replace("[邀请码]", inviteCode)); } } } return templateList; } @Override public void updateTemplate(UserConvertLinkTemplate template) throws Exception { if (template.getId() == null || StringUtil.isNullOrEmpty(template.getTemplate())) throw new Exception("参数不完整"); UserConvertLinkTemplate oldTemplate = userConvertLinkTemplateDao.get(template.getId()); if (oldTemplate == null) throw new Exception("模板不存在"); verifyTemplate(template.getTemplate(), oldTemplate.getUid()); oldTemplate.setTemplate(template.getTemplate()); oldTemplate.setUpdateTime(new Date()); userConvertLinkTemplateDao.save(oldTemplate); } @Override public void deleteConvertLinkTemplate(String id, Long uid) throws Exception { UserConvertLinkTemplate template = userConvertLinkTemplateDao.get(id); if (template != null && template.getUid().longValue() != uid) throw new Exception("只能删除属于自己的"); userConvertLinkTemplateDao.delete(id); } @Override public void addConvertLinkTemplate(UserConvertLinkTemplate template) throws Exception { if (template.getUid() == null || StringUtil.isNullOrEmpty(template.getTemplate())) throw new Exception("参数不完整"); int maxCount = 5; // 判断数量 long count = countByUid(template.getUid()); if (count >= maxCount) { throw new Exception("数量超限"); } verifyTemplate(template.getTemplate(), template.getUid()); // 添加模板 if (template.getCreateTime() == null) template.setCreateTime(new Date()); template.setId(template.getUid() + "-" + StringUtil.Md5(UUID.randomUUID().toString() + "#" + System.currentTimeMillis())); userConvertLinkTemplateDao.save(template); } }