100], [['order_id', 'remark'], 'string', 'max' => 255], ]; } /** * {@inheritdoc} */ public function attributeLabels() { return [ 'id' => 'id', 'mall_id' => '商城ID', 'user_id' => '用户ID', 'order_no' => '订单编号', 'activity_id' => '拼团活动id', 'goods_id' => '商品id', 'goods_attr_id' => '商品规格id', 'group_status' => '拼团状态 0待支付 1参团中 2拼团成功 3拼团失败', 'attend_type' => '拼团方式 1手动拼团 2自动拼团', 'money' => '订单金额', 'buy_nums' => '购买数量', 'win_time' => '拼中时间', 'win_nums' => '拼中次数', 'payment_type' => '支付方式:1.在线支付 2.货到付款 3.余额支付', 'pay_at' => '支付时间', 'order_id' => '主订单id,多个订单使用逗号隔开', 'mch_id' => '多商户id,0表示商城订单', 'total_price' => '订单总金额(含运费)', 'total_pay_price' => '实际支付总费用(含运费)', 'shipping_money' => '运费', 'total_goods_price' => '订单商品总金额(优惠后)', 'total_goods_original_price' => '订单商品总金额(优惠前)', 'remark' => '用户订单备注', 'is_pay' => '是否支付:0.未支付|1.已支付', 'address_id' => '用户收货地址id', 'major_data' => '主订单数据', 'extra' => '额外信息', 'open_times' => '开奖次数', 'yu_attend_times' => '预扣参团次数', 'consume' => '预扣消耗', 'expend_type' => '消耗类型', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '创建时间', 'updated_at' => '更新时间', ]; } public function getGoods() { return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,original_price,cost_price,cover_pic'); } public function getOrderMarketing() { return $this->hasOne(LuckyGroupOrderMarketing::class, ['order_id' => 'id'])->select('member_price_deduct,coupon_discount_money,order_id'); } public function getAddress() { return $this->hasOne(UserAddress::class, ['id' => 'address_id'])->select('name,province,city,district,town,mobile,detail,id'); } public function getUser() { return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,avatar_url'); } public function getGoodsAttr() { return $this->hasOne(GoodsAttr::class, ['id' => 'goods_attr_id'])->select('id,name'); } /** * @desc:生成订单 * @Author: hua * @Date: 2021/12/15 * @Time: 15:14 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $form * @return array|false */ public static function createOrder($form) { $stock_locks = []; try { $group_order_goods = $form['group_order_goods']; $data = is_array($form->data) ? $form->data : Json::decode($form->data, true); $trans = Yii::$app->db->beginTransaction(); $order_trade_info = []; $order_id = ''; $remark = empty($data['remark'])?'':$data['remark'][0]; //只有一个订单 foreach ($group_order_goods as $mch_order) { $order_no = StringHelper::generateOrderNo('lgy'); $mch_order_info = $mch_order['mch_order_info']; $goods = reset($mch_order['goods']); $mch_order_info['pay_money'] = bcadd($mch_order_info['goods_price'],$mch_order_info['shipping_money'],2); $order = new self(); $order->loadDefaultValues(); $order->load($mch_order_info, ''); $order->remark = $remark; $order->activity_id = $data['activity_id']; $order->is_pay = OrderStatusEnum::NOT_PAY; $order->total_pay_price = $mch_order_info['pay_money']; $order->total_price = $mch_order_info['pay_money']; $order->money = $mch_order_info['pay_money']; $order->buy_nums = $data['num']; $order->order_no = $order_no; $order->attend_type = $data['attend_type']; $order->goods_attr_id = $data['attr_id']; $order->goods_id = $goods['goods_id']; $order->total_goods_price = $mch_order_info['goods_price']; $order->total_goods_original_price = $mch_order_info['original_goods_price']; $order->yu_attend_times = $data['yu_attend_times']; $order->consume = $data['total_expend_num']; $order->expend_type = $data['expend_type']; $order->major_data = json_encode($data); $order->address_id = $form->address_id; $res = $order->save(); if ($res === false) throw new \Exception($order->getErrorMessage()); $order_id = $order->id; // 订单营销 if (!empty($goods['goods_details'][0]['marketing'])) { $order_marketing = new LuckyGroupOrderMarketing(); $order_marketing->loadDefaultValues(); $order_marketing->load($goods['goods_details'][0]['marketing'], ''); $order_marketing->order_id = $order_id; $res = $order_marketing->save(); if ($res === false) throw new \Exception($order_marketing->getErrorMessage()); } $order_trade_info[] = [ 'order_no' => $order->order_no, 'amount' => $order->total_pay_price, 'title' => mb_strlen($goods['goods_name']) > 32 ? mb_substr($goods['goods_name'], 0, 32) : $goods['goods_name'], 'notify_class' => LuckyGroupEnum::NOTIFY_CLASS, 'order_type' => PaymentTradeOrderEnum::LUCKY_GROUP_BUY_ORDER, ]; } $trans->commit(); LockHelper::batchUlock($stock_locks); //生成支付流水 $res = \Yii::$app->services->pay->createTrade($form['mall_id'], $form['user_id'], $order_trade_info); if ($res == false) throw new \Exception('生成支付流水失败'); $arr['transaction'] = $res; $arr['pay_success_page'] = Yii::$app->services->setting->mall->get($form['mall_id'], 'order.pay_after_link');// 支付成功后跳转的地址 $arr['order_id'] = $order_id; return $arr; } catch (\Exception $e) { LockHelper::batchUlock($stock_locks); if (isset($trans)) $trans->rollback(); self::$static_error = $e->getMessage(); return false; } } public static function subUserWithholding($item) { $lucky_user = LuckyGroupUser::getGroupUser($item->mall_id, $item->user_id); if($item['consume']>0){ if ($item['expend_type'] == LuckyGroupEnum::EXPEND_TYPE_SCORE) {//积分 $data = ['message_id' => 0, 'mall_id' => $item->mall_id, 'user_id' => $item->user_id, 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE, 'is_frozen' => false, 'unfrozen' => false, 'money' => -$item['consume'], 'desc' => '幸运拼团预扣', 'source_table' => AssetsType::LUCKY_GROUP, 'source_table_id' => $item->id, 'from_type' => UserWalletEnum::FROM_LUCKY_GROUP, 'capital_type' => UserWalletEnum::LUCKY_GROUP_CONSUME ]; } else {//余额 $data = ['message_id' => 0, 'mall_id' => $item->mall_id, 'user_id' => $item->user_id, 'wallet_type' => UserWalletService::WALLET_TYPE_BALANCE, 'is_frozen' => false, 'unfrozen' => false, 'money' => -$item['consume'], 'desc' => '幸运拼团预扣', 'source_table' => AssetsType::LUCKY_GROUP, 'source_table_id' => $item->id, 'from_type' => UserWalletEnum::FROM_LUCKY_GROUP, 'capital_type' => UserWalletEnum::LUCKY_GROUP_CONSUME ]; } $state = UserWalletService::handle(0, $data); if ($state==false) return false; } $lucky_user->attend_times = $lucky_user->attend_times - $item['yu_attend_times']; if($lucky_user->attend_times<0) $lucky_user->attend_times = 0; return $lucky_user->save(); } public static function pay($order, $pay_type) { $trans = Yii::$app->db->beginTransaction(); try { $model = self::findOne(['id' => $order->id, 'status' => [StatusEnum::ENABLED]]); if (empty($model)) throw new \Exception('订单不存在或已删除'); // if ($model->is_pay != OrderStatusEnum::NOT_PAY) throw new \Exception('订单已经被处理,无法进行支付'); // if ($model->is_pay == StatusEnum::ENABLED) throw new \Exception('订单已支付,请不要重复支付'); $model->is_pay = OrderStatusEnum::PAY; $model->group_status = LuckyGroupEnum::GROUP_JOINING; $model->payment_type = $pay_type; $model->pay_at = time(); $marketing = LuckyGroupOrderMarketing::getOne([['order_id' => $model->id]]); if ($marketing && $marketing->user_coupon_id) { $use_coupon_id = \Qiniu\json_decode($marketing->user_coupon_id, true); $coupon_ids = array_keys($use_coupon_id); $coupon_state = AddonsCouponUserRelate::updateAll(['is_use' => 1], ['and', ['user_id' => $model->user_id], ['in', 'id', $coupon_ids]]); if ($coupon_state === false) throw new \Exception('修改订单失败:' . AddonsCouponUserRelate::getStaticError()); } $res = $model->save(); if ($res === false) throw new \Exception('修改订单失败:' . $model->getErrorMessage()); // $user = LuckyGroupUser::getGroupUser($model->mall_id, $model->user_id); // $user->attend_money = bcadd($user->attend_money, $model->total_pay_price, 2); // if ($model->total_pay_price > $user->max_order_money) { // $user->max_order_money = $model->total_pay_price; // } // $user->save(); $trans->commit(); $attend_peoples = 1; LuckyGroupMallOpenGroupStatistics::updateStatistics(['attend_peoples' => $attend_peoples, 'mall_id' => $model->mall_id]); $statistics = [ 'attend_money' => $model->total_pay_price, 'attend_peoples' => $attend_peoples, 'mall_id' => $model->mall_id, ]; LuckyGroupMallStatistics::updateStatistics($statistics); // 触发订单支付事件,事务提交完成后触发 $event = new LuckyGroupOrderPaidEvent($model->mall_id); $data['id'] = $model['id']; $data['operator_id'] = $params['operator_id'] ?? 0; $data['operator_name'] = $params['operator_name'] ?? ''; Yii::$app->services->events->dispatch($event, $data, $event->listeners); return true; } catch (\Exception $e) { if (isset($trans)) $trans->rollback(); self::$static_error = $e->getMessage(); return false; } } /** * 众健酒需求增加 * 获取语音播报列表 * @param $mall_id * @param $activity_id * @param $goods_warehouse_id * @return array */ public static function getWinText($mall_id, $activity_id, $goods_id) { // $lucky_group_customize_text_configs = (new \app\plugins\lucky_group\forms\LuckyGroupConfigForm())->textCustomizeSetting($mall_id); $list = self::find()->where(['mall_id' => $mall_id, 'activity_id' => $activity_id, 'goods_id' => $goods_id, 'group_status' => LuckyGroupEnum::GROUP_SUCCESS]) ->orderBy('id desc')->limit(20) ->all(); $user_list = User::find()->where(['in', 'id', array_column($list, 'user_id')])->all(); $lottery_period_list = LuckyGroupLotteryPeriod::find()->where(['in', 'order_id', array_column($list, 'id')])->all(); $lottery_period_arr = array_column($lottery_period_list, 'number', 'order_id'); $url = ((int)$_SERVER['SERVER_PORT'] == 80 ? 'http://' : 'https://') . $_SERVER['HTTP_HOST']; foreach ($user_list as $v) { $user_arr[$v['id']] = !empty($v['nickname']) ? $v['nickname'] : $v['username']; $avatar_url_arr[$v['id']] = !empty($v['avatar_url']) ? $v['avatar_url'] : $url . '/web/uploads/images/original/20210422/1619055025518.jpg';; } $rows = []; $join_success_text = DefaultModel::getConfig($mall_id); foreach ($list as $v) { $number = !empty($lottery_period_arr[$v['id']]) ? $lottery_period_arr[$v['id']] : 1; $nickname = !empty($user_arr[$v['user_id']]) ? $user_arr[$v['user_id']] : ''; $avatar_url = !empty($avatar_url_arr[$v['user_id']]) ? $avatar_url_arr[$v['user_id']] : ''; $str = "恭喜{$nickname}" . $join_success_text['customize_text_res']['join_success_text'] . "成为第{$number}期幸运儿"; $rows[] = [ 'text' => $str, 'avatar_url' => $avatar_url, ]; } return $rows; } //去支付,再次支付调用 public static function toPay($params){ try { $where = ['id' => $params['id'], 'user_id' => $params['user_id'], 'mall_id' => $params['mall_id'],'group_status'=>0]; $lucky_group_order = LuckyGroupOrder::findOne($where); if(empty($lucky_group_order))throw new \Exception('查无订单'); $activity = LuckyGroupActivity::findOne(['id'=>$lucky_group_order->activity_id]); if(empty($activity)) throw new \Exception('活动不存在'); $time = time(); if(strtotime(date('Y-m-d ').$activity->end_time)<$time) throw new \Exception('活动已过期'); $goods = Goods::findOne(['id'=>$lucky_group_order->goods_id]); $orders[] = [ 'order_id' => $lucky_group_order->id, 'order_no' => $lucky_group_order->order_no, 'amount' => $lucky_group_order->total_pay_price, 'title' => mb_strlen($goods['goods_name']) > 32 ? mb_substr($goods['goods_name'], 0, 32) : $goods['goods_name'], 'notify_class' => LuckyGroupEnum::NOTIFY_CLASS, 'order_type' => PaymentTradeOrderEnum::LUCKY_GROUP_BUY_ORDER, ]; $payment_expire_time = Yii::$app->services->setting->mall->get($params['mall_id'], 'order.payment_expire_time'); if(empty($payment_expire_time)) $payment_expire_time = 30; $order_time = $lucky_group_order['created_at']+$payment_expire_time*60; //生成支付流水 $res = \Yii::$app->services->pay->createTrade($params['mall_id'], $params['user_id'], $orders); if ($res == false) throw new \Exception('生成支付流水失败'); $res['surplus_second'] = 30*60;//剩余秒数 if(!empty($order_time)) $res['surplus_second'] = ($order_time - time())>0?$order_time - time():0; return $res; }catch (\Exception $e){ self::$static_error = $e->getMessage(); return false; } } public static function close($params){ try { $where = ['id' => $params['id'], 'user_id' => $params['user_id'], 'mall_id' => $params['mall_id'],'group_status'=>0,'pay_at'=>0]; $lucky_group_order = LuckyGroupOrder::findOne($where); if(empty($lucky_group_order))throw new \Exception('查无订单'); $lucky_group_order->group_status = -1; $res = $lucky_group_order->save(); if($res){ //如果遇到组合支付订单,查看是否余额/积分支付,原路返回 $groupOrders = PaymentTradeGroupOrder::find()->where(['order_no'=>$lucky_group_order->order_no,'is_pay'=>1])->asArray()->all(); if($groupOrders){ $insert_wallet = []; foreach($groupOrders as $groupOrder){ if(in_array($groupOrder['pay_type'],[PayTypeEnum::BALANCE,PayTypeEnum::SCORE])){ $wallet_type = $groupOrder['pay_type'] == PayTypeEnum::SCORE ? UserWalletService::WALLET_TYPE_SCORE : UserWalletService::WALLET_TYPE_BALANCE; $trade = PaymentTrade::findOne($groupOrder['trade_id']); $insert_wallet[] = [ 'mall_id' => $lucky_group_order->mall_id, 'user_id' => $lucky_group_order->user_id, 'is_frozen' => false, 'wallet_type' => $wallet_type,// 暂定退回余额或积分 'money' => $trade->pay_fee, 'desc' => '订单退款', 'source_table' => PaymentTradeGroupOrder::tableName(), 'source_table_id' => $groupOrder['id'], 'from_type' => UserWalletEnum::REFUND, 'capital_type' => UserWalletEnum::REFUND_ORDER, 'order_no' => $lucky_group_order->order_no, 'not_add_total' => true ]; } } if($insert_wallet){ $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); } } } return true; }catch (\Exception $e){ self::$static_error = $e->getMessage(); return false; } } }