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
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.domain.ds1.entity.Inspection
import cn.flightfeather.supervision.domain.ds1.mapper.InspectionMapper
import cn.flightfeather.supervision.common.utils.DateUtil
import cn.flightfeather.supervision.common.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.*
import cn.flightfeather.supervision.lightshare.vo.*
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
 
@Service
class InspectionServiceImpl(val inspectionMapper: InspectionMapper) : InspectionService {
 
    @Autowired
    lateinit var problemlistService: ProblemlistService
 
    @Autowired
    lateinit var gitlistService: GitlistService
 
    @Autowired
    lateinit var mediafileService: MediafileService
 
    @Autowired
    lateinit var itemevaluationService: ItemevaluationService
 
    @Autowired
    lateinit var subtaskService: SubtaskService
 
    private val dateUtil = DateUtil()
 
    //获取污染场景版本主页的监管情况展示内容
    override fun getInspectionInfoByScene(sceneId: String, topTaskId: String): InspectionInfoVo {
        val maps = inspectionMapper.getInspectionInfoByScene(sceneId, topTaskId)
        val inspectionInfoVo = InspectionInfoVo()
        if (maps.isNotEmpty()) {
            val map = maps[0]
            if (map.isNotEmpty()) {
                inspectionInfoVo.topTaskid = map["topTaskId"].toString()
                inspectionInfoVo.topTaskName = map["topTaskName"].toString()
                inspectionInfoVo.sceneId = map["sceneId"].toString()
                inspectionInfoVo.sceneName = map["sceneName"].toString()
                inspectionInfoVo.inspectionId = map["inspectionId"].toString()
                inspectionInfoVo.subtaskId = map["subtaskId"].toString()
                inspectionInfoVo.inspected = map["isInspected"].toString().equals("1")
                map["inspectionTime"]?.let {
                    val time = it.toString()
                    inspectionInfoVo.inspectionTime = dateUtil.StringToString(time, DateUtil.DateStyle.YYYY_MM_DD)
                }
                inspectionInfoVo.inspectionTimes = map["inspectionTimes"].toString().toInt()
                inspectionInfoVo.promised = map["isPromised"].toString().equals("1")
                inspectionInfoVo.changed = map["isChanged"].toString().equals("1")
                inspectionInfoVo.unChangedCount = map["unChangedCount"].toString().toInt()
                inspectionInfoVo.changedCount = map["changedCount"].toString().toInt()
                inspectionInfoVo.promisedTime = map["promisedTime"].toString()
            }
        }
        return inspectionInfoVo
    }
 
    //根据巡查ID获取问题列表
    override fun findProblemListById(id: String): MutableList<ProblemlistVo> {
 
        val problemList = mutableListOf<ProblemlistVo>()
        val inspection = inspectionMapper.selectByPrimaryKey(id)
        if (inspection != null) {
            val problemListVo = problemlistService.findByInspectionID(inspection.guid!!)
            //判断是否有问题列表
            if (!problemListVo.isEmpty()) {
                //根据每个问题,获取媒体文件
                problemListVo.forEach {
                    val mediafileVo = mediafileService.findByBusinessGUID(it.guid!!)
                    //判断是否有媒体资料
                    if (!mediafileVo.isEmpty()) {
                        //赋值
                        it.mediafileList = mediafileVo
                    } else {
                        it.mediafileList = mutableListOf()
                    }
                    problemList.add(it)
                }
            }
        }
        //problemList.sortBy { it.time }
        return problemList
 
    }
 
