Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
yii2_fecshop
提交
d35d0a13
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 搜索 >>
提交
d35d0a13
编写于
7月 18, 2019
作者:
T
Terry
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support mysql: product favorite
上级
e2c47e97
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
465 addition
and
0 deletion
+465
-0
services/product/favorite/FavoriteInterface.php
services/product/favorite/FavoriteInterface.php
+27
-0
services/product/favorite/FavoriteMongodb.php
services/product/favorite/FavoriteMongodb.php
+219
-0
services/product/favorite/FavoriteMysqldb.php
services/product/favorite/FavoriteMysqldb.php
+219
-0
未找到文件。
services/product/favorite/FavoriteInterface.php
0 → 100644
浏览文件 @
d35d0a13
<?php
/*
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace
fecshop\services\product\review
;
/**
* Product services interface.
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
interface
FavoriteInterface
{
public
function
getByPrimaryKey
(
$primaryKey
);
public
function
coll
(
$filter
);
public
function
save
(
$one
);
public
function
remove
(
$ids
);
}
services/product/favorite/FavoriteMongodb.php
0 → 100644
浏览文件 @
d35d0a13
<?php
/*
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace
fecshop\services\product\favorite
;
//use fecshop\models\mongodb\product\Favorite as FavoriteModel;
use
fecshop\services\Service
;
use
Yii
;
/**
* Product Favorite Services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class
FavoriteMongodb
extends
Service
{
protected
$_favoriteModelName
=
'\fecshop\models\mongodb\product\Favorite'
;
protected
$_favoriteModel
;
public
function
init
()
{
parent
::
init
();
list
(
$this
->
_favoriteModelName
,
$this
->
_favoriteModel
)
=
\
Yii
::
mapGet
(
$this
->
_favoriteModelName
);
}
protected
function
actionGetPrimaryKey
()
{
return
'_id'
;
}
protected
function
actionGetByPrimaryKey
(
$val
)
{
$one
=
$this
->
_favoriteModel
->
findOne
(
$val
);
if
(
$one
[
$this
->
getPrimaryKey
()])
{
return
$one
;
}
else
{
return
new
$this
->
_favoriteModelName
();
}
}
/**
* @param $product_id | String , 产品id
* @param $user_id | Int ,用户id
* @return $this->_favoriteModel ,如果用户在该产品收藏,则返回相应model。
*/
protected
function
actionGetByProductIdAndUserId
(
$product_id
,
$user_id
=
''
)
{
if
(
!
$user_id
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$user_id
=
$identity
[
'id'
];
}
if
(
$user_id
)
{
$one
=
$this
->
_favoriteModel
->
findOne
([
'product_id'
=>
$product_id
,
'user_id'
=>
$user_id
,
]);
if
(
$one
[
$this
->
getPrimaryKey
()])
{
return
$one
;
}
}
}
/**
* @param $product_id | String , 产品id
* @param $user_id | Int ,用户id
* @return boolean,用户收藏该产品时,执行的操作。
*/
protected
function
actionAdd
(
$product_id
,
$user_id
)
{
$user_id
=
(
int
)
$user_id
;
$productPrimaryKey
=
Yii
::
$service
->
product
->
getPrimaryKey
();
$product
=
Yii
::
$service
->
product
->
getByPrimaryKey
(
$product_id
);
// 检查产品是否存在,如果不存在,输出报错信息。
if
(
!
isset
(
$product
[
$productPrimaryKey
]))
{
Yii
::
$service
->
helper
->
errors
->
add
(
'product is not exist!'
);
return
;
}
//echo $product_id;exit;
$favoritePrimaryKey
=
Yii
::
$service
->
product
->
favorite
->
getPrimaryKey
();
$one
=
$this
->
_favoriteModel
->
findOne
([
'product_id'
=>
$product_id
,
'user_id'
=>
$user_id
,
]);
if
(
isset
(
$one
[
$favoritePrimaryKey
]))
{
$one
->
updated_at
=
time
();
$one
->
store
=
Yii
::
$service
->
store
->
currentStore
;
$one
->
save
();
return
true
;
}
$one
=
new
$this
->
_favoriteModelName
();
$one
->
product_id
=
$product_id
;
$one
->
user_id
=
$user_id
;
$one
->
created_at
=
time
();
$one
->
updated_at
=
time
();
$one
->
store
=
Yii
::
$service
->
store
->
currentStore
;
$one
->
save
();
// 更新该用户总的收藏产品个数到用户表
$this
->
updateUserFavoriteCount
(
$user_id
);
$this
->
updateProductFavoriteCount
(
$product_id
);
return
true
;
}
/**
* @param $product_id | String
* 更新该产品被收藏的总次数。
*/
protected
function
updateProductFavoriteCount
(
$product_id
)
{
if
(
$product_id
)
{
$count
=
$this
->
_favoriteModel
->
find
()
->
where
([
'product_id'
=>
$product_id
])
->
count
();
Yii
::
$service
->
product
->
updateProductFavoriteCount
(
$product_id
,
$count
);
}
}
/**
* @param $user_id | Int
* 更新该用户总的收藏产品个数到用户表
*/
protected
function
updateUserFavoriteCount
(
$user_id
=
''
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
if
(
!
$user_id
)
{
$user_id
=
$identity
[
'id'
];
}
if
(
$user_id
)
{
$count
=
$this
->
_favoriteModel
->
find
()
->
where
([
'user_id'
=>
$user_id
])
->
count
();
$identity
->
favorite_product_count
=
$count
;
$identity
->
save
();
}
}
/*
* example filter:
* [
* 'numPerPage' => 20,
* 'pageNum' => 1,
* 'orderBy' => [$this->getPrimaryKey() => SORT_DESC, 'sku' => SORT_ASC ],
* 'where' => [
['>','price',1],
['<=','price',10]
* ['sku' => 'uk10001'],
* ],
* 'asArray' => true,
* ]
*/
protected
function
actionList
(
$filter
)
{
$query
=
$this
->
_favoriteModel
->
find
();
$query
=
Yii
::
$service
->
helper
->
ar
->
getCollByFilter
(
$query
,
$filter
);
return
[
'coll'
=>
$query
->
all
(),
'count'
=>
$query
->
limit
(
null
)
->
offset
(
null
)
->
count
(),
];
}
protected
function
actionColl
(
$filter
)
{
return
$this
->
list
(
$filter
);
}
/**
* @param $favorite_id | string
* 通过id删除favorite
*/
public
function
removeByProductIdAndUserId
(
$product_id
,
$user_id
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$user_id
=
$identity
[
'id'
];
$one
=
$this
->
_favoriteModel
->
findOne
([
//'_id' => new \MongoDB\BSON\ObjectId($favorite_id),
'user_id'
=>
$user_id
,
'product_id'
=>
$product_id
,
]);
if
(
$one
[
'_id'
])
{
$one
->
delete
();
$this
->
updateUserFavoriteCount
(
$user_id
);
$product_id
=
(
string
)
$one
[
'product_id'
];
$this
->
updateProductFavoriteCount
(
$product_id
);
return
true
;
}
}
/**
* @param $favorite_id | string
* 通过id删除favorite
*/
protected
function
actionCurrentUserRemove
(
$favorite_id
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$user_id
=
$identity
[
'id'
];
$one
=
$this
->
_favoriteModel
->
findOne
([
'_id'
=>
new
\
MongoDB\BSON\ObjectId
(
$favorite_id
),
'user_id'
=>
$user_id
,
]);
if
(
$one
[
'_id'
])
{
$one
->
delete
();
$this
->
updateUserFavoriteCount
(
$user_id
);
$product_id
=
(
string
)
$one
[
'product_id'
];
$this
->
updateProductFavoriteCount
(
$product_id
);
return
true
;
}
}
}
services/product/favorite/FavoriteMysqldb.php
0 → 100644
浏览文件 @
d35d0a13
<?php
/*
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace
fecshop\services\product\favorite
;
//use fecshop\models\mongodb\product\Favorite as FavoriteModel;
use
fecshop\services\Service
;
use
Yii
;
/**
* Product Favorite Services
* @author Terry Zhao <2358269014@qq.com>
* @since 1.0
*/
class
FavoriteMysqldb
extends
Service
{
protected
$_favoriteModelName
=
'\fecshop\models\mysqldb\product\Favorite'
;
protected
$_favoriteModel
;
public
function
init
()
{
parent
::
init
();
list
(
$this
->
_favoriteModelName
,
$this
->
_favoriteModel
)
=
\
Yii
::
mapGet
(
$this
->
_favoriteModelName
);
}
protected
function
actionGetPrimaryKey
()
{
return
'id'
;
}
protected
function
actionGetByPrimaryKey
(
$val
)
{
$one
=
$this
->
_favoriteModel
->
findOne
(
$val
);
if
(
$one
[
$this
->
getPrimaryKey
()])
{
return
$one
;
}
else
{
return
new
$this
->
_favoriteModelName
();
}
}
/**
* @param $product_id | String , 产品id
* @param $user_id | Int ,用户id
* @return $this->_favoriteModel ,如果用户在该产品收藏,则返回相应model。
*/
protected
function
actionGetByProductIdAndUserId
(
$product_id
,
$user_id
=
''
)
{
if
(
!
$user_id
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$user_id
=
$identity
[
'id'
];
}
if
(
$user_id
)
{
$one
=
$this
->
_favoriteModel
->
findOne
([
'product_id'
=>
$product_id
,
'user_id'
=>
$user_id
,
]);
if
(
$one
[
$this
->
getPrimaryKey
()])
{
return
$one
;
}
}
}
/**
* @param $product_id | String , 产品id
* @param $user_id | Int ,用户id
* @return boolean,用户收藏该产品时,执行的操作。
*/
protected
function
actionAdd
(
$product_id
,
$user_id
)
{
$user_id
=
(
int
)
$user_id
;
$productPrimaryKey
=
Yii
::
$service
->
product
->
getPrimaryKey
();
$product
=
Yii
::
$service
->
product
->
getByPrimaryKey
(
$product_id
);
// 检查产品是否存在,如果不存在,输出报错信息。
if
(
!
isset
(
$product
[
$productPrimaryKey
]))
{
Yii
::
$service
->
helper
->
errors
->
add
(
'product is not exist!'
);
return
;
}
//echo $product_id;exit;
$favoritePrimaryKey
=
Yii
::
$service
->
product
->
favorite
->
getPrimaryKey
();
$one
=
$this
->
_favoriteModel
->
findOne
([
'product_id'
=>
$product_id
,
'user_id'
=>
$user_id
,
]);
if
(
isset
(
$one
[
$favoritePrimaryKey
]))
{
$one
->
updated_at
=
time
();
$one
->
store
=
Yii
::
$service
->
store
->
currentStore
;
$one
->
save
();
return
true
;
}
$one
=
new
$this
->
_favoriteModelName
();
$one
->
product_id
=
$product_id
;
$one
->
user_id
=
$user_id
;
$one
->
created_at
=
time
();
$one
->
updated_at
=
time
();
$one
->
store
=
Yii
::
$service
->
store
->
currentStore
;
$one
->
save
();
// 更新该用户总的收藏产品个数到用户表
$this
->
updateUserFavoriteCount
(
$user_id
);
$this
->
updateProductFavoriteCount
(
$product_id
);
return
true
;
}
/**
* @param $product_id | String
* 更新该产品被收藏的总次数。
*/
protected
function
updateProductFavoriteCount
(
$product_id
)
{
if
(
$product_id
)
{
$count
=
$this
->
_favoriteModel
->
find
()
->
where
([
'product_id'
=>
$product_id
])
->
count
();
Yii
::
$service
->
product
->
updateProductFavoriteCount
(
$product_id
,
$count
);
}
}
/**
* @param $user_id | Int
* 更新该用户总的收藏产品个数到用户表
*/
protected
function
updateUserFavoriteCount
(
$user_id
=
''
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
if
(
!
$user_id
)
{
$user_id
=
$identity
[
'id'
];
}
if
(
$user_id
)
{
$count
=
$this
->
_favoriteModel
->
find
()
->
where
([
'user_id'
=>
$user_id
])
->
count
();
$identity
->
favorite_product_count
=
$count
;
$identity
->
save
();
}
}
/*
* example filter:
* [
* 'numPerPage' => 20,
* 'pageNum' => 1,
* 'orderBy' => [$this->getPrimaryKey() => SORT_DESC, 'sku' => SORT_ASC ],
* 'where' => [
['>','price',1],
['<=','price',10]
* ['sku' => 'uk10001'],
* ],
* 'asArray' => true,
* ]
*/
protected
function
actionList
(
$filter
)
{
$query
=
$this
->
_favoriteModel
->
find
();
$query
=
Yii
::
$service
->
helper
->
ar
->
getCollByFilter
(
$query
,
$filter
);
return
[
'coll'
=>
$query
->
all
(),
'count'
=>
$query
->
limit
(
null
)
->
offset
(
null
)
->
count
(),
];
}
protected
function
actionColl
(
$filter
)
{
return
$this
->
list
(
$filter
);
}
/**
* @param $favorite_id | string
* 通过id删除favorite
*/
public
function
removeByProductIdAndUserId
(
$product_id
,
$user_id
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$user_id
=
$identity
[
'id'
];
$favoritePrimaryKey
=
$this
->
getPrimaryKey
();
$one
=
$this
->
_favoriteModel
->
findOne
([
//'_id' => new \MongoDB\BSON\ObjectId($favorite_id),
'user_id'
=>
$user_id
,
'product_id'
=>
$product_id
,
]);
if
(
$one
[
$favoritePrimaryKey
])
{
$one
->
delete
();
$this
->
updateUserFavoriteCount
(
$user_id
);
$product_id
=
(
string
)
$one
[
'product_id'
];
$this
->
updateProductFavoriteCount
(
$product_id
);
return
true
;
}
}
/**
* @param $favorite_id | string
* 通过id删除favorite
*/
protected
function
actionCurrentUserRemove
(
$favorite_id
)
{
$identity
=
Yii
::
$app
->
user
->
identity
;
$user_id
=
$identity
[
'id'
];
$favoritePrimaryKey
=
$this
->
getPrimaryKey
();
$one
=
$this
->
_favoriteModel
->
findOne
([
$favoritePrimaryKey
=>
$favorite_id
,
'user_id'
=>
$user_id
,
]);
if
(
$one
[
$favoritePrimaryKey
])
{
$one
->
delete
();
$this
->
updateUserFavoriteCount
(
$user_id
);
$product_id
=
(
string
)
$one
[
'product_id'
];
$this
->
updateProductFavoriteCount
(
$product_id
);
return
true
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录