18034727885
2026-04-27 3a4c1bbb2cf4fb1cdda8a5061b0674f30e7456b4
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
package com.example.smart.ui.projectevent.widget
 
 
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
 
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.LinearLayout
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.basecore.util.showCenterToast
import com.example.basecore.util.toast
import com.example.smart.R
import com.example.smart.bean.CommonOptionsBean
import com.example.smart.bean.EntityRef
import com.example.smart.bean.IdRef
import com.example.smart.bean.ImageRef
import com.example.smart.databinding.IssueAddUpdateWidgetBinding
import com.example.smart.enumlist.ErrorLevel
import com.example.smart.image.ImageProvider
import com.example.smart.ui.icondraw.DrawLiveData
import com.example.smart.ui.item.adapter.ImageShowAdapter
import com.example.smart.ui.item.bean.ImageBean
import com.example.smart.ui.project.ProjectDetailsActivity
import com.example.smart.ui.project.issue.bean.IssueBean
import com.example.smart.ui.project.issue.bean.IssueDetailBean
import com.example.smart.ui.projectevent.bean.ProblemRecordBean
import com.example.smart.ui.projectevent.bean.ReviewBean
import com.example.smart.ui.projectevent.event.IIssueEvent
import com.example.smart.ui.projectevent.pop.IssueReviewPopupWindow
import com.example.smart.widget.common.ICommonDataEvent
import com.example.smart.widget.dialog.CommonDialog
import com.example.smart.widget.dialog.list.CommListDialog
import com.example.smart.widget.dialog.list.CommonListBean
 
 
/**
 * @description: 添加问题&修改问题view
 */
