WarehousingActivity.kt 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. package xn.hxp.ui.warehousing
  2. import android.os.Bundle
  3. import android.os.CountDownTimer
  4. import android.os.Handler
  5. import android.os.Looper
  6. import android.view.KeyEvent
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.widget.EditText
  10. import android.widget.Toast
  11. import androidx.core.content.ContextCompat
  12. import androidx.lifecycle.lifecycleScope
  13. import androidx.recyclerview.widget.LinearLayoutManager
  14. import androidx.viewbinding.ViewBinding
  15. import com.blankj.utilcode.util.LogUtils
  16. import com.bumptech.glide.Glide
  17. import com.bumptech.glide.load.engine.DiskCacheStrategy
  18. import com.bumptech.glide.request.RequestOptions
  19. import com.rc.core.ui.activity.BaseActivity
  20. import com.rc.httpcore.HttpConfig
  21. import com.rc.httpcore.bean.*
  22. import com.rc.httpcore.client.ApiRepository
  23. import com.rc.httpcore.client.HttpTool
  24. import com.rc.httpcore.exception.NetException
  25. import com.sun.jna.Pointer
  26. import kotlinx.coroutines.*
  27. import retrofit2.HttpException
  28. import xn.hxp.R
  29. import xn.hxp.app.ChemicalApp
  30. import xn.hxp.blu.BluetoothConnectionManager
  31. import xn.hxp.comm.Constants
  32. import xn.hxp.databinding.ActivityWarehousingBinding
  33. import xn.hxp.ui.PrintBean
  34. import xn.hxp.ui.adapter.ChemicalsAdapter
  35. import xn.hxp.ui.adapter.InventoryListAdapter
  36. import xn.hxp.ui.verify.TwoVerificationActivity
  37. import xn.hxp.utils.*
  38. import xn.hxp.utils.bluetooth.BluetoothTool
  39. import xn.hxp.utils.bluetooth.BluetoothTool.BluetoothCallBack
  40. import xn.hxp.weidith.*
  41. import java.lang.Runnable
  42. import java.math.BigDecimal
  43. import java.net.ConnectException
  44. import java.net.SocketTimeoutException
  45. import java.nio.charset.StandardCharsets
  46. //待入库
  47. class WarehousingActivity : BaseActivity() {
  48. private val mAdapter by lazy { InventoryListAdapter(this) }
  49. private val mAdapterChemicals by lazy { ChemicalsAdapter(this) }
  50. private var mChemicalInfoBean: ChemicalInfoBean? = null //实验室信息
  51. private var mListData: List<RuleBean>? = null //柜子信息
  52. private var mLabelDialog: LabelDialog? = null //化学品信息生成
  53. // private var jobTow: Job? = null
  54. private var mWeighingValue: EditText? = null
  55. private var mStockModeBean = StockModeBean(mutableListOf())
  56. private var mStockModelList = mutableListOf<StockModel>()
  57. private lateinit var timeUpdater: TimeUpdater
  58. private var mWeighDialog: WeighDialog? = null
  59. private var mCabinetId = "" //柜子id
  60. private var mCabinetName = "" //柜子名称
  61. private var mDoorName = "" //柜门名称
  62. private var mDoorId: String = ""
  63. private lateinit var mUnifiedVerBean: UnifiedVerBean //当前是否需要双人认证
  64. private var mLayers = 1 //柜子层数
  65. private var mLayersTow = 1 //柜子选择层数
  66. private var mCabinetLockVoList: List<LockVoListBean>? = null //柜锁会又多个
  67. private var mLockStr = ArrayList<String>() // 需要打开得柜锁数量
  68. private val handlerUtil = HandlerUtil.getInstance()
  69. private var mJoinType = 1 //称重方式 1 称重,2 录入
  70. private var mPages = 1 //总页码
  71. private var mDataPage = -1 //返回得总条数
  72. private var page = 1
  73. private var pageSize = 10
  74. private var mPrintTag = 0 //需要打印得内容条数
  75. private var mStockDetailsModel: MutableList<StockDetailsModel>? = null
  76. private var mLayer: Int = -1
  77. private val handlerPrint = HandlerUtil.getInstance()
  78. private var h = Pointer.NULL
  79. private var pritBot = false
  80. private var isClisLit = false //是否点击称重关闭按钮
  81. // private lateinit var mChemicalConfsBean: ChemicalConfsBean //获取化学品的称重超过比例or
  82. lateinit var viewBinding: ActivityWarehousingBinding
  83. override fun setViewBinding(): ViewBinding {
  84. viewBinding = ActivityWarehousingBinding.inflate(layoutInflater)
  85. return viewBinding
  86. }
  87. override fun onInit() {
  88. initTitle()
  89. initAdapter()
  90. viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"
  91. viewBinding.tvReturn.setOnClickListener {
  92. finish()
  93. }
  94. viewBinding.tvOutLogin.setOnClickListener {
  95. HttpTool.logout()
  96. }
  97. //查询后根据返回集合数量 判断当天的显示
  98. viewBinding.options.setOnClickListener {
  99. //类别获取
  100. getOptions()
  101. }
  102. viewBinding.reSou.setOnClickListener {
  103. page = 1
  104. waitList()
  105. }
  106. //选择柜子信息
  107. viewBinding.imgPosition.setOnClickListener {
  108. // mType = 1, 可以选择柜层
  109. val storageDialog =
  110. StorageDialog(ct = this,
  111. listData = mListData!!,
  112. lint = object : StorageDialog.IonClickListener {
  113. override fun onClick(
  114. cabinetId: String,
  115. cabinetName: String,
  116. doorName: String,
  117. doorId: String,
  118. cabinetLockVoList: List<LockVoListBean>,
  119. layers: Int,
  120. lockNum: String
  121. ) {
  122. mCabinetId = cabinetId
  123. mCabinetName = cabinetName
  124. mDoorName = doorName
  125. mDoorId = doorId
  126. mCabinetLockVoList = cabinetLockVoList
  127. viewBinding.cabinet.text = "存储位置:${cabinetName}-${doorName}"
  128. viewBinding.cabinet.tag = "$doorId"
  129. mLayers = layers //当前需要显示得层数
  130. if (mStockModelList != null && mStockModelList.size > 0) {
  131. mStockModelList.forEach {
  132. it.cabinetId = mCabinetId
  133. it.cabinetName = mCabinetName
  134. it.doorId = mDoorId
  135. it.doorName = mDoorName
  136. it.layers = mLayers
  137. }
  138. }
  139. //
  140. mAdapterChemicals.updateData(mLayers)
  141. }
  142. override fun onClose() {
  143. }
  144. })
  145. storageDialog.show()
  146. }
  147. viewBinding.butOpen.setOnClickListener {
  148. if (mCabinetLockVoList != null && mCabinetLockVoList!!.size > 0) {
  149. mLockStr.clear()
  150. mCabinetLockVoList!!.forEach { it ->
  151. when (it.unlockingMethod) {
  152. 2 -> { //智能锁
  153. mLockStr.add("${it.lockNum}")
  154. }
  155. }
  156. }
  157. if (mLockStr != null && mLockStr.size > 0) {
  158. openLock(mLockStr)
  159. } else {
  160. subAddData(0)
  161. }
  162. } else {
  163. subAddData(0)
  164. }
  165. }
  166. //待入库 确认入库
  167. // viewBinding.subAdd.setOnClickListener {
  168. ButtonDebouncer.setDebouncedOnClickListener(viewBinding.subAdd, 500L) {
  169. if (mAdapterChemicals.data.size > 0) {
  170. if (mUnifiedVerBean.verify) {
  171. //需要双人认证 1人脸 2刷卡 3扫码 获取基础信息配置
  172. if (ChemicalApp.confs!!.verifyType.length == 1) {
  173. val map = mutableMapOf<String, Any>()
  174. var chemicalLevel = 2
  175. val dataList = mAdapterChemicals.data
  176. dataList.forEach {
  177. if (it.chemicalLevel == 1) {
  178. chemicalLevel = it.chemicalLevel!!
  179. return@forEach
  180. }
  181. }
  182. map["chemicalLevel"] = chemicalLevel //管控类型
  183. map["doorId"] = "${viewBinding.cabinet.tag}" //柜子id
  184. map["mTag"] = 2
  185. when (ChemicalApp.confs!!.verifyType) {
  186. "1" -> {//人脸
  187. map["mVerTyps"] = 1
  188. }
  189. "2" -> {//刷卡
  190. map["mVerTyps"] = 2
  191. }
  192. "3" -> {//扫码
  193. map["mVerTyps"] = 3
  194. }
  195. }
  196. UiManager.switcher(
  197. this,
  198. map,
  199. TwoVerificationActivity::class.java
  200. )
  201. // map["hides"] = 4 //隐藏扫码
  202. // when (ChemicalApp.confs!!.verifyType) {
  203. // "1" -> {//人脸
  204. // UiManager.switcher(
  205. // this,
  206. // map,
  207. // TwoPersonActivity::class.java,
  208. // Constants.REQUEST_CODE
  209. // )
  210. // }
  211. //
  212. // "2" -> {//刷卡
  213. // UiManager.switcher(
  214. // this,
  215. // map,
  216. // SwipeCodeTwoActivity::class.java,
  217. // Constants.REQUEST_CODE
  218. // )
  219. //
  220. // }
  221. // "3" -> {//扫码
  222. // UiManager.switcher(
  223. // this,
  224. // map,
  225. // ScanCodeTwoActivity::class.java,
  226. // Constants.REQUEST_CODE
  227. // )
  228. // }
  229. // }
  230. } else {
  231. verifyChecking()
  232. }
  233. } else {
  234. if (mCabinetLockVoList != null && mCabinetLockVoList!!.size > 0) {
  235. mLockStr.clear()
  236. mCabinetLockVoList!!.forEach { it ->
  237. when (it.unlockingMethod) {
  238. 2 -> { //智能锁
  239. mLockStr.add("${it.lockNum}")
  240. }
  241. }
  242. }
  243. if (mLockStr != null && mLockStr.size > 0) {
  244. openLock(mLockStr)
  245. } else {
  246. subAddData(0)
  247. }
  248. } else {
  249. subAddData(0)
  250. }
  251. }
  252. } else {
  253. showToast("缺少录入信息")
  254. }
  255. }
  256. viewBinding.butUpper.setOnClickListener {
  257. //上一页
  258. if (mDataPage != 0) {
  259. if (page != 1) {
  260. page--
  261. getRelList() //刷新页面
  262. }
  263. }
  264. }
  265. viewBinding.butNext.setOnClickListener {
  266. //下一页
  267. if (mDataPage != 0) {
  268. if (mDataPage != page) {
  269. page++
  270. getRelList() //刷新页面
  271. }
  272. }
  273. }
  274. val handler = Handler(Looper.getMainLooper())
  275. timeUpdater = TimeUpdater(handler) { currentTime ->
  276. viewBinding.nowTime.text = "$currentTime"
  277. }
  278. // 启动定时更新
  279. timeUpdater.startUpdating()
  280. getRelList()//获取实验室 信息
  281. }
  282. //验证方式
  283. private fun verifyChecking() {
  284. val map = mutableMapOf<String, Any>()
  285. var chemicalLevel = 2
  286. val dataList = mAdapterChemicals.data
  287. dataList.forEach {
  288. if (it.chemicalLevel == 1) {
  289. chemicalLevel = it.chemicalLevel!!
  290. return@forEach
  291. }
  292. }
  293. map["chemicalLevel"] = chemicalLevel //管控类型
  294. map["doorId"] = "${viewBinding.cabinet.tag}" //柜子id
  295. map["mTag"] = 2
  296. map["mVerTyps"] = 0
  297. if (ChemicalApp.confs!!.verifyType == "1,2" || ChemicalApp.confs!!.verifyType == "2,1") {
  298. //人脸+刷卡
  299. map["hides"] = 1 //隐藏扫码
  300. // UiManager.switcher(this, map, TwoPersonActivity::class.java)
  301. } else if (ChemicalApp.confs!!.verifyType == "1,3" || ChemicalApp.confs!!.verifyType == "3,1") {
  302. //人脸+扫码
  303. map["hides"] = 2 //隐藏刷卡
  304. // UiManager.switcher(this, map, TwoPersonActivity::class.java)
  305. } else if (ChemicalApp.confs!!.verifyType == "2,3" || ChemicalApp.confs!!.verifyType == "3,2") {
  306. map["hides"] = 3 //隐藏扫码
  307. // UiManager.switcher(this, map, SwipeCodeTwoActivity::class.java)
  308. } else {
  309. //人脸+刷卡+扫码
  310. map["hides"] = 4
  311. // UiManager.switcher(this, map, TwoPersonActivity::class.java)
  312. }
  313. UiManager.switcher(
  314. this,
  315. map,
  316. TwoVerificationActivity::class.java,
  317. Constants.REQUEST_CODE
  318. )
  319. }
  320. private fun initTitle() {
  321. viewBinding.tvName.text = ChemicalApp.userData!!.userName
  322. val imageView = viewBinding.imageName
  323. // 使用 Glide 加载网络图片
  324. Glide.with(this)
  325. .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.userData!!.avatar}")
  326. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  327. .into(imageView)
  328. // 使用 Glide 加载网络图片
  329. viewBinding.deptName.text = "${ChemicalApp.confs!!.deptName}-${ChemicalApp.confs!!.roomNum}"
  330. Glide.with(this)
  331. .load("${HttpConfig.API_BASE_IMG_URL}${ChemicalApp.confs!!.circularLogo}")
  332. .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
  333. .into(viewBinding.image)
  334. }
  335. override fun onBackPressed() {
  336. super.onBackPressed()
  337. finish()
  338. }
  339. override fun cdTime(cd: Int) {
  340. viewBinding.tvReturn.text = "返回${cd}s"
  341. }
  342. private fun initAdapter() {
  343. //入库列表信息
  344. viewBinding.rvInventory?.layoutManager = LinearLayoutManager(this)
  345. viewBinding.rvInventory?.adapter = mAdapter
  346. //待入库清单
  347. viewBinding.recyclerViewList?.layoutManager = LinearLayoutManager(this)
  348. viewBinding.recyclerViewList?.adapter = mAdapterChemicals
  349. mAdapterChemicals.setOnItemChildClickListener { adapter, view, position ->
  350. // 在这里处理子View的点击事件
  351. when (view.id) {
  352. R.id.imgDelete -> {
  353. mAdapterChemicals.data.removeAt(position)
  354. mStockModelList = mAdapterChemicals.data
  355. LogUtils.i("=======删除前${mStockModeBean.stockModelList.size}")
  356. mStockModeBean.stockModelList = mStockModelList
  357. if (mAdapterChemicals.data.size == 0) {
  358. viewBinding.imgPosition.visibility = View.VISIBLE
  359. }
  360. LogUtils.i("=======删除后${mStockModeBean.stockModelList.size}")
  361. mAdapterChemicals.notifyItemRemoved(position)
  362. }
  363. }
  364. }
  365. //列表信息点击
  366. mAdapter.setOnItemClickListener { adapter, view, position ->
  367. mAdapter.data.forEach { it.isType = false }
  368. mAdapter.data[position].isType = true
  369. mAdapter.notifyDataSetChanged()
  370. getStockWait(mAdapter.data[position].waitId, mAdapter.data[position])
  371. }
  372. }
  373. private fun getStockWait(waitId: String, hxpStockWaitListBean: HxpStockWaitListBean) {
  374. showLoading("查询中...")
  375. val disposable = ApiRepository.getStockWaitDetails(waitId)
  376. .subscribe({ data ->
  377. dismissLoading()
  378. mUnifiedVerBean = data
  379. //当前需要生成的化学品信息
  380. modelA = hxpStockWaitListBean
  381. putDateInfo()
  382. }, { throwable ->
  383. dismissLoading()
  384. showNetError(throwable)
  385. })
  386. addDisposable(disposable)
  387. }
  388. private fun subAddData(types: Int) {
  389. try {// 从 SharedPreferences 中取出集合
  390. val retrievedList =
  391. SharedPreferencesHelper.getList(this@WarehousingActivity)
  392. if (retrievedList != null && retrievedList.size > 0) {
  393. SharedPreferencesHelper.clearList(this@WarehousingActivity)
  394. }
  395. } catch (e: Exception) {
  396. }
  397. showLoading("入库中...")
  398. val disposable = ApiRepository.stockWaitAdd(mStockModeBean)
  399. .subscribe({ data ->
  400. dismissLoading()
  401. if (types == 0) {
  402. MediaPlayerHelper.playRawMp3(this, R.raw.qingjianghuaxuekaimen)
  403. customDialogView(1, "请将化学品放入化学品柜")
  404. } else {
  405. MediaPlayerHelper.playRawMp3(this, R.raw.guimenyikai)
  406. customDialogView(1, "柜门已开,请将化学品放入化学品柜")
  407. }
  408. mAdapterChemicals.data.clear()
  409. mStockModelList.clear()
  410. viewBinding.lintView.visibility = View.VISIBLE
  411. viewBinding.imgPosition.visibility = View.VISIBLE
  412. mAdapterChemicals.notifyDataSetChanged()
  413. getRelList() //刷新页面
  414. }, { throwable ->
  415. dismissLoading()
  416. showNetError(throwable)
  417. })
  418. addDisposable(disposable)
  419. }
  420. private var modelA: HxpStockWaitListBean? = null
  421. private var mHandler: Handler = Handler(Looper.getMainLooper())
  422. private fun putDateInfo() {
  423. // val netContent = modelA!!.chemicalDensity * modelA!!.normsNum // 净量 = 密度*规格
  424. var density = "${modelA!!.normsNum}${modelA!!.normsUnit}/${modelA!!.applyUnit}"
  425. mLabelDialog =
  426. LabelDialog(
  427. this,
  428. "${modelA!!.applyNum.subtract(BigDecimal(modelA!!.stockNum!!))}",
  429. mLayers,
  430. density,
  431. modelA!!.chemicalName,
  432. object : LabelDialog.IRfidClick {
  433. override fun onItemClick(
  434. modelList: MutableList<StockDetailsModel>?,
  435. layer: Int
  436. ) {
  437. mStockDetailsModel = modelList
  438. mLayersTow = layer
  439. if (modelList == null) {
  440. //取消
  441. } else {
  442. //确定 链接蓝牙
  443. mWeighDialog = WeighDialog(this@WarehousingActivity, density,
  444. object : WeighDialog.IViewLint {
  445. override fun weighingView(weighingValue: EditText) {
  446. // showLoading("蓝牙连接中...")
  447. mWeighingValue = weighingValue
  448. LogUtils.i("========${mWeighingValue!!.isEnabled}")
  449. if (mWeighingValue!!.isEnabled) {
  450. // EditText允许输入
  451. // 在这里执行相应的操作
  452. weiView()
  453. } else {
  454. // EditText禁止输入
  455. // 在这里执行相应的操作
  456. // connectToDeviceWithTimeout()
  457. isClisLit = false
  458. lifecycleScope.launch {
  459. connectToDeviceWithTimeout()
  460. }
  461. }
  462. mWeighingValue!!.setOnEditorActionListener { v, actionId, event ->
  463. if (event?.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_ENTER) {
  464. // 确认是否触发了"actionDone"
  465. true
  466. } else {
  467. closEnd()
  468. false
  469. }
  470. }
  471. }
  472. //关闭
  473. override fun viewCloses() {
  474. isClisLit = true
  475. closEndTwo()
  476. }
  477. override fun onMovement() {
  478. weiView()
  479. }
  480. override fun onTimerReply() {
  481. }
  482. })
  483. mWeighDialog!!.show()
  484. mHandler.postDelayed({
  485. }, 1000 * 60) // 60秒后关闭对话框
  486. }
  487. }
  488. override fun onPrint(
  489. tagCode: String,
  490. wxCode: String,
  491. rfidCode: String,
  492. position: Int
  493. ) {
  494. LogUtils.i("=======打印标记${mUnifiedVerBean.qrCodePrint} $position")
  495. getCheckRfids(rfidCode, tagCode, wxCode, position)
  496. }
  497. override fun opTageCode(data: MutableList<StockDetailsModel>, layer: Int) {
  498. if (mUnifiedVerBean.qrCodePrint) {
  499. showLoading("打印中,请稍等")
  500. mStockDetailsModel = data
  501. mLayer = layer
  502. mPrintTag = mStockDetailsModel!!.size
  503. handlerPrint.startTask(taskPrint, 3000)
  504. } else {
  505. //打印完成
  506. mLabelDialog!!.printingCom()
  507. }
  508. }
  509. })
  510. mLabelDialog!!.show()
  511. }
  512. private fun weiView() {
  513. showToast("请手动输入")
  514. mJoinType = 2
  515. mWeighingValue!!.isEnabled = true
  516. mWeighingValue!!.requestFocus()
  517. }
  518. private suspend fun connectToDeviceWithTimeout() {
  519. showToast("连接中...", Toast.LENGTH_SHORT)
  520. lifecycleScope.launch {
  521. val deviceAddress = withContext(Dispatchers.IO) {
  522. BluetoothConnectionManager.getDeviceAddress()
  523. }
  524. if (deviceAddress != null) {
  525. val socket = withContext(Dispatchers.IO) {
  526. BluetoothConnectionManager.connectToDevice(deviceAddress)
  527. }
  528. if (socket != null) {
  529. // 连接成功后的处理逻辑
  530. // 在这里使用返回的 BluetoothSocket 对象进行后续操作
  531. MediaPlayerHelper.playRawMp3(this@WarehousingActivity, R.raw.qing_zheng_zhong)
  532. // 连接成功后的处理逻辑
  533. withContext(Dispatchers.Main) {
  534. do {
  535. // try {
  536. val inputStream = socket!!.inputStream
  537. val bt = ByteArray(1024)
  538. val content = inputStream!!.read(bt)
  539. if (content != null && content > 0) {
  540. val contents = String(
  541. bt,
  542. 0,
  543. content,
  544. StandardCharsets.UTF_8
  545. )
  546. val split = contents.split("\n")
  547. val weight = split[0].trim()
  548. LogUtils.i("===================称重未处理之前=======$contents")
  549. LogUtils.i("===================weight=======$weight")
  550. LogUtils.i("=====称重数据$weight $taskStarted ${mWeighingValue!!.text.toString()}")
  551. if (weight.toDouble() > 2999) {
  552. turnOffWeighing()
  553. } else {
  554. if (weight.toDouble() > 0) {
  555. if (mWeighingValue!!.text.toString().trim()
  556. .isNotEmpty()
  557. ) {
  558. if (weight.toDouble() == mWeighingValue!!.text.toString()
  559. .trim().toDouble()
  560. ) {
  561. if (taskStarted == false) {
  562. taskStarted = true
  563. LogUtils.i("=====执行了")
  564. socket.close()
  565. delayedTaskToUpdateUI()
  566. break
  567. }
  568. }
  569. }
  570. mWeighingValue!!.setText("$weight")
  571. } else {
  572. mWeighingValue!!.setText("")
  573. }
  574. }
  575. delay(200)
  576. } else {
  577. // jobTow?.cancel()
  578. showToast("请手动输入")
  579. weiView()
  580. break
  581. }
  582. // } catch (e: Exception) {
  583. // jobTow?.cancel()
  584. // weiView()
  585. // break
  586. // }
  587. } while (mWeighDialog != null && mWeighDialog!!.isShowing)
  588. }
  589. } else {
  590. // 连接失败的处理逻辑
  591. showToast("连接失败或超时", Toast.LENGTH_SHORT)
  592. weiView()
  593. }
  594. } else {
  595. // 根据设备名称获取的设备地址为空,处理无法找到设备的情况
  596. showToast("找不到设备", Toast.LENGTH_SHORT)
  597. weiView()
  598. }
  599. }
  600. }
  601. private fun showToast(message: String, duration: Int) {
  602. Toast.makeText(this, message, duration).show()
  603. }
  604. private fun closEndTwo() {
  605. // jobTow?.cancel()
  606. mWeighDialog!!.dismiss()
  607. }
  608. private fun closEnd() {
  609. // jobTow?.cancel()
  610. mWeighDialog!!.dismiss()
  611. val weigh =
  612. mWeighingValue!!.text.toString().trim() //称重后的重量
  613. if (weigh.isEmpty()) {
  614. showToast("未称重")
  615. return
  616. }
  617. if (weigh.toDouble() <= 0) {
  618. showToast("当前重量有误")
  619. return
  620. }
  621. val netContent =
  622. modelA!!.chemicalDensity * modelA!!.normsNum // 净含量=密度*规格
  623. var belongId = ""
  624. var belongName = ""
  625. var belongType = 2
  626. if (modelA!!.topicGroup) {
  627. belongId = modelA!!.topicGroupId
  628. belongName = modelA!!.topicGroupName
  629. belongType = 1
  630. } else {
  631. belongId = modelA!!.applyUserId
  632. belongName = modelA!!.applyUserName
  633. }
  634. if (mListData != null && mListData!!.size == 0) {
  635. showToast("化学品柜 未到找")
  636. return
  637. }
  638. var model = StockModel(
  639. belongId,
  640. belongName,
  641. belongType, //死值
  642. modelA!!.buildId,
  643. modelA!!.buildName,
  644. mCabinetId, //柜子信息
  645. mCabinetName,
  646. modelA!!.casNum,
  647. modelA!!.chemicalCategory.toInt(),
  648. modelA!!.chemicalId, //化学品id
  649. modelA!!.chemicalLevel,
  650. modelA!!.chemicalName,
  651. "${mStockDetailsModel!!.size}",
  652. modelA!!.chemicalPurity,
  653. modelA!!.deptId,
  654. modelA!!.deptName,
  655. viewBinding.cabinet.tag.toString(),
  656. mDoorName,
  657. modelA!!.factory,
  658. modelA!!.floorId,
  659. modelA!!.floorName,
  660. true, //11
  661. mJoinType, //入库方式1 称重,2 录入
  662. "", //视频地址
  663. mLayersTow, //当前选择得
  664. mLayers, //一共几层
  665. modelA!!.applyNum,
  666. modelA!!.applyUnit,
  667. modelA!!.normsNum,
  668. modelA!!.normsUnit,
  669. mStockDetailsModel, //每一条信息
  670. 2,
  671. modelA!!.subId,
  672. modelA!!.subName,
  673. modelA!!.roomNum,
  674. modelA!!.waitId,
  675. mWeighingValue!!.text.toString().toDouble(),
  676. netContent,
  677. expireTime = modelA!!.expireTime
  678. )
  679. verificationData(model)
  680. }
  681. //校验当前数据是否合理
  682. private fun verificationData(model: StockModel) {
  683. showLoading("查询中...")
  684. val disposable = ApiRepository.addStockCheck(model)
  685. .subscribe({ data ->
  686. dismissLoading()
  687. mStockModelList.add(model)
  688. mStockModeBean = StockModeBean(mStockModelList)
  689. //创建需要提交的数据源
  690. mAdapterChemicals.updateData(mLayers)
  691. //创建需要提交的数据源
  692. if (mAdapterChemicals.data != null && mAdapterChemicals.data.size > 0) {
  693. mAdapterChemicals.setList(mStockModelList)
  694. } else {
  695. viewBinding.lintView.visibility = View.GONE
  696. mAdapterChemicals.setNewInstance(mStockModelList.toMutableList())
  697. }
  698. viewBinding.imgPosition.visibility = View.GONE
  699. }, { throwable ->
  700. dismissLoading()
  701. showNetError(throwable)
  702. })
  703. addDisposable(disposable)
  704. }
  705. // private fun getControlConfigs(chemicalNumber: String) {
  706. // showLoading("提交中...")
  707. // val disposable = ApiRepository.controlConfigs(modelA.chemicalLevel)
  708. // .subscribe({ data ->
  709. // dismissLoading()
  710. // mChemicalConfsBean = data
  711. // }, { throwable ->
  712. // showNetError(throwable)
  713. // dismissLoading()
  714. // })
  715. // addDisposable(disposable)
  716. // }
  717. private fun getCheckRfids(
  718. rfid: String,
  719. tagCode: String,
  720. wxCode: String,
  721. position: Int
  722. ) {
  723. showLoading("查询标签...")
  724. val disposable = ApiRepository.checkRfids(rfid)
  725. .subscribe({ data ->
  726. dismissLoading()
  727. LogUtils.i("===========查询成功$data")
  728. if (data) {
  729. customDialogView(2, "rfid标签不能重复,请重新扫描")
  730. mLabelDialog!!.getRfidTag(position)
  731. } else {
  732. if (mUnifiedVerBean.qrCodePrint) {
  733. //需要打印
  734. val print = PrintBean()
  735. print.tag = tagCode //化学品编码
  736. print.wxCode = wxCode
  737. print.name = modelA!!.chemicalName!! //化学品名称
  738. if (modelA!!.casNum != null) {
  739. print.casNo = modelA!!.casNum!! //cas号
  740. } else {
  741. print.casNo = ""
  742. }
  743. if (modelA!!.topicGroup) {
  744. print.person = modelA!!.topicGroupName //归属人
  745. } else {
  746. print.person = modelA!!.applyUserName //归属人
  747. }
  748. //管控 1 非管控 2
  749. if (modelA!!.chemicalLevel == 1) {
  750. print.level = "管控"
  751. } else {
  752. print.level = "非管控"
  753. }
  754. //
  755. print.types = "${modelA!!.chemicalCategoryName}"
  756. PrintTool.INSTANCE.print(!modelA!!.topicGroup, print)
  757. }
  758. }
  759. }, { throwable ->
  760. showNetError(throwable)
  761. mLabelDialog!!.getRfidTag(position)
  762. dismissLoading()
  763. })
  764. addDisposable(disposable)
  765. }
  766. private
  767. val taskPrint = Runnable {
  768. if (mPrintTag == 0) {
  769. //全部打印完成 关闭 并且调用下一步业务
  770. LogUtils.i("=====csd====打印完成$mPrintTag")
  771. dismissLoading()
  772. handlerPrint.stopAllTasks()
  773. //打印完成
  774. mLabelDialog!!.printingCom()
  775. } else {
  776. val print = PrintBean()
  777. print.tag = mStockDetailsModel!![mPrintTag - 1].tagCode!! //化学品编码
  778. print.wxCode = mStockDetailsModel!![mPrintTag - 1].wxCode!! //化学品编码
  779. print.name = modelA!!.chemicalName!! //化学品名称
  780. if (modelA!!.casNum != null) {
  781. print.casNo = modelA!!.casNum!! //cas号
  782. } else {
  783. print.casNo = ""
  784. }
  785. if (modelA!!.topicGroup) {
  786. print.person = modelA!!.topicGroupName //归属人
  787. } else {
  788. print.person = modelA!!.applyUserName //归属人
  789. }
  790. //管控 1 非管控 2
  791. if (modelA!!.chemicalLevel == 1) {
  792. print.level = "管控"
  793. } else {
  794. print.level = "非管控"
  795. }
  796. //
  797. print.types = "${modelA!!.chemicalCategoryName}"
  798. PrintTool.INSTANCE.print(!modelA!!.topicGroup, print)
  799. }
  800. mPrintTag--
  801. }
  802. //查询存储位置 默认第一个
  803. private fun getCabinet(subId: String) {
  804. showLoading("查询中...")
  805. val disposable = ApiRepository.getCabinetList(subId)
  806. .subscribe({ data ->
  807. dismissLoading()
  808. mListData = data
  809. viewBinding.cabinet.text =
  810. "存储位置:${mListData!![0].cabinetName}-${mListData!![0].cabinetDoorVoList[0].doorName}"
  811. viewBinding.cabinet.tag =
  812. "${mListData!![0].cabinetDoorVoList[0].doorUniqueId}"
  813. mCabinetId = mListData!![0].cabinetId
  814. mCabinetName = mListData!![0].cabinetName
  815. mDoorName = mListData!![0].cabinetDoorVoList[0].doorName
  816. mDoorId = mListData!![0].cabinetDoorVoList[0].doorUniqueId
  817. mCabinetLockVoList =
  818. mListData!![0].cabinetDoorVoList[0].cabinetLockVoList
  819. mLayers = mListData!![0].cabinetDoorVoList[0].doorLayers //当前柜子一共几层
  820. }, { throwable ->
  821. dismissLoading()
  822. showNetError(throwable)
  823. })
  824. addDisposable(disposable)
  825. }
  826. var taskStarted = false
  827. // 声明一个全局的 Handler 变量
  828. private var mHandlerTime: Handler? = null
  829. // 在需要执行任务的地方调用这个函数
  830. private fun delayedTaskToUpdateUI() {
  831. // 初始化 Handler,并关联到主线程 Looper
  832. if (isClisLit == false) {
  833. mHandlerTime = Handler(Looper.getMainLooper())
  834. // 定义一个延迟执行的任务
  835. val updateUITask = Runnable {
  836. LogUtils.i("=====执行了")
  837. // 在这里执行更新 UI 的操作
  838. // 例如,更新 TextView 的文本
  839. // textView.text = "Updated text"
  840. closEnd()
  841. taskStarted = false
  842. // 执行完更新操作后,可以关闭 Handler
  843. mHandlerTime?.removeCallbacksAndMessages(null)
  844. mHandlerTime = null
  845. }
  846. // 延迟 3 秒执行任务
  847. mHandlerTime?.postDelayed(updateUITask, 3300)
  848. }
  849. }
  850. //超过量程
  851. private fun turnOffWeighing() {
  852. showToast("超过量程,手动输入")
  853. mJoinType = 2
  854. mWeighingValue!!.isEnabled = true
  855. }
  856. // 获取实验室基本信息
  857. private fun getRelList() {
  858. val disposable = ApiRepository.getRelList(ChemicalApp.subjectId!!)
  859. .subscribe({ data ->
  860. mChemicalInfoBean = data
  861. getCabinet(mChemicalInfoBean!!.subId)
  862. waitList()
  863. }, { throwable ->
  864. dismissLoading()
  865. showNetError(throwable)
  866. })
  867. addDisposable(disposable)
  868. }
  869. //化学品/cas搜索
  870. private fun waitList() {
  871. val map = mutableMapOf<String, Any>()
  872. map["subId"] = mChemicalInfoBean!!.subId
  873. val searchValue = viewBinding.searchValue.text.toString() //输入内容
  874. if (searchValue.isNotEmpty()) {
  875. map["searchValue"] = searchValue
  876. }
  877. try {
  878. val tag = viewBinding.value.tag //类别id
  879. if (tag != null) {
  880. map["chemicalCategory"] = tag
  881. }
  882. } catch (e: Exception) {
  883. }
  884. map["page"] = page
  885. map["pageSize"] = pageSize
  886. showLoading("加载中...")
  887. val disposable =
  888. ApiRepository.waitList(map)
  889. .subscribe({ data ->
  890. dismissLoading()
  891. // current 当前页 pages 共计多少页
  892. mDataPage = data.pages //总页
  893. if (data.records != null && data.records.size == 0) {
  894. mAdapter.data.clear()
  895. mAdapter.notifyDataSetChanged()
  896. initPagesView(data.pages)
  897. }
  898. LogUtils.i("===条==${data.pages} ===条==${data.current}")
  899. if (data.pages != 0) {
  900. if (data.pages > 1 && data.current > 1) {
  901. //当前需要更新页码
  902. upPagesView(data.pages, data.current)
  903. mAdapter.setList(data.records.toMutableList())
  904. } else {
  905. mAdapter.setNewInstance(data.records.toMutableList())
  906. initPagesView(data.pages)
  907. }
  908. }
  909. }, { throwable ->
  910. dismissLoading()
  911. showNetError(throwable)
  912. })
  913. addDisposable(disposable)
  914. }
  915. /**
  916. * 总页码 当前页 初始化
  917. */
  918. private fun initPagesView(pages: Int) {
  919. mPages = pages
  920. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_page)
  921. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  922. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  923. viewBinding.one.setTextColor(ContextCompat.getColor(this, R.color.white))
  924. viewBinding.two.setTextColor(ContextCompat.getColor(this, R.color.black))
  925. when (pages) {
  926. 1 -> {
  927. viewBinding.one.visibility = View.VISIBLE
  928. }
  929. 2 -> {
  930. viewBinding.two.visibility = View.VISIBLE
  931. }
  932. 3 -> {
  933. viewBinding.two.visibility = View.VISIBLE
  934. viewBinding.three.visibility = View.VISIBLE
  935. }
  936. else -> {
  937. viewBinding.two.visibility = View.VISIBLE
  938. viewBinding.three.visibility = View.VISIBLE
  939. viewBinding.drop.visibility = View.VISIBLE
  940. viewBinding.many.visibility = View.VISIBLE
  941. viewBinding.many.text = "$pages"
  942. }
  943. }
  944. if (pages == 1 || pages == 0) {
  945. viewBinding.two.visibility = View.GONE
  946. viewBinding.three.visibility = View.GONE
  947. viewBinding.many.visibility = View.GONE
  948. viewBinding.drop.visibility = View.GONE
  949. }
  950. if (pages < 4) {
  951. when (pages) {
  952. 1 -> {
  953. viewBinding.two.visibility = View.GONE
  954. viewBinding.three.visibility = View.GONE
  955. viewBinding.many.visibility = View.GONE
  956. viewBinding.drop.visibility = View.GONE
  957. }
  958. 2 -> {
  959. viewBinding.three.visibility = View.GONE
  960. viewBinding.many.visibility = View.GONE
  961. viewBinding.drop.visibility = View.GONE
  962. }
  963. 3 -> {
  964. viewBinding.many.visibility = View.GONE
  965. viewBinding.drop.visibility = View.GONE
  966. }
  967. }
  968. }
  969. }
  970. //更新页码
  971. private fun upPagesView(pages: Int, current: Int) {
  972. LogUtils.i("======2")
  973. if (pages < 4) {
  974. // viewBinding.one.visibility = View.VISIBLE
  975. when (pages) {
  976. 1 -> {
  977. viewBinding.two.visibility = View.GONE
  978. viewBinding.three.visibility = View.GONE
  979. viewBinding.many.visibility = View.GONE
  980. viewBinding.drop.visibility = View.GONE
  981. }
  982. 2 -> {
  983. viewBinding.three.visibility = View.GONE
  984. viewBinding.many.visibility = View.GONE
  985. viewBinding.drop.visibility = View.GONE
  986. }
  987. 3 -> {
  988. viewBinding.many.visibility = View.GONE
  989. viewBinding.drop.visibility = View.GONE
  990. }
  991. }
  992. when (current) {
  993. 1 -> {
  994. viewBinding.one.setTextColor(
  995. ContextCompat.getColor(
  996. this,
  997. R.color.white
  998. )
  999. )
  1000. viewBinding.two.setTextColor(
  1001. ContextCompat.getColor(
  1002. this,
  1003. R.color.black
  1004. )
  1005. )
  1006. viewBinding.three.setTextColor(
  1007. ContextCompat.getColor(
  1008. this,
  1009. R.color.black
  1010. )
  1011. )
  1012. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1013. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1014. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1015. }
  1016. 2 -> {
  1017. // viewBinding.two.visibility = View.VISIBLE
  1018. viewBinding.one.setTextColor(
  1019. ContextCompat.getColor(
  1020. this,
  1021. R.color.black
  1022. )
  1023. )
  1024. viewBinding.two.setTextColor(
  1025. ContextCompat.getColor(
  1026. this,
  1027. R.color.white
  1028. )
  1029. )
  1030. viewBinding.three.setTextColor(
  1031. ContextCompat.getColor(
  1032. this,
  1033. R.color.black
  1034. )
  1035. )
  1036. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1037. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1038. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1039. }
  1040. 3 -> {
  1041. // viewBinding.three.visibility = View.VISIBLE
  1042. viewBinding.one.setTextColor(
  1043. ContextCompat.getColor(
  1044. this,
  1045. R.color.black
  1046. )
  1047. )
  1048. viewBinding.two.setTextColor(
  1049. ContextCompat.getColor(
  1050. this,
  1051. R.color.black
  1052. )
  1053. )
  1054. viewBinding.three.setTextColor(
  1055. ContextCompat.getColor(
  1056. this,
  1057. R.color.white
  1058. )
  1059. )
  1060. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1061. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1062. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1063. }
  1064. }
  1065. } else {
  1066. // viewBinding.many.visibility = View.VISIBLE
  1067. viewBinding.three.text = "3"
  1068. when (current) {
  1069. 1 -> {
  1070. viewBinding.one.setTextColor(
  1071. ContextCompat.getColor(
  1072. this,
  1073. R.color.white
  1074. )
  1075. )
  1076. viewBinding.two.setTextColor(
  1077. ContextCompat.getColor(
  1078. this,
  1079. R.color.black
  1080. )
  1081. )
  1082. viewBinding.three.setTextColor(
  1083. ContextCompat.getColor(
  1084. this,
  1085. R.color.black
  1086. )
  1087. )
  1088. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1089. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1090. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1091. }
  1092. 2 -> {
  1093. viewBinding.one.setTextColor(
  1094. ContextCompat.getColor(
  1095. this,
  1096. R.color.black
  1097. )
  1098. )
  1099. viewBinding.two.setTextColor(
  1100. ContextCompat.getColor(
  1101. this,
  1102. R.color.white
  1103. )
  1104. )
  1105. viewBinding.three.setTextColor(
  1106. ContextCompat.getColor(
  1107. this,
  1108. R.color.black
  1109. )
  1110. )
  1111. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1112. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1113. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1114. }
  1115. 3 -> {
  1116. viewBinding.one.setTextColor(
  1117. ContextCompat.getColor(
  1118. this,
  1119. R.color.black
  1120. )
  1121. )
  1122. viewBinding.two.setTextColor(
  1123. ContextCompat.getColor(
  1124. this,
  1125. R.color.black
  1126. )
  1127. )
  1128. viewBinding.three.setTextColor(
  1129. ContextCompat.getColor(
  1130. this,
  1131. R.color.white
  1132. )
  1133. )
  1134. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1135. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1136. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1137. }
  1138. else -> {
  1139. viewBinding.three.text = "$current"
  1140. if (pages == current) {
  1141. viewBinding.three.text = "${current - 1}"
  1142. viewBinding.one.setTextColor(
  1143. ContextCompat.getColor(
  1144. this,
  1145. R.color.black
  1146. )
  1147. )
  1148. viewBinding.two.setTextColor(
  1149. ContextCompat.getColor(
  1150. this,
  1151. R.color.black
  1152. )
  1153. )
  1154. viewBinding.three.setTextColor(
  1155. ContextCompat.getColor(
  1156. this,
  1157. R.color.black
  1158. )
  1159. )
  1160. viewBinding.many.setTextColor(
  1161. ContextCompat.getColor(
  1162. this,
  1163. R.color.white
  1164. )
  1165. )
  1166. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1167. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1168. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1169. viewBinding.many.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1170. } else {
  1171. viewBinding.one.setTextColor(
  1172. ContextCompat.getColor(
  1173. this,
  1174. R.color.black
  1175. )
  1176. )
  1177. viewBinding.two.setTextColor(
  1178. ContextCompat.getColor(
  1179. this,
  1180. R.color.black
  1181. )
  1182. )
  1183. viewBinding.three.setTextColor(
  1184. ContextCompat.getColor(
  1185. this,
  1186. R.color.white
  1187. )
  1188. )
  1189. viewBinding.many.setTextColor(
  1190. ContextCompat.getColor(
  1191. this,
  1192. R.color.black
  1193. )
  1194. )
  1195. viewBinding.one.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1196. viewBinding.two.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1197. viewBinding.three.setBackgroundResource(R.drawable.bg_sou_suo_page)
  1198. viewBinding.many.setBackgroundResource(R.drawable.bg_sou_suo_two_page)
  1199. }
  1200. }
  1201. }
  1202. }
  1203. }
  1204. private fun getOptions() {
  1205. showLoading("查询中...")
  1206. val disposable = ApiRepository.getOptions("chemicals_category")
  1207. .subscribe({ data ->
  1208. dismissLoading()
  1209. val chemDialog = ChemicalTypeDialog(
  1210. this,
  1211. data.toMutableList(),
  1212. object : ChemicalTypeDialog.IonClickListener {
  1213. override fun onClick(data: ChemicalTypeBean) {
  1214. viewBinding.value.text = "${data.label}"
  1215. viewBinding.value.tag = "${data.value}"
  1216. }
  1217. })
  1218. if (!chemDialog.isShowing) {
  1219. chemDialog.show()
  1220. }
  1221. }, { throwable ->
  1222. dismissLoading()
  1223. showNetError(throwable)
  1224. })
  1225. addDisposable(disposable)
  1226. }
  1227. private fun openLock(mLockStr: ArrayList<String>) {
  1228. val map = mutableMapOf<String, Any>()
  1229. map["subId"] = ChemicalApp.subjectId!!
  1230. map["subName"] = ChemicalApp.subjectName!!
  1231. map["cabinetId"] = "$mCabinetId"
  1232. map["cabinetName"] = "$mCabinetName"
  1233. map["doorId"] = "$mDoorId"
  1234. map["doorName"] = "$mDoorName"
  1235. // map["lockNum"] = "$mLockNum"
  1236. // map["lockNum"] = "guisuo001"
  1237. map["lockNumList"] = mLockStr
  1238. map["operationType"] = 2
  1239. if (ChemicalApp.subRoom != null) {
  1240. map["subRoom"] = ChemicalApp.subRoom!!
  1241. }
  1242. map["type"] = true
  1243. try {// 从 SharedPreferences 中取出集合
  1244. val retrievedList = SharedPreferencesHelper.getList(this)
  1245. if (retrievedList != null && retrievedList.size > 0) {
  1246. LogUtils.i("从本地取出得双人信息${retrievedList[0].name}")
  1247. map["oneUserId"] = "${retrievedList[0].userId}"
  1248. map["oneUserName"] = "${retrievedList[0].name}"
  1249. map["twoUserId"] = "${retrievedList[1].userId}"
  1250. map["twoUserName"] = "${retrievedList[1].name}"
  1251. // 清空 SharedPreferences 中的集合
  1252. // SharedPreferencesHelper.clearList(this)
  1253. }
  1254. } catch (e: Exception) {
  1255. }
  1256. showLoading("加载中...")
  1257. val disposable = ApiRepository.lockOperate(map)
  1258. .subscribe({ data ->
  1259. dismissLoading()
  1260. val containsFalse =
  1261. mCabinetLockVoList!!.any { it.isOk == false && it.unlockingMethod == 2 }
  1262. if (containsFalse) {
  1263. showLoading("查询中...")
  1264. startCountdownLock()
  1265. handlerUtil.startTask(task, 1000)
  1266. } else {
  1267. dismissLoading()
  1268. //开锁成功
  1269. subAddData(1)
  1270. handlerUtil.stopAllTasks()
  1271. countdownTimer?.cancel()
  1272. }
  1273. }, { throwable ->
  1274. dismissLoading()
  1275. showNetError(throwable)
  1276. })
  1277. addDisposable(disposable)
  1278. }
  1279. private var countdownTimer: CountDownTimer? = null
  1280. private fun startCountdownLock() {
  1281. val totalTime = 30 * 1000 // 30秒
  1282. val interval = 1000 // 每秒更新一次
  1283. if (countdownTimer != null) {
  1284. countdownTimer!!.cancel()
  1285. }
  1286. countdownTimer = object : CountDownTimer(totalTime.toLong(), interval.toLong()) {
  1287. override fun onTick(millisUntilFinished: Long) {
  1288. val secondsLeft = millisUntilFinished / 1000
  1289. println("剩余时间: $secondsLeft 秒")
  1290. }
  1291. override fun onFinish() {
  1292. println("倒计时结束!")
  1293. // mCabinetLockVoList = null
  1294. // mLockStr.clear()
  1295. viewBinding.butOpen.visibility = View.VISIBLE
  1296. showToast("开锁超时,可重新操作")
  1297. dismissLoading()
  1298. handlerUtil.stopAllTasks()
  1299. // 在这里执行合适的操作,比如退出应用程序
  1300. }
  1301. }
  1302. println("倒计时开始...")
  1303. countdownTimer?.start()
  1304. }
  1305. override fun onResume() {
  1306. super.onResume()
  1307. try {
  1308. if (Constants.AUTHENTICATION) {
  1309. try {// 从 SharedPreferences 中取出集合
  1310. val retrievedList =
  1311. SharedPreferencesHelper.getList(this@WarehousingActivity)
  1312. if (retrievedList != null && retrievedList.size > 0) {
  1313. LogUtils.i("===a===${retrievedList[0].userId}")
  1314. if (mStockModelList != null && mStockModelList.size > 0) {
  1315. mStockModelList.forEach {
  1316. it.oneUserId = "${retrievedList[0].userId}"
  1317. it.oneUserName = "${retrievedList[0].name}"
  1318. it.twoUserId = "${retrievedList[1].userId}"
  1319. it.twoUserName = "${retrievedList[1].name}"
  1320. }
  1321. }
  1322. // // 清空 SharedPreferences 中的集合
  1323. // SharedPreferencesHelper.clearList(this@WarehousingActivity)
  1324. }
  1325. } catch (e: Exception) {
  1326. }
  1327. if (mCabinetLockVoList != null && mCabinetLockVoList!!.size > 0) {
  1328. mCabinetLockVoList!!.forEach { it ->
  1329. mLockStr.clear()
  1330. when (it.unlockingMethod) {
  1331. 2 -> { //智能锁
  1332. mLockStr.add("${it.lockNum}")
  1333. }
  1334. }
  1335. }
  1336. } else {
  1337. mLockStr.clear()
  1338. }
  1339. if (mLockStr != null && mLockStr.size > 0) {
  1340. openLock(mLockStr)
  1341. } else {
  1342. subAddData(0)
  1343. }
  1344. }
  1345. Constants.AUTHENTICATION = false
  1346. } catch (e: Exception) {
  1347. Constants.AUTHENTICATION = false
  1348. }
  1349. }
  1350. override fun onDestroy() {
  1351. super.onDestroy()
  1352. try {
  1353. handlerUtil.stopAllTasks()
  1354. mHandler.removeCallbacksAndMessages(null)
  1355. countdownTimer?.cancel()
  1356. } catch (e: Exception) {
  1357. }
  1358. try {
  1359. timeUpdater.stopUpdating()
  1360. } catch (e: Exception) {
  1361. }
  1362. }
  1363. override fun cdFinish() {
  1364. finish()
  1365. }
  1366. //=================开锁流程================
  1367. private val task = Runnable {
  1368. //判断所有的锁开锁成功
  1369. val containsFalse =
  1370. mCabinetLockVoList!!.any { it.isOk == false && it.unlockingMethod == 2 }
  1371. LogUtils.i("=======查询调用开锁状态====$containsFalse")
  1372. if (containsFalse) {
  1373. getLocks()
  1374. } else {
  1375. dismissLoading()
  1376. //开锁成功
  1377. subAddData(1)
  1378. handlerUtil.stopAllTasks()
  1379. countdownTimer?.cancel()
  1380. }
  1381. }
  1382. //查询开锁信息
  1383. private fun getLocks() {
  1384. mCabinetLockVoList!!.forEachIndexed { index, fruit ->
  1385. if (!fruit.isOk && fruit.unlockingMethod == 2) {
  1386. getLocksDate(index, fruit.lockNum)
  1387. }
  1388. }
  1389. }
  1390. //查询开锁状态
  1391. private fun getLocksDate(index: Int, fruit: String) {
  1392. val disposable = ApiRepository.getLocks(ChemicalApp.subjectId!!, fruit)
  1393. .subscribe({ data ->
  1394. if (data) {
  1395. mCabinetLockVoList!![index].isOk = true
  1396. }
  1397. }, { throwable ->
  1398. dismissLoading()
  1399. showNetError(throwable)
  1400. })
  1401. addDisposable(disposable)
  1402. }
  1403. /**
  1404. * 0 没有图标 1 绿色(成功) 2红色(失败)
  1405. * 失败或者成功的弹框
  1406. */
  1407. private fun customDialogView(types: Int, msg: String) {
  1408. if (!this.isFinishing && !this.isDestroyed) {
  1409. val customDialog = CustomDialog(this, types, msg)
  1410. customDialog.show()
  1411. }
  1412. }
  1413. /**
  1414. * 异常处理
  1415. */
  1416. private fun throwableView(throwable: Throwable) {
  1417. when (throwable) {
  1418. is NetException -> {
  1419. if (throwable.message.isNullOrEmpty()) {
  1420. "接口请求失败(${throwable.code})"
  1421. } else {
  1422. throwable.message!!
  1423. }
  1424. }
  1425. is SocketTimeoutException -> "请求超时,请稍后重试"
  1426. is ConnectException -> "无法连接服务器,请检查网络"
  1427. is HttpException -> "服务器繁忙,请稍后重试"
  1428. else -> null
  1429. }?.let { customDialogView(2, "$it") }
  1430. }
  1431. }