mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
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
package com.sinata.modular.system.controller;
 
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sinata.common.enums.EnumNoticeMessageType;
import com.sinata.core.base.controller.BaseController;
import com.sinata.core.common.annotion.BussinessLog;
import com.sinata.core.common.annotion.Permission;
import com.sinata.core.common.constant.factory.PageFactory;
import com.sinata.core.log.LogObjectHolder;
import com.sinata.core.util.ToolUtil;
import com.sinata.modular.member.model.MemMerchant;
import com.sinata.modular.member.model.MemUser;
import com.sinata.modular.member.service.IMemMerchantService;
import com.sinata.modular.member.service.IMemUserService;
import com.sinata.modular.system.model.TNotice;
import com.sinata.modular.system.model.TNoticeMessage;
import com.sinata.modular.system.service.ITNoticeMessageService;
import com.sinata.modular.system.service.ITNoticeService;
import com.sinata.modular.system.service.RedisTemplateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 公告控制器
 */
@Controller
@RequestMapping("/tNotice")
public class TNoticeController extends BaseController {
 
    private String PREFIX = "/system/tNotice/";
 
    @Autowired
    private ITNoticeService tNoticeService;
 
    @Autowired
    private RedisTemplateService redisTemplateService;
 
    @Autowired
    private ITNoticeMessageService itNoticeMessageService;
 
    @Autowired
    private IMemUserService iMemUserService;
 
    @Autowired
    private IMemMerchantService iMemMerchantService;
 
    /**
     * 跳转到公告首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tNotice.html";
    }
 
    /**
     * 跳转到添加公告
     */
    @RequestMapping("/tNotice_add")
    public String tNoticeAdd() {
        return PREFIX + "tNotice_add.html";
    }
 
    /**
     * 跳转到修改公告
     */
    @RequestMapping("/tNotice_update/{tNoticeId}")
    public String tNoticeUpdate(@PathVariable Integer tNoticeId, Model model) {
        TNotice tNotice = tNoticeService.selectById(tNoticeId);
        model.addAttribute("item",tNotice);
        LogObjectHolder.me().set(tNotice);
        return PREFIX + "tNotice_edit.html";
    }
 
