liugl
2021-01-14 40f9ac00ec5e4d6363450d1c8e0b0937cc0b1de4
app/src/main/java/com/okgoincar/base/MoneyBroadCastReceiver.kt
@@ -5,85 +5,225 @@
import android.content.Intent
import android.os.Handler
import android.os.Looper
import cn.sinata.rxnetty.netStatus.NetUtils
import cn.sinata.xldutils.netstatus.NetUtils
import cn.sinata.xldutils.utils.toast
import com.google.gson.Gson
import com.okgoincar.bean.LoginBean
import com.okgoincar.bean.LocalOrderBean
import com.okgoincar.bean.OrderBean
import com.okgoincar.netUtls.Api
import com.okgoincar.netUtls.callNet
import com.okgoincar.netUtls.getMapByAny
import com.okgoincar.netUtls.getToken
import com.okgoincar.slab.SlabLoginActivity
import com.okgoincar.slab.SlabMainActivity
import com.okgoincar.utils.Cache.CacheKey
import com.okgoincar.utils.Cache.CacheUtil
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
import java.lang.Exception
/***
 * 为了支持本页面所需的 MyApplication.currentOrderId 所以在订单列表获取处,订单页面,设置了这个常量的值
 *
 * 1:接单后 重车时无网
 *  --车载屏无反应
 *  缓存 订单开始状态
 *  来网后空车时 --》 查看是否有进行中id,是否有缓存
 *
 *
 * 2:接单后 空车时无网
 *
 *
 *
 * 3:接单后 正常状态
 *
 *
 *
 * 4:接单后 都没有网络
 *
 *
 */
class MoneyBroadCastReceiver : BroadcastReceiver() {
    override fun onReceive(contexts: Context?, intent: Intent?) {
        if (intent == null && contexts == null){
        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) //代表让订单到进行中
                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)
                        var price = intent.getDoubleExtra("price", 0.0) //计价器价格,单位为元
                        var mileage = intent.getDoubleExtra("mileage", 0.0) //里程,单位为km
                        callOver(contexts!!, price)
                    }
                }
            }
        } 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 { //无网络 存本地
                    toast("当前没有网络,订单将保存在本地")
                    var bean = LocalOrderBean()
                    bean.orderId = MyApplication.currentOrderId
                    bean.startTime = System.currentTimeMillis()
                    CacheKey.saveLocalOrderBean(bean)
                }
            } else { //有数据存着,并且有网络上传异常订单 如果没有网络就不处理,这个肯定是线下的单
                if (NetUtils.isNetworkConnected(MyApplication.getInstance())) {
                    toast("有网络,有异常数据,此次数据不受理")
//                    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 { //无网络 存本地
                    toast("当前没有网络,订单将保存在本地")
                    var bean = LocalOrderBean()
                    bean.orderId = MyApplication.currentOrderId
                    bean.endTime = System.currentTimeMillis()
                    bean.money = travelFee
                    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)
                        }
//                        callEndOrder(contexts, travelFee)
                        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.endTime <= 0L){
                    return@let
                }
                if (bean.startTime <= 0L) {
                    if (NetUtils.isNetworkConnected(MyApplication.getInstance())){
                        CacheKey.saveLocalOrderBeanNull()
                        toast("调用正常结束,在异常订单中")
                        callEndOrder(contexts,bean.money)
                    }
                }else{
                    if (NetUtils.isNetworkConnected(MyApplication.getInstance())){
                        val map = getMapByAny()
                        map["orderId"] = bean.orderId
                        map["orderType"] = bean.orderType
                        map["type"] = "1"
                        map["travelFee"] = bean.money
                        CacheKey.saveLocalOrderBeanNull()
                        toast("调用异常结束,在异常订单中 id==="+bean.orderId)
                        callNet(contexts, "api/order/confirmFees$", map,{
                            MyApplication.currentOrderId = ""
                            toast("上传异常订单成功")
                            EventBus.getDefault().post(BaseEvent(BaseEvent.ERROR_INFO))
                        }) {
                            toast("上传异常订单失败")
                            CacheKey.saveLocalOrderBean(bean)
                        }
                    }
                }
            }
        }
         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) {
            if (MyApplication.getLocation().latitude == 0.0){
                Handler(Looper.getMainLooper()).post {
                    toast("结束时的位置为空,停止结束")
                }
                return
            }
            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) {
                var mapOrder = getMapByAny()
                mapOrder["orderId"] = MyApplication.currentOrderId
                mapOrder["orderType"] = MyApplication.currentOrderType
                callNet(contexts, Api.queryOrderInfo, mapOrder) {
                    var orderBean = Gson().fromJson(it, OrderBean::class.java)
                    EventBus.getDefault().post(BaseEvent(BaseEvent.UPDATA_MAIN_CAR))
                    if (orderBean.data.orderState == 7){
                        EventBus.getDefault().post(BaseEvent(BaseEvent.SURE_MONEY))
                    }
                    MyApplication.currentOrderId = ""
                    if (orderBean.data.orderState == 2 ||orderBean.data.orderState == 3 || orderBean.data.orderState == 4 ){
                        Handler(Looper.getMainLooper()).postDelayed({
                            toast("发送网约")
                            var intent = Intent();
                            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
                            intent.action = "wisdom.intent.action.topLight";
                            intent.putExtra("state", 0)
                            contexts.sendBroadcast(intent)
                        },10000)
                    }
                }
            }
        }catch (e:Exception){
        }
    }
    private fun callStatue(contexts: Context,i: Int) {
        if (MyApplication.currentOrderId.isEmpty()){
            toast("没有进行中的订单,无法开始")
            return
        }
        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 callOver(contexts: Context,travelFee:Double){
        if (MyApplication.currentOrderId.isEmpty()){
            toast("没有进行中的订单,无法结束")
            return
        }
        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))
        }
    }
}