ChemicalsAlsoActivity.kt 62 KB

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