作者 耿培杰

接口开发

... ... @@ -73,12 +73,17 @@ function get_price($arr)
{
//会员特价
if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['vip_price'];
//团购价格
if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 2) return get_vip_price_default($arr['group_price']);
if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 3) return get_staff_price_default($arr['group_price']);
if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 4) return get_agency_price_default($arr['group_price']);
//代理基础价
if ($arr['user_type'] == 4) {
return get_agency_price_default($arr['goods_price']);
} elseif ($arr['user_type'] == 3) {
//员工基础价
return get_vip_price_default($arr['goods_price']);
return get_staff_price_default($arr['goods_price']);
} elseif ($arr['user_type'] == 2) {
//会员基础价
return get_vip_price_default($arr['goods_price']);
... ... @@ -94,12 +99,16 @@ function get_discount_price($arr)
{
//会员特价
if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['goods_price'] - $arr['vip_price'];
//团购价格
if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 2) return $arr['group_price'] - get_vip_price_default($arr['group_price']);
if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 3) return $arr['group_price'] - get_staff_price_default($arr['group_price']);
if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 4) return $arr['group_price'] - get_agency_price_default($arr['group_price']);
//代理基础价折扣
if ($arr['user_type'] == 4) {
return $arr['goods_price'] - get_staff_price_default($arr['goods_price']);
return $arr['goods_price'] - get_agency_price_default($arr['goods_price']);
} elseif ($arr['user_type'] == 3) {
//员工基础价折扣
return $arr['goods_price'] - get_vip_price_default($arr['goods_price']);
return $arr['goods_price'] - get_staff_price_default($arr['goods_price']);
} elseif ($arr['user_type'] == 2) {
//会员基础价折扣
return $arr['goods_price'] - get_vip_price_default($arr['goods_price']);
... ...
... ... @@ -86,77 +86,6 @@ class Goods extends Api
/**
* @ApiTitle (团购商品列表(首页))
* @ApiSummary (团购商品列表(首页))
* @ApiMethod (POST)
* @ApiRoute (/api/goods/getGroupGoodsIndex)
* @ApiReturn({
"code": 1,
"msg": "请求成功",
"time": "1587463117",
"data": [
{
"goods_id": 1, 商品id
"ch_name": "EMP精选 澳洲白肉油桃 500~540g 4只装", 中文名称
"en_name": "EMP selected Australian white meat nectarines Australian meat nectarines", 英文名称
"image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图
"goods_price": "0.00", 普通售价
"group_price": "0.00", 拼团售价 拼团售价优先级最高
"vip_price": "0.00", 会员售价
"country_ch_name": "意大利", 原产地中文
"country_en_name": "Ltaly", 原产地英文
"is_vip_price": "2", 会员特价:1=开启,2=关闭
"stock_num": 55 库存
}
]
})
*/
public function getGroupGoodsIndex()
{
$where['g.is_group'] = 1;
$data = $this->goodsModel->selectData($where, 5);
$this->success('请求成功', $data);
}
/**
* @ApiTitle (团购商品列表)
* @ApiSummary (团购商品列表)
* @ApiMethod (POST)
* @ApiRoute (/api/goods/getGroupGoodsList)
* @ApiParams (name=page, type=string, required=false, description="页数")
* @ApiReturn({
"code": 1,
"msg": "请求成功",
"time": "1587463117",
"data": [
{
"goods_id": 1, 商品id
"ch_name": "EMP精选 澳洲白肉油桃 500~540g 4只装", 中文名称
"en_name": "EMP selected Australian white meat nectarines Australian meat nectarines", 英文名称
"image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图
"goods_price": "0.00", 普通售价
"group_price": "0.00", 拼团售价 拼团售价优先级最高
"vip_price": "0.00", 会员售价
"country_ch_name": "意大利", 原产地中文
"country_en_name": "Ltaly", 原产地英文
"is_vip_price": "2", 会员特价:1=开启,2=关闭
"stock_num": 55 库存
}
]
})
*/
public function getGroupGoodsList()
{
$page = $this->request->param('page');
$limit = Config::get('paginate.index_rows');
$where['g.is_group'] = 1;
$data = $this->goodsModel->selectPageData($where,$page,$limit);
$this->success('请求成功', $data);
}
/**
* @ApiTitle (商品列表)
* @ApiSummary (商品列表)
* @ApiMethod (POST)
... ...
... ... @@ -2,6 +2,8 @@
namespace app\api\controller;
use app\api\model\TeamGroup;
use app\api\model\TeamGroupInfo;
use app\common\controller\Api;
use think\Config;
... ... @@ -94,4 +96,94 @@ class GroupGoods extends Api
$this->success('请求成功', $data);
}
/**
* @ApiTitle (拼团列表)
* @ApiSummary (拼团列表)
* @ApiMethod (POST)
* @ApiRoute (/api/group_goods/getTeamList)
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
* @ApiParams (name=limit, type=string, required=false, description="条数 不传查询全部")
* @ApiReturn({
"code": 1,
"msg": "请求成功",
"time": "1587983117",
"data":
"is_end": 1 .剩余库存量<成团人数 开团库存不足立即开团不可选
"list": [
{
"team_group_id": 2, 拼团id
"user_avatar": "", 用户头像
"user_nickname": "admin", 用户昵称
"grouptime": 1588066875, 有效时间
"group_num": 2, 拼团人数
"num": 1 还差几人拼团成功
}
]
})
*/
public function getTeamList()
{
$teamGroupModel = new TeamGroup();
$teamGroupInfoModel = new TeamGroupInfo();
$goods_id = $this->request->param('goods_id');
if (!$goods_id) $this->error('缺少参数 goods_id!');
$limit = $this->request->param('limit')?$this->request->param('limit'):'';
$goods = $this->goodsModel->where('id',$goods_id)->field('group_num,stock_num')->find();
if ($goods['group_num']>$goods['stock_num']) $data['is_end'] = 1;
$list = $teamGroupModel->selectData(['goods_id'=>$goods_id],$limit);
foreach ($list as $k=>$v){
$count = $teamGroupInfoModel->where('team_group_id',$v['team_group_id'])->count();
$list[$k]['num'] = $v['group_num'] - $count;
}
$data['list'] = $list;
$this->success('请求成功', $data);
}
/**
* @ApiTitle (拼团详情)
* @ApiSummary (拼团详情)
* @ApiMethod (POST)
* @ApiRoute (/api/group_goods/getTeamInfo)
* @ApiParams (name=team_group_id, type=string, required=true, description="拼团id")
* @ApiReturn({
"code": 1,
"msg": "请求成功",
"time": "1587984522",
"data": {
"total": 2, 总人数
"grouptime": 1588066875, 有效时间
"num": 1, 目前人数
"list": [
{
"id": 3,
"team_group_id": 2,
"user_avatar": "",
"user_nickname": "admin"
},
{
"user_avatar": "../1.jpg"
}
]
}
})
*/
public function getTeamInfo()
{
$teamGroupModel = new TeamGroup();
$teamGroupInfoModel = new TeamGroupInfo();
$team_group_id = $this->request->param('team_group_id');
if (!$team_group_id) $this->error('缺少参数 team_group_id!');
$team = $teamGroupModel->where('id',$team_group_id)->field('group_num,grouptime')->find();
$list = $teamGroupInfoModel->selectData(['team_group_id'=>$team_group_id]);
$newList = [];
for ($i=0;$i<$team['group_num'];$i++){
if (!empty($list[$i])) $newList[$i] = $list[$i];
else $newList[$i]['user_avatar'] = '../1.jpg'; //默认头像
}
$data['total'] = $team['group_num'];
$data['grouptime'] = $team['grouptime'];
$data['num'] = count($list);
$data['list'] = $newList;
$this->success('请求成功', $data);
}
}
... ...
... ... @@ -104,7 +104,7 @@ class Order extends Api
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
//判断用户收货区域是否有库存
$areaStock = $goodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
if (!$areaStock) $data['is_lack_stock'] = 1;
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
}
}
... ... @@ -118,12 +118,12 @@ class Order extends Api
if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
//判断用户收货区域是否有库存
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
if (!$areaStock) $data['is_lack_stock'] = 1;
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
}
}
//判断优惠码是否可用
if (!empty($ticketCode)) {
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->field('user_id,validtime')->find();
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->find();
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
... ... @@ -268,7 +268,7 @@ class Order extends Api
if (!$validate->check($param)) {
$this->error($validate->getError());
}
if ($param['pay_type'] == 3 || $param['pay_type'] == 4) $this->error('员工或代理不能使用余额支付!');
if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
//判断优惠券和优惠码不能同时使用
if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
//获取普通商品id
... ... @@ -383,11 +383,11 @@ class Order extends Api
'pay_total' => $data['price'],
'discount_price' => $data['discount_price'],
'score' => $data['integral'],
'present_id' => !empty($data['present_id']) ? $data['present_id'] : '',
'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
'postage_total' => $postage,
'postage_type' => $param['postage_type'],
'postage_date' => $param['postage_date'],
'postage_time' => $param['postage_time'],
'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
... ... @@ -472,6 +472,384 @@ class Order extends Api
}
}
/**
* @ApiTitle (确认订单(积分商品))
* @ApiSummary (确认订单(积分商品))
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
* @ApiParams (name=province_id, type=string, required=true, description="省id")
* @ApiParams (name=city_id, type=string, required=true, description="市")
* @ApiParams (name=county_id, type=string, required=true, description="县")
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
* @ApiRoute (/api/order/orderIntegral)
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1587691008",
"data": {
"price": 263.7, 总价
"discount_price": 12.3, 折扣价
"original_price": 246, 原价
"ticketcode_discount_price": 5, 优惠码优惠价
"ticket_discount_price": 10, 优惠券优惠价
"integral": 0, 积分
"postage": "30.00" 运费
}
},
})
*/
public function orderIntegral()
{
$carModel = new \app\api\model\Car();
$goodsModel = new \app\api\model\Goods();
$ticketCodeModel = new \app\api\model\Ticketcode();
$userTicketModel = new \app\api\model\UserTicket();
$userId = $this->getUserId();
$goodsId = $this->request->param('goods_id');
$ticketId = $this->request->param('ticket_user_id');
$ticketCode = $this->request->param('ticket_code');
$provinceId = $this->request->param('province_id');
$cityId = $this->request->param('city_id');
$countyId = $this->request->param('county_id');
$postageType = $this->request->param('postage_type');
if (empty($goodsId)) $this->error('缺少参数 goods_id!');
if (empty($provinceId)) $this->error('缺少参数 province_id!');
if (empty($cityId)) $this->error('缺少参数 city_id!');
if (empty($countyId)) $this->error('缺少参数 county_id!');
if (empty($postageType)) $this->error('缺少参数 postage_type!');
//判断优惠券和优惠码不能同时使用
if (!empty($ticketCode) && !empty($ticketId)) $this->error('优惠券与优惠码不能同时使用!');
//获取积分商品状态
$goodsInfo = $this->integralGoodsModel->where(['id' => $goodsId])->field('id,status,stock_num,goods_price,integral')->find();
//判断商品是否有库存和状态
if ($goodsInfo['status'] == 2) $this->error($goodsInfo['ch_name'] . '商品已下架');
if ($goodsInfo['stock_num'] == 0) $this->error($goodsInfo['ch_name'] . '商品库存不足');
//判断用户收货区域是否有库存
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $goodsInfo['id'], 'd.area_id' => $provinceId]);
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
//判断优惠码是否可用
if (!empty($ticketCode)) {
$ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->find();
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
}
//判断优惠券是否可用
if (!empty($ticketId)) {
$ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $ticketId, 'status' => 1])->find();
if (!$ticketInfo) $this->error('优惠码不存在!');
}
//获取原价,现价,折扣
$goodsInfo['user_type'] = $this->user['type'];
$data['price'] = round(get_price($goodsInfo), 2); //折扣后价格
$data['discount_price'] = round(get_discount_price($goodsInfo), 2);//折扣金额
$data['original_price'] = $goodsInfo['goods_price'];//原价
$data['integral'] = $goodsInfo['integral']; //积分
//判断积分
if ($data['integral'] > $this->user['score']) $this->error('积分不足');
//获取运费
$is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
//特殊地区满200免运费
if ($is_special == 1 && $data['price'] >= 200) {
$postage = 0;
//其他地区满300免运费
} elseif ($data['price'] >= 300) {
$postage = 0;
} else {
//都不满足获取对应的运费
$postageWhere = ['province_id' => $provinceId, 'city_id' => $cityId, 'county_id' => $countyId];
if ($postageType == 2) {
//配送为闪送
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
if (!$postage) {
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage2');
}
} else {
//配送为普通配送
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
if (!$postage) {
$postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage1');
}
}
}
$data['postage'] = $postage;
$data['price'] += $postage;
//使用优惠码
if (!empty($ticketCodeInfo)) {
$data['price'] = $data['price'] - $ticketCodeInfo['price'];
$data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
}
//使用优惠券
if (!empty($ticketInfo)) {
$data['price'] = $data['price'] - $ticketInfo['price'];
$data['ticket_discount_price'] = $ticketInfo['price'];
}
$this->success('success', $data);
}
/**
* @ApiTitle (创建订单(积分商品))
* @ApiSummary (创建订单(积分商品))
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
* @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
* @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
* @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
* @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
* @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
* @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
* @ApiParams (name=address, type=string, required=true, description="商品收货地址")
* @ApiParams (name=name, type=string, required=true, description="商品收货人")
* @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
* @ApiParams (name=present_id, type=string, required=false, description="赠品id")
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
* @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
* @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
* @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
* @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
* @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
* @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
* @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
* @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
* @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
* @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
* @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
* @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
* @ApiParams (name=order_note, type=string, required=false, description="订单备注")
* @ApiRoute (/api/order/createOrderIntegral)
* @ApiReturn({
},
})
*/
public function createOrderIntegral()
{
$ticketCodeModel = new \app\api\model\Ticketcode();
$userTicketModel = new \app\api\model\UserTicket();
$userId = $this->getUserId();
$param = $this->request->param();
$param['user_id'] = $userId;
$validate = new Validate([
'goods_id' => 'require',
'pay_type' => 'require',
'name' => 'require',
'mobile' => 'require',
'address' => 'require',
'province_id' => 'require',
'city_id' => 'require',
'county_id' => 'require',
'postage_type' => 'require',
'invoice_status' => 'require',
]);
$validate->message([
'goods_id' => '缺少参数 goods_id!',
'pay_type' => '缺少参数 pay_type!',
'name' => '缺少参数 name!',
'mobile' => '缺少参数 mobile!',
'address' => '缺少参数 address!',
'province_id' => '缺少参数 province_id!',
'city_id' => '缺少参数 city_id!',
'county_id' => '缺少参数 county_id!',
'postage_type' => '缺少参数 postage_type!',
'invoice_status' => '缺少参数 invoice_status!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
//判断优惠券和优惠码不能同时使用
if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
//获取积分商品状态
$goodsInfo = $this->integralGoodsModel->where(['id' => $param['goods_id']])->field('id,ch_name,en_name,image,status,stock_num,goods_price,integral')->find();
//判断商品是否有库存和状态
if ($goodsInfo['status'] == 2) $this->error($goodsInfo['ch_name'] . '商品已下架');
if ($goodsInfo['stock_num'] == 0) $this->error($goodsInfo['ch_name'] . '商品库存不足');
//判断用户收货区域是否有库存
$areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $goodsInfo['id'], 'd.area_id' => $param['province_id']]);
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
//判断优惠码是否可用
if (!empty($param['ticket_code'])) {
$ticketCodeInfo = $ticketCodeModel->where('code', $param['ticket_code'])->find();
if (!$ticketCodeInfo) $this->error('优惠码不存在!');
if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
}
//判断优惠券是否可用
if (!empty($param['ticket_user_id'])) {
$ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $param['ticket_user_id'], 'status' => 1])->find();
if (!$ticketInfo) $this->error('优惠码不存在!');
}
//获取原价,现价,折扣
$goodsInfo['user_type'] = $this->user['type'];
$data['price'] = round(get_price($goodsInfo), 2); //折扣后价格
$data['discount_price'] = round(get_discount_price($goodsInfo), 2);//折扣金额
$data['original_price'] = $goodsInfo['goods_price'];//原价
$data['integral'] = $goodsInfo['integral']; //积分
//判断积分
if ($data['integral'] > $this->user['score']) $this->error('积分不足');
//获取运费
$is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
//特殊地区满200免运费
if ($is_special == 1 && $data['price'] >= 200) {
$postage = 0;
//其他地区满300免运费
} elseif ($data['price'] >= 300) {
$postage = 0;
} else {
//都不满足获取对应的运费
$postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
if ($param['postage_type'] == 2) {
//配送为闪送
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
if (!$postage) {
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
}
} else {
//配送为普通配送
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
if (!$postage) {
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
}
}
}
$data['postage'] = $postage;
$data['price'] += $postage;
Db::startTrans();
try {
//使用优惠码
if (!empty($ticketCodeInfo)) {
$data['price'] = $data['price'] - $ticketCodeInfo['price'];
$data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
$data['ticketcode_id'] = $ticketCodeInfo['id'];
$this->ticketCodeModel->where('id', $ticketCodeInfo['id'])->update(['user_id' => $userId]);
}
//使用优惠券
if (!empty($ticketInfo)) {
$data['price'] = $data['price'] - $ticketInfo['price'];
$data['ticket_discount_price'] = $ticketInfo['price'];
$data['user_ticket_id'] = $ticketInfo['id'];
$this->userTicketModel->where('id', $ticketInfo['id'])->update(['status' => 2]);
}
//order表数据
$orderData = [
'order_num' => get_order_num(),
'user_id' => $userId,
'pay_type' => $param['pay_type'],
'goods_total' => $data['original_price'],
'pay_total' => $data['price'],
'discount_price' => $data['discount_price'],
'score' => $data['integral'],
'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
'postage_total' => $postage,
'postage_type' => $param['postage_type'],
'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
'invoice_status' => $param['invoice_status'],
'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
'type' => 2,
'status' => 1,
'createtime' => time(),
'updatetime' => time(),
];
$res1 = $this->orderModel->insertGetId($orderData);
//收货地址数据
$orderAddress = [
'order_id' => $res1,
'name' => $param['name'],
'mobile' => $param['mobile'],
'address' => $param['address'],
'province_id' => $param['province_id'],
'city_id' => $param['city_id'],
'county_id' => $param['county_id'],
'createtime' => time(),
'updatetime' => time(),
];
$res2 = $this->orderAddressModel->insertData($orderAddress);
if ($param['invoice_status'] != 3) {
//收货地址数据
$orderAddressInvoice = [
'order_id' => $res1,
'name' => $param['invoice_name'],
'mobile' => $param['invoice_mobile'],
'address' => $param['invoice_address'],
'province_id' => $param['invoice_province_id'],
'city_id' => $param['invoice_city_id'],
'county_id' => $param['invoice_county_id'],
'createtime' => time(),
'updatetime' => time(),
];
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
}
//获取商品详情列表
$orderInfoData = [
'order_id' => $res1,
'pay_type' => $param['pay_type'],
'user_id' => $userId,
'goods_id' => $goodsInfo['id'],
'ch_goods_name' => $goodsInfo['ch_name'],
'en_goods_name' => $goodsInfo['en_name'],
'goods_image' => $goodsInfo['image'],
'number' => 1,
'type' => 2, //积分商品
'score' => !empty($data['integral']) ? $data['integral'] : 0,
'goods_total' => get_price($goodsInfo),
'pay_total' => $data['price'],
'postage_total' => $postage,
];
$res4 = $this->orderInfoModel->save($orderInfoData);
if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
Db::commit();
return true;
} else {
return false;
}
} catch (Exception $e) {
Db::rollback();
return false;
}
}
/**
* @ApiTitle (确认订单(团购商品))
... ... @@ -479,7 +857,6 @@ class Order extends Api
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
* @ApiParams (name=goods_id, type=string, required=true, description="商品id")
* @ApiParams (name=number, type=string, required=true, description="商品数量")
* @ApiParams (name=province_id, type=string, required=true, description="省id")
* @ApiParams (name=city_id, type=string, required=true, description="市")
* @ApiParams (name=county_id, type=string, required=true, description="县")
... ... @@ -502,43 +879,36 @@ class Order extends Api
*/
public function orderTeam()
{
$goodsModel = new \app\api\model\Goods();
$userId = $this->getUserId();
$number = $this->request->param('number');
$goodsId = $this->request->param('goods_id');
$provinceId = $this->request->param('province_id');
$cityId = $this->request->param('city_id');
$countyId = $this->request->param('county_id');
$postageType = $this->request->param('postage_type');
if (empty($goodsId)) $this->error('缺少参数 goods_id!');
if (empty($number)) $this->error('缺少参数 number!');
if (empty($provinceId)) $this->error('缺少参数 province_id!');
if (empty($cityId)) $this->error('缺少参数 city_id!');
if (empty($countyId)) $this->error('缺少参数 county_id!');
if (empty($postageType)) $this->error('缺少参数 postage_type!');
$goodsInfo = $goodsModel->where(['id' => $goodsId])->field('id,status,stock_num')->find();
$goodsInfo = $this->goodsModel->where(['id' => $goodsId])->field('id,status,stock_num,is_group,group_num,group_price,grouptime,goods_price')->find();
//判断商品是否有库存和状态
if ($goodsInfo['status'] == 2) $this->error('商品已下架');
if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
if ($goodsInfo['is_group'] != 1) $this->error('该商品不是团购商品');
//判断用户收货区域是否有库存
$areaStock = $goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);
if (!$areaStock) $data['is_lack_stock'] = 1;
$areaStock = $this->goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
//获取原价,现价,折扣
$priceArr = $this->carModel->getCarMoney(['id' => ['in', $carId]]);
$data['price'] = 0; //折扣后价格
$data['discount_price'] = 0; //折扣金额
$data['original_price'] = 0; //原价
$data['integral'] = 0; //积分
foreach ($priceArr as $k => $v) {
$priceArr[$k]['user_type'] = $this->user['type'];
$data['price'] += round(get_price($v), 2);
$data['discount_price'] += round(get_discount_price($v), 2);
$data['original_price'] += $v['goods_price'];
if (!empty($v['integral'])) $data['integral'] += $v['integral'];
$goodsInfo['user_type'] = $this->user['type'];
$data['price'] += round(get_price($goodsInfo), 2);
$data['discount_price'] += round(get_discount_price($goodsInfo), 2);
$data['original_price'] += $goodsInfo['group_price'];
}
//获取运费
$is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
//特殊地区满200免运费
... ... @@ -572,6 +942,229 @@ class Order extends Api
}
/**
* @ApiTitle (创建订单(团购商品))
* @ApiSummary (创建订单(团购商品))
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true description="请求的Token")
* @ApiParams (name=goods_id, type=string, required=false, description="商品id")
* @ApiParams (name=team_group_id, type=string, required=false, description="拼团id")
* @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
* @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
* @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
* @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
* @ApiParams (name=address, type=string, required=true, description="商品收货地址")
* @ApiParams (name=name, type=string, required=true, description="商品收货人")
* @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
* @ApiParams (name=present_id, type=string, required=false, description="赠品id")
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
* @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
* @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
* @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
* @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
* @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
* @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
* @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
* @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
* @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
* @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
* @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
* @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
* @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
* @ApiParams (name=order_note, type=string, required=false, description="订单备注")
* @ApiRoute (/api/order/createOrderTeam)
* @ApiReturn({
},
})
*/
public function createOrderTeam()
{
$userId = $this->getUserId();
$param = $this->request->param();
$param['user_id'] = $userId;
$validate = new Validate([
'goods_id' => 'require',
'pay_type' => 'require',
'name' => 'require',
'mobile' => 'require',
'address' => 'require',
'province_id' => 'require',
'city_id' => 'require',
'county_id' => 'require',
'postage_type' => 'require',
'invoice_status' => 'require',
]);
$validate->message([
'goods_id' => '缺少参数 goods_id!',
'pay_type' => '缺少参数 pay_type!',
'name' => '缺少参数 name!',
'mobile' => '缺少参数 mobile!',
'address' => '缺少参数 address!',
'province_id' => '缺少参数 province_id!',
'city_id' => '缺少参数 city_id!',
'county_id' => '缺少参数 county_id!',
'postage_type' => '缺少参数 postage_type!',
'invoice_status' => '缺少参数 invoice_status!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
$goodsInfo = $this->goodsModel->where(['id' => $param['goods_id']])->field('id,status,stock_num,is_group,group_num,group_price,grouptime,goods_price,ch_name,en_name,image')->find();
//判断商品是否有库存和状态
if ($goodsInfo['status'] == 2) $this->error('商品已下架');
if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
if ($goodsInfo['is_group'] != 1) $this->error('该商品不是团购商品');
//判断用户收货区域是否有库存
$areaStock = $this->goodsModel->getAreaStockNum(['g.id' => $param['goods_id'], 'd.area_id' => $param['province_id']]);
if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
//判断团购是否满人
if (!empty($param['team_group_id'])) {
$teamInfo = Db::name('team_group')->where('id', $param['team_group_id'])->field('group_num,grouptime')->find();
$count = Db::name('team_group_info')->where('team_group_id', $param['team_group_id'])->count();
$is_join_team = Db::name('team_group_info')->where(['team_group_id' => $param['team_group_id'], 'user_id' => $userId])->count();
if (!$teamInfo) $this->error('没有此拼团信息');
if ($teamInfo['grouptime'] <= time()) $this->error('拼团已失效');
if ($teamInfo['group_num'] <= $count) $this->error('参团人数已满');
if ($is_join_team) $this->error('您已参团了');
}
//获取原价,现价,折扣
$data['price'] = 0; //折扣后价格
$data['discount_price'] = 0; //折扣金额
$data['original_price'] = 0; //原价
$goodsInfo['user_type'] = $this->user['type'];
$data['price'] += round(get_price($goodsInfo), 2);
$data['discount_price'] += round(get_discount_price($goodsInfo), 2);
$data['original_price'] += $goodsInfo['group_price'];
//获取运费
$is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
//特殊地区满200免运费
if ($is_special == 1 && $data['price'] >= 200) {
$postage = 0;
//其他地区满300免运费
} elseif ($data['price'] >= 300) {
$postage = 0;
} else {
//都不满足获取对应的运费
$postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
if ($param['postage_type'] == 2) {
//配送为闪送
$postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
if (!$postage) {
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
}
} else {
//配送为普通配送
$postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
if (!$postage) {
$postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
}
}
}
$data['postage'] = $postage;
$data['price'] += $postage;
Db::startTrans();
try {
//order表数据
$orderData = [
'order_num' => get_order_num(),
'user_id' => $userId,
'pay_type' => $param['pay_type'],
'goods_total' => $data['original_price'],
'pay_total' => $data['price'],
'discount_price' => $data['discount_price'],
'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
'team_group_id' => !empty($param['team_group_id']) ? $param['team_group_id'] : '',
'team_group_status' => 1,
'postage_total' => $postage,
'postage_type' => $param['postage_type'],
'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
'invoice_status' => $param['invoice_status'],
'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
'type' => 3,
'status' => 1,
'createtime' => time(),
'updatetime' => time(),
];
$res1 = $this->orderModel->insertGetId($orderData);
//收货地址数据
$orderAddress = [
'order_id' => $res1,
'name' => $param['name'],
'mobile' => $param['mobile'],
'address' => $param['address'],
'province_id' => $param['province_id'],
'city_id' => $param['city_id'],
'county_id' => $param['county_id'],
'createtime' => time(),
'updatetime' => time(),
];
$res2 = $this->orderAddressModel->insertData($orderAddress);
if ($param['invoice_status'] != 3) {
//收货地址数据
$orderAddressInvoice = [
'order_id' => $res1,
'name' => $param['invoice_name'],
'mobile' => $param['invoice_mobile'],
'address' => $param['invoice_address'],
'province_id' => $param['invoice_province_id'],
'city_id' => $param['invoice_city_id'],
'county_id' => $param['invoice_county_id'],
'createtime' => time(),
'updatetime' => time(),
];
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
}
//获取商品详情
$orderInfoData = [
'order_id' => $res1,
'pay_type' => $param['pay_type'],
'user_id' => $userId,
'goods_id' => $param['goods_id'],
'ch_goods_name' => $goodsInfo['ch_name'],
'en_goods_name' => $goodsInfo['en_name'],
'goods_image' => $goodsInfo['image'],
'number' => 1, //团购商品购买数量只能是1
'type' => 3,
'goods_total' => $data['original_price'],
'pay_total' => $data['price'],
'postage_total' => $postage,
];
$res4 = $this->orderInfoModel->save($orderInfoData);
if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
Db::commit();
return true;
} else {
return false;
}
} catch (Exception $e) {
Db::rollback();
return false;
}
}
/**
* @ApiTitle (订单付款)
... ... @@ -601,6 +1194,7 @@ class Order extends Api
$this->error(__($validate->getError()));
}
$order = $this->orderModel->where(['id' => $param['order_id']])->find();
if (!$order) $this->error('订单不存在');
if ($order['status'] == 2) $this->error('订单已支付');
if ($order['status'] == -1) $this->error('订单已取消');
//判断积分
... ... @@ -618,6 +1212,13 @@ class Order extends Api
if (!$is_stock) $this->error($v['ch_goods_name'] . ' 库存不足');
}
}
//判断团购是否满人
if (!empty($order['team_group_id'])) {
$teamInfo = Db::name('team_group')->where('id', $order['team_group_id'])->field('group_num,grouptime')->find();
$count = Db::name('team_group_info')->where('team_group_id', $order['team_group_id'])->count();
if ($teamInfo['group_num'] <= $count) $this->error('参团人数已满');
if ($teamInfo['grouptime'] <= time()) $this->error('拼团已失效');
}
$amount = $order['pay_total'];
$payType = $order['pay_type'];
if ($payType == 2) {
... ... @@ -634,4 +1235,41 @@ class Order extends Api
//微信支付
}
}
/**
* @ApiTitle (取消订单)
* @ApiSummary (取消订单)
* @ApiMethod (POST)
* @ApiRoute (/api/order/orderCancel)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name=order_id, type=string, required=true, description="订单id")
* @ApiReturn ({
})
*/
public function orderCancel()
{
$userId = $this->getUserId();
$param = $this->request->param();
$rule = [
'order_id' => 'require',
];
$msg = [
'order_id' => '缺少参数 order_id!',
];
$validate = new Validate($rule, $msg);
$result = $validate->check($param);
if (!$result) {
$this->error(__($validate->getError()));
}
$order = $this->orderModel->where(['id' => $param['order_id']])->find();
if (!$order) $this->error('订单不存在');
if ($order['status'] == 2) $this->error('订单已支付');
if ($order['status'] == -1) $this->error('订单已取消');
$res = $this->orderModel->orderCancel($order);
if ($res) $this->success('取消订单成功');
else $this->error('取消订单失败');
}
}
... ...
... ... @@ -209,12 +209,15 @@ class Goods extends Model
$residue_number = $number - $province_stock_num;//还需多少库存
if($residue_number > 0){
//需要其它仓库提供库存
$arr1['depots_id'] = $province_depot['id'];
$arr1['area_id'] = $province_depot['area_id'];
$arr1['goods_id'] = $province_depot['goods_id'];
$arr1['type'] = $province_depot['type'];
$arr1['number'] = $province_stock_num;
if ($province_depot) $arr[] = $arr1;
if ($province_stock_num){
$arr1['depots_id'] = $province_depot['id'];
$arr1['area_id'] = $province_depot['area_id'];
$arr1['goods_id'] = $province_depot['goods_id'];
$arr1['type'] = $province_depot['type'];
$arr1['number'] = $province_stock_num;
if ($province_depot) $arr[] = $arr1;
}
$depots = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
... ...
... ... @@ -30,15 +30,18 @@ class Order extends Model
$orderInfoModel = new \app\api\model\OrderInfo();
Db::startTrans();
try {
$areaId = Db::name('order_address')->where('order_id', $order_id)->value('province_id');
$oldmoney = Db::name('user')->where('id', $user_id)->value('money');
$oldscore = Db::name('user')->where('id', $user_id)->value('score');
$integral_total = 0; //购买商品所需积分
//获取订单
$order = $this->where('id', $order_id)->field('id,pay_total,postage_total,ticket_price,ticketcode_price,type,team_group_id,team_group_status')->find();
//减去用户余额
Db::name('user')->where('id', $user_id)->setDec('money', $money);
foreach ($goodsList as $key => $goods) {
$integral_total += $goods['score']; //累计积分总数
if ($goods['type'] == 3) $goods['type'] = '1';
//根据仓库进行拆单
$data[] = $goodsModel->getDepot($goods['goods_id'], $areaId, $goods['number'], $goods['type']);
if (!$data[$key]) return ['code' => -1, 'msg' => $goods['ch_goods_name'] . ' 库存不足'];
... ... @@ -60,12 +63,13 @@ class Order extends Model
//添加订单子表
$detailId = $orderDetailModel->insertGetId($detailData);
$oldOrderInfoIds = []; //获取创建订单时 order_info_id
foreach ($goodsDepot as $key => $goodsDepotInfo) {
$where = [
'order_id' => $order_id,
'user_id' => $user_id,
'goods_id' => $goodsDepotInfo['goods_id'],
'type' => $goodsDepotInfo['type'],
'type' => $order['type'] == 3 ? 3 : $goodsDepotInfo['type'],
];
$orderInfo = $orderInfoModel->where($where)->find();
$oldOrderInfoIds[] = $orderInfo['id'];
... ... @@ -101,7 +105,7 @@ class Order extends Model
//添加订单详情
$orderInfoModel->saveAll($orderInfoData);
//删除之前订单详情
$orderInfoModel->where(['id'=>['in',$oldOrderInfoIds]])->delete();
$orderInfoModel->where(['id' => ['in', $oldOrderInfoIds]])->delete();
//有积分商品减积分
if ($integral_total != 0) {
... ... @@ -110,6 +114,7 @@ class Order extends Model
//将订单状态修改为已支付
$this->where('id', $order_id)->update(['status' => 2, 'paytime' => time()]);
//积分变动表记录
$oldscore = Db::name('user')->where('id', $user_id)->value('score');
$scoredata = [
'user_id' => $user_id,
'type' => 1, //购物返积分
... ... @@ -134,16 +139,16 @@ class Order extends Model
$user_pid = Db::name('user')->where('id', $user_id)->value('pid');
//判断是否有上级进行返利
if ($user_pid) {
$order = $this->where('id',$order_id)->field('pay_total,postage_total,ticket_price,ticketcode_price')->find();
//判断是否有上级进行返利 团购订单不在这里返利
if ($user_pid && $order['type'] != 3) {
if (!empty($order['ticket_price'])) $order['pay_total'] += $order['ticket_price'];
if (!empty($order['ticketcode_price'])) $order['pay_total'] += $order['ticketcode_price'];
if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
$parentInfo = Db::name('user')->where('id', $user_pid)->find();
//普通用户无返利
if ($parentInfo['type'] != 1){
$rebateMoney = get_rebate($order['pay_total'],$parentInfo['type']);
if ($parentInfo['type'] != 1) {
$rebateMoney = get_rebate($order['pay_total'], $parentInfo['type']);
$rebatedata = [
'user_id' => $user_pid,
'type' => 4, //佣金收入(返利)
... ... @@ -153,9 +158,155 @@ class Order extends Model
'createtime' => time(),
];
Db::name('user_money_log')->insert($rebatedata);
Db::name('user')->where('id',$user_pid)->setInc('money',$rebateMoney);
Db::name('user')->where('id', $user_pid)->setInc('money', $rebateMoney);
}
}
if ($order['type'] == 3 && !empty($order['team_group_id'])) {
//参团
$this->joinTeam($order, $user_id);
} elseif ($order['type'] == 3) {
//开团
$this->createTeam($order, $user_id);
}
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
return false;
}
}
public function createTeam($order, $user_id)
{
$goods_id = Db::name('order_info')->where(['order_id' => $order['id']])->value('goods_id');
$goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_agency_rebate,group_num,grouptime')->find();
$user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();
if (!empty($user_info['pid'])) { //返利
if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
$parent_info = Db::name('user')->where('id', $user_info['pid'])->find();
//普通用户无返利
if ($parent_info['type'] == 2) {
//会员返利
$rebateMoney = $order['pay_total'] * $goods_info['group_vip_rebate'];
} elseif ($parent_info['type'] == 3) {
//员工返利
$rebateMoney = $order['pay_total'] * $goods_info['group_staff_rebate'];
} elseif ($parent_info['type'] == 4) {
//代理返利
$rebateMoney = $order['pay_total'] * $goods_info['group_agency_rebate'];
}
$rebatedata = [
'user_id' => $user_info['pid'],
'type' => 4, //佣金收入(返利)
'money' => $rebateMoney,
'before' => $parent_info['money'],
'after' => $parent_info['money'] + $rebateMoney,
'createtime' => time(),
];
Db::name('user_money_log')->insert($rebatedata);
Db::name('user')->where('id', $parent_info['id'])->setInc('money', $rebateMoney);
}
//拼团表
$teamData = [
'goods_id' => $goods_id,
'user_id' => $user_id,
'user_avatar' => $user_info['avatar'],
'user_nickname' => $user_info['nickname'],
'grouptime' => strtotime('+' . $goods_info['grouptime'] . 'hour'),
'group_num' => $goods_info['group_num'],
'status' => 1,
'createtime' => time(),
'updatetime' => time(),
];
$teamId = Db::name('team_group')->insertGetId($teamData);
//给订单添加拼团id
$this->where('id',$order['id'])->update(['team_group_id'=>$teamId,'team_group_status'=>1]);
//拼团详情表
$teamInfoData = [
'team_group_id' => $teamId,
'order_id' => $order['id'],
'user_id' => $user_id,
'user_avatar' => $user_info['avatar'],
'user_nickname' => $user_info['nickname'],
'is_head' => 1,
'createtime' => time(),
'updatetime' => time(),
];
Db::name('team_group_info')->insertGetId($teamInfoData);
}
public function joinTeam($order, $user_id)
{
$user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();
$goods_id = Db::name('team_group')->where(['id' => $order['team_group_id']])->value('goods_id');
$goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_agency_rebate,group_price')->find();
if (!empty($user_info['pid'])) { //返利
if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
$parent_info = Db::name('user')->where('id', $user_info['pid'])->find();
//普通用户无返利
if ($parent_info['type'] == 2) {
//会员返利
$rebateMoney = $order['pay_total'] * $goods_info['group_vip_rebate'];
} elseif ($parent_info['type'] == 3) {
//员工返利
$rebateMoney = $order['pay_total'] * $goods_info['group_staff_rebate'];
} elseif ($parent_info['type'] == 4) {
//代理返利
$rebateMoney = $order['pay_total'] * $goods_info['group_agency_rebate'];
}
$rebatedata = [
'user_id' => $user_info['pid'],
'type' => 4, //佣金收入(返利)
'money' => $rebateMoney,
'before' => $parent_info['money'],
'after' => $parent_info['money'] + $rebateMoney,
'createtime' => time(),
];
Db::name('user_money_log')->insert($rebatedata);
Db::name('user')->where('id', $parent_info['id'])->setInc('money', $rebateMoney);
}
//添加拼团详情
$teamInfoData = [
'team_group_id' => $order['team_group_id'],
'order_id' => $order['id'],
'user_id' => $user_id,
'user_avatar' => $user_info['avatar'],
'user_nickname' => $user_info['nickname'],
'is_head' => 2,
'createtime' => time(),
'updatetime' => time(),
];
Db::name('team_group_info')->insertGetId($teamInfoData);
$teamInfo = Db::name('team_group')->where('id', $order['team_group_id'])->field('group_num,grouptime')->find();
$count = Db::name('team_group_info')->where('team_group_id', $order['team_group_id'])->count();
//判断是否拼团成功
if ($teamInfo['group_num'] == $count) {
//拼团成功
$ids = Db::name('team_group_info')->where(['team_group_id'=>$order['team_group_id']])->column('user_id');
Db::name('team_group')->where('id', $order['team_group_id'])->update(['status'=>2]);
$this->where(['team_group_id' => $order['team_group_id'],'user_id'=>['in',$ids]])->update(['team_group_status' => 2]);
$this->where(['team_group_id' => $order['team_group_id'],'user_id'=>['not in',$ids]])->update(['team_group_status' => 3]);
}
}
public function orderCancel($order){
$userTicketModel = new UserTicket();
$ticketcodeModel = new Ticketcode();
Db::startTrans();
try {
$this->where('id',$order['id'])->update(['status'=>-1]);
if (!empty($order['user_ticket_id'])){
$userTicketModel->where('id',$order['user_ticket_id'])->update(['status'=>'1','updatetime'=>time()]);
}
if (!empty($order['ticketcode_id'])){
$ticketcodeModel->where('id',$order['ticketcode_id'])->update(['user_id'=>'','updatetime'=>time()]);
}
Db::commit();
return true;
... ...
<?php
namespace app\api\model;
use think\Model;
class TeamGroup extends Model
{
public function selectData($where, $limit)
{
$where['status'] = 1;
$data = $this
->where($where)
->limit($limit)
->order('createtime desc')
->field('id team_group_id,user_avatar,user_nickname,grouptime,group_num ')
->select();
return $data;
}
}
... ...
<?php
namespace app\api\model;
use think\Model;
class TeamGroupInfo extends Model
{
public function selectData($where)
{
$data = $this
->where($where)
->order('createtime ASC')
->field('id,team_group_id,user_avatar,user_nickname')
->select();
return $data;
}
}
... ...
此 diff 太大无法显示。