GroupActivityLogic.php 11 KB

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