提交 cb8f92ea 编写于 作者: T Terry

product mysql and mongodb services

上级 91096a0f
......@@ -391,6 +391,119 @@ class ProductMongodb extends Service implements ProductInterface
return $model;
}
/**
* @param $one|array , 产品数据数组
* 用于将mysql的数据,同步到mongodb中
*/
public function sync($one)
{
if (!$this->initSave($one)) {
return false;
}
$defaultLangTitle = Yii::$service->fecshoplang->getDefaultLangAttrVal($one['name'], 'name');
$product_one = $this->_productModel->find()->where([
'sku' => $one['sku'],
])->one();
if ($product_one['sku']) {
$model = $product_one;
} else {
$model = new $this->_productModelName();
$model->created_at = time();
$primaryVal = new \MongoDB\BSON\ObjectId();
$model->{$this->getPrimaryKey()} = $primaryVal;
}
if (isset($one['attr_group']) && $one['attr_group']) {
$this->addGroupAttrs($one['attr_group']);
}
// 保存mongodb表中的产品id到字段origin_mongo_id
$origin_mysql_id = $one['id'];
$model->origin_mysql_id = $origin_mysql_id;
$model->updated_at = time();
// 计算出来产品的最终价格。
$one['final_price'] = Yii::$service->product->price->getFinalPrice($one['price'], $one['special_price'], $one['special_from'], $one['special_to']);
$one['score'] = (int) $one['score'];
unset($one['id']);
/**
* 如果 $one['custom_option'] 不为空,则计算出来库存总数,填写到qty
*/
if (is_array($one['custom_option']) && !empty($one['custom_option'])) {
$custom_option_qty = 0;
foreach ($one['custom_option'] as $co_one) {
$custom_option_qty += $co_one['qty'];
}
$one['qty'] = $custom_option_qty;
}
/**
* 保存产品
*/
//$one = $this->serializeSaveData($one);
// 得到对应的mongodb的分类id数组
if ($c = $this->syncGetProductCategorys($one['category'])){
var_dump($c);
$one['category'] = $c;
}
$saveStatus = Yii::$service->helper->ar->save($model, $one);
$product_id = (string)$model->{$this->getPrimaryKey()};
// 自定义url部分
$originUrlKey = 'catalog/product/index';
$originUrl = $originUrlKey.'?'.$this->getPrimaryKey() .'='. $product_id;
$originUrlKey = isset($one['url_key']) ? $one['url_key'] : '';
//var_dump([$defaultLangTitle, $originUrl, $originUrlKey]);
//echo $defaultLangTitle;
$urlKey = Yii::$service->url->saveRewriteUrlKeyByStr($defaultLangTitle, $originUrl, $originUrlKey);
$model->url_key = $urlKey;
$model->save();
/**
* 更新产品库存。
*/
Yii::$service->product->stock->saveProductStock($product_id, $one);
/**
* 更新产品信息到搜索表。
*/
Yii::$service->search->syncProductInfo([$product_id]);
return $model;
}
/**
* @param $mysqlCategorys
* 同步数据,得到mongodb category arr
*/
protected function syncGetProductCategorys($mysqlCategorys)
{
if (empty($mysqlCategorys) || !is_array($mysqlCategorys)) {
return null;
}
Yii::$service->category->changeToMongoStorage();
$filter = [
'where' => [
['in', 'origin_mysql_id', $mysqlCategorys]
],
'numPerPage' => 10000,
'pageNum' => 1,
];
$data = Yii::$service->category->coll($filter);
$cIds = [];
if (is_array($data['coll']) || !empty($data['coll'])) {
foreach ($data['coll'] as $one) {
$cIds[] = (string)$one['_id'];
}
}
return $cIds;
}
public function getCategoryIdsByProductId($product_id)
{
$product = Yii::$service->product->getByPrimaryKey($product_id);
......
......@@ -232,23 +232,33 @@ class ProductMysqldb extends Service implements ProductInterface
}
/**
* 和coll()的不同在于,该方式不走active record,因此可以获取产品的所有数据的。
* 走这种方式,可以绕过产品属性组,因为产品属性组需要根据不同的
* 属性组,在active record 上面附加相应的属性,对api这种不适合。
*
*/
public function apicoll()
{
$collection = $this->_productModel->find()->getCollection();
$cursor = $collection->find();
$count = $collection->count();
$arr = [];
foreach ($cursor as $k =>$v) {
$v['_id'] = (string) $v['_id'];
$arr[$k] = $v;
$data = $this->coll();
$coll = $data['coll'];
$count = $data['count'];
$collArr = [];
if (is_array($coll)) {
foreach ($coll as $one) {
$arr = [];
foreach ($one as $k=>$v) {
if ($k != 'attr_group_info') {
$arr[$k] = $v;
} else if (is_array($v)){
foreach ($v as $spu_attr=>$spu_val) {
$arr[$spu_attr] = $spu_val;
}
}
}
$collArr[] = $arr;
}
}
return [
'coll' => $arr,
'coll' => $collArr,
'count'=> $count,
];
}
......@@ -275,8 +285,7 @@ class ProductMysqldb extends Service implements ProductInterface
*/
public function apiSave($product_one)
{
$collection = $this->_productModel->find()->getCollection();
$collection->save($product_one);
$this->save($product_one);
return true;
}
......@@ -287,8 +296,7 @@ class ProductMysqldb extends Service implements ProductInterface
*/
public function apiDelete($primaryKey)
{
$collection = $this->_productModel->find()->getCollection();
$collection->remove(['_id' => $primaryKey]);
$this->remove($primaryKey);
return true;
}
......@@ -615,7 +623,7 @@ class ProductMysqldb extends Service implements ProductInterface
])->all();
$arr = [];
foreach ($coll as $one) {
$arr[] = $one['category_id'];
$arr[] = (int)$one['category_id'];
}
return $arr;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册