|
|
@@ -6,14 +6,17 @@ import android.view.LayoutInflater
|
|
|
import android.view.View
|
|
|
import android.view.ViewGroup
|
|
|
import android.widget.TextView
|
|
|
+import androidx.fragment.app.Fragment
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
import cn.muchinfo.rma.R
|
|
|
import cn.muchinfo.rma.business.future.adapter.ChannelOrderReqData
|
|
|
import cn.muchinfo.rma.global.GlobalDataCollection
|
|
|
import cn.muchinfo.rma.global.data.futureOrders.FutureHoldData
|
|
|
+import cn.muchinfo.rma.netManage.base.InteractiveException
|
|
|
import cn.muchinfo.rma.view.MyApplication
|
|
|
import cn.muchinfo.rma.view.autoWidget.onThrottleFirstClick
|
|
|
+import cn.muchinfo.rma.view.autoWidget.utils.NumberUtils
|
|
|
import cn.muchinfo.rma.view.base.BaseViewModel
|
|
|
import cn.muchinfo.rma.view.base.app.FutureConstent
|
|
|
import cn.muchinfo.rma.view.eventbus.EventConstent
|
|
|
@@ -21,6 +24,8 @@ import cn.muchinfo.rma.view.eventbus.TradeMessageEvent
|
|
|
import com.blankj.utilcode.util.TimeUtils
|
|
|
import kotlinx.coroutines.GlobalScope
|
|
|
import kotlinx.coroutines.launch
|
|
|
+import mtp.polymer.com.autowidget.dialog.createWarningDialog
|
|
|
+import mtp.polymer.com.autowidget.utils.TaskUiModel
|
|
|
import org.greenrobot.eventbus.EventBus
|
|
|
|
|
|
/**
|
|
|
@@ -37,13 +42,14 @@ class HoldViewModel : BaseViewModel() {
|
|
|
/**
|
|
|
* 获取配置器。
|
|
|
*/
|
|
|
- fun getAdapter(): RecyclerView.Adapter<HoldAdapter.HoldHolder> {
|
|
|
- return HoldAdapter(context)
|
|
|
+ fun getAdapter(fragment : Fragment): RecyclerView.Adapter<HoldAdapter.HoldHolder> {
|
|
|
+ return HoldAdapter(context,fragment)
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 通用的适配器。
|
|
|
*/
|
|
|
- class HoldAdapter(context: Context?) : RecyclerView.Adapter<HoldAdapter.HoldHolder>() {
|
|
|
+ class HoldAdapter(context: Context?,fragment : Fragment) : RecyclerView.Adapter<HoldAdapter.HoldHolder>() {
|
|
|
private var list: ArrayList<FutureHoldData>? = null
|
|
|
private var inflater: LayoutInflater? = null
|
|
|
private var context: Context? = null
|
|
|
@@ -51,13 +57,14 @@ class HoldViewModel : BaseViewModel() {
|
|
|
private var green: Drawable? = null
|
|
|
private var priceRed: Int = 0
|
|
|
private var priceGreen: Int = 0
|
|
|
+ private var fragment : Fragment? = null
|
|
|
var futureHoldData: FutureHoldData? = null // 当前选择的item对应的数据
|
|
|
|
|
|
init {
|
|
|
this.list = GlobalDataCollection.instance?.futureHoldData
|
|
|
this.inflater = LayoutInflater.from(context)
|
|
|
this.context = context
|
|
|
-
|
|
|
+ this.fragment = fragment
|
|
|
pink = context?.getDrawable(R.drawable.rma_red)
|
|
|
green = context?.getDrawable(R.drawable.rma_green)
|
|
|
priceGreen = context?.getColor(R.color.p_price_green)!!
|
|
|
@@ -78,8 +85,10 @@ class HoldViewModel : BaseViewModel() {
|
|
|
var idTransDirection = itemView?.findViewById<TextView>(R.id.id_trans_direction) // 方向
|
|
|
var idAvailable = itemView?.findViewById<TextView>(R.id.id_available) // 可用
|
|
|
var idHoldTotal = itemView?.findViewById<TextView>(R.id.id_hold_total) // 持仓
|
|
|
- var idOpenAveragePrice = itemView?.findViewById<TextView>(R.id.id_open_average_price) // 开仓均价
|
|
|
- var idHoldAveragePrice = itemView?.findViewById<TextView>(R.id.id_hold_average_price) // 持仓均价
|
|
|
+ var idOpenAveragePrice =
|
|
|
+ itemView?.findViewById<TextView>(R.id.id_open_average_price) // 开仓均价
|
|
|
+ var idHoldAveragePrice =
|
|
|
+ itemView?.findViewById<TextView>(R.id.id_hold_average_price) // 持仓均价
|
|
|
var idZPl = itemView?.findViewById<TextView>(R.id.id_zpl) // 盯市浮盈
|
|
|
var idPlp = itemView?.findViewById<TextView>(R.id.id_plp) // 盈亏比例
|
|
|
var id_pl = itemView?.findViewById<TextView>(R.id.id_pl)//逐笔浮盈
|
|
|
@@ -97,11 +106,15 @@ class HoldViewModel : BaseViewModel() {
|
|
|
holder.idGoodsName?.text = itemData?.goodsname
|
|
|
holder.idAvailable?.text = itemData?.enableqty.toString() // 可用(总仓可用)
|
|
|
holder.idHoldTotal?.text = itemData?.curpositionqty.toString() // 持仓(总仓数量, 期末头寸)
|
|
|
- holder.idOpenAveragePrice?.text = itemData?.openaverageprice.toString()//开仓均价【头寸变化更新】 = 开仓成本 / 期末头寸 / 合约单位
|
|
|
- holder.idHoldAveragePrice?.text = itemData?.positionaverageprice.toString()//持仓均价【头寸变化更新】= 持仓成本 / 期末头寸 / 合约单位
|
|
|
- holder.idZPl?.text = itemData?.positionpl.toString()//盯市浮盈【实时行情更新】(MTP:浮动盈亏、持仓盈亏) 买方向 = (最新价 - 持仓均价) * 买期末头寸 * 合约单位;卖方向 = (持仓均价 - 最新价) * 卖期末头寸 * 合约单位
|
|
|
+ holder.idOpenAveragePrice?.text =
|
|
|
+ itemData?.openaverageprice.toString()//开仓均价【头寸变化更新】 = 开仓成本 / 期末头寸 / 合约单位
|
|
|
+ holder.idHoldAveragePrice?.text =
|
|
|
+ itemData?.positionaverageprice.toString()//持仓均价【头寸变化更新】= 持仓成本 / 期末头寸 / 合约单位
|
|
|
+ holder.idZPl?.text =
|
|
|
+ itemData?.positionpl.toString()//盯市浮盈【实时行情更新】(MTP:浮动盈亏、持仓盈亏) 买方向 = (最新价 - 持仓均价) * 买期末头寸 * 合约单位;卖方向 = (持仓均价 - 最新价) * 卖期末头寸 * 合约单位
|
|
|
holder.idPlp?.text = itemData?.positionplrate.toString()//持仓盈亏比例【实时行情更新】 = 持仓盈亏 / 开仓成本
|
|
|
- holder.id_pl?.text = itemData?.openpl.toString()//逐笔浮盈【实时行情更新】(MTP:开仓盈亏、平仓盈亏) 买方向 = (最新价 - 开仓均价) * 买期末头寸 * 合约单位;卖方向 = (开仓均价 - 最新价) * 卖期末头寸 * 合约单位
|
|
|
+ holder.id_pl?.text =
|
|
|
+ itemData?.openpl.toString()//逐笔浮盈【实时行情更新】(MTP:开仓盈亏、平仓盈亏) 买方向 = (最新价 - 开仓均价) * 买期末头寸 * 合约单位;卖方向 = (开仓均价 - 最新价) * 卖期末头寸 * 合约单位
|
|
|
holder.idMargin?.text = itemData?.usedmargin.toString()//占用保证金
|
|
|
if (list?.get(position)?.buyorsell == 0) {
|
|
|
holder.idTransDirection?.text = context?.resources?.getString(R.string.str_buy)
|
|
|
@@ -111,11 +124,28 @@ class HoldViewModel : BaseViewModel() {
|
|
|
|
|
|
/** 快捷反手 */
|
|
|
holder.quick_backhand?.onThrottleFirstClick {
|
|
|
-
|
|
|
+ fragment?.createWarningDialog {
|
|
|
+ setTitle("快捷反手确认")
|
|
|
+ setMessage("确认将该合约的所有持仓进行快捷反手操作?")
|
|
|
+ addAction("取消") { dialog, _ -> dialog.dismiss() }
|
|
|
+ addAction("确定") { dialog, _ ->
|
|
|
+ commitDealData(true ,true)
|
|
|
+ dialog.dismiss()
|
|
|
+ }
|
|
|
+ }?.show()
|
|
|
}
|
|
|
|
|
|
/** 一键平仓 **/
|
|
|
holder.key_positions?.onThrottleFirstClick {
|
|
|
+ fragment?.createWarningDialog {
|
|
|
+ setTitle("快捷反手确认")
|
|
|
+ setMessage("确认将该合约的所有持仓进行快捷反手操作?")
|
|
|
+ addAction("取消") { dialog, _ -> dialog.dismiss() }
|
|
|
+ addAction("确定") { dialog, _ ->
|
|
|
+ commitDealData(false ,true)
|
|
|
+ dialog.dismiss()
|
|
|
+ }
|
|
|
+ }?.show()
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -134,7 +164,7 @@ class HoldViewModel : BaseViewModel() {
|
|
|
holder.key_positions?.visibility = View.VISIBLE
|
|
|
holder.id_chart?.visibility = View.VISIBLE
|
|
|
holder.id_entrust_detail?.visibility = View.VISIBLE
|
|
|
- } else if(list?.get(position)?.selected == false) {
|
|
|
+ } else if (list?.get(position)?.selected == false) {
|
|
|
holder.quick_backhand?.visibility = View.GONE
|
|
|
holder.key_positions?.visibility = View.GONE
|
|
|
holder.id_chart?.visibility = View.GONE
|
|
|
@@ -144,7 +174,14 @@ class HoldViewModel : BaseViewModel() {
|
|
|
holder.itemView.onThrottleFirstClick {
|
|
|
clickItem(it!!)
|
|
|
val data = list?.get(position)
|
|
|
- EventBus.getDefault().post(TradeMessageEvent(messageType = EventConstent.TRADEGOODSID,goodsId = data?.goodsid.toString(),buyOrSell = data?.buyorsell ?: 0,outGoodsCode = data?.outgoodscode ?: ""))
|
|
|
+ EventBus.getDefault().post(
|
|
|
+ TradeMessageEvent(
|
|
|
+ messageType = EventConstent.TRADEGOODSID,
|
|
|
+ goodsId = data?.goodsid.toString(),
|
|
|
+ buyOrSell = data?.buyorsell ?: 0,
|
|
|
+ outGoodsCode = data?.outgoodscode ?: ""
|
|
|
+ )
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
}
|
|
|
@@ -154,13 +191,17 @@ class HoldViewModel : BaseViewModel() {
|
|
|
|
|
|
// 选择i
|
|
|
list?.forEach { itemValue ->
|
|
|
- if ((itemValue.goodsid + itemValue.buyorsell) == futureHoldData?.goodsid ?: "" + futureHoldData?.buyorsell) {
|
|
|
- itemValue.selected = !itemValue.selected
|
|
|
- } else {
|
|
|
+ if (itemValue.goodsid == futureHoldData?.goodsid){
|
|
|
+ if (itemValue.buyorsell == futureHoldData?.buyorsell){
|
|
|
+ itemValue.selected = !itemValue.selected
|
|
|
+ }else{
|
|
|
+ itemValue.selected = false
|
|
|
+ }
|
|
|
+ }else{
|
|
|
itemValue.selected = false
|
|
|
}
|
|
|
}
|
|
|
- // 刷新数据
|
|
|
+ // 刷新数据
|
|
|
notifyDataSetChanged()
|
|
|
}
|
|
|
|
|
|
@@ -172,69 +213,83 @@ class HoldViewModel : BaseViewModel() {
|
|
|
* 操作请求
|
|
|
* @param isClose Boolean 是否是平仓单
|
|
|
*/
|
|
|
- private fun commitDealData(isClose: Boolean) {
|
|
|
+ fun commitDealData(isBackHand : Boolean,isClose: Boolean) {
|
|
|
+ (fragment as HoldFragment).loadingDialogStatus.postValue(TaskUiModel.inFlight())
|
|
|
+ var tradePrice = 0.0 //交易价格
|
|
|
+ var itemData : FutureHoldData
|
|
|
+ if (isClose){
|
|
|
+ itemData = futureHoldData ?: FutureHoldData()
|
|
|
+ }else{
|
|
|
+ itemData = GlobalDataCollection.instance?.itemData ?: FutureHoldData()
|
|
|
+ }
|
|
|
val datas = ChannelOrderReqData()
|
|
|
datas.setAccountID(GlobalDataCollection.instance?.accountId ?: 0) // 交易账号
|
|
|
- val goodsQuote = GlobalDataCollection.instance?.getmGoodsInfoClass()?.get(futureHoldData?.outgoodscode)
|
|
|
+ val goodsQuote = GlobalDataCollection.instance?.goodsInfoAndQuotesList?.find {
|
|
|
+ it.outgoodscode == itemData.outgoodscode
|
|
|
+ }
|
|
|
if (isClose) {
|
|
|
- if (futureHoldData?.buyorsell == 0) {
|
|
|
+ if (itemData.buyorsell == 0) {
|
|
|
//平仓---平买单,方向是卖
|
|
|
- datas.setBuyOrSell(0)
|
|
|
+ datas.setBuyOrSell(1)
|
|
|
} else {
|
|
|
//平仓---平卖单,方向是买
|
|
|
- datas.setBuyOrSell(1)
|
|
|
+ datas.setBuyOrSell(0)
|
|
|
}
|
|
|
} else {
|
|
|
- datas.setBuyOrSell(futureHoldData?.buyorsell ?: 0)
|
|
|
+ if (itemData.buyorsell == 0) {
|
|
|
+ //平仓---平买单,方向是卖
|
|
|
+ datas.setBuyOrSell(1)
|
|
|
+ } else {
|
|
|
+ //平仓---平卖单,方向是买
|
|
|
+ datas.setBuyOrSell(0)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //在fragment内设置一个变量用来控制是否在平仓后再进行建仓/且把用来反手建仓的数据暂存在数据中心
|
|
|
+ if (isBackHand) {
|
|
|
+ GlobalDataCollection.instance?.setItemData(itemData)
|
|
|
+ (fragment as HoldFragment).isBackhandByLose = 1
|
|
|
+ }else{
|
|
|
+ (fragment as HoldFragment).isBackhandByLose = 0
|
|
|
}
|
|
|
datas.clientOrderTime = TimeUtils.getNowString() // 客户端委托时间 当前时间
|
|
|
datas.clientType = 3 // 客户端类型: 1客户端 3 手机端 android
|
|
|
- datas.goodsID = futureHoldData?.goodsid ?: 0//商品id
|
|
|
+ datas.goodsID = itemData.goodsid ?: 0//商品id
|
|
|
datas.loginID = GlobalDataCollection.instance?.loginRsp?.loginID ?: 0 //登录账号
|
|
|
|
|
|
- datas.marketID = futureHoldData?.marketid ?: 0
|
|
|
+ datas.marketID = itemData.marketid ?: 0
|
|
|
datas.validType = 1 // 有效类型-1当日有效
|
|
|
- datas.channelOperateType = FutureConstent.ChannelOperateType_ORDER // 操作类型 1:正常类型 默认写死???
|
|
|
+ datas.channelOperateType =
|
|
|
+ FutureConstent.ChannelOperateType_ORDER // 操作类型 1:正常类型 默认写死???
|
|
|
datas.channelOrderSrc = 1 // 单据来源委托来源-1:客户端 //默认写死?
|
|
|
datas.hedgeFlag = 0 // 投机套保标志-0:无
|
|
|
-// if (goodsQuote.getQuoteDay() != null && goodsInfo.getQuoteDay().getAsk() != null) {
|
|
|
-// val decial: Int = goodsInfo.getGoodsInfo().getDecimalPlace()
|
|
|
-// val setp: Double
|
|
|
+ if (goodsQuote?.quoteDayData != null) {
|
|
|
+ val decial = goodsQuote.goodsInfo.decimalplace
|
|
|
+ val setp: Double
|
|
|
// setp = if ("0" == overDot) { //如果没有拿到超价点数则按照原来的计算吧
|
|
|
-// goodsInfo.getGoodsInfo().getQuoteMinUnit() * Math.pow(
|
|
|
-// 10.0,
|
|
|
-// -goodsInfo.getGoodsInfo().getDecimalPlace()
|
|
|
-// )
|
|
|
+//
|
|
|
// } else {
|
|
|
// goodsInfo.getGoodsInfo().getQuoteMinUnit() * Math.pow(
|
|
|
// 10.0,
|
|
|
// -goodsInfo.getGoodsInfo().getDecimalPlace()
|
|
|
// ) * java.lang.Double.valueOf(overDot)
|
|
|
// }
|
|
|
-// val askLimitPrice: String = Utils.roundNum(
|
|
|
-// if (java.lang.Double.valueOf(
|
|
|
-// goodsInfo.getQuoteDay().getAsk()
|
|
|
-// ) != 0
|
|
|
-// ) java.lang.Double.valueOf(
|
|
|
-// goodsInfo.getQuoteDay().getAsk()
|
|
|
-// ) + setp /*limitStep*/ else 0, decial
|
|
|
-// )
|
|
|
-// val bidLimitPrice: String = Utils.roundNum(
|
|
|
-// if (java.lang.Double.valueOf(
|
|
|
-// goodsInfo.getQuoteDay().getBid()
|
|
|
-// ) != 0
|
|
|
-// ) java.lang.Double.valueOf(
|
|
|
-// goodsInfo.getQuoteDay().getBid()
|
|
|
-// ) - setp /*limitStep*/ else 0, decial
|
|
|
-// )
|
|
|
-// if (buyOrSell == 0) {
|
|
|
-// tradePrice = bidLimitPrice.toDouble()
|
|
|
-// } else {
|
|
|
-// tradePrice = askLimitPrice.toDouble()
|
|
|
-// }
|
|
|
-// }
|
|
|
-// datas.orderPrice = orderPrice.toDouble()
|
|
|
- datas.orderQty = futureHoldData?.enableqty?.toLong() ?: 0
|
|
|
+ setp = goodsQuote.goodsInfo.quoteminunit * Math.pow(
|
|
|
+ 10.0,
|
|
|
+ -decial.toDouble()
|
|
|
+ )
|
|
|
+ val askLimitPrice: String =
|
|
|
+ NumberUtils.roundNum(goodsQuote.quoteDayData.ask + setp /*limitStep*/, decial)
|
|
|
+ val bidLimitPrice: String = NumberUtils.roundNum(
|
|
|
+ goodsQuote.quoteDayData.bid - setp /*limitStep*/, decial
|
|
|
+ )
|
|
|
+ if (itemData.buyorsell == 0) {
|
|
|
+ tradePrice = bidLimitPrice.toDouble()
|
|
|
+ } else {
|
|
|
+ tradePrice = askLimitPrice.toDouble()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ datas.orderPrice = tradePrice
|
|
|
+ datas.orderQty = itemData.enableqty?.toLong() ?: 0
|
|
|
|
|
|
if (isClose) {
|
|
|
// 平仓
|
|
|
@@ -271,10 +326,20 @@ class HoldViewModel : BaseViewModel() {
|
|
|
// val bbb: String = Utils.getCommonIp()
|
|
|
// datas.setIp(bbb)
|
|
|
datas.setValidtime("") // 有效时间string
|
|
|
- GlobalScope.launch {
|
|
|
- MyApplication.getInstance()?.futureManager?.tradeRequest(data = datas){isCompleted, err ->
|
|
|
- if (isCompleted){
|
|
|
|
|
|
+ GlobalScope.launch {
|
|
|
+ MyApplication.getInstance()?.futureManager?.tradeRequest(data = datas) { isCompleted, err ->
|
|
|
+ if (isCompleted) {
|
|
|
+ if ((fragment as HoldFragment).isBackhandByLose == 1){
|
|
|
+ commitDealData(false ,false)
|
|
|
+ }else{
|
|
|
+ (fragment as HoldFragment).loadingDialogStatus.postValue(TaskUiModel.success(msg = "请求成功"))
|
|
|
+ (fragment as HoldFragment).onRefresh()
|
|
|
+ }
|
|
|
+ }else{
|
|
|
+ (fragment as HoldFragment).loadingDialogStatus.postValue(TaskUiModel.failed(
|
|
|
+ InteractiveException(errorMessage = err?.message!!)
|
|
|
+ ))
|
|
|
}
|
|
|
}
|
|
|
}
|