batch_create_page.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import 'package:chicken_farm/core/config/app_config.dart';
  2. import 'package:chicken_farm/core/utils/datetime_util.dart';
  3. import 'package:chicken_farm/core/utils/logger.dart';
  4. import 'package:chicken_farm/apis/index.dart';
  5. import 'package:chicken_farm/components/vb_rfid_field.dart';
  6. import 'package:chicken_farm/components/vb_search_select.dart';
  7. import 'package:chicken_farm/components/vb_select.dart';
  8. import 'package:chicken_farm/modes/breeding/batch.dart';
  9. import 'package:chicken_farm/modes/breeding/family.dart';
  10. import 'package:chicken_farm/modes/rfid/rfid_model.dart';
  11. import 'package:flutter/material.dart';
  12. import 'package:chicken_farm/components/vb_app_bar.dart';
  13. import 'package:chicken_farm/core/utils/toast.dart';
  14. class BatchCreatePage extends StatefulWidget {
  15. const BatchCreatePage({super.key});
  16. @override
  17. State<BatchCreatePage> createState() => _BatchCreatePageState();
  18. }
  19. class _BatchCreatePageState extends State<BatchCreatePage> {
  20. final List<String> _rfids = [];
  21. String? _batchNum;
  22. String? _familyId;
  23. @override
  24. Widget build(BuildContext context) {
  25. return Scaffold(
  26. appBar: const VberAppBar(title: '个体绑定', showLeftButton: true),
  27. body: Padding(
  28. padding: const EdgeInsets.all(16.0),
  29. child: Column(
  30. crossAxisAlignment: CrossAxisAlignment.start,
  31. children: [
  32. // 批次选择
  33. _buildBatchInput(),
  34. const SizedBox(height: 10),
  35. // 家系号选择
  36. _buildFamilyInput(),
  37. const SizedBox(height: 20),
  38. // 电子编号区域
  39. _buildRfidSection(),
  40. const SizedBox(height: 20),
  41. // 提交按钮
  42. SizedBox(
  43. width: double.infinity,
  44. child: ElevatedButton(
  45. onPressed:
  46. _rfids.isNotEmpty && _batchNum != null && _familyId != null
  47. ? _handleSubmit
  48. : null,
  49. style: ElevatedButton.styleFrom(
  50. backgroundColor:
  51. _rfids.isNotEmpty &&
  52. _batchNum != null &&
  53. _familyId != null
  54. ? Colors.blue
  55. : Colors.grey,
  56. foregroundColor: Colors.white,
  57. ),
  58. child: const Text('提交'),
  59. ),
  60. ),
  61. const SizedBox(height: 20),
  62. // 已识别的电子编号列表
  63. if (_rfids.isNotEmpty) ...[
  64. Row(
  65. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  66. children: [
  67. const Text(
  68. '已识别的电子编号',
  69. style: TextStyle(fontWeight: FontWeight.bold),
  70. ),
  71. IconButton(
  72. icon: const Icon(Icons.clear, size: 18),
  73. onPressed: _clearRfids,
  74. tooltip: '清空编号',
  75. ),
  76. ],
  77. ),
  78. const SizedBox(height: 10),
  79. Expanded(
  80. child: Container(
  81. padding: const EdgeInsets.all(10),
  82. decoration: BoxDecoration(
  83. border: Border.all(color: Colors.grey),
  84. borderRadius: BorderRadius.circular(8),
  85. ),
  86. child: ListView.builder(
  87. padding: EdgeInsets.zero,
  88. itemCount: _rfids.length,
  89. itemBuilder: (context, index) {
  90. return ListTile(
  91. visualDensity: VisualDensity.compact,
  92. contentPadding: const EdgeInsets.symmetric(
  93. horizontal: 2,
  94. vertical: 0,
  95. ),
  96. title: Text(_rfids[index]),
  97. trailing: IconButton(
  98. icon: const Icon(
  99. Icons.delete,
  100. size: 18,
  101. color: Colors.red,
  102. ),
  103. onPressed: () => _removeRfid(index),
  104. ),
  105. );
  106. },
  107. ),
  108. ),
  109. ),
  110. ],
  111. const SizedBox(height: 20),
  112. ],
  113. ),
  114. ),
  115. );
  116. }
  117. Widget _buildBatchInput() {
  118. if (AppConfig.isOffline) {
  119. // 离线模式下使用文本输入框
  120. return Column(
  121. crossAxisAlignment: CrossAxisAlignment.start,
  122. children: [
  123. const Text('批次号', style: TextStyle(fontWeight: FontWeight.bold)),
  124. const SizedBox(height: 10),
  125. Container(
  126. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  127. decoration: BoxDecoration(
  128. border: Border.all(color: Colors.grey),
  129. borderRadius: BorderRadius.circular(8),
  130. ),
  131. child: TextField(
  132. decoration: const InputDecoration(
  133. hintText: '请输入批次号',
  134. border: InputBorder.none,
  135. ),
  136. onChanged: (value) {
  137. setState(() {
  138. _batchNum = value.isNotEmpty ? value : null;
  139. });
  140. },
  141. ),
  142. ),
  143. ],
  144. );
  145. } else {
  146. // 在线模式下使用原来的搜索选择器
  147. return _buildBatchSearchSelect();
  148. }
  149. }
  150. Widget _buildFamilyInput() {
  151. if (AppConfig.isOffline) {
  152. // 离线模式下使用文本输入框
  153. return Column(
  154. crossAxisAlignment: CrossAxisAlignment.start,
  155. children: [
  156. const Text('家系号', style: TextStyle(fontWeight: FontWeight.bold)),
  157. const SizedBox(height: 10),
  158. Container(
  159. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  160. decoration: BoxDecoration(
  161. border: Border.all(color: Colors.grey),
  162. borderRadius: BorderRadius.circular(8),
  163. ),
  164. child: TextField(
  165. decoration: const InputDecoration(
  166. hintText: '请输入家系号',
  167. border: InputBorder.none,
  168. ),
  169. onChanged: (value) {
  170. setState(() {
  171. _familyId = value.isNotEmpty ? value : null;
  172. });
  173. },
  174. ),
  175. ),
  176. ],
  177. );
  178. } else {
  179. // 在线模式下使用原来的搜索选择器
  180. return _buildFamilySearchSelect();
  181. }
  182. }
  183. Widget _buildBatchSearchSelect() {
  184. return Column(
  185. crossAxisAlignment: CrossAxisAlignment.start,
  186. children: [
  187. const Text('选择批次', style: TextStyle(fontWeight: FontWeight.bold)),
  188. const SizedBox(height: 10),
  189. Container(
  190. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  191. decoration: BoxDecoration(
  192. border: Border.all(color: Colors.grey),
  193. borderRadius: BorderRadius.circular(8),
  194. ),
  195. child: VberSearchSelect<BatchModel>(
  196. searchApi: ({required dynamic queryParams}) async {
  197. final result = await apis.breeding.queryApi.queryPageBatchs(
  198. queryParams,
  199. );
  200. return {'rows': result.rows, 'total': result.total};
  201. },
  202. converter: (BatchModel data) {
  203. return SelectOption(
  204. label: '${data.batchNum}(${data.batchName})',
  205. value: data.batchNum,
  206. extra: data,
  207. );
  208. },
  209. value: _batchNum,
  210. hint: '批次号',
  211. onChanged: (String? value) {
  212. setState(() {
  213. _batchNum = value;
  214. // 当切换批次时,重置后续选项和数据
  215. _familyId = null;
  216. });
  217. // // 获取翅号数据
  218. // if (value != null) {
  219. // // 使用Future.microtask确保在下一个微任务中加载数据,避免在构建过程中修改状态
  220. // Future.microtask(() => _loadWingTags(value));
  221. // }
  222. },
  223. hideUnderline: true,
  224. ),
  225. ),
  226. ],
  227. );
  228. }
  229. Widget _buildFamilySearchSelect() {
  230. return Column(
  231. crossAxisAlignment: CrossAxisAlignment.start,
  232. children: [
  233. const Text('选择家系号', style: TextStyle(fontWeight: FontWeight.bold)),
  234. const SizedBox(height: 10),
  235. Container(
  236. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  237. decoration: BoxDecoration(
  238. border: Border.all(color: Colors.grey),
  239. borderRadius: BorderRadius.circular(8),
  240. ),
  241. child: VberSearchSelect<FamilyModel>(
  242. searchApi: ({required dynamic queryParams}) async {
  243. final result = await apis.breeding.queryApi.queryPageFamilys(
  244. queryParams,
  245. );
  246. return {'rows': result.rows, 'total': result.total};
  247. },
  248. converter: (FamilyModel data) {
  249. return SelectOption(
  250. label: data.familyNum,
  251. value: data.id.toString(),
  252. extra: data,
  253. );
  254. },
  255. value: _familyId,
  256. hint: '家系号',
  257. onChanged: (String? value) {
  258. setState(() {
  259. _familyId = value;
  260. });
  261. },
  262. hideUnderline: true,
  263. ),
  264. ),
  265. ],
  266. );
  267. }
  268. Widget _buildRfidSection() {
  269. return VberRfidField(
  270. rfids: _rfids,
  271. onRfidsScanned: (List<RfidModel> scannedRfids) {
  272. // 过滤出未存在的RFID
  273. final newRfids = scannedRfids
  274. .where((rfid) => !_rfids.contains(rfid.uid))
  275. .toList();
  276. if (newRfids.isNotEmpty) {
  277. setState(() {
  278. // 将新的RFID添加到列表中
  279. for (var rfid in newRfids) {
  280. _rfids.insert(0, rfid.uid);
  281. }
  282. });
  283. ToastUtil.success("新增 ${newRfids.length} 枚电子编号");
  284. // if (newRfids.length == scannedRfids.length) {
  285. // // 全都是新添加的
  286. // ToastUtil.success("成功添加 ${newRfids.length} 枚新的电子编号");
  287. // } else {
  288. // // 部分是重复的
  289. // ToastUtil.info("新增 ${newRfids.length} 枚电子编号");
  290. // // ,${scannedRfids.length - newRfids.length} 枚已存在
  291. // }
  292. } else {
  293. // 所有RFID都已存在
  294. ToastUtil.info("电子编号已存在");
  295. }
  296. },
  297. multiple: true,
  298. label: '电子编号',
  299. multiplePlaceholder: '未识别',
  300. multipleFormat: '已识别 %d 枚电子编号',
  301. );
  302. }
  303. // 移除指定索引的电子编号
  304. void _removeRfid(int index) {
  305. setState(() {
  306. _rfids.removeAt(index);
  307. });
  308. }
  309. // 清空所有已识别的电子编号
  310. void _clearRfids() {
  311. setState(() {
  312. _rfids.clear();
  313. });
  314. ToastUtil.info('已清空所有电子编号');
  315. }
  316. // 提交数据
  317. void _handleSubmit() {
  318. final data = _rfids.map((rfid) {
  319. return {
  320. 'rfid': rfid,
  321. 'batchNum': _batchNum,
  322. 'familyId': _familyId,
  323. 'date': DateTimeUtil.format(DateTime.now()),
  324. };
  325. }).toList();
  326. apis.breeding.submitApi
  327. .bindChicken(data)
  328. .then((_) {
  329. if (mounted) {
  330. ScaffoldMessenger.of(context).showSnackBar(
  331. const SnackBar(
  332. content: Text('批量绑定个体成功'),
  333. backgroundColor: Colors.green,
  334. ),
  335. );
  336. // 提交后重置表单
  337. setState(() {
  338. _rfids.clear();
  339. });
  340. }
  341. })
  342. .catchError((err) {
  343. ToastUtil.error('批量绑定个体失败');
  344. if (mounted && err != null) {
  345. String errorMessage = err.toString();
  346. if (err is Exception) {
  347. errorMessage = err.toString();
  348. }
  349. logger.e(errorMessage);
  350. ScaffoldMessenger.of(context).showSnackBar(
  351. SnackBar(
  352. content: Text(errorMessage),
  353. backgroundColor: Colors.red,
  354. ),
  355. );
  356. }
  357. });
  358. }
  359. }