linwe
2023-11-28 a656c28de35fd7027511cfd960d1c85c9a0b56a9
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.discuss.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.discuss.entity.TopicsEntity;
import org.springblade.modules.discuss.entity.UserPublicEnrollEntity;
import org.springblade.modules.discuss.entity.UserTopicsEntity;
import org.springblade.modules.discuss.mapper.UserTopicsMapper;
import org.springblade.modules.discuss.service.ITopicsService;
import org.springblade.modules.discuss.service.IUserTopicsService;
import org.springblade.modules.discuss.vo.UserTopicsVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 用户议题报表 服务实现类
 *
 * @author BladeX
 * @since 2023-11-22
 */
@Service
public class UserTopicsServiceImpl extends ServiceImpl<UserTopicsMapper, UserTopicsEntity> implements IUserTopicsService {
    @Resource
    private ITopicsService topicsService;
 
    @Override
    public IPage<UserTopicsVO> selectUserTopicsPage(IPage<UserTopicsVO> page, UserTopicsVO userTopics) {
        return page.setRecords(baseMapper.selectUserTopicsPage(page, userTopics));
    }
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchSave(List<TopicsEntity> topics) {
        List<UserTopicsEntity> objects = new ArrayList<>();
        for (TopicsEntity topic : topics) {
            UserTopicsEntity userTopicsEntity = new UserTopicsEntity();
            userTopicsEntity.setUserId(AuthUtil.getUserId());
            userTopicsEntity.setSelected(topic.getSelected());
            userTopicsEntity.setTopicsId(topic.getId());
            userTopicsEntity.setPublicDiscussId(topic.getPublicDiscussId());
            objects.add(userTopicsEntity);
            if (topic.getOptionRange().equals(0)) {
                if (StringUtils.isBlank(topic.getSelected())) {
                    break;
                }
                UserTopicsEntity userTopics = new UserTopicsEntity();
                userTopics.setTopicsId(Integer.valueOf(topic.getSelected()));
                userTopics.setUserId(AuthUtil.getUserId());
                userTopics.setPublicDiscussId(topic.getPublicDiscussId());
                UpdateWrapper<TopicsEntity> objectUpdateWrapper = new UpdateWrapper<>();
                objectUpdateWrapper.setSql("number = number + 1");
                objectUpdateWrapper.eq("id", topic.getSelected());
                topicsService.update(null, objectUpdateWrapper);
                baseMapper.insert(userTopics);
            } else {
                if (StringUtils.isBlank(topic.getSelected())) {
                    break;
                }
                JSONArray objects1 = JSON.parseArray(topic.getSelected());
                List<UserTopicsEntity> objectsTwo = new ArrayList<>();
                for (Object o : objects1) {
                    UserTopicsEntity userTopics = new UserTopicsEntity();
                    userTopics.setTopicsId((Integer) o);
                    userTopics.setUserId(AuthUtil.getUserId());
                    userTopics.setPublicDiscussId(topic.getPublicDiscussId());
                    UpdateWrapper<TopicsEntity> objectUpdateWrapper = new UpdateWrapper<>();
                    objectUpdateWrapper.setSql("number = number + 1");
                    objectUpdateWrapper.eq("id", o);
                    objectsTwo.add(userTopics);
                    topicsService.update(null, objectUpdateWrapper);
                }
                saveOrUpdateBatch(objectsTwo);
            }
        }
        return saveOrUpdateBatch(objects);
    }
 
 
    @Override
    public Long getCount(Integer id) {
        return baseMapper.selectCount(Wrappers.<UserTopicsEntity>lambdaQuery().eq(UserTopicsEntity::getPublicDiscussId, id).groupBy(UserTopicsEntity::getUserId));
    }
}