GroupActivityLogic.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\api\logic;
  3. use think\Exception;
  4. use think\facade\Db;
  5. use app\common\logic\BaseLogic;
  6. use app\common\model\service_area\ServiceArea;
  7. use app\common\model\group_activity\GroupOrder;
  8. use app\common\model\group_activity\GroupActivity;
  9. use app\common\model\group_activity\GroupUserOrder;
  10. /**
  11. * 拼团活动逻辑处理
  12. * Class GroupActivityLogic
  13. * @package app\api\logic
  14. */
  15. class GroupActivityLogic extends BaseLogic
  16. {
  17. /**
  18. * @notes 拼团活动详情
  19. */
  20. public static function detail($id){
  21. $detail = GroupActivity::with('goods')->where(['id'=>$id])->visible([
  22. 'id','title','image','start_time','end_time','type','equity_id',
  23. 'participant_num','origin_price','price','form_time_limit'
  24. ])->findOrEmpty()->toArray();
  25. if(!empty($detail)){
  26. $detail['price'] = explode(",",$detail['price']);
  27. $detail['participant_num'] = explode(",",$detail['participant_num']);
  28. }
  29. return $detail;
  30. }
  31. /**
  32. * @notes 拼团订单详情
  33. */
  34. public static function orderDetail($sn,$userId){
  35. $detail = GroupOrder::where(['sn'=>$sn])->findOrEmpty()->toArray();
  36. if(!empty($detail)){
  37. $detail['activity'] = GroupActivity::with('goods')->where(['id'=>$detail['group_activity_id']])->findOrEmpty()->toArray();
  38. $detail['users'] = GroupUserOrder::alias('a')
  39. ->leftJoin('user b','a.user_id=b.id')
  40. ->where(['a.sn'=>$sn,'a.status'=>1])
  41. ->visible(['a.id','a.user_id','a.status','a.create_time','b.avatar','b.nickname'])
  42. ->select()
  43. ->toArray();
  44. }
  45. return $detail;
  46. }
  47. /**
  48. * @notes 用户订单详情
  49. */
  50. public static function userOrderDetail($sn,$userId){
  51. $detail = GroupUserOrder::where(['sn'=>$sn, 'user_id' => $userId])->findOrEmpty()->toArray();
  52. return $detail;
  53. }
  54. /**
  55. * 提交订单
  56. * @param array $params
  57. * @return array|false
  58. */
  59. public static function submitOrder($params)
  60. {
  61. Db::startTrans();
  62. try {
  63. // 订单位置是否在服务区内
  64. $area = ServiceArea::serviceArea(['lon'=>$params['lon'],'lat'=>$params['lat']]);
  65. if (!$area) {
  66. throw new Exception('已超出服务区域!-1001');
  67. }
  68. if(empty($params['mobile'])){
  69. throw new Exception('请先补充您的联系方式后在提交订单');
  70. }
  71. $is_join = GroupUserOrder::where('status','<>',4)->where(['group_activity_id' => $params['group_activity_id'], 'user_id' => $params['user_id']])->value('id');
  72. if ($is_join) {
  73. throw new Exception('您已参加过该活动!');
  74. }
  75. //校验拼团活动
  76. if (empty($params['sn'])) {
  77. //新开团
  78. $activity = GroupActivity::findOrEmpty($params['group_activity_id']); //活动详情
  79. if ($activity->isEmpty()) {
  80. throw new Exception('拼团活动不存在!'); //拼团活动不存在
  81. }
  82. //校验活动时间
  83. if (time() < strtotime($activity['start_time'])) {
  84. throw new Exception('拼团活动未开始!'); //拼团活动未开始
  85. }
  86. if (time() > strtotime($activity['end_time'])) {
  87. throw new Exception('拼团活动已结束!'); //拼团活动已结束
  88. }
  89. $order_amount = explode(",",$activity['price'])[0];
  90. //生成拼团单
  91. $data = [
  92. 'sn' => generate_sn(GroupOrder::class, 'sn'),
  93. 'group_activity_id' => $params['group_activity_id'],
  94. 'goods_id' => $activity['goods_id'],
  95. 'user_id' => $params['user_id'],
  96. 'origin_price' => $activity['origin_price'],
  97. 'price' => $order_amount,
  98. 'create_time' => time(),
  99. 'end_time' => time() + $activity['form_time_limit'] * 60,
  100. ];
  101. $group_order = GroupOrder::create($data);
  102. } else {
  103. //加入已开的拼团单
  104. $group_order = GroupOrder::where(['group_activity_id' => $params['group_activity_id'], 'sn' => $params['sn']])->findOrEmpty()->toArray();
  105. if (empty($group_order)) {
  106. throw new Exception('拼团订单不存在!'); //拼团活动不存在
  107. }
  108. if ($group_order['status'] != 0) {
  109. throw new Exception('拼团活动已结束!'); //拼团活动已结束
  110. }
  111. if (strtotime($group_order['end_time']) < time()) {
  112. throw new Exception('拼团活动已结束!'); //拼团活动已结束
  113. }
  114. if ($group_order['num'] >= 100) {
  115. throw new Exception('拼团人数已满!'); //拼团人数已满
  116. }
  117. $order_amount = $group_order['price'];
  118. }
  119. //生成用户拼单订单
  120. $data = [
  121. 'sn' => $group_order['sn'],
  122. 'group_order_id' => $group_order['id'],
  123. 'group_activity_id' => $params['group_activity_id'],
  124. 'user_id' => $params['user_id'],
  125. 'real_name' => $params['real_name'],
  126. 'mobile' => $params['mobile'],
  127. 'address' => $params['address'],
  128. 'lon' => $params['lon'],
  129. 'lat' => $params['lat'],
  130. 'province' => $areas['province']??0,
  131. 'city' => $areas['city']??0,
  132. 'area_name' => $areas['area_name']??'',
  133. 'service_area_id' => $areas['id']??0,
  134. 'order_amount' => $order_amount,
  135. ];
  136. $group_user_order = GroupUserOrder::create($data);
  137. Db::commit();
  138. } catch (\Exception $e) {
  139. Db::rollback();
  140. self::setError($e->getMessage());
  141. return false;
  142. }
  143. return [
  144. 'order_id' => (int)$group_user_order['id'],
  145. 'sn' => $group_order['sn']
  146. ];
  147. }
  148. /**
  149. * 取消订单
  150. * @param $params
  151. * @return false|void
  152. */
  153. public static function cancelOrder($params)
  154. {
  155. Db::startTrans();
  156. try {
  157. $detail = GroupUserOrder::where([
  158. 'user_id' => $params['user_id'],
  159. 'sn'=>$params['sn']
  160. ])->file('id,status')->findOrEmpty()->toArray();
  161. if(empty($detail)){
  162. throw new Exception('订单不存在');
  163. }
  164. if($detail['status'] == 1){
  165. throw new Exception('已支付订单不能取消');
  166. }
  167. if($detail['status'] != 0){
  168. throw new Exception('当前订单不能取消');
  169. }
  170. //将用户订单状态更新为已取消
  171. GroupUserOrder::where('id',$detail['id'])->update(['status' => 4, 'pay_status' => 2]);
  172. Db::commit();
  173. }
  174. catch (\Exception $e) {
  175. Db::rollback();
  176. self::setError($e->getMessage());
  177. return false;
  178. }
  179. }
  180. }