yujian
2020-01-17 8d84ca36593e33de7e0b604824c471db3587fdb4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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<UserConvertLinkTemplate> 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<UserConvertLinkTemplate> 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);
    }
 
}