TeamForm.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Goods;
  9. use app\models\Level;
  10. use app\models\Md;
  11. use app\models\Option;
  12. use app\models\Order;
  13. use app\models\OrderDetail;
  14. use app\models\OrderRefund;
  15. use app\models\OrderShare;
  16. use app\models\SaasUser;
  17. use app\models\ShareLevel;
  18. use app\models\UserShareMoney;
  19. use yii\base\Model;
  20. use yii\data\Pagination;
  21. use yii\helpers\Json;
  22. use app\models\User;
  23. use app\models\Share;
  24. use app\models\OrderPospal;
  25. class TeamForm extends Model
  26. {
  27. public $yinbao = 0;
  28. public $user_id;
  29. public $store_id;
  30. public $share_setting;
  31. public $status;
  32. public $page;
  33. public $limit;
  34. public $child_user_id;
  35. public function rules()
  36. {
  37. return [
  38. [['status', 'page', 'limit', 'status', 'child_user_id'], 'integer'],
  39. [['page',], 'default', 'value' => 1],
  40. [['limit',], 'default', 'value' => 10],
  41. [['status'], 'in', 'range' => [1, 2, 3], 'on' => 'TEAM'],
  42. [['status'], 'in', 'range' => [-1, 0, 1, 2, 3], 'on' => 'ORDER'],
  43. [['store_id', 'yinbao'],'integer']
  44. ];
  45. }
  46. /**
  47. * 团队信息
  48. * @return array|mixed
  49. */
  50. public function getList()
  51. {
  52. if (!$this->validate()) {
  53. return [
  54. 'code' => 1,
  55. 'msg' => $this->getErrorSummary(false)[0],
  56. ];
  57. }
  58. $exit = Share::find()->andWhere(['user_id' => $this->user_id, 'is_delete' => 0])->exists();
  59. $user = User::findOne(['id' => $this->user_id]);
  60. // $setting = Setting::findOne(['store_id' => $this->store_id]);
  61. $setting = Option::get('share_basic_setting', $this->store_id);
  62. $setting = $setting ? Json::decode($setting['value']) : [];
  63. if ($setting['level']['value'] == 0) {
  64. return [
  65. 'code' => 1,
  66. 'msg' => '网络异常',
  67. 'data' => []
  68. ];
  69. }
  70. if (!$exit || $user->is_distributor != 1) {
  71. return [
  72. 'code' => 1,
  73. 'msg' => '网络异常',
  74. 'data' => []
  75. ];
  76. }
  77. $team = self::team($this->store_id, $this->user_id);
  78. $user_list = $team[1];
  79. $data = $team[0];
  80. if ($setting['level']['value'] > 0 && $this->status == 1) {
  81. $data['list'] = $user_list['f_c'];
  82. }
  83. if ($this->status == 2 && $setting['level']['value'] > 1) {
  84. $data['list'] = $user_list['s_c'];
  85. }
  86. if ($this->status == 3 && $setting['level']['value'] > 2) {
  87. $data['list'] = $user_list['t_c'];
  88. }
  89. foreach ($data['list'] as $index => $value) {
  90. $data['list'][$index]['time'] = date('Y-m-d', $value['created_at']);
  91. $child_count = User::find()->where(['old_parent_id' => $value['id'], 'is_delete' => 0])->count();
  92. $data['list'][$index]['child_count'] = $child_count ? $child_count : 0;
  93. }
  94. return [
  95. 'code' => 0,
  96. 'msg' => '',
  97. 'data' => $data
  98. ];
  99. }
  100. /**
  101. * 获取团队信息
  102. * @param $store_id
  103. * @param $user_id
  104. * @return array
  105. */
  106. public static function team($store_id, $user_id)
  107. {
  108. // $share_setting = Setting::findOne(['store_id' => $store_id]);
  109. $share_setting = Option::get('share_basic_setting', $store_id);
  110. $share_setting = $share_setting ? Json::decode($share_setting['value']) : [];
  111. $list = User::find()->alias('u')
  112. ->where(['and', ['u.is_delete' => 0, 'u.store_id' => $store_id], ['>', 'u.old_parent_id', 0]])
  113. ->leftJoin(Order::tableName() . ' o', "o.is_price=1 and o.user_id=u.id and o.parent_id = u.parent_id")
  114. ->andWhere([
  115. 'or',
  116. ['o.is_delete' => 0, ['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]],
  117. 'isnull(o.id)'
  118. ])
  119. ->select([
  120. "sum(case when isnull(o.id) then 0 else o.pay_price end) price",
  121. 'count(o.id) count',
  122. 'u.nickname', 'u.created_at', 'u.parent_id', 'u.old_parent_id', 'u.id', 'u.avatar_url'
  123. ])
  124. ->groupBy('u.id')
  125. ->asArray()->all();
  126. $user_list = array();
  127. $data = [];
  128. $data['first'] = 0;
  129. $data['second'] = 0;
  130. $data['third'] = 0;
  131. $data['list'] = [];
  132. $user_list['f_c'] = [];
  133. $user_list['s_c'] = [];
  134. $user_list['t_c'] = [];
  135. //获取用户下线的数量及订单情况
  136. foreach ($list as $index => $value) {
  137. if ($value['old_parent_id'] == $user_id) {
  138. $data['first']++;
  139. $user_list['f_c'][] = $value;
  140. if ($share_setting['level']['value'] > 1) {
  141. foreach ($list as $i => $v) {
  142. if ($v['old_parent_id'] == $value['id']) {
  143. $data['second']++;
  144. $user_list['s_c'][] = $v;
  145. if ($share_setting['level']['value'] > 2) {
  146. foreach ($list as $j => $item) {
  147. if ($item['old_parent_id'] == $v['id']) {
  148. $data['third']++;
  149. $user_list['t_c'][] = $item;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. return [$data, $user_list];
  159. }
  160. /**
  161. * 获取分销团队总人数
  162. * @return array
  163. */
  164. public function getTeamCount()
  165. {
  166. $share_basic_setting = Option::get('share_basic_setting', $this->store_id, 'share', '[]');
  167. $share_basic_setting = json_decode($share_basic_setting['value'], true);
  168. $share_money_setting = Option::get('share_money_setting', $this->store_id, 'share', '[]');
  169. $share_money_setting = json_decode($share_money_setting['value'], true);
  170. $this->share_setting = array_merge($share_basic_setting, $share_money_setting);
  171. $team = [
  172. 'f_c' => [],
  173. 's_c' => [],
  174. 't_c' => []
  175. ];
  176. // if (!$this->share_setting || $this->share_setting['level']['value'] == 0) {
  177. // return [
  178. // 'team_count' => 0,
  179. // 'team' => $team
  180. // ];
  181. // }
  182. // if ($this->share_setting['level']['value'] == 4) {
  183. // return [
  184. // 'team_count' => 0,
  185. // 'team' => $team
  186. // ];
  187. // }
  188. $first = User::find()->select(['id'])
  189. ->where(['store_id' => $this->store_id, 'old_parent_id' => $this->user_id, 'is_delete' => 0, 'type' => 1])->column();
  190. $count = count($first);
  191. $team['f_c'] = $first;
  192. $second = User::find()->select(['id'])
  193. ->where(['store_id' => $this->store_id, 'old_parent_id' => $first, 'is_delete' => 0, 'type' => 1])->column();
  194. $count += count($second);
  195. $team['s_c'] = $second;
  196. $third = User::find()->select(['id'])
  197. ->where(['store_id' => $this->store_id, 'old_parent_id' => $second, 'is_delete' => 0, 'type' => 1])->column();
  198. $count += count($third);
  199. $team['t_c'] = $third;
  200. return [
  201. 'team_count' => $count,
  202. 'team' => $team
  203. ];
  204. }
  205. /**
  206. * 获取订单数量
  207. * @return array
  208. * @throws \yii\db\Exception
  209. */
  210. public function getOrderCount()
  211. {
  212. $arr = $this->getTeamCount();
  213. $team_arr = $arr['team'];
  214. $sql = $this->getSql();
  215. $order_count = 0;
  216. $andWhere_1 = " WHERE al.user_id != {$this->user_id} AND al.parent_id_1 = {$this->user_id} AND al.first_price > 0";
  217. $order_money = 0;
  218. $order_money_un = 0;
  219. $select_1 = "SELECT al.first_price,al.is_price, al.id";
  220. $first_price = \Yii::$app->db->createCommand($select_1 . $sql . $andWhere_1)->queryAll();
  221. if ($first_price) {
  222. foreach ($first_price as $index => $value) {
  223. $order_count++;
  224. $order_money += doubleval($value['first_price']);
  225. if ($value['is_price'] == 0) {
  226. $order_money_un += doubleval($value['first_price']);
  227. }
  228. }
  229. }
  230. $team_f = '(' . implode(',', $team_arr['f_c']) . ')';
  231. $team_s = '(' . implode(',', $team_arr['s_c']) . ')';
  232. $team_t = '(' . implode(',', $team_arr['t_c']) . ')';
  233. if (!empty($team_arr['s_c'])) {
  234. $select_2 = "SELECT al.second_price,al.is_price, al.id";
  235. $andWhere_2 = " WHERE al.user_id != {$this->user_id} AND al.second_price > 0 AND
  236. ((al.user_id IN {$team_s} AND al.parent_id_1 IN {$team_f} AND al.parent_id_2 = 0)
  237. OR (al.parent_id_2 = {$this->user_id}))";
  238. $second_price = \Yii::$app->db->createCommand($select_2 . $sql . $andWhere_2)->queryAll();
  239. if ($second_price) {
  240. foreach ($second_price as $index => $value) {
  241. $order_count++;
  242. $order_money += doubleval($value['second_price']);
  243. if ($value['is_price'] == 0) {
  244. $order_money_un += doubleval($value['second_price']);
  245. }
  246. }
  247. }
  248. }
  249. if (!empty($team_arr['t_c'])) {
  250. $select_3 = "SELECT al.third_price,al.is_price, al.id";
  251. $andWhere_3 = " WHERE al.user_id != {$this->user_id} AND al.third_price > 0 AND
  252. ((al.user_id IN {$team_t} AND al.parent_id_1 IN {$team_s} AND al.parent_id_2 = 0)
  253. OR (al.parent_id_3 = {$this->user_id}))";
  254. $third_price = \Yii::$app->db->createCommand($select_3 . $sql . $andWhere_3)->queryAll();
  255. if ($third_price) {
  256. foreach ($third_price as $index => $value) {
  257. $order_count++;
  258. $order_money += doubleval($value['third_price']);
  259. if ($value['is_price'] == 0) {
  260. $order_money_un += doubleval($value['third_price']);
  261. }
  262. }
  263. }
  264. }
  265. $select = "SELECT al.rebate,al.is_price,al.id {$sql} WHERE al.rebate > 0 AND al.user_id = {$this->user_id}";
  266. $rebate = \Yii::$app->db->createCommand($select)->queryAll();
  267. if ($rebate) {
  268. foreach ($rebate as $index => $value) {
  269. $order_count++;
  270. $order_money += doubleval($value['rebate']);
  271. if ($value['is_price'] == 0) {
  272. $order_money_un += doubleval($value['rebate']);
  273. }
  274. }
  275. }
  276. // 因为存在佣金,积分,余额多种发放模式,目前分销中心展示订单数量
  277. $arr['order_count'] = $order_count;
  278. $arr['share_order_count'] = UserShareMoney::find()->where(['store_id' => get_store_id(), 'user_id' => $this->user_id,
  279. 'source' => 5, 'type' => 0])->count();
  280. $arr['order_money'] = doubleval(sprintf('%.2f', $order_money));
  281. $arr['order_money_un'] = doubleval(sprintf('%.2f', $order_money_un));
  282. return $arr;
  283. }
  284. /**
  285. * 获取分销订单详情
  286. * @return array
  287. * @throws \yii\db\Exception
  288. */
  289. public function getOrder()
  290. {
  291. if (!$this->validate()) {
  292. return [
  293. 'code' => 1,
  294. 'msg' => $this->getErrorSummary(false)[0],
  295. ];
  296. }
  297. $arr = $this->getTeamCount();
  298. $team_arr = $arr['team'];
  299. $share_setting = Option::get('share_basic_setting', $this->store_id);
  300. $share_setting = $share_setting ? Json::decode($share_setting['value']) : [];
  301. $first = '(' . implode(',', $team_arr['f_c']) . ')';
  302. $second = '(' . implode(',', $team_arr['s_c']) . ')';
  303. $third = '(' . implode(',', $team_arr['t_c']) . ')';
  304. $all = '(' . implode(',', array_merge($team_arr['f_c'], $team_arr['s_c'], $team_arr['t_c'])) . ')';
  305. $user_table = User::tableName();
  306. $saas_user_table = SaasUser::tableName();
  307. $sql = $this->getSql();
  308. $select = "SELECT al.*,su.name as nickname,su.avatar as avatar_url ";
  309. $where = " WHERE ((al.user_id = {$this->user_id} AND al.rebate > 0)";
  310. if ($share_setting['level']['value'] != 4) {
  311. $where .= "OR (al.user_id != {$this->user_id} ";
  312. if ($share_setting['level']['value'] >= 1) {
  313. $where .= " AND (al.parent_id_1 = {$this->user_id} AND al.first_price > 0) ";
  314. }
  315. if ($share_setting['level']['value'] >= 2) {
  316. $where .= " OR (al.parent_id_2 = {$this->user_id} AND al.second_price > 0) ";
  317. }
  318. if ($share_setting['level']['value'] == 3) {
  319. $where .= " OR (al.parent_id_3 = {$this->user_id} AND al.third_price > 0) ";
  320. }
  321. if (!empty($team_arr['f_c']) && !empty($team_arr['s_c'])) {
  322. $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$second} AND al.parent_id_1 IN {$first} AND al.second_price > 0) ";
  323. }
  324. if (!empty($team_arr['s_c']) && !empty($team_arr['t_c'])) {
  325. $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$third} AND al.parent_id_1 IN {$second} AND al.third_price > 0)";
  326. }
  327. $where .= ")";
  328. }
  329. $where .= ")";
  330. $sql = $sql . " LEFT JOIN {$user_table} AS u ON u.id=al.user_id LEFT JOIN {$saas_user_table} AS su ON su.mobile=u.binding {$where}";
  331. if(!$this->yinbao){
  332. if ($this->status == 0) {
  333. $sql = $sql . " AND al.trade_status = -1 ";
  334. } elseif ($this->status == 1) {
  335. $sql = $sql . " AND al.is_pay = 1 AND al.trade_status IN (0,2,3) and al.is_price = 0";
  336. } elseif ($this->status == 2) {
  337. $sql = $sql . " AND al.trade_status = 2";
  338. } elseif ($this->status == 3) {
  339. $sql = $sql . " AND al.trade_status = 3 and is_price = 1";
  340. }
  341. }
  342. if ($this->child_user_id) {
  343. $sql = $sql . " AND al.user_id = " . $this->child_user_id;
  344. }
  345. $sql = $sql . " GROUP BY al.id ,al.order_type ";
  346. $count = \Yii::$app->db->createCommand("select count(*)".$sql)->execute();
  347. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  348. $list = \Yii::$app->db->createCommand($select . $sql . "ORDER BY al.created_at DESC LIMIT {$pagination->limit} OFFSET {$pagination->offset}")->queryAll();
  349. $new_list = [];
  350. $setting = Option::get('share_money_setting', $this->store_id);
  351. $setting = $setting ? Json::decode($setting['value']) : [];
  352. foreach ($list as $index => $value) {
  353. $new_list[$index]['status'] = intval($value['trade_status']);
  354. $new_list[$index]['share_send_type'] = $value['share_send_type'];
  355. $new_list[$index]['order_type'] = $value['order_type'];
  356. $new_list[$index]['order_no'] = $value['order_no'];
  357. $new_list[$index]['nickname'] = $value['nickname'];
  358. $new_list[$index]['avatar_url'] = $value['avatar_url'];
  359. $new_list[$index]['is_price'] = $value['is_price'];
  360. $new_list[$index]['status_text'] = Order::getStatusText($value['trade_status']);// "待付款";
  361. // if ($value['is_pay'] == 0) {
  362. // $new_list[$index]['status'] = "待付款";
  363. // } elseif ($value['is_pay'] == 1 && $value['is_price'] == 0) {
  364. // $new_list[$index]['status'] = "已付款";
  365. // } elseif ($value['is_price'] == 1) {
  366. // $new_list[$index]['status'] = "已完成";
  367. // }
  368. if ($value['parent_id_2'] == 0) {
  369. if ($this->user_id == $value['parent_id_1']) {
  370. $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级";
  371. $new_list[$index]['share_money'] = $value['first_price'];
  372. } elseif (in_array($value['user_id'], $team_arr['s_c']) && in_array($value['parent_id_1'], $team_arr['f_c'])) {
  373. $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级";
  374. $new_list[$index]['share_money'] = $value['second_price'];
  375. } elseif (in_array($value['user_id'], $team_arr['t_c']) && in_array($value['parent_id_1'], $team_arr['s_c'])) {
  376. $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级";
  377. $new_list[$index]['share_money'] = $value['third_price'];
  378. }
  379. } else {
  380. if ($value['parent_id_1'] == $this->user_id) {
  381. $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级";
  382. $new_list[$index]['share_money'] = $value['first_price'];
  383. } elseif ($value['parent_id_2'] == $this->user_id) {
  384. $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级";
  385. $new_list[$index]['share_money'] = $value['second_price'];
  386. } elseif ($value['parent_id_3'] == $this->user_id) {
  387. $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级";
  388. $new_list[$index]['share_money'] = $value['third_price'];
  389. }
  390. }
  391. if ($value['rebate'] > 0 && $value['user_id'] == $this->user_id) {
  392. $new_list[$index]['share_status'] = "自购返现";
  393. $new_list[$index]['share_money'] = $value['rebate'];
  394. }
  395. // 订单商品详情
  396. $new_list[$index]['orderDetail'] = $this->getOrderDetail($value['id']);
  397. $refund = OrderRefund::findOne(['order_id' => $value['id'], 'is_delete' => 0,
  398. 'store_id' => $this->store_id, 'type' => 1]);
  399. if ($refund) {
  400. if ($refund['status'] == 1) {
  401. $new_list[$index]['status_text'] = "已退款";
  402. } elseif ($refund['status'] == 0) {
  403. $new_list[$index]['status_text'] = '售后申请中';
  404. }
  405. }
  406. }
  407. return [
  408. 'code' => 0,
  409. 'msg' => 'success',
  410. 'data' => [
  411. 'row_count' => $count,
  412. 'page_count' => $pagination->pageCount,
  413. 'list' => $new_list,
  414. ]
  415. ];
  416. }
  417. /**
  418. * 获取分销订单详情2
  419. */
  420. public function getOrder2()
  421. {
  422. if (!$this->validate()) {
  423. return [
  424. 'code' => 1,
  425. 'msg' => $this->getErrorSummary(false)[0],
  426. ];
  427. }
  428. $arr = $this->getTeamCount();
  429. $team_arr = $arr['team'];
  430. $share_setting = Option::get('share_basic_setting', $this->store_id);
  431. $share_setting = $share_setting ? Json::decode($share_setting['value']) : [];
  432. $first = '(' . implode(',', $team_arr['f_c']) . ')';
  433. $second = '(' . implode(',', $team_arr['s_c']) . ')';
  434. $third = '(' . implode(',', $team_arr['t_c']) . ')';
  435. $user_table = User::tableName();
  436. $saas_user_table = SaasUser::tableName();
  437. $sql = $this->getSql2();
  438. $select = "SELECT al.*,su.name as nickname,su.avatar as avatar_url ";
  439. $where = " WHERE ((al.user_id = {$this->user_id} AND al.rebate > 0)";
  440. if ($share_setting['level']['value'] != 4) {
  441. $where .= "OR (al.user_id != {$this->user_id} ";
  442. if ($share_setting['level']['value'] >= 1) {
  443. $where .= " AND ((al.parent_id_1 = {$this->user_id}) ";
  444. }
  445. if ($share_setting['level']['value'] >= 2) {
  446. $where .= " OR (al.parent_id_2 = {$this->user_id}) ";
  447. }
  448. if ($share_setting['level']['value'] == 3) {
  449. $where .= " OR (al.parent_id_3 = {$this->user_id}) ";
  450. }
  451. if (!empty($team_arr['f_c']) && !empty($team_arr['s_c'])) {
  452. $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$second} AND al.parent_id_1 IN {$first}) ";
  453. }
  454. if (!empty($team_arr['s_c']) && !empty($team_arr['t_c'])) {
  455. $where .= " OR (al.parent_id_2 = 0 AND al.user_id IN {$third} AND al.parent_id_1 IN {$second})";
  456. }
  457. $where .= "))";
  458. }
  459. $where .= ")";
  460. $sql = $sql . " LEFT JOIN {$user_table} AS u ON u.id=al.user_id LEFT JOIN {$saas_user_table} AS su ON su.mobile=u.binding {$where}";
  461. if(!$this->yinbao){
  462. if ($this->status == 0) {
  463. $sql = $sql . " AND al.is_pay = 0 ";
  464. } elseif ($this->status == 1) {
  465. $sql = $sql . " AND al.is_pay = 1";
  466. } elseif ($this->status == 2) {
  467. $sql = $sql . " AND al.is_price = 1 ";
  468. }
  469. }
  470. $sql = $sql . " GROUP BY al.id ,al.order_type ";
  471. $count = \Yii::$app->db->createCommand("select count(*)".$sql)->execute();
  472. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  473. $list = \Yii::$app->db->createCommand($select . $sql . "ORDER BY al.created_at DESC LIMIT {$pagination->limit} OFFSET {$pagination->offset}")->queryAll();
  474. $new_list = [];
  475. $setting = Option::get('share_money_setting', $this->store_id);
  476. $setting = $setting ? Json::decode($setting['value']) : [];
  477. foreach ($list as $index => $value) {
  478. $new_list[$index]['id'] = $value['id'];
  479. $new_list[$index]['md_id'] = $value['md_id'];
  480. $new_list[$index]['share_send_type'] = $value['share_send_type'];
  481. $new_list[$index]['order_type'] = $value['order_type'];
  482. $new_list[$index]['order_no'] = $value['order_no'];
  483. $new_list[$index]['nickname'] = $value['nickname'];
  484. $new_list[$index]['avatar_url'] = $value['avatar_url'];
  485. $new_list[$index]['is_price'] = $value['is_price'];
  486. $new_list[$index]['is_offline'] = $value['is_offline'];
  487. $new_list[$index]['status'] = "待付款";
  488. if ($value['is_pay'] == 0) {
  489. $new_list[$index]['status'] = "待付款";
  490. } elseif ($value['is_pay'] == 1 && $value['is_price'] == 0) {
  491. $new_list[$index]['status'] = "已付款";
  492. } elseif ($value['is_price'] == 1) {
  493. $new_list[$index]['status'] = "已完成";
  494. }
  495. if ($value['parent_id_2'] == 0) {
  496. if ($this->user_id == $value['parent_id_1']) {
  497. $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级";
  498. $new_list[$index]['share_money'] = $value['first_price'];
  499. } elseif (in_array($value['user_id'], $team_arr['s_c']) && in_array($value['parent_id_1'], $team_arr['f_c'])) {
  500. $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级";
  501. $new_list[$index]['share_money'] = $value['second_price'];
  502. } elseif (in_array($value['user_id'], $team_arr['t_c']) && in_array($value['parent_id_1'], $team_arr['s_c'])) {
  503. $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级";
  504. $new_list[$index]['share_money'] = $value['third_price'];
  505. }
  506. } else {
  507. if ($value['parent_id_1'] == $this->user_id) {
  508. $new_list[$index]['share_status'] = $setting['level_one']['text'] ? $setting['level_one']['text'] : "一级";
  509. $new_list[$index]['share_money'] = $value['first_price'];
  510. } elseif ($value['parent_id_2'] == $this->user_id) {
  511. $new_list[$index]['share_status'] = $setting['level_two']['text'] ? $setting['level_two']['text'] : "二级";
  512. $new_list[$index]['share_money'] = $value['second_price'];
  513. } elseif ($value['parent_id_3'] == $this->user_id) {
  514. $new_list[$index]['share_status'] = $setting['level_three']['text'] ? $setting['level_three']['text'] : "三级";
  515. $new_list[$index]['share_money'] = $value['third_price'];
  516. }
  517. }
  518. if ($value['rebate'] > 0 && $value['user_id'] == $this->user_id) {
  519. $new_list[$index]['share_status'] = "自购返现";
  520. $new_list[$index]['share_money'] = $value['rebate'];
  521. }
  522. // 订单商品详情
  523. $new_list[$index]['orderDetail'] = $this->getOrderDetail($value['id']);
  524. $new_list[$index]['md_info'] = null;
  525. if ($value['md_id']) {
  526. $new_list[$index]['md_info'] = Md::find()->where(['id' => $value['md_id']])
  527. ->select('id, name, address, cover_url, start_time, end_time, mobile, open_status, shop_time_type, is_single')->one();
  528. }
  529. }
  530. return [
  531. 'code' => 0,
  532. 'msg' => 'success',
  533. 'data' => [
  534. 'row_count' => $count,
  535. 'page_count' => $pagination->pageCount,
  536. 'list' => $new_list,
  537. ]
  538. ];
  539. }
  540. // 订单详情--商城
  541. public function getOrderDetail($id)
  542. {
  543. $list = OrderDetail::find()->alias('od')->where(['od.order_id' => $id])
  544. ->leftJoin(['g' => Goods::tableName()], 'g.id=od.goods_id')
  545. ->select(['od.id', 'od.num', 'od.pic goods_pic','od.total_price', 'od.attr', 'g.name', 'g.cover_pic'])
  546. ->asArray()->all();
  547. foreach ($list as $index => $value) {
  548. if (!$value['goods_pic']) {
  549. $list[$index]['goods_pic'] = $value['cover_pic'];
  550. }
  551. }
  552. return $list;
  553. }
  554. // 订单详情--秒杀
  555. private function getMsOrderDetail($id)
  556. {
  557. $one = MsOrder::find()->alias('o')
  558. ->leftJoin(['g' => MsGoods::tableName()], 'g.id=o.goods_id')
  559. ->where(['o.store_id' => $this->store_id, 'o.id' => $id])
  560. ->select(['o.id', 'o.num', 'o.pic goods_pic', 'g.name'])->asArray()->one();
  561. $list[] = $one;
  562. return $list;
  563. }
  564. // 订单详情--拼团
  565. public function getPtOrderDetail($id)
  566. {
  567. $list = PtOrderDetail::find()->alias('od')
  568. ->leftJoin(['g' => PtGoods::tableName()], 'od.goods_id=g.id')
  569. ->where(['od.order_id' => $id, 'od.is_delete' => 0])
  570. ->select(['od.id', 'od.num', 'od.pic goods_pic', 'g.name'])->asArray()->all();
  571. return $list;
  572. }
  573. // 订单详情--预约
  574. public function getYyOrderDetail($id)
  575. {
  576. $list = YyOrder::find()->alias('o')
  577. ->leftJoin(['g' => YyGoods::tableName()], 'o.goods_id=g.id')
  578. ->where(['o.id' => $id, 'o.store_id' => $this->store_id])
  579. ->select(['o.id', 'g.cover_pic goods_pic', 'g.name'])->asArray()->all();
  580. foreach ($list as $index => $value) {
  581. $list[$index]['num'] = 1;
  582. }
  583. return $list;
  584. }
  585. //获取团队详情
  586. public function getTeam()
  587. {
  588. if (!$this->validate()) {
  589. return [
  590. 'code' => 1,
  591. 'msg' => $this->getErrorSummary(false)[0],
  592. ];
  593. }
  594. $arr = $this->getTeamCount();
  595. $team_arr = $arr['team'];
  596. $data = [];
  597. $data['first'] = count($team_arr['f_c']);
  598. $data['second'] = count($team_arr['s_c']);
  599. $data['third'] = count($team_arr['t_c']);
  600. if (empty($team_arr['f_c'])) {
  601. $data['list'] = [];
  602. return [
  603. 'code' => 0,
  604. 'msg' => '',
  605. 'data' => $data
  606. ];
  607. }
  608. $first = '(' . implode(',', $team_arr['f_c']) . ')';
  609. $second = '(' . implode(',', $team_arr['s_c']) . ')';
  610. $third = '(' . implode(',', $team_arr['t_c']) . ')';
  611. $user_table = User::tableName();
  612. $sql = $this->getSql();
  613. $select = "( SELECT al.* ";
  614. $sql = $select . $sql;
  615. $query = "SELECT
  616. u.nickname, u.created_at, u.parent_id,u.old_parent_id, u.id, u.avatar_url,count(al.id) count, u.binding,
  617. sum(case when isnull(al.id) then 0 else al.pay_price end) price, u.remark_name, u.level
  618. FROM {$user_table} AS u ";
  619. if ($this->status == 1) {
  620. $sql .= " WHERE al.parent_id_1 = {$this->user_id} AND al.is_price = 1 )";
  621. $query .= " LEFT JOIN {$sql} as al ON al.user_id =u.id
  622. WHERE u.store_id = {$this->store_id} AND u.id IN {$first} ";
  623. } elseif ($this->status == 2) {
  624. if (empty($team_arr['s_c'])) {
  625. $data['list'] = [];
  626. return [
  627. 'code' => 0,
  628. 'msg' => '',
  629. 'data' => $data
  630. ];
  631. }
  632. $sql .= " WHERE (
  633. (al.user_id IN {$second} AND al.parent_id_1 IN {$first} AND al.parent_id_2 = 0)
  634. OR (al.parent_id_2 = {$this->user_id})
  635. )AND al.is_price = 1)";
  636. $query .= " LEFT JOIN {$sql} as al ON al.user_id =u.id
  637. WHERE u.store_id = {$this->store_id} AND u.id IN {$second}";
  638. } else {
  639. if (empty($team_arr['t_c']) || empty($team_arr['s_c'])) {
  640. $data['list'] = [];
  641. return [
  642. 'code' => 0,
  643. 'msg' => '',
  644. 'data' => $data
  645. ];
  646. }
  647. $sql .= " WHERE (
  648. ( al.user_id IN {$third} AND al.parent_id_2 IN {$second} AND al.parent_id_2 = 0)
  649. OR (al.parent_id_3 = {$this->user_id})
  650. ) AND al.is_price = 1)";
  651. $query .= " LEFT JOIN {$sql} as al ON al.user_id =u.id
  652. WHERE u.store_id = {$this->store_id} AND u.id IN {$third}";
  653. }
  654. $count = \Yii::$app->db->createCommand($query)->query()->count();
  655. $pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);
  656. $list = \Yii::$app->db->createCommand($query . " GROUP BY u.id ORDER BY u.created_at DESC LIMIT {$pagination->limit} OFFSET {$pagination->offset}")->queryAll();
  657. foreach ($list as $index => $value) {
  658. $saas_user = SaasUser::findOne(['mobile' => $value['binding']]);
  659. $list[$index]['nickname'] = $saas_user->name ?: $value['nickname'];
  660. $list[$index]['avatar_url'] = $saas_user->avatar ?: $value['avatar_url'];
  661. $list[$index]['time'] = date('Y-m-d', $value['created_at']);
  662. $child_count = User::find()->where(['old_parent_id' => $value['id'], 'is_delete' => 0])->count();
  663. $list[$index]['child_count'] = $child_count ? $child_count : 0;
  664. $level = Level::findOne(['store_id' => $this->store_id, 'level' => $value['level'], 'is_delete' => 0]);
  665. $list[$index]['level'] = $level->name ?? '普通用户';
  666. $share = Share::findOne(['user_id' => $value['id'], 'is_delete' => 0]);
  667. $list[$index]['share_level'] = $share ? '默认等级' : "普通用户";
  668. if ($share && $share->level > 0) {
  669. $shareLevel = ShareLevel::findOne(['level' => $share->level, 'store_id' => $this->store_id, 'is_delete' => 0]);
  670. $list[$index]['share_level'] = $shareLevel->name ?? '默认等级';
  671. }
  672. }
  673. $data['list'] = $list;
  674. return [
  675. 'code' => 0,
  676. 'msg' => '',
  677. 'data' => $data
  678. ];
  679. }
  680. private function getSql()
  681. {
  682. $s_table = Order::tableName();
  683. if($this->yinbao){
  684. $s_table = OrderPospal::tableName();
  685. }
  686. // $ms_table = MsOrder::tableName();
  687. // $pt_table = PtOrder::tableName();
  688. // $yy_table = YyOrder::tableName();
  689. $order_id = OrderRefund::find()->where(['store_id' => $this->store_id, 'type' => 1, 'status' => 1, 'is_agree' => 1, 'is_delete' => 0])->select('order_id')->column();
  690. $order_id = implode(',', $order_id);
  691. $sql_s = "(
  692. SELECT
  693. 's' AS `order_type`,
  694. `id`,
  695. `order_no`,
  696. `is_pay`,
  697. `pay_price`,
  698. `is_price`,
  699. `user_id`,
  700. `apply_delete`,
  701. `trade_status`,
  702. `share_send_type`,
  703. `created_at` AS `created_at`,
  704. `old_parent_id` AS `parent_id_1`,
  705. `old_parent_id_1` AS `parent_id_2`,
  706. `old_parent_id_2` AS `parent_id_3`,
  707. `first_price`,
  708. `second_price`,
  709. `third_price`,
  710. `rebate`,
  711. `mch_id`,
  712. `store_id`,
  713. `is_show`
  714. FROM
  715. {$s_table}
  716. WHERE
  717. (`trade_status` != 1)
  718. AND (`is_delete` = 0)
  719. AND (`old_parent_id` > 0 OR `rebate` != 0 OR `limit_id` != 0)
  720. AND (store_id = {$this->store_id})";
  721. if ($order_id) {
  722. $sql_s .= " AND id NOT IN ({$order_id})";
  723. }
  724. $sql_s .= ")";
  725. $sql = " FROM (
  726. SELECT * FROM ( {$sql_s} ) AS `l`
  727. WHERE
  728. `l`.`store_id` = {$this->store_id}
  729. AND `l`.`is_show` = 1
  730. ) AS `al` ";
  731. return $sql;
  732. }
  733. private function getSql2()
  734. {
  735. $s_table = Order::tableName();
  736. if($this->yinbao){
  737. $s_table = OrderPospal::tableName();
  738. }
  739. $sql_s = "(
  740. SELECT
  741. 's' AS `order_type`,
  742. `id`,
  743. `order_no`,
  744. `is_pay`,
  745. `pay_price`,
  746. `is_price`,
  747. `user_id`,
  748. `apply_delete`,
  749. `trade_status`,
  750. `share_send_type`,
  751. `created_at` AS `created_at`,
  752. `old_parent_id` AS `parent_id_1`,
  753. `old_parent_id_1` AS `parent_id_2`,
  754. `old_parent_id_2` AS `parent_id_3`,
  755. `first_price`,
  756. `second_price`,
  757. `third_price`,
  758. `rebate`,
  759. `mch_id`,
  760. `store_id`,
  761. `is_offline`,
  762. `md_id`,
  763. `is_show`
  764. FROM
  765. {$s_table}
  766. WHERE
  767. (`trade_status` != 1)
  768. AND (`is_delete` = 0)
  769. AND (`is_offline` = 1)
  770. AND (`old_parent_id` > 0 OR `rebate` != 0 OR `limit_id` != 0)
  771. AND (`mch_id` = 0 OR (`mch_id` > 0 ))
  772. AND (store_id = {$this->store_id})
  773. )";
  774. $sql = " FROM (
  775. SELECT * FROM ( {$sql_s} ) AS `l`
  776. WHERE
  777. `l`.`store_id` = {$this->store_id}
  778. AND `l`.`is_show` = 1
  779. ) AS `al` ";
  780. return $sql;
  781. }
  782. }