纵有疾风起
人生不言弃

php商品拼单功能

设计图

 

 

 

 

 

 

 

需求简介:

用户可以创建拼单,也可以加入拼单,每个拼单有效时间是48小时内。

接口:

1、发起拼单

2、加入拼单

3、单个商品拼单

4、我的拼单

5、商城首页拼单商品推荐

mysql:

— —————————-
— Table structure for bao_ecbuy
— —————————-
DROP TABLE IF EXISTS `bao_ecbuy`;
CREATE TABLE `bao_ecbuy` (
`ecbuy_id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘拼单ID’,
`user_id` int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘创建拼单用户id’,
`goods_id` int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘拼单商品id’,
`title` varchar(11) NOT NULL DEFAULT ” COMMENT ‘商品标题’,
`photo` text NOT NULL COMMENT ‘ 商品详情页’,
`price` int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘商品当时的原价’,
`details` text COMMENT ‘商品当时的详情页’,
`profit_sharing` int(11) NOT NULL DEFAULT ‘0’ COMMENT ‘平台分成商品比例’,
`ecbuy_price` int(11) NOT NULL COMMENT ‘拼单价格’,
`ecbuy_num` int(11) NOT NULL COMMENT ‘拼单人数’,
`create_time` int(11) NOT NULL,
`end_time` int(11) NOT NULL COMMENT ‘结束时间’,
`status` int(1) NOT NULL DEFAULT ‘0’,
PRIMARY KEY (`ecbuy_id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8mb4;

— —————————-
— Table structure for bao_ecbuy_log
— —————————-
DROP TABLE IF EXISTS `bao_ecbuy_log`;
CREATE TABLE `bao_ecbuy_log` (
`ecbuy_id` int(11) NOT NULL,
`user_id` int(11) NOT NULL,
`create_time` int(11) NOT NULL,
`sku` varchar(50) NOT NULL DEFAULT ” COMMENT ‘参与拼单的人对应的sku’
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

php后台:

/*
* 创建拼单
* goods_id 商品id
* sku 商品sku
*/
public function ecbuy() {
$input = $this->request();
$goods_id = (int)filter($input[‘goods_id’]);
$sku = filter($input[‘sku’]);
$result = D(‘Ecbuy’)->where([‘goods_id’=>$goods_id,’user_id’=>$this->user[‘id’],’end_time’=>[‘EGT’,time()]])->find();
if($result){
$this->errorResponse(‘100031′,’您此商品下创建的拼单时间还没有到期哦’);
}
$goods = D(‘Goods’)->field(‘goods_id,cate_id,title,photo,price,details,ecbuy_num,ecbuy_price’)->where([‘goods_id’=>$goods_id])->find();
$cate = D(‘GoodsCate’)->field(‘profit_sharing’)->where([‘cate_id’=>$goods[‘cate_id’]])->find();

if(!$goods){
$this->errorResponse(‘100031′,’商品不存在’);
}
$endtime = strtotime(date(“Y-m-d H:i:s”,strtotime(“+2days”,strtotime(“now”))));

$data = [
‘goods_id’=>$goods[‘goods_id’],
‘title’=>$goods[‘title’],
‘photo’=>$goods[‘photo’],
‘price’=>$goods[‘price’],
‘details’=>$goods[‘details’],
‘profit_sharing’=>$cate[‘profit_sharing’],
‘user_id’=>$this->user[‘id’],
‘ecbuy_price’=>$goods[‘ecbuy_price’],
‘ecbuy_num’=>$goods[‘ecbuy_num’],
‘create_time’=>time(),
‘end_time’=>$endtime,
];
$ecbuy = D(‘Ecbuy’);
$ecbuy->startTrans();
if(!$id = D(‘Ecbuy’)->add($data)){
$this->errorResponse(‘100031′,’创建拼单失败’);
}else{
D(‘EcbuyLog’)->add([
‘ecbuy_id’=>$id,
‘user_id’=>$this->user[‘id’],
‘sku’=>$sku,
‘create_time’=>time(),
]);
if(!$ecbuy->commit()){
$ecbuy->rollback();
$this->errorResponse(‘100031′,’创建拼单失败’);
}else{
unset($data);
$data[‘ecbuy_id’] = $id;
$this->response($data);
}
}

}

 

/*
* 加入拼单
* 拼单id
* sku 商品sku
*/

public function joinEcbuy($ecbuy_id) {
if(empty($ecbuy_id)){
$this->errorResponse(‘100031′,’拼单id缺失’);
}
$input = $this->request();
$sku = filter($input[‘sku’]);
$Ecbuy = D(‘Ecbuy’);
$EcbuyLog = D(‘EcbuyLog’);
$ecbuy = $Ecbuy->find($ecbuy_id);

$Ecbuy->startTrans();
$ecbuy_num = $EcbuyLog->where([‘ecbuy_id’=>$ecbuy[‘ecbuy_id’]])->count();

if($ecbuy_num>=$ecbuy[‘ecbuy_num’]){
$this->errorResponse(‘100031′,’拼单人数已够,请选择其他拼单’);
}

$result = $EcbuyLog->add([
‘ecbuy_id’ => $ecbuy_id,
‘user_id’ =>$this->user[‘id’],
‘sku’ =>$sku,
‘create_time’=>time(),
]);

if(!$result){
$this->errorResponse(‘100031′,’加入拼单失败’);
}
$surplus = $ecbuy[‘ecbuy_num’]-$ecbuy_num;

if((int)$surplus==1){//表示拼单成功
$Ecbuy->setField([‘ecbuy_id’=>$ecbuy[‘ecbuy_id’],’status’=>1]); //ecybuy表记录此拼单成功
D(‘Goods’)->where([‘goods_id’=>$ecbuy[‘goods_id’]])->setInc(‘ecbuy_total’); //商品拼单成功记录数加1
}
if($Ecbuy->commit()){
$data[‘message’] = ‘成功加入拼单’;
$this->response($data);
}else{
$Ecbuy->rollback();
$this->errorResponse(‘100031′,’加入拼单失败’);
}

}

/*
* 详情页拼单列表
*/
public function goodDetailsEcbuy($goods_id) {
$ecbuy = D(‘Ecbuy’);
$time = time();
$sql = “select ecbuy.ecbuy_id,ecbuy.user_id,ecbuy.ecbuy_num,ecbuy.create_time,ecbuy.end_time,user.nickname,user.face ”
. “FROM bao_ecbuy as ecbuy left join bao_users as user”
. ” on ecbuy.user_id=user.user_id ”
. “where ecbuy.goods_id = {$goods_id} ”
. “and ecbuy.status=0 and ecbuy.end_time > {$time}”;
$list = $ecbuy->query($sql);
foreach ($list as $key => $value) {
//差几人成团
$list[$key][‘surplus_people’] = (int)$value[‘ecbuy_num’] – (int)D(‘EcbuyLog’)->where([‘ecbuy_id’=>$value[‘ecbuy_id’]])->count();
//差多少时间结束拼单
$list[$key][‘surplus_time’] = time2Units((int)$value[‘end_time’]- (int)time());
}

$this->response($list);
}

 

/*
* 一周拼单的最多商品
* 热门模块
*/
public function hotEcbuy() {
$input = $this->reqruest();
$page = filter($input[‘page’], 1);
$perPage = filter($input[‘per_page’], 10);
$perPage > 30 && $perPage = 30;

$Ecbuy = D(‘Ecbuy’);
$where = [];
$week = $endtime = strtotime(date(“Y-m-d H:i:s”,strtotime(“-7days”,strtotime(“now”))));
$where[‘create_time’] = [‘gt’,$week];
$where[‘status’] = 0;
$ecbuy = $Ecbuy
->where($where)
->field(‘goods_id,ecbuy_id’)
->distinct(‘goods_id’)
->page($page,$perPage)
->select();

foreach ($ecbuy as $key=>$value) {
$ecbuy[$key][‘totalnum’] = D(‘EcbuyLog’)->where([‘ecbuy_id’=>$value[‘ecbuy_id’]])->count();
}

$item=array();
foreach($ecbuy as $k=>$v){
if(!isset($item[$v[‘goods_id’]])){
$item[$v[‘goods_id’]]=$v;
}else{
$item[$v[‘goods_id’]][‘totalnum’]+=(int)$v[‘totalnum’];
}
}
ModelUtils::hasOne($item, [‘relation’ => ‘Goods’, ‘refKey’ => ‘goods_id’, ‘foreignKey’ => ‘goods_id’,’name’=>’goods’,’fields’=>’goods_id,title,photo,ecbuy_price,ecbuy_total’]);
$goodslist = array();
foreach ($item as $kk => $kv) {
if($kk == $kv[‘goods’][‘goods_id’]){
$goodslist[$kk] = array_merge($item[$kk],$kv[‘goods’]);
unset($goodslist[$kk][‘goods’]);
unset($goodslist[$kk][‘monthly_sales_count’]);
unset($goodslist[$kk][‘ecbuy_id’]);
}
}
$this->response($goodslist);
}

 

/*
* 我的拼单列表
* user_id
*/
public function myEcbuy($user_id) {

}

未经允许不得转载:起风网 » php商品拼单功能
分享到: 生成海报

评论 抢沙发

评论前必须登录!

立即登录