作者 耿培杰

接口开发

@@ -73,12 +73,17 @@ function get_price($arr) @@ -73,12 +73,17 @@ function get_price($arr)
73 { 73 {
74 //会员特价 74 //会员特价
75 if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['vip_price']; 75 if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['vip_price'];
  76 + //团购价格
  77 + if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 2) return get_vip_price_default($arr['group_price']);
  78 + if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 3) return get_staff_price_default($arr['group_price']);
  79 + if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 4) return get_agency_price_default($arr['group_price']);
  80 +
76 //代理基础价 81 //代理基础价
77 if ($arr['user_type'] == 4) { 82 if ($arr['user_type'] == 4) {
78 return get_agency_price_default($arr['goods_price']); 83 return get_agency_price_default($arr['goods_price']);
79 } elseif ($arr['user_type'] == 3) { 84 } elseif ($arr['user_type'] == 3) {
80 //员工基础价 85 //员工基础价
81 - return get_vip_price_default($arr['goods_price']); 86 + return get_staff_price_default($arr['goods_price']);
82 } elseif ($arr['user_type'] == 2) { 87 } elseif ($arr['user_type'] == 2) {
83 //会员基础价 88 //会员基础价
84 return get_vip_price_default($arr['goods_price']); 89 return get_vip_price_default($arr['goods_price']);
@@ -94,12 +99,16 @@ function get_discount_price($arr) @@ -94,12 +99,16 @@ function get_discount_price($arr)
94 { 99 {
95 //会员特价 100 //会员特价
96 if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['goods_price'] - $arr['vip_price']; 101 if (!empty($arr['is_vip_price']) && $arr['is_vip_price'] == 1 && $arr['user_type'] == 2) return $arr['goods_price'] - $arr['vip_price'];
  102 + //团购价格
  103 + if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 2) return $arr['group_price'] - get_vip_price_default($arr['group_price']);
  104 + if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 3) return $arr['group_price'] - get_staff_price_default($arr['group_price']);
  105 + if (!empty($arr['is_group']) && $arr['is_group'] == 1 && $arr['user_type'] == 4) return $arr['group_price'] - get_agency_price_default($arr['group_price']);
97 //代理基础价折扣 106 //代理基础价折扣
98 if ($arr['user_type'] == 4) { 107 if ($arr['user_type'] == 4) {
99 - return $arr['goods_price'] - get_staff_price_default($arr['goods_price']); 108 + return $arr['goods_price'] - get_agency_price_default($arr['goods_price']);
100 } elseif ($arr['user_type'] == 3) { 109 } elseif ($arr['user_type'] == 3) {
101 //员工基础价折扣 110 //员工基础价折扣
102 - return $arr['goods_price'] - get_vip_price_default($arr['goods_price']); 111 + return $arr['goods_price'] - get_staff_price_default($arr['goods_price']);
103 } elseif ($arr['user_type'] == 2) { 112 } elseif ($arr['user_type'] == 2) {
104 //会员基础价折扣 113 //会员基础价折扣
105 return $arr['goods_price'] - get_vip_price_default($arr['goods_price']); 114 return $arr['goods_price'] - get_vip_price_default($arr['goods_price']);
@@ -86,77 +86,6 @@ class Goods extends Api @@ -86,77 +86,6 @@ class Goods extends Api
86 86
87 87
88 /** 88 /**
89 - * @ApiTitle (团购商品列表(首页))  
90 - * @ApiSummary (团购商品列表(首页))  
91 - * @ApiMethod (POST)  
92 - * @ApiRoute (/api/goods/getGroupGoodsIndex)  
93 - * @ApiReturn({  
94 - "code": 1,  
95 - "msg": "请求成功",  
96 - "time": "1587463117",  
97 - "data": [  
98 - {  
99 - "goods_id": 1, 商品id  
100 - "ch_name": "EMP精选 澳洲白肉油桃 500~540g 4只装", 中文名称  
101 - "en_name": "EMP selected Australian white meat nectarines Australian meat nectarines", 英文名称  
102 - "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图  
103 - "goods_price": "0.00", 普通售价  
104 - "group_price": "0.00", 拼团售价 拼团售价优先级最高  
105 - "vip_price": "0.00", 会员售价  
106 - "country_ch_name": "意大利", 原产地中文  
107 - "country_en_name": "Ltaly", 原产地英文  
108 - "is_vip_price": "2", 会员特价:1=开启,2=关闭  
109 - "stock_num": 55 库存  
110 - }  
111 - ]  
112 - })  
113 - */  
114 - public function getGroupGoodsIndex()  
115 - {  
116 - $where['g.is_group'] = 1;  
117 -  
118 - $data = $this->goodsModel->selectData($where, 5);  
119 -  
120 - $this->success('请求成功', $data);  
121 - }  
122 -  
123 - /**  
124 - * @ApiTitle (团购商品列表)  
125 - * @ApiSummary (团购商品列表)  
126 - * @ApiMethod (POST)  
127 - * @ApiRoute (/api/goods/getGroupGoodsList)  
128 - * @ApiParams (name=page, type=string, required=false, description="页数")  
129 - * @ApiReturn({  
130 - "code": 1,  
131 - "msg": "请求成功",  
132 - "time": "1587463117",  
133 - "data": [  
134 - {  
135 - "goods_id": 1, 商品id  
136 - "ch_name": "EMP精选 澳洲白肉油桃 500~540g 4只装", 中文名称  
137 - "en_name": "EMP selected Australian white meat nectarines Australian meat nectarines", 英文名称  
138 - "image": "http://q7s0a1rb4.bkt.clouddn.com/uploads/20200420/26f5e51b8ac7fbd6f1c649cc45a18265.png", 缩略图  
139 - "goods_price": "0.00", 普通售价  
140 - "group_price": "0.00", 拼团售价 拼团售价优先级最高  
141 - "vip_price": "0.00", 会员售价  
142 - "country_ch_name": "意大利", 原产地中文  
143 - "country_en_name": "Ltaly", 原产地英文  
144 - "is_vip_price": "2", 会员特价:1=开启,2=关闭  
145 - "stock_num": 55 库存  
146 - }  
147 - ]  
148 - })  
149 - */  
150 - public function getGroupGoodsList()  
151 - {  
152 - $page = $this->request->param('page');  
153 - $limit = Config::get('paginate.index_rows');  
154 - $where['g.is_group'] = 1;  
155 - $data = $this->goodsModel->selectPageData($where,$page,$limit);  
156 - $this->success('请求成功', $data);  
157 - }  
158 -  
159 - /**  
160 * @ApiTitle (商品列表) 89 * @ApiTitle (商品列表)
161 * @ApiSummary (商品列表) 90 * @ApiSummary (商品列表)
162 * @ApiMethod (POST) 91 * @ApiMethod (POST)
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 namespace app\api\controller; 3 namespace app\api\controller;
4 4
  5 +use app\api\model\TeamGroup;
  6 +use app\api\model\TeamGroupInfo;
5 use app\common\controller\Api; 7 use app\common\controller\Api;
6 use think\Config; 8 use think\Config;
7 9
@@ -94,4 +96,94 @@ class GroupGoods extends Api @@ -94,4 +96,94 @@ class GroupGoods extends Api
94 $this->success('请求成功', $data); 96 $this->success('请求成功', $data);
95 } 97 }
96 98
  99 + /**
  100 + * @ApiTitle (拼团列表)
  101 + * @ApiSummary (拼团列表)
  102 + * @ApiMethod (POST)
  103 + * @ApiRoute (/api/group_goods/getTeamList)
  104 + * @ApiParams (name=goods_id, type=string, required=true, description="商品id")
  105 + * @ApiParams (name=limit, type=string, required=false, description="条数 不传查询全部")
  106 + * @ApiReturn({
  107 + "code": 1,
  108 + "msg": "请求成功",
  109 + "time": "1587983117",
  110 + "data":
  111 + "is_end": 1 .剩余库存量<成团人数 开团库存不足立即开团不可选
  112 + "list": [
  113 + {
  114 + "team_group_id": 2, 拼团id
  115 + "user_avatar": "", 用户头像
  116 + "user_nickname": "admin", 用户昵称
  117 + "grouptime": 1588066875, 有效时间
  118 + "group_num": 2, 拼团人数
  119 + "num": 1 还差几人拼团成功
  120 + }
  121 + ]
  122 + })
  123 + */
  124 + public function getTeamList()
  125 + {
  126 + $teamGroupModel = new TeamGroup();
  127 + $teamGroupInfoModel = new TeamGroupInfo();
  128 + $goods_id = $this->request->param('goods_id');
  129 + if (!$goods_id) $this->error('缺少参数 goods_id!');
  130 + $limit = $this->request->param('limit')?$this->request->param('limit'):'';
  131 + $goods = $this->goodsModel->where('id',$goods_id)->field('group_num,stock_num')->find();
  132 + if ($goods['group_num']>$goods['stock_num']) $data['is_end'] = 1;
  133 + $list = $teamGroupModel->selectData(['goods_id'=>$goods_id],$limit);
  134 + foreach ($list as $k=>$v){
  135 + $count = $teamGroupInfoModel->where('team_group_id',$v['team_group_id'])->count();
  136 + $list[$k]['num'] = $v['group_num'] - $count;
  137 + }
  138 + $data['list'] = $list;
  139 + $this->success('请求成功', $data);
  140 + }
  141 +
  142 + /**
  143 + * @ApiTitle (拼团详情)
  144 + * @ApiSummary (拼团详情)
  145 + * @ApiMethod (POST)
  146 + * @ApiRoute (/api/group_goods/getTeamInfo)
  147 + * @ApiParams (name=team_group_id, type=string, required=true, description="拼团id")
  148 + * @ApiReturn({
  149 + "code": 1,
  150 + "msg": "请求成功",
  151 + "time": "1587984522",
  152 + "data": {
  153 + "total": 2, 总人数
  154 + "grouptime": 1588066875, 有效时间
  155 + "num": 1, 目前人数
  156 + "list": [
  157 + {
  158 + "id": 3,
  159 + "team_group_id": 2,
  160 + "user_avatar": "",
  161 + "user_nickname": "admin"
  162 + },
  163 + {
  164 + "user_avatar": "../1.jpg"
  165 + }
  166 + ]
  167 + }
  168 + })
  169 + */
  170 + public function getTeamInfo()
  171 + {
  172 + $teamGroupModel = new TeamGroup();
  173 + $teamGroupInfoModel = new TeamGroupInfo();
  174 + $team_group_id = $this->request->param('team_group_id');
  175 + if (!$team_group_id) $this->error('缺少参数 team_group_id!');
  176 + $team = $teamGroupModel->where('id',$team_group_id)->field('group_num,grouptime')->find();
  177 + $list = $teamGroupInfoModel->selectData(['team_group_id'=>$team_group_id]);
  178 + $newList = [];
  179 + for ($i=0;$i<$team['group_num'];$i++){
  180 + if (!empty($list[$i])) $newList[$i] = $list[$i];
  181 + else $newList[$i]['user_avatar'] = '../1.jpg'; //默认头像
  182 + }
  183 + $data['total'] = $team['group_num'];
  184 + $data['grouptime'] = $team['grouptime'];
  185 + $data['num'] = count($list);
  186 + $data['list'] = $newList;
  187 + $this->success('请求成功', $data);
  188 + }
97 } 189 }
@@ -104,7 +104,7 @@ class Order extends Api @@ -104,7 +104,7 @@ class Order extends Api
104 if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足'); 104 if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
105 //判断用户收货区域是否有库存 105 //判断用户收货区域是否有库存
106 $areaStock = $goodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]); 106 $areaStock = $goodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
107 - if (!$areaStock) $data['is_lack_stock'] = 1; 107 + if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
108 } 108 }
109 } 109 }
110 110
@@ -118,12 +118,12 @@ class Order extends Api @@ -118,12 +118,12 @@ class Order extends Api
118 if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足'); 118 if ($v['stock_num'] == 0) $this->error($v['ch_name'] . '商品库存不足');
119 //判断用户收货区域是否有库存 119 //判断用户收货区域是否有库存
120 $areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]); 120 $areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $v['id'], 'd.area_id' => $provinceId]);
121 - if (!$areaStock) $data['is_lack_stock'] = 1; 121 + if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
122 } 122 }
123 } 123 }
124 //判断优惠码是否可用 124 //判断优惠码是否可用
125 if (!empty($ticketCode)) { 125 if (!empty($ticketCode)) {
126 - $ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->field('user_id,validtime')->find(); 126 + $ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->find();
127 if (!$ticketCodeInfo) $this->error('优惠码不存在!'); 127 if (!$ticketCodeInfo) $this->error('优惠码不存在!');
128 if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!'); 128 if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
129 if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!'); 129 if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
@@ -268,7 +268,7 @@ class Order extends Api @@ -268,7 +268,7 @@ class Order extends Api
268 if (!$validate->check($param)) { 268 if (!$validate->check($param)) {
269 $this->error($validate->getError()); 269 $this->error($validate->getError());
270 } 270 }
271 - if ($param['pay_type'] == 3 || $param['pay_type'] == 4) $this->error('员工或代理不能使用余额支付!'); 271 + if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
272 //判断优惠券和优惠码不能同时使用 272 //判断优惠券和优惠码不能同时使用
273 if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!'); 273 if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
274 //获取普通商品id 274 //获取普通商品id
@@ -383,11 +383,11 @@ class Order extends Api @@ -383,11 +383,11 @@ class Order extends Api
383 'pay_total' => $data['price'], 383 'pay_total' => $data['price'],
384 'discount_price' => $data['discount_price'], 384 'discount_price' => $data['discount_price'],
385 'score' => $data['integral'], 385 'score' => $data['integral'],
386 - 'present_id' => !empty($data['present_id']) ? $data['present_id'] : '', 386 + 'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
387 'postage_total' => $postage, 387 'postage_total' => $postage,
388 'postage_type' => $param['postage_type'], 388 'postage_type' => $param['postage_type'],
389 - 'postage_date' => $param['postage_date'],  
390 - 'postage_time' => $param['postage_time'], 389 + 'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
  390 + 'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
391 'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '', 391 'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
392 'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '', 392 'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
393 'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '', 393 'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
@@ -472,6 +472,384 @@ class Order extends Api @@ -472,6 +472,384 @@ class Order extends Api
472 } 472 }
473 } 473 }
474 474
  475 + /**
  476 + * @ApiTitle (确认订单(积分商品))
  477 + * @ApiSummary (确认订单(积分商品))
  478 + * @ApiMethod (POST)
  479 + * @ApiHeaders (name=token, type=string, required=true description="请求的Token")
  480 + * @ApiParams (name=goods_id, type=string, required=true, description="商品id")
  481 + * @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
  482 + * @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
  483 + * @ApiParams (name=province_id, type=string, required=true, description="省id")
  484 + * @ApiParams (name=city_id, type=string, required=true, description="市")
  485 + * @ApiParams (name=county_id, type=string, required=true, description="县")
  486 + * @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
  487 + * @ApiRoute (/api/order/orderIntegral)
  488 + * @ApiReturn({
  489 + "code": 1,
  490 + "msg": "success",
  491 + "time": "1587691008",
  492 + "data": {
  493 + "price": 263.7, 总价
  494 + "discount_price": 12.3, 折扣价
  495 + "original_price": 246, 原价
  496 + "ticketcode_discount_price": 5, 优惠码优惠价
  497 + "ticket_discount_price": 10, 优惠券优惠价
  498 + "integral": 0, 积分
  499 + "postage": "30.00" 运费
  500 + }
  501 + },
  502 + })
  503 + */
  504 + public function orderIntegral()
  505 + {
  506 + $carModel = new \app\api\model\Car();
  507 + $goodsModel = new \app\api\model\Goods();
  508 + $ticketCodeModel = new \app\api\model\Ticketcode();
  509 + $userTicketModel = new \app\api\model\UserTicket();
  510 + $userId = $this->getUserId();
  511 + $goodsId = $this->request->param('goods_id');
  512 + $ticketId = $this->request->param('ticket_user_id');
  513 + $ticketCode = $this->request->param('ticket_code');
  514 + $provinceId = $this->request->param('province_id');
  515 + $cityId = $this->request->param('city_id');
  516 + $countyId = $this->request->param('county_id');
  517 + $postageType = $this->request->param('postage_type');
  518 + if (empty($goodsId)) $this->error('缺少参数 goods_id!');
  519 + if (empty($provinceId)) $this->error('缺少参数 province_id!');
  520 + if (empty($cityId)) $this->error('缺少参数 city_id!');
  521 + if (empty($countyId)) $this->error('缺少参数 county_id!');
  522 + if (empty($postageType)) $this->error('缺少参数 postage_type!');
  523 + //判断优惠券和优惠码不能同时使用
  524 + if (!empty($ticketCode) && !empty($ticketId)) $this->error('优惠券与优惠码不能同时使用!');
  525 +
  526 +
  527 + //获取积分商品状态
  528 + $goodsInfo = $this->integralGoodsModel->where(['id' => $goodsId])->field('id,status,stock_num,goods_price,integral')->find();
  529 +
  530 + //判断商品是否有库存和状态
  531 + if ($goodsInfo['status'] == 2) $this->error($goodsInfo['ch_name'] . '商品已下架');
  532 + if ($goodsInfo['stock_num'] == 0) $this->error($goodsInfo['ch_name'] . '商品库存不足');
  533 + //判断用户收货区域是否有库存
  534 + $areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $goodsInfo['id'], 'd.area_id' => $provinceId]);
  535 + if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
  536 +
  537 + //判断优惠码是否可用
  538 + if (!empty($ticketCode)) {
  539 + $ticketCodeInfo = $ticketCodeModel->where('code', $ticketCode)->find();
  540 + if (!$ticketCodeInfo) $this->error('优惠码不存在!');
  541 + if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
  542 + if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
  543 + }
  544 +
  545 + //判断优惠券是否可用
  546 + if (!empty($ticketId)) {
  547 + $ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $ticketId, 'status' => 1])->find();
  548 + if (!$ticketInfo) $this->error('优惠码不存在!');
  549 + }
  550 +
  551 + //获取原价,现价,折扣
  552 + $goodsInfo['user_type'] = $this->user['type'];
  553 + $data['price'] = round(get_price($goodsInfo), 2); //折扣后价格
  554 + $data['discount_price'] = round(get_discount_price($goodsInfo), 2);//折扣金额
  555 + $data['original_price'] = $goodsInfo['goods_price'];//原价
  556 + $data['integral'] = $goodsInfo['integral']; //积分
  557 +
  558 +
  559 + //判断积分
  560 + if ($data['integral'] > $this->user['score']) $this->error('积分不足');
  561 + //获取运费
  562 + $is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
  563 + //特殊地区满200免运费
  564 + if ($is_special == 1 && $data['price'] >= 200) {
  565 + $postage = 0;
  566 + //其他地区满300免运费
  567 + } elseif ($data['price'] >= 300) {
  568 + $postage = 0;
  569 + } else {
  570 + //都不满足获取对应的运费
  571 + $postageWhere = ['province_id' => $provinceId, 'city_id' => $cityId, 'county_id' => $countyId];
  572 + if ($postageType == 2) {
  573 + //配送为闪送
  574 + $postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
  575 + if (!$postage) {
  576 + $postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage2');
  577 + }
  578 + } else {
  579 + //配送为普通配送
  580 + $postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
  581 + if (!$postage) {
  582 + $postage = $this->areaExtendModel->where(['province_id' => $provinceId])->value('postage1');
  583 + }
  584 + }
  585 + }
  586 + $data['postage'] = $postage;
  587 + $data['price'] += $postage;
  588 +
  589 + //使用优惠码
  590 + if (!empty($ticketCodeInfo)) {
  591 + $data['price'] = $data['price'] - $ticketCodeInfo['price'];
  592 + $data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
  593 + }
  594 +
  595 + //使用优惠券
  596 + if (!empty($ticketInfo)) {
  597 + $data['price'] = $data['price'] - $ticketInfo['price'];
  598 + $data['ticket_discount_price'] = $ticketInfo['price'];
  599 + }
  600 +
  601 + $this->success('success', $data);
  602 +
  603 + }
  604 +
  605 +
  606 + /**
  607 + * @ApiTitle (创建订单(积分商品))
  608 + * @ApiSummary (创建订单(积分商品))
  609 + * @ApiMethod (POST)
  610 + * @ApiHeaders (name=token, type=string, required=true description="请求的Token")
  611 + * @ApiParams (name=goods_id, type=string, required=true, description="商品id")
  612 + * @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
  613 + * @ApiParams (name=ticket_user_id, type=string, required=false, description="用户优惠券id")
  614 + * @ApiParams (name=ticket_code, type=string, required=false, description="优惠码")
  615 + * @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
  616 + * @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
  617 + * @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
  618 + * @ApiParams (name=address, type=string, required=true, description="商品收货地址")
  619 + * @ApiParams (name=name, type=string, required=true, description="商品收货人")
  620 + * @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
  621 + * @ApiParams (name=present_id, type=string, required=false, description="赠品id")
  622 + * @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
  623 + * @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
  624 + * @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
  625 + * @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
  626 + * @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
  627 + * @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
  628 + * @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
  629 + * @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
  630 + * @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
  631 + * @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
  632 + * @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
  633 + * @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
  634 + * @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
  635 + * @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
  636 + * @ApiParams (name=order_note, type=string, required=false, description="订单备注")
  637 + * @ApiRoute (/api/order/createOrderIntegral)
  638 + * @ApiReturn({
  639 +
  640 + },
  641 + })
  642 + */
  643 + public function createOrderIntegral()
  644 + {
  645 + $ticketCodeModel = new \app\api\model\Ticketcode();
  646 + $userTicketModel = new \app\api\model\UserTicket();
  647 + $userId = $this->getUserId();
  648 + $param = $this->request->param();
  649 + $param['user_id'] = $userId;
  650 + $validate = new Validate([
  651 + 'goods_id' => 'require',
  652 + 'pay_type' => 'require',
  653 + 'name' => 'require',
  654 + 'mobile' => 'require',
  655 + 'address' => 'require',
  656 + 'province_id' => 'require',
  657 + 'city_id' => 'require',
  658 + 'county_id' => 'require',
  659 + 'postage_type' => 'require',
  660 + 'invoice_status' => 'require',
  661 +
  662 + ]);
  663 +
  664 + $validate->message([
  665 + 'goods_id' => '缺少参数 goods_id!',
  666 + 'pay_type' => '缺少参数 pay_type!',
  667 + 'name' => '缺少参数 name!',
  668 + 'mobile' => '缺少参数 mobile!',
  669 + 'address' => '缺少参数 address!',
  670 + 'province_id' => '缺少参数 province_id!',
  671 + 'city_id' => '缺少参数 city_id!',
  672 + 'county_id' => '缺少参数 county_id!',
  673 + 'postage_type' => '缺少参数 postage_type!',
  674 + 'invoice_status' => '缺少参数 invoice_status!',
  675 + ]);
  676 +
  677 + if (!$validate->check($param)) {
  678 + $this->error($validate->getError());
  679 + }
  680 + if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
  681 + //判断优惠券和优惠码不能同时使用
  682 + if (!empty($param['ticket_code']) && !empty($param['ticket_user_id'])) $this->error('优惠券与优惠码不能同时使用!');
  683 + //获取积分商品状态
  684 + $goodsInfo = $this->integralGoodsModel->where(['id' => $param['goods_id']])->field('id,ch_name,en_name,image,status,stock_num,goods_price,integral')->find();
  685 +
  686 + //判断商品是否有库存和状态
  687 + if ($goodsInfo['status'] == 2) $this->error($goodsInfo['ch_name'] . '商品已下架');
  688 + if ($goodsInfo['stock_num'] == 0) $this->error($goodsInfo['ch_name'] . '商品库存不足');
  689 + //判断用户收货区域是否有库存
  690 + $areaStock = $this->integralGoodsModel->getAreaStockNum(['g.id' => $goodsInfo['id'], 'd.area_id' => $param['province_id']]);
  691 + if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
  692 +
  693 + //判断优惠码是否可用
  694 + if (!empty($param['ticket_code'])) {
  695 + $ticketCodeInfo = $ticketCodeModel->where('code', $param['ticket_code'])->find();
  696 + if (!$ticketCodeInfo) $this->error('优惠码不存在!');
  697 + if ($ticketCodeInfo['validtime'] < time()) $this->error('优惠码已过期!');
  698 + if ($ticketCodeInfo['user_id']) $this->error('优惠码已使用!');
  699 + }
  700 +
  701 + //判断优惠券是否可用
  702 + if (!empty($param['ticket_user_id'])) {
  703 + $ticketInfo = $userTicketModel->where(['user_id' => $userId, 'id' => $param['ticket_user_id'], 'status' => 1])->find();
  704 + if (!$ticketInfo) $this->error('优惠码不存在!');
  705 + }
  706 +
  707 + //获取原价,现价,折扣
  708 + $goodsInfo['user_type'] = $this->user['type'];
  709 + $data['price'] = round(get_price($goodsInfo), 2); //折扣后价格
  710 + $data['discount_price'] = round(get_discount_price($goodsInfo), 2);//折扣金额
  711 + $data['original_price'] = $goodsInfo['goods_price'];//原价
  712 + $data['integral'] = $goodsInfo['integral']; //积分
  713 +
  714 + //判断积分
  715 + if ($data['integral'] > $this->user['score']) $this->error('积分不足');
  716 + //获取运费
  717 + $is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
  718 + //特殊地区满200免运费
  719 + if ($is_special == 1 && $data['price'] >= 200) {
  720 + $postage = 0;
  721 + //其他地区满300免运费
  722 + } elseif ($data['price'] >= 300) {
  723 + $postage = 0;
  724 + } else {
  725 + //都不满足获取对应的运费
  726 + $postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
  727 + if ($param['postage_type'] == 2) {
  728 + //配送为闪送
  729 + $postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
  730 + if (!$postage) {
  731 + $postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
  732 + }
  733 + } else {
  734 + //配送为普通配送
  735 + $postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
  736 + if (!$postage) {
  737 + $postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
  738 + }
  739 + }
  740 + }
  741 + $data['postage'] = $postage;
  742 + $data['price'] += $postage;
  743 +
  744 + Db::startTrans();
  745 + try {
  746 + //使用优惠码
  747 + if (!empty($ticketCodeInfo)) {
  748 + $data['price'] = $data['price'] - $ticketCodeInfo['price'];
  749 + $data['ticketcode_discount_price'] = $ticketCodeInfo['price'];
  750 + $data['ticketcode_id'] = $ticketCodeInfo['id'];
  751 + $this->ticketCodeModel->where('id', $ticketCodeInfo['id'])->update(['user_id' => $userId]);
  752 + }
  753 +
  754 + //使用优惠券
  755 + if (!empty($ticketInfo)) {
  756 + $data['price'] = $data['price'] - $ticketInfo['price'];
  757 + $data['ticket_discount_price'] = $ticketInfo['price'];
  758 + $data['user_ticket_id'] = $ticketInfo['id'];
  759 + $this->userTicketModel->where('id', $ticketInfo['id'])->update(['status' => 2]);
  760 +
  761 + }
  762 +
  763 + //order表数据
  764 + $orderData = [
  765 + 'order_num' => get_order_num(),
  766 + 'user_id' => $userId,
  767 + 'pay_type' => $param['pay_type'],
  768 + 'goods_total' => $data['original_price'],
  769 + 'pay_total' => $data['price'],
  770 + 'discount_price' => $data['discount_price'],
  771 + 'score' => $data['integral'],
  772 + 'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
  773 + 'postage_total' => $postage,
  774 + 'postage_type' => $param['postage_type'],
  775 + 'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
  776 + 'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
  777 + 'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
  778 + 'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
  779 + 'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
  780 + 'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
  781 + 'invoice_status' => $param['invoice_status'],
  782 + 'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
  783 + 'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
  784 + 'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
  785 + 'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
  786 + 'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
  787 + 'type' => 2,
  788 + 'status' => 1,
  789 + 'createtime' => time(),
  790 + 'updatetime' => time(),
  791 + ];
  792 + $res1 = $this->orderModel->insertGetId($orderData);
  793 +
  794 + //收货地址数据
  795 + $orderAddress = [
  796 + 'order_id' => $res1,
  797 + 'name' => $param['name'],
  798 + 'mobile' => $param['mobile'],
  799 + 'address' => $param['address'],
  800 + 'province_id' => $param['province_id'],
  801 + 'city_id' => $param['city_id'],
  802 + 'county_id' => $param['county_id'],
  803 + 'createtime' => time(),
  804 + 'updatetime' => time(),
  805 + ];
  806 + $res2 = $this->orderAddressModel->insertData($orderAddress);
  807 + if ($param['invoice_status'] != 3) {
  808 + //收货地址数据
  809 + $orderAddressInvoice = [
  810 + 'order_id' => $res1,
  811 + 'name' => $param['invoice_name'],
  812 + 'mobile' => $param['invoice_mobile'],
  813 + 'address' => $param['invoice_address'],
  814 + 'province_id' => $param['invoice_province_id'],
  815 + 'city_id' => $param['invoice_city_id'],
  816 + 'county_id' => $param['invoice_county_id'],
  817 + 'createtime' => time(),
  818 + 'updatetime' => time(),
  819 + ];
  820 + $res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
  821 + }
  822 +
  823 + //获取商品详情列表
  824 + $orderInfoData = [
  825 + 'order_id' => $res1,
  826 + 'pay_type' => $param['pay_type'],
  827 + 'user_id' => $userId,
  828 + 'goods_id' => $goodsInfo['id'],
  829 + 'ch_goods_name' => $goodsInfo['ch_name'],
  830 + 'en_goods_name' => $goodsInfo['en_name'],
  831 + 'goods_image' => $goodsInfo['image'],
  832 + 'number' => 1,
  833 + 'type' => 2, //积分商品
  834 + 'score' => !empty($data['integral']) ? $data['integral'] : 0,
  835 + 'goods_total' => get_price($goodsInfo),
  836 + 'pay_total' => $data['price'],
  837 + 'postage_total' => $postage,
  838 + ];
  839 +
  840 + $res4 = $this->orderInfoModel->save($orderInfoData);
  841 +
  842 + if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
  843 + Db::commit();
  844 + return true;
  845 + } else {
  846 + return false;
  847 + }
  848 + } catch (Exception $e) {
  849 + Db::rollback();
  850 + return false;
  851 + }
  852 + }
