LabelController.php 1.9 KB

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