GroupActivityLogic.php 11 KB

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