package com.casic.br.extensions import android.content.Context import android.util.Log import com.casic.br.R import com.casic.br.callback.OnImageCompressListener import com.casic.br.model.ErrorMessageModel import com.casic.br.utils.LocaleConstant import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.pengxh.kt.lite.extensions.createCompressImageDir import com.pengxh.kt.lite.extensions.toJson import com.pengxh.kt.lite.utils.SaveKeyValues import org.json.JSONObject import top.zibin.luban.Luban import top.zibin.luban.OnCompressListener import java.io.File import java.text.ParseException import java.text.SimpleDateFormat import java.util.* import kotlin.math.abs /** * String扩展方法 */ fun String.createCommand(value: Any): String { val map = HashMap<String, Any>() map[this] = value val command = map.toJson() Log.d("kTag", "createCommand: $command") return command } fun String.compressImage(context: Context, listener: OnImageCompressListener) { Luban.with(context) .load(this) .ignoreBy(100) .setTargetDir(context.createCompressImageDir().toString()) .filter { !(it.isBlank() || it.lowercase(Locale.getDefault()).endsWith(".gif")) } .setCompressListener(object : OnCompressListener { override fun onStart() { } override fun onSuccess(file: File) { listener.onSuccess(file) } override fun onError(e: Throwable) { listener.onError(e) } }).launch() } /** * 时间差-天 * */ fun String.diffDate(needFormat: Boolean): Int { if (this.isBlank()) { return 0 } val diff = if (needFormat) { val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA) val date = simpleDateFormat.parse(this)!! abs(System.currentTimeMillis() - date.time) } else { try { abs(System.currentTimeMillis() - this.toLong() * 1000L) } catch (e: ParseException) { e.printStackTrace() } } as Long return (diff / (86400000)).toInt() } fun String.separateResponseCode(): Int { if (this.isBlank()) { return 404 } return JSONObject(this).getInt("code") } fun String.toErrorMessage(): String { val errorModel = Gson().fromJson<ErrorMessageModel>( this, object : TypeToken<ErrorMessageModel>() {}.type ) return errorModel.message.toString() } //拼接图片地址 fun String.combineImagePath(): String { if (this.isEmpty()) return this val defaultValue = SaveKeyValues.getValue( LocaleConstant.DEFAULT_SERVER_CONFIG, LocaleConstant.SERVER_BASE_URL ) as String return "$defaultValue/static/${this.replace("\\", "/")}" } /** * 时间差-分钟 * */ fun String.diffMinute(): Int { if (this.isBlank()) { return 0 } try { val diff = abs(System.currentTimeMillis() - this.toLong()) println(diff) return (diff / (60000)).toInt() } catch (e: ParseException) { e.printStackTrace() } return 0 } fun String.toChineseTypeName(): String { if (this.isBlank()) { return "其他" } return when (this) { "yyj" -> "智能油烟机" "rs" -> "智能热水器" "rq" -> "智能燃气灶" "bgl" -> "智能壁挂炉" "pc" -> "Wi-Fi智能插排" "cz" -> "Wi-Fi智能插座" "wg2" -> "智能网关" "wsdcg" -> "温湿度传感器" "ywbj" -> "火灾报警器" "rqbj" -> "燃气报警器" "cobj" -> "一氧化碳报警器" "yinsj" -> "即热式台式饮水机" "kj" -> "空气净化器" "sd" -> "智能扫地机器人" else -> "其他" } } //电量转图片 fun String.toBatteryImage(): Int { if (this.isBlank()) { return R.drawable.ic_battery_0 } try { when (this.toInt()) { in 0..10 -> return R.drawable.ic_battery_0 in 11..30 -> return R.drawable.ic_battery_1 in 31..60 -> return R.drawable.ic_battery_2 in 61..90 -> return R.drawable.ic_battery_3 in 91..100 -> return R.drawable.ic_battery_4 } } catch (e: ClassCastException) { e.printStackTrace() } return R.drawable.ic_battery_0 } fun String.convertValue(deciPoint: Int): Double { if (this.isBlank()) { return 0.0 } when (deciPoint) { 0 -> return this.toDouble() 1 -> return (this.toDouble()) / 10 2 -> return (this.toDouble()) / 100 3 -> return (this.toDouble()) / 1000 4 -> return (this.toDouble()) / 10000 } return this.toDouble() } fun String.toBrandCode(): String { return when (this) { "rs" -> "PP0001" "yyj" -> "PP0001" "bgl" -> "PP0001" "rq" -> "PP0001" "pc" -> "PP0002" "cz" -> "PP0002" "wg2" -> "PP0002" "0204" -> "PP0002" "wsdcg" -> "PP0002" "ywbj" -> "PP0002" "rqbj" -> "PP0002" "cobj" -> "PP0002" "yinsj" -> "PP0002" "kj" -> "PP0002" "sd" -> "PP0002" else -> "PP0004" } } fun String.toBrandName(): String { return when (this) { "rs" -> "万家乐" "yyj" -> "万家乐" "bgl" -> "万家乐" "rq" -> "万家乐" "pc" -> "涂鸦" "cz" -> "涂鸦" "wg2" -> "涂鸦" "0204" -> "涂鸦" "wsdcg" -> "涂鸦" "ywbj" -> "涂鸦" "rqbj" -> "涂鸦" "cobj" -> "涂鸦" "yinsj" -> "涂鸦" "kj" -> "涂鸦" "sd" -> "涂鸦" else -> "汉威" } } fun String.toProductCode(): String { return when (this) { "rs" -> "JSQ30-16RS051" "yyj" -> "CXW-320-CXS051" "bgl" -> "LL1GBQ28-28LS051" "rq" -> "JZT-QZS051" "pc" -> "MS800" "cz" -> "MC010" "wg2" -> "DMD8CE" "0204" -> "MD180" "wsdcg" -> "TT180" "ywbj" -> "LH-601ZB" "rqbj" -> "LT-LH-801" "cobj" -> "HS1CA" "yinsj" -> "WM001-W" "kj" -> "TT100" "sd" -> "CR101" else -> "JT-KBZ1" } } fun String.toInternationalCode(): String { return when (this) { "yyj" -> "6973046010620" "rs" -> "6973046010606" "bgl" -> "6973046010590" "rq" -> "6973046010613" else -> "" } } fun String.toProductSize(): String { return when (this) { "rs" -> "590*340*160" "yyj" -> "800*420*318" "bgl" -> "800*420*318" "rq" -> "800*450*150" "pc" -> "86*86*29" "cz" -> "39*60*51" "wg2" -> "" "0204" -> "" "wsdcg" -> "" "ywbj" -> "" "rqbj" -> "70x70x28" "cobj" -> "60*60*49.2" "yinsj" -> "20.5*20.5*16.0" "kj" -> "" "sd" -> "" else -> "89×89×32" } } fun String.toProductWeight(): String { return when (this) { "rs" -> "13kg" "yyj" -> "23kg" "bgl" -> "41.5kg" "rq" -> "14kg" "pc" -> "0.5kg" "yinsj" -> "2kg" "kj" -> "2kg" "sd" -> "2kg" else -> "0.2kg" } } fun String.toProductQuality(): String { return when (this) { "yyj" -> "钢化玻璃+冷板喷涂" "rs" -> "底盖Q235" "bgl" -> "预涂板1.0" "rq" -> "玻璃" else -> "" } } fun String.toProductColor(): String { return when (this) { "yyj" -> "黑色" "bgl" -> "高光白" "rq" -> "黑色" else -> "白色" } }