    //根据日期和场景ID,获取上一次问题列表
    override fun findLastProblemListBySenceId(id: String, date: String): ProblemlistsubtastVo {
        //初始化返回值
        val problemlistsubtastVo = ProblemlistsubtastVo()
        //构建查询条件
        val example = Example(Inspection::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("sguid", id)
        criteria.andLessThan("executionstarttime", DateUtil().StringToDate(date))
        //添加巡查按执行时间排序*****
        example.orderBy("executionstarttime").desc()
        //**************************
        //根据条件查询
        val inspections = inspectionMapper.selectByExample(example)
        //判断结果
        if (!inspections.isEmpty()) {
            val pblemList = findProblemListById(inspections[0].guid.toString())
            val subtaskVo = subtaskService.findByID(inspections[0].stguid!!)
            problemlistsubtastVo.problemlistVo = pblemList
            problemlistsubtastVo.subtaskVo = subtaskVo
        }
        return problemlistsubtastVo
    }
 
    //根据子任务ID获取巡查信息
    override fun findBySubTaskID(subTaskID: String): InspectionVo {
        val inspectionVo = InspectionVo()
        //创建查询条件
        val inspection = Inspection()
        inspection.stguid = subTaskID
        //根据inspection获取数据
        val inspectionlist = inspectionMapper.selectOne(inspection)
        //如果为空,根据subtaskID操作
        if (inspectionlist == null) {
            //根据guid查询子任务
            val subtaskvo = subtaskService.findByID(subTaskID)
            //如果子任务不存在,返回
            if (subtaskvo.stguid == null) {
                return inspectionVo
            }
            //如果存在,创建新的巡查
            val guid = UUIDGenerator.generate16ShortUUID()
            inspection.guid = guid
            inspection.sguid = subtaskvo.scenseid
            inspection.scensename = subtaskvo.scensename
            inspection.scenseaddress = subtaskvo.scenseaddress
            //插入数据库
            inspectionMapper.insert(inspection)
            //返回值赋值
            BeanUtils.copyProperties(inspection, inspectionVo)
            inspectionVo.problemList = mutableListOf()
            inspectionVo.gitList = mutableListOf()
            inspectionVo.itemevaluationList = mutableListOf()
            //返回
            return inspectionVo
        } else {
            BeanUtils.copyProperties(inspectionlist, inspectionVo)
        }
 
        //根据子任务ID获取问题列表
        val problemListVo = problemlistService.findBySubtaskId(subTaskID)
        //判断是否有问题列表
        if (!problemListVo.isEmpty()) {
            //赋值
            inspectionVo.problemList = problemListVo
            //根据每个问题,获取媒体文件
            problemListVo.forEach {
                val mediafileVo = mediafileService.findByBusinessGUID(it.guid!!)
                //判断是否有媒体资料
                if (!mediafileVo.isEmpty()) {
                    //赋值
                    it.mediafileList = mediafileVo
                }
            }
        } else {
            inspectionVo.problemList = mutableListOf()
        }
        //根据子任务ID获取技防措施列表
        val gitListVo = gitlistService.findBySubTaskID(subTaskID)
        //判断是否有技防措施列表
        if (!gitListVo.isEmpty()) {
            //赋值
            inspectionVo.gitList = gitListVo
            //根据每个技防措施获取媒体文件
            gitListVo.forEach {
                val mediafileVo = mediafileService.findByBusinessGUID(it.guid!!)
                //判断是否为空
                if (!mediafileVo.isEmpty()) {
                    //赋值
                    it.mediafileList = mediafileVo
                }
            }
        } else {
            inspectionVo.gitList = mutableListOf()
        }
 
        //根据子任务id获取单项考核评估
//        val itemevaluation = itemevaluationService.findBySubTaskID(subTaskID)
//        if (!itemevaluation.isEmpty()) {
//            inspectionVo.itemevaluationList = itemevaluation
//        } else {
//            inspectionVo.itemevaluationList = mutableListOf()
//        }
 
        //返回
        return inspectionVo
    }
 
    //根据ID查询
    override fun findByID(id: String): InspectionVo {
        val inspectionVo = InspectionVo()
        val inspection = inspectionMapper.selectByPrimaryKey(id)
        if (inspection != null) {
            BeanUtils.copyProperties(inspection, inspectionVo)
        }
 
        return inspectionVo
    }
 
    //根据巡查ID获取技防措施列表
    override fun findGitListById(id: String): List<GitlistVo> {
        val gitList = mutableListOf<GitlistVo>()
        val inspection = inspectionMapper.selectByPrimaryKey(id)
        if (inspection != null) {
            val gitListVo =gitlistService.findByInspectionID(inspection.guid!!)
            //判断是否有问题列表
            if (!gitListVo.isEmpty()) {
                //根据每个问题,获取媒体文件
                gitListVo.forEach {
                    val mediafileVo = mediafileService.findByBusinessGUID(it.guid!!)
                    //判断是否有媒体资料
                    if (!mediafileVo.isEmpty()) {
                        //赋值
                        it.mediafileList = mediafileVo
                    } else {
                        it.mediafileList = mutableListOf()
                    }
                    gitList.add(it)
                }
            }
        }
        //problemList.sortBy { it.time }
        return gitList
    }
 
    override fun findAll(): MutableList<Inspection> = inspectionMapper.selectAll()
 
    override fun save(inspection: Inspection): Int = inspectionMapper.insert(inspection)
 
    override fun update(inspection: Inspection): Int = inspectionMapper.updateByPrimaryKey(inspection)
 
    override fun delete(id: String): Int = inspectionMapper.deleteByPrimaryKey(id)
 
    override fun getStatistic(topTaskId: String, sceneTypeId: String): BaseResponse<List<InspectionStatisticVo>> {
        val result = inspectionMapper.getStatistic(topTaskId, sceneTypeId)
        return if (result.isNotEmpty()) {
            BaseResponse(true, data = result)
        } else {
            BaseResponse(false)
        }
    }
}