WarehousingActivity.kt 60 KB

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