riku
2021-09-24 4f1b7973c53b45f57e451191bfd5a3d2136a004c
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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.domain.entity.LedgerMediaFile
import cn.flightfeather.supervision.domain.entity.LedgerRecord
import cn.flightfeather.supervision.domain.entity.LedgerSubType
import cn.flightfeather.supervision.domain.enumeration.LedgerCheckStatus
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.LedgerMediaFileMapper
import cn.flightfeather.supervision.domain.mapper.LedgerRecordMapper
import cn.flightfeather.supervision.domain.mapper.LedgerSubTypeMapper
import cn.flightfeather.supervision.domain.mapper.UserinfoMapper
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.infrastructure.utils.FileUtil
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.LedgerService
import cn.flightfeather.supervision.lightshare.vo.LedgerSubTypeVo
import cn.flightfeather.supervision.lightshare.vo.LedgerVo
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.servlet.http.HttpServletResponse
import kotlin.collections.ArrayList
import kotlin.math.ceil
 
@Service
class LedgerServiceImpl(
        val ledgerSubTypeMapper: LedgerSubTypeMapper,
        val userinfoMapper: UserinfoMapper,
        val ledgerRecordMapper: LedgerRecordMapper,
        val ledgerMediaFileMapper: LedgerMediaFileMapper
):LedgerService {
 
    override fun getLedgerType(sceneType: Int): ArrayList<LedgerSubTypeVo> {
        val ledgerSubTypes = ledgerSubTypeMapper.selectByExample(Example(LedgerSubType::class.java).apply {
            if (sceneType != SceneType.NoType.value) {
                createCriteria().andEqualTo("lScenetype", sceneType)
            }
        })
        val resultList = ArrayList<LedgerSubTypeVo>()
        ledgerSubTypes.forEach {
            val l = LedgerSubTypeVo(
                    it.lsSubtypeid,
                    it.lsName,
                    it.getlTypeid(),
                    it.getlTypename(),
                    needUpdate = it.getlNeedupdate(),
                    sceneType = it.getlScenetype(),
                    iconUrl = it.getlIconurl()
            )
            resultList.add(l)
        }
        return resultList
    }
 
    override fun getUserLedgerSummary(userId: String, sceneType: Int, time: String): List<LedgerSubTypeVo> {
        val ledgerSubTypes = ledgerSubTypeMapper.selectByExample(Example(LedgerSubType::class.java).apply {
            if (sceneType != SceneType.NoType.value) {
                createCriteria().andEqualTo("lScenetype", sceneType)
            }
        })
        val records = getLedgerRecords(userId, null, sceneType, time)
        val resultList = mutableListOf<LedgerSubTypeVo>()
        ledgerSubTypes.forEach {
            val l = LedgerSubTypeVo(
                    it.lsSubtypeid,
                    it.lsName,
                    it.getlTypeid(),
                    it.getlTypename(),
                    needUpdate = it.getlNeedupdate(),
                    sceneType = it.getlScenetype(),
                    iconUrl = it.getlIconurl()
            )
            for (r in records) {
                if (l.ledgerSubTypeId == r.lsSubtypeid) {
                    l.ledgerFinished = true
                    l.upLoad = true
                    l.checkStatus = r.lrVerifyrst?.toIntOrNull() ?: LedgerCheckStatus.UnCheck.value
                    break
                }
            }
            resultList.add(l)
        }
 
        return resultList
    }
 
    override fun getLedgerDetail(userId: String, ledgerSubTypeId: Int?, sceneType: Int,
                                 startTime: String, endTime: String,
                                 page: Int?, perPage: Int, response: HttpServletResponse
    ): ArrayList<LedgerVo> {
        val result = ArrayList<LedgerVo>()
 
        response.setIntHeader("totalPage", 1)
        response.setIntHeader("currentPage", page ?: 1)
 
        //fixme 此处暂时强制为1页
        if (page != null && page > 1) {
            return result
        }
 
        val startDate = DateUtil().StringToDate(startTime)
        val endDate = DateUtil().StringToDate(endTime)
        val cal = Calendar.getInstance().apply { time = startDate }
        val year = cal.get(Calendar.YEAR)
        val month = cal.get(Calendar.MONTH) + 1
 
//        val records = ledgerRecordMapper.selectByUser(userId, year, month.toByte(), ledgerSubTypeId,
//                sceneType.toString(), page?.minus(1)?.times(perPage), perPage)
 
        val records = ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
            createCriteria().andEqualTo("lrSubmitid", userId)
                    .andEqualTo("lrYear", year)
                    .andEqualTo("lrMonth", month.toByte())
                    .apply {
                        ledgerSubTypeId?.let { andEqualTo("lsSubtypeid", it) }
                    }
            if (sceneType == SceneType.Restaurant.value) {
                and(createCriteria().andIsNull("lrExtension1")
                        .orEqualTo("lrExtension1", sceneType.toString()))
            }else if (sceneType != SceneType.NoType.value) {
                and(createCriteria().andEqualTo("lrExtension1", sceneType.toString()))
            }
        })
 
        records.forEach {
            val media = ledgerMediaFileMapper.selectByExample(
                    Example(LedgerMediaFile::class.java).apply {
                        createCriteria().andEqualTo("lrGuid", it.lrGuid)
                                .andEqualTo("mfIsdelete", false)
                                .andEqualTo("mfFiletype", it.lrEasubmitkind)
                        orderBy("mfSavetime").desc()
                    }
            )
            val type = ledgerSubTypeMapper.selectByExample(
                    Example(LedgerSubType::class.java).apply {
                        createCriteria().andEqualTo("lsSubtypeid", it.lsSubtypeid)
                    }
            )
            result.add(LedgerVo().apply {
                id = it.lrGuid
                this.ledgerSubTypeId = it.lsSubtypeid
                ledgerName = it.lsSubtypename
                ledgerTypeId = type?.get(0)?.getlTypeid()
                ledgerType = type?.get(0)?.getlTypename()
                ledgerFinished = true
                isUpLoad = true
                updateDate = it.lrSubmitdate
                updateType = it.lrUpdatetype
                fileType = it.lrEasubmitkind.toInt()
                path1 = media?.get(0)?.mfPath1
                remark1 = media?.get(0)?.mfDescription1
                path2 = media?.get(0)?.mfPath2
                remark2 = media?.get(0)?.mfDescription2
                this.sceneType = if(it.lrExtension1 == null) SceneType.Restaurant.value else it.lrExtension1.toInt()
            })
        }
 