475 853
476 /** 854 /**
477 * @ApiTitle (确认订单(团购商品)) 855 * @ApiTitle (确认订单(团购商品))
@@ -479,7 +857,6 @@ class Order extends Api @@ -479,7 +857,6 @@ class Order extends Api
479 * @ApiMethod (POST) 857 * @ApiMethod (POST)
480 * @ApiHeaders (name=token, type=string, required=true description="请求的Token") 858 * @ApiHeaders (name=token, type=string, required=true description="请求的Token")
481 * @ApiParams (name=goods_id, type=string, required=true, description="商品id") 859 * @ApiParams (name=goods_id, type=string, required=true, description="商品id")
482 - * @ApiParams (name=number, type=string, required=true, description="商品数量")  
483 * @ApiParams (name=province_id, type=string, required=true, description="省id") 860 * @ApiParams (name=province_id, type=string, required=true, description="省id")
484 * @ApiParams (name=city_id, type=string, required=true, description="市") 861 * @ApiParams (name=city_id, type=string, required=true, description="市")
485 * @ApiParams (name=county_id, type=string, required=true, description="县") 862 * @ApiParams (name=county_id, type=string, required=true, description="县")
@@ -502,43 +879,36 @@ class Order extends Api @@ -502,43 +879,36 @@ class Order extends Api
502 */ 879 */
503 public function orderTeam() 880 public function orderTeam()
504 { 881 {
505 - $goodsModel = new \app\api\model\Goods();  
506 $userId = $this->getUserId(); 882 $userId = $this->getUserId();
507 - $number = $this->request->param('number');  
508 $goodsId = $this->request->param('goods_id'); 883 $goodsId = $this->request->param('goods_id');
509 $provinceId = $this->request->param('province_id'); 884 $provinceId = $this->request->param('province_id');
510 $cityId = $this->request->param('city_id'); 885 $cityId = $this->request->param('city_id');
511 $countyId = $this->request->param('county_id'); 886 $countyId = $this->request->param('county_id');
512 $postageType = $this->request->param('postage_type'); 887 $postageType = $this->request->param('postage_type');
513 if (empty($goodsId)) $this->error('缺少参数 goods_id!'); 888 if (empty($goodsId)) $this->error('缺少参数 goods_id!');
514 - if (empty($number)) $this->error('缺少参数 number!');  
515 if (empty($provinceId)) $this->error('缺少参数 province_id!'); 889 if (empty($provinceId)) $this->error('缺少参数 province_id!');
516 if (empty($cityId)) $this->error('缺少参数 city_id!'); 890 if (empty($cityId)) $this->error('缺少参数 city_id!');
517 if (empty($countyId)) $this->error('缺少参数 county_id!'); 891 if (empty($countyId)) $this->error('缺少参数 county_id!');
518 if (empty($postageType)) $this->error('缺少参数 postage_type!'); 892 if (empty($postageType)) $this->error('缺少参数 postage_type!');
519 893
520 - $goodsInfo = $goodsModel->where(['id' => $goodsId])->field('id,status,stock_num')->find(); 894 + $goodsInfo = $this->goodsModel->where(['id' => $goodsId])->field('id,status,stock_num,is_group,group_num,group_price,grouptime,goods_price')->find();
521 //判断商品是否有库存和状态 895 //判断商品是否有库存和状态
522 if ($goodsInfo['status'] == 2) $this->error('商品已下架'); 896 if ($goodsInfo['status'] == 2) $this->error('商品已下架');
523 if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足'); 897 if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
  898 + if ($goodsInfo['is_group'] != 1) $this->error('该商品不是团购商品');
524 //判断用户收货区域是否有库存 899 //判断用户收货区域是否有库存
525 - $areaStock = $goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);  
526 - if (!$areaStock) $data['is_lack_stock'] = 1; 900 + $areaStock = $this->goodsModel->getAreaStockNum(['g.id' => $goodsId, 'd.area_id' => $provinceId]);
  901 + if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
527 902
528 //获取原价,现价,折扣 903 //获取原价,现价,折扣
529 - $priceArr = $this->carModel->getCarMoney(['id' => ['in', $carId]]);  
530 $data['price'] = 0; //折扣后价格 904 $data['price'] = 0; //折扣后价格
531 $data['discount_price'] = 0; //折扣金额 905 $data['discount_price'] = 0; //折扣金额
532 $data['original_price'] = 0; //原价 906 $data['original_price'] = 0; //原价
533 - $data['integral'] = 0; //积分  
534 - foreach ($priceArr as $k => $v) {  
535 - $priceArr[$k]['user_type'] = $this->user['type'];  
536 - $data['price'] += round(get_price($v), 2);  
537 - $data['discount_price'] += round(get_discount_price($v), 2);  
538 - $data['original_price'] += $v['goods_price'];  
539 - if (!empty($v['integral'])) $data['integral'] += $v['integral']; 907 + $goodsInfo['user_type'] = $this->user['type'];
  908 + $data['price'] += round(get_price($goodsInfo), 2);
  909 + $data['discount_price'] += round(get_discount_price($goodsInfo), 2);
  910 + $data['original_price'] += $goodsInfo['group_price'];
540 911
541 - }  
542 //获取运费 912 //获取运费
543 $is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special'); 913 $is_special = $this->areaExtendModel->where(['province_id' => $provinceId])->value('is_special');
544 //特殊地区满200免运费 914 //特殊地区满200免运费
@@ -572,6 +942,229 @@ class Order extends Api @@ -572,6 +942,229 @@ class Order extends Api
572 942
573 } 943 }
574 944
  945 + /**
  946 + * @ApiTitle (创建订单(团购商品))
  947 + * @ApiSummary (创建订单(团购商品))
  948 + * @ApiMethod (POST)
  949 + * @ApiHeaders (name=token, type=string, required=true description="请求的Token")
  950 + * @ApiParams (name=goods_id, type=string, required=false, description="商品id")
  951 + * @ApiParams (name=team_group_id, type=string, required=false, description="拼团id")
  952 + * @ApiParams (name=pay_type, type=string, required=false, description="支付类型:1=微信支付,2=余额支付")
  953 + * @ApiParams (name=province_id, type=string, required=true, description="商品收货地址省id")
  954 + * @ApiParams (name=city_id, type=string, required=true, description="商品收货地址市id")
  955 + * @ApiParams (name=county_id, type=string, required=true, description="商品收货地址县id")
  956 + * @ApiParams (name=address, type=string, required=true, description="商品收货地址")
  957 + * @ApiParams (name=name, type=string, required=true, description="商品收货人")
  958 + * @ApiParams (name=mobile, type=string, required=true, description="商品收货电话")
  959 + * @ApiParams (name=present_id, type=string, required=false, description="赠品id")
  960 + * @ApiParams (name=postage_type, type=string, required=true, description="配送类型:1=普通配送,2=闪送")
  961 + * @ApiParams (name=postage_date, type=string, required=false, description="配送日期")
  962 + * @ApiParams (name=postage_time, type=string, required=false, description="配送时间")
  963 + * @ApiParams (name=invoice_status, type=string, required=true, description="发票状态:1=个人或事业单位,2=企业,3=不需要开发票")
  964 + * @ApiParams (name=invoice_type, type=string, required=false, description="发票类型:1=纸质发票,2=电子发票")
  965 + * @ApiParams (name=invoice_email, type=string, required=false, description="电子发票邮箱")
  966 + * @ApiParams (name=taitou, type=string, required=false, description="发票抬头")
  967 + * @ApiParams (name=duty_num, type=string, required=false, description="发票税号")
  968 + * @ApiParams (name=invoice_province_id, type=string, required=false, description="发票收货地址省id")
  969 + * @ApiParams (name=invoice_city_id, type=string, required=false, description="发票收货地址市id")
  970 + * @ApiParams (name=invoice_county_id, type=string, required=false, description="发票收货地址县id")
  971 + * @ApiParams (name=invoice_address, type=string, required=false, description="发票收货地址")
  972 + * @ApiParams (name=invoice_name, type=string, required=false, description="发票收货人")
  973 + * @ApiParams (name=invoice_mobile, type=string, required=false, description="发票收货电话")
  974 + * @ApiParams (name=order_note, type=string, required=false, description="订单备注")
  975 + * @ApiRoute (/api/order/createOrderTeam)
  976 + * @ApiReturn({
  977 +
  978 + },
  979 + })
  980 + */
  981 + public function createOrderTeam()
  982 + {
  983 + $userId = $this->getUserId();
  984 + $param = $this->request->param();
  985 + $param['user_id'] = $userId;
  986 + $validate = new Validate([
  987 + 'goods_id' => 'require',
  988 + 'pay_type' => 'require',
  989 + 'name' => 'require',
  990 + 'mobile' => 'require',
  991 + 'address' => 'require',
  992 + 'province_id' => 'require',
  993 + 'city_id' => 'require',
  994 + 'county_id' => 'require',
  995 + 'postage_type' => 'require',
  996 + 'invoice_status' => 'require',
  997 +
  998 + ]);
  999 +
  1000 + $validate->message([
  1001 + 'goods_id' => '缺少参数 goods_id!',
  1002 + 'pay_type' => '缺少参数 pay_type!',
  1003 + 'name' => '缺少参数 name!',
  1004 + 'mobile' => '缺少参数 mobile!',
  1005 + 'address' => '缺少参数 address!',
  1006 + 'province_id' => '缺少参数 province_id!',
  1007 + 'city_id' => '缺少参数 city_id!',
  1008 + 'county_id' => '缺少参数 county_id!',
  1009 + 'postage_type' => '缺少参数 postage_type!',
  1010 + 'invoice_status' => '缺少参数 invoice_status!',
  1011 + ]);
  1012 +
  1013 + if (!$validate->check($param)) {
  1014 + $this->error($validate->getError());
  1015 + }
  1016 + if (($param['pay_type'] == 2 && $this->user['type'] == 3) || ($param['pay_type'] == 2 && $this->user['type'] == 4)) $this->error('员工或代理不能使用余额支付!');
  1017 +
  1018 + $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();
  1019 + //判断商品是否有库存和状态
  1020 + if ($goodsInfo['status'] == 2) $this->error('商品已下架');
  1021 + if ($goodsInfo['stock_num'] == 0) $this->error('商品库存不足');
  1022 + if ($goodsInfo['is_group'] != 1) $this->error('该商品不是团购商品');
  1023 + //判断用户收货区域是否有库存
  1024 + $areaStock = $this->goodsModel->getAreaStockNum(['g.id' => $param['goods_id'], 'd.area_id' => $param['province_id']]);
  1025 + if (!empty($areaStock) && $areaStock['stock_num'] == 0) $data['is_lack_stock'] = 1;
  1026 +
  1027 + //判断团购是否满人
  1028 + if (!empty($param['team_group_id'])) {
  1029 + $teamInfo = Db::name('team_group')->where('id', $param['team_group_id'])->field('group_num,grouptime')->find();
  1030 + $count = Db::name('team_group_info')->where('team_group_id', $param['team_group_id'])->count();
  1031 + $is_join_team = Db::name('team_group_info')->where(['team_group_id' => $param['team_group_id'], 'user_id' => $userId])->count();
  1032 + if (!$teamInfo) $this->error('没有此拼团信息');
  1033 + if ($teamInfo['grouptime'] <= time()) $this->error('拼团已失效');
  1034 + if ($teamInfo['group_num'] <= $count) $this->error('参团人数已满');
  1035 + if ($is_join_team) $this->error('您已参团了');
  1036 + }
  1037 +
  1038 + //获取原价,现价,折扣
  1039 + $data['price'] = 0; //折扣后价格
  1040 + $data['discount_price'] = 0; //折扣金额
  1041 + $data['original_price'] = 0; //原价
  1042 + $goodsInfo['user_type'] = $this->user['type'];
  1043 + $data['price'] += round(get_price($goodsInfo), 2);
  1044 + $data['discount_price'] += round(get_discount_price($goodsInfo), 2);
  1045 + $data['original_price'] += $goodsInfo['group_price'];
  1046 +
  1047 + //获取运费
  1048 + $is_special = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('is_special');
  1049 + //特殊地区满200免运费
  1050 + if ($is_special == 1 && $data['price'] >= 200) {
  1051 + $postage = 0;
  1052 + //其他地区满300免运费
  1053 + } elseif ($data['price'] >= 300) {
  1054 + $postage = 0;
  1055 + } else {
  1056 + //都不满足获取对应的运费
  1057 + $postageWhere = ['province_id' => $param['province_id'], 'city_id' => $param['city_id'], 'county_id' => $param['county_id']];
  1058 + if ($param['postage_type'] == 2) {
  1059 + //配送为闪送
  1060 + $postage = $this->areaExtendModel->where($postageWhere)->value('postage2');
  1061 + if (!$postage) {
  1062 + $postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage2');
  1063 + }
  1064 + } else {
  1065 + //配送为普通配送
  1066 + $postage = $this->areaExtendModel->where($postageWhere)->value('postage1');
  1067 + if (!$postage) {
  1068 + $postage = $this->areaExtendModel->where(['province_id' => $param['province_id']])->value('postage1');
  1069 + }
  1070 + }
  1071 + }
  1072 + $data['postage'] = $postage;
  1073 + $data['price'] += $postage;
  1074 +
  1075 +
  1076 + Db::startTrans();
  1077 + try {
  1078 + //order表数据
  1079 + $orderData = [
  1080 + 'order_num' => get_order_num(),
  1081 + 'user_id' => $userId,
  1082 + 'pay_type' => $param['pay_type'],
  1083 + 'goods_total' => $data['original_price'],
  1084 + 'pay_total' => $data['price'],
  1085 + 'discount_price' => $data['discount_price'],
  1086 + 'present_id' => !empty($param['present_id']) ? $param['present_id'] : '',
  1087 + 'team_group_id' => !empty($param['team_group_id']) ? $param['team_group_id'] : '',
  1088 + 'team_group_status' => 1,
  1089 + 'postage_total' => $postage,
  1090 + 'postage_type' => $param['postage_type'],
  1091 + 'postage_date' => !empty($data['postage_date']) ? $data['postage_date'] : '',
  1092 + 'postage_time' => !empty($data['postage_time']) ? $data['postage_time'] : '',
  1093 + 'user_ticket_id' => !empty($data['user_ticket_id']) ? $data['user_ticket_id'] : '',
  1094 + 'ticket_price' => !empty($data['ticket_discount_price']) ? $data['ticket_discount_price'] : '',
  1095 + 'ticketcode_id' => !empty($data['ticketcode_id']) ? $data['ticketcode_id'] : '',
  1096 + 'ticketcode_price' => !empty($data['ticketcode_discount_price']) ? $data['ticketcode_discount_price'] : '',
  1097 + 'invoice_status' => $param['invoice_status'],
  1098 + 'invoice_type' => !empty($param['invoice_type']) ? $param['invoice_type'] : '',
  1099 + 'invoice_email' => !empty($param['invoice_email']) ? $param['invoice_email'] : '',
  1100 + 'taitou' => !empty($param['taitou']) ? $param['taitou'] : '',
  1101 + 'duty_num' => !empty($param['duty_num']) ? $param['duty_num'] : '',
  1102 + 'order_note' => !empty($param['order_note']) ? $param['order_note'] : '',
  1103 + 'type' => 3,
  1104 + 'status' => 1,
  1105 + 'createtime' => time(),
  1106 + 'updatetime' => time(),
  1107 + ];
  1108 + $res1 = $this->orderModel->insertGetId($orderData);
  1109 +
  1110 + //收货地址数据
  1111 + $orderAddress = [
  1112 + 'order_id' => $res1,
  1113 + 'name' => $param['name'],
  1114 + 'mobile' => $param['mobile'],
  1115 + 'address' => $param['address'],
  1116 + 'province_id' => $param['province_id'],
  1117 + 'city_id' => $param['city_id'],
  1118 + 'county_id' => $param['county_id'],
  1119 + 'createtime' => time(),
  1120 + 'updatetime' => time(),
  1121 + ];
  1122 + $res2 = $this->orderAddressModel->insertData($orderAddress);
  1123 + if ($param['invoice_status'] != 3) {
  1124 + //收货地址数据
  1125 + $orderAddressInvoice = [
  1126 + 'order_id' => $res1,
  1127 + 'name' => $param['invoice_name'],
  1128 + 'mobile' => $param['invoice_mobile'],
  1129 + 'address' => $param['invoice_address'],
  1130 + 'province_id' => $param['invoice_province_id'],
  1131 + 'city_id' => $param['invoice_city_id'],
  1132 + 'county_id' => $param['invoice_county_id'],
  1133 + 'createtime' => time(),
  1134 + 'updatetime' => time(),
  1135 + ];
  1136 + $res3 = $this->orderAddressInvoiceModel->insertData($orderAddressInvoice);
  1137 + }
  1138 +
  1139 + //获取商品详情
  1140 + $orderInfoData = [
  1141 + 'order_id' => $res1,
  1142 + 'pay_type' => $param['pay_type'],
  1143 + 'user_id' => $userId,
  1144 + 'goods_id' => $param['goods_id'],
  1145 + 'ch_goods_name' => $goodsInfo['ch_name'],
  1146 + 'en_goods_name' => $goodsInfo['en_name'],
  1147 + 'goods_image' => $goodsInfo['image'],
  1148 + 'number' => 1, //团购商品购买数量只能是1
  1149 + 'type' => 3,
  1150 + 'goods_total' => $data['original_price'],
  1151 + 'pay_total' => $data['price'],
  1152 + 'postage_total' => $postage,
  1153 + ];
  1154 +
  1155 + $res4 = $this->orderInfoModel->save($orderInfoData);
  1156 +
  1157 + if ($res1 && $res2 && $res4 && (empty($res3) || $res3)) {
  1158 + Db::commit();
  1159 + return true;
  1160 + } else {
  1161 + return false;
  1162 + }
  1163 + } catch (Exception $e) {
  1164 + Db::rollback();
  1165 + return false;
  1166 + }
  1167 + }
