linwei
2024-01-06 6a53233a381819bcdcc7acdc3a018db9b3b8a311
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
/*
 *      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.jetbrains.annotations.Nullable;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.discuss.entity.PublicDiscussEntity;
import org.springblade.modules.discuss.entity.TopicsEntity;
import org.springblade.modules.discuss.entity.UserTopicsEntity;
import org.springblade.modules.discuss.mapper.UserTopicsMapper;
import org.springblade.modules.discuss.service.IPublicDiscussService;
import org.springblade.modules.discuss.service.ITopicsService;
import org.springblade.modules.discuss.service.IUserTopicsService;
import org.springblade.modules.discuss.vo.TopicsVO;
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<TopicsVO> topics) throws Exception {
        // 判断是否一户一票 还是一人一票
        IPublicDiscussService bean = SpringUtil.getBean(IPublicDiscussService.class);
        PublicDiscussEntity one = bean.getOne(Wrappers.<PublicDiscussEntity>lambdaQuery().eq(PublicDiscussEntity::getArticleId, topics.get(0).getArticleId()));
        // 一户一票
        if (one.getVoteRestrictions().equals(CommonConstant.NUMBER_ONE)) {
            long count = count(Wrappers.<UserTopicsEntity>lambdaQuery().eq(UserTopicsEntity::getHouseCode, topics.get(0).getHouseCode()));
            if (count > 1) {
                throw new Exception("您的房屋已投票,不能重复投票!");
            }
        } else {
            //
            long count = count(Wrappers.<UserTopicsEntity>lambdaQuery().eq(UserTopicsEntity::getUserId, AuthUtil.getUserId()));
            if (count > 1) {
                throw new Exception("您的已投票,不能重复投票!");
            }
        }
        Boolean userTopics = getaBoolean(topics);
        if (userTopics != null) return userTopics;
        return false;
    }
 
    @Nullable
    private Boolean getaBoolean(List<TopicsVO> topics) {
        List<UserTopicsEntity> objects = new ArrayList<>();
        for (TopicsVO 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.setSelected(topic.getSelected());
                userTopics.setUserId(AuthUtil.getUserId());
                userTopics.setPublicDiscussId(topic.getPublicDiscussId());
                userTopics.setArticleId(topic.getArticleId());
                userTopics.setHouseCode(topic.getHouseCode());
                UpdateWrapper<TopicsEntity> objectUpdateWrapper = new UpdateWrapper<>();
                objectUpdateWrapper.setSql("number = number + 1");
                objectUpdateWrapper.eq("id", topic.getSelected());
                topicsService.update(null, objectUpdateWrapper);
                return save(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.setSelected(topic.getSelected());
                    userTopics.setUserId(AuthUtil.getUserId());
                    userTopics.setPublicDiscussId(topic.getPublicDiscussId());
                    userTopics.setArticleId(topic.getArticleId());
                    userTopics.setHouseCode(topic.getHouseCode());
                    UpdateWrapper<TopicsEntity> objectUpdateWrapper = new UpdateWrapper<>();
                    objectUpdateWrapper.setSql("number = number + 1");
                    objectUpdateWrapper.eq("id", o);
                    objectsTwo.add(userTopics);
                    topicsService.update(null, objectUpdateWrapper);
                }
                return save(objectsTwo.get(0));
            }
        }
        return null;
    }
 
 
    @Override
    public Integer getCount(Integer id) {
        List<UserTopicsEntity> list = list(Wrappers.<UserTopicsEntity>lambdaQuery()
            .eq(UserTopicsEntity::getPublicDiscussId, id)
            .groupBy(UserTopicsEntity::getUserId));
        return list.size();
    }
}