//        val counts = ledgerRecordMapper.selectCountByExample(Example(LedgerRecord::class.java))
//        val totalPage = Math.ceil(counts.toDouble() / perPage.toDouble()).toInt()
 
        return result
 
    }
 
    override fun getLedgerDetail2(userId: String, ledgerSubTypeId: Int?, sceneType: Int, time: String): List<LedgerVo> {
        val records = getLedgerRecords(userId, ledgerSubTypeId, sceneType, time)
        val result = ArrayList<LedgerVo>()
        records.forEach {
            val media = ledgerMediaFileMapper.selectByExample(
                    Example(LedgerMediaFile::class.java).apply {
                        createCriteria().andEqualTo("lrGuid", it.lrGuid)
                                .andEqualTo("mfIsdelete", false)
                                .andEqualTo("mfFiletype", it.lrEasubmitkind)
                        orderBy("mfSavetime").desc()
                    }
            )
            val type = ledgerSubTypeMapper.selectByExample(
                    Example(LedgerSubType::class.java).apply {
                        createCriteria().andEqualTo("lsSubtypeid", it.lsSubtypeid)
                    }
            )
            result.add(LedgerVo().apply {
                id = it.lrGuid
                this.ledgerSubTypeId = it.lsSubtypeid
                ledgerName = it.lsSubtypename
                ledgerTypeId = type?.get(0)?.getlTypeid()
                ledgerType = type?.get(0)?.getlTypename()
                ledgerFinished = true
                isUpLoad = true
                updateDate = it.lrSubmitdate
                updateType = it.lrUpdatetype
                fileType = it.lrEasubmitkind.toInt()
                path1 = media?.get(0)?.mfPath1
                remark1 = media?.get(0)?.mfDescription1
                path2 = media?.get(0)?.mfPath2
                remark2 = media?.get(0)?.mfDescription2
                this.sceneType = if(it.lrExtension1 == null) SceneType.Restaurant.value else it.lrExtension1.toInt()
            })
        }
        return result
    }
 
    override fun uploadLedger(userId: String, ledgerVoList: String, files: Array<MultipartFile>):Boolean {
 
        val mapper = ObjectMapper()
 
        val ledgerVos = mapper.readValue<List<LedgerVo>>(ledgerVoList, object :TypeReference<List<LedgerVo>>(){})
 
        ledgerVos.forEach {
            val userInfo = userinfoMapper.selectByPrimaryKey(userId)
            val today = Calendar.getInstance().apply { time = Date() }
                    .get(Calendar.DAY_OF_MONTH)
            val cal = Calendar.getInstance().apply { time = it.updateDate }
            val updateYear = cal.get(Calendar.YEAR)
            val updateMonth = cal.get(Calendar.MONTH) + 1
            val updateDay = cal.get(Calendar.DAY_OF_MONTH)
            //生成台账更新记录
            val ledgerRecord = LedgerRecord().apply {
                lrGuid = it.id
                lsSubtypeid = it.ledgerSubTypeId
                lsSubtypename = it.ledgerName
                lrYear = updateYear
                lrMonth = updateMonth.toByte()
                lrDay = updateDay.toByte()
                lrEasubmitkind = it.fileType?.toByte() ?: 1
                lrSubmitid = userId
                lrSubmitname = userInfo?.acountname ?: ""
                lrIssubmitontime = today <= 10
                lrSubmitdate = Date()
                lrUpdatetype = it.updateType
                lrExtension1 = it.sceneType.toString()
            }
 
            //检查数据库是否已有记录,选择插入或更新
            val tmp = ledgerRecordMapper.selectByPrimaryKey(ledgerRecord.lrGuid)
            if (tmp == null || tmp.lrGuid == null) {
                ledgerRecordMapper.insert(ledgerRecord)
            } else {
                ledgerRecordMapper.updateByPrimaryKey(ledgerRecord)
            }
 
            //对每张图片生成相应的路径并保存
            var picPath = ""
            val time = DateUtil().DateToString(Date(), DateUtil.DateStyle.YYYY_MM)
            files.forEach {file->
                val fileName = file.originalFilename
                //TODO 此处的文件路径需要修改为动态配置
                val basePath = "D:/02product/05ledger/images/"
                val path = "$time/$userId/${it.ledgerName}/"
                picPath += if (picPath.isEmpty()) {
                    "$path$fileName"
                } else {
                    ";$path$fileName"
                }
                try {
                    //调用文件保存方法
                    FileUtil().uploadFile(file.bytes, basePath + path, fileName!!)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
 
            //生成一条多媒体文件记录
            val ledgerMedia = LedgerMediaFile().apply {
                mfGuid = UUIDGenerator.generate16ShortUUID()
                lrGuid = it.id
                mfFiletype = it.fileType
                mfPath1 = picPath
                mfDescription1 = it.remark1
                mfSavetime = Date()
                mfIsdelete = false
            }
 
            //查询是否已有记录,对已有记录将其标记为"删除"
            val example = Example(LedgerMediaFile::class.java)
            val criteria = example.createCriteria()
            criteria.andEqualTo("lrGuid", it.id)
                    .andEqualTo("mfIsdelete", false)
            val result = ledgerMediaFileMapper.selectByExample(example)
            result.forEach {mediaFile ->
                mediaFile.mfIsdelete = true
                ledgerMediaFileMapper.updateByPrimaryKey(mediaFile)
            }
 
            //插入新的多媒体文件记录数据
            ledgerMediaFileMapper.insert(ledgerMedia)
 
            return true
        }
 
        return false
    }
 
    override fun getLedgerImg(userId: String, ledgerType: Int): List<String> {
        val result = mutableListOf<String>()
 
        PageHelper.startPage<LedgerRecord>(1, 1)
        val record = ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
            createCriteria().andEqualTo("lsSubtypeid", ledgerType)
                    .andEqualTo("lrSubmitid", userId)
            orderBy("lrYear").desc().orderBy("lrMonth").desc().orderBy("lrDay").desc()
        }).takeIf { it.isNotEmpty() }?.get(0)
 
        record?.let {
            ledgerMediaFileMapper.selectByExample(Example(LedgerMediaFile::class.java).apply {
                createCriteria().andEqualTo("lrGuid", it.lrGuid)
            }).forEach {
                it.mfPath1?.split(";")?.let {list ->
                    result.addAll(list)
                }
            }
        }
 
        return result
    }
 
    override fun getLedgerImgs(userId: String, ledgerType: List<Int>): List<LedgerVo> {
        val result = mutableListOf<String>()
 
        PageHelper.startPage<LedgerRecord>(1, 1)
        val record = ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
            createCriteria().andIn("lsSubtypeid", ledgerType)
                    .andEqualTo("lrSubmitid", userId)
            orderBy("lrYear").desc().orderBy("lrMonth").desc().orderBy("lrDay").desc()
        })
 
        return emptyList()
    }
 
    private fun getLedgerRecords(userId: String, ledgerSubTypeId: Int?, sceneType: Int, time: String): List<LedgerRecord> {
        val ledgerSubTypes = ledgerSubTypeMapper.selectByExample(Example(LedgerSubType::class.java).apply {
            if (ledgerSubTypeId != null) {
                createCriteria().andEqualTo("lsSubtypeid", ledgerSubTypeId)
            } else if (sceneType != SceneType.NoType.value) {
                createCriteria().andEqualTo("lScenetype", sceneType)
            }
        })
        val c = Calendar.getInstance().apply { this.time = DateUtil().StringToDate(time) }
        val year = c.get(Calendar.YEAR)
        val month = c.get(Calendar.MONTH) + 1
        val map = mutableMapOf<Int, MutableList<Int>>()
        ledgerSubTypes.forEach {
            val p = it.getlPeriod()
            if (!map.containsKey(p)) {
                map[p] = mutableListOf()
            }
            map[p]?.add(it.lsSubtypeid)
        }
        val records = mutableListOf<LedgerRecord>()
        map.forEach { (p, v) ->
            // FIXME: 2020/11/10  此处根据周期和当前月份计算得到当前月份所在周期的始末月,只适用于周期小于等于12个月的情况。后续待修改
            val startMon = ceil(month.toDouble() / p).toInt().minus(1).times(p).plus(1)
            val endMon = startMon + p - 1
            val r = ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                createCriteria().andEqualTo("lrSubmitid", userId)
                        .andEqualTo("lrYear", year)
                        .andGreaterThanOrEqualTo("lrMonth", startMon.toByte())
                        .andLessThanOrEqualTo("lrMonth", endMon.toByte())
                and(
                        createCriteria().apply {
                            v.forEach {
                                orEqualTo("lsSubtypeid", it)
                            }
                        }
                )
            })
            records.addAll(r)
        }
 
        return records
    }
}