18034727885
2026-06-26 253f680fb9666751e0f9a8e5e8f2f9e3eb1894e3
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
package com.example.smart.ui.creatplan
 
import android.text.TextUtils
import android.view.LayoutInflater
import androidx.activity.viewModels
import com.example.basecore.BaseBindingActivity
import com.example.smart.bean.CommonOptionsBean
import com.example.smart.databinding.ActivityCreatePlanBinding
import com.example.smart.ui.creatplan.bean.CreatePlanParam
import com.example.smart.ui.creatplan.bean.PlanEventBean
import com.example.smart.ui.creatplan.event.PlanCreateEvent
import com.example.smart.widget.common.ICommonDataEvent
import com.example.smart.widget.dialog.CommonListPopup
import com.example.basecore.util.showCenterToast
import com.example.smart.bean.EntityRef
import com.example.smart.enumlist.PlanType
import kotlinx.coroutines.flow.flow
 
/**
 * 新建计划
 */
class CreatePlanActivity : BaseBindingActivity() {
 
    private val binding by lazy { ActivityCreatePlanBinding.inflate(LayoutInflater.from(mContext)) }
 
    private val viewModel: CreatePlanViewModel by viewModels()
 
    private var isSelectedEdit = false
 
    private var lastEntrustSearchResultList = ArrayList<CommonOptionsBean>()
 
    override fun getRootView() = binding.root
 
    override fun initView() {
        transStatusBar()
        requestOptions()
        addListener()
        bindEventObserver()
    }
 
    private fun bindEventObserver() {
        //流程修改,新建计划时不再新建项目
        PlanCreateEvent.observe(this) {
            if (it.code == PlanCreateEvent.CODE_PROJECT_SUCCESS) {
                createPlan()
            }
        }
    }
 
    private fun addListener() {
        binding.apply {
 
            /**
             * 设备体系类型重新选择
             */
            systemWidget.setDataEvent(object : ICommonDataEvent {
                override fun onClickView(containerId: String) {
                    mathWidget.clearTxt()
                    gradeWidget.clearTxt()
                    requestSystemChildOptions(containerId)
                }
            })
 
            //设备体系模型
            equipmentWidget.setDataEvent(object : ICommonDataEvent {
                override fun onClickView(containerId: String) {
                    facilityWidget.clearTxt()
                    facilityMathWidget.clearTxt()
                    viewModel.callDeviceSystemChildOptions(containerId)
                }
            })
 
//            entrustWidget.getEdtView().textChangeFlow() // 构建输入框文字变化流
//                .filter {
//                    val isSelected = isSelectedEdit
//                    if (isSelectedEdit) {
//                        isSelectedEdit = false
//                    }
//                    it.isNotEmpty() && !isSelected
//                }
//                .debounce(300) // 300ms防抖
//                .flatMapLatest {
//                    searchFlow(it.toString())
//                } // 新搜索覆盖旧搜索
//                .flowOn(Dispatchers.IO) // 让搜索在异步线程中执行
////                .onEach { updateUi(it) } // 获取搜索结果并更新界面
//                .launchIn(viewModel.viewModelScope) // 在主线程收集搜索结果
 
            stepTv.setOnClickListener {
//                var isClickNameRight = false
//                for (data in lastEntrustSearchResultList) {
//                    if (data.name == binding.entrustWidget.getContent()) {
//                        isClickNameRight = true
//                        break
//                    }
//                }
//                if (!isClickNameRight) {
//                    showCenterToast("请输入正确的委托方")
//                    return@setOnClickListener
//                }
                if (!checkData()) {
                    return@setOnClickListener
                }
                createPlan()
            }
        }
    }
 
    private fun searchFlow(key: String) = flow { emit(search(key)) }
 
    private fun search(key: String) {
        viewModel.searchEntrustName(key) {}
    }
 
    //
    private fun requestOptions() {
        val list = ArrayList<CommonOptionsBean>()
        //计划类型
        PlanType.values().forEach {
            list.add(CommonOptionsBean(it.name, it.desc))
        }
        binding.planWidget.bindData(list)
 
        //综合体系模型
        viewModel.requestSystemOptions {
            showCenterToast(it)
        }
 
        //设备体系模型
        viewModel.requestDeviceSystemOptions()
 
        //委托方
        viewModel.requestEntrustOption()
    }
 
    /**
     * 综合体系模型子模型
     * 综合打分专家&综合模糊专家
     */
    private fun requestSystemChildOptions(id: String) {
        viewModel.requestSystemChildOptions(id) {
            showCenterToast(it)
        }
    }
 
    private var popupWindow: CommonListPopup? = null
    override fun bindDataObserver() {
        viewModel.run {
            createResult.observe(this@CreatePlanActivity) {
                PlanCreateEvent.setResult(PlanEventBean(PlanCreateEvent.CODE_PLAN_SUCCESS))
                finish()
            }
            entrustOptionsBean.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.entrustWidget.bindData(it)
            }
 
            /**
             * 搜索
             */
//            searchResult.observe(this@CreatePlanActivity) {
//                if (it.isEmpty()) {
//                    val bean = CommonOptionsBean("", "无数据")
//                    it.add(bean)
//                } else {
//                    lastEntrustSearchResultList.clear()
//                    lastEntrustSearchResultList.addAll(it)
//                }
//
//                popupWindow?.dismiss()
//                popupWindow =
//                    CommonListPopup(mContext, it, object : CommonListPopup.ItemClickListener {
//                        override fun onItemClick(bean: CommonOptionsBean) {
//                            if (bean.code.isNotEmpty()) {
//                                isSelectedEdit = true
//                                binding.entrustWidget.updateData(bean)
//                            }
//                        }
//                    })
//                popupWindow!!.contentView.measure(
//                    View.MeasureSpec.UNSPECIFIED,
//                    View.MeasureSpec.UNSPECIFIED
//                )
//                popupWindow!!.showAsDropDown(
//                    binding.entrustWidget.getEdtView(),
//                    dp2px(14),
//                    dp2px(14),
//                    Gravity.RIGHT or Gravity.BOTTOM
//                )
//            }
 
            //综合
            systemOptionsBean.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.systemWidget.bindData(it)
 
                requestSystemChildOptions(it.first().code)
            }
 
            //综合体系模型子模型 数学
            systemScoreOptionsBean.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.mathWidget.bindData(it)
 
            }
            //综合体系模型子模型 模糊
            systemFuzzyOptionsBean.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.gradeWidget.bindData(it)
            }
 
            //设备体系模型
            deviceOptionBeans.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.equipmentWidget.bindData(it)
                viewModel.callDeviceSystemChildOptions(it.first().code)
            }
 
            //设备体系模型  数学
            deviceScoreOptionsBean.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.facilityMathWidget.bindData(it)
            }
 
            //设备体系模型 模糊
            deviceFuzzyOptionsBean.observe(this@CreatePlanActivity) {
                if (it.isEmpty()) {
                    return@observe
                }
                binding.facilityWidget.bindData(it)
            }
        }
 
    }
 
    /**
     * 新建计划
     */
    private fun createPlan() {
        if (!checkData()) {
            return
        }
        val param = CreatePlanParam()
 
        binding.apply {
            param.name = nameWidget.getContent()
            param.type = planWidget.getContainerId()
 
            param.client = EntityRef(entrustWidget.getContainerId().toInt())
            param.systemModel = EntityRef(systemWidget.getContainerId().toInt())
            //生成其他成员变量赋值
            param.systemFuzzyExpert = EntityRef(mathWidget.getContainerId().toInt())
            param.systemScoreExpert = EntityRef(gradeWidget.getContainerId().toInt())
            param.deviceModel = EntityRef(equipmentWidget.getContainerId().toInt())
            param.deviceScoreExpert = EntityRef(facilityMathWidget.getContainerId().toInt())
            param.deviceFuzzyExpert = EntityRef(facilityWidget.getContainerId().toInt())
 
        }
 
        viewModel.createItem(param) {
            showCenterToast(it)
        }
 
    }
 
    private fun checkData(): Boolean {
        binding.run {
            if (!checkEmptyData(nameWidget.getPreTxt(), nameWidget.getContent())) {
                return false
            }
            if (!checkEmptyData(entrustWidget.getPreTxt(), entrustWidget.getContent())) {
                return false
            }
 
            if (!checkEmptyData(planWidget.getPreTxt(), planWidget.getContent())) {
                return false
            }
            if (!checkEmptyData(systemWidget.getPreTxt(), systemWidget.getContent())) {
                return false
            }
            if (!checkEmptyData(mathWidget.getPreTxt(), mathWidget.getContent())) {
                return false
            }
            if (!checkEmptyData(gradeWidget.getPreTxt(), gradeWidget.getContent())) {
                return false
            }
            if (!checkEmptyData(equipmentWidget.getPreTxt(), equipmentWidget.getContent())) {
                return false
            }
            if (!checkEmptyData(facilityMathWidget.getPreTxt(), facilityMathWidget.getContent())) {
                return false
            }
 
            if (!checkEmptyData(facilityWidget.getPreTxt(), facilityWidget.getContent())) {
                return false
            }
 
        }
 
        return true
    }
 
    private fun checkEmptyData(name: String, content: String): Boolean {
        if (TextUtils.isEmpty(content)) {
            showCenterToast("请输入$name")
            return false
        }
 
        return true
    }
}