GroupActivityLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. namespace app\api\logic;
  3. use think\Exception;
  4. use think\facade\Db;
  5. use app\common\enum\RefundEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\logic\RefundLogic;
  8. use app\common\model\equity\UserEquity;
  9. use app\common\model\refund\RefundRecord;
  10. use app\common\model\equity\UserEquityLog;
  11. use app\common\model\service_area\ServiceArea;
  12. use app\common\model\group_activity\GroupOrder;
  13. use app\common\model\group_activity\GroupActivity;
  14. use app\common\model\group_activity\GroupUserOrder;
  15. /**
  16. * 拼团活动逻辑处理
  17. * Class GroupActivityLogic
  18. * @package app\api\logic
  19. */
  20. class GroupActivityLogic extends BaseLogic
  21. {
  22. /**
  23. * @notes 拼团活动详情
  24. */
  25. public static function detail($id){
  26. $detail = GroupActivity::with('goods')->where(['id'=>$id])->visible([
  27. 'id','title','image','start_time','end_time','type','equity_id',
  28. 'participant_num','origin_price','price','form_time_limit'
  29. ])->findOrEmpty()->toArray();
  30. if(!empty($detail)){
  31. $detail['price'] = explode(",",$detail['price']);
  32. $detail['participant_num'] = explode(",",$detail['participant_num']);
  33. }
  34. return $detail;
  35. }
  36. /**
  37. * @notes 拼团订单详情
  38. */
  39. public static function orderDetail($sn,$userId){
  40. $detail = GroupOrder::where(['sn'=>$sn])->findOrEmpty()->toArray();
  41. if(!empty($detail)){
  42. $detail['activity'] = GroupActivity::with('goods')->where(['id'=>$detail['group_activity_id']])->visible([
  43. 'id','title','image','start_time','end_time','type','equity_id',
  44. 'participant_num','origin_price','price','form_time_limit'
  45. ])->findOrEmpty()->toArray();
  46. $detail['users'] = GroupUserOrder::alias('a')
  47. ->leftJoin('user b','a.user_id=b.id')
  48. ->where(['a.sn'=>$sn,'a.status'=>1])
  49. ->field(['a.id','a.user_id','a.status','a.create_time','b.avatar','b.nickname'])
  50. ->order('a.create_time','asc')
  51. ->select()
  52. ->toArray();
  53. }
  54. return $detail;
  55. }
  56. /**
  57. * @notes 用户订单详情
  58. */
  59. public static function userOrderDetail($sn,$userId){
  60. $detail = GroupUserOrder::where(['sn'=>$sn, 'user_id' => $userId])->findOrEmpty()->toArray();
  61. return $detail;
  62. }
  63. /**
  64. * 提交订单
  65. * @param array $params
  66. * @return array|false
  67. */
  68. public static function submitOrder($params)
  69. {
  70. Db::startTrans();
  71. try {
  72. // 订单位置是否在服务区内
  73. $area = ServiceArea::serviceArea(['lon'=>$params['lon'],'lat'=>$params['lat']]);
  74. if (!$area) {
  75. throw new Exception('已超出服务区域!-1001');
  76. }
  77. if(empty($params['mobile'])){
  78. throw new Exception('请先补充您的联系方式后在提交订单');
  79. }
  80. $is_join = GroupUserOrder::where('status','<>',4)->where(['group_activity_id' => $params['group_activity_id'], 'user_id' => $params['user_id']])->value('id');
  81. if ($is_join) {
  82. throw new Exception('您已参加过该活动!');
  83. }
  84. //校验拼团活动
  85. if (empty($params['sn'])) {
  86. //新开团
  87. $activity = GroupActivity::findOrEmpty($params['group_activity_id']); //活动详情
  88. if ($activity->isEmpty()) {
  89. throw new Exception('拼团活动不存在!'); //拼团活动不存在
  90. }
  91. //校验活动时间
  92. if (time() < strtotime($activity['start_time'])) {
  93. throw new Exception('拼团活动未开始!'); //拼团活动未开始
  94. }
  95. if (time() > strtotime($activity['end_time'])) {
  96. throw new Exception('拼团活动已结束!'); //拼团活动已结束
  97. }
  98. $order_amount = explode(",",$activity['price'])[0];
  99. //生成拼团单
  100. $data = [
  101. 'sn' => generate_sn(GroupOrder::class, 'sn'),
  102. 'group_activity_id' => $params['group_activity_id'],
  103. 'goods_id' => $activity['goods_id'],
  104. 'user_id' => $params['user_id'],
  105. 'origin_price' => $activity['origin_price'],
  106. 'price' => $order_amount,
  107. 'create_time' => time(),
  108. 'end_time' => time() + $activity['form_time_limit'] * 60 * 60,
  109. ];
  110. $group_order = GroupOrder::create($data);
  111. } else {
  112. //加入已开的拼团单
  113. $group_order = GroupOrder::where(['group_activity_id' => $params['group_activity_id'], 'sn' => $params['sn']])->findOrEmpty()->toArray();
  114. if (empty($group_order)) {
  115. throw new Exception('拼团订单不存在!'); //拼团活动不存在
  116. }
  117. if ($group_order['num'] >= 100) {
  118. throw new Exception('拼团人数已满!'); //拼团人数已满
  119. }
  120. if ($group_order['status'] == 1 ) {
  121. throw new Exception('订单已支付!');
  122. }
  123. if ($group_order['status'] >= 1 ) {
  124. throw new Exception('订单已取消!');
  125. }
  126. if (strtotime($group_order['end_time']) < time()) {
  127. throw new Exception('拼团活动已结束!'); //拼团活动已结束
  128. }
  129. $order_amount = $group_order['price'];
  130. }
  131. //生成用户拼单订单
  132. $data = [
  133. 'sn' => $group_order['sn'],
  134. 'group_order_id' => $group_order['id'],
  135. 'group_activity_id' => $params['group_activity_id'],
  136. 'user_id' => $params['user_id'],
  137. 'real_name' => $params['real_name'],
  138. 'mobile' => $params['mobile'],
  139. 'address' => $params['address'],
  140. 'lon' => $params['lon'],
  141. 'lat' => $params['lat'],
  142. 'province' => $areas['province']??0,
  143. 'city' => $areas['city']??0,
  144. 'area_name' => $areas['area_name']??'',
  145. 'service_area_id' => $areas['id']??0,
  146. 'order_amount' => $order_amount,
  147. 'order_terminal' => $params['terminal'],
  148. ];
  149. $group_user_order = GroupUserOrder::create($data);
  150. Db::commit();
  151. } catch (\Exception $e) {
  152. Db::rollback();
  153. self::setError($e->getMessage());
  154. return false;
  155. }
  156. return [
  157. 'order_id' => (int)$group_user_order['id'],
  158. 'sn' => $group_order['sn']
  159. ];
  160. }
  161. /**
  162. * 取消订单
  163. * @param $params
  164. * @return false|void
  165. */
  166. public static function cancelOrder($params)
  167. {
  168. Db::startTrans();
  169. try {
  170. $detail = GroupUserOrder::where([
  171. 'user_id' => $params['user_id'],
  172. 'sn'=>$params['sn']
  173. ])->field('id,status')->findOrEmpty()->toArray();
  174. if(empty($detail)){
  175. throw new Exception('订单不存在');
  176. }
  177. if($detail['status'] == 1){
  178. throw new Exception('已支付订单不支持取消');
  179. }
  180. if($detail['status'] != 0){
  181. throw new Exception('当前订单不支持取消');
  182. }
  183. //将用户订单状态更新为已取消
  184. GroupUserOrder::where('id',$detail['id'])->update(['status' => 4, 'pay_status' => 2]);
  185. Db::commit();
  186. return true;
  187. }
  188. catch (\Exception $e) {
  189. Db::rollback();
  190. self::setError($e->getMessage());
  191. return false;
  192. }
  193. }
  194. /**
  195. * 订单退款
  196. * @param $params
  197. * @return false|void
  198. */
  199. public static function refundOrder($params)
  200. {
  201. Db::startTrans();
  202. try {
  203. $order = GroupUserOrder::where([
  204. 'user_id' => $params['user_id'],
  205. 'sn'=>$params['sn']
  206. ])->field('id,sn,status,order_amount,pay_status,pay_way,user_equity_id,user_id')->findOrEmpty()->toArray();
  207. if(empty($order)){
  208. throw new Exception('订单不存在');
  209. }
  210. if($order['status'] != 1 || $order['pay_status'] != 1){
  211. throw new Exception('当前订单不支持退款');
  212. }
  213. if ($order['user_equity_id']) {
  214. //判断权益卡是否已使用
  215. $used = UserEquityLog::where(['user_equity_id' => $order['user_equity_id'],'user_id' => $params['user_id']])->findOrEmpty();
  216. if ($used) {
  217. throw new Exception('当前权益卡已使用,不支持退款');
  218. }
  219. }
  220. //将用户订单状态更新为申请退款
  221. GroupUserOrder::where('id',$order['id'])->update(['status' => 2,'refund_status' => 1]);
  222. // 生成退款记录
  223. $recordSn = generate_sn(RefundRecord::class, 'sn');
  224. $record = RefundRecord::create([
  225. 'sn' => $recordSn,
  226. 'user_id' => $order['user_id'],
  227. 'order_id' => $order['id'],
  228. 'order_sn' => $order['sn'],
  229. 'order_type' => RefundEnum::ORDER_TYPE_GROUP,
  230. 'order_amount' => $order['order_amount'],
  231. 'refund_amount' => $order['order_amount'],
  232. 'refund_type' => RefundEnum::TYPE_ADMIN,
  233. 'transaction_id' => $order['transaction_id'] ?? '',
  234. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  235. ]);
  236. // 退款
  237. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  238. Db::commit();
  239. return true;
  240. }
  241. catch (\Exception $e) {
  242. Db::rollback();
  243. self::setError($e->getMessage());
  244. return false;
  245. }
  246. }
  247. public static function deleteOrder($params):bool
  248. {
  249. try{
  250. $order = GroupUserOrder::where([
  251. 'status' => 0,
  252. 'user_id' => $params['worker_iuser_idd'],
  253. 'sn' => $params['sn']
  254. ])->findOrEmpty();
  255. if($order->isEmpty()){
  256. throw new \Exception('订单不存在');
  257. }
  258. $order->delete();
  259. return true;
  260. } catch(\Exception $e){
  261. self::setError($e->getMessage());
  262. return false;
  263. }
  264. }
  265. }