8.7
liugl
2020-08-07 4cb70f4d45d586a5048750b77675d82f142deacc
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
package com.okgoincar.base
 
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Handler
import android.os.Looper
import cn.sinata.xldutils.netstatus.NetUtils
import cn.sinata.xldutils.utils.toast
import com.okgoincar.bean.LocalOrderBean
import com.okgoincar.netUtls.Api
import com.okgoincar.netUtls.callNet
import com.okgoincar.netUtls.getMapByAny
import com.okgoincar.slab.util.DialogUtil
import com.okgoincar.utils.Cache.CacheKey
import io.netty.util.NetUtil
import org.greenrobot.eventbus.EventBus
import java.lang.Exception
 
class MoneyBroadCastReceiver : BroadcastReceiver() {
    override fun onReceive(contexts: Context?, intent: Intent?) {
        if (intent == null && contexts == null) {
            toast("内容获取为空,广播接收消息错误")
            return
        }
        try {
            Handler(Looper.getMainLooper()).post {
                when (intent!!.action) {
                    "wisdom.intent.action.ledLight" -> {
                        val isHeavy = intent!!.getBooleanExtra("isHeavy", true)
                        if (isHeavy) {
                            callStatue(contexts!!, 5) //代表让订单到进行中
                        }
                    }
 
                    "wisdom.intent.action.priceDevice" -> {
                        var price = intent.getDoubleExtra("price", 0.0) //计价器价格,单位为元
                        var mileage = intent.getDoubleExtra("mileage", 0.0) //里程,单位为km
                        callOver(contexts!!, price)
                    }
 
                    "android.net.conn.CONNECTIVITY_SUCCESS", "android.net.conn.CONNECTIVITY_FAILURE", "android.net.conn.CONNECTIVITY_CHANGE" -> {
//                        toast("收到网络变化")
//                        if (NetUtils.isNetworkConnected(MyApplication.getInstance())) {
//                            callErrorOrder(contexts!!)
//                        }
                    }
                }
            }
        } catch (e: Exception) {
 
        }
    }
 
    companion object {
        fun callStatue(contexts: Context, i: Int) {
            if (MyApplication.currentOrderId.isEmpty()) {
                toast("没有进行中的订单,无法开始")
                return
            }
            if (CacheKey.getLocalOrderBean() == null) { //没有数据 代表没有异常订单
                if (NetUtils.isNetworkConnected(MyApplication.getInstance())) { //有网络直接处理
                    callStartOrder(contexts, i)
                } else { //无网络 存本地
                    var bean = LocalOrderBean()
                    bean.orderId = MyApplication.currentOrderId
                    bean.startTime = System.currentTimeMillis()
                    CacheKey.saveLocalOrderBean(bean)
                }
            } else { //有数据存着,并且有网络上传异常订单 如果没有网络就不处理,这个肯定是线下的单
                if (NetUtils.isNetworkConnected(MyApplication.getInstance())) {
                    callErrorOrder(contexts)
                }
            }
        }
 
 
        fun callOver(contexts: Context, travelFee: Double) {
            if (MyApplication.currentOrderId.isEmpty()) {
                toast("没有进行中的订单,无法结束")
                return
            }
            if (CacheKey.getLocalOrderBean() == null) { //没有数据 代表没有异常订单
                if (NetUtils.isNetworkConnected(MyApplication.getInstance())) { //有网络直接处理
                    callEndOrder(contexts, travelFee)
                } else { //无网络 存本地
                    var bean = LocalOrderBean()
                    bean.orderId = MyApplication.currentOrderId
                    bean.endTime = System.currentTimeMillis()
                    CacheKey.saveLocalOrderBean(bean)
                }
            } else { //有数据存着,并且有网络上传异常订单 如果没有网络就不处理,这个肯定是线下的单
                if (NetUtils.isNetworkConnected(MyApplication.getInstance())) {
                    var bean = CacheKey.getLocalOrderBean()
                    bean?.let {
                        if (bean.money <= 0.0) {
                            bean.money = travelFee
                            bean.orderId = MyApplication.currentOrderId
                            bean.endTime = System.currentTimeMillis()
                            CacheKey.saveLocalOrderBean(bean)
                        }
                        callErrorOrder(contexts)
                    }
                } else {
                    val bean = CacheKey.getLocalOrderBean()
                    bean?.let {
                        if (bean.money >= 0.0) {
                            return@callOver
                        }
                        bean.endTime = System.currentTimeMillis()
                        bean.money = travelFee
                        bean.orderId = MyApplication.currentOrderId
                        CacheKey.saveLocalOrderBean(bean)
                    }
                }
            }
        }
 
        /***
         * 上传异常订单
         */
        fun callErrorOrder(contexts: Context) {
            var bean = CacheKey.getLocalOrderBean()
            bean?.let {
                if (bean.money <= 0.0) {
                    return@let
                }
                val map = getMapByAny()
                map["orderId"] = bean.orderId
                map["orderType"] = bean.orderType
                map["type"] = "1"
                map["travelFee"] = bean.money
                callNet(contexts, "api/order/confirmFees$", map) {
                    toast("上传异常订单成功")
                    CacheKey.saveLocalOrderBeanNull()
                    EventBus.getDefault().post(BaseEvent(BaseEvent.ERROR_INFO))
                }
            }
        }
 
        private fun callStartOrder(contexts: Context, i: Int) {
            var map = getMapByAny()
            map["orderId"] = MyApplication.currentOrderId
            map["orderType"] = MyApplication.currentOrderType
            map["state"] = i
            map["lat"] = MyApplication.getLocation().latitude
            map["lon"] = MyApplication.getLocation().longitude
            callNet(contexts, Api.process, map) {
                EventBus.getDefault().post(BaseEvent(BaseEvent.UP_TRIP))
            }
        }
 
 
        private fun callEndOrder(contexts: Context, travelFee: Double) {
            var map = getMapByAny()
            map["orderId"] = MyApplication.currentOrderId
            map["orderType"] = MyApplication.currentOrderType
            map["travelFee"] = travelFee
            map["lat"] = MyApplication.getLocation().latitude
            map["lon"] = MyApplication.getLocation().longitude
            map["type"] = 1
            callNet(contexts, Api.confirmFees_, map) {
                EventBus.getDefault().post(BaseEvent(BaseEvent.SURE_MONEY))
            }
        }
 
    }
 
 
}