| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\models\StoreSyncLock;
- use Yii;
- use app\models\StoreSync;
- use app\models\StoreSyncLog;
- use app\models\Store;
- use app\jobs\StoreSyncJob;
- class StoreSyncController extends BaseController
- {
- public function actionGetList()
- {
- $status = get_params('status', -1);
- $query = StoreSync::find()->where([
- 'is_delete' => 0,
- ]);
- if ($status > -1 && \in_array($status, [0, 1, 2])) {
- $query->andWhere(['status' => $status]);
- }
- $pagination = pagination_make($query, true, 'id DESC');
- $list = $pagination['list'];
- foreach ($list as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $fromStore = Store::findOne($item['from_store_id']);
- $item['from_store_name'] = '-';
- if ($fromStore) {
- $item['from_store_name'] = $fromStore->name;
- }
- $toStoreIds = \json_decode($item['to_store_ids'], true);
- $item['imports'] = \json_decode($item['imports'], true);
- $item['to_store_ids'] = $toStoreIds;
- $item['status'] = (int)$item['status'];
- $storeNames = [];
- foreach ($toStoreIds as $storeId) {
- $store = Store::findOne($storeId);
- if ($store) {
- $storeNames[] = [
- 'id' => $storeId,
- 'name' => $store->name
- ];
- }
- }
- $item['to_store_names'] = $storeNames;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ]);
- }
- public function actionGetLogList()
- {
- $query = StoreSyncLog::find();
- $pagination = pagination_make($query, true, 'id DESC');
- $list = $pagination['list'];
- foreach ($list as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $fromStore = Store::findOne($item['from_store_id']);
- $item['from_store_name'] = '-';
- if ($fromStore) {
- $item['from_store_name'] = $fromStore->name;
- }
- $toStoreIds = \json_decode($item['to_store_ids'], true);
- $storeNames = [];
- foreach ($toStoreIds as $storeId) {
- $store = Store::findOne($storeId);
- if ($store) {
- $storeNames[] = $store->name;
- }
- }
- $item['to_store_names'] = $storeNames;
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list,
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ],
- ]);
- }
- public function actionGetStoreList()
- {
- $list = Store::find()->select('id, name')->where([
- 'is_delete' => 0,
- ])->asArray()->all();
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $list,
- ]);
- }
- public function actionSave()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $data = \post_params();
- if (isset($data['id']) && $data['id']) {
- $model = StoreSync::findOne($data['id']);
- if (!$model) {
- throw new \Exception('任务未找到');
- }
- $model->status = 0;
- } else {
- $model = new StoreSync();
- }
- //同步店铺不能作为源店铺判断
- if ($data['from_store_id']) {
- $to_store_lock = StoreSyncLock::findOne(['to_store_id' => $data['from_store_id'], 'is_delete' => 0]);
- if ($to_store_lock) {
- throw new \Exception('当前店铺已经设置为被同步店铺不可设置为源店铺');
- }
- }
- $model->from_store_id = $data['from_store_id'];
- $model->to_store_ids = \json_encode($data['to_store_ids']);
- if ($data['imports']) {
- //
- if ((
- in_array('diy', $data['imports']) ||
- in_array('activityCutPrice', $data['imports']) ||
- in_array('pintuan', $data['imports']) ||
- in_array('seckill', $data['imports']) ||
- in_array('shareHolderConfig', $data['imports']) ||
- in_array('videoGoods', $data['imports']) ||
- in_array('integralStore', $data['imports']) ||
- in_array('workerConfig', $data['imports'])
- ) && !in_array('product', $data['imports'])) {
- throw new \Exception('同步装修、砍价、拼团、秒杀、股东分红、短视频、积分商城、上门服务需要同步商品');
- }
- }
- $model->imports = \json_encode($data['imports']);
- if (!$model->save()) {
- throw new \Exception(json_encode($model->errors, JSON_UNESCAPED_UNICODE));
- }
- if (!empty($data['to_store_ids'])) {
- if (isset($data['id']) && $data['id']) {
- StoreSyncLock::deleteAll(['batch_id' => $model->id]);
- }
- foreach ($data['to_store_ids'] as $to_store_id) {
- $lock = new StoreSyncLock();
- $lock->from_store_id = $data['from_store_id'];
- $lock->to_store_id = $to_store_id;
- $lock->imports = \json_encode($data['imports']);
- $lock->batch_id = $model->id;
- if(!$lock->save()) {
- throw new \Exception(json_encode($lock->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功',
- ]);
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => '保存失败:' . $e->getMessage(),
- ]);
- }
- }
- public function actionDel()
- {
- $id = \post_params('id');
- if (!$id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺少参数',
- ]);
- }
- $item = StoreSync::findOne($id);
- if (!$item) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '任务未找到',
- ]);
- }
- $item->is_delete = 1;
- if ($item->save()) {
- StoreSyncLock::updateAll(['is_delete' => 1], ['batch_id' => $id, 'is_delete' => 0]);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功',
- ]);
- }
- public function actionExe()
- {
- $id = \post_params('id');
- if (!$id) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺少参数',
- ]);
- }
- $item = StoreSync::findOne($id);
- if (!$item) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '任务未找到',
- ]);
- }
- if ($item->status == 1) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '当前已有相同的任务在等待执行中,请稍后再试!',
- ]);
- }
- $log = new StoreSyncLog();
- $log->from_store_id = $item->from_store_id;
- $log->to_store_ids = $item->to_store_ids;
- $log->status = 0;
- $log->sync_id = $item->id;
- $log->save();
- $to_store_ids = json_decode($item->to_store_ids, true);
- if (!empty($to_store_ids)) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- StoreSyncLock::deleteAll(['batch_id' => $item->id]);
- foreach ($to_store_ids as $to_store_id) {
- $lock = new StoreSyncLock();
- $lock->from_store_id = $item->from_store_id;
- $lock->to_store_id = $to_store_id;
- $lock->imports = $item->imports;
- $lock->batch_id = $item->id;
- if(!$lock->save()) {
- throw new \Exception(json_encode($lock->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- $id = \queue_push(new StoreSyncJob(['id' => $log->id]), 0, 1);
- debug_log($id, 'queuePush.log');
- $item->status = 1;
- $item->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- public function actionGetConfig() {
- try {
- $config = [
- [
- 'name' => '配送规则',
- 'key' => 'delivery_rules'
- ],
- [
- 'name' => '商品分类',
- 'key' => 'product_cat',
- 'require' => 'delivery_rules'
- ],
- [
- 'name' => '商城商品',
- 'key' => 'product',
- 'require' => 'product_cat'
- ],
- [
- 'name' => '商城装修',
- 'key' => 'diy',
- 'require' => 'product'
- ],
- [
- 'name' => '砍价',
- 'key' => 'activityCutPrice',
- 'require' => 'product'
- ],
- [
- 'name' => '拼团',
- 'key' => 'pintuan',
- 'require' => 'product'
- ],
- [
- 'name' => '秒杀',
- 'key' => 'seckill',
- 'require' => 'product'
- ],
- [
- 'name' => '分销设置',
- 'key' => 'shareConfig',
- 'require' => 'product'
- ],
- [
- 'name' => '股东设置',
- 'key' => 'shareHolderConfig',
- 'require' => 'product'
- ],
- [
- 'name' => '短视频',
- 'key' => 'videoGoods',
- 'require' => 'product'
- ],
- [
- 'name' => '积分商城',
- 'key' => 'integralStore',
- 'require' => 'product'
- ],
- [
- 'name' => '上门服务',
- 'key' => 'workerConfig',
- 'require' => 'product'
- ],
- [
- 'name' => '预约商品',
- 'key' => 'bookingGoods',
- 'require' => 'product'
- ],
- [
- 'name' => '文章',
- 'key' => 'article'
- ],
- [
- 'name' => '专题',
- 'key' => 'topic'
- ]
- ];
- return $this->asJson([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'config' => $config
- ]
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage()
- ]);
- }
- }
- }
|