class IssueWidget @JvmOverloads constructor(
    val mContext: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : LinearLayout(mContext, attrs, defStyleAttr), IIssueEvent {
 
    private var onClickEvent: OnClickEvent? = null
 
    private var canReview = false
 
    private var issueDetailBean: IssueDetailBean? = null
    private var issueBean: IssueBean? = null
 
    private var dValue: Double = 0.0
    private var cValue: Double = 0.0
    private var rnValue: Double = 0.0
    private var taskId = -1
 
    private val specificTerms = ArrayList<CommonListBean>()
 
    fun bindEvent(onClickEvent: OnClickEvent) {
        this.onClickEvent = onClickEvent
    }
 
    private val mImageProvider by lazy {
        ImageProvider(mContext as Activity, ImageCallBack())
    }
 
    private inner class ImageCallBack : ImageProvider.IImageCallback {
 
        override fun onUploadSuccess(entityRef: List<ImageRef>) {
            adapter.notifyData(entityRef)
//            mImagePaths.addAll(entityRef)
        }
 
        override fun onUploadFailed() {
            mContext.showCenterToast("图片上传失败,请稍后再试...")
        }
    }
 
    private lateinit var adapter: ImageShowAdapter
 
    private val mImageList = ArrayList<ImageBean>()
 
//    private val mImagePaths = ArrayList<ImageRef>()
 
    private val binding by lazy(LazyThreadSafetyMode.NONE) {
        IssueAddUpdateWidgetBinding.bind(this)
    }
 
    init {
        LayoutInflater.from(mContext).inflate(R.layout.issue_add_update_widget, this)
        initView()
        addListener()
        DrawLiveData.observeForever { drawData ->
            adapter.updateDrawData(drawData)
        }
    }
 
    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        DrawLiveData.removeObserver { }
    }
 
 
    private fun initView() {
        orientation = VERTICAL
        with(binding) {
            recyclerView.layoutManager =
                LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
            adapter = ImageShowAdapter(mContext, mImageList)
            recyclerView.adapter = adapter
 
            recyclerView.isNestedScrollingEnabled = false
            recyclerView.setHasFixedSize(false)
 
            val listener = ItemClick()
            clauseWidget.getContentView().setOnClickListener {
                if (mContext is FragmentActivity) {
                    if (issueDetailBean == null) {
                        return@setOnClickListener
                    }
                    if (specificTerms.isEmpty()) {
                        mContext.showCenterToast("规范条款内容为空")
                        return@setOnClickListener
                    }
 
                    val listDialog = CommListDialog(specificTerms, listener)
                    listDialog.show(mContext.supportFragmentManager, "123")
 
//                    val dialog = Co
                    //                    mmonDialog()
//                        .setContent(specificItem.toString())
//                    dialog.show(mContext.supportFragmentManager, "123")
                }
            }
 
            reviewLayout.visibility = GONE
        }
    }
 
 
    private fun addListener() {
        binding.apply {
            takePhotoLayout.setOnClickListener {
                if (issueDetailBean == null) {
                    mContext.showCenterToast("数据错误,请稍后再试")
                    return@setOnClickListener
                }
                mImageProvider.showImageSelect()
            }
            deleteTv.setOnClickListener {
                if (mContext is ProjectDetailsActivity) {
                    //获取任务id
                    if (!(mContext).canUpdateTask()) {
                        return@setOnClickListener
                    }
                }
                val dialog = CommonDialog().setContent("确定要删除问题?").setCertainListener {
                    onClickEvent?.onDelete(issueBean?.id ?: "")
                }
                if (mContext is FragmentActivity) {
                    dialog.show(mContext.supportFragmentManager, "1")
                }
            }
 
            editTv.setOnClickListener {
 
                checkParams()?.let {
                    onClickEvent?.onEdit(it)
                }
//                if (binding.editTv.text == "编辑") {//防误触
//                    editTv.text = "保存"
//                    maskView.visibility = GONE
//                } else {
//                    checkParams()?.let {
//                        onClickEvent?.onEdit(it)
//                    }
//                }
            }
 
            submitTv.setOnClickListener {
                if (canReview) {
                    if (issueBean == null) {
                        mContext.toast("数据错误")
                        return@setOnClickListener
                    }
                    val pop = IssueReviewPopupWindow(mContext, issueBean!!.id)
                    pop.showPopupWindow()
                } else {
                    checkParams()?.let {
                        onClickEvent?.onAdd(it)
                    }
                }
            }
            //拦截底部点击事件
            maskView.setOnClickListener {
 
            }
 
            //人为不可以选择
//            levelWidget.setDataEvent(object : ICommonDataEvent {
//                override fun onClickView(containerId: String) {
//
//                }
//
//            })
//
            countWidget.setDataEvent(object : ICommonDataEvent {
                override fun onClickView(containerId: String) {
//                    cValue = containerId.toDouble()
                    cValue = issueDetailBean?.numberFactors?.get(containerId.toInt())?.value ?: 0.0
                    updateErrorLevel()
                }
 
            })
            scaleWidget.setDataEvent(object : ICommonDataEvent {
                override fun onClickView(containerId: String) {
//                    rnValue = containerId.toDouble()
                    rnValue = issueDetailBean?.scaleFactors?.get(containerId.toInt())?.factor ?: 0.0
                    updateErrorLevel()
                }
            })
//            clauseWidget.setDataEvent(object :ICommonDataEvent {
//                override fun onClickView(containerId: String) {
//
//                }
//            })
        }
    }
 
    private class ItemClick : CommListDialog.ItemClickListener {
        override fun onItemClick(bean: CommonListBean) {
 
        }
 
    }
 
    private fun checkParams(): ProblemRecordBean? {
        with(binding) {
            if (TextUtils.isEmpty(positionWidget.getContent())) {
                mContext.showCenterToast("请输入问题部位")
                return null
            }
        }
        if (issueDetailBean == null) {
            mContext.showCenterToast("数据错误,请稍后再试")
            return null
        }
        val issueParam = generateIssueParam(issueDetailBean!!.id)
        return issueParam
    }
 
 
    private fun generateIssueParam(id: Int): ProblemRecordBean {
        val array = mutableListOf<Int>()
        specificTerms.forEach {
            if (it.checked) {
                array.add(it.id.toInt())
            }
        }
        val numIndex = binding.countWidget.getContainerId().toInt()
        val scaleIndex = binding.scaleWidget.getContainerId().toInt()
        val errorRecordId = issueBean?.id ?: ""
 
        val list = ArrayList<IdRef>()
        if (mImageList.isNotEmpty()) {
            for (data in mImageList) {
                list.add(IdRef(data.uri))
            }
        }
        var device:EntityRef? =  null
        if (issueBean != null && issueBean!!.device != null) {
            device = EntityRef(issueBean!!.device!!.id)
        }
        return ProblemRecordBean(
            errorRecordId,
            EntityRef(taskId),
            EntityRef(id),
            device = device,
            images = list,
            numberFactorIndex = numIndex,
            scaleFactorIndex = scaleIndex,
            specificTermIndexes = array,
            problemPart = binding.positionWidget.getContent(),
            suggestion = binding.suggestEdt.text.toString(),
            remark = binding.remarkWidget.getContent(),
            responsiblePerson = binding.personWidget.getContent()
        )
    }
 
    /**
     * 更新数据
     */
    @SuppressLint("SetTextI18n")
    fun updateData(taskId: Int, infoBean: IssueDetailBean, bean: IssueBean?) {
        issueDetailBean = infoBean
        this.taskId = taskId
        this.issueBean = bean
        with(binding) {
            describeWidget.setContentTxt(infoBean.description)
            remarkWidget.setEditTxt(infoBean.remark)
 
            var suggest = ""
            infoBean.suggestActions?.forEach {
                suggest += it + "\n"
            }
            suggestEdt.setText(suggest)
 
            departmentWidget.setContentTxt(infoBean.responsibleDepartment)
 
            //数量
            val countOptions = mutableListOf<CommonOptionsBean>()
            for ((index, item) in infoBean.numberFactors.withIndex()) {
                countOptions.add(CommonOptionsBean(index.toString(), item.name))
            }
 
            countWidget.bindData(countOptions)
 
            //规模
            val scaleOptions = mutableListOf<CommonOptionsBean>()
            for ((index, item) in infoBean.scaleFactors.withIndex()) {
                scaleOptions.add(CommonOptionsBean(index.toString(), item.name))
            }
            scaleWidget.bindData(scaleOptions)
 
            //条款
//            val clauseOptions = mutableListOf<CommonOptionsBean>()
//            infoBean.specificTerms.forEach {
//                clauseOptions.add(CommonOptionsBean(it, it))
//            }
//            clauseWidget.bindData(clauseOptions)
 
 
            infoBean.specificTerms?.let { terms ->
                if (terms.isNotEmpty()) {
                    clauseWidget.setContentTxt(terms[0])
                    for (i in terms.indices) {
                        specificTerms.add(CommonListBean(i.toString(), terms[i]))
                    }
                }
            }
 
            if (infoBean.numberFactors.isNotEmpty() && infoBean.scaleFactors.isNotEmpty()) {
                dValue = issueDetailBean!!.numValue;
                cValue = infoBean.numberFactors[0].value
                rnValue = infoBean.scaleFactors[0].factor
                updateErrorLevel()
            }
 
 
            //以下是编辑问题
 
            if (bean == null) {
                submitTv.visibility = VISIBLE
                deleteTv.visibility = GONE
                editTv.visibility = GONE
                return
            } else {
                submitTv.visibility = GONE
                deleteTv.visibility = VISIBLE
                editTv.visibility = VISIBLE
            }
 
            val issueBean = bean
 
            countWidget.setDataById(issueBean.numberFactorIndex.toString())
            scaleWidget.setDataById(issueBean.scaleFactorIndex.toString())
            cValue = infoBean.numberFactors[issueBean.numberFactorIndex].value
            rnValue = infoBean.scaleFactors[issueBean.scaleFactorIndex].factor
 
            infoBean.specificTerms?.let { terms ->
                specificTerms.forEach {
                    for (item in terms) {
                        it.checked = item == it.name
                    }
                }
            }
 
 
            if (issueBean.device != null) {
                if (issueBean.device.id > 0 ){
                    deviceWidget.setContentTxt(issueBean.device.name)
                    deviceWidget.visibility = VISIBLE
                }
            }
            personWidget.setEditTxt(issueBean.responsiblePerson)
            positionWidget.setEditTxt(issueBean.problemPart)
            suggestEdt.setText(issueBean.suggestion)
            remarkWidget.setEditTxt(issueBean.remark)
 
 
            commitLayout.visibility = VISIBLE
            submitterTv.text = "提交人: " + issueBean.creator?.name
            timeTv.text = "提交时间: ${issueBean.createTime}"
            levelWidget.setContentTxt(issueBean.level.desc)
 
 
            if (!issueBean.images.isNullOrEmpty()) {
                for (data in issueBean.images) {
                    mImageList.add(ImageBean(data.id.toString(), data.name))
                }
                adapter.notifyData()
            }
 
            if (mContext is ProjectDetailsActivity) {
                if (mContext.canReviewTask()) {
                    submitTv.visibility = VISIBLE
                    deleteTv.visibility = GONE
                    editTv.visibility = GONE
                    submitTv.text = "复核"
                    canReview = true
                }
            }
        }
    }
 
    @SuppressLint("SetTextI18n")
    fun bindReviewData(reviewBean: ReviewBean) {
        binding.reviewLayout.visibility = VISIBLE
        val list = ArrayList<ImageBean>()
        reviewBean.images.forEach {
            list.add(ImageBean(it.id.toString(), it.name))
        }
        binding.recyclerViewReview.layoutManager =
            LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
        val adapter = ImageShowAdapter(mContext, list, false)
        binding.recyclerViewReview.adapter = adapter
 
//        binding.reviewInfoTv.text = "复核情况:${reviewBean.content}"
        val state = reviewBean.state.desc
        binding.reviewStateTv.text = "复核状态:${state}"
        val time = if (reviewBean.createTime.isEmpty()) {
            reviewBean.updateTime
        }  else{
            reviewBean.createTime
        }
        binding.reviewTimeTv.text = "复核时间:${time}"
 
        binding.reviewOpinionTv.text = "复核建议:${reviewBean.content}"
 
    }
 
 
    fun updateErrorLevel() {
        val level = calcErrorLevel(dValue, cValue, rnValue)
        binding.levelWidget.setContentTxt(level.desc)
    }
 
    /**
     * 隐患等级计算
     */
    private fun calcErrorLevel(d: Double, c: Double, rn: Double): ErrorLevel {
        val l: Double = d * c * rn
        return if (l > 24) {
            ErrorLevel.P1
        } else if (l > 12) {
            ErrorLevel.P2
        } else if (l > 4) {
            ErrorLevel.P3
        } else {
            ErrorLevel.P4
        }
    }
 
 
    interface OnClickEvent {
        fun onEdit(infoBean: ProblemRecordBean)
 
        fun onDelete(errorRecordId: String)
 
        fun onAdd(infoBean: ProblemRecordBean)
 
    }
 
}