ItemController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\item\classes\PtItemClass;
  4. use bizGhs\item\classes\ItemClass;
  5. use bizGhs\item\services\ItemService;
  6. use common\components\util;
  7. use bizGhs\product\classes\ProductClass;
  8. use Yii;
  9. class ItemController extends BaseController
  10. {
  11. public function actionList()
  12. {
  13. $classId = Yii::$app->request->get('classId', 0);
  14. $where['sjId'] = $this->sjId;
  15. if (!empty($classId)) {
  16. $where['classId'] = $classId;
  17. }
  18. $list = ItemClass::getGhsItemList($where);
  19. util::success($list);
  20. }
  21. public function actionAdd()
  22. {
  23. $post = Yii::$app->request->post();
  24. $post['adminId'] = $this->adminId;
  25. $post['sjId'] = $this->sjId;
  26. $connection = Yii::$app->db;//事务处理
  27. $transaction = $connection->beginTransaction();
  28. try {
  29. ItemService::addGhsItem($post);
  30. $transaction->commit();
  31. util::complete('添加成功');
  32. } catch (Exception $e) {
  33. $transaction->rollBack();
  34. util::fail('添加失败');
  35. }
  36. }
  37. //批量添加花材
  38. public function actionBatchAdd()
  39. {
  40. $shopAdmin = $this->shopAdmin;
  41. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  42. util::fail('超管才能操作');
  43. }
  44. $post = Yii::$app->request->post();
  45. //[{itemId:1000,classId:2,price:1,stockWarning:1}]
  46. $data = $post['data'];
  47. if (empty($data)) {
  48. util::fail("请选花材");
  49. }
  50. $data = json_decode($data, true);
  51. if (!is_array($data) || empty($data)) {
  52. util::fail("请选花材");
  53. }
  54. $sjId = $this->sjId;
  55. $ids = array_column($data,'itemId');
  56. $ids = array_unique(array_filter($ids));
  57. $has = ProductClass::getByCondition(['sjId' => $sjId, 'itemId'=>['in',$ids]]);
  58. if(!empty($has)){
  59. util::fail("有花材已经添加过了");
  60. }
  61. $connection = Yii::$app->db;//事务处理
  62. $transaction = $connection->beginTransaction();
  63. try {
  64. foreach ($data as $v) {
  65. $v['adminId'] = $this->adminId;
  66. $v['sjId'] = $sjId;
  67. ItemService::addGhsItem($v);
  68. }
  69. $transaction->commit();
  70. util::complete('添加成功');
  71. } catch (Exception $e) {
  72. $transaction->rollBack();
  73. util::fail();
  74. }
  75. }
  76. public function actionUpdate()
  77. {
  78. $data = Yii::$app->request->post();
  79. $data['adminId'] = $this->adminId;
  80. $data['sjId'] = $this->sjId;
  81. ItemService::updateGshItem($data);
  82. util::complete('修改成功');
  83. }
  84. public function actionDelete()
  85. {
  86. util::fail('不能删除哦!');
  87. }
  88. //获取平台里的花材信息
  89. public function actionPlatformItem()
  90. {
  91. $py = Yii::$app->request->get('py', '');
  92. $where = [];
  93. if ($py) {
  94. $where = ['py' => $py];
  95. }
  96. $list = PtItemClass::getItemList($where);
  97. util::success($list);
  98. }
  99. }