1
0

GroupActivityLogic.php 12 KB

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