    /**
     * 获取公告列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String title, Integer source, String beginTime, String endTime) {
        Wrapper<TNotice> wrapper = new EntityWrapper<TNotice>().orderBy("id", false);
        // 未删除的信息
        wrapper.eq("is_delete", 0);
        // 标题名称
        if (ToolUtil.isNotEmpty(title)) {
            wrapper.and().like("title", title);
        }
        // 状态搜索
        if (ToolUtil.isNotEmpty(source) && source != -1) {
            wrapper.and().eq("source", source);
        }
        // 创建时间
        if (ToolUtil.isNotEmpty(beginTime)) {
            wrapper.ge("create_time", beginTime + " 00:00:00");
        }
        if (ToolUtil.isNotEmpty(endTime)) {
            wrapper.le("create_time", endTime + " 23:59:59");
        }
        // 物理分页
        Page<TNotice> page = new PageFactory<TNotice>().defaultPage();
        page = tNoticeService.selectPage(page,wrapper);
        return super.packForBT(page);
    }
 
    /**
     * 新增公告
     */
    @Permission
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TNotice tNotice) {
        if (tNotice != null) {
            tNotice.setIsDelete( 0);
            if (ToolUtil.isNotEmpty(tNotice.getContent())) {
                // H5页面代码包装(判断加上自适应代码)
                tNotice.setContent(ToolUtil.h5Warpper(tNotice.getContent()));
            }
            tNotice.setCreateTime(new Date());
            tNoticeService.insert(tNotice);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    //消息来源 1-全部用户,2-全部商家
                    if (tNotice.getSource().equals("1")){
                        List<Object> ids = iMemUserService.selectObjs(new EntityWrapper<MemUser>().setSqlSelect("id").eq("is_delete",0));
                        if (CollectionUtils.isNotEmpty(ids)){
                            List<TNoticeMessage> tNoticeMessages = new ArrayList<TNoticeMessage>();
                            tNoticeMessages.addAll(ids.stream().map(id->{
                                TNoticeMessage tNoticeMessage = new TNoticeMessage();
                                tNoticeMessage.setContent(tNotice.getTitle());
                                tNoticeMessage.setCreateTime(tNotice.getCreateTime());
                                tNoticeMessage.setIsDelete(0);
                                tNoticeMessage.setIsRead(0);
                                tNoticeMessage.setOtherId(tNotice.getId().toString());
                                tNoticeMessage.setShowImg(tNotice.getCoverPlan());
                                tNoticeMessage.setUserId(new Integer(id.toString()));
                                tNoticeMessage.setType(EnumNoticeMessageType.NOTICE.index);
                                tNoticeMessage.setUserType(1);
                                return tNoticeMessage;
                            }).collect(Collectors.toList()));
                            itNoticeMessageService.insertList(tNoticeMessages);
                        }
                    }else{
                        List<Object> ids = iMemMerchantService.selectObjs(new EntityWrapper<MemMerchant>().setSqlSelect("id").eq("is_delete",0));
                        if (CollectionUtils.isNotEmpty(ids)){
                            List<TNoticeMessage> tNoticeMessages = new ArrayList<TNoticeMessage>();
                            tNoticeMessages = ids.stream().map(id->{
                                TNoticeMessage tNoticeMessage = new TNoticeMessage();
                                tNoticeMessage.setContent(tNotice.getTitle());
                                tNoticeMessage.setCreateTime(tNotice.getCreateTime());
                                tNoticeMessage.setIsDelete(0);
                                tNoticeMessage.setIsRead(0);
                                tNoticeMessage.setOtherId(tNotice.getId().toString());
                                tNoticeMessage.setShowImg(tNotice.getCoverPlan());
                                tNoticeMessage.setUserId(new Integer(id.toString()));
                                tNoticeMessage.setType(EnumNoticeMessageType.NOTICE.index);
                                tNoticeMessage.setUserType(2);
                                return tNoticeMessage;
                            }).collect(Collectors.toList());
                            itNoticeMessageService.insertList(tNoticeMessages);
                        }
                    }
                }
            });
            thread.start();
        }
        return SUCCESS_TIP;
    }
 
    /**
     * 删除/批量删除
     */
    @Permission
    @ResponseBody
    @BussinessLog(value = "删除/批量删除")
    @RequestMapping(value = "/delete")
    public Object delete(@RequestParam String ids) {
        // 逻辑删除
        tNoticeService.updateForSet("is_delete = 1", new EntityWrapper<TNotice>().in("id", ids.split(",")));
        //删除公告信息
        this.itNoticeMessageService.delete(new EntityWrapper<TNoticeMessage>().in("other_id",ids).eq("type",EnumNoticeMessageType.NOTICE.index));
        return SUCCESS_TIP;
    }
 
    /**
     * 修改公告
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TNotice tNotice) {
        if (tNotice != null) {
            if (ToolUtil.isNotEmpty(tNotice.getContent())) {
                // H5页面代码包装(判断加上自适应代码)
                tNotice.setContent(ToolUtil.h5Warpper(tNotice.getContent()));
            }
            tNoticeService.updateById(tNotice);
            //消息来源 1-全部用户,2-全部商家
            if (tNotice.getSource().equals("1")){
                List<Object> ids = this.iMemUserService.selectObjs(new EntityWrapper<MemUser>().setSqlSelect("id").eq("is_delete",0));
                if (CollectionUtils.isNotEmpty(ids)){
                    List<TNoticeMessage> tNoticeMessages = new ArrayList<TNoticeMessage>();
                    tNoticeMessages.addAll(ids.stream().map(id->{
                        TNoticeMessage tNoticeMessage = new TNoticeMessage();
                        tNoticeMessage.setContent(tNotice.getTitle());
                        tNoticeMessage.setCreateTime(tNotice.getCreateTime());
                        tNoticeMessage.setIsDelete(0);
                        tNoticeMessage.setIsRead(0);
                        tNoticeMessage.setOtherId(tNotice.getId().toString());
                        tNoticeMessage.setShowImg(tNotice.getCoverPlan());
                        tNoticeMessage.setUserId(new Integer(id.toString()));
                        tNoticeMessage.setType(EnumNoticeMessageType.NOTICE.index);
                        tNoticeMessage.setUserType(1);
                        return tNoticeMessage;
                    }).collect(Collectors.toList()));
                    this.itNoticeMessageService.insertList(tNoticeMessages);
                }
 
            }else{
                List<Object> ids = this.iMemMerchantService.selectObjs(new EntityWrapper<MemMerchant>().setSqlSelect("id").eq("is_delete",0));
                if (CollectionUtils.isNotEmpty(ids)){
                    List<TNoticeMessage> tNoticeMessages = new ArrayList<TNoticeMessage>();
                    tNoticeMessages.addAll(ids.stream().map(id->{
                        TNoticeMessage tNoticeMessage = new TNoticeMessage();
                        tNoticeMessage.setContent(tNotice.getTitle());
                        tNoticeMessage.setCreateTime(tNotice.getCreateTime());
                        tNoticeMessage.setIsDelete(0);
                        tNoticeMessage.setIsRead(0);
                        tNoticeMessage.setOtherId(tNotice.getId().toString());
                        tNoticeMessage.setShowImg(tNotice.getCoverPlan());
                        tNoticeMessage.setUserId(new Integer(id.toString()));
                        tNoticeMessage.setType(EnumNoticeMessageType.NOTICE.index);
                        tNoticeMessage.setUserType(2);
                        return tNoticeMessage;
                    }).collect(Collectors.toList()));
                    this.itNoticeMessageService.insertList(tNoticeMessages);
                }
 
            }
        }
        return SUCCESS_TIP;
    }
 
    /**
     * 修改公告状态
     */
    @RequestMapping(value = "/updateState")
    @ResponseBody
    public Object updateState(@RequestParam Integer tNoticeId) {
        TNotice obj = tNoticeService.selectById(tNoticeId);
        if (obj != null) {
            if(obj.getIsTop() == 0) {
                obj.setIsTop(1);
            } else {
                obj.setIsTop(0);
            }
            tNoticeService.updateById(obj);
        }
        return SUCCESS_TIP;
    }
 
 
    /**
     * 公告详情
     */
    @RequestMapping(value = "/detail/{tNoticeId}")
    @ResponseBody
    public Object detail(@PathVariable("tNoticeId") Integer tNoticeId) {
        return tNoticeService.selectById(tNoticeId);
    }
}