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() ]); } } }