罗明文
2024-06-16 9673bcd57c6100ad9fdfbee728ef078104511fc1
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
package com.dollearn.student.ui.home
 
import android.content.Intent
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.utils.showAllowingStateLoss
import cn.sinata.xldutils.visible
import com.google.gson.Gson
import com.dollearn.student.R
import com.dollearn.student.dialog.*
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Banner
import com.dollearn.student.network.entity.Match
import com.dollearn.student.network.entity.PayCourse
import com.dollearn.student.network.entity.Student
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.adapter.HomeBannerAdapter
import com.dollearn.student.ui.home.adapter.StudentAdapter
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import com.dollearn.student.utils.extention.clickDelay
import com.dollearn.student.utils.interfaces.StringCallback
import com.dollearn.student.utils.pay.PayListener
import com.dollearn.student.utils.pay.PayUtil
import kotlinx.android.synthetic.main.activity_join_match.*
import kotlinx.android.synthetic.main.activity_join_match.banner
import kotlinx.android.synthetic.main.activity_join_match.mtvcheckmore
import kotlinx.android.synthetic.main.activity_join_match.tv_action
import kotlinx.android.synthetic.main.activity_join_match.tv_address
import kotlinx.android.synthetic.main.activity_join_match.tv_age
import kotlinx.android.synthetic.main.activity_join_match.tv_city
import kotlinx.android.synthetic.main.activity_join_match.tv_coin
import kotlinx.android.synthetic.main.activity_join_match.tv_condition
import kotlinx.android.synthetic.main.activity_join_match.tv_course_time
import kotlinx.android.synthetic.main.activity_join_match.tv_deadline
import kotlinx.android.synthetic.main.activity_join_match.tv_end_time
import kotlinx.android.synthetic.main.activity_join_match.tv_introduce
import kotlinx.android.synthetic.main.activity_join_match.tv_name
import kotlinx.android.synthetic.main.activity_join_match.tv_play_address
import kotlinx.android.synthetic.main.activity_join_match.tv_price
import kotlinx.android.synthetic.main.activity_join_match.tv_shops
import kotlinx.android.synthetic.main.activity_join_match.tv_start_time
import kotlinx.android.synthetic.main.activity_match_deatil.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivityForResult
 
class JoinMatchActivity:TransparentStatusBarActivity(), PayListener {
    override fun setContentView() = R.layout.activity_join_match
 
    private val match by lazy { intent.getParcelableExtra<Match>("match") as Match }
    private val bannerImg = arrayListOf<Banner>()
    private val bannerAdapter by lazy {
        HomeBannerAdapter(bannerImg, this)
    }
 
    private val students = arrayListOf<Student>()
    private val studentAdapter = StudentAdapter(students, StudentAdapter.TYPE_MATCH_DETAIL)
 
    override fun initClick() {
        tv_add_student.clickDelay {
            ChooseStudentDialog.show(supportFragmentManager,ChooseStudentDialog.TYPE_MATCH,students,object :ChooseStudentDialog.ResultCallback{
                override fun checked(list: List<Student>) {
                    students.clear()
                    students.addAll(list)
                    studentAdapter.notifyDataSetChanged()
                }
            },null,match.isReal == 1)
        }
 
        tv_action.clickDelay {
            if (students.isEmpty())
                myToast("请添加活动人员")
            else{
                if (match.storeInfos?.size == 1){
                    match.storeInfos?.firstOrNull()?.let {
                        ConfirmShopDialog.show(supportFragmentManager,it.storeCoverDrawing?:"",it.name?:"",it.address?:"",
                            if (it.distance == null) "" else "${it.distance}km",object :StringCallback{
                                override fun onResult(rst: String) {
                                    PayDialog.show(supportFragmentManager,false,object :StringCallback{
                                        override fun onResult(rst: String) {
                                            pay(rst.toInt())
                                        }
                                    },students.size*((match.cashPrice?:"0.0").toDouble()),students.size*((match.playPaiCoin?:"0").toInt()),students.size*((match.classPrice?:"0").toInt()),null,students.filter { it.isStudent == 0 }.isNullOrEmpty())
                                }
                            })
                    }
                }else{
                    ConfirmShopsDialog.show(supportFragmentManager,match.storeInfos?.joinToString("\n") { it.name }?:"",object :StringCallback{
                            override fun onResult(rst: String) {
                                PayDialog.show(supportFragmentManager,false,object :StringCallback{
                                    override fun onResult(rst: String) {
                                        pay(rst.toInt())
                                    }
                                },students.size*((match.cashPrice?:"0.0").toDouble()),students.size*((match.playPaiCoin?:"0").toInt()),students.size*((match.classPrice?:"0").toInt()),null,students.filter { it.isStudent == 0 }.isNullOrEmpty())
                            }
                        })
                }
            }
        }
    }
 
    override fun initView() {
        banner.adapter = bannerAdapter
        rv_student.layoutManager = LinearLayoutManager(this)
        rv_student.adapter = studentAdapter
        studentAdapter.delCallback = object : StringCallback {
            override fun onResult(rst: String) {
                students.removeAt(rst.toInt())
                studentAdapter.notifyDataSetChanged()
            }
        }
        match.apply {
            storeInfos?.let {
                val stores = StringBuffer()
                for (s in it.indices) {
                    if (s == it.size - 1) stores.append(it[s].name)
                    else stores.append(it[s].name + ",")
                }
                tv_shops.text = stores
                if (stores.length > 40) {
                    mtvcheckmore.visible()
                    mtvcheckmore.clickDelay {
                        val checkShopsDialog = CheckShopsDialog()
                        checkShopsDialog.arguments =
                            bundleOf("shopinfo" to stores.toString())
                        checkShopsDialog.showAllowingStateLoss(
                            supportFragmentManager,
                            "checkshops"
                        )
                    }
                } else {
                    mtvcheckmore.gone()
                }
            }
 
            val list = imgs.split(",").map {
                Banner(url = it)
            }
            bannerImg.addAll(list)
            bannerAdapter.notifyDataSetChanged()
            tv_name.text = name
            tv_condition.text = getCondition()+"参与"
            tv_address.text = "${storeName}(${storeAddress})"
            tv_deadline.text = registerEndTime
            tv_start_time.text = startTime
            tv_end_time.text = endTime
            tv_age.text = age
            tv_city.text = "${province}|${city}"
            tv_play_address.text = address
            if (cashPrice!=null&&cashPrice.toDouble()!=0.0){
                val price = "支付:¥%s/人".format(cashPrice)
                tv_price.text = SpanBuilder(price).color(this@JoinMatchActivity,0,3,R.color.textColor66).bold(3,price.length).build()
                tv_price.visible()
            }
            if (playPaiCoin!=null&&playPaiCoin.toInt()!=0){
                val coin = "玩湃币:%s币/人".format(playPaiCoin)
                tv_coin.text = SpanBuilder(coin).color(this@JoinMatchActivity,0,4,R.color.textColor66).bold(4,coin.length).build()
                tv_coin.visible()
            }
            if (classPrice!=null&&classPrice.toInt()!=0){
                val time = "课时:%s课时/人".format(classPrice)
                tv_course_time.text = SpanBuilder(time).color(this@JoinMatchActivity,0,3,R.color.textColor66).bold(3,time.length).build()
                tv_course_time.visible()
            }
            tv_introduce.text = introduction
        }
        PayUtil.addPayListener(this)
    }
 
    private fun pay(payType:Int,coursePaymentId:String? = null){
        if (payType == 4&&coursePaymentId == null){//课时支付
            HttpManager.paymentCompetitionCourseList(match.id).request(this){_,data->
                val list = arrayListOf<PayCourse>()
                list.addAll(data?: arrayListOf())
                if (list.isEmpty())
                    myToast("暂无可用课时")
                else
                    CoursePayDialog.show(supportFragmentManager,object :StringCallback{
                        override fun onResult(rst: String) {
                            pay(payType, rst)
                        }
                    },students.size*((match.classPrice?:"0").toInt()), list)
            }
        }else
            HttpManager.paymentCompetition(payType,match.id, Gson().toJson(students),coursePaymentId).request(this,false,success = { _, data->
                if (payType == 2){
                    PayUtil.aliPay(this,data?.orderInfo?:"")
                }else if (payType == 3||payType == 4){
                    EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_USER)) //玩湃币支付后刷新首页
                    onPaySuccess()
                }else{
                    PayUtil.initWeChatPay(this, Const.WX_APP_ID)
                    PayUtil.weChatPay(data!!)
                }
            }){_,msg->
                startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_COURSE_FAILED,"msg" to msg)
            }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode == 1){
            tv_action.postDelayed({
                PayDialog.show(supportFragmentManager,false,object :StringCallback{
                    override fun onResult(rst: String) {
                        pay(rst.toInt())
                    }
 
                },students.size*((match.cashPrice?:"0.0").toDouble()),students.size*((match.playPaiCoin?:"0").toInt()),students.size*((match.classPrice?:"0").toInt()))
            },500)
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        PayUtil.removePayListener(this)
        PayUtil.unregisterApp()
    }
 
    override fun onPaySuccess() {
        startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_MATCH_SUC)
        finish()
    }
 
    override fun onPayCancel() {
    }
 
    override fun onPayError(msg: String) {
        startActivityForResult<PayResultActivity>(1,"type" to PayResultActivity.TYPE_MATCH_FAILED)
    }
 
}