StoreSyncController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\StoreSyncLock;
  9. use Yii;
  10. use app\models\StoreSync;
  11. use app\models\StoreSyncLog;
  12. use app\models\Store;
  13. use app\jobs\StoreSyncJob;
  14. class StoreSyncController extends BaseController
  15. {
  16. public function actionGetList()
  17. {
  18. $status = get_params('status', -1);
  19. $query = StoreSync::find()->where([
  20. 'is_delete' => 0,
  21. ]);
  22. if ($status > -1 && \in_array($status, [0, 1, 2])) {
  23. $query->andWhere(['status' => $status]);
  24. }
  25. $pagination = pagination_make($query, true, 'id DESC');
  26. $list = $pagination['list'];
  27. foreach ($list as &$item) {
  28. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  29. $fromStore = Store::findOne($item['from_store_id']);
  30. $item['from_store_name'] = '-';
  31. if ($fromStore) {
  32. $item['from_store_name'] = $fromStore->name;
  33. }
  34. $toStoreIds = \json_decode($item['to_store_ids'], true);
  35. $item['imports'] = \json_decode($item['imports'], true);
  36. $item['to_store_ids'] = $toStoreIds;
  37. $item['status'] = (int)$item['status'];
  38. $storeNames = [];
  39. foreach ($toStoreIds as $storeId) {
  40. $store = Store::findOne($storeId);
  41. if ($store) {
  42. $storeNames[] = [
  43. 'id' => $storeId,
  44. 'name' => $store->name
  45. ];
  46. }
  47. }
  48. $item['to_store_names'] = $storeNames;
  49. }
  50. return $this->asJson([
  51. 'code' => 0,
  52. 'msg' => 'success',
  53. 'data' => [
  54. 'data' => $list,
  55. 'pageNo' => $pagination['pageNo'],
  56. 'totalCount' => $pagination['totalCount'],
  57. ],
  58. ]);
  59. }
  60. public function actionGetLogList()
  61. {
  62. $query = StoreSyncLog::find();
  63. $pagination = pagination_make($query, true, 'id DESC');
  64. $list = $pagination['list'];
  65. foreach ($list as &$item) {
  66. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  67. $fromStore = Store::findOne($item['from_store_id']);
  68. $item['from_store_name'] = '-';
  69. if ($fromStore) {
  70. $item['from_store_name'] = $fromStore->name;
  71. }
  72. $toStoreIds = \json_decode($item['to_store_ids'], true);
  73. $storeNames = [];
  74. foreach ($toStoreIds as $storeId) {
  75. $store = Store::findOne($storeId);
  76. if ($store) {
  77. $storeNames[] = $store->name;
  78. }
  79. }
  80. $item['to_store_names'] = $storeNames;
  81. }
  82. return $this->asJson([
  83. 'code' => 0,
  84. 'msg' => 'success',
  85. 'data' => [
  86. 'data' => $list,
  87. 'pageNo' => $pagination['pageNo'],
  88. 'totalCount' => $pagination['totalCount'],
  89. ],
  90. ]);
  91. }
  92. public function actionGetStoreList()
  93. {
  94. $list = Store::find()->select('id, name')->where([
  95. 'is_delete' => 0,
  96. ])->asArray()->all();
  97. return $this->asJson([
  98. 'code' => 0,
  99. 'msg' => 'success',
  100. 'data' => $list,
  101. ]);
  102. }
  103. public function actionSave()
  104. {
  105. $t = \Yii::$app->db->beginTransaction();
  106. try {
  107. $data = \post_params();
  108. if (isset($data['id']) && $data['id']) {
  109. $model = StoreSync::findOne($data['id']);
  110. if (!$model) {
  111. throw new \Exception('任务未找到');
  112. }
  113. $model->status = 0;
  114. } else {
  115. $model = new StoreSync();
  116. }
  117. //同步店铺不能作为源店铺判断
  118. if ($data['from_store_id']) {
  119. $to_store_lock = StoreSyncLock::findOne(['to_store_id' => $data['from_store_id'], 'is_delete' => 0]);
  120. if ($to_store_lock) {
  121. throw new \Exception('当前店铺已经设置为被同步店铺不可设置为源店铺');
  122. }
  123. }
  124. $model->from_store_id = $data['from_store_id'];
  125. $model->to_store_ids = \json_encode($data['to_store_ids']);
  126. if ($data['imports']) {
  127. //
  128. if ((
  129. in_array('diy', $data['imports']) ||
  130. in_array('activityCutPrice', $data['imports']) ||
  131. in_array('pintuan', $data['imports']) ||
  132. in_array('seckill', $data['imports']) ||
  133. in_array('shareHolderConfig', $data['imports']) ||
  134. in_array('videoGoods', $data['imports']) ||
  135. in_array('integralStore', $data['imports']) ||
  136. in_array('workerConfig', $data['imports'])
  137. ) && !in_array('product', $data['imports'])) {
  138. throw new \Exception('同步装修、砍价、拼团、秒杀、股东分红、短视频、积分商城、上门服务需要同步商品');
  139. }
  140. }
  141. $model->imports = \json_encode($data['imports']);
  142. if (!$model->save()) {
  143. throw new \Exception(json_encode($model->errors, JSON_UNESCAPED_UNICODE));
  144. }
  145. if (!empty($data['to_store_ids'])) {
  146. if (isset($data['id']) && $data['id']) {
  147. StoreSyncLock::deleteAll(['batch_id' => $model->id]);
  148. }
  149. foreach ($data['to_store_ids'] as $to_store_id) {
  150. $lock = new StoreSyncLock();
  151. $lock->from_store_id = $data['from_store_id'];
  152. $lock->to_store_id = $to_store_id;
  153. $lock->imports = \json_encode($data['imports']);
  154. $lock->batch_id = $model->id;
  155. if(!$lock->save()) {
  156. throw new \Exception(json_encode($lock->errors, JSON_UNESCAPED_UNICODE));
  157. }
  158. }
  159. }
  160. $t->commit();
  161. return $this->asJson([
  162. 'code' => 0,
  163. 'msg' => '保存成功',
  164. ]);
  165. } catch (\Exception $e) {
  166. $t->rollBack();
  167. return $this->asJson([
  168. 'code' => 1,
  169. 'msg' => '保存失败:' . $e->getMessage(),
  170. ]);
  171. }
  172. }
  173. public function actionDel()
  174. {
  175. $id = \post_params('id');
  176. if (!$id) {
  177. return $this->asJson([
  178. 'code' => 1,
  179. 'msg' => '缺少参数',
  180. ]);
  181. }
  182. $item = StoreSync::findOne($id);
  183. if (!$item) {
  184. return $this->asJson([
  185. 'code' => 1,
  186. 'msg' => '任务未找到',
  187. ]);
  188. }
  189. $item->is_delete = 1;
  190. if ($item->save()) {
  191. StoreSyncLock::updateAll(['is_delete' => 1], ['batch_id' => $id, 'is_delete' => 0]);
  192. }
  193. return $this->asJson([
  194. 'code' => 0,
  195. 'msg' => '删除成功',
  196. ]);
  197. }
  198. public function actionExe()
  199. {
  200. $id = \post_params('id');
  201. if (!$id) {
  202. return $this->asJson([
  203. 'code' => 1,
  204. 'msg' => '缺少参数',
  205. ]);
  206. }
  207. $item = StoreSync::findOne($id);
  208. if (!$item) {
  209. return $this->asJson([
  210. 'code' => 1,
  211. 'msg' => '任务未找到',
  212. ]);
  213. }
  214. if ($item->status == 1) {
  215. return $this->asJson([
  216. 'code' => 1,
  217. 'msg' => '当前已有相同的任务在等待执行中,请稍后再试!',
  218. ]);
  219. }
  220. $log = new StoreSyncLog();
  221. $log->from_store_id = $item->from_store_id;
  222. $log->to_store_ids = $item->to_store_ids;
  223. $log->status = 0;
  224. $log->sync_id = $item->id;
  225. $log->save();
  226. $to_store_ids = json_decode($item->to_store_ids, true);
  227. if (!empty($to_store_ids)) {
  228. $t = \Yii::$app->db->beginTransaction();
  229. try {
  230. StoreSyncLock::deleteAll(['batch_id' => $item->id]);
  231. foreach ($to_store_ids as $to_store_id) {
  232. $lock = new StoreSyncLock();
  233. $lock->from_store_id = $item->from_store_id;
  234. $lock->to_store_id = $to_store_id;
  235. $lock->imports = $item->imports;
  236. $lock->batch_id = $item->id;
  237. if(!$lock->save()) {
  238. throw new \Exception(json_encode($lock->errors, JSON_UNESCAPED_UNICODE));
  239. }
  240. }
  241. $t->commit();
  242. } catch (\Exception $e) {
  243. $t->rollBack();
  244. return $this->asJson([
  245. 'code' => 1,
  246. 'msg' => $e->getMessage(),
  247. ]);
  248. }
  249. }
  250. $id = \queue_push(new StoreSyncJob(['id' => $log->id]), 0, 1);
  251. debug_log($id, 'queuePush.log');
  252. $item->status = 1;
  253. $item->save();
  254. return $this->asJson([
  255. 'code' => 0,
  256. 'msg' => '操作成功',
  257. ]);
  258. }
  259. public function actionGetConfig() {
  260. try {
  261. $config = [
  262. [
  263. 'name' => '配送规则',
  264. 'key' => 'delivery_rules'
  265. ],
  266. [
  267. 'name' => '商品分类',
  268. 'key' => 'product_cat',
  269. 'require' => 'delivery_rules'
  270. ],
  271. [
  272. 'name' => '商城商品',
  273. 'key' => 'product',
  274. 'require' => 'product_cat'
  275. ],
  276. [
  277. 'name' => '商城装修',
  278. 'key' => 'diy',
  279. 'require' => 'product'
  280. ],
  281. [
  282. 'name' => '砍价',
  283. 'key' => 'activityCutPrice',
  284. 'require' => 'product'
  285. ],
  286. [
  287. 'name' => '拼团',
  288. 'key' => 'pintuan',
  289. 'require' => 'product'
  290. ],
  291. [
  292. 'name' => '秒杀',
  293. 'key' => 'seckill',
  294. 'require' => 'product'
  295. ],
  296. [
  297. 'name' => '分销设置',
  298. 'key' => 'shareConfig',
  299. 'require' => 'product'
  300. ],
  301. [
  302. 'name' => '股东设置',
  303. 'key' => 'shareHolderConfig',
  304. 'require' => 'product'
  305. ],
  306. [
  307. 'name' => '短视频',
  308. 'key' => 'videoGoods',
  309. 'require' => 'product'
  310. ],
  311. [
  312. 'name' => '积分商城',
  313. 'key' => 'integralStore',
  314. 'require' => 'product'
  315. ],
  316. [
  317. 'name' => '上门服务',
  318. 'key' => 'workerConfig',
  319. 'require' => 'product'
  320. ],
  321. [
  322. 'name' => '预约商品',
  323. 'key' => 'bookingGoods',
  324. 'require' => 'product'
  325. ],
  326. [
  327. 'name' => '文章',
  328. 'key' => 'article'
  329. ],
  330. [
  331. 'name' => '专题',
  332. 'key' => 'topic'
  333. ]
  334. ];
  335. return $this->asJson([
  336. 'code' => 0,
  337. 'msg' => '获取成功',
  338. 'data' => [
  339. 'config' => $config
  340. ]
  341. ]);
  342. } catch (\Exception $e) {
  343. return $this->asJson([
  344. 'code' => 1,
  345. 'msg' => $e->getMessage()
  346. ]);
  347. }
  348. }
  349. }