Orders.php 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use App\Models\Orders as Model;
  4. use App\Models\Product;
  5. use App\Models\Product\Skus as ProductSkus;
  6. use App\Http\Requests\Api\Orders as Request;
  7. use App\Models\Business;
  8. use App\Models\City;
  9. use App\Models\Custom;
  10. use App\Models\CustomAddr;
  11. use App\Models\CustomCoupon;
  12. use App\Models\CustomScore;
  13. use App\Models\OrdersAddr;
  14. use App\Models\OrdersProduct;
  15. use App\Models\Regiment;
  16. use App\Models\RegimentActive;
  17. use App\Models\RegimentRecord;
  18. use App\Models\ShopCart;
  19. use Illuminate\Support\Facades\DB;
  20. use App\Models\WeiBan\Tags as WeiBanTags;
  21. /**
  22. * 订单接口
  23. *
  24. * @author 刘相欣
  25. *
  26. * */
  27. class Orders extends Api{
  28. /**
  29. * 创建订单 /api/orders/create
  30. *
  31. * */
  32. public function create(Request $request,Custom $Custom,City $City,Model $Model,WeiBanTags $WeiBanTags,OrdersAddr $OrdersAddr,OrdersProduct $OrdersProduct,Product $Product,ProductSkus $ProductSkus,CustomCoupon $CustomCoupon,ShopCart $ShopCart,CustomAddr $CustomAddr,CustomScore $CustomScore){
  33. // 接口验签
  34. // $this->verify_sign();
  35. // 验证参数
  36. $request->scene('create')->validate();
  37. // 检查登录
  38. $uid = $this->checkLogin();
  39. // 接收参数
  40. $isCart = request('is_cart',0);
  41. $productList = request('product_list','[]');
  42. $customCouponId = request('custom_coupon_id',0);
  43. $addrId = request('addr_id',0);
  44. // 如果不存在数据
  45. if( !$addrId ) return json_send(['code'=>'error','msg'=>'请选择收货地址','data'=>['error'=>'请选择收货地址']]);
  46. // 解码购买信息
  47. $buyList = json_decode($productList,true);
  48. // 如果不存在数据
  49. if( empty($buyList) ) return json_send(['code'=>'error','msg'=>'没有需下单的产品','data'=>['error'=>'产品列表为空']]);
  50. // 选择地址
  51. $addr = $CustomAddr->getOne($addrId);
  52. // 如果不存在数据
  53. if( !$addr ) return json_send(['code'=>'error','msg'=>'地址有误,请核对','data'=>['error'=>'没有找到对应的地址']]);
  54. // 重组数据
  55. $addr = ['contact_name'=>$addr['contact_name'],'contact_shop'=>$addr['contact_shop'],'contact_phone'=>$addr['contact_phone'],'contact_province'=>$addr['contact_province'],'contact_city'=>$addr['contact_city'],'contact_area'=>$addr['contact_area'],'contact_addr'=>$addr['contact_addr']];
  56. // 获取客户城市ID
  57. $custom = $Custom->getOne($uid);
  58. // 如果不存在的话
  59. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'用户不存在,请重新登录','data'=>['error'=>'用户不存在,请重新登录']]);
  60. // 如果不存在的话
  61. if( !$custom['city_id'] ) return json_send(['code'=>'error','msg'=>'请选择所在城市后下单','data'=>['error'=>'请选择所在城市后下单']]);
  62. // 获取城市ID
  63. $cityId = $custom['city_id'];
  64. $cityName = $City->getOne($cityId,'name');
  65. $pid = $City->getOne($cityId,'pid');
  66. $province = $City->getOne($pid,'name');
  67. // 判断选择的城市名称是不是一致
  68. if( $cityName != $addr['contact_city'] ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.$province.'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  69. // 商品购买数量
  70. $buyNum = [];
  71. // 循环购买信息
  72. foreach ($buyList as $key => $value) {
  73. // 获取每个商品的总量
  74. $buyNum[$value['product_id']] = isset($buyNum[$value['product_id']]) ? ($buyNum[$value['product_id']] + $value['buy_num']) : $value['buy_num'];
  75. }
  76. // 查询用户标签
  77. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  78. // 查询产品信息
  79. $productList = $Product->getListByIds(array_column($buyList,'product_id'));
  80. $skusList = $ProductSkus->getListByIds(array_column($buyList,'product_skuid'));
  81. // 当前时间
  82. $time = time();
  83. // 产品以商业公司分组,方便写入订单
  84. $orderProduct = [];
  85. // 产品价格同级,用于优惠券计算
  86. $productPrice = [];
  87. // 循环处理购买信息
  88. foreach ($buyList as $buyInfo) {
  89. // 如果产品不存在
  90. if( empty($productList[$buyInfo['product_id']]) ) return json_send(['code'=>'error','msg'=>'产品不存在或已下架','data'=>['error'=>'产品不存在或已下架=>'.$buyInfo['product_id']]]);
  91. // 获取产信息
  92. $productInfo = $productList[$buyInfo['product_id']];
  93. // 判断是不是可以参与
  94. if( $productInfo['tag_scope'] ) {
  95. // 解析数组
  96. $productInfo['tag_scope'] = explode(',',$productInfo['tag_scope']);
  97. // 标签范围限定时,默认不能参与
  98. $allowJoin = 0;
  99. // 判断标签是不是存在
  100. foreach ($tags as $value) {
  101. // 标签范围内,允许参加
  102. if( in_array($value['name'],$productInfo['tag_scope']) ) $allowJoin = 1;
  103. }
  104. // 如果不能参与
  105. if( !$allowJoin ) return json_send(['code'=>'error','msg'=>'不在 '.$productInfo['product_name'].' 参与范围','data'=>['error'=>'不在标签范围内']]);
  106. }
  107. // 判断是不是可以参与
  108. if( $productInfo['tag_exclude'] ) {
  109. // 解析数组
  110. $productInfo['tag_exclude'] = explode(',',$productInfo['tag_exclude']);
  111. // 判断标签是不是存在
  112. foreach ($tags as $value) {
  113. // 标签排除范围内,不允许参加
  114. if( in_array($value['name'],$productInfo['tag_exclude']) ) return json_send(['code'=>'error','msg'=>'不在 '.$productInfo['product_name'].' 可参与范围','data'=>['error'=>'用户在标签排除范围']]);
  115. }
  116. }
  117. // 如果产品限购
  118. if( $productInfo['quota'] ) {
  119. // 是否在限购时间,当前时间大于开始时间,并且小于结束时间
  120. if( $productInfo['quota_start'] <= $time && $time <= $productInfo['quota_end'] ){
  121. // 通过时间查询商品的购买总数
  122. $total = $OrdersProduct->query()->where([['custom_uid','=',$uid],['product_id','=',$productInfo['id']],['insert_time','>=',$productInfo['quota_start']],['insert_time','<=',$productInfo['quota_end']]])->sum('buy_num');
  123. // 判断限购数量
  124. $total = $buyNum[$buyInfo['product_id']] + $total;
  125. // 如果超过数量
  126. if( $total > $productInfo['quota'] ) return json_send(['code'=>'error','msg'=>'限购'.$productInfo['quota'].'套-'.$productInfo['product_name'],'data'=>['error'=>'已超限=>'.($total - $productInfo['quota'])]]);
  127. }
  128. }
  129. // 如果存在SKU
  130. if( $buyInfo['product_skuid'] ) {
  131. // 判断SKU信息存不存在
  132. if( empty($skusList[$buyInfo['product_skuid']]) ) return json_send(['code'=>'error','msg'=>'该产品规格不存在或已下架','data'=>['error'=>'SKU不存在或已下架=>'.$buyInfo['product_skuid']]]);
  133. // 产品ID不匹配的话
  134. if( $skusList[$buyInfo['product_skuid']]['product_id'] != $buyInfo['product_id'] ) return json_send(['code'=>'error','msg'=>'该产品规格不存在或已下架','data'=>['error'=>'SKU不匹配=>'.$buyInfo['product_skuid']]]);
  135. // 如果SKU存在,合并产品信息
  136. $productInfo = array_merge($productInfo,$skusList[$buyInfo['product_skuid']]);
  137. // 需要扣除的库存
  138. $skusList[$buyInfo['product_skuid']]['decr'] = empty($skusList[$buyInfo['product_skuid']]['decr']) ? $buyInfo['buy_num'] : $skusList[$buyInfo['product_skuid']]['decr'] + $buyInfo['buy_num'];
  139. }
  140. // 需要扣除的库存
  141. $productList[$buyInfo['product_id']]['decr'] = empty($productList[$buyInfo['product_id']]['decr']) ? $buyInfo['buy_num'] : $productList[$buyInfo['product_id']]['decr'] + $buyInfo['buy_num'];
  142. // 判断库存
  143. if( $productInfo['stock'] < $productList[$buyInfo['product_id']]['decr'] ) return json_send(['code'=>'error','msg'=>$productInfo['product_name'].'-库存不足','data'=>['error'=>'产品库存不足=>'.$buyInfo['product_id']]]);
  144. // 计算价值
  145. $priceTotal = $buyInfo['buy_num'] * $productInfo['price'];
  146. // 购买信息
  147. $buyInfo = ['is_rebate'=>0,'custom_uid'=>$uid,'business_id'=>$productInfo['business_id'],'product_id'=>$buyInfo['product_id'],'buy_num'=>$buyInfo['buy_num'],'price_total'=>$priceTotal,'pay_total'=>$priceTotal,'coupon_total'=>0,'product_name'=>$productInfo['product_name'],'sku_attr_names'=>$productInfo['sku_attr_names'],'product_thumb'=>$productInfo['product_thumb']];
  148. // 获取信息
  149. if( !isset($orderProduct[$buyInfo['business_id']]) ) $orderProduct[$buyInfo['business_id']] = ['business_id'=>$buyInfo['business_id'],'custom_uid'=>$buyInfo['custom_uid'],'price_total'=>0,'pay_total'=>0,'coupon_total'=>0,'product_list'=>[]];
  150. // 订单产品
  151. $orderProduct[$buyInfo['business_id']]['price_total'] += $buyInfo['price_total'];
  152. $orderProduct[$buyInfo['business_id']]['pay_total'] += $buyInfo['price_total'];
  153. $orderProduct[$buyInfo['business_id']]['product_list'][] = $buyInfo;
  154. // 商品优惠信息不存在,创建
  155. if( !isset($productPrice[$buyInfo['product_id']]) ) $productPrice[$buyInfo['product_id']] = ['price_total'=>0,'rebate_price'=>0];
  156. // 计算总价
  157. $productPrice[$buyInfo['product_id']]['price_total'] = $productPrice[$buyInfo['product_id']]['price_total'] + $priceTotal;
  158. }
  159. // 优惠券数据
  160. $couponRebate = $CustomCoupon->getRebatePrice($customCouponId,$uid,$productPrice);
  161. // 判断是否使用了优惠券
  162. $usedCoupon = $couponRebate['is_used'];
  163. // 获取优惠券扣减金额
  164. $productPrice = $couponRebate['product_price'];
  165. // 获取优惠券赠品信息
  166. $rebateProduct = $couponRebate['rebate_product'];
  167. // 组合订单数据
  168. foreach ($orderProduct as $key => $order) {
  169. // 判断哪一家的赠品
  170. if( isset($rebateProduct[$order['business_id']]) ){
  171. // 循环赠品
  172. foreach ( $rebateProduct[$order['business_id']] as $value) {
  173. // 没有对应的产品
  174. if( !isset($productList[$value['product_id']]) ) $productList[$value['product_id']] = ['id'=>$value['id'],'stock'=>$value['stock']];
  175. // 判断库存,如果库存不足,只赠送最后的库存
  176. if( $productList[$value['product_id']]['stock'] <= $value['buy_num'] ) $value['buy_num'] = $productList[$value['product_id']]['stock'];
  177. // 库存扣减
  178. $productList[$value['product_id']]['decr'] = empty($productList[$value['product_id']]['decr']) ? $value['buy_num'] : $productList[$value['product_id']]['decr'] + $value['buy_num'];
  179. // 追加到订单表
  180. $order['product_list'][] = ['is_rebate'=>1,'custom_uid'=>$uid,'business_id'=>$value['business_id'],'product_id'=>$value['product_id'],'buy_num'=>$value['buy_num'],'price_total'=>$value['price_total'],'pay_total'=>$value['pay_total'],'coupon_total'=>$value['coupon_total'],'product_name'=>$value['product_name'],'sku_attr_names'=>$value['sku_attr_names'],'product_thumb'=>$value['product_thumb']];
  181. }
  182. }
  183. // 计算总价格
  184. foreach ($order['product_list'] as $k=>$product) {
  185. // 商品不存在,不进行扣减
  186. if( empty($productPrice[$product['product_id']]['rebate_price']) ) {
  187. // 重组
  188. $order['product_list'][$k] = $product;
  189. continue;
  190. }
  191. // 总优惠增加
  192. $order['coupon_total'] = $order['coupon_total'] + $productPrice[$product['product_id']]['rebate_price'];
  193. // 当前商品的优惠折扣计算
  194. $product['coupon_total'] = number_format( $productPrice[$product['product_id']]['rebate_price'] * ($product['price_total'] / $productPrice[$product['product_id']]['price_total']) , 2 , '.' ,'');
  195. // 成交小计
  196. $product['pay_total'] = $product['pay_total'] - $product['coupon_total'];
  197. // 重组
  198. $order['product_list'][$k] = $product;
  199. }
  200. // 成交总价
  201. $order['pay_total'] = $order['pay_total'] - $order['coupon_total'];
  202. // 赠送积分
  203. $order['order_score'] = (config('order_score_send',0) && floor( $order['pay_total'] * 1 ) > 0 ) ? floor( $order['pay_total'] * 1 ) : 0;
  204. // 成交总价
  205. $order['custom_uid'] = $uid;
  206. // 重组
  207. $orderProduct[$key] = $order;
  208. }
  209. // 组合数据,写入订单表,子表
  210. DB::beginTransaction();
  211. // 写入数据
  212. try {
  213. // 扣减商品库存
  214. foreach ($productList as $key => $value) {
  215. // 扣减库存
  216. $result = $Product->edit($value['id'],['stock'=>DB::raw('stock+-'.$value['decr'])]);
  217. // 判断结果
  218. if( !$result ) {
  219. // 回退数据
  220. DB::rollBack();
  221. // 错误提示
  222. return json_send(['code'=>'error','msg'=>'库存扣减失败','data'=>['error'=>'库存扣减失败']]);
  223. }
  224. }
  225. // 扣减商品库存
  226. foreach ($skusList as $key => $value) {
  227. // 扣减库存
  228. $result = $ProductSkus->edit($value['sku_id'],['stock'=>DB::raw('stock+-'.$value['decr'])]);
  229. // 判断结果
  230. if( !$result ) {
  231. // 回退数据
  232. DB::rollBack();
  233. // 错误提示
  234. return json_send(['code'=>'error','msg'=>'库存扣减失败','data'=>['error'=>'库存扣减失败']]);
  235. }
  236. }
  237. // 循环订单数据
  238. foreach ($orderProduct as $order) {
  239. // 先获取产品列表,并去除key
  240. $productItem = array_values($order['product_list']);
  241. // 删除非必要数据
  242. unset($order['product_list']);
  243. // 创建总订单
  244. $orderId = $Model->add($order);
  245. // 如果订单写入失败
  246. if( !$orderId ) {
  247. // 回退数据
  248. DB::rollBack();
  249. // 错误提示
  250. return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'订单创建失败']]);
  251. }
  252. // 创建子订单
  253. foreach ($productItem as $key=>$value) {
  254. // 增加订单ID
  255. $value['order_id'] = $orderId;
  256. // 增加订单ID
  257. $value['insert_time']= $time;
  258. $value['update_time']= $time;
  259. // 结果
  260. $productItem[$key] = $value;
  261. }
  262. // 写入子表
  263. $result = $OrdersProduct->query()->insert($productItem);
  264. // 如果扣减失败
  265. if( !$result ) {
  266. // 回退数据
  267. DB::rollBack();
  268. // 提示信息
  269. return json_send(['code'=>'error','msg'=>'子订单创建失败','data'=>['error'=>'子订单创建失败']]);
  270. }
  271. // 写入订单地址表
  272. $addr['order_id'] = $orderId;
  273. // 写入订单地址表
  274. $result = $OrdersAddr->add($addr);
  275. // 地址写入失败
  276. if( !$result ) {
  277. // 回退数据
  278. DB::rollBack();
  279. // 提示信息
  280. return json_send(['code'=>'error','msg'=>'地址写入失败','data'=>['error'=>'地址写入失败']]);
  281. }
  282. // 赠送积分
  283. if( $order['order_score'] > 0 ) $CustomScore->trade($order['custom_uid'],$orderId,$order['order_score'],5,1);
  284. // 如果使用了优惠券
  285. if( $usedCoupon ) $CustomCoupon->edit($usedCoupon,['status'=>1,'order_id'=>$orderId]);
  286. // 购物车
  287. if( $isCart ) $ShopCart->query()->where([['custom_uid','=',$uid]])->whereIn('skuid',array_column($buyList,'product_skuid'))->delete();
  288. }
  289. // 自动发放优惠券
  290. $this->autoCoupon($uid);
  291. // 提交数据
  292. DB::commit();
  293. // 返回结果
  294. return json_send(['code'=>'success','msg'=>'下单成功','data'=>['id'=>null]]);
  295. // 返回结果
  296. } catch (\Throwable $th) {
  297. // 回退数据
  298. DB::rollBack();
  299. // 判断结果,如果库存扣减失败的话
  300. if( stripos($th->getMessage(),'UNSIGNED') ) return json_send(['code'=>'error','msg'=>'库存不足','data'=>['error'=>'产品库存扣减失败']]);
  301. // 下单失败提示
  302. return json_send(['code'=>'error','msg'=>'系统异常,请稍后再试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  303. }
  304. }
  305. /**
  306. * 获取列表 /api/orders/get_list
  307. *
  308. * @param string $name 产品名称
  309. * @param int $page 页码,默认1
  310. * @param int $limit 每页条数,默认10条
  311. *
  312. * */
  313. public function get_list(Request $request,Model $Model,OrdersProduct $OrdersProduct,Business $Business){
  314. // 接口验签
  315. // $this->verify_sign();
  316. // 验证参数
  317. $request->scene('get_list')->validate();
  318. // 检查登录
  319. $uid = $this->checkLogin();
  320. // 接收参数
  321. $status = request('status',0);
  322. $limit = request('limit',10);
  323. // 显示
  324. $map = [['custom_uid','=',$uid]];
  325. // 查询状态
  326. if( $status ) $map[] = ['status','=',$status];
  327. // 查询
  328. $Paginator = $Model->query()->where($map)->orderByDesc('id')->paginate($limit,['id','pay_total','status','price_total','coupon_total','business_id','pay_total','weizan_orderid','insert_time']);
  329. // 订单产品
  330. $productList = $OrdersProduct->query()->whereIn('order_id', array_column($Paginator->items(),'id'))->select(['id as item_id','order_id','product_id','buy_num','is_rebate','sku_attr_names as product_spec','product_name','product_thumb'])->get()->toArray();
  331. // 循环处理
  332. foreach ($Paginator as $key => $order) {
  333. // 商品列表
  334. $itemList = [];
  335. // 返回结果
  336. foreach ($productList as $item) {
  337. // 产品图路径
  338. $item['product_thumb'] = path_compat($item['product_thumb']);
  339. // 如果是订单的
  340. if( $item['order_id'] == $order['id'] ) $itemList[] = $item;
  341. }
  342. // 获取子列表
  343. $order['state'] = (string) $Model->getState($order['status'],'state');
  344. // 获取子列表
  345. $order['business_name'] = (string) ($order['business_id'] ? $Business->getOne($order['business_id'],'name') : ($order['weizan_orderid'] ? '微赞订单' : ''));
  346. // 获取子列表
  347. $order['contents_class']= 0;
  348. // 获取子列表
  349. $order['product_list'] = $itemList;
  350. // 重组
  351. $Paginator[$key] = $order;
  352. }
  353. // 获取数据
  354. $data['total'] = $Paginator->total();
  355. $data['current_page'] = $Paginator->currentPage();
  356. $data['per_page'] = $Paginator->perPage();
  357. $data['last_page'] = $Paginator->lastPage();
  358. $data['data'] = $Paginator->items();
  359. // 返回结果
  360. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  361. }
  362. /**
  363. * 自动发放优惠券
  364. *
  365. */
  366. private function autoCoupon($uid){
  367. // 模型实例
  368. $Rule = new \App\Models\CouponRewardRule();
  369. // 获取配置列表
  370. $ruleList = $Rule->getList();
  371. // 如果没有信息的话
  372. if( !$ruleList ) return ['success'=>'暂无活动'];
  373. // 其他实例
  374. $RuleProduct = new \App\Models\CouponRewardProduct();
  375. $OrdersProduct = new \App\Models\OrdersProduct();
  376. $Custom = new \App\Models\Custom();
  377. $Coupon = new \App\Models\Coupon();
  378. $CustomCoupon = new \App\Models\CustomCoupon();
  379. // 获取客户城市ID
  380. $customCityId = (int) $Custom->getValue($uid,'city_id');
  381. // 循环配置列表
  382. foreach ( $ruleList as $value ) {
  383. // 如果存在城市范围,并且不在城市范围,不参与这个活动
  384. if( $value['city_ids'] && !in_array($customCityId,explode(',',$value['city_ids'])) ) continue;
  385. // 未到开始时间
  386. if( $value['start_time'] > time() ) continue;
  387. // 通过配置ID获取对应的商品范围
  388. $productList = $RuleProduct->getListByRule($value['id']);
  389. // 如果不存在产品范围,跳过
  390. if( !$productList ) continue;
  391. // 获取客户 规定时段内订单的商品ID以及购买数量
  392. $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->get(['product_id','buy_num'])->toArray();
  393. // 如果没有订单总数
  394. if( !$orderList ) continue;
  395. // 计算商品总量
  396. $total = 0;
  397. // 循环商品范围
  398. foreach ($productList as $scope) {
  399. // 循环订单产品
  400. foreach ($orderList as $order) {
  401. // 如果产品不相等
  402. if( $scope['product_id'] != $order['product_id'] ) continue;
  403. // 相等的计算总量
  404. $total += $scope['product_units'] * $order['buy_num'];
  405. }
  406. }
  407. // 判断总数是不是达标
  408. if( $total < $value['std_num'] ) continue;
  409. // 达标的是否已经发送过优惠券
  410. $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['status']);
  411. // 已经发过优惠券的,不发
  412. if( $havaCoupon ) continue;
  413. // 获取优惠券的可用时间
  414. $expTime = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('exp_time');
  415. // 时间转时间
  416. $expTime = $Coupon->getExpTime($expTime);
  417. // 发送优惠券
  418. $CustomCoupon->add(['coupon_id'=>$value['coupon_id'],'custom_uid'=>$uid,'exp_time'=>$expTime]);
  419. }
  420. // 返回成功
  421. return ['success'=>'操作成功'];
  422. }
  423. /**
  424. * 取消 /api/orders/cancel
  425. *
  426. * */
  427. public function cancel( Request $request, Model $Model,OrdersProduct $OrdersProduct,CustomScore $CustomScore){
  428. // 验证参数
  429. $request->scene('cancel')->validate();
  430. // 检查登录
  431. $uid = $this->checkLogin();
  432. // 接收参数
  433. $id = request('id',0);
  434. $status = 4;
  435. // 获取产品和数量
  436. $oldData = $Model->query()->where([['id','=',$id],['custom_uid','=',$uid]])->first(['id','order_score','status','custom_uid','insert_time']);
  437. // 如果用户不存在
  438. if( !$oldData ) return json_send(['code'=>'error','msg'=>'订单不存在']);
  439. // 如果已经取消
  440. if( $oldData['status'] == 4 ) return json_send(['code'=>'error','msg'=>'订单已取消']);
  441. // 组合数据,写入订单表,子表
  442. DB::beginTransaction();
  443. try{
  444. // 查询数据
  445. $result = $Model->setOrderStatus($id,$status,$OrdersProduct);
  446. // 提示新增失败
  447. if( isset($result['error']) ) {
  448. // 回退数据
  449. DB::rollBack();
  450. // 提示信息
  451. return json_send(['code'=>'error','msg'=>$result['error'],'data'=>['error'=>$result['error']]]);
  452. }
  453. if( $status == 4 ){
  454. // 取消积分
  455. if( $oldData['order_score'] > 0 ) {
  456. // 如果扣减失败
  457. $result = $CustomScore->trade($oldData['custom_uid'],$oldData['id'],($oldData['order_score']*-1),6,1);
  458. // 提示新增失败
  459. if( isset($result['error']) ) {
  460. // 回退数据
  461. DB::rollBack();
  462. // 提示信息
  463. return json_send(['code'=>'error','msg'=>'取消赠送积分失败','data'=>['error'=>$result['error']]]);
  464. }
  465. }
  466. // 取消关联订单
  467. $result = $Model->cancelRelate($oldData['insert_time'],$oldData['custom_uid'],$OrdersProduct,$CustomScore);
  468. // 提示新增失败
  469. if( isset($result['error']) ) {
  470. // 回退数据
  471. DB::rollBack();
  472. // 提示信息
  473. return json_send(['code'=>'error','msg'=>'取消关联订单失败','data'=>['error'=>$result['error']]]);
  474. }
  475. }
  476. // 提交数据
  477. DB::commit();
  478. // 告知结果
  479. return json_send(['code'=>'success','msg'=>'取消成功']);
  480. // 返回结果
  481. } catch (\Throwable $th) {
  482. // 回退数据
  483. DB::rollBack();
  484. // 下单失败提示
  485. return json_send(['code'=>'error','msg'=>'取消失败','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  486. }
  487. }
  488. /**
  489. * 获取订单子数据 /api/orders/get_item
  490. *
  491. * @param string $name 产品名称
  492. * @param int $page 页码,默认1
  493. * @param int $limit 每页条数,默认10条
  494. *
  495. * */
  496. public function get_item(Request $request,Model $Model,OrdersProduct $OrdersProduct,Business $Business){
  497. // 接口验签
  498. // $this->verify_sign();
  499. // 验证参数
  500. $request->scene('get_item')->validate();
  501. // 检查登录
  502. $uid = $this->checkLogin();
  503. // 接收参数
  504. $id = request('id',0);
  505. // 查询
  506. $orderInfo = $Model->query()->where([['id','=',$id],['custom_uid','=',$uid]])->first(['id','pay_total','status','price_total','coupon_total','business_id','pay_total','weizan_orderid','insert_time']);
  507. // 如果用户不存在
  508. if( !$orderInfo ) return json_send(['code'=>'error','msg'=>'订单不存在']);
  509. // 转数组
  510. $orderInfo = $orderInfo->toArray();
  511. // 订单产品
  512. $orderInfo['product_list'] = $OrdersProduct->query()->where([['order_id','=',$id]])->select(['id as item_id','order_id','product_id','buy_num','is_rebate','sku_attr_names as product_spec','product_name','product_thumb'])->get()->toArray();
  513. // 循环处理
  514. foreach ($orderInfo['product_list'] as $key => $item) {
  515. // 产品图路径
  516. $item['product_thumb'] = path_compat($item['product_thumb']);
  517. // 重组
  518. $orderInfo['product_list'][$key] = $item;
  519. }
  520. // 获取子列表
  521. $orderInfo['state'] = (string) $Model->getState($orderInfo['status'],'state');
  522. // 获取子列表
  523. $orderInfo['business_name'] = (string) ($orderInfo['business_id'] ? $Business->getOne($orderInfo['business_id'],'name') : ($orderInfo['weizan_orderid'] ? '微赞订单' : ''));
  524. // 返回结果
  525. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$orderInfo]);
  526. }
  527. /**
  528. * 创建拼团订单 /api/orders/create_regiment
  529. *
  530. * */
  531. public function create_regiment(Request $request,Custom $Custom,City $City,Model $Model,WeiBanTags $WeiBanTags,OrdersAddr $OrdersAddr,OrdersProduct $OrdersProduct,Product $Product,ProductSkus $ProductSkus,CustomCoupon $CustomCoupon,ShopCart $ShopCart,CustomAddr $CustomAddr,CustomScore $CustomScore){
  532. // 接口验签
  533. // $this->verify_sign();
  534. // 验证参数
  535. $request->scene('create')->validate();
  536. // 检查登录
  537. $uid = $this->checkLogin();
  538. // 接收参数
  539. $isCart = request('is_cart',0);
  540. $productList = request('product_list','[]');
  541. $customCouponId = request('custom_coupon_id',0);
  542. $addrId = request('addr_id',0);
  543. $regimentId = request('regiment_id',0);
  544. $regimentActiveId = request('regiment_active_id',0);
  545. $btnType = request('btn_type',0);
  546. // 如果不存在数据
  547. if( !$addrId ) return json_send(['code'=>'error','msg'=>'请选择收货地址','data'=>['error'=>'请选择收货地址']]);
  548. // 解码购买信息
  549. $buyList = json_decode($productList,true);
  550. // 如果不存在数据
  551. if( empty($buyList) ) return json_send(['code'=>'error','msg'=>'没有需下单的产品','data'=>['error'=>'产品列表为空']]);
  552. $time = time();
  553. // 选择地址
  554. $addr = $CustomAddr->getOne($addrId);
  555. // 如果不存在数据
  556. if( !$addr ) return json_send(['code'=>'error','msg'=>'地址有误,请核对','data'=>['error'=>'没有找到对应的地址']]);
  557. // 重组数据
  558. $addr = ['contact_name'=>$addr['contact_name'],'contact_shop'=>$addr['contact_shop'],'contact_phone'=>$addr['contact_phone'],'contact_province'=>$addr['contact_province'],'contact_city'=>$addr['contact_city'],'contact_area'=>$addr['contact_area'],'contact_addr'=>$addr['contact_addr']];
  559. // 获取客户城市ID
  560. $custom = $Custom->getOne($uid);
  561. // 如果不存在的话
  562. if( !$custom ) return json_send(['code'=>'no_login','msg'=>'用户不存在,请重新登录','data'=>['error'=>'用户不存在,请重新登录']]);
  563. // 如果不存在的话
  564. if( !$custom['city_id'] ) return json_send(['code'=>'error','msg'=>'请选择所在城市后下单','data'=>['error'=>'请选择所在城市后下单']]);
  565. // 获取城市ID
  566. $cityId = $custom['city_id'];
  567. $cityName = $City->getOne($cityId,'name');
  568. $pid = $City->getOne($cityId,'pid');
  569. $province = $City->getOne($pid,'name');
  570. // 判断选择的城市名称是不是一致
  571. if( $cityName != $addr['contact_city'] ) return json_send(['code'=>'error','msg'=>'收货地址请选择'.$province.'/'.$cityName,'data'=>['error'=>'收货地址需与您所选城市一致']]);
  572. //获取团活动信息
  573. $regimentActiveInfo = RegimentActive::query()
  574. ->where([['id','=',$regimentActiveId],['status','=',1],['start_time','<=',$time],['end_time','>=',$time]])
  575. ->first();
  576. if (!$regimentActiveInfo) return json_send(['code'=>'error','msg'=>'拼团活动不存在','data'=>['error'=>'拼团活动不存在']]);
  577. //是否符合开团条件
  578. if($btnType == 3){
  579. if ($regimentActiveInfo['open_people'] == 1){
  580. if ($custom['insert_time'] > $regimentActiveInfo['start_time']) return json_send(['code'=>'error','msg'=>'此活动只允许老用户开团','data'=>['error'=>'此活动只允许老用户开团']]);
  581. }
  582. if ($regimentActiveInfo['open_people'] == 2){
  583. if ($custom['insert_time'] < $regimentActiveInfo['start_time']) return json_send(['code'=>'error','msg'=>'此活动只允许新用户开团','data'=>['error'=>'此活动只允许新用户开团']]);
  584. }
  585. }
  586. //是否符合参团条件
  587. if($btnType == 4){
  588. if ($regimentActiveInfo['partake_people'] == 1){
  589. if ($custom['insert_time'] > $regimentActiveInfo['start_time']) return json_send(['code'=>'error','msg'=>'此活动只允许老用户参与','data'=>['error'=>'此活动只允许老用户开团']]);
  590. }
  591. if ($regimentActiveInfo['open_people'] == 2){
  592. if ($custom['insert_time'] < $regimentActiveInfo['start_time']) return json_send(['code'=>'error','msg'=>'此活动只允许新用户参与','data'=>['error'=>'此活动只允许新用户开团']]);
  593. }
  594. //查询团信息
  595. $regimentInfo = Regiment::query()
  596. ->where([['id','=',$regimentId],['status','=',1],['start_time','<=',$time],['end_time','>=',$time]])
  597. ->first();
  598. if (!$regimentInfo) return json_send(['code'=>'error','msg'=>'拼团活动不存在','data'=>['error'=>'拼团活动不存在']]);
  599. }
  600. //查询用户参团记录
  601. $regimentRecordCount = RegimentRecord::query()
  602. ->where([['custom_uid','=',$uid],['active_id','=',$regimentActiveInfo['id']]])
  603. ->count('id');
  604. if ($regimentRecordCount >= $regimentActiveInfo['participation_number']) return json_send(['code'=>'error','msg'=>'该活动每人仅限参与'.$regimentActiveInfo['participation_number'].'次','data'=>['error'=>'该活动每人仅限参与'.$regimentActiveInfo['participation_number'].'次']]);
  605. $tags = $WeiBanTags->getListByWeibanExtid($custom['weiban_extid']);
  606. // 查询产品信息
  607. $productList = $Product->getListByIds(array_column($buyList,'product_id'));
  608. // 当前时间
  609. $time = time();
  610. // 产品以商业公司分组,方便写入订单
  611. $orderProduct = [];
  612. // 产品价格同级,用于优惠券计算
  613. $productPrice = [];
  614. $priceTotal = 0;
  615. $payTotal = 0;
  616. $buyInfo = $buyList[0];
  617. if ($buyInfo){
  618. if( empty($productList[$buyInfo['product_id']]) ) return json_send(['code'=>'error','msg'=>'产品不存在或已下架','data'=>['error'=>'产品不存在或已下架=>'.$buyInfo['product_id']]]);
  619. // 获取产信息
  620. $productInfo = $productList[$buyInfo['product_id']];
  621. // 判断是不是可以参与
  622. if( $productInfo['tag_scope'] ) {
  623. // 解析数组
  624. $productInfo['tag_scope'] = explode(',',$productInfo['tag_scope']);
  625. // 标签范围限定时,默认不能参与
  626. $allowJoin = 0;
  627. // 判断标签是不是存在
  628. foreach ($tags as $value) {
  629. // 标签范围内,允许参加
  630. if( in_array($value['name'],$productInfo['tag_scope']) ) $allowJoin = 1;
  631. }
  632. // 如果不能参与
  633. if( !$allowJoin ) return json_send(['code'=>'error','msg'=>'不在 '.$productInfo['product_name'].' 参与范围','data'=>['error'=>'不在标签范围内']]);
  634. }
  635. // 判断是不是可以参与
  636. if( $productInfo['tag_exclude'] ) {
  637. // 解析数组
  638. $productInfo['tag_exclude'] = explode(',',$productInfo['tag_exclude']);
  639. // 判断标签是不是存在
  640. foreach ($tags as $value) {
  641. // 标签排除范围内,不允许参加
  642. if( in_array($value['name'],$productInfo['tag_exclude']) ) return json_send(['code'=>'error','msg'=>'不在 '.$productInfo['product_name'].' 可参与范围','data'=>['error'=>'用户在标签排除范围']]);
  643. }
  644. }
  645. //拼团活动限购
  646. if ($regimentActiveInfo['quota']){
  647. if ($buyInfo['buy_num'] > $regimentActiveInfo['quota']) return json_send(['code'=>'error','msg'=>'超过拼团限购','data'=>['error'=>'拼团限购=>'.$buyInfo['product_id']]]);
  648. }
  649. // 如果产品限购
  650. if( $productInfo['quota'] ) {
  651. // 是否在限购时间,当前时间大于开始时间,并且小于结束时间
  652. if( $productInfo['quota_start'] <= $time && $time <= $productInfo['quota_end'] ){
  653. // 通过时间查询商品的购买总数
  654. $total = $OrdersProduct->query()->where([['custom_uid','=',$uid],['product_id','=',$productInfo['id']],['insert_time','>=',$productInfo['quota_start']],['insert_time','<=',$productInfo['quota_end']]])->sum('buy_num');
  655. // 判断限购数量
  656. $total = $buyInfo['buy_num'] + $total;
  657. // 如果超过数量
  658. if( $total > $productInfo['quota'] ) return json_send(['code'=>'error','msg'=>'限购'.$productInfo['quota'].'套-'.$productInfo['product_name'],'data'=>['error'=>'已超限=>'.($total - $productInfo['quota'])]]);
  659. }
  660. }
  661. // 需要扣除的库存
  662. $productList[$buyInfo['product_id']]['decr'] = $buyInfo['buy_num'];
  663. // 判断库存
  664. if( $productInfo['stock'] < $productList[$buyInfo['product_id']]['decr'] ) return json_send(['code'=>'error','msg'=>$productInfo['product_name'].'-库存不足','data'=>['error'=>'产品库存不足=>'.$buyInfo['product_id']]]);
  665. // 计算价值
  666. $priceTotal = $buyInfo['buy_num'] * $regimentActiveInfo['regiment_price'];
  667. $payTotal = $buyInfo['buy_num'] * $productInfo['price'];
  668. }
  669. $orderInfo = [
  670. 'custom_uid' => $uid,
  671. 'status' => 10,
  672. 'pay_total' => $payTotal,
  673. 'price_total' => $priceTotal,
  674. 'business_id' => $productList[$buyInfo['product_id']]['business_id'],
  675. ];
  676. // 赠送积分
  677. $orderInfo['order_score'] = (config('order_score_send',0) && floor( $orderInfo['pay_total'] * 1 ) > 0 ) ? floor( $orderInfo['pay_total'] * 1 ) : 0;
  678. $orderProductInfo = [
  679. 'product_name' => $productList[$buyInfo['product_id']]['product_name'],
  680. 'sku_attr_names' => $productList[$buyInfo['product_id']]['sku_attr_names'],
  681. 'product_thumb' => $productList[$buyInfo['product_id']]['product_thumb'],
  682. 'buy_num' => $buyInfo['buy_num'],
  683. 'price_total' => $priceTotal,
  684. 'pay_total' => $buyInfo['buy_num'] * $productList[$buyInfo['product_id']]['price'],
  685. 'product_id' => $buyInfo['product_id'],
  686. 'custom_uid' => $uid,
  687. 'business_id' => $productList[$buyInfo['product_id']]['business_id'],
  688. ];
  689. // 组合数据,写入订单表,子表
  690. DB::beginTransaction();
  691. // 写入数据
  692. try {
  693. // 扣减商品库存
  694. foreach ($productList as $key => $value) {
  695. // 扣减库存
  696. $result = $Product->edit($value['id'],['stock'=>DB::raw('stock+-'.$value['decr'])]);
  697. // 判断结果
  698. if( !$result ) {
  699. // 回退数据
  700. DB::rollBack();
  701. // 错误提示
  702. return json_send(['code'=>'error','msg'=>'库存扣减失败','data'=>['error'=>'库存扣减失败']]);
  703. }
  704. }
  705. //创建拼团
  706. if($btnType == 3){
  707. $regimentInfo = [
  708. 'custom_uid' => $uid,
  709. 'active_id' => $regimentActiveInfo['id'],
  710. 'product_id' => $buyInfo['product_id'],
  711. 'people_number' => 1,
  712. 'status' => 1,
  713. 'start_time' => $time,
  714. 'end_time' => $time + $regimentActiveInfo['expiration']*3600,
  715. 'update_time' => $time,
  716. 'insert_time' => $time,
  717. ];
  718. $regimentId = Regiment::query()->insert($regimentInfo);
  719. if( !$regimentId ) {
  720. // 回退数据
  721. DB::rollBack();
  722. // 提示信息
  723. return json_send(['code'=>'error','msg'=>'创建拼团失败','data'=>['error'=>'创建拼团失败']]);
  724. }
  725. }
  726. $orderInfo['regiment_id'] = $regimentId;
  727. // 创建总订单
  728. $orderId = $Model->add($orderInfo);
  729. // 如果订单写入失败
  730. if( !$orderId ) {
  731. // 回退数据
  732. DB::rollBack();
  733. // 错误提示
  734. return json_send(['code'=>'error','msg'=>'下单失败','data'=>['error'=>'订单创建失败']]);
  735. }
  736. $orderProductInfo['order_id'] = $orderId;
  737. // 写入子表
  738. $result = $OrdersProduct->query()->insert($orderProductInfo);
  739. // 如果扣减失败
  740. if( !$result ) {
  741. // 回退数据
  742. DB::rollBack();
  743. // 提示信息
  744. return json_send(['code'=>'error','msg'=>'子订单创建失败','data'=>['error'=>'子订单创建失败']]);
  745. }
  746. // 写入订单地址表
  747. $addr['order_id'] = $orderId;
  748. // 写入订单地址表
  749. $result = $OrdersAddr->add($addr);
  750. // 地址写入失败
  751. if( !$result ) {
  752. // 回退数据
  753. DB::rollBack();
  754. // 提示信息
  755. return json_send(['code'=>'error','msg'=>'地址写入失败','data'=>['error'=>'地址写入失败']]);
  756. }
  757. if($btnType == 4){
  758. // 拼团人数加1
  759. $inc = Regiment::query()->where('id','=',$regimentId)->increment('people_number',1);
  760. if( !$inc ) {
  761. // 回退数据
  762. DB::rollBack();
  763. // 提示信息
  764. return json_send(['code'=>'error','msg'=>'创建拼团失败','data'=>['error'=>'创建拼团失败']]);
  765. }
  766. //团满
  767. if ((($regimentInfo['people_number'] + 1) == $regimentActiveInfo['number']) && $regimentActiveInfo == 1) {
  768. $res = regiment::query()->where('id','=',$regimentId)->update(['status'=>3]);
  769. if( !$res ) {
  770. // 回退数据
  771. DB::rollBack();
  772. // 提示信息
  773. return json_send(['code'=>'error','msg'=>'创建拼团失败','data'=>['error'=>'创建拼团失败']]);
  774. }
  775. //修改订单
  776. $orderRes = $Model::query()->where('regiment_id','=',$regimentId)->update(['status'=>'1']);
  777. if( !$orderRes ) {
  778. // 回退数据
  779. DB::rollBack();
  780. // 提示信息
  781. return json_send(['code'=>'error','msg'=>'创建拼团失败','data'=>['error'=>'创建拼团失败']]);
  782. }
  783. //赠送积分
  784. $orderList = $Model::query()->where('regiment_id','=',$regimentId)->get();
  785. foreach ($orderList as $key => $value) {
  786. if( $value['order_score'] > 0 ) $CustomScore->trade($orderInfo['custom_uid'],$value['id'],$value['order_score'],5,1);
  787. }
  788. }
  789. }
  790. //拼团记录
  791. $regimentRecordInfo = [
  792. 'custom_uid' => $uid,
  793. 'regiment_id' => $regimentId,
  794. 'active_id' => $regimentActiveInfo['id'],
  795. 'product_id' => $buyInfo['product_id'],
  796. 'order_id' => $orderId,
  797. 'update_time' => $time,
  798. 'insert_time' => $time,
  799. ];
  800. $result = RegimentRecord::query()->insert($regimentRecordInfo);
  801. if( !$result ) {
  802. // 回退数据
  803. DB::rollBack();
  804. // 提示信息
  805. return json_send(['code'=>'error','msg'=>'创建拼团记录失败','data'=>['error'=>'创建拼团记录失败']]);
  806. }
  807. // 提交数据
  808. DB::commit();
  809. // 返回结果
  810. return json_send(['code'=>'success','msg'=>'下单成功','data'=>['id'=>null]]);
  811. // 返回结果
  812. } catch (\Throwable $th) {
  813. // 回退数据
  814. DB::rollBack();
  815. // 判断结果,如果库存扣减失败的话
  816. if( stripos($th->getMessage(),'UNSIGNED') ) return json_send(['code'=>'error','msg'=>'库存不足','data'=>['error'=>'产品库存扣减失败']]);
  817. // 下单失败提示
  818. return json_send(['code'=>'error','msg'=>'系统异常,请稍后再试','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  819. }
  820. }
  821. /**
  822. * 取消拼团 /api/orders/cancel_regiment
  823. *
  824. * */
  825. public function cancel_regiment( Request $request, Model $Model,OrdersProduct $OrdersProduct,CustomScore $CustomScore){
  826. // 验证参数
  827. $request->scene('cancel')->validate();
  828. // 检查登录
  829. $uid = $this->checkLogin();
  830. // 接收参数
  831. $id = request('id',0);
  832. $status = 4;
  833. // 获取产品和数量
  834. $oldData = $Model->query()->where([['id','=',$id],['custom_uid','=',$uid]])->first(['id','order_score','status','custom_uid','insert_time']);
  835. // 如果用户不存在
  836. if( !$oldData ) return json_send(['code'=>'error','msg'=>'订单不存在']);
  837. // 如果已经取消
  838. if( $oldData['status'] == 4 ) return json_send(['code'=>'error','msg'=>'订单已取消']);
  839. // 如果团不存在
  840. if( !$oldData['regiment_id'] ) return json_send(['code'=>'error','msg'=>'拼团不存在']);
  841. // 组合数据,写入订单表,子表
  842. DB::beginTransaction();
  843. try{
  844. // 查询数据
  845. $result = $Model->setOrderStatus($id,$status,$OrdersProduct);
  846. // 提示新增失败
  847. if( isset($result['error']) ) {
  848. // 回退数据
  849. DB::rollBack();
  850. // 提示信息
  851. return json_send(['code'=>'error','msg'=>$result['error'],'data'=>['error'=>$result['error']]]);
  852. }
  853. //取消加入团
  854. $regimentRes = Regiment::query()->where('id','=',$oldData['regiment_id'])->decrement('people_number',1);
  855. if( !$regimentRes ) {
  856. // 回退数据
  857. DB::rollBack();
  858. // 提示信息
  859. return json_send(['code'=>'error','msg'=>'取消拼团失败']);
  860. }
  861. //修改拼团记录
  862. $regimentRes = RegimentRecord::query()->where([['order_id','=',$oldData['id'],]])->update(['status'=>'2']);
  863. if( !$regimentRes ) {
  864. // 回退数据
  865. DB::rollBack();
  866. // 提示信息
  867. return json_send(['code'=>'error','msg'=>'取消拼团失败']);
  868. }
  869. // 提交数据
  870. DB::commit();
  871. // 告知结果
  872. return json_send(['code'=>'success','msg'=>'取消成功']);
  873. // 返回结果
  874. } catch (\Throwable $th) {
  875. // 回退数据
  876. DB::rollBack();
  877. // 下单失败提示
  878. return json_send(['code'=>'error','msg'=>'取消失败','data'=>['error'=>$th->getMessage().$th->getLine()]]);
  879. }
  880. }
  881. }