| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1\pt;
- use app\models\Goods;
- use app\models\Order;
- use app\models\PtActivity;
- use app\models\PtActivityBanner;
- use app\models\PtActivityGoods;
- use app\models\PtActivityGoodsCat;
- use app\models\PtActivityOrder;
- use app\models\PtActivityOrderDetail;
- use app\models\SaasUser;
- use app\models\User;
- use app\modules\client\models\v1\pt\order\OrderForm;
- use yii\base\Model;
- class PtActivityOrderForm extends Model
- {
- //状态
- public $status;
- //拼团号
- public $pt_number;
- public $pt_order_id;
- public function rules()
- {
- return [
- [['status', 'pt_number', 'pt_order_id'],'integer'],
- ];
- }
- //获取订单列表
- public function search() {
- try {
- $status = $this->status;
- $pay_end_time = time() - (60 * 15);
- $query = PtActivityOrder::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id()]);
- switch ($status) {
- case 1:
- $query->andWhere(['AND', ['is_pay' => 0], ['<>' ,'trade_status', 1]]);
- break;
- case 2:
- $query->andWhere(['AND', ['is_pay' => 1], ['is_pt_finish' => 0]]);
- break;
- case 3:
- $query->andWhere(['AND', ['is_pt_finish' => 1], ['is_winner' => 1]]);
- break;
- case 4:
- $query->andWhere(['OR', ['AND', ['is_pt_finish' => 1], ['is_winner' => 0]], ['AND', ['<', 'created_at', $pay_end_time], ['is_pay' => 0]], ['is_pt_finish' => 2]]);
- break;
- }
- $query->select("id, pay_price, pt_number, is_pay, is_pt_finish, is_winner, pay_time, trade_status, created_at")->orderBy('created_at desc');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- if (!empty($item['pt_number'])) {
- //获取开团订单信息
- $first_order = PtActivityOrder::findOne($item['pt_number']);
- $item['pay_time'] = $first_order->pay_time;
- }
- $item['surplus_num'] = 0;
- $item['end_time'] = 0;
- //商品数据 活动数据
- $item['goods_list'] = PtActivityOrderDetail::find()->where(['order_id' => $item['id']])
- ->select('id, goods_id, pic, goods_name, attr, num, activity_id, total_price')->asArray()->one();
- $pt_number = $item['id'];
- if ((int)$item['pt_number'] > 0) {
- $pt_number = $item['pt_number'];
- }
- if (!empty($item['goods_list'])) {
- $item['goods_list']['attr'] = json_decode($item['goods_list']['attr'], true);
- $pt_activity = PtActivity::findOne($item['goods_list']['activity_id']);
- if ($pt_activity) {
- //拼团剩余数据
- $end_time = (($pt_activity->split_time * 60 * 60) + $item['pay_time']) - time();
- $item['end_time'] = ($end_time > 0) ? $end_time : 0;
- if ((int)$item['is_pt_finish'] !== 0) {
- $item['end_time'] = 0;
- }
- $order_num = PtActivityOrder::find()->where(['is_pay' => 1])
- ->andWhere(['OR' , ['id' => $pt_number], ['pt_number' => $pt_number]])
- ->select('id')->count();
- $item['surplus_num'] = ($pt_activity->party_size - $order_num);
- $item['party_type'] = $pt_activity['party_type'];
- $item['surplus_num_goods'] = 0;
- if($pt_activity['party_type'] == 1){
- $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
- ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
- $goodsCount = $goodsCountQuery->sum('pod.num');
- $item['surplus_num_goods'] = $pt_activity['party_goods_count'] - (int)$goodsCount;
- }
- }
- }
- $item['order_id'] = 0;
- if ((int)$item['is_pay'] === 0 && (int)$item['trade_status'] !== 1) { //待付款
- $item['status'] = 1;
- $order_form = new OrderForm();
- $item['pay_type_list'] = $order_form->getPayTypeList();
- $item['createdDate'] = $item['created_at'];
- }
- if ((int)$item['is_pay'] === 1 && (int)$item['is_pt_finish'] === 0) { //进行中
- $item['status'] = 2;
- }
- if ((int)$item['is_pt_finish'] === 1 && (int)$item['is_winner'] === 1) { //已成功
- $item['status'] = 3;
- $order = Order::findOne(['pt_order_id' => $item['id']]);
- $item['order_id'] = $order->id ?? 0;
- }
- if (((int)$item['is_pt_finish'] === 1 && (int)$item['is_winner'] === 0) || ($item['created_at'] < $pay_end_time && (int)$item['is_pay'] === 0) || (int)$item['is_pt_finish'] === 2) { //已失败
- $item['status'] = 4;
- }
- $item['pt_number'] = (int)$pt_number;
- unset($item['is_pay'], $item['trade_status'], $item['is_pt_finish'], $item['is_winner'], $item['pay_time']);
- }
- return [
- 'code' => 0,
- 'msg' => '',
- 'data' => [
- 'list' => $list['list'] ?? [],
- 'pageNo' => $list['pageNo'] ?? 0,
- 'totalCount' => $list['totalCount'] ?? 0
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 拼团信息 成功 失败 进行中
- */
- public function orderInfo() {
- try {
- $pt_number = $this->pt_number;
- $pt_order_id = $this->pt_order_id;
- if (empty($pt_number)) {
- throw new \Exception("缺少必要参数");
- }
- $order = PtActivityOrder::findOne(['id' => $pt_number, 'is_pay' => 1]);
- if (empty($order)) {
- throw new \Exception("未找到对应的拼团数据");
- }
- //商品信息
- $order_detail = PtActivityOrderDetail::findOne(['order_id' => $pt_number]);
- $goods_id = $order_detail->goods_id;
- $activity_id = $order_detail->activity_id;
- $pt_goods = PtActivityGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0, 'activity_id' => $activity_id]);
- $activity = PtActivity::findOne($activity_id);
- $party_size = $activity->party_size;
- $activity_time = ($activity->split_time * 60 * 60);
- $goods_info = Goods::findOne($pt_goods->goods_id);
- if (empty($order_detail) || empty($pt_goods) || empty($activity) || empty($goods_info)) {
- throw new \Exception("拼团数据错误");
- }
- $goods = [
- 'name' => $order_detail->goods_name,
- 'id' => $goods_id,
- 'party_size' => $party_size,
- 'party_type' => $activity->party_type,
- 'party_goods_count' => $activity->party_goods_count,
- 'sale_num' => ($pt_goods->sale_num + $pt_goods->virtual_sales),
- 'pt_price' => $pt_goods->pt_price,
- 'price' => $goods_info->price,
- 'cover_pic' => $goods_info->cover_pic,
- 'num' => $goods_info->goods_num
- ];
- $pt_attr = json_decode($pt_goods->attr, true);
- $pt_price_arr = array_column($pt_attr, 'pt_price');
- $pt_price_arr = array_filter($pt_price_arr);
- $goods['pt_price'] = sprintf("%.2f", min($pt_price_arr));
- //最高省的钱
- $diff_price = sprintf("%.2f", $goods_info->price - $goods['pt_price']);
- $pt_order = PtActivityOrder::find()->alias('o')
- ->where(['o.is_pay' => 1])
- ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
- ->andWhere(['OR', ['o.id' => $pt_number], ['o.pt_number' => $pt_number]])
- ->select('su.name, su.avatar, o.id, o.is_winner')
- ->orderBy('o.pt_number asc')->asArray()->all();
- // $winner_order_id = 0;
- // foreach ($pt_order as $value) {
- // if ((int)$value['is_winner'] === 1) {
- // $winner_order_id = $value['id'];
- // }
- // }
- $current_num = count($pt_order);
- $surplus_num = $activity->party_type == 0 ? ($party_size - $current_num) : 0;
- $surplus_num_goods = 0;
- if($activity->party_type == 1){
- $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
- ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
- $goodsCount = $goodsCountQuery->sum('pod.num');
- $surplus_num_goods = $activity->party_goods_count - (int)$goodsCount;
- }
- $end_time = 0;
- if ((int)$order->is_pt_finish === 0) {
- $end_time = ($order->pay_time + $activity_time) - time();
- $end_time = $end_time > 0 ? $end_time : 0;
- }
- //查自己是否拼团成功
- $once_order = PtActivityOrder::find()->where(['is_pay' => 1, 'user_id' => get_user_id()])
- ->andWhere(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]])
- ->select('is_winner, is_pt_finish')->asArray()->one();
- $status = 0;
- if (!empty($once_order)) {
- if ((int)$once_order['is_pt_finish'] === 0) { //进行中
- $status = 2;
- }
- if ((int)$once_order['is_pt_finish'] === 1 && (int)$once_order['is_winner'] === 1) {//成功
- $status = 3;
- }
- if (((int)$once_order['is_pt_finish'] === 1 && (int)$once_order['is_winner'] === 0) || $once_order['is_pt_finish'] === 2) {//失败
- $status = 4;
- }
- }
- //获取拼团成功人员
- $pt_msg = PtActivityOrder::find()->alias('o')->where(['is_winner' => 1])
- ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
- ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
- ->leftJoin(['od' => PtActivityOrderDetail::tableName()], 'od.order_id = o.id')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->select('o.total_price, su.avatar, su.name, g.price, o.id')->orderBy('o.created_at desc')
- ->limit(1)
- ->asArray()->one();
- // foreach ($pt_msg as &$msg) {
- // $msg['diff_price'] = sprintf("%.2f", ($msg['price'] - $msg['total_price']));
- // }
- $order = Order::findOne(['pt_order_id' => $pt_order_id]);
- if (!$pt_msg) {
- $pt_msg = null;
- } else {
- $pt_msg['diff_price'] = sprintf("%.2f", ($pt_msg['price'] - $pt_msg['total_price']));
- }
- $order = [
- 'user_list' => $pt_order,
- 'diff_price' => $diff_price,
- 'party_type' => $activity->party_type,
- 'surplus_num_goods' => $surplus_num_goods,
- 'surplus_num' => $surplus_num,
- 'end_time' => $end_time,
- 'status' => $status,
- 'order_id' => $order->id
- ];
- return [
- 'code' => 0,
- 'msg' => "获取成功",
- 'data' => [
- 'goods' => $goods,
- 'order' => $order,
- 'pt_msg' => $pt_msg
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|