作者 耿培杰

接口开发

... ... @@ -2,44 +2,106 @@
/*
* 获取会员默认价格
*/
function get_vip_price_default($money){
return $money*0.95;
function get_vip_price_default($money)
{
return $money * 0.95;
}
/*
* 获取员工代理默认价格
* 获取员工默认价格
*/
function get_staff_price_default($money){
return $money*0.90;
function get_staff_price_default($money)
{
return $money * 0.90;
}
/*
* 获取代理默认价格
*/
function get_agency_price_default($money)
{
return $money * 0.85;
}
/*
* 获取会员返利
*/
function get_vip_rebate($money)
{
return floor(($money * 0.025)*10)/10;
}
/*
* 获取员工返利
*/
function get_staff_rebate($money)
{
return floor(($money * 0.02)*10)/10;
}
/*
* 获取代理返利
*/
function get_agency_rebate($money)
{
return floor(($money * 0.015)*10)/10;
}
/*
* 获取返利
*/
function get_rebate($money, $user_type)
{
switch ($user_type) {
case 2:
$rebate = get_vip_rebate($money);
break;
case 3:
$rebate = get_staff_rebate($money);
break;
case 4:
$rebate = get_agency_rebate($money);
break;
}
return $rebate;
}
/*
* 根据商品状态和用户身份获取最终价格
*/
function get_price($arr){
function get_price($arr)
{
//会员特价
if(!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['vip_price'];
//员工或代理与会员折扣不同
if($arr['user_type'] == 3 || $arr['user_type'] == 4){
return get_staff_price_default($arr['goods_price']);
}elseif ($arr['user_type'] == 2){
//会员基础价,九五折
if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['vip_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']);
} elseif ($arr['user_type'] == 2) {
//会员基础价
return get_vip_price_default($arr['goods_price']);
}
//原价
return $arr['goods_price'];
}
/*
* 根据商品状态和用户身份获取折扣价
*/
function get_discount_price($arr){
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($arr['user_type'] == 3 || $arr['user_type'] == 4) {
if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['goods_price'] - $arr['vip_price'];
//代理基础价折扣
if ($arr['user_type'] == 4) {
return $arr['goods_price'] - get_staff_price_default($arr['goods_price']);
}elseif ($arr['user_type'] == 2){
//会员基础价,九五折
} elseif ($arr['user_type'] == 3) {
//员工基础价折扣
return $arr['goods_price'] - get_vip_price_default($arr['goods_price']);
} elseif ($arr['user_type'] == 2) {
//会员基础价折扣
return $arr['goods_price'] - get_vip_price_default($arr['goods_price']);
}
//原价
... ... @@ -52,16 +114,17 @@ function get_discount_price($arr){
* @param $endDate
* @return array
*/
function period_date($startDate, $endDate){
function period_date($startDate, $endDate)
{
$startTime = strtotime($startDate);
$endTime = strtotime($endDate);
$arr = array();
$i = 0;
while ($startTime <= $endTime){
$arr[$i]['date'] = date('m-d', $startTime) . '/' . get_week(date('Y-m-d', $startTime));
while ($startTime <= $endTime) {
$arr[$i]['date'] = date('m-d', $startTime) . '/' . get_week(date('Y-m-d', $startTime));
$arr[$i]['week'] = date('w', $startTime);
$startTime = strtotime('+1 day', $startTime);
$i+=1;
$i += 1;
}
return $arr;
}
... ... @@ -71,9 +134,10 @@ function period_date($startDate, $endDate){
* @param $date string 2020-4-22
* @return string
*/
function get_week($date){
$weekArr=["周日","周一","周二","周三","周四","周五","周六"];
return $weekArr[date("w",strtotime($date))];
function get_week($date)
{
$weekArr = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
return $weekArr[date("w", strtotime($date))];
}
/**
... ... @@ -83,4 +147,21 @@ function get_week($date){
function get_order_num()
{
return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
function setArr($data)
{
$goodsDepotList = [];
foreach ($data as $k => $v) {
foreach ($v as $key => $item) {
$goodsDepotList[] = $item;
}
}
$new_arr = [];
foreach ($goodsDepotList as $k => $goodsDepot) {
$tmp = $goodsDepot['area_id'];
$new_arr[$tmp][$k] = $goodsDepot;
}
return $new_arr;
}
\ No newline at end of file
... ...
... ... @@ -153,6 +153,8 @@ class Car extends Api
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$is_group = Db::name('goods')->where(['id'=>$data['goods_id'],'is_group'=>1])->count();
if (!$is_group) $this->error('团购商品不可以加入购物车');
$last = $this->carModel->where(['user_id'=>$userId,'goods_id'=>$data['goods_id']])->count();
if ($last) $this->error('该商品已添加购物车');
$res = $this->carModel->save($data);
... ...
... ... @@ -87,6 +87,7 @@ class Order extends Api
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('优惠券与优惠码不能同时使用!');
//获取普通商品id
... ... @@ -267,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 (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
//获取普通商品id
... ... @@ -382,6 +383,7 @@ 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'] : '',
'postage_total' => $postage,
'postage_type' => $param['postage_type'],
'postage_date' => $param['postage_date'],
... ... @@ -429,15 +431,13 @@ class Order extends Api
'createtime' => time(),
'updatetime' => time(),
];
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddress);
$res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
}
$where = ['order_address_id' => $res2, 'id' => $res1];
if (!empty($res3)) $where['order_address_invoice_id'] = $res3;
$this->orderModel->update($where);
//获取商品详情列表
$list = $this->carModel->getCarMoney(['id' => ['in', $param['car_id']]]);
$orderInfoData = [];
$integral_total = 0;
foreach ($list as $k => $goodsItem) {
$goodsItem['user_type'] = $this->user['type'];
$orderInfoData[$k] = [
... ... @@ -450,25 +450,17 @@ class Order extends Api
'goods_image' => $goodsItem['image'],
'number' => $goodsItem['number'],
'type' => $goodsItem['type'],
'score' => !empty($goodsItem['integral']) ? $goodsItem['integral'] : '',
'score' => !empty($goodsItem['integral']) ? $goodsItem['integral'] : 0,
'goods_total' => get_price($goodsItem),
'pay_total' => $data['price'],
'postage_total' => $postage,
'status' => 1,
];
$integral_total += $orderInfoData[$k]['score'];
}
$res4 = $this->orderInfoModel->saveAll($orderInfoData);
//有积分商品减积分
if (!empty($goodsItem['integral'])) {
$userScoreLogModel = new UserScoreLog();
$res5 = $userScoreLogModel->setDecScore($userId, $goodsItem['integral']);
}
//减库存
if ($res1 && $res2 && $res4 && (empty($res3) || $res3) && (empty($res5) || $res5)) {
if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
Db::commit();
return true;
} else {
... ... @@ -480,6 +472,107 @@ 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=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="县")
* @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
* @ApiRoute (/api/order/orderTeam)
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1587691008",
"data": {
"price": 263.7, 总价
"discount_price": 12.3, 折扣价
"original_price": 246, 原价
"integral": 0, 积分
"postage": "30.00" 运费
"is_lack_stock": "1" 收货地址没货
}
},
})
*/
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();
//判断商品是否有库存和状态
if ($goodsInfo['status'] == 2) $this->error('商品已下架');
if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
//判断用户收货区域是否有库存
$areaStock = $goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);
if (!$areaStock) $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'];
}
//获取运费
$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;
$this->success('success', $data);
}
/**
* @ApiTitle (订单付款)
* @ApiSummary (订单付款)
... ... @@ -492,6 +585,7 @@ class Order extends Api
*/
public function orderPay()
{
$userId = $this->getUserId();
$param = $this->request->param();
$rule = [
'order_id' => 'require',
... ... @@ -506,15 +600,32 @@ class Order extends Api
if (!$result) {
$this->error(__($validate->getError()));
}
$order = $this->orderModel->where(['id' => $param['order_id']])->find();
if ($order['status'] == 2) $this->error('订单已支付');
if ($order['status'] == -1) $this->error('订单已取消');
//判断积分
if ($order['score'] > $this->user['score']) $this->error('积分不足');
//获取订单商品id,库存
$goodsList = $this->orderInfoModel->where('order_id', $order['id'])->field('goods_id,number,ch_goods_name,type,score')->select();
foreach ($goodsList as $k => $v) {
if ($v['type'] == 2) {
//判断积分商品是否有库存
$is_stock = $this->integralGoodsModel->checkStockNum($v['goods_id'], $v['number']);
if (!$is_stock) $this->error($v['ch_goods_name'] . ' 库存不足');
} else {
//判断商品是否有库存
$is_stock = $this->goodsModel->checkStockNum($v['goods_id'], $v['number']);
if (!$is_stock) $this->error($v['ch_goods_name'] . ' 库存不足');
}
}
$amount = $order['pay_total'];
$payType = $order['pay_type'];
if ($payType == 2) {
//余额支付
if ($this->user['money'] >= $amount) {
$res = $this->orderModel->balancePay($param['order_id'], $this->auth->id, $amount);
if ($res) $this->success('支付成功');
$res = $this->orderModel->balancePay($param['order_id'], $userId, $amount, $goodsList);
if ($res && empty($res['code'])) $this->success('支付成功');
elseif (!empty($res['code'])) $this->error($res['msg']);
else $this->error('支付失败');
} else {
$this->error('余额不足');
... ...
... ... @@ -173,6 +173,7 @@ class Goods extends Model
* @return mixed
*/
public function getAreaStockNum($where){
$where['type'] = '1';
$stockNum = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where($where)
... ... @@ -187,36 +188,43 @@ class Goods extends Model
* @param $areaId
* @return mixed
*/
public function getDepot($goodsId,$areaId,$number){
public function getDepot($goodsId,$areaId,$number,$type){
$arr = [];//仓库对应需扣除库存信息
//获取全部仓库库存
$sum_stock_num = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
->where(['g.id' => $goodsId,'d.type' => $type])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return '库存不足';
return false; //库存不足
}
//获取当前省仓库库存
$province_depot = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
->field('d.id,d.stock_num')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId, 'd.type' => $type])
->field('d.id,d.stock_num,d.area_id,d.type,g.id goods_id')->order('d.weigh desc')
->find();
$province_stock_num = !empty($province_depot['stock_num']) ? $province_depot['stock_num'] : 0;
$residue_number = $number - $province_stock_num;//还需多少库存
if($residue_number > 0){
//需要其它仓库提供库存
$arr1['area_id'] = $areaId;
$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;
$arr[] = $arr1;
if ($province_depot) $arr[] = $arr1;
$depots = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => ['not in'=>[$areaId]]])
->field('d.id,d.stock_num')->select();
->where(['g.id' => $goodsId, 'd.id' => ['not in',$province_depot['id']], 'd.type' => $type])
->field('d.id,d.stock_num,d.area_id,d.type,g.id goods_id')->order('d.weigh desc')->select();
foreach($depots as $key => $depot){
$arr1['depots_id'] = $depot['id'];
$arr1['area_id'] = $depot['area_id'];
$arr1['goods_id'] = $depot['goods_id'];
$arr1['type'] = $depot['type'];
if($depot['stock_num'] < $residue_number){
//还是不够
$arr1['number'] = $depot['stock_num'];
... ... @@ -226,39 +234,36 @@ class Goods extends Model
//足够
$arr1['number'] = $residue_number;
$arr[] = $arr1;
break;
}
}
}else{
$arr1['area_id'] = $areaId;
$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'] = $number;
$arr[] = $arr1;
}
return $arr;
/*$this->recursion($depot,'',$number,$goodsId);
if (!empty($depot) && $number == 0){
return $depot['id'];
}else{
$depotid = Db::name('depot')->where('goods_id',$goodsId)->order('weigh DESC')->value('id');
return $depotid;
}*/
}
public function recursion($depot,$ids,$number,$goodsId){
//判断仓库是否满足商品数量
if(!empty($depot) && $depot['stock_num'] < $number){
$number -= $depot['stock_num'];
$ids .= $depot['id'].',';
$newdepot = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['goods_id'=>$goodsId,'id'=>['not in',rtrim($ids,',')]])
->field('d.id,d.stock_num')
->order('weigh DESC')
->find();
$this->recursion($newdepot,$ids,$number,$goodsId);
}elseif (!empty($depot) && $depot['stock_num'] > $number){
/**
* 检查商品是否有库存
* @param $goodsId
* @param $number
* @return boolean
*/
public function checkStockNum($goodsId,$number){
$sum_stock_num = $this->alias('g')
->join('fa_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}else{
return true;
}
}
}
... ...
... ... @@ -70,8 +70,9 @@ class IntegralGoods extends Model
*/
public function getAreaStockNum($where)
{
$where['type'] = '2';
$stockNum = $this->alias('g')
->join('fa_integral_depot d', 'g.id=d.goods_id')
->join('fa_depot d', 'g.id=d.goods_id')
->where($where)
->field('d.stock_num,g.ch_name')
->find();
... ... @@ -84,19 +85,72 @@ class IntegralGoods extends Model
* @param $areaId
* @return mixed
*/
public function getDepot($goodsId, $areaId)
{
$depotid = $this->alias('g')
->join('fa_integral_depot d', 'g.id=d.goods_id')
public function getDepot($goodsId,$areaId,$number){
$arr = [];//仓库对应需扣除库存信息
//获取全部仓库库存
$sum_stock_num = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}
//获取当前省仓库库存
$province_depot = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.area_id' => $areaId])
->field('d.stock_num,g.ch_name')
->value('d.id');
if ($depotid) {
return $depotid;
} else {
$depotid = Db::name('depot')->where('goods_id', $goodsId)->order('weigh DESC')->value('id');
return $depotid;
->field('d.id,d.stock_num')->order('d.weigh desc')
->find();
$province_stock_num = !empty($province_depot['stock_num']) ? $province_depot['stock_num'] : 0;
$residue_number = $number - $province_stock_num;//还需多少库存
if($residue_number > 0){
//需要其它仓库提供库存
$arr1['depots_id'] = $province_depot['id'];
$arr1['number'] = $province_stock_num;
$arr[] = $arr1;
$depots = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId, 'd.id' => ['not in',$province_depot['id']]])
->field('d.id,d.stock_num')->order('d.weigh desc')->select();
foreach($depots as $key => $depot){
$arr1['depots_id'] = $depot['id'];
if($depot['stock_num'] < $residue_number){
//还是不够
$arr1['number'] = $depot['stock_num'];
$arr[] = $arr1;
$residue_number = $residue_number - $depot['stock_num'];
}else{
//足够
$arr1['number'] = $residue_number;
$arr[] = $arr1;
break;
}
}
}else{
$arr1['depots_id'] = $province_depot['id'];
$arr1['number'] = $number;
$arr[] = $arr1;
}
return $arr;
}
/**
* 检查商品是否有库存
* @param $goodsId
* @param $number
* @return boolean
*/
public function checkStockNum($goodsId,$number){
$sum_stock_num = $this->alias('g')
->join('fa_integral_depot d','g.id=d.goods_id')
->where(['g.id' => $goodsId])
->field('d.id,d.stock_num')
->sum('d.stock_num');
if($number > $sum_stock_num){
return false; //库存不足
}else{
return true;
}
}
}
... ...
... ... @@ -22,59 +22,143 @@ class Order extends Model
* @param $money 支付金额
* @return boolean
*/
public function balancePay($order_id, $user_id, $money)
public function balancePay($order_id, $user_id, $money, $goodsList)
{
$goodsModel = new \app\api\model\Goods();
$userScoreLogModel = new \app\api\model\UserScoreLog();
$orderDetailModel = new \app\api\model\OrderDetail();
$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; //购买商品所需积分
//减去用户余额
$res1 = Db::name('user')->where('id', $user_id)->setDec('money', $money);
Db::name('user')->where('id', $user_id)->setDec('money', $money);
foreach ($goodsList as $key => $goods) {
$integral_total += $goods['score']; //累计积分总数
//根据仓库进行拆单
$data[] = $goodsModel->getDepot($goods['goods_id'], $areaId, $goods['number'], $goods['type']);
if (!$data[$key]) return ['code' => -1, 'msg' => $goods['ch_goods_name'] . ' 库存不足'];
}
//获取以仓库分好的商品
$goodsDepotList = setArr($data);
$orderInfoData = [];
foreach ($goodsDepotList as $k => $goodsDepot) {
$detailArr = reset($goodsDepot);
$detailData = [
'order_id' => $order_id,
'user_id' => $user_id,
'pay_type' => 2, //余额支付
'depot_id' => $detailArr['depots_id'],
'status' => 2, //代发货
'createtime' => time(),
'updatetime' => time(),
];
//添加订单子表
$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'],
];
$orderInfo = $orderInfoModel->where($where)->find();
$oldOrderInfoIds[] = $orderInfo['id'];
$orderInfoData[] = [
'pay_type' => $orderInfo['pay_type'], //余额支付
'order_id' => $order_id,
'order_detail_id' => $detailId,
'user_id' => $user_id,
'goods_id' => $goodsDepotInfo['goods_id'],
'type' => $goodsDepotInfo['type'],
'ch_goods_name' => $orderInfo['ch_goods_name'],
'en_goods_name' => $orderInfo['en_goods_name'],
'goods_image' => $orderInfo['goods_image'],
'number' => $goodsDepotInfo['number'],
'goods_total' => round($orderInfo['goods_total'] / $orderInfo['number'] * $goodsDepotInfo['number'], 2),
'pay_total' => $orderInfo['pay_total'],
'postage_total' => $orderInfo['postage_total'],
'score' => $orderInfo['score'] / $orderInfo['number'] * $goodsDepotInfo['number'],
];
//减仓库库存
Db::name('depot')->where('id', $goodsDepotInfo['depots_id'])->setDec('stock_num', $goodsDepotInfo['number']);
//判断商品类型减去库存 加销量
if ($goodsDepotInfo['type'] == 2) {
Db::name('integral_goods')->where('id', $goodsDepotInfo['goods_id'])->setDec('stock_num', $goodsDepotInfo['number']);
Db::name('integral_goods')->where('id', $goodsDepotInfo['goods_id'])->setInc('sales_actual', $goodsDepotInfo['number']);
} else {
Db::name('goods')->where('id', $goodsDepotInfo['goods_id'])->setDec('stock_num', $goodsDepotInfo['number']);
Db::name('goods')->where('id', $goodsDepotInfo['goods_id'])->setInc('sales_actual', $goodsDepotInfo['number']);
}
}
}
//添加订单详情
$orderInfoModel->saveAll($orderInfoData);
//删除之前订单详情
$orderInfoModel->where(['id'=>['in',$oldOrderInfoIds]])->delete();
//有积分商品减积分
if ($integral_total != 0) {
$userScoreLogModel->setDecScore($user_id, $integral_total);
}
//将订单状态修改为已支付
$res2 = $this->where('id', $order_id)->update(['status' => 2,'paytime'=>time()]);
//将订单商品状态修改为待发货
$res3 = Db::name('order_info')->where('order_id',$order_id)->update(['status' => 2,'paytime'=>time()]);
$this->where('id', $order_id)->update(['status' => 2, 'paytime' => time()]);
//积分变动表记录
$scoredata = [
'user_id' => $user_id,
'type' => 1, //购物返积分
'score' => intval($money),
'before' => $oldscore,
'after' => $oldscore+intval($money),
'after' => $oldscore + intval($money),
'createtime' => time(),
];
$res4 = Db::name('user_score_log')->insert($scoredata);
Db::name('user_score_log')->insert($scoredata);
Db::name('user')->where('id', $user_id)->setInc('score', intval($money));
//余额支付变动记录
$data = [
'user_id' => $user_id,
'type' => 3, //购物消费
'money' => $money,
'before' => $oldmoney,
'after' => $oldmoney-$money,
'after' => $oldmoney - $money,
'createtime' => time(),
];
$res5 = Db::name('user_money_log')->insert($data);
//
$user_pid = Db::name('user')->where('id',$user_id)->value('pid');
Db::name('user_money_log')->insert($data);
$user_pid = Db::name('user')->where('id', $user_id)->value('pid');
//判断是否有上级进行返利
if ($user_pid){
$parentInfo = Db::name('user')->where('id',$user_pid)->find();
$rebatedata = [
'user_id' => $user_pid,
'type' => 4, //佣金收入(返利)
'money' => $money,
'before' => $parentInfo['money'],
'after' => $parentInfo['money']+$money,
'createtime' => time(),
];
$res5 = Db::name('user_money_log')->insert($data);
}
if ($res1 && $res2 && $res3 && $res4 && $res5) {
Db::commit();
return true;
}else{
return false;
if ($user_pid) {
$order = $this->where('id',$order_id)->field('pay_total,postage_total,ticket_price,ticketcode_price')->find();
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']);
$rebatedata = [
'user_id' => $user_pid,
'type' => 4, //佣金收入(返利)
'money' => $rebateMoney,
'before' => $parentInfo['money'],
'after' => $parentInfo['money'] + $rebateMoney,
'createtime' => time(),
];
Db::name('user_money_log')->insert($rebatedata);
Db::name('user')->where('id',$user_pid)->setInc('money',$rebateMoney);
}
}
Db::commit();
return true;
} catch (Exception $e) {
Db::rollback();
return false;
... ...
... ... @@ -20,9 +20,7 @@ class OrderAddress extends Model
$city = $areaModel->where('id',$data['city_id'])->field('name,pinyin')->find();
$county = $areaModel->where('id',$data['county_id'])->field('name,pinyin')->find();
unset($data['province_id']);
unset($data['city_id']);
unset($data['county_id']);
$data['ch_province_name'] = $province['name'];
$data['ch_city_name'] = $city['name'];
$data['ch_county_name'] = $county['name'];
... ...
<?php
namespace app\api\model;
use think\Model;
class OrderDetail extends Model
{
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
}
... ...
此 diff 太大无法显示。