ShareOrderForm.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models;
  8. use app\models\Goods;
  9. use app\models\Order;
  10. use app\models\OrderDetail;
  11. use app\models\OrderRefund;
  12. use app\models\OrderShare;
  13. use app\models\SaasUser;
  14. use app\models\Share;
  15. use app\models\ShareLevel;
  16. use app\models\Shop;
  17. use app\models\User;
  18. use app\models\StatisticForm;
  19. use app\models\UserBindLog;
  20. use app\models\UserShareMoney;
  21. use app\utils\Export;
  22. use app\utils\ExportList;
  23. use yii\base\Model;
  24. use yii\console\widgets\Table;
  25. use yii\data\Pagination;
  26. use yii\db\Query;
  27. use yii\helpers\VarDumper;
  28. use app\models\OrderPospal;
  29. use app\models\OrderPospalDetail;
  30. class ShareOrderForm extends Model
  31. {
  32. public $yinbao = 0;
  33. public $store_id;
  34. public $user_id;
  35. public $keyword;
  36. public $status;
  37. public $is_offline;
  38. public $clerk_id;
  39. public $parent_id;
  40. public $shop_id;
  41. public $id;
  42. public $order_type;
  43. public $platform;//所属平台
  44. public $comments; //用户备注
  45. public $dateStart;
  46. public $dateEnd;
  47. public $type;//分销下级等级
  48. public $flag;
  49. public $fields = [
  50. [
  51. 'key' => 'order_no',
  52. 'value' => '订单编号',
  53. 'hidden' => false,
  54. 'selected' => 1,
  55. 'type' => [0, 1],
  56. ],
  57. [
  58. 'key' => 'nickname',
  59. 'value' => '下单用户',
  60. 'hidden' => false,
  61. 'selected' => 1,
  62. 'type' => [0, 1],
  63. ],
  64. [
  65. 'key' => 'total_price',
  66. 'value' => '总金额',
  67. 'hidden' => false,
  68. 'selected' => 1,
  69. 'type' => [0],
  70. ],
  71. [
  72. 'key' => 'pay_price',
  73. 'value' => '实际付款',
  74. 'hidden' => false,
  75. 'selected' => 1,
  76. 'type' => [0],
  77. ],
  78. [
  79. 'key' => 'created_at',
  80. 'value' => '下单时间',
  81. 'hidden' => false,
  82. 'selected' => 1,
  83. 'type' => [0],
  84. ],
  85. [
  86. 'key' => 'order_status',
  87. 'value' => '订单状态',
  88. 'hidden' => true,
  89. 'selected' => 1,
  90. 'type' => [0],
  91. ],
  92. [
  93. 'key' => 'rebate',
  94. 'value' => '自购返利',
  95. 'hidden' => false,
  96. 'selected' => 1,
  97. 'order_type' => [5],
  98. 'type' => [0, 1],
  99. ],
  100. [
  101. 'key' => 'share_commission_first',
  102. 'value' => '一级分销商',
  103. 'hidden' => false,
  104. 'selected' => 1,
  105. 'order_type' => [5],
  106. 'type' => [0, 1],
  107. ],
  108. [
  109. 'key' => 'share_commission_second',
  110. 'value' => '二级分销商',
  111. 'hidden' => false,
  112. 'selected' => 1,
  113. 'order_type' => [5],
  114. 'type' => [0, 1],
  115. ],
  116. [
  117. 'key' => 'share_commission_third',
  118. 'value' => '三级分销商',
  119. 'hidden' => false,
  120. 'selected' => 1,
  121. 'order_type' => [5],
  122. 'type' => [0, 1],
  123. ],
  124. ];
  125. public function rules()
  126. {
  127. return [
  128. [['keyword', 'order_type', 'flag'], 'trim'],
  129. [['fields', 'yinbao'], 'safe'],
  130. [['status', 'user_id', 'is_offline', 'clerk_id', 'shop_id', 'id', 'type'], 'integer'],
  131. [['status',], 'default', 'value' => -1],
  132. ['type', 'default', 'value' => 1],
  133. [['order_type', 'flag', 'dateStart', 'dateEnd'], 'string'],
  134. [['order_type'], 'in', 'range' => ['s', 'ms', 'pt', 'yy','ds','b']],
  135. ];
  136. }
  137. public function search()
  138. {
  139. if (!$this->validate()) {
  140. return [
  141. 'code' => 1,
  142. 'msg' => $this->getErrorSummary(false)[0],
  143. ];
  144. }
  145. if($this->yinbao){
  146. $s_table = \app\models\OrderPospal::tableName();
  147. }else{
  148. $s_table = Order::tableName();
  149. }
  150. $order_share_table = OrderShare::tableName();
  151. $user_table = User::tableName();
  152. $sql_s = "
  153. SELECT
  154. 's' AS `order_type`,
  155. `id`,
  156. `order_no`,
  157. `is_pay`,
  158. `user_id`,
  159. `apply_delete`,
  160. `created_at`,
  161. `old_parent_id` AS `parent_id_1`,
  162. `old_parent_id_1` AS `parent_id_2`,
  163. `old_parent_id_2` AS `parent_id_3`,
  164. `first_price`,
  165. `second_price`,
  166. `third_price`,
  167. `rebate`,
  168. `trade_status`,
  169. `is_recycle`,
  170. `limit_price`,
  171. `limit_id`,
  172. `future_sales_time`
  173. FROM
  174. {$s_table}
  175. WHERE
  176. (`store_id` = {$this->store_id})
  177. AND (`trade_status` != 1)
  178. AND (`is_delete` = 0)
  179. AND (`is_show` = 1)
  180. AND (first_price > 0 OR second_price > 0 OR third_price > 0 OR rebate > 0 OR limit_price > 0)
  181. ";
  182. $select = "SELECT `al`.*,u.nickname,u.platform,u.avatar_url ";
  183. // if($this->order_type){
  184. // switch($this->order_type){
  185. // case 's':
  186. // $sqlTable = '('.$sql_s . 'AND mch_id = 0)';
  187. // break;
  188. // case 'ds':
  189. // $sqlTable = '('.$sql_s . 'AND mch_id > 0)';
  190. // break;
  191. // case 'b':
  192. // $sqlTable = '('.$sql_s . 'AND type = 2)';
  193. // break;
  194. // default:
  195. // $sqlTable = $sql_s;
  196. // }
  197. // $sql = "FROM {$sqlTable} `al` LEFT JOIN {$user_table} `u` ON al.user_id = u.id";
  198. // }else{
  199. $sql = "FROM ( ({$sql_s})) `al` LEFT JOIN {$user_table}
  200. `u` ON al.user_id = u.id";
  201. // }
  202. $andWhere = '';
  203. if ($this->keyword) {
  204. if (!empty($andWhere)) {
  205. $andWhere .= " AND ";
  206. } else {
  207. $andWhere = " WHERE ";
  208. }
  209. $andWhere .= " (`al`.order_no LIKE '%{$this->keyword}%' OR u.nickname LIKE '%{$this->keyword}%') ";
  210. }
  211. if ($this->dateStart) {
  212. if (!empty($andWhere)) {
  213. $andWhere .= " AND ";
  214. } else {
  215. $andWhere = " WHERE ";
  216. }
  217. $andWhere .= ' (`al`.created_at >= ' . strtotime($this->dateStart) .' AND `al`.created_at < ' . strtotime($this->dateEnd) . ') ';
  218. }
  219. // 12.8新增订单状态判断
  220. if ($this->status) {
  221. if (!empty($andWhere)) {
  222. $andWhere .= " AND ";
  223. } else {
  224. $andWhere = " WHERE ";
  225. }
  226. switch ($this->status) {
  227. case '1':
  228. $andWhere .= "(`al`.is_pay = 0)";
  229. break;
  230. case '2':
  231. $andWhere .= "(`al`.trade_status = 0)";
  232. break;
  233. case '3':
  234. $andWhere .= "(`al`.trade_status = 2)";
  235. break;
  236. case '4':
  237. $andWhere .= "(`al`.trade_status = 3)";
  238. break;
  239. case '5':
  240. $andWhere .= "(`al`.apply_delete = 1)";
  241. break;
  242. case '6':
  243. $andWhere .= "(`al`.trade_status = 1)";
  244. break;
  245. case '7':
  246. $andWhere .= "(`al`.is_recycle = 1)";
  247. break;
  248. default:
  249. # code...
  250. break;
  251. }
  252. // $andWhere .= "(`al`.trade_status = {$this->status})";
  253. }
  254. if ($this->parent_id) {
  255. if (!empty($andWhere)) {
  256. $andWhere .= " AND ";
  257. } else {
  258. $andWhere = " WHERE ";
  259. }
  260. $andWhere .= " (`al`.parent_id_1 = {$this->parent_id}) ";
  261. }
  262. if (isset($this->platform)) {
  263. if (!empty($andWhere)) {
  264. $andWhere .= " AND ";
  265. } else {
  266. $andWhere = " WHERE ";
  267. }
  268. $andWhere .= "(`u`.platform = {$this->platform})";
  269. }
  270. $sql .= $andWhere;
  271. if ($this->flag == Export::EXPORT) {
  272. $export = new ExportList();
  273. $export->is_offline = $this->is_offline;
  274. $export->order_type = 0;
  275. $export->fields = $this->fields;
  276. $export->shareOrderExportData($sql);
  277. }
  278. $count = \Yii::$app->db->createCommand($select . $sql)->query()->count();
  279. $pageNo = get_params('pageNo', 1);
  280. $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
  281. $offset = ($pageNo - 1) * $pageSize;
  282. $list = \Yii::$app->db->createCommand($select . $sql . " ORDER BY created_at DESC LIMIT {$pageSize}
  283. OFFSET {$offset}")->queryAll();
  284. $list = $this->transform($list);
  285. return [
  286. 'code' => 0,
  287. 'msg' => 'success',
  288. 'data' => [
  289. 'data' => $list,
  290. 'pageNo' => intval($pageNo),
  291. 'totalCount' => intval($count),
  292. 'sql' => $select . $sql . " ORDER BY created_at DESC LIMIT {$pageSize} OFFSET {$offset}",
  293. ],
  294. ];
  295. }
  296. /**
  297. * 数据格式整理
  298. * @param $list
  299. * @return mixed
  300. */
  301. public function transform($list) {
  302. foreach ($list as $i => $item) {
  303. if ($item['order_type'] == 's') {
  304. $list[$i]['goods_list'] = $this->getOrderGoodsList($item['id']);
  305. if($this->yinbao){
  306. $order = \app\models\OrderPospal::find()->where(['id' => $item['id']])->asArray()->one();
  307. }else{
  308. $order = Order::find()->where(['id' => $item['id']])->asArray()->one();
  309. }
  310. $order_refund = OrderRefund::findOne(['store_id' => $this->store_id, 'order_id' => $item['id'],
  311. 'is_delete' => 0]);
  312. $list[$i]['refund'] = "";
  313. if ($order_refund) {
  314. $list[$i]['refund'] = $order_refund->status;
  315. }
  316. if($order['mch_id'] > 0){
  317. $list[$i]['order_type'] = 'ds';
  318. }
  319. }
  320. $bind_share = null;
  321. if ($item['limit_price'] > 0) {
  322. $bind_share_ = UserBindLog::find()->alias('ubl')->where([
  323. 'ubl.id' => $item['limit_id']
  324. ])->leftJoin(['u' => User::tableName()], 'u.id = ubl.parent_id')
  325. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  326. ->select('su.name, ubl.parent_id')->asArray()->one();
  327. $bind_share = [
  328. 'money' => $item['limit_price'],
  329. 'name' => $bind_share_['name'] ?: '',
  330. 'parent_id' => $bind_share_['parent_id'],
  331. 'limit_id' => $item['limit_id']
  332. ];
  333. }
  334. $list[$i]['bind_share'] = $bind_share;
  335. $list[$i] = array_merge($list[$i], $order);
  336. if ($order['is_offline'] == 1 || $item['order_type'] == 'yy') {
  337. if (!empty($order['clerk_id'])) {
  338. $user = User::findOne(['id' => $order['clerk_id'], 'store_id' => $this->store_id]);
  339. $list[$i]['clerk_name'] = $user->nickname;
  340. } else {
  341. $list[$i]['clerk_name'] = '';
  342. }
  343. }
  344. if ($order['shop_id'] && $order['shop_id'] != 0) {
  345. $shop = Shop::find()->where(['store_id' => $this->store_id, 'id' => $order['shop_id']])->asArray()->one();
  346. $list[$i]['shop'] = $shop;
  347. }
  348. $share = null;
  349. if ($item['parent_id_1'] != 0 && $item['parent_id_1'] != -1) {
  350. $share = User::find()->alias('u')->where(['u.id' => $item['parent_id_1'], 'u.store_id' =>
  351. $this->store_id, 'u.is_delete' => 0])
  352. ->leftJoin(SaasUser::tableName() . ' s', 's.mobile = u.binding')->select([
  353. 'u.nickname', 'u.platform', 'u.binding'
  354. ])->asArray()->one();
  355. }
  356. $list[$i]['share'] = $share;
  357. if ($item['parent_id_2'] != 0 && $item['parent_id_1'] != -1) {
  358. $share_1 = User::find()->alias('u')->where(['u.id' => $item['parent_id_2'], 'u.store_id' =>
  359. $this->store_id, 'u.is_delete' => 0])
  360. ->leftJoin(SaasUser::tableName() . ' s', 's.mobile = u.binding')->select([
  361. 'u.nickname', 'u.platform', 'u.binding'
  362. ])->asArray()->one();
  363. $list[$i]['share_1'] = $share_1;
  364. }
  365. if ($item['parent_id_3'] != 0 && $item['parent_id_1'] != -1) {
  366. $share_2 = User::find()->alias('u')->where(['u.id' => $item['parent_id_3'], 'u.store_id' =>
  367. $this->store_id, 'u.is_delete' => 0])
  368. ->leftJoin(SaasUser::tableName() . ' s', 's.mobile = u.binding')->select([
  369. 'u.nickname', 'u.platform', 'u.binding'
  370. ])->asArray()->one();
  371. $list[$i]['share_2'] = $share_2;
  372. }
  373. $list[$i]['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  374. }
  375. return $list;
  376. }
  377. /**
  378. * 普通订单
  379. * @param $order_id
  380. * @return array|\yii\db\ActiveRecord[]
  381. */
  382. public function getOrderGoodsList($order_id)
  383. {
  384. if($this->yinbao){
  385. $find= \app\models\OrderPospalDetail::find();
  386. }else{
  387. $find = OrderDetail::find();
  388. }
  389. $order_detail_list = $find->alias('od')
  390. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')
  391. ->where([
  392. 'od.is_delete' => 0,
  393. 'od.order_id' => $order_id,
  394. ])->select('od.*,g.name,g.unit')->asArray()->all();
  395. foreach ($order_detail_list as $i => $order_detail) {
  396. $order_detail_list[$i]['attr_list'] = json_decode($order_detail['attr']);
  397. }
  398. return $order_detail_list;
  399. }
  400. /**
  401. * 分销商详情
  402. * @param $user_id
  403. * @return array
  404. */
  405. public function detail() {
  406. if (!$this->validate()) {
  407. return [
  408. 'code' => 1,
  409. 'msg' => $this->getErrorSummary(false)[0],
  410. ];
  411. }
  412. if ($this->user_id) {
  413. $user = User::find()->alias('u')
  414. ->leftJoin(['s' => Share::tableName()], 'u.id = s.user_id')
  415. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  416. ->where([
  417. 'u.id' => $this->user_id,
  418. 'u.store_id' => $this->store_id
  419. ])->select('u.*,s.created_at create_time,s.id s_id,s.seller_comments,su.avatar sass_avatar,su.name sass_name')->asArray()->one();
  420. $user['create_time'] = date('Y-m-d H:i:s',$user['create_time']);
  421. // 上级id
  422. $user['name'] = !empty($user['sass_name']) ? $user['sass_name'] : $user['nickname'];
  423. $user['nickname'] = !empty($user['sass_name']) ? $user['sass_name'] : $user['nickname'];
  424. $user['avatar_url'] = !empty($user['sass_avatar']) ? $user['sass_avatar'] : $user['avatar_url'];
  425. $parentUser = User::find()->where(['id' => $user['old_parent_id']])->one();
  426. $user['parent_name'] = '总店';
  427. if ($parentUser) {
  428. $saasParentUser = SaasUser::findOne(['mobile' => $parentUser->binding]);
  429. $user['parent_name'] = $saasParentUser->name;
  430. }
  431. // 分销下级人数
  432. $level_count = count(User::findOld()->childs($this->user_id)->andWhere(['store_id' => $this->store_id])->asArray()->all()) ?: 0;
  433. // 一级人数
  434. $level_first = User::findOld()->childs($this->user_id, false, 1)
  435. ->andWhere(['store_id' => $this->store_id])
  436. ->select('id')->column();
  437. // 二级人数
  438. $level_second = User::find()
  439. ->where(['in','old_parent_id',$level_first,'store_id' => $this->store_id])
  440. ->select('id')->column();
  441. // 三级人数
  442. $level_third = User::find()
  443. ->where(['in','old_parent_id',$level_second, 'store_id' => $this->store_id])
  444. ->select('id')->column();
  445. $first = count($level_first);
  446. $second = count($level_second);
  447. $third = count($level_third);
  448. $user['level_info'] = [
  449. 'first' => $first,
  450. 'second' => $second,
  451. 'third' => $third,
  452. 'level_count' => $level_count
  453. ];
  454. return [
  455. 'code' => 0,
  456. 'msg' => 'success',
  457. 'data' => $user
  458. ];
  459. } else {
  460. return [
  461. 'code' => 0,
  462. 'msg' => '缺少参数',
  463. ];
  464. }
  465. }
  466. /**
  467. * 分销商订单
  468. * @param $user_id
  469. * @return array
  470. */
  471. public function shareOrder() {
  472. if (!$this->validate()) {
  473. return [
  474. 'code' => 1,
  475. 'msg' => $this->getErrorSummary(false)[0],
  476. ];
  477. }
  478. if ($this->user_id) {
  479. if($this->yinbao){
  480. $s_table = \app\models\OrderPospal::tableName();
  481. }else{
  482. $s_table = Order::tableName();
  483. }
  484. $user_table = User::tableName();
  485. $sql_s = "
  486. SELECT
  487. 's' AS `order_type`,
  488. `id`,
  489. `order_no`,
  490. `is_pay`,
  491. `user_id`,
  492. `apply_delete`,
  493. `created_at`,
  494. `parent_id` AS `parent_id_1`,
  495. `parent_id_1` AS `parent_id_2`,
  496. `parent_id_2` AS `parent_id_3`,
  497. `first_price`,
  498. `second_price`,
  499. `third_price`,
  500. `rebate`,
  501. `trade_status`,
  502. `is_recycle`
  503. FROM
  504. {$s_table}
  505. WHERE
  506. (`store_id` = {$this->store_id})
  507. AND (`trade_status` != 1)
  508. AND (`is_delete` = 0)
  509. AND (`is_show` = 1)
  510. AND (`old_parent_id` > 0 OR `rebate` != 0)
  511. AND (mch_id = 0 OR (mch_id > 0 )) AND ((first_price > 0 OR second_price > 0 OR third_price > 0) OR `rebate` != 0)
  512. ";
  513. $select = "SELECT `al`.*,u.nickname,u.platform,u.avatar_url ";
  514. $sql = "FROM ( ({$sql_s})) `al` LEFT JOIN {$user_table}
  515. `u` ON al.user_id = u.id";
  516. $andWhere = '';
  517. if (!empty($andWhere)) {
  518. $andWhere .= " AND ";
  519. } else {
  520. $andWhere = " WHERE ";
  521. }
  522. $andWhere .= "(`u`.id = {$this->user_id})";
  523. $sql .= $andWhere;
  524. $count = \Yii::$app->db->createCommand("select count(*) " . $sql)->queryScalar();
  525. $pageNo = get_params('pageNo', 1);
  526. $pageSize = get_params('pageSize', \Yii::$app->params['pageSize']);
  527. $offset = ($pageNo - 1) * $pageSize;
  528. $list = \Yii::$app->db->createCommand($select . $sql . " ORDER BY created_at DESC LIMIT {$pageSize}
  529. OFFSET {$offset}")->queryAll();
  530. $list = $this->transform($list);
  531. return [
  532. 'q' => $sql,
  533. 'code' => 0,
  534. 'msg' => 'success',
  535. 'data' => [
  536. 'data' => $list,
  537. 'pageNo' => intval($pageNo),
  538. 'totalCount' => intval($count)
  539. ],
  540. ];
  541. } else {
  542. return [
  543. 'code' => 0,
  544. 'msg' => '缺少参数',
  545. ];
  546. }
  547. }
  548. //获取分销商下级
  549. public function shareChildList() {
  550. $user_id = $this->user_id;
  551. $store_id = $this->store_id;
  552. $type = $this->type;//1一级 2二级 3三级
  553. // 一级人数
  554. $user_id_arr = User::find()->where(['old_parent_id' => $user_id, 'is_delete' => 0])
  555. ->andWhere(['store_id' => $store_id])
  556. ->select('id')->column();
  557. if ($type > 1) {
  558. // 二级人数
  559. $user_id_arr = User::find()
  560. ->where(['in', 'old_parent_id' , $user_id_arr])
  561. ->andWhere(['store_id' => $store_id, 'is_delete' => 0])
  562. ->select('id')->column();
  563. if (intval($type) === 3) {
  564. // 三级人数
  565. $user_id_arr = User::find()
  566. ->where(['in', 'old_parent_id', $user_id_arr])
  567. ->andWhere(['store_id' => $store_id, 'is_delete' => 0])
  568. ->select('id')->column();
  569. }
  570. }
  571. $query = User::find()->alias('u')
  572. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  573. ->where(['u.is_delete' => 0, 'su.is_delete' => 0, 'u.id' => $user_id_arr])
  574. ->select('su.name, su.avatar, su.avatar, su.mobile, u.total_price, u.price');
  575. $list = pagination_make($query);
  576. foreach ($list['list'] as &$item) {
  577. $share = Share::findOne(['user_id' => $item['id'], 'is_delete' => 0]);
  578. if (isset($share) && $share->level > 0) {
  579. $item['level'] = ShareLevel::findOne(['is_delete' => 0, 'status' => 1, 'level' => $item['level']])->name ?? '无等级';
  580. } else {
  581. $item['level'] = '无等级';
  582. }
  583. }
  584. return [
  585. 'code' => 0,
  586. 'data' => [
  587. 'data' => $list['list'],
  588. 'pageNo' => $list['pageNo'] ?? '',
  589. 'totalCount' => $list['totalCount'] ?? ''
  590. ]
  591. ];
  592. }
  593. }