feiyu02
2022-07-20 39e208b6b0482a25c77e53590087c02d9d937563
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
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.domain.ds1.entity.Scense
import cn.flightfeather.supervision.domain.ds1.entity.Userinfo
import cn.flightfeather.supervision.domain.ds1.mapper.ScenseMapper
import cn.flightfeather.supervision.domain.ds1.mapper.UserinfoMapper
import cn.flightfeather.supervision.domain.ds2.entity.LedgerMediaFile
import cn.flightfeather.supervision.domain.ds2.entity.LedgerRecord
import cn.flightfeather.supervision.domain.ds2.entity.LedgerSubType
import cn.flightfeather.supervision.domain.ds2.entity.UserMap
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerMediaFileMapper
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerRecordMapper
import cn.flightfeather.supervision.domain.ds2.mapper.LedgerSubTypeMapper
import cn.flightfeather.supervision.domain.ds2.mapper.UserMapMapper
import cn.flightfeather.supervision.lightshare.service.LedgerService
import cn.flightfeather.supervision.lightshare.vo.BaseResponse
import cn.flightfeather.supervision.lightshare.vo.CopyLedgerVo
import cn.flightfeather.supervision.lightshare.vo.SceneLedgerSummary
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.util.*
 
@Service
class LedgerServiceImpl(
    private val scenseMapper: ScenseMapper,
    private val userMapMapper: UserMapMapper,
    private val userinfoMapper: UserinfoMapper,
    private val ledgerSubTypeMapper: LedgerSubTypeMapper,
    private val ledgerRecordMapper: LedgerRecordMapper,
    private val ledgerMediaFileMapper: LedgerMediaFileMapper
) : LedgerService {
 
 
    override fun getLedgerSummary(time: String, districtCode: String, sceneType: Byte): List<SceneLedgerSummary> {
        val result = mutableListOf<SceneLedgerSummary>()
        val year = time.split("-")[0]
        val month = time.split("-")[1]
        scenseMapper.selectByExample(Example(Scense::class.java).apply {
            createCriteria().andEqualTo("districtcode", districtCode)
                .andEqualTo("typeid", sceneType)
                .andNotEqualTo("extension1", "0")
        }).forEach {
            // 记录场景信息
            val sceneLedgerSummary = SceneLedgerSummary().apply {
                scene = it
                this.time = time
            }
            // 查询本系统和飞羽环境系统的各自用户信息
            userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
                createCriteria().andEqualTo("dGuid", it.guid)
            })?.takeIf { i-> i.isNotEmpty() }?.let {
                val user = it[0]
                userMapMapper.selectByExample(Example(UserMap::class.java).apply {
                    createCriteria().andEqualTo("svUserId", user.guid)
                })?.takeIf { m ->m.isNotEmpty() }?.let { m->
                    // 记录飞羽环境系统中的用户id
                    sceneLedgerSummary.tzUserId = m[0].tzUserId
 
                    // 查询用户的台账完成情况
                    val typeCount = ledgerSubTypeMapper.selectCountByExample(Example(LedgerSubType::class.java).apply {
                        createCriteria().andEqualTo(
                            "lScenetype",
                            Constant.ScenseType.typeMap(sceneLedgerSummary.scene?.typeid)
                        )
                            .andEqualTo("lNeedupdate", true)
                    })
 
                    val ledgerCount = ledgerRecordMapper.getNeedUpdateCount(
                        sceneLedgerSummary.tzUserId,
                        year.toInt(),
                        month.toByte()
                    )
 
                    sceneLedgerSummary.summary = when (typeCount - ledgerCount) {
                        typeCount -> 0
                        in 1 until typeCount -> 1
                        0 -> 2
                        else -> 2
                    }
                }
            }
 
            result.add(sceneLedgerSummary)
        }
 
        return result
    }
 
    override fun copyLedger(userId: String, time: String, copyLedgerList: List<CopyLedgerVo>): BaseResponse<String> {
        val year = time.split("-")[0]
        val month = time.split("-")[1]
        copyLedgerList.forEach {
            //去重判断
            val r = ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                createCriteria().andEqualTo("lrSubmitid", userId)
                    .andEqualTo("lsSubtypeid", it.subTypeId)
                    .andEqualTo("lrYear", year)
                    .andEqualTo("lrMonth", month)
            })
            if (r.size > 0) {
                return@forEach
            }
 
            val y = it.time?.split("-")?.get(0) ?: return@forEach
            val m = it.time?.split("-")?.get(1) ?: return@forEach
            ledgerRecordMapper.selectByExample(Example(LedgerRecord::class.java).apply {
                createCriteria().andEqualTo("lrSubmitid", userId)
                    .andEqualTo("lsSubtypeid", it.subTypeId)
                    .andEqualTo("lrYear", y)
                    .andEqualTo("lrMonth", m)
            }).forEach record@ {lr ->
                //获取记录对应的文件信息
                val fileList = ledgerMediaFileMapper.selectByExample(Example(LedgerMediaFile::class.java).apply {
                    createCriteria().andEqualTo("lrGuid", lr.lrGuid)
                })
                if (fileList.size == 0) return@record
                val file = fileList[0] ?: return@record
 
                //修改记录信息,复制为新的台账
                lr.lrGuid = UUIDGenerator.generate16ShortUUID()
                lr.lrYear = year.toInt()
                lr.lrMonth = month.toByte()
                lr.lrIssubmitontime = true
                lr.lrSubmitdate = Date()
                lr.lrExtension2 = "copy"//表明是复制的台账
                ledgerRecordMapper.insert(lr)
 
                //修改对应的文件信息
                file.apply {
                    mfGuid = UUIDGenerator.generate16ShortUUID()
                    lrGuid = lr.lrGuid
                    mfSavetime = Date()
                }
                ledgerMediaFileMapper.insert(file)
            }
        }
 
        return BaseResponse(true)
    }
}