feiyu02
2022-10-21 f22c4b9230808fed4fec80c435eccb4c833349a0
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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.net.WXHttpService
import cn.flightfeather.supervision.common.wx.MessageWxVo
import cn.flightfeather.supervision.common.wx.TemplateManager
import cn.flightfeather.supervision.common.wx.WxTokenManager
import cn.flightfeather.supervision.domain.entity.*
import cn.flightfeather.supervision.domain.enumeration.DistrictType
import cn.flightfeather.supervision.domain.enumeration.UserType
import cn.flightfeather.supervision.lightshare.repository.MeetingParticipantRepository
import cn.flightfeather.supervision.lightshare.service.NotificationService
import cn.flightfeather.supervision.lightshare.vo.NoticeReadStateVo
import cn.flightfeather.supervision.lightshare.vo.NotificationVo
import cn.flightfeather.supervision.push.PushService
import cn.flightfeather.supervision.domain.enumeration.ParticipantType
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
import com.alibaba.fastjson.JSON
import com.flightfeather.taizhang.model.enumeration.NotificationType
import com.flightfeather.taizhang.model.enumeration.WorkSubType
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.lang.StringBuilder
import java.util.*
import java.util.concurrent.Executors
import javax.servlet.http.HttpServletResponse
 
@Service
class NotificationServiceImpl(
        val notificationMapper: NotificationMapper,
        val noticeMapper: NoticeMapper,
        val noticeReadStateMapper: NoticeReadStateMapper,
        val userinfoMapper: UserinfoMapper,
        val meetingParticipantRepository: MeetingParticipantRepository,
        val templateManager: TemplateManager
) : NotificationService {
 
    override fun getNotificationUnRead(userId: String, page: Int, per_page: Int, response: HttpServletResponse): List<NotificationVo> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return emptyList()
        val userTypeId = userInfo.usertypeid
        val district = userInfo.extension1
        var districtCode = DistrictType.getCode(district)
        var sceneTypeId = userInfo.extension2
 
        when (userTypeId?.toInt()) {
            UserType.Admin.value,
            UserType.Insider.value -> {
                districtCode = null
                sceneTypeId = null
            }
            UserType.Gov.value -> {
                sceneTypeId = null
            }
            UserType.Enterprise.value -> {
                //do nothing
            }
        }
 
        val myPage = PageHelper.startPage<Notice>(page, per_page)
 
        val noticeIdList = mutableListOf<String?>()
        val resultList = noticeMapper.getUnReadNotification(userId, districtCode, sceneTypeId).also {
            it.forEach {vo->
                noticeIdList.add(vo.id)
            }
        }
        if (noticeIdList.isEmpty()) {
            return resultList
        }
        val readStateList = noticeReadStateMapper.selectByExample(Example(NoticeReadState::class.java).apply {
            createCriteria().andEqualTo("nrUserid", userId)
                    .andIn("ecGuid", noticeIdList)
        })
 
        resultList.forEach {
            for (i in readStateList.indices) {
                val r = readStateList[i]
                if (it.id == r.ecGuid) {
                    it.hasRead = r.nrReadstate
                    it.hasSigned = r.nrSignstate
                    break
                }
            }
        }
 
        response.setIntHeader("totalPage", myPage.pages)
        response.setIntHeader("currentPage", myPage.pageNum)
 
        return resultList
    }
 
    override fun updateReadState(userId: String, readStates: List<NoticeReadStateVo>) {
        readStates.forEach {
            val noticeReadState = NoticeReadState().apply {
                nrUserid = userId
                ecGuid = it.noticeId
                nrReadstate = it.hasRead
                nrSignstate = it.hasSigned
                val example = Example(NoticeReadState::class.java)
                example.createCriteria().andEqualTo("nrUserid", nrUserid)
                        .andEqualTo("ecGuid", ecGuid)
                val result = noticeReadStateMapper.selectByExample(example)
                if (result.isEmpty()) {
                    noticeReadStateMapper.insert(this)
                } else {
                    nrId = result[0].nrId
                    noticeReadStateMapper.updateByPrimaryKey(this)
                }
            }
        }
    }
 
    override fun getNotificationText(notificationId: String): String {
        val notification = notificationMapper.selectByPrimaryKey(notificationId)
        return notification.ntContent
    }
 
//    override fun getUnReadNoticeNum(userId: String): Int {
//        val example = Example(NoticeReadState::class.java)
//        example.createCriteria()
//                .andEqualTo("nrUserid", userId)
//        example.and(example.createCriteria()
//                .andEqualTo("nrReadstate", true)
//                .andEqualTo("nrSignstate", true)
//        )
//
//        val readNum = noticeReadStateMapper.selectCountByExample(example)
//        val userInfo = userinfoMapper.selectByPrimaryKey(userId)
//        val totalNum = noticeMapper.selectByUser(userInfo?.extension2 ?: "-1",
//                userId, 1, 1000).size
////        noticeMapper.selectCountByExample(Example(Notice::class.java))
//
//        return totalNum - readNum
//    }
 
    override fun getUnReadNoticeNum(userId: String): Int {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return 0
        val userTypeId = userInfo.usertypeid
        val district = userInfo.extension1
        var districtCode = DistrictType.getCode(district)
        var sceneTypeId = userInfo.extension2
 
        when (userTypeId?.toInt()) {
            UserType.Admin.value,
            UserType.Insider.value -> {
                districtCode = null
                sceneTypeId = null
            }
            UserType.Gov.value -> {
                sceneTypeId = null
            }
            UserType.Enterprise.value -> {
                //do nothing
            }
        }
 
        val noticeIdList = mutableListOf<String?>()
        val resultList = noticeMapper.getUnReadNotification(userId, districtCode, sceneTypeId).also {
            it.forEach {vo->
                noticeIdList.add(vo.id)
            }
        }
        if (noticeIdList.isEmpty()) {
            return 0
        }
        val readStateList = noticeReadStateMapper.selectByExample(Example(NoticeReadState::class.java).apply {
            createCriteria().andEqualTo("nrUserid", userId)
        })
 
        var totalCount = resultList.size
        readStateList.forEach {
            if (noticeIdList.contains(it.ecGuid) && it.nrSignstate && it.nrReadstate) {
                totalCount--
            }
            if (totalCount < 0) {
                totalCount = 0
            }
        }
 
        return totalCount
    }
 
    override fun pushMeetingReleaseNotification(meetingVo: MeetingInfo, roomVo: VMRoom?, userId: String, title: String, body: String) {
        val accountList = mutableListOf<String>()
        meetingParticipantRepository.getParticipantByType(meetingVo.miGuid
                ?: "", roomVo?.vmrGuid, ParticipantType.All.value).forEach {
            if (it.mpParticipantid != userId) {
                accountList.add(it.mpParticipantid)
            }
        }
//        Executors.newSingleThreadExecutor().execute {
//            PushService().run {
//                pushByAccount(accountList)
//                push(meetingVo.miTitle ?: title, body)
//            }
//        }
        val receiverId = StringBuilder()
        accountList.forEach {
            receiverId.append(it)
            receiverId.append(";")
        }
 
        val user = userinfoMapper.selectByPrimaryKey(userId)
 
        val notificationVo = NotificationVo(
                UUIDGenerator.generate16ShortUUID(),
                user.guid,
                user.realname,
                user.headIconUrl,
                NotificationType.Work.value.toString(),
                NotificationType.Work.des,
                WorkSubType.Meeting.value.toString(),
                WorkSubType.Meeting.des,
                meetingVo.miTitle ?: title,
                body,
                receiverType = "-1",
                receiverId = receiverId.toString(),
                district = "-1"
        )
 
        releaseNotice(userId, notificationVo)
    }
 
    override fun releaseNotice(userId: String, noticeVo: NotificationVo): Boolean {
        val notice = Notice().apply {
            noticeVo.let {
                ecGuid = it.id
                ecNoticetype = it.typeId?.toInt()
                ecNoticetypename = it.typeName
                ecNoticesubtype = it.subTypeId?.toInt()
                ecNoticesubtypename = it.subTypeName
                ecNoticetitle = it.title
                ecNoticecontent = it.content
                ecIsinuse = true
                ecCreatedate = Date()
                ecCreatorid = it.authorId
                ecCreator = it.authorName
                ecUpdatedate = Date()
                ecModifierid = it.authorId
                ecModifier = it.authorName
                ecReceivertype = it.receiverType
                ecReceiverid = it.receiverId
                ecPicurl = it.picUrl
                ecBodyurl = it.bodyUrl
                ecNeedsign = it.needSigned
                ecExtension1 = it.district
            }
        }
        Executors.newSingleThreadExecutor().execute {
            PushService().run {
                if (noticeVo.receiverId?.isNotBlank() == true) {
                    val accountList = noticeVo.receiverId!!.split(";")
                    pushByAccount(accountList)
                } else {
                    val accountList = mutableListOf<String>()
                    val districts = mutableListOf<String>()
                    val sceneTypes = mutableListOf<String>()
                    noticeVo.district?.split(";")?.forEach {
                        if (it.isNotBlank()) {
                            districts.add(it)
                        }
                    }
                    noticeVo.receiverType.split(";")?.forEach {
                        if (it.isNotBlank()) {
                            sceneTypes.add(it)
                        }
                    }
                    val districtCodeList = mutableListOf<String>()
                    districts.forEach {
                        DistrictType.getDes(it)?.let { d-> districtCodeList.add(d)}
                    }
                    userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
                        createCriteria()
                                .apply {
                                    if (districts.isNotEmpty()) {
                                        andIn("extension1", districtCodeList)
                                    }
                                    if (noticeVo.receiverType != SceneType.NoType.value.toString()) {
                                        andIn("extension2", sceneTypes)
                                    }
                                }
                    }).forEach {
                        accountList.add(it.guid ?: "")
                    }
                    pushByAccount(accountList)
                }
                push(noticeVo.title?:"新通知", noticeVo.content?:"新通知")
            }
        }
        return noticeMapper.insert(notice) == 1
    }
 
    override fun pushMsgWx(templateId: Int): String {
        val res = templateManager.sendMsg(templateId, "otZkc5VRlwauEMPqMluQYdVa4zuE", listOf("台账上传", "2022年10月10日", "3", "请重点关注现场自寻查部分"))
        return if (res) "success" else "fail"
    }
}