Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
yii2_fecshop
提交
cb8f92ea
Y
yii2_fecshop
项目概览
OpenDocCN
/
yii2_fecshop
通知
17
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Y
yii2_fecshop
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
cb8f92ea
编写于
7月 19, 2019
作者:
T
Terry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
product mysql and mongodb services
上级
91096a0f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
138 addition
and
17 deletion
+138
-17
services/product/ProductMongodb.php
services/product/ProductMongodb.php
+113
-0
services/product/ProductMysqldb.php
services/product/ProductMysqldb.php
+25
-17
未找到文件。
services/product/ProductMongodb.php
浏览文件 @
cb8f92ea
...
...
@@ -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
);
...
...
services/product/ProductMysqldb.php
浏览文件 @
cb8f92ea
...
...
@@ -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'
=>
$
a
rr
,
'coll'
=>
$
collA
rr
,
'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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录