575 1168
576 /** 1169 /**
577 * @ApiTitle (订单付款) 1170 * @ApiTitle (订单付款)
@@ -601,6 +1194,7 @@ class Order extends Api @@ -601,6 +1194,7 @@ class Order extends Api
601 $this->error(__($validate->getError())); 1194 $this->error(__($validate->getError()));
602 } 1195 }
603 $order = $this->orderModel->where(['id' => $param['order_id']])->find(); 1196 $order = $this->orderModel->where(['id' => $param['order_id']])->find();
  1197 + if (!$order) $this->error('订单不存在');
604 if ($order['status'] == 2) $this->error('订单已支付'); 1198 if ($order['status'] == 2) $this->error('订单已支付');
605 if ($order['status'] == -1) $this->error('订单已取消'); 1199 if ($order['status'] == -1) $this->error('订单已取消');
606 //判断积分 1200 //判断积分
@@ -618,6 +1212,13 @@ class Order extends Api @@ -618,6 +1212,13 @@ class Order extends Api
618 if (!$is_stock) $this->error($v['ch_goods_name'] . ' 库存不足'); 1212 if (!$is_stock) $this->error($v['ch_goods_name'] . ' 库存不足');
619 } 1213 }
620 } 1214 }
  1215 + //判断团购是否满人
  1216 + if (!empty($order['team_group_id'])) {
  1217 + $teamInfo = Db::name('team_group')->where('id', $order['team_group_id'])->field('group_num,grouptime')->find();
  1218 + $count = Db::name('team_group_info')->where('team_group_id', $order['team_group_id'])->count();
  1219 + if ($teamInfo['group_num'] <= $count) $this->error('参团人数已满');
  1220 + if ($teamInfo['grouptime'] <= time()) $this->error('拼团已失效');
  1221 + }
621 $amount = $order['pay_total']; 1222 $amount = $order['pay_total'];
622 $payType = $order['pay_type']; 1223 $payType = $order['pay_type'];
623 if ($payType == 2) { 1224 if ($payType == 2) {
@@ -634,4 +1235,41 @@ class Order extends Api @@ -634,4 +1235,41 @@ class Order extends Api
634 //微信支付 1235 //微信支付
635 } 1236 }
636 } 1237 }
  1238 +
  1239 + /**
  1240 + * @ApiTitle (取消订单)
  1241 + * @ApiSummary (取消订单)
  1242 + * @ApiMethod (POST)
  1243 + * @ApiRoute (/api/order/orderCancel)
  1244 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  1245 + * @ApiParams (name=order_id, type=string, required=true, description="订单id")
  1246 + * @ApiReturn ({
  1247 + })
  1248 + */
  1249 + public function orderCancel()
  1250 + {
  1251 + $userId = $this->getUserId();
  1252 + $param = $this->request->param();
  1253 + $rule = [
  1254 + 'order_id' => 'require',
  1255 + ];
  1256 +
  1257 + $msg = [
  1258 + 'order_id' => '缺少参数 order_id!',
  1259 + ];
  1260 +
  1261 + $validate = new Validate($rule, $msg);
  1262 + $result = $validate->check($param);
  1263 + if (!$result) {
  1264 + $this->error(__($validate->getError()));
  1265 + }
  1266 + $order = $this->orderModel->where(['id' => $param['order_id']])->find();
  1267 + if (!$order) $this->error('订单不存在');
  1268 + if ($order['status'] == 2) $this->error('订单已支付');
  1269 + if ($order['status'] == -1) $this->error('订单已取消');
  1270 +
  1271 + $res = $this->orderModel->orderCancel($order);
  1272 + if ($res) $this->success('取消订单成功');
  1273 + else $this->error('取消订单失败');
  1274 + }
637 } 1275 }
@@ -209,12 +209,15 @@ class Goods extends Model @@ -209,12 +209,15 @@ class Goods extends Model
209 $residue_number = $number - $province_stock_num;//还需多少库存 209 $residue_number = $number - $province_stock_num;//还需多少库存
210 if($residue_number > 0){ 210 if($residue_number > 0){
211 //需要其它仓库提供库存 211 //需要其它仓库提供库存
212 - $arr1['depots_id'] = $province_depot['id'];  
213 - $arr1['area_id'] = $province_depot['area_id'];  
214 - $arr1['goods_id'] = $province_depot['goods_id'];  
215 - $arr1['type'] = $province_depot['type'];  
216 - $arr1['number'] = $province_stock_num;  
217 - if ($province_depot) $arr[] = $arr1; 212 + if ($province_stock_num){
  213 + $arr1['depots_id'] = $province_depot['id'];
  214 + $arr1['area_id'] = $province_depot['area_id'];
  215 + $arr1['goods_id'] = $province_depot['goods_id'];
  216 + $arr1['type'] = $province_depot['type'];
  217 + $arr1['number'] = $province_stock_num;
  218 + if ($province_depot) $arr[] = $arr1;
  219 + }
  220 +
218 221
219 $depots = $this->alias('g') 222 $depots = $this->alias('g')
220 ->join('fa_depot d','g.id=d.goods_id') 223 ->join('fa_depot d','g.id=d.goods_id')
@@ -30,15 +30,18 @@ class Order extends Model @@ -30,15 +30,18 @@ class Order extends Model
30 $orderInfoModel = new \app\api\model\OrderInfo(); 30 $orderInfoModel = new \app\api\model\OrderInfo();
31 Db::startTrans(); 31 Db::startTrans();
32 try { 32 try {
33 -  
34 $areaId = Db::name('order_address')->where('order_id', $order_id)->value('province_id'); 33 $areaId = Db::name('order_address')->where('order_id', $order_id)->value('province_id');
35 $oldmoney = Db::name('user')->where('id', $user_id)->value('money'); 34 $oldmoney = Db::name('user')->where('id', $user_id)->value('money');
36 $oldscore = Db::name('user')->where('id', $user_id)->value('score'); 35 $oldscore = Db::name('user')->where('id', $user_id)->value('score');
37 $integral_total = 0; //购买商品所需积分 36 $integral_total = 0; //购买商品所需积分
  37 + //获取订单
  38 + $order = $this->where('id', $order_id)->field('id,pay_total,postage_total,ticket_price,ticketcode_price,type,team_group_id,team_group_status')->find();
  39 +
38 //减去用户余额 40 //减去用户余额
39 Db::name('user')->where('id', $user_id)->setDec('money', $money); 41 Db::name('user')->where('id', $user_id)->setDec('money', $money);
40 foreach ($goodsList as $key => $goods) { 42 foreach ($goodsList as $key => $goods) {
41 $integral_total += $goods['score']; //累计积分总数 43 $integral_total += $goods['score']; //累计积分总数
  44 + if ($goods['type'] == 3) $goods['type'] = '1';
42 //根据仓库进行拆单 45 //根据仓库进行拆单
43 $data[] = $goodsModel->getDepot($goods['goods_id'], $areaId, $goods['number'], $goods['type']); 46 $data[] = $goodsModel->getDepot($goods['goods_id'], $areaId, $goods['number'], $goods['type']);
44 if (!$data[$key]) return ['code' => -1, 'msg' => $goods['ch_goods_name'] . ' 库存不足']; 47 if (!$data[$key]) return ['code' => -1, 'msg' => $goods['ch_goods_name'] . ' 库存不足'];
@@ -60,12 +63,13 @@ class Order extends Model @@ -60,12 +63,13 @@ class Order extends Model
60 //添加订单子表 63 //添加订单子表
61 $detailId = $orderDetailModel->insertGetId($detailData); 64 $detailId = $orderDetailModel->insertGetId($detailData);
62 $oldOrderInfoIds = []; //获取创建订单时 order_info_id 65 $oldOrderInfoIds = []; //获取创建订单时 order_info_id
  66 +
63 foreach ($goodsDepot as $key => $goodsDepotInfo) { 67 foreach ($goodsDepot as $key => $goodsDepotInfo) {
64 $where = [ 68 $where = [
65 'order_id' => $order_id, 69 'order_id' => $order_id,
66 'user_id' => $user_id, 70 'user_id' => $user_id,
67 'goods_id' => $goodsDepotInfo['goods_id'], 71 'goods_id' => $goodsDepotInfo['goods_id'],
68 - 'type' => $goodsDepotInfo['type'], 72 + 'type' => $order['type'] == 3 ? 3 : $goodsDepotInfo['type'],
69 ]; 73 ];
70 $orderInfo = $orderInfoModel->where($where)->find(); 74 $orderInfo = $orderInfoModel->where($where)->find();
71 $oldOrderInfoIds[] = $orderInfo['id']; 75 $oldOrderInfoIds[] = $orderInfo['id'];
@@ -101,7 +105,7 @@ class Order extends Model @@ -101,7 +105,7 @@ class Order extends Model
101 //添加订单详情 105 //添加订单详情
102 $orderInfoModel->saveAll($orderInfoData); 106 $orderInfoModel->saveAll($orderInfoData);
103 //删除之前订单详情 107 //删除之前订单详情
104 - $orderInfoModel->where(['id'=>['in',$oldOrderInfoIds]])->delete(); 108 + $orderInfoModel->where(['id' => ['in', $oldOrderInfoIds]])->delete();
105 109
106 //有积分商品减积分 110 //有积分商品减积分
107 if ($integral_total != 0) { 111 if ($integral_total != 0) {
@@ -110,6 +114,7 @@ class Order extends Model @@ -110,6 +114,7 @@ class Order extends Model
110 //将订单状态修改为已支付 114 //将订单状态修改为已支付
111 $this->where('id', $order_id)->update(['status' => 2, 'paytime' => time()]); 115 $this->where('id', $order_id)->update(['status' => 2, 'paytime' => time()]);
112 //积分变动表记录 116 //积分变动表记录
  117 + $oldscore = Db::name('user')->where('id', $user_id)->value('score');
113 $scoredata = [ 118 $scoredata = [
114 'user_id' => $user_id, 119 'user_id' => $user_id,
115 'type' => 1, //购物返积分 120 'type' => 1, //购物返积分
@@ -134,16 +139,16 @@ class Order extends Model @@ -134,16 +139,16 @@ class Order extends Model
134 139
135 140
136 $user_pid = Db::name('user')->where('id', $user_id)->value('pid'); 141 $user_pid = Db::name('user')->where('id', $user_id)->value('pid');
137 - //判断是否有上级进行返利  
138 - if ($user_pid) {  
139 - $order = $this->where('id',$order_id)->field('pay_total,postage_total,ticket_price,ticketcode_price')->find(); 142 +
  143 + //判断是否有上级进行返利 团购订单不在这里返利
  144 + if ($user_pid && $order['type'] != 3) {
140 if (!empty($order['ticket_price'])) $order['pay_total'] += $order['ticket_price']; 145 if (!empty($order['ticket_price'])) $order['pay_total'] += $order['ticket_price'];
141 if (!empty($order['ticketcode_price'])) $order['pay_total'] += $order['ticketcode_price']; 146 if (!empty($order['ticketcode_price'])) $order['pay_total'] += $order['ticketcode_price'];
142 if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total']; 147 if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
143 $parentInfo = Db::name('user')->where('id', $user_pid)->find(); 148 $parentInfo = Db::name('user')->where('id', $user_pid)->find();
144 //普通用户无返利 149 //普通用户无返利
145 - if ($parentInfo['type'] != 1){  
146 - $rebateMoney = get_rebate($order['pay_total'],$parentInfo['type']); 150 + if ($parentInfo['type'] != 1) {
  151 + $rebateMoney = get_rebate($order['pay_total'], $parentInfo['type']);
147 $rebatedata = [ 152 $rebatedata = [
148 'user_id' => $user_pid, 153 'user_id' => $user_pid,
149 'type' => 4, //佣金收入(返利) 154 'type' => 4, //佣金收入(返利)
@@ -153,9 +158,155 @@ class Order extends Model @@ -153,9 +158,155 @@ class Order extends Model
153 'createtime' => time(), 158 'createtime' => time(),
154 ]; 159 ];
155 Db::name('user_money_log')->insert($rebatedata); 160 Db::name('user_money_log')->insert($rebatedata);
156 - Db::name('user')->where('id',$user_pid)->setInc('money',$rebateMoney); 161 + Db::name('user')->where('id', $user_pid)->setInc('money', $rebateMoney);
157 } 162 }
158 } 163 }
  164 + if ($order['type'] == 3 && !empty($order['team_group_id'])) {
  165 + //参团
  166 + $this->joinTeam($order, $user_id);
  167 + } elseif ($order['type'] == 3) {
  168 + //开团
  169 + $this->createTeam($order, $user_id);
  170 + }
  171 + Db::commit();
  172 + return true;
  173 +
  174 + } catch (Exception $e) {
  175 + Db::rollback();
  176 + return false;
  177 + }
  178 + }
  179 +
  180 + public function createTeam($order, $user_id)
  181 + {
  182 + $goods_id = Db::name('order_info')->where(['order_id' => $order['id']])->value('goods_id');
  183 + $goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_agency_rebate,group_num,grouptime')->find();
  184 + $user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();
  185 + if (!empty($user_info['pid'])) { //返利
  186 + if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
  187 + $parent_info = Db::name('user')->where('id', $user_info['pid'])->find();
  188 + //普通用户无返利
  189 + if ($parent_info['type'] == 2) {
  190 + //会员返利
  191 + $rebateMoney = $order['pay_total'] * $goods_info['group_vip_rebate'];
  192 + } elseif ($parent_info['type'] == 3) {
  193 + //员工返利
  194 + $rebateMoney = $order['pay_total'] * $goods_info['group_staff_rebate'];
  195 + } elseif ($parent_info['type'] == 4) {
  196 + //代理返利
  197 + $rebateMoney = $order['pay_total'] * $goods_info['group_agency_rebate'];
  198 + }
  199 + $rebatedata = [
  200 + 'user_id' => $user_info['pid'],
  201 + 'type' => 4, //佣金收入(返利)
  202 + 'money' => $rebateMoney,
  203 + 'before' => $parent_info['money'],
  204 + 'after' => $parent_info['money'] + $rebateMoney,
  205 + 'createtime' => time(),
  206 + ];
  207 + Db::name('user_money_log')->insert($rebatedata);
  208 + Db::name('user')->where('id', $parent_info['id'])->setInc('money', $rebateMoney);
  209 + }
  210 + //拼团表
  211 + $teamData = [
  212 + 'goods_id' => $goods_id,
  213 + 'user_id' => $user_id,
  214 + 'user_avatar' => $user_info['avatar'],
  215 + 'user_nickname' => $user_info['nickname'],
  216 + 'grouptime' => strtotime('+' . $goods_info['grouptime'] . 'hour'),
  217 + 'group_num' => $goods_info['group_num'],
  218 + 'status' => 1,
  219 + 'createtime' => time(),
  220 + 'updatetime' => time(),
  221 + ];
  222 + $teamId = Db::name('team_group')->insertGetId($teamData);
  223 + //给订单添加拼团id
  224 + $this->where('id',$order['id'])->update(['team_group_id'=>$teamId,'team_group_status'=>1]);
  225 + //拼团详情表
  226 + $teamInfoData = [
  227 + 'team_group_id' => $teamId,
  228 + 'order_id' => $order['id'],
  229 + 'user_id' => $user_id,
  230 + 'user_avatar' => $user_info['avatar'],
  231 + 'user_nickname' => $user_info['nickname'],
  232 + 'is_head' => 1,
  233 + 'createtime' => time(),
  234 + 'updatetime' => time(),
  235 + ];
  236 +
  237 + Db::name('team_group_info')->insertGetId($teamInfoData);
  238 + }
  239 +
  240 + public function joinTeam($order, $user_id)
  241 + {
  242 + $user_info = Db::name('user')->where(['id' => $user_id])->field('id,avatar,nickname,pid')->find();
  243 + $goods_id = Db::name('team_group')->where(['id' => $order['team_group_id']])->value('goods_id');
  244 + $goods_info = Db::name('goods')->where(['id' => $goods_id])->field('group_vip_rebate,group_staff_rebate,group_agency_rebate,group_price')->find();
  245 +
  246 + if (!empty($user_info['pid'])) { //返利
  247 + if (!empty($order['postage_total'])) $order['pay_total'] -= $order['postage_total'];
  248 + $parent_info = Db::name('user')->where('id', $user_info['pid'])->find();
  249 + //普通用户无返利
  250 + if ($parent_info['type'] == 2) {
  251 + //会员返利
  252 + $rebateMoney = $order['pay_total'] * $goods_info['group_vip_rebate'];
  253 + } elseif ($parent_info['type'] == 3) {
  254 + //员工返利
  255 + $rebateMoney = $order['pay_total'] * $goods_info['group_staff_rebate'];
  256 + } elseif ($parent_info['type'] == 4) {
  257 + //代理返利
  258 + $rebateMoney = $order['pay_total'] * $goods_info['group_agency_rebate'];
  259 + }
  260 + $rebatedata = [
  261 + 'user_id' => $user_info['pid'],
  262 + 'type' => 4, //佣金收入(返利)
  263 + 'money' => $rebateMoney,
  264 + 'before' => $parent_info['money'],
  265 + 'after' => $parent_info['money'] + $rebateMoney,
  266 + 'createtime' => time(),
  267 + ];
  268 + Db::name('user_money_log')->insert($rebatedata);
  269 + Db::name('user')->where('id', $parent_info['id'])->setInc('money', $rebateMoney);
  270 + }
  271 + //添加拼团详情
  272 + $teamInfoData = [
  273 + 'team_group_id' => $order['team_group_id'],
  274 + 'order_id' => $order['id'],
  275 + 'user_id' => $user_id,
  276 + 'user_avatar' => $user_info['avatar'],
  277 + 'user_nickname' => $user_info['nickname'],
  278 + 'is_head' => 2,
  279 + 'createtime' => time(),
  280 + 'updatetime' => time(),
  281 + ];
  282 + Db::name('team_group_info')->insertGetId($teamInfoData);
  283 +
  284 + $teamInfo = Db::name('team_group')->where('id', $order['team_group_id'])->field('group_num,grouptime')->find();
  285 + $count = Db::name('team_group_info')->where('team_group_id', $order['team_group_id'])->count();
  286 + //判断是否拼团成功
  287 + if ($teamInfo['group_num'] == $count) {
  288 + //拼团成功
  289 + $ids = Db::name('team_group_info')->where(['team_group_id'=>$order['team_group_id']])->column('user_id');
  290 + Db::name('team_group')->where('id', $order['team_group_id'])->update(['status'=>2]);
  291 + $this->where(['team_group_id' => $order['team_group_id'],'user_id'=>['in',$ids]])->update(['team_group_status' => 2]);
  292 + $this->where(['team_group_id' => $order['team_group_id'],'user_id'=>['not in',$ids]])->update(['team_group_status' => 3]);
  293 +
  294 + }
  295 + }
  296 +
  297 + public function orderCancel($order){
  298 + $userTicketModel = new UserTicket();
  299 + $ticketcodeModel = new Ticketcode();
  300 +
  301 + Db::startTrans();
  302 + try {
  303 + $this->where('id',$order['id'])->update(['status'=>-1]);
  304 + if (!empty($order['user_ticket_id'])){
  305 + $userTicketModel->where('id',$order['user_ticket_id'])->update(['status'=>'1','updatetime'=>time()]);
  306 + }
  307 + if (!empty($order['ticketcode_id'])){
  308 + $ticketcodeModel->where('id',$order['ticketcode_id'])->update(['user_id'=>'','updatetime'=>time()]);
  309 + }
159 Db::commit(); 310 Db::commit();
160 return true; 311 return true;
161 312
  1 +<?php
  2 +
  3 +namespace app\api\model;
  4 +
  5 +use think\Model;
  6 +
  7 +
  8 +class TeamGroup extends Model
  9 +{
  10 + public function selectData($where, $limit)
  11 + {
  12 + $where['status'] = 1;
  13 + $data = $this
  14 + ->where($where)
  15 + ->limit($limit)
  16 + ->order('createtime desc')
  17 + ->field('id team_group_id,user_avatar,user_nickname,grouptime,group_num ')
  18 + ->select();
  19 + return $data;
  20 + }
  21 +}
  1 +<?php
  2 +
  3 +namespace app\api\model;
  4 +
  5 +use think\Model;
  6 +
  7 +
  8 +class TeamGroupInfo extends Model
  9 +{
  10 + public function selectData($where)
  11 + {
  12 + $data = $this
  13 + ->where($where)
  14 + ->order('createtime ASC')
  15 + ->field('id,team_group_id,user_avatar,user_nickname')
  16 + ->select();
  17 + return $data;
  18 + }
  19 +}
此 diff 太大无法显示。