LabelController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\item\classes\LabelClass;
  4. use common\components\util;
  5. use Yii;
  6. class LabelController extends BaseController
  7. {
  8. //批量添加电子标 ssh 20230821
  9. public function actionBatchAddLabel()
  10. {
  11. $post = Yii::$app->request->post();
  12. $data = $post['data'] ?? '';
  13. $arr = json_decode($data, true);
  14. if (empty($arr)) {
  15. util::fail('没有电子标');
  16. }
  17. $ptItemId = $post['ptItemId'] ?? 0;
  18. if (empty($ptItemId)) {
  19. util::fail('没有花材信息');
  20. }
  21. $list = LabelClass::getAllByCondition(['mainId' => $this->mainId, 'use' => 0], null, '*');
  22. if (!empty($list)) {
  23. $has = array_column($list, 'epc');
  24. $common = array_intersect($arr, $has);
  25. if (!empty($common)) {
  26. $currentLabel = $common[0] ?? '';
  27. util::fail('标签已经添加过了:' . $currentLabel);
  28. }
  29. }
  30. $connection = Yii::$app->db;//事务处理
  31. $transaction = $connection->beginTransaction();
  32. try {
  33. foreach ($arr as $epc) {
  34. LabelClass::add(['mainId' => $this->mainId, 'epc' => $epc, 'itemId' => $ptItemId]);
  35. }
  36. $transaction->commit();
  37. util::complete('操作成功');
  38. } catch (\Exception $e) {
  39. $transaction->rollBack();
  40. Yii::info("保存失败原因:" . $e->getMessage());
  41. util::fail('保存失败');
  42. }
  43. }
  44. //获取我没有使用的电子标 ssh 20230817
  45. public function actionGetMyLabelList()
  46. {
  47. $mainId = $this->mainId;
  48. $list = LabelClass::getMyUnUseLabelList($mainId);
  49. util::success(['list' => $list]);
  50. }
  51. }