lmw
2024-06-17 f571288a24fcf10143dcc8015ffbbf38dbc0c614
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
package com.dollearn.student.ui.course
 
import android.content.Intent
import android.graphics.Paint
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.invisible
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.visible
import com.dollearn.student.R
import com.dollearn.student.dialog.PayDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Banner
import com.dollearn.student.network.entity.JoinedCourse
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.PayResultActivity
import com.dollearn.student.ui.home.adapter.HomeBannerAdapter
import com.dollearn.student.ui.home.adapter.LessonCountAdapter
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_course.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.textColorResource
 
class UnPayCourseDetailActivity : TransparentStatusBarActivity(), PayListener {
    override fun setContentView() = R.layout.activity_join_course
 
    private val id by lazy { intent.getStringExtra("id")?:"" }
    private val orderId by lazy { intent.getStringExtra("orderId")?:"" }
 
    private val bannerImg = arrayListOf<Banner>()
    private val bannerAdapter by lazy {
        HomeBannerAdapter(bannerImg, this)
    }
    private val arrayListOf = arrayListOf<String>()
    private val lessonCountAdapter = LessonCountAdapter(arrayListOf)
 
    private val students = arrayListOf<Student>()
    private val studentAdapter = StudentAdapter(students, StudentAdapter.TYPE_COURSE_CHOOSE)
 
    private var course:JoinedCourse? = null
 
    override fun initClick() {
    }
 
    override fun initView() {
        banner.adapter = bannerAdapter
        tv_action.text = "立即支付"
        tv_add_student.invisible() //安雪晖:待支付不能修改成员
        studentAdapter.delable = false
        rv_lesson_count.layoutManager = GridLayoutManager(this, 3)
        rv_student.layoutManager = LinearLayoutManager(this)
        rv_student.adapter = studentAdapter
        PayUtil.addPayListener(this)
        EventBus.getDefault().register(this)
        getDetail()
    }
 
    private fun getDetail(){
        HttpManager.registeredData(id,orderId).request(this){ _, data->
            course = data
            data?.apply {
                bannerImg.addAll((detailDrawing?:"").split(",").map { Banner(url = it) })
                bannerAdapter.notifyDataSetChanged()
                banner.currentItem = 1
                tv_name.text = coursePackageName
                tv_week.text = weeks?.replace(";","、")
                tv_time.text = courseTimeFrame
                tv_shop.text = storeName
                tv_address.text = storeAddress
 
                arrayListOf.addAll(list.filter { it.classHours == chooseHours }.map { "${it.classHours}课时" })
                lessonCountAdapter.checked = 0
                rv_lesson_count.adapter = lessonCountAdapter
 
                when(type){
                    2->{ //假期班
                        gp_count.gone()
                        mll_class_yxq.visible()
                        mtv_class_yxq.text = time
                        mtvtag.text = "假期班"
                    }
                    3->{ //体验课
                        gp_count.gone()
                        mll_class_yxq.visible()
                        mtvtag.text = "体验课"
                        cl_time.gone()
                    }
                    else->{
                        mtvtag.text = "常规班"
                    }
                }
 
                HttpManager.listOfStu().request(this@UnPayCourseDetailActivity){ _, data->
                    students.addAll((data?: arrayListOf()).filter { it.stuId in stuIds })
                    studentAdapter.checked.addAll(stuIds)
                    studentAdapter.notifyDataSetChanged()
                }
 
                if (payPrice?:0.0 == 0.0)
                    tv_price.gone()
                else{
                    tv_price.text = "¥%s".format(payPrice.toString())
                    if (orinPrice?:0.0!=0.0&&orinPrice!=payPrice){//享受了优惠,展示原价
                        tv_vip_price.text = "¥%s".format(orinPrice.toString())
                        tv_vip_price.textColorResource = R.color.textColor66
                        tv_vip_price.paintFlags =
                            tv_vip_price.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG
                    }
                }
 
                if (coinPrice?:0 == 0)
                    tv_coin_price.gone()
                else
                    tv_coin_price.text = SpanBuilder("玩湃币:%d币".format(coinPrice)).color(this@UnPayCourseDetailActivity,0,4,R.color.textColor).build()
 
                tv_action.clickDelay {
                    PayDialog.show(supportFragmentManager, false, object : StringCallback {
                        override fun onResult(rst: String) {
                            pay(rst.toInt())
                        }
 
                    }, payPrice, coinPrice)
                }
            }
        }
    }
 
 
    private fun pay(payType: Int) {
        HttpManager.paymentCourse(
            payType,
            course?.coursePackageId?:"",
            course!!.list[lessonCountAdapter.checked].id,
            if (payType == 3) course!!.coinPrice!!.toDouble() else course!!.payPrice!!,
            students.joinToString(";") { it.id ?: "" },
            0,orderId
        ).request(this, false, success = { _, data ->
            if (payType == 2) {
                PayUtil.aliPay(this, data?.orderInfo ?: "")
            } else if (payType == 3) {
                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
            )
        }
    }
 
    @Subscribe
    fun onSwitch(e: EmptyEvent) {
 
    }
 
    override fun onPaySuccess() {
        startActivityForResult<PayResultActivity>(
            1,
            "type" to PayResultActivity.TYPE_COURSE_SUC,
            "lessonId" to course!!.list[lessonCountAdapter.checked].id
        )
        finish()
    }
 
    override fun onPayCancel() {
    }
 
    override fun onPayError(msg: String) {
        startActivityForResult<PayResultActivity>(
            1,
            "type" to PayResultActivity.TYPE_COURSE_FAILED
        )
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK&&requestCode == 1) {
            tv_action.postDelayed({
                tv_action.callOnClick()
            }, 500)
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        PayUtil.removePayListener(this)
        PayUtil.unregisterApp()
        EventBus.getDefault().unregister(this)
    }
}