GoodsController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace addons\QiDingDong\backend\controllers;
  3. use addons\QiDingDong\common\models\QiDingDongGoods;
  4. use addons\QiDingDong\common\service\CategoryService;
  5. use addons\QiDingDong\common\service\GoodsService;
  6. use addons\QiDingDong\common\service\MallService;
  7. use addons\QiDingDong\common\service\SettingService;
  8. use common\enums\RabbitMqEnum;
  9. use common\enums\StatusEnum;
  10. use common\models\mall\Mall;
  11. use Yii;
  12. use yii\helpers\Json;
  13. class GoodsController extends BaseController
  14. {
  15. public $enableCsrfValidation = false;
  16. /**
  17. * 首页
  18. *
  19. * @return string
  20. */
  21. public function actionIndex()
  22. {
  23. if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
  24. $data = Yii::$app->request->get();
  25. $valid = Yii::$app->services->validate->validate($data, [
  26. [['limit', 'page', 'cate_id', 'goods_id', 'goods_type', 'discount', 'goods_id'], 'integer'],
  27. [['sort', 'sort_type', 'goods_name'], 'string'],
  28. ]);
  29. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  30. // 取数据
  31. // 判断需要本地的数据还是第三方的数据
  32. $config = (new SettingService())->get($this->mall_id);
  33. if (!empty($config['is_default'])) {
  34. $ret = (new GoodsService())->thirdPage($this->mall_id, $data);
  35. } else {
  36. if (empty($config['is_enable'])) {
  37. return $this->error('请设置基础设置');
  38. }
  39. foreach (Mall::getEnableMallIds() as $mall_id) {
  40. $config = (new SettingService())->get($mall_id);
  41. if (!empty($config['is_default'])) break;
  42. $config = null;
  43. }
  44. if (empty($config)) return $this->error('还未指定标准选品库');
  45. $ret = (new GoodsService())->localPage($mall_id, $data);
  46. }
  47. if (!empty($ret['list'])) {
  48. $ids = array_column($ret['list'], "goods_id");
  49. $local_list = QiDingDongGoods::lists([
  50. 'where' => [
  51. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
  52. ['third_goods_id' => $ids],
  53. ['mall_id' => $this->mall_id],
  54. ],
  55. 'select'=> 'third_goods_id',
  56. 'index' => 'third_goods_id'
  57. ]);
  58. foreach ($ret['list'] as &$item) {
  59. $item['is_storage'] = !empty($local_list[$item['goods_id']]);
  60. }
  61. }
  62. return is_string($ret) ? $this->error($ret) : $this->success('获取成功', $ret);
  63. }
  64. $cats = CategoryService::custom($this->mall_id);
  65. return $this->render('index',['cats' => Json::encode(!empty($cats) ? $cats : []), 'rate' => MallService::rate($this->mall_id)]);
  66. }
  67. /**
  68. * @return mixed|null
  69. */
  70. public function actionImport()
  71. {
  72. $data = Yii::$app->request->post();
  73. $valid = Yii::$app->services->validate->validate($data, [
  74. [['goods'], 'required'],
  75. ]);
  76. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  77. // 这里执行?
  78. $ret = (new GoodsService())->batchImport($this->mall_id, Json::decode($data['goods']));
  79. return is_string($ret) ? $this->error($ret) : $this->success('导入成功');
  80. }
  81. /**
  82. * 商品详情
  83. * @return mixed|null
  84. * @throws \Exception
  85. */
  86. public function actionDetail()
  87. {
  88. $data = Yii::$app->request->get();
  89. $valid = Yii::$app->services->validate->validate($data, [
  90. [['id'], 'required'],
  91. ]);
  92. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  93. try {
  94. $ret = (new GoodsService())->detail($this->mall_id, $data['id']);
  95. if (is_array($ret)) {
  96. $ret['rate'] = MallService::rate($this->mall_id);
  97. }
  98. return $this->success('获取成功', $ret);
  99. } catch (\Exception $e) {
  100. return $this->error($e->getMessage());
  101. }
  102. }
  103. /**
  104. * 商品详情
  105. * @return mixed|null
  106. * @throws \Exception
  107. */
  108. public function actionDel()
  109. {
  110. $data = Yii::$app->request->post();
  111. $valid = Yii::$app->services->validate->validate($data, [
  112. [['id'], 'required'],
  113. ]);
  114. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  115. try {
  116. $ret = (new GoodsService())->delete($this->mall_id, $data['id']);
  117. return is_string($ret) ? $this->error($ret) : $this->success('删除成功');
  118. } catch (\Exception $e) {
  119. return $this->error($e->getMessage());
  120. }
  121. }
  122. public function actionWrite()
  123. {
  124. $data = Yii::$app->request->post();
  125. $valid = Yii::$app->services->validate->validate($data, [
  126. [['id'], 'required'],
  127. ]);
  128. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  129. try {
  130. $ret = (new GoodsService())->write([
  131. 'mall_id' => $this->mall_id,
  132. 'id' => $data['id']
  133. ]);
  134. return is_string($ret) ? $this->error($ret) : $this->success('导入成功');
  135. } catch (\Exception $e) {
  136. return $this->error($e->getMessage());
  137. }
  138. }
  139. public function actionBatchWrite()
  140. {
  141. $data = Yii::$app->request->post();
  142. $valid = Yii::$app->services->validate->validate($data, [
  143. [['id'], 'required'],
  144. [['id'], 'safe'],
  145. ]);
  146. if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  147. try {
  148. $cnt = 0;
  149. if (!empty($data['id'])) {
  150. $ids = QiDingDongGoods::find()->where(['mall_id' => $this->mall_id])
  151. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  152. ->andWhere(['goods_id' => 0])
  153. ->andWhere(['id' => $data['id']])->select('id')->column();
  154. if (!empty($ids)) {
  155. $cnt = count($ids);
  156. foreach ($ids as $id) {
  157. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  158. 'mall_id' => $this->mall_id,
  159. 'id' => $id
  160. ],
  161. GoodsService::class, 'write');
  162. }
  163. }
  164. }
  165. return $this->success("后台正在导入 $cnt 件商品");
  166. } catch (\Exception $e) {
  167. return $this->error($e->getMessage());
  168. }
  169. }
  170. }