PtActivityOrderForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\pt;
  8. use app\models\Goods;
  9. use app\models\Order;
  10. use app\models\PtActivity;
  11. use app\models\PtActivityBanner;
  12. use app\models\PtActivityGoods;
  13. use app\models\PtActivityGoodsCat;
  14. use app\models\PtActivityOrder;
  15. use app\models\PtActivityOrderDetail;
  16. use app\models\SaasUser;
  17. use app\models\User;
  18. use app\modules\client\models\v1\pt\order\OrderForm;
  19. use yii\base\Model;
  20. class PtActivityOrderForm extends Model
  21. {
  22. //状态
  23. public $status;
  24. //拼团号
  25. public $pt_number;
  26. public $pt_order_id;
  27. public function rules()
  28. {
  29. return [
  30. [['status', 'pt_number', 'pt_order_id'],'integer'],
  31. ];
  32. }
  33. //获取订单列表
  34. public function search() {
  35. try {
  36. $status = $this->status;
  37. $pay_end_time = time() - (60 * 15);
  38. $query = PtActivityOrder::find()->where(['store_id' => get_store_id(), 'user_id' => get_user_id()]);
  39. switch ($status) {
  40. case 1:
  41. $query->andWhere(['AND', ['is_pay' => 0], ['<>' ,'trade_status', 1]]);
  42. break;
  43. case 2:
  44. $query->andWhere(['AND', ['is_pay' => 1], ['is_pt_finish' => 0]]);
  45. break;
  46. case 3:
  47. $query->andWhere(['AND', ['is_pt_finish' => 1], ['is_winner' => 1]]);
  48. break;
  49. case 4:
  50. $query->andWhere(['OR', ['AND', ['is_pt_finish' => 1], ['is_winner' => 0]], ['AND', ['<', 'created_at', $pay_end_time], ['is_pay' => 0]], ['is_pt_finish' => 2]]);
  51. break;
  52. }
  53. $query->select("id, pay_price, pt_number, is_pay, is_pt_finish, is_winner, pay_time, trade_status, created_at")->orderBy('created_at desc');
  54. $list = pagination_make($query);
  55. foreach ($list['list'] as &$item) {
  56. if (!empty($item['pt_number'])) {
  57. //获取开团订单信息
  58. $first_order = PtActivityOrder::findOne($item['pt_number']);
  59. $item['pay_time'] = $first_order->pay_time;
  60. }
  61. $item['surplus_num'] = 0;
  62. $item['end_time'] = 0;
  63. //商品数据 活动数据
  64. $item['goods_list'] = PtActivityOrderDetail::find()->where(['order_id' => $item['id']])
  65. ->select('id, goods_id, pic, goods_name, attr, num, activity_id, total_price')->asArray()->one();
  66. $pt_number = $item['id'];
  67. if ((int)$item['pt_number'] > 0) {
  68. $pt_number = $item['pt_number'];
  69. }
  70. if (!empty($item['goods_list'])) {
  71. $item['goods_list']['attr'] = json_decode($item['goods_list']['attr'], true);
  72. $pt_activity = PtActivity::findOne($item['goods_list']['activity_id']);
  73. if ($pt_activity) {
  74. //拼团剩余数据
  75. $end_time = (($pt_activity->split_time * 60 * 60) + $item['pay_time']) - time();
  76. $item['end_time'] = ($end_time > 0) ? $end_time : 0;
  77. if ((int)$item['is_pt_finish'] !== 0) {
  78. $item['end_time'] = 0;
  79. }
  80. $order_num = PtActivityOrder::find()->where(['is_pay' => 1])
  81. ->andWhere(['OR' , ['id' => $pt_number], ['pt_number' => $pt_number]])
  82. ->select('id')->count();
  83. $item['surplus_num'] = ($pt_activity->party_size - $order_num);
  84. $item['party_type'] = $pt_activity['party_type'];
  85. $item['surplus_num_goods'] = 0;
  86. if($pt_activity['party_type'] == 1){
  87. $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
  88. ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
  89. ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
  90. ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
  91. $goodsCount = $goodsCountQuery->sum('pod.num');
  92. $item['surplus_num_goods'] = $pt_activity['party_goods_count'] - (int)$goodsCount;
  93. }
  94. }
  95. }
  96. $item['order_id'] = 0;
  97. if ((int)$item['is_pay'] === 0 && (int)$item['trade_status'] !== 1) { //待付款
  98. $item['status'] = 1;
  99. $order_form = new OrderForm();
  100. $item['pay_type_list'] = $order_form->getPayTypeList();
  101. $item['createdDate'] = $item['created_at'];
  102. }
  103. if ((int)$item['is_pay'] === 1 && (int)$item['is_pt_finish'] === 0) { //进行中
  104. $item['status'] = 2;
  105. }
  106. if ((int)$item['is_pt_finish'] === 1 && (int)$item['is_winner'] === 1) { //已成功
  107. $item['status'] = 3;
  108. $order = Order::findOne(['pt_order_id' => $item['id']]);
  109. $item['order_id'] = $order->id ?? 0;
  110. }
  111. 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) { //已失败
  112. $item['status'] = 4;
  113. }
  114. $item['pt_number'] = (int)$pt_number;
  115. unset($item['is_pay'], $item['trade_status'], $item['is_pt_finish'], $item['is_winner'], $item['pay_time']);
  116. }
  117. return [
  118. 'code' => 0,
  119. 'msg' => '',
  120. 'data' => [
  121. 'list' => $list['list'] ?? [],
  122. 'pageNo' => $list['pageNo'] ?? 0,
  123. 'totalCount' => $list['totalCount'] ?? 0
  124. ]
  125. ];
  126. } catch (\Exception $e) {
  127. return [
  128. 'code' => 1,
  129. 'msg' => $e->getMessage()
  130. ];
  131. }
  132. }
  133. /**
  134. * 拼团信息 成功 失败 进行中
  135. */
  136. public function orderInfo() {
  137. try {
  138. $pt_number = $this->pt_number;
  139. $pt_order_id = $this->pt_order_id;
  140. if (empty($pt_number)) {
  141. throw new \Exception("缺少必要参数");
  142. }
  143. $order = PtActivityOrder::findOne(['id' => $pt_number, 'is_pay' => 1]);
  144. if (empty($order)) {
  145. throw new \Exception("未找到对应的拼团数据");
  146. }
  147. //商品信息
  148. $order_detail = PtActivityOrderDetail::findOne(['order_id' => $pt_number]);
  149. $goods_id = $order_detail->goods_id;
  150. $activity_id = $order_detail->activity_id;
  151. $pt_goods = PtActivityGoods::findOne(['goods_id' => $goods_id, 'is_delete' => 0, 'activity_id' => $activity_id]);
  152. $activity = PtActivity::findOne($activity_id);
  153. $party_size = $activity->party_size;
  154. $activity_time = ($activity->split_time * 60 * 60);
  155. $goods_info = Goods::findOne($pt_goods->goods_id);
  156. if (empty($order_detail) || empty($pt_goods) || empty($activity) || empty($goods_info)) {
  157. throw new \Exception("拼团数据错误");
  158. }
  159. $goods = [
  160. 'name' => $order_detail->goods_name,
  161. 'id' => $goods_id,
  162. 'party_size' => $party_size,
  163. 'party_type' => $activity->party_type,
  164. 'party_goods_count' => $activity->party_goods_count,
  165. 'sale_num' => ($pt_goods->sale_num + $pt_goods->virtual_sales),
  166. 'pt_price' => $pt_goods->pt_price,
  167. 'price' => $goods_info->price,
  168. 'cover_pic' => $goods_info->cover_pic,
  169. 'num' => $goods_info->goods_num
  170. ];
  171. $pt_attr = json_decode($pt_goods->attr, true);
  172. $pt_price_arr = array_column($pt_attr, 'pt_price');
  173. $pt_price_arr = array_filter($pt_price_arr);
  174. $goods['pt_price'] = sprintf("%.2f", min($pt_price_arr));
  175. //最高省的钱
  176. $diff_price = sprintf("%.2f", $goods_info->price - $goods['pt_price']);
  177. $pt_order = PtActivityOrder::find()->alias('o')
  178. ->where(['o.is_pay' => 1])
  179. ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
  180. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  181. ->andWhere(['OR', ['o.id' => $pt_number], ['o.pt_number' => $pt_number]])
  182. ->select('su.name, su.avatar, o.id, o.is_winner')
  183. ->orderBy('o.pt_number asc')->asArray()->all();
  184. // $winner_order_id = 0;
  185. // foreach ($pt_order as $value) {
  186. // if ((int)$value['is_winner'] === 1) {
  187. // $winner_order_id = $value['id'];
  188. // }
  189. // }
  190. $current_num = count($pt_order);
  191. $surplus_num = $activity->party_type == 0 ? ($party_size - $current_num) : 0;
  192. $surplus_num_goods = 0;
  193. if($activity->party_type == 1){
  194. $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
  195. ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
  196. ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
  197. ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
  198. $goodsCount = $goodsCountQuery->sum('pod.num');
  199. $surplus_num_goods = $activity->party_goods_count - (int)$goodsCount;
  200. }
  201. $end_time = 0;
  202. if ((int)$order->is_pt_finish === 0) {
  203. $end_time = ($order->pay_time + $activity_time) - time();
  204. $end_time = $end_time > 0 ? $end_time : 0;
  205. }
  206. //查自己是否拼团成功
  207. $once_order = PtActivityOrder::find()->where(['is_pay' => 1, 'user_id' => get_user_id()])
  208. ->andWhere(['OR', ['id' => $pt_number], ['pt_number' => $pt_number]])
  209. ->select('is_winner, is_pt_finish')->asArray()->one();
  210. $status = 0;
  211. if (!empty($once_order)) {
  212. if ((int)$once_order['is_pt_finish'] === 0) { //进行中
  213. $status = 2;
  214. }
  215. if ((int)$once_order['is_pt_finish'] === 1 && (int)$once_order['is_winner'] === 1) {//成功
  216. $status = 3;
  217. }
  218. if (((int)$once_order['is_pt_finish'] === 1 && (int)$once_order['is_winner'] === 0) || $once_order['is_pt_finish'] === 2) {//失败
  219. $status = 4;
  220. }
  221. }
  222. //获取拼团成功人员
  223. $pt_msg = PtActivityOrder::find()->alias('o')->where(['is_winner' => 1])
  224. ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
  225. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  226. ->leftJoin(['od' => PtActivityOrderDetail::tableName()], 'od.order_id = o.id')
  227. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
  228. ->select('o.total_price, su.avatar, su.name, g.price, o.id')->orderBy('o.created_at desc')
  229. ->limit(1)
  230. ->asArray()->one();
  231. // foreach ($pt_msg as &$msg) {
  232. // $msg['diff_price'] = sprintf("%.2f", ($msg['price'] - $msg['total_price']));
  233. // }
  234. $order = Order::findOne(['pt_order_id' => $pt_order_id]);
  235. if (!$pt_msg) {
  236. $pt_msg = null;
  237. } else {
  238. $pt_msg['diff_price'] = sprintf("%.2f", ($pt_msg['price'] - $pt_msg['total_price']));
  239. }
  240. $order = [
  241. 'user_list' => $pt_order,
  242. 'diff_price' => $diff_price,
  243. 'party_type' => $activity->party_type,
  244. 'surplus_num_goods' => $surplus_num_goods,
  245. 'surplus_num' => $surplus_num,
  246. 'end_time' => $end_time,
  247. 'status' => $status,
  248. 'order_id' => $order->id
  249. ];
  250. return [
  251. 'code' => 0,
  252. 'msg' => "获取成功",
  253. 'data' => [
  254. 'goods' => $goods,
  255. 'order' => $order,
  256. 'pt_msg' => $pt_msg
  257. ]
  258. ];
  259. } catch (\Exception $e) {
  260. return [
  261. 'code' => 1,
  262. 'msg' => $e->getMessage()
  263. ];
  264. }
  265. }
  266. }