| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace addons\QiShangTong\backend\controllers;
- use addons\QiShangTong\common\models\QiShangTongGoods;
- use addons\QiShangTong\common\service\CategoryService;
- use addons\QiShangTong\common\service\GoodsService;
- use addons\QiShangTong\common\service\MallService;
- use addons\QiShangTong\common\service\SettingService;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\mall\Mall;
- use Yii;
- use yii\helpers\Json;
- class GoodsController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['limit', 'page', 'cate_id', 'goods_id', 'goods_type', 'discount'], 'integer'],
- [['sort', 'sort_type', 'goods_name'], 'string'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 取数据
- $service = new GoodsService();
- // $list = $service->thirdPage($this->mall_id, $data);
- // 判断需要本地的数据还是第三方的数据
- $config = (new SettingService())->get($this->mall_id);
- if (!empty($config['is_default'])) {
- $list = $service->thirdPage($this->mall_id, $data);
- } else {
- if (empty($config['is_enable'])) {
- return $this->error('请设置基础设置');
- }
- foreach (Mall::getEnableMallIds() as $mall_id) {
- $config = (new SettingService())->get($mall_id);
- if (!empty($config['is_default'])) break;
- $config = null;
- }
- if (empty($config)) return $this->error('还未指定标准选品库');
- $list = $service->localPage($mall_id, $data);
- $count = $list['count'];
- $list = $list['list'];
- }
- $ret = [
- 'list' => [],
- 'count' => $count ?? 0
- ];
- if (!empty($list) && is_array($list)) {
- $ids = array_column($list, "goods_id");
- $local_list = QiShangTongGoods::lists([
- 'where' => [
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ['third_goods_id' => $ids],
- ['mall_id' => $this->mall_id],
- ],
- 'select'=> 'third_goods_id',
- 'index' => 'third_goods_id'
- ]);
- foreach ($list as &$item) {
- $item['is_storage'] = !empty($local_list[$item['goods_id']]);
- }
- $ret['list'] = $list;
- if (!isset($count)) $ret['count'] = $service->thirdCount($this->mall_id, $data)['count'] ?? 0;
- }
- return is_string($list) ? $this->error($list) : $this->success('获取成功', $ret);
- }
- $cats = CategoryService::custom($this->mall_id);
- return $this->render('index',['cats' => Json::encode(!empty($cats) ? $cats : []), 'rate' => MallService::rate($this->mall_id)]);
- }
- /**
- * @return mixed|null
- */
- public function actionImport()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['goods'], 'required'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 这里执行?
- $ret = (new GoodsService())->batchImport($this->mall_id, Json::decode($data['goods']));
- return is_string($ret) ? $this->error($ret) : $this->success('导入成功');
- }
- /**
- * 商品详情
- * @return mixed|null
- * @throws \Exception
- */
- public function actionDetail()
- {
- $data = Yii::$app->request->get();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id'], 'required'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $ret = (new GoodsService())->detail($this->mall_id, $data['id']);
- if (is_array($ret)) {
- $ret['rate'] = MallService::rate($this->mall_id);
- }
- return $this->success('获取成功', $ret);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 商品详情
- * @return mixed|null
- * @throws \Exception
- */
- public function actionDel()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id'], 'required'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $ret = (new GoodsService())->delete($this->mall_id, $data['id']);
- return is_string($ret) ? $this->error($ret) : $this->success('删除成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function actionWrite()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id'], 'required'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $ret = (new GoodsService())->write([
- 'mall_id' => $this->mall_id,
- 'id' => $data['id']
- ]);
- return is_string($ret) ? $this->error($ret) : $this->success('导入成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- public function actionBatchWrite()
- {
- $data = Yii::$app->request->post();
- $valid = Yii::$app->services->validate->validate($data, [
- [['id'], 'required'],
- [['id'], 'safe'],
- ]);
- if ($valid === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $cnt = 0;
- if (!empty($data['id'])) {
- $ids = QiShangTongGoods::find()->where(['mall_id' => $this->mall_id])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['goods_id' => 0])
- ->andWhere(['id' => $data['id']])->select('id')->column();
- if (!empty($ids)) {
- $cnt = count($ids);
- foreach ($ids as $id) {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'mall_id' => $this->mall_id,
- 'id' => $id
- ],
- GoodsService::class, 'write');
- }
- }
- }
- return $this->success("后台正在导入 $cnt 件商品");
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|