GoodsController.php 7.2 KB

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