罗元桥
2021-09-27 faae303cd25f90b1c732b6bc737ede8b8ab714a0
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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.isNotBlank;
 
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
import javax.annotation.Resource;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.exceptions.ServiceException;
import com.panzhihua.common.model.dtos.community.convenient.ConvenientMerchantDTO;
import com.panzhihua.common.model.dtos.community.convenient.DisableOrEnableConvenientMerchantDTO;
import com.panzhihua.common.model.dtos.community.convenient.ExportMerchantDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageClassifyMerchantDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageConvenientMerchantDTO;
import com.panzhihua.common.model.dtos.community.convenient.PagePopularMerchantDTO;
import com.panzhihua.common.model.dtos.community.convenient.PageSearchDTO;
import com.panzhihua.common.model.dtos.community.convenient.ResetPasswordConvenientMerchantDTO;
import com.panzhihua.common.model.dtos.grid.EventGridMemberPassResetDTO;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.convenient.ConvenientConsultationStatisticsVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientMerchantVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductSpecificationVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientProductVO;
import com.panzhihua.common.model.vos.community.convenient.ConvenientViewStatisticsVO;
import com.panzhihua.common.model.vos.community.convenient.ExportMerchantVO;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.service_community.dao.ComActDAO;
import com.panzhihua.service_community.dao.ConvenientConsultationStatisticsDAO;
import com.panzhihua.service_community.dao.ConvenientMerchantDAO;
import com.panzhihua.service_community.dao.ConvenientProductDAO;
import com.panzhihua.service_community.dao.ConvenientServiceCategoryDAO;
import com.panzhihua.service_community.dao.ConvenientViewStatisticsDAO;
import com.panzhihua.service_community.model.dos.ComActDO;
import com.panzhihua.service_community.model.dos.ConvenientConsultationStatisticsDO;
import com.panzhihua.service_community.model.dos.ConvenientMerchantDO;
import com.panzhihua.service_community.model.dos.ConvenientServiceCategoryDO;
import com.panzhihua.service_community.model.dos.ConvenientViewStatisticsDO;
import com.panzhihua.service_community.service.ConvenientMerchantService;
 
/**
 * @title: ConvenientMerchantServiceImpl
 * @projectName: 成都呐喊信息技术有限公司-智慧社区项目
 * @description: 便民服务商家服务实现类
 * @author: hans
 * @date: 2021/09/16 16:14
 */
@Service
@Slf4j
public class ConvenientMerchantServiceImpl extends ServiceImpl<ConvenientMerchantDAO, ConvenientMerchantDO> implements ConvenientMerchantService {
 
    @Resource
    private UserService userService;
    @Resource
    private ConvenientServiceCategoryDAO convenientServiceCategoryDAO;
    @Resource
    private ComActDAO comActDAO;
    @Resource
    private ConvenientProductDAO convenientProductDAO;
    @Resource
    private ConvenientConsultationStatisticsDAO convenientConsultationStatisticsDAO;
    @Resource
    private ConvenientViewStatisticsDAO convenientViewStatisticsDAO;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addMerchant(ConvenientMerchantDTO convenientMerchantDTO) {
        log.error("catTimeStamp开始执行addMerchant", Instant.now().toEpochMilli());
        ComActDO comActDO = comActDAO.selectById(convenientMerchantDTO.getCommunityId());
        ConvenientMerchantDO convenientMerchantDO = new ConvenientMerchantDO();
        BeanUtils.copyProperties(convenientMerchantDTO, convenientMerchantDO);
        convenientMerchantDO.setUserId(0L);
        convenientMerchantDO.setCommunityName(comActDO.getName());
        convenientMerchantDO.setCreatedAt(new Date());
        int insertResult = this.baseMapper.insert(convenientMerchantDO);
        if (insertResult > 0) {
            Long merchantId = convenientMerchantDO.getId();
            if (nonNull(merchantId)) {
                //添加服务
                List<Long> serviceIds = convenientMerchantDTO.getServiceIds();
                serviceIds.forEach(serviceId -> {
                    ConvenientServiceCategoryDO convenientServiceCategoryDO = convenientServiceCategoryDAO.selectById(serviceId);
                    convenientServiceCategoryDAO.createMerchantServiceRelation(merchantId, serviceId,
                            convenientServiceCategoryDO.getName(), convenientMerchantDTO.getCreatedBy());
                });
            }
        }
        log.error("catTimeStamp商家创建完成开始添加用户", Instant.now().toEpochMilli());
        //添加user
        R addUserResult = userService.addConvenientMerchantUser(convenientMerchantDTO);
        if (R.isOk(addUserResult)) {
            Long merchantUserId = ((Integer) addUserResult.getData()).longValue();
            convenientMerchantDO.setUserId(merchantUserId);
            this.baseMapper.updateById(convenientMerchantDO);
        } else {
            throw new ServiceException("406", addUserResult.getMsg());
        }
        log.error("catTimeStamp全部完成", Instant.now().toEpochMilli());
        return R.ok();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R putMerchant(ConvenientMerchantDTO convenientMerchantDTO) {
        Long merchantId = convenientMerchantDTO.getId();
        ConvenientMerchantDO convenientMerchantDO = this.baseMapper.selectById(merchantId);
        if (isNull(convenientMerchantDO)) {
            return R.fail("商家不存在");
        }
        Long communityId = convenientMerchantDTO.getCommunityId();
        if (nonNull(communityId)) {
            ComActDO comActDO = comActDAO.selectById(communityId);
            convenientMerchantDO.setCommunityName(comActDO.getName());
        }
        String account = convenientMerchantDTO.getAccount();
        if (isNotBlank(account)) {
            //修改商家绑定账户号
            R putAccountResult = userService.putUserAccount(convenientMerchantDO.getUserId(), account);
            if (!R.isOk(putAccountResult)) {
                return putAccountResult;
            }
        }
        List<Long> serviceIds = convenientMerchantDTO.getServiceIds();
        if (!serviceIds.isEmpty()) {
            convenientServiceCategoryDAO.deleteMerchantServiceRelation(merchantId);
            serviceIds.forEach(serviceId -> {
                ConvenientServiceCategoryDO convenientServiceCategoryDO = convenientServiceCategoryDAO.selectById(serviceId);
                convenientServiceCategoryDAO.createMerchantServiceRelation(merchantId, serviceId,
                        convenientServiceCategoryDO.getName(), convenientMerchantDTO.getCreatedBy());
            });
        }
        BeanUtils.copyProperties(convenientMerchantDTO, convenientMerchantDO);
        convenientMerchantDO.setIntroduction(convenientMerchantDTO.getIntroduction());
        this.baseMapper.updateById(convenientMerchantDO);
        return R.ok();
    }
 
    @Override
    public R deleteMerchant(Long merchantId, Long operator) {
        int result = this.baseMapper.deleteMerchantById(merchantId, operator);
        if (result > 0) {
            return R.ok();
        }
        return R.fail("删除失败");
    }
 
    @Override
    public R pageMerchant(PageConvenientMerchantDTO pageConvenientMerchantDTO) {
        Page page = new Page<>();
        page.setSize(pageConvenientMerchantDTO.getPageSize());
        page.setCurrent(pageConvenientMerchantDTO.getPageNum());
        IPage<ConvenientMerchantVO> iPage = this.baseMapper.pageMerchant(page, pageConvenientMerchantDTO);
        if (nonNull(pageConvenientMerchantDTO.getServiceId())) {
            List<ConvenientMerchantVO> records = iPage.getRecords();
            if (!records.isEmpty()) {
                records.forEach(record -> {
                    String serviceScope = convenientServiceCategoryDAO.selectServiceScopeByMerchantId(record.getId());
                    record.setServiceScope(serviceScope);
                });
            }
        }
        return R.ok(iPage);
    }
 
    @Override
    public R getMerchant(Long merchantId) {
        ConvenientMerchantVO convenientMerchantVO = this.baseMapper.selectMerchantById(merchantId);
        List<Long> serviceIds = convenientServiceCategoryDAO.selectServiceIdsForMerchant(merchantId);
        convenientMerchantVO.setServiceIds(serviceIds);
        return R.ok(convenientMerchantVO);
    }
 
    @Override
    public R disableOrEnableMerchant(DisableOrEnableConvenientMerchantDTO disableOrEnableConvenientMerchantDTO) {
        List<ConvenientMerchantDO> convenientMerchantDOList = this.baseMapper.selectList(new QueryWrapper<ConvenientMerchantDO>()
                .lambda().in(ConvenientMerchantDO::getId, disableOrEnableConvenientMerchantDTO.getIds()));
        List<Long> userIds = convenientMerchantDOList.stream().map(ConvenientMerchantDO::getUserId).collect(Collectors.toList());
        disableOrEnableConvenientMerchantDTO.setUserIds(userIds);
        R result = userService.disableOrEnableMerchantUsers(disableOrEnableConvenientMerchantDTO);
        int type = disableOrEnableConvenientMerchantDTO.getType().intValue();
        if (type == 2) {
            this.baseMapper.batchUpdateBusinessStatus(convenientMerchantDOList, 0);
        }
        if (R.isOk(result)) {
            return R.ok();
        }
        return R.fail("操作失败");
    }
 
    @Override
    public R resetPasswordMerchant(ResetPasswordConvenientMerchantDTO resetPasswordConvenientMerchantDTO) {
        List<ConvenientMerchantDO> convenientMerchantDOList = this.baseMapper.selectList(new QueryWrapper<ConvenientMerchantDO>()
                .lambda().in(ConvenientMerchantDO::getId, resetPasswordConvenientMerchantDTO.getIds()));
        List<Long> userIds = convenientMerchantDOList.stream().map(ConvenientMerchantDO::getUserId).collect(Collectors.toList());
        EventGridMemberPassResetDTO eventGridMemberPassResetDTO = new EventGridMemberPassResetDTO();
        eventGridMemberPassResetDTO.setPassword(resetPasswordConvenientMerchantDTO.getPassword());
        eventGridMemberPassResetDTO.setIds(userIds);
        return userService.gridMemberPassReset(eventGridMemberPassResetDTO);
    }
 
    @Override
    public R<ConvenientMerchantVO> getUserMerchantInfoByAccount(String account) {
        try {
            R<LoginUserInfoVO> loginUserInfoVOR = userService.getUserInfo(account + "_10");
            LoginUserInfoVO loginUserInfoVO =
                    JSONObject.parseObject(JSONObject.toJSONString(loginUserInfoVOR.getData()), LoginUserInfoVO.class);
 
            if (isNull(loginUserInfoVO)) {
                return R.fail(500, "该账号不存在");
            }
            ConvenientMerchantDO merchantDO = this.baseMapper.selectOne(new LambdaQueryWrapper<ConvenientMerchantDO>()
                    .eq(ConvenientMerchantDO::getIsDel, false).eq(ConvenientMerchantDO::getUserId, loginUserInfoVO.getUserId()));
            if (isNull(merchantDO)) {
                return R.fail(500, "该账号不存在");
            }
            ConvenientMerchantVO merchantVO = new ConvenientMerchantVO();
            BeanUtils.copyProperties(merchantDO, merchantVO);
            merchantVO.setAccount(account);
            merchantVO.setAccountStatus(loginUserInfoVO.getStatus());
            return R.ok(merchantVO);
        } catch (Exception e) {
            return R.fail();
        }
    }
 
    @Override
    public R<ConvenientMerchantVO> getUserConvenientMerchantInfo(Long userId) {
        ConvenientMerchantDO merchantDO = this.baseMapper.selectOne(new QueryWrapper<ConvenientMerchantDO>()
                .lambda().eq(ConvenientMerchantDO::getUserId, userId));
        if (isNull(merchantDO)) {
            return R.fail("商家不存在");
        }
        Long merchantId = merchantDO.getId();
        ConvenientMerchantVO convenientMerchantVO = new ConvenientMerchantVO();
        BeanUtils.copyProperties(merchantDO, convenientMerchantVO);
        List<Long> serviceIds = convenientServiceCategoryDAO.selectServiceIdsForMerchant(merchantId);
        convenientMerchantVO.setServiceIds(serviceIds);
        Date nowDate = new Date();
        SimpleDateFormat mothFormat = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
        String moth = mothFormat.format(nowDate);
        String day = dayFormat.format(nowDate);
        int consultationCount = convenientConsultationStatisticsDAO.selectTotalConsultationVolume(merchantId);
        int monthConsultationVolume = convenientConsultationStatisticsDAO.selectMonthConsultationVolume(merchantId, moth);
        int dayConsultationVolume = convenientConsultationStatisticsDAO.selectDayConsultationVolume(merchantId, day);
        Integer viewCount = convenientViewStatisticsDAO.selectTotalViewNum(merchantId);
        int monthViewNum = convenientViewStatisticsDAO.selectMonthViewNum(merchantId, moth);
        int dayViewNum = convenientViewStatisticsDAO.selectDayViewNum(merchantId, day);
        convenientMerchantVO.setConsultationVolume(consultationCount);
        convenientMerchantVO.setViewNum(viewCount);
        convenientMerchantVO.setMonthConsultationVolume(monthConsultationVolume);
        convenientMerchantVO.setDayConsultationVolume(dayConsultationVolume);
        convenientMerchantVO.setMonthViewNum(monthViewNum);
        convenientMerchantVO.setDayViewNum(dayViewNum);
        return R.ok(convenientMerchantVO);
    }
 
    @Override
    public R getPopularMerchants(PagePopularMerchantDTO pagePopularMerchantDTO) {
        Date nowDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String currentMon = simpleDateFormat.format(nowDate).concat("%");
        Page page = new Page<>();
        page.setSize(pagePopularMerchantDTO.getPageSize());
        page.setCurrent(pagePopularMerchantDTO.getPageNum());
        IPage<ConvenientMerchantVO> merchantVOList = this.baseMapper.getPopularMerchants(page, pagePopularMerchantDTO.getCommunityId(), currentMon);
        return R.ok(merchantVOList);
    }
 
    @Override
    public R getClassifyMerchants(PageClassifyMerchantDTO pageClassifyMerchantDTO) {
        Date nowDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String currentMon = simpleDateFormat.format(nowDate).concat("%");
        Page page = new Page<>();
        page.setSize(pageClassifyMerchantDTO.getPageSize());
        page.setCurrent(pageClassifyMerchantDTO.getPageNum());
        IPage<ConvenientMerchantVO> merchantVOList = this.baseMapper.getClassifyMerchants(page, pageClassifyMerchantDTO, currentMon);
        List<ConvenientMerchantVO> records = merchantVOList.getRecords();
        if (!records.isEmpty()) {
            records.forEach(record -> {
                String serviceScope = convenientServiceCategoryDAO.selectServiceScopeByMerchantId(record.getId());
                record.setServiceScope(serviceScope);
            });
        }
        return R.ok(merchantVOList);
    }
 
    /**
     * 小程序获取商家详情
     * @param merchantId
     * @return
     */
    @Override
    public R getMerchantDetail(Long merchantId) {
        Date nowDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String currentMon = simpleDateFormat.format(nowDate).concat("%");
        return R.ok(this.baseMapper.getMerchantDetail(merchantId, currentMon));
    }
 
    @Override
    public R pageSearchMerchant(PageSearchDTO pageSearchDTO) {
        Date nowDate = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String currentMon = simpleDateFormat.format(nowDate).concat("%");
        Page page = new Page<>();
        page.setSize(pageSearchDTO.getPageSize());
        page.setCurrent(pageSearchDTO.getPageNum());
        IPage<ConvenientMerchantVO> merchantVOIPage = this.baseMapper.pageSearchMerchant(page, pageSearchDTO, currentMon);
        List<ConvenientMerchantVO> records = merchantVOIPage.getRecords();
        if (!records.isEmpty()) {
            records.forEach(record -> {
                List<ConvenientProductVO> merchantProduct = convenientProductDAO.getMerchantProduct(record.getId());
                List<ConvenientProductSpecificationVO> productSpecifications = convenientProductDAO.getProductSpecifications(record.getId());
                if (!merchantProduct.isEmpty()) {
                    if (merchantProduct.size() > 2) {
                        merchantProduct = merchantProduct.subList(0, 2);
                    }
                    merchantProduct.forEach(product -> {
                        List<ConvenientProductSpecificationVO> list = new ArrayList<>();
                        productSpecifications.forEach(specification -> {
                            if (product.getId().equals(specification.getProductId())) {
                                list.add(specification);
                            }
                        });
                        product.setProductSpecificationVOList(list);
                    });
                }
                record.setProductVOList(merchantProduct);
            });
        }
        return R.ok(merchantVOIPage);
    }
 
    @Override
    public R consultMerchant(Long merchantId) {
        ConvenientMerchantDO merchantDO = this.baseMapper.selectById(merchantId);
        if (isNull(merchantDO)) {
            return R.fail("商家不存在");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = simpleDateFormat.format(new Date());
        ConvenientConsultationStatisticsDO statisticsDO = convenientConsultationStatisticsDAO.selectOne(new LambdaQueryWrapper<ConvenientConsultationStatisticsDO>()
                .eq(ConvenientConsultationStatisticsDO::getMerchantId, merchantId).eq(ConvenientConsultationStatisticsDO::getStatisticDate, nowDate));
        if (isNull(statisticsDO)) {
            convenientConsultationStatisticsDAO.createTodayStatistic(merchantId);
        } else {
            convenientConsultationStatisticsDAO.incrConsultationNum(merchantId, nowDate);
        }
        return R.ok();
    }
 
    @Override
    public R incrMerchantView(Long merchantId) {
        ConvenientMerchantDO merchantDO = this.baseMapper.selectById(merchantId);
        if (isNull(merchantDO)) {
            return R.fail("商家不存在");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = simpleDateFormat.format(new Date());
        ConvenientViewStatisticsDO statisticsDO = convenientViewStatisticsDAO.selectOne(new LambdaQueryWrapper<ConvenientViewStatisticsDO>()
                .eq(ConvenientViewStatisticsDO::getMerchantId, merchantId).eq(ConvenientViewStatisticsDO::getStatisticDate, nowDate));
        if (isNull(statisticsDO)) {
            convenientViewStatisticsDAO.createTodayStatistic(merchantId);
        } else {
            convenientViewStatisticsDAO.incrViewNum(merchantId, nowDate);
        }
        return R.ok();
    }
 
    @Override
    public R exportMerchant(ExportMerchantDTO exportMerchantDTO) {
        List<Long> needExportIds = exportMerchantDTO.getIds();
        if (nonNull(needExportIds) && !needExportIds.isEmpty()) {
            //根据id导出
            return R.ok(this.baseMapper.exportMerchantByIds(needExportIds));
        }
        List<ExportMerchantVO> exportMerchantVOList = this.baseMapper.exportMerchantBySearch(exportMerchantDTO);
        if (nonNull(exportMerchantDTO.getServiceId()) && !exportMerchantVOList.isEmpty()) {
            exportMerchantVOList.forEach(merchant -> {
                String serviceScope = convenientServiceCategoryDAO.selectServiceScopeByMerchantId(merchant.getId());
                merchant.setServiceScope(serviceScope);
            });
        }
        return R.ok(exportMerchantVOList);
    }
 
    /**
     * 定时任务每隔半小时将商家浏览量和咨询量总值计入指定商家数据中
     * @return
     */
    @Override
    public R timedTaskWriteDataToMerchantJobHandler() {
        List<ConvenientConsultationStatisticsVO> consultationVOList = convenientConsultationStatisticsDAO.selectSumForConsultationNum();
        List<ConvenientViewStatisticsVO> viewVOList = convenientViewStatisticsDAO.selectSumForViewNum();
        this.baseMapper.batchUpdateMerchantConsultationNum(consultationVOList);
        this.baseMapper.batchUpdateMerchantViewNum(viewVOList);
        return R.ok();
    }
}