|  |  |  | @ -5,18 +5,17 @@ import android.view.View | 
		
	
		
			
				|  |  |  |  | import androidx.activity.result.contract.ActivityResultContracts | 
		
	
		
			
				|  |  |  |  | import androidx.lifecycle.lifecycleScope | 
		
	
		
			
				|  |  |  |  | import androidx.recyclerview.widget.LinearLayoutManager | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.DBUtils | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.entity.Gas | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.R | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.TestUtils | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.databinding.ActivityQueryDataBinding | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.base.BaseActivity | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.constant.DEFAULT_QUERY_TIME_INTERVAL | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.constant.TimeStep | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.DBUtils | 
		
	
		
			
				|  |  |  |  | import com.common.commonlib.db.entity.Gas | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.utils.DateUtils | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.ui.adapter.HistoryDataAdapter | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.ui.view.CommonTopBar | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.ui.view.CommonDialog | 
		
	
		
			
				|  |  |  |  | import com.yinuo.safetywatcher.watcher.utils.DateUtils | 
		
	
		
			
				|  |  |  |  | import kotlinx.coroutines.Dispatchers | 
		
	
		
			
				|  |  |  |  | import kotlinx.coroutines.launch | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
	
		
			
				
					|  |  |  | @ -111,83 +110,77 @@ class QueryDataActivity : BaseActivity() { | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |     private fun queryData() { | 
		
	
		
			
				|  |  |  |  |         lifecycleScope.launch { | 
		
	
		
			
				|  |  |  |  |             // 根据步长,多个数据合一。合一步长 | 
		
	
		
			
				|  |  |  |  |             val step = when (timeStep) { | 
		
	
		
			
				|  |  |  |  |                 TimeStep.SECOND_30 -> 1 | 
		
	
		
			
				|  |  |  |  |                 TimeStep.MINUTE_1 -> 2 | 
		
	
		
			
				|  |  |  |  |                 TimeStep.MINUTE_3 -> 6 | 
		
	
		
			
				|  |  |  |  |                 TimeStep.MINUTE_5 -> 10 | 
		
	
		
			
				|  |  |  |  |             // 根据步长,多个数据合一。间隔时长 | 
		
	
		
			
				|  |  |  |  |             val intervalMs = when (timeStep) { | 
		
	
		
			
				|  |  |  |  |                 TimeStep.SECOND_30 -> 30_000 | 
		
	
		
			
				|  |  |  |  |                 TimeStep.MINUTE_1 -> 60_000 | 
		
	
		
			
				|  |  |  |  |                 TimeStep.MINUTE_3 -> 180_000 | 
		
	
		
			
				|  |  |  |  |                 TimeStep.MINUTE_5 -> 300_000 | 
		
	
		
			
				|  |  |  |  |                 else -> 1 | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |             // 全量数据 | 
		
	
		
			
				|  |  |  |  |             val gasDao = DBUtils.gasDao() | 
		
	
		
			
				|  |  |  |  |             val gasList = gasDao.getAllByTime(startTime, endTime) | 
		
	
		
			
				|  |  |  |  |             if (step == 1) { | 
		
	
		
			
				|  |  |  |  |                 val sortList = mutableListOf<Gas>() | 
		
	
		
			
				|  |  |  |  |                 sortList.apply { | 
		
	
		
			
				|  |  |  |  |                     addAll(gasList) | 
		
	
		
			
				|  |  |  |  |                     sortWith { o1, o2 -> | 
		
	
		
			
				|  |  |  |  |                         (o2.time - o1.time).toInt() | 
		
	
		
			
				|  |  |  |  |             // 多气体,分开创建list,存储之后。 再分步 | 
		
	
		
			
				|  |  |  |  |             val gasTypeDao = DBUtils.gasTypeDao() | 
		
	
		
			
				|  |  |  |  |             val gasTypes = gasTypeDao.getAll() | 
		
	
		
			
				|  |  |  |  |             // 每种气体建立一个list | 
		
	
		
			
				|  |  |  |  |             val gasMap = HashMap<String, MutableList<Gas>>() | 
		
	
		
			
				|  |  |  |  |             // 预先创建分步合一之后的list | 
		
	
		
			
				|  |  |  |  |             val gasNewMap = HashMap<String, MutableList<Gas>>() | 
		
	
		
			
				|  |  |  |  |             gasTypes.forEach { | 
		
	
		
			
				|  |  |  |  |                 gasMap[it.type] = mutableListOf() | 
		
	
		
			
				|  |  |  |  |                 gasNewMap[it.type] = mutableListOf() | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |             // 分开各个气体的数据 | 
		
	
		
			
				|  |  |  |  |             gasList.forEach { | 
		
	
		
			
				|  |  |  |  |                 val list = gasMap[it.gasName] | 
		
	
		
			
				|  |  |  |  |                 list?.add(it) | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |             //根据步长分割数据填充新的列表 | 
		
	
		
			
				|  |  |  |  |             gasMap.forEach { (type, list) -> | 
		
	
		
			
				|  |  |  |  |                 val newMapList = gasNewMap[type] | 
		
	
		
			
				|  |  |  |  |                 var startGas: Gas? = null | 
		
	
		
			
				|  |  |  |  |                 var gasValue = 0.0 | 
		
	
		
			
				|  |  |  |  |                 var count = 0 | 
		
	
		
			
				|  |  |  |  |                 list.forEachIndexed { index, gas -> | 
		
	
		
			
				|  |  |  |  |                     if (startGas == null) { | 
		
	
		
			
				|  |  |  |  |                         startGas = gas | 
		
	
		
			
				|  |  |  |  |                         gasValue = 0.0 | 
		
	
		
			
				|  |  |  |  |                         count = 0 | 
		
	
		
			
				|  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 launch(Dispatchers.Main) { | 
		
	
		
			
				|  |  |  |  |                     mAdapter.setData(sortList) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |             } else { | 
		
	
		
			
				|  |  |  |  |                 // 多气体,分开创建list,存储之后。 再分步 | 
		
	
		
			
				|  |  |  |  |                 val gasTypeDao = DBUtils.gasTypeDao() | 
		
	
		
			
				|  |  |  |  |                 val gasTypes = gasTypeDao.getAll() | 
		
	
		
			
				|  |  |  |  |                 // 每种气体建立一个list | 
		
	
		
			
				|  |  |  |  |                 val gasMap = HashMap<String, MutableList<Gas>>() | 
		
	
		
			
				|  |  |  |  |                 // 预先创建分步合一之后的list | 
		
	
		
			
				|  |  |  |  |                 val gasNewMap = HashMap<String, MutableList<Gas>>() | 
		
	
		
			
				|  |  |  |  |                 gasTypes.forEach { | 
		
	
		
			
				|  |  |  |  |                     gasMap[it.type] = mutableListOf() | 
		
	
		
			
				|  |  |  |  |                     gasNewMap[it.type] = mutableListOf() | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 // 分开各个气体的数据 | 
		
	
		
			
				|  |  |  |  |                 gasList.forEach { | 
		
	
		
			
				|  |  |  |  |                     val list = gasMap[it.gasName] | 
		
	
		
			
				|  |  |  |  |                     list?.add(it) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 //根据步长分割数据填充新的列表 | 
		
	
		
			
				|  |  |  |  |                 gasMap.forEach { (type, list) -> | 
		
	
		
			
				|  |  |  |  |                     val newMapList = gasNewMap[type] | 
		
	
		
			
				|  |  |  |  |                     var gasValue = 0.0 | 
		
	
		
			
				|  |  |  |  |                     var startGas: Gas? = null | 
		
	
		
			
				|  |  |  |  |                     list.forEachIndexed { index, gas -> | 
		
	
		
			
				|  |  |  |  |                         if (index % step == 0) { | 
		
	
		
			
				|  |  |  |  |                             if (gasValue != 0.0) { | 
		
	
		
			
				|  |  |  |  |                                 startGas!!.gasValue = gasValue / step | 
		
	
		
			
				|  |  |  |  |                                 newMapList?.add(startGas!!) | 
		
	
		
			
				|  |  |  |  |                             } | 
		
	
		
			
				|  |  |  |  |                             startGas = gas | 
		
	
		
			
				|  |  |  |  |                             gasValue = gas.gasValue | 
		
	
		
			
				|  |  |  |  |                         } else { | 
		
	
		
			
				|  |  |  |  |                             gasValue += gas.gasValue | 
		
	
		
			
				|  |  |  |  |                     if (gas.time - startGas!!.time > intervalMs) { | 
		
	
		
			
				|  |  |  |  |                         if (gasValue != 0.0) { | 
		
	
		
			
				|  |  |  |  |                             startGas!!.gasValue = gasValue / count | 
		
	
		
			
				|  |  |  |  |                             newMapList?.add(startGas!!) | 
		
	
		
			
				|  |  |  |  |                         } | 
		
	
		
			
				|  |  |  |  |                         // 最后一个特殊处理 | 
		
	
		
			
				|  |  |  |  |                         if (list.size - 1 == index) { | 
		
	
		
			
				|  |  |  |  |                             // 不能整除的时候 | 
		
	
		
			
				|  |  |  |  |                             val dev = if (list.size % step == 0) step else list.size % step | 
		
	
		
			
				|  |  |  |  |                             if (gasValue != 0.0) { | 
		
	
		
			
				|  |  |  |  |                                 startGas!!.gasValue = gasValue / dev | 
		
	
		
			
				|  |  |  |  |                                 newMapList?.add(startGas!!) | 
		
	
		
			
				|  |  |  |  |                             } | 
		
	
		
			
				|  |  |  |  |                         startGas = gas | 
		
	
		
			
				|  |  |  |  |                         gasValue = gas.gasValue | 
		
	
		
			
				|  |  |  |  |                         count = 1 | 
		
	
		
			
				|  |  |  |  |                     } else { | 
		
	
		
			
				|  |  |  |  |                         gasValue += gas.gasValue | 
		
	
		
			
				|  |  |  |  |                         count++ | 
		
	
		
			
				|  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |                     // 最后一个特殊处理 | 
		
	
		
			
				|  |  |  |  |                     if (list.size - 1 == index) { | 
		
	
		
			
				|  |  |  |  |                         if (gasValue != 0.0) { | 
		
	
		
			
				|  |  |  |  |                             startGas!!.gasValue = gasValue / count | 
		
	
		
			
				|  |  |  |  |                             newMapList?.add(startGas!!) | 
		
	
		
			
				|  |  |  |  |                         } | 
		
	
		
			
				|  |  |  |  |                     } | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  | 
 | 
		
	
		
			
				|  |  |  |  |                 val newList = mutableListOf<Gas>() | 
		
	
		
			
				|  |  |  |  |                 gasNewMap.forEach { (t, u) -> | 
		
	
		
			
				|  |  |  |  |                     newList.addAll(u) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 newList.sortWith { o1, o2 -> | 
		
	
		
			
				|  |  |  |  |                     (o2.time - o1.time).toInt() | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |                 launch(Dispatchers.Main) { | 
		
	
		
			
				|  |  |  |  |                     mAdapter.setData(newList) | 
		
	
		
			
				|  |  |  |  |                 } | 
		
	
		
			
				|  |  |  |  |             val newList = mutableListOf<Gas>() | 
		
	
		
			
				|  |  |  |  |             gasNewMap.forEach { (t, u) -> | 
		
	
		
			
				|  |  |  |  |                 newList.addAll(u) | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |             newList.sortWith { o1, o2 -> | 
		
	
		
			
				|  |  |  |  |                 (o2.time - o1.time).toInt() | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |             launch(Dispatchers.Main) { | 
		
	
		
			
				|  |  |  |  |                 mAdapter.setData(newList) | 
		
	
		
			
				|  |  |  |  |             } | 
		
	
		
			
				|  |  |  |  |         } | 
		
	
		
			
				|  |  |  |  |     } | 
		
	
	
		
			
				
					|  |  |  | 
 |