package cn.muchinfo.rma.global

import cn.muchinfo.rma.view.autoWidget.utils.NumberUtils
import com.blankj.utilcode.util.TimeUtils
import java.text.SimpleDateFormat
import java.util.*

/**
 * 页面显示的时间
 * @receiver String
 * @param format String 页面显示的时间格式
 * @return String
 */
fun String.isShowTimeString(format : String) : String{
    if (this.isNullOrEmpty()){
        return "--"
    }
    val mill = TimeUtils.string2Millis(this)
    val time = TimeUtils.millis2String(mill,TimeUtils.getSafeDateFormat(format))
    return time
}

fun String.isBlankString() : String{
    if (this.isNullOrEmpty()){
        return "--"
    }
    return this
}

/**
 * 金额数量不存在时显示0
 * @receiver String
 * @return String
 */
fun String.isBlankNumber() : String{
    if (this.isNullOrEmpty()){
        return "0"
    }
    return this
}

fun String.toPercentage() : String{
    if (this.isEmpty()){
        return "--"
    }
    return NumberUtils.roundNum(toDouble().times(100),2) + "%"
}

/**
 * 成交单类型
 */
fun String.channelbuildtype() : String{
    if (this.isNullOrEmpty()){
        return "--"
    }
    return  if (this == "1"){"建仓"} else{"平仓"}
}

/**
 * 和isShowTimeString的接收时间格式不同
 * @receiver String
 * @return String
 */
fun String.toShowTime(format : String = "yyyy-MM-dd HH:mm:ss") : String{
    if (this.isNullOrEmpty()){
        return "--"
    }
    val sdf = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss+08:00", Locale.CHINA)
    val sdf1 = SimpleDateFormat(format, Locale.CHINA)
    var timeString = sdf1.format(sdf.parse(this)!!)
    if (TimeUtils.isToday(timeString)){
        timeString = timeString.split(" ").get(1)
    }
    return timeString
}

/**
 * 历史委托单的状态
 */
fun String.channelinnerorderstatus() : String{
    if (this.isNullOrEmpty()){
        return "--"
    }
    var str = ""
    when {
        this == "1" -> {
            str = "委托请求"
        }
        this == "2" -> {
            str = "冻结成功"
        }
        this == "3" -> {
            str = "委托失败"
        }
        this == "4" -> {
            str = "委托部成部失败"
        }
        this == "5" -> {
            str = "委托成功"
        }
        this == "6" -> {
            str = "全部撤销"
        }
        this == "7" -> {
            str = "部成部撤"
        }
        this == "8" -> {
            str = "部成部撤部失败"
        }
        this == "9" -> {
            str = "全部成交"
        }
    }

    return str
}


