GLDriver/app/src/main/java/com/dahe/gldriver/utils/OrderUtils.kt
2024-04-17 16:43:45 +08:00

247 lines
9.4 KiB
Kotlin

package com.dahe.gldriver.utils
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import com.dahe.gldriver.R
import com.dahe.gldriver.base.App
import com.dahe.gldriver.base.AppConfig
import com.dahe.gldriver.base.AppConfig.NEED_UP_ORDER
import com.dahe.gldriver.bean.CarBean
import com.dahe.gldriver.bean.OrderDetailBean
import com.dahe.gldriver.bean.UpDriverCar
import com.dahe.gldriver.bean.UpLocation
import com.dahe.gldriver.callback.OnResultListener
import com.dahe.gldriver.net.BaseObserver
import com.dahe.gldriver.net.DataManager
import com.dahe.gldriver.net.RxHttpCallBack
import com.dahe.gldriver.service.UpLocationService
import com.dahe.gldriver.service.UpLocationService2
import com.dahe.gldriver.ui.waybill.activity.WaybillLoadActivity
import com.dahe.mylibrary.base.SingletonNoPHolder
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.ActivityUtils
import com.dahe.mylibrary.utils.BaseSPUtils
import com.dahe.mylibrary.utils.ToastUtils
import com.google.gson.Gson
import com.gyf.cactus.Cactus
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
/**
* @ClassName OrderUtils
* @Author john
* @Date 2024/3/4 14:48
* @Description 订单状态转换
*/
class OrderUtils private constructor() {
companion object : SingletonNoPHolder<OrderUtils>(::OrderUtils)
fun getOrderStatu(statu: String): String {
return when (statu) {
"102010" -> "草稿箱"
"102020" -> "待调度"
"102030" -> "待接单"
"102040" -> "待装货"
"102050" -> "待卸货"
"102060" -> "待签收"
"102070" -> "待结算"
"102090" -> "已完成"
"103000" -> "已取消"
else -> {
""
}
}
}
fun getNextByStatu(statu: String): String {
return when (statu) {
"102010" -> "草稿箱"
"102020" -> "待调度"
"102030" -> "去接单"
"102040" -> "去装货"
"102050" -> "去卸货"
"102060" -> "待签收"
"102070" -> "去评价"
"102090" -> "去评价"
"103000" -> "已取消"
else -> {
""
}
}
}
fun getStatuByString(statu: String): String {
return when (statu) {
"草稿箱" -> "102010"
"待调度" -> "102020"
"待接单" -> "102030"
"待装货" -> "102040"
"待卸货" -> "102050"
"待签收" -> "102060"
"待结算" -> "102070"
"待评价" -> "102080"
"已取消" -> "103000"
else -> {
""
}
}
}
/**
*
* 接单(包含选择车辆)->装货
* */
fun goReceWaybill(
context: Context,
orderId: String,
selDriverId: String = "",
selCarId: String = "",
listener: OnResultListener
) {
val userInfo = SPUtils.instance.getUserInfo(context)
if (userInfo.isCarCaptain == "1") {
DataManager.getInstance()
.selectCarsList(userInfo.captainId)
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(BaseObserver(context, object : RxHttpCallBack<MutableList<CarBean>>() {
override fun onSuccess(t: CommonResponseBean<MutableList<CarBean>>) {
super.onSuccess(t)
if (t.data == null || t.data.size == 0) {
ToastUtils.showToast(context, "暂未查询到车辆")
return
}
CommonPopUtils.getInstance()
.showCarList(context, t.data, true,selDriverId,selCarId) {
//分配车辆redeliveryOrder
DataManager.getInstance()
.redeliveryOrder(UpDriverCar(orderId, it.driverId, it.carId))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
BaseObserver(
context,
object : RxHttpCallBack<Any>() {
override fun onSuccess(t: CommonResponseBean<Any>) {
super.onSuccess(t)
listener.onResult(true)
ToastUtils.showToast(context, "分配车辆成功")
}
})
)
}
}
}))
} else {
//选择车辆
DataManager.getInstance().carList()
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(
BaseObserver(context, object : RxHttpCallBack<MutableList<CarBean>>() {
override fun onSuccess(t: CommonResponseBean<MutableList<CarBean>>) {
super.onSuccess(t)
if (t.data == null || t.data.size == 0) {
ToastUtils.showToast(context, "暂未查询到车辆")
return
}
if (t.data.size > 1) {//多辆车需选择车辆,单个车辆直接接单
CommonPopUtils.getInstance()
.showCarList(context, t.data, false,selDriverId,selCarId) {
//接单接口
receivingOrders(context, orderId, it, listener)
}
} else {
//单量车,直接默认当前车辆 ,接单接口
receivingOrders(context, orderId, t.data[0], listener)
}
}
})
)
}
}
/**
*
*调用接单接口
* */
private fun receivingOrders(
context: Context,
orderId: String,
car: CarBean,
listener: OnResultListener
) {
DataManager.getInstance()
.receivingOrders(orderId, car.carId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
BaseObserver(
context,
object : RxHttpCallBack<Any>() {
override fun onSuccess(t: CommonResponseBean<Any>) {
super.onSuccess(t)
listener.onResult(true)
//接单成功,弹窗是否去装货
CommonPopUtils.getInstance()
.showLoadPop(context, car) {
ActivityUtils.startActivity(context,
WaybillLoadActivity::class.java,
Bundle().apply {
putString(AppConfig.ORDER_ID, orderId)
}
)
}
}
})
)
}
/**
* 上传运行中运单轨迹
* @param context Context
*/
fun upLocation(context: Context) {
if (SPUtils.instance.getUserInfo(context).isCarCaptain == "1")//车队长角色不需要上传轨迹
return
DataManager.getInstance().orderDetail("")
.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(BaseObserver(context, object : RxHttpCallBack<OrderDetailBean>() {
override fun onSuccess(t: CommonResponseBean<OrderDetailBean>) {
super.onSuccess(t)
if (t.data != null && !t.data.orderId.isNullOrEmpty()) {
BaseSPUtils.put(context, NEED_UP_ORDER, Gson().toJson(t.data))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(
Intent(
context,
UpLocationService2::class.java
)
)
} else {
context.startService(Intent(context, UpLocationService2::class.java))
}
} else {
context.stopService(Intent(context, UpLocationService2::class.java))
BaseSPUtils.remove(context, NEED_UP_ORDER)
}
}
}))
}
/**
* 关闭上传运行中运单轨迹
* @param context Context
*/
fun stopUpLocation(context: Context) {
context.stopService(Intent(context, UpLocationService2::class.java))
}
}