mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.panzhihua.service_community.service.impl;
 
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.constants.Constants;
import com.panzhihua.common.model.dtos.community.integral.ComActIntegralCommunityRankDTO;
import com.panzhihua.common.model.dtos.community.integral.ComActIntegralCountDTO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.integral.ComActIntegralUserListVO;
import com.panzhihua.common.model.vos.community.integral.ComActIntegralUserRuleVO;
import com.panzhihua.common.model.vos.community.integral.ComActIntegralUserVO;
import com.panzhihua.common.model.vos.community.integral.IntegralUserRankVO;
import com.panzhihua.common.utlis.DateUtils;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_community.dao.ComActDAO;
import com.panzhihua.service_community.dao.ComActIntegralUserMapper;
import com.panzhihua.service_community.dao.ComActUserWalletMapper;
import com.panzhihua.service_community.model.dos.ComActDO;
import com.panzhihua.service_community.model.dos.ComActIntegralRuleDO;
import com.panzhihua.service_community.model.dos.ComActIntegralUserDO;
import com.panzhihua.service_community.model.dos.ComActIntegralUserTradeDO;
import com.panzhihua.service_community.service.ComActIntegralRuleService;
import com.panzhihua.service_community.service.ComActIntegralUserService;
import com.panzhihua.service_community.service.ComActIntegralUserTradeService;
 
import lombok.extern.slf4j.Slf4j;
 
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
 
/**
 * @auther lyq
 * @create 2021-07-28 16:02:57
 * @describe 用户积分账户表服务实现类
 */
@Slf4j
@Service
public class ComActIntegralUserServiceImpl extends ServiceImpl<ComActIntegralUserMapper, ComActIntegralUserDO>
    implements ComActIntegralUserService {
 
    @Resource
    private ComActUserWalletMapper comActUserWalletMapper;
    @Resource
    private ComActIntegralRuleService comActIntegralRuleService;
    @Resource
    private ComActIntegralUserTradeService comActIntegralUserTradeService;
    @Resource
    private ComActDAO comActDAO;
 
    /**
     * 小程序-查询积分说明
     * 
     * @return 积分说明
     */
    @Override
    public R getIntegralExplainApplets() {
        String result = "";
        Map<String, String> resultMap = comActUserWalletMapper.getAgreementContent(0L, 6);
        if (resultMap != null) {
            if (StringUtils.isEmpty(resultMap.get("content"))) {
                comActUserWalletMapper.insertSysAgreement(Constants.INTEGRAL_EXPLAIN, "积分规则说明", 0L, 6);
                result = Constants.INTEGRAL_EXPLAIN;
            } else {
                result = resultMap.get("content");
            }
        } else {
            comActUserWalletMapper.insertSysAgreement(Constants.INTEGRAL_EXPLAIN, "积分规则说明", 0L, 6);
            result = Constants.INTEGRAL_EXPLAIN;
        }
        return R.ok(result);
    }
 
    /**
     * 用户查询当前社区可领取积分规则列表
     * 
     * @param userId
     *            用户id
     * @param communityId
     *            社区id
     * @return 可领取积分规则列表
     */
    @Override
    public R getIntegralReceiveApplets(Long userId, Long communityId) {
 
        List<ComActIntegralUserRuleVO> integralUserRuleList =
            comActIntegralRuleService.getIntegralReceiveApplets(communityId);
        if (integralUserRuleList.isEmpty()) {
            // 如果没有查询到列表数据,增给社区增加默认规则列表再查询
            List<ComActIntegralRuleDO> integralRuleList = comActIntegralRuleService.getIntegralRuleList(communityId);
            comActIntegralRuleService.saveBatch(integralRuleList);
            integralUserRuleList = comActIntegralRuleService.getIntegralReceiveApplets(communityId);
        }
        if (!integralUserRuleList.isEmpty()) {
            integralUserRuleList.forEach(integralUserRule -> {
                if (integralUserRule.getIsRestrict().equals(ComActIntegralRuleDO.isRestrict.no)) {
                    integralUserRule.setIsComplete(ComActIntegralUserRuleVO.isComplete.no);
                } else {
                    ComActIntegralCountDTO integralCountDTO = new ComActIntegralCountDTO();
                    integralCountDTO.setUserId(userId);
                    integralCountDTO.setCommunityId(communityId);
                    integralCountDTO.setType(ComActIntegralUserTradeDO.changeType.add);
                    integralCountDTO.setServiceType(integralUserRule.getIntegralType());
                    if (integralUserRule.getType().equals(ComActIntegralRuleDO.type.month)) {
                        integralCountDTO.setStartTime(DateUtils.getFirstDayOfMonthString());
                        integralCountDTO.setEndTime(DateUtils.getLastDayOfMonthString());
                    } else if (integralUserRule.getType().equals(ComActIntegralRuleDO.type.day)) {
                        integralCountDTO.setStartTime(DateUtils.getDayOfMonthString() + " 00:00:00");
                        integralCountDTO.setEndTime(DateUtils.getDayOfMonthString() + " 23:59:59");
                    }
                    Integer count = comActIntegralUserTradeService.getIntegralCount(integralCountDTO);
                    if (count >= integralUserRule.getCount()) {
                        integralUserRule.setIsComplete(ComActIntegralUserRuleVO.isComplete.yes);
                    } else {
                        integralUserRule.setIsComplete(ComActIntegralUserRuleVO.isComplete.no);
                    }
                }
            });
        }
        return R.ok(integralUserRuleList);
    }
 
    /**
     * 小程序-积分统计
     * 
     * @param userId
     *            用户id
     * @return 用户积分列表
     */
    @Override
    public R getIntegralUserListApplets(Long userId, Long communityId) {
        if (nonNull(communityId)) {
            // 查询该用户在本社区下的积分账户是否存在,不存在则需要给用户创建
            ComActIntegralUserDO integralUserDO =
                    this.baseMapper.selectOne(new QueryWrapper<ComActIntegralUserDO>().lambda()
                            .eq(ComActIntegralUserDO::getCommunityId, communityId).eq(ComActIntegralUserDO::getUserId, userId));
            if (integralUserDO == null) {
                integralUserDO = new ComActIntegralUserDO();
                integralUserDO.setCommunityId(communityId);
                integralUserDO.setUserId(userId);
                integralUserDO.setCreateAt(new Date());
                this.baseMapper.insert(integralUserDO);
            }
        }
        ComActIntegralUserVO integralUserVO = this.baseMapper.getUserIntegralStatistics(userId, communityId);
        if (isNull(communityId)) {
            // 各社区已获积分统计
            List<ComActIntegralUserListVO> integralUserList = this.baseMapper.getIntegralUserListApplets(userId);
            integralUserVO.setIntegralUserList(integralUserList);
        }
        return R.ok(integralUserVO);
    }
 
    /**
     * 查询社区积分账户排行榜
     * 
     * @param communityRankDTO
     *            请求参数
     * @return 社区积分账户排行榜
     */
    @Override
    public R getIntegralCommunityRankApplets(ComActIntegralCommunityRankDTO communityRankDTO) {
        return R.ok(this.baseMapper.getIntegralCommunityRankApplets(
            new Page(communityRankDTO.getPageNum(), communityRankDTO.getPageSize()), communityRankDTO));
    }
 
    /**
     * 根据社区活动id查询社区活动报名且未参与人员列表
     * 
     * @param activityId
     *            活动id
     * @return 社区活动报名且未参与人员列表
     */
    @Override
    public R getTaskActivityPeopleList(Long activityId) {
        return R.ok(this.baseMapper.getTaskActivityPeopleList(activityId));
    }
 
    @Override
    public R getUserIntegralBalance(Long userId, Long communityId) {
        ComActIntegralUserVO integralUserVO = new ComActIntegralUserVO();
        // 查询该用户在本社区下的积分账户是否存在,不存在则需要给用户创建
        ComActIntegralUserDO integralUserDO =
                this.baseMapper.selectOne(new QueryWrapper<ComActIntegralUserDO>().lambda()
                        .eq(ComActIntegralUserDO::getCommunityId, communityId).eq(ComActIntegralUserDO::getUserId, userId));
        if (integralUserDO == null) {
            integralUserDO = new ComActIntegralUserDO();
            integralUserDO.setCommunityId(communityId);
            integralUserDO.setUserId(userId);
            integralUserDO.setCreateAt(new Date());
            this.baseMapper.insert(integralUserDO);
        }
        // 查询用户绑定社区名字
        ComActDO actDO = comActDAO.selectById(communityId);
        if (actDO != null) {
            integralUserVO.setCommunityName(actDO.getName());
        }
        integralUserVO.setAmount(integralUserDO.getIntegralSum());
        return R.ok(integralUserVO);
    }
 
    /**
     * 获取积分详情
     * @param id
     * @return
     */
    @Override
    public R getUserIntegralDetail(Long id) {
        return comActIntegralUserTradeService.getUserIntegralDetail(id);
    }
 
    @Override
    public IntegralUserRankVO getIntegralUserRank(Long communityId, Long userId)
    {
        return baseMapper.getIntegralUserRank(communityId,userId);
    }
}