StatisticForm.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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\utils\Export;
  9. use yii\base\Model;
  10. use app\models\User;
  11. use app\models\Share;
  12. use app\models\Order;
  13. use app\models\Shop;
  14. use app\models\OrderDetail;
  15. use app\models\UserCoupon;
  16. use app\models\Coupon;
  17. use app\models\UserCard;
  18. use app\models\Card;
  19. use app\models\Goods;
  20. use app\models\UserViews;
  21. use app\models\Cart;
  22. use app\models\Mch;
  23. use app\models\MchAccountLog;
  24. use app\models\AccountLog;
  25. use yii\db\Query;
  26. class StatisticForm extends Model
  27. {
  28. public $keyword_1;
  29. public $keyword;
  30. public $store_id;
  31. public $page;
  32. public $limit;
  33. public $status;
  34. public $flag;
  35. public $date_start;
  36. public $date_end;
  37. public $platform;
  38. public $mch_id;
  39. public $sign;
  40. public $type;
  41. public $name;
  42. public $gids;
  43. /**
  44. * Undocumented function
  45. *
  46. * @Author LGL 24963@qq.com
  47. * @DateTime 2021-01-21
  48. * @desc: 分销排行
  49. */
  50. public function getShareList()
  51. {
  52. if ($this->validate()) {
  53. $query = Share::find()->alias('s')
  54. ->where(['s.is_delete' => 0, 's.store_id' => get_store_id()])
  55. ->leftJoin('{{%user}} u', 'u.id=s.user_id')
  56. ->andWhere(['u.is_delete' => 0])
  57. ->andWhere(['in', 's.status', [0, 1]]);
  58. if ($this->keyword) {
  59. $query->andWhere([
  60. 'or',
  61. ['like', 's.name', $this->keyword],
  62. ['like', 'u.nickname', $this->keyword],
  63. ]);
  64. }
  65. if ($this->status == 0 && $this->status != '') {
  66. $query->andWhere(['s.status' => 0]);
  67. }
  68. if ($this->status == 1) {
  69. $query->andWhere(['s.status' => 1]);
  70. }
  71. if (isset($this->platform) && in_array($this->platform, [1, 0])) {
  72. $query->andWhere(['u.platform' => $this->platform]);
  73. }
  74. $query->orderBy('s.status ASC,s.created_at DESC')
  75. ->select([
  76. 's.*', 'u.nickname', 'u.avatar_url', 'u.platform', 'u.time', 'u.price', 'u.total_price', 'u.id user_id', 'u.parent_id', 'parent_nickname' => User::find()->alias('parent')->where('parent.id = u.parent_id')->select('nickname')
  77. ])->orderBy(['status' => SORT_ASC, 'total_price' => SORT_DESC])->asArray()->all();
  78. $data = pagination_make($query);
  79. foreach ($data['list'] as $index => &$value) {
  80. $first = $this->getTeam1($value['user_id'], 1);
  81. $value['first'] = count($first['data']);
  82. $second = $this->getTeam1($value['user_id'], 2);
  83. $value['second'] = count($second['data']);
  84. $third = $this->getTeam1($value['user_id'], 3);
  85. $value['orderCount'] = Order::find()->where([
  86. 'store_id' => get_store_id(), 'is_delete' => 0
  87. ])->andWhere(['<>', 'trade_status', Order::ORDER_FLOW_CANCEL])->andWhere(['user_id' => $value['user_id']])->count();
  88. $value['third'] = count($third['data']);
  89. $value['all']=$value['first'] + $value['second'] + $value['third'];
  90. $value['yiti']=$value['total_price'] - $value['price'];
  91. $value['o_count']=$value['order_count']+ $value['ms_order_count']+$value['pt_order_count']+$value['yy_order_count'];
  92. }
  93. $data['data'] = $data['list'];
  94. unset($data['list']);
  95. return [
  96. 'code' => 0,
  97. 'msg' => 'success',
  98. 'data' => $data
  99. ];
  100. } else {
  101. // 验证失败:$errors 是一个包含错误信息的数组
  102. return [
  103. 'code' => 1,
  104. "msg" => $this->getErrorSummary(false)[0]
  105. ];
  106. }
  107. }
  108. /**
  109. *
  110. * @Author LGL 24963@qq.com
  111. * @DateTime 2021-01-21
  112. * @desc: 获取分销商下级
  113. * @param integer $user_id
  114. * @param integer $level
  115. * @return array
  116. */
  117. public function getTeam1($user_id, $level)
  118. {
  119. $firstQuery = User::find()->alias('f')->select('f.*')
  120. ->where(['f.is_delete' => 0, 'f.parent_id' => $user_id, 'f.store_id' => get_store_id()]);
  121. $query = $firstQuery;
  122. if ($level > 1) {
  123. $secondQuery = User::find()->alias('s')->where(['s.is_delete' => 0, 's.store_id' => get_store_id()])
  124. ->innerJoin(['f' => $firstQuery], 'f.id=s.parent_id');
  125. $query = $secondQuery;
  126. if ($level > 2) {
  127. $thirdQuery = User::find()->alias('t')->where(['t.is_delete' => 0, 't.store_id' => get_store_id()])
  128. ->innerJoin(['s' => $secondQuery], 's.id=t.parent_id');
  129. $query = $thirdQuery;
  130. }
  131. }
  132. $list = $query->asArray()->all();
  133. return [
  134. 'code' => 0,
  135. 'msg' => 'success',
  136. 'data' => $list
  137. ];
  138. }
  139. /**
  140. * Undocumented function
  141. *
  142. * @Author LGL 24963@qq.com
  143. * @DateTime 2021-01-21
  144. * @desc: 销售统计
  145. * @return array
  146. */
  147. public function OrderSearch()
  148. {
  149. if (!$this->validate()) {
  150. return [
  151. 'code' => 1,
  152. "msg" => $this->getErrorSummary(false)[0]
  153. ];
  154. }
  155. $query = $this->where();
  156. $nowtime = date('Y-m-d 00:00:00');
  157. $date_query = $this->date_where();
  158. $all_query = clone $date_query;
  159. $now_query = clone $date_query;
  160. //时间查询
  161. if ($this->date_start) {
  162. $this->date_start=substr($this->date_start, 0, 10);
  163. $all_query->andWhere(['>=', 'o.created_at', $this->date_start . ' 00:00:00']);
  164. }
  165. if ($this->date_end) {
  166. $this->date_end=substr($this->date_end, 0, 10);
  167. $all_query->andWhere(['<=', 'o.created_at', $this->date_end . ' 23:59:59']);
  168. }
  169. if ($this->sign == 'advance') {
  170. $all = $all_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`,
  171. COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`+(`ao`.`deposit`*`ao`.`goods_num`)),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`")
  172. ->asArray()
  173. ->one();
  174. $now = $now_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`,
  175. COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`+(`ao`.`deposit`*`ao`.`goods_num`)),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`")
  176. ->andWhere(['>=', 'o.created_at', strtotime($nowtime)])
  177. ->asArray()
  178. ->one();
  179. $query->select("DATE_FORMAT(`o`.`created_at`, '%Y-%m-%d') AS `time`,COUNT(DISTINCT `o`.`user_id`) AS `user_num`,
  180. COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`+(`ao`.`deposit`*`ao`.`goods_num`)),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`")
  181. ->groupBy('time')
  182. ->orderBy('time DESC');
  183. } else {
  184. $all = $all_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`,
  185. COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`")
  186. ->asArray()
  187. ->one();
  188. $now = $now_query->select("COUNT(DISTINCT `o`.`user_id`) AS `user_num`,
  189. COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`")
  190. ->andWhere(['>=', 'o.created_at', strtotime($nowtime)])
  191. ->asArray()
  192. ->one();
  193. $query->select("FROM_UNIXTIME(`o`.`created_at`, '%Y-%m-%d') AS `time`,COUNT(DISTINCT `o`.`user_id`) AS `user_num`,
  194. COUNT(`o`.`id`) AS `order_num`,COALESCE(SUM(`o`.`pay_price`),0) AS `pay_price`,COALESCE(sum(`d`.`num`),0) as `goods_num`")
  195. ->groupBy('time')
  196. ->orderBy('time DESC');
  197. }
  198. //门店列表
  199. $store_query = $this->store_where();
  200. $store_list = $store_query->select('id,name')
  201. ->asArray()
  202. ->all();
  203. if ($this->flag == Export::EXPORT) {
  204. $new_query = clone $query;
  205. $this->export($new_query);
  206. return false;
  207. }
  208. $this->limit=20;
  209. $count = $query->count();
  210. $commandQuery = clone $query;
  211. $data = pagination_make($query);
  212. $data['data'] = $data['list'];
  213. unset($data['list']);
  214. return [
  215. 'code' => 0,
  216. 'msg' => 'success',
  217. 'data' => $data,
  218. 'store_list' => $store_list,
  219. 'all' => $all,
  220. 'now' => $now,
  221. 'row_count' => $count,
  222. 'd' => $this->attributes
  223. ];
  224. }
  225. protected function store_where()
  226. {
  227. $query = Shop::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->orderBy('name');
  228. return $query;
  229. }
  230. protected function where()
  231. {
  232. $orderQuery = OrderDetail::find()->alias('od')->where(['is_delete' => 0])
  233. ->select(['od.order_id', 'COALESCE(SUM(`od`.`num`),0) AS `num`'])->groupBy('od.order_id');
  234. $query = Order::find()->alias('o')->where(['o.is_delete' => 0, 'o.store_id' => get_store_id()])
  235. ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
  236. ->rightJoin(['d' => $orderQuery], 'd.order_id = o.id')
  237. ->leftJoin(['i' => User::tableName()], 'i.id = o.user_id');
  238. //时间查询
  239. if ($this->date_start) {
  240. $this->date_start=substr($this->date_start, 0, 10);
  241. $query->andWhere(['>=', 'o.created_at', strtotime($this->date_start . ' 00:00:00')]);
  242. }
  243. if ($this->date_end) {
  244. $this->date_end=substr($this->date_end, 0, 10);
  245. $query->andWhere(['<=', 'o.created_at', strtotime($this->date_end . ' 23:59:59')]);
  246. }
  247. //多商户
  248. if ($this->mch_id) {
  249. $query->andWhere(['o.mch_id' => $this->mch_id]);
  250. }
  251. //门店
  252. if (get_store_id()) {
  253. $query->andWhere(['o.store_id' => get_store_id()]);
  254. }
  255. //平台标识查询
  256. if ($this->platform) {
  257. $query->andWhere(['i.platform' => $this->platform]);
  258. }
  259. return $query;
  260. }
  261. protected function date_where()
  262. {
  263. $orderQuery = OrderDetail::find()->alias('od')->where(['is_delete' => 0])
  264. ->select(['od.order_id', 'COALESCE(SUM(`od`.`num`),0) AS `num`'])->groupBy('od.order_id');
  265. $query = Order::find()->alias('o')->where(['o.is_delete' => 0, 'o.store_id' => get_store_id()])
  266. ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]])
  267. ->rightJoin(['d' => $orderQuery], 'd.order_id = o.id')
  268. ->leftJoin(['i' => User::tableName()], 'i.id = o.user_id');
  269. //平台标识查询
  270. if ($this->platform) {
  271. $query->andWhere(['i.platform' => $this->platform]);
  272. }
  273. return $query;
  274. }
  275. /**
  276. * Undocumented function
  277. *
  278. * @Author LGL 24963@qq.com
  279. * @DateTime 2021-01-22
  280. * @desc: 发放查询
  281. */
  282. public function GrantSearch()
  283. {
  284. if (!$this->validate()) {
  285. return [
  286. 'code' => 1,
  287. "msg" => $this->getErrorSummary(false)[0]
  288. ];
  289. }
  290. $query = $this->coupon_where();
  291. if ($this->type == 'card') {
  292. $query = $this->card_where();
  293. } else {
  294. $query = $this->coupon_where();
  295. }
  296. if ($this->flag == Export::EXPORT) {
  297. $new_query = clone $query;
  298. $this->export($new_query);
  299. return false;
  300. }
  301. $all_data = $query->asArray()->one();
  302. $all_data = [
  303. 'all_num' => !empty($all_data['all_num']) ? $all_data['all_num'] : 0,
  304. 'unuse_num' => !empty($all_data['unuse_num']) ? $all_data['unuse_num'] : 0,
  305. 'use_num' => !empty($all_data['use_num']) ? $all_data['use_num'] : 0,
  306. 'end_num' => !empty($all_data['end_num']) ? $all_data['end_num'] : 0,
  307. ];
  308. $query->groupBy('`date`,name') ->orderBy('`date` desc');
  309. $data = pagination_make($query);
  310. $data['data'] = $data['list'];
  311. unset($data['list']);
  312. return [
  313. 'all_data' => $all_data,
  314. 'data' => $data,
  315. 'code' => 0,
  316. 'd' => $query->createCommand()->getRawSql()
  317. ];
  318. }
  319. /**
  320. * +
  321. *
  322. * @Author LGL 24963@qq.com
  323. * @DateTime 2021-01-22
  324. * @desc: 优惠券modal
  325. * @return Query
  326. */
  327. protected function coupon_where()
  328. {
  329. $query = UserCoupon::find()->alias('uc')
  330. ->select("FROM_UNIXTIME(uc.`created_at`,'%Y-%m-%d') AS `date`,c.`name`,
  331. COUNT(uc.`created_at`) AS all_num,
  332. SUM(CASE uc.`is_use` WHEN 0 THEN 1 ELSE 0 END) AS `unuse_num`,
  333. SUM(CASE uc.`is_use` WHEN 1 THEN 1 ELSE 0 END) AS `use_num`,
  334. SUM(CASE WHEN uc.`end_time`< NOW() AND uc.`is_use` = 0 THEN 1 ELSE 0 END) AS `end_num`")
  335. ->leftJoin(['c' => Coupon::tableName()], 'c.id = uc.coupon_id')
  336. ->leftJoin(['i' => User::tableName()], 'i.id = uc.user_id')
  337. ->where(['uc.store_id' => $this->store_id, 'uc.is_delete' => 0]);
  338. if ($this->name) {
  339. $query->andWhere(['like', 'c.name', $this->name]);
  340. }
  341. //时间查询
  342. if ($this->date_start) {
  343. $this->date_start=substr($this->date_start, 0, 10);
  344. $query->andWhere(['>=', 'uc.created_at', strtotime($this->date_start . ' 00:00:00')]);
  345. }
  346. if ($this->date_end) {
  347. $this->date_end=substr($this->date_end, 0, 10);
  348. $query->andWhere(['<=', 'uc.created_at', strtotime($this->date_end . ' 23:59:59')]);
  349. }
  350. //平台标识查询
  351. if ($this->platform && $this->platform >= 0) {
  352. $query->andWhere(['i.platform' => $this->platform]);
  353. }
  354. $query->orderBy(!empty($this->order) ? $this->order : '`date` desc,uc.`created_at` desc');
  355. return $query;
  356. }
  357. /**
  358. * Undocumented function
  359. *
  360. * @Author LGL 24963@qq.com
  361. * @DateTime 2021-01-22
  362. * @desc: 卡券model
  363. * @return Query
  364. */
  365. protected function card_where()
  366. {
  367. $query = UserCard::find()->alias('uc')
  368. ->select("FROM_UNIXTIME(uc.`created_at`,'%Y-%m-%d') AS `date`,c.`name`,
  369. COUNT(uc.`created_at`) AS all_num,
  370. SUM(CASE uc.`is_use` WHEN 0 THEN 1 ELSE 0 END) AS `unuse_num`,
  371. SUM(CASE uc.`is_use` WHEN 1 THEN 1 ELSE 0 END) AS `use_num`,
  372. SUM(CASE WHEN uc.`created_at`< NOW() AND uc.`is_use` = 0 THEN 1 ELSE 0 END) AS `end_num`")
  373. ->leftJoin(['i' => User::tableName()], 'i.id = uc.user_id')
  374. ->leftJoin(['c' => Card::tableName()], 'c.id = uc.card_id')
  375. ->where(['uc.store_id' => $this->store_id, 'uc.is_delete' => 0]);
  376. if ($this->name) {
  377. $query->andWhere(['like', 'c.name', $this->name]);
  378. }
  379. //时间查询
  380. if ($this->date_start) {
  381. $this->date_start=substr($this->date_start, 0, 10);
  382. $query->andWhere(['>=', 'uc.created_at', strtotime($this->date_start . ' 00:00:00')]);
  383. }
  384. if ($this->date_end) {
  385. $this->date_end=substr($this->date_end, 0, 10);
  386. $query->andWhere(['<=', 'uc.created_at', strtotime($this->date_end . ' 23:59:59')]);
  387. }
  388. //平台标识查询
  389. if ($this->platform && $this->platform >= 0) {
  390. $query->andWhere(['i.platform' => $this->platform]);
  391. }
  392. $query->orderBy(!empty($this->order) ? $this->order : '`date` desc, uc.`created_at` desc');
  393. return $query;
  394. }
  395. /**
  396. * Undocumented function
  397. *
  398. * @Author LGL 24963@qq.com
  399. * @DateTime 2021-01-22
  400. * @desc: 商品销售记录
  401. */
  402. public function GoodsSearch()
  403. {
  404. if (!$this->validate()) {
  405. return [
  406. 'code' => 1,
  407. "msg" => $this->getErrorSummary(false)[0]
  408. ];
  409. }
  410. $query = Goods::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]);
  411. // $query->leftJoin(['uv' => UserViews::tableName()], 'g.id = uv.goods_id');
  412. if($this->gids){
  413. $query->andWhere(['id' => $this->gids]);
  414. }
  415. if ($this->keyword) {
  416. $query->andWhere(['LIKE', 'name', $this->keyword]);
  417. }
  418. $list = pagination_make($query);
  419. $listArray = $list['list'];
  420. foreach ($listArray as $k => $v) {
  421. //访客数
  422. $fangke = UserViews::find()->where(['goods_id' => $v['id']]);
  423. if ($this->date_start) {
  424. $fangke->andWhere(['>=', 'updated_at', strtotime($this->date_start)]);
  425. }
  426. if ($this->date_end) {
  427. $fangke->andWhere(['<=', 'updated_at', strtotime($this->date_end)]);
  428. }
  429. $fangke = $fangke->SUM('visitors');
  430. if (!$fangke) {
  431. $fangke = 0;
  432. }
  433. //浏览量
  434. $views = UserViews::find()->where(['goods_id' => $v['id']]);
  435. if ($this->date_start) {
  436. $views->andWhere(['>=', 'updated_at', strtotime($this->date_start)]);
  437. }
  438. if ($this->date_end) {
  439. $views->andWhere(['<=', 'updated_at', strtotime($this->date_end)]);
  440. }
  441. $views = $views->SUM('views');
  442. if (!$views) {
  443. $views = 0;
  444. }
  445. //加购数
  446. $cart = Cart::find()->where(['goods_id' => $v['id']]);
  447. if ($this->date_start) {
  448. $cart->andWhere(['>=', 'created_at', strtotime($this->date_start)]);
  449. }
  450. if ($this->date_end) {
  451. $cart->andWhere(['<=', 'created_at',strtotime($this->date_end)]);
  452. }
  453. $cart = $cart->count();
  454. if (!$cart) {
  455. $cart = 0;
  456. }
  457. // 付款人数
  458. $paynum = Order::find()->alias('o')->where(['o.is_pay' => 1])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  459. $paynum->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id');
  460. $paynum->andWhere(['od.goods_id'=>$v['id']]);
  461. if ($this->date_start) {
  462. $paynum->andWhere(['>=', 'o.created_at', strtotime($this->date_start)]);
  463. }
  464. if ($this->date_end) {
  465. $paynum->andWhere(['<=', 'o.created_at',strtotime($this->date_end)]);
  466. }
  467. $paynum = $paynum->count();
  468. if (!$paynum) {
  469. $paynum = 0;
  470. }
  471. // 付款金额
  472. $jine = Order::find()->alias('o')->where(['o.is_pay' => 1])->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CANCEL]);
  473. $jine->leftJoin(['od' => OrderDetail::tableName()], 'o.id = od.order_id');
  474. $jine->andWhere(['od.goods_id'=>$v['id']]);
  475. if ($this->date_start) {
  476. $jine->andWhere(['>=', 'o.created_at', strtotime($this->date_start)]);
  477. }
  478. if ($this->date_end) {
  479. $jine->andWhere(['<=', 'o.created_at',strtotime($this->date_end)]);
  480. }
  481. $jine = $jine->SUM('od.total_price');
  482. if (!$jine) {
  483. $jine = '0.00';
  484. }
  485. $listArray[$k]['fangke'] = $fangke;
  486. $listArray[$k]['views'] = $views;
  487. $listArray[$k]['cart'] = $cart;
  488. $listArray[$k]['paynum'] = $paynum;
  489. $listArray[$k]['jine'] = $jine;
  490. }
  491. $this->gids && array_multisort(array_column($listArray, 'paynum'), SORT_DESC, $listArray);
  492. $list['data'] = $listArray;
  493. unset($list['list']);
  494. return [
  495. 'code' => 0,
  496. 'msg' => 'success',
  497. 'data' => $list
  498. ];
  499. }
  500. /**
  501. * Undocumented function
  502. *
  503. * @Author LGL 24963@qq.com
  504. * @DateTime 2021-01-22
  505. * @desc: 商户统计报表
  506. */
  507. public function MchCountSearch()
  508. {
  509. if (!$this->validate()) {
  510. return [
  511. 'code' => 1,
  512. "msg" => $this->getErrorSummary(false)[0]
  513. ];
  514. }
  515. $query = Mch::find()->alias('m');
  516. if ($this->keyword) {
  517. $query->andWhere(['LIKE', 'm.name', $this->keyword]);
  518. }
  519. $query->orderBy('m.created_at DESC');
  520. $list = pagination_make($query);
  521. $listArray = $list['list'];
  522. foreach ($listArray as $k =>$v) {
  523. $order_num = Order::find()->where(['mch_id' => $v['id']])->count();
  524. if (!$order_num) {
  525. $order_num = 0;
  526. }
  527. $listArray[$k]['order_num'] = $order_num;
  528. $yongjin = MchAccountLog::find()->where(['mch_id' => $v['id']])->SUM('price');
  529. if (!$yongjin) {
  530. $yongjin = 0;
  531. }
  532. $listArray[$k]['yongjin'] = $yongjin;
  533. }
  534. $list['data'] = $listArray;
  535. unset($list['list']);
  536. return [
  537. 'code' => 0,
  538. 'msg' => 'success',
  539. 'data' => $list
  540. ];
  541. }
  542. /**
  543. * Undocumented function
  544. *
  545. * @Author LGL 24963@qq.com
  546. * @DateTime 2021-01-22
  547. * @desc: 商户订单报表
  548. */
  549. public function MchOrderSearch()
  550. {
  551. if (!$this->validate()) {
  552. return [
  553. 'code' => 1,
  554. "msg" => $this->getErrorSummary(false)[0]
  555. ];
  556. }
  557. $query = Order::find()->alias('o')->leftJoin(Mch::tableName() . ' m', 'o.mch_id=m.id');
  558. $query->andWhere(['>', 'o.mch_id', 0]);
  559. if (isset($this->platform) && $this->platform >= 0) {
  560. $query->andWhere(['o.platform' => $this->platform]);
  561. }
  562. if ($this->date_start) {
  563. $query->andWhere(['>=', 'o.created_at', strtotime($this->date_start)]);
  564. }
  565. if ($this->date_end) {
  566. $query->andWhere(['<=', 'o.created_at',strtotime($this->date_end)]);
  567. }
  568. if ($this->status == 1) {
  569. $query->andWhere(['=', 'o.is_pay',0]);
  570. }
  571. if ($this->status == 2) {
  572. $query->andWhere(['=', 'o.trade_status', Order::ORDER_FLOW_NO_SEND]);
  573. }
  574. if ($this->status == 3) {
  575. $query->andWhere(['<>', 'o.trade_status', Order::ORDER_FLOW_CONFIRM]);
  576. }
  577. if ($this->status == 4) {
  578. $query->andWhere(['=', 'o.trade_status', Order::ORDER_FLOW_CONFIRM]);
  579. }
  580. $query->orderBy('o.created_at DESC')->select('o.id,o.order_no,o.pay_price,o.is_pay,o.trade_status,m.name');
  581. $list = pagination_make($query);
  582. $listArray = $list['list'];
  583. foreach ($listArray as $k =>$v) {
  584. $yongjin = MchAccountLog::find()->where(['LIKE', 'desc', $v['order_no']])->asArray()->all();
  585. if ($yongjin) {
  586. $listArray[$k]['yongjin'] = $yongjin[0]['price'];
  587. } else {
  588. $listArray[$k]['yongjin'] = '暂未结算';
  589. }
  590. }
  591. $list['data'] = $listArray;
  592. unset($list['list']);
  593. return [
  594. 'code' => 0,
  595. 'msg' => 'success',
  596. 'data' => $list
  597. ];
  598. }
  599. /**
  600. * Undocumented function
  601. *
  602. * @Author LGL 24963@qq.com
  603. * @DateTime 2021-01-23
  604. * @desc: 积分统计
  605. */
  606. public function IntegralSearch()
  607. {
  608. if (!$this->validate()) {
  609. return [
  610. 'code' => 1,
  611. "msg" => $this->getErrorSummary(false)[0]
  612. ];
  613. }
  614. $query = $this->IntWhere();
  615. $query->select(["FROM_UNIXTIME(`il`.`created_at`, '%Y-%m-%d') AS `date`,
  616. COALESCE(SUM(CASE WHEN `il`.`log_type`=1 THEN `il`.`amount` ELSE 0 END), 0) AS in_integral,
  617. COALESCE(SUM(CASE WHEN `il`.`log_type`=2 THEN `il`.`amount` ELSE 0 END), 0) AS out_integral"]);
  618. $all_query = clone $query;
  619. $query->groupBy('`date`') ->orderBy('`date` desc');
  620. $all_data = $all_query->asArray()->one();
  621. $count = $query->count();
  622. $list = pagination_make($query);
  623. unset($all_data[0]['date']);
  624. $all_data['all_num'] = $count;
  625. $list['data'] = $list['list'];
  626. unset($list['list']);
  627. return [
  628. 'code' => 0,
  629. 'msg' => 'success',
  630. 'data' => $list,
  631. 'all_data' => $all_data
  632. ];
  633. }
  634. /**
  635. * Undocumented function
  636. *
  637. * @Author LGL 24963@qq.com
  638. * @DateTime 2021-01-23
  639. * @desc: 积分model
  640. * @return Query
  641. */
  642. protected function IntWhere()
  643. {
  644. $query = AccountLog::find()->alias('il')
  645. ->where(['il.store_id' => $this->store_id])
  646. ->leftJoin(['i' => User::tableName()], 'i.id = il.user_id')
  647. ->where(['il.type' => 1]);
  648. //时间查询
  649. if ($this->date_start) {
  650. $this->date_start=substr($this->date_start, 0, 10);
  651. $query->andWhere(['>=', 'il.created_at', strtotime($this->date_start . ' 00:00:00')]);
  652. }
  653. if ($this->date_end) {
  654. $this->date_end=substr($this->date_end, 0, 10);
  655. $query->andWhere(['<=', 'il.created_at', strtotime($this->date_end . ' 23:59:59')]);
  656. }
  657. //平台标识查询
  658. if ($this->platform && in_array($this->platform, [0,1])) {
  659. $query->andWhere(['i.platform' => $this->platform]);
  660. }
  661. return $query;
  662. }
  663. public function orderSaleGoodsList() {
  664. $order_type_arr = [
  665. 0 => '全部',
  666. 1 => '到店自提',
  667. 2 => '快递',
  668. 3 => '同城配送'
  669. ];
  670. $trade_status = Order::TRADE_STATUS_TEXT;
  671. try {
  672. $status = $this->status;
  673. $type = $this->type;
  674. $date_start = $this->date_start;
  675. $date_end = $this->date_end;
  676. $store_id = $this->store_id;
  677. if (!isset($status) && !isset($type) && !isset($date_start) && !isset($date_end)) {
  678. throw new \Exception('参数为空');
  679. }
  680. $query = OrderDetail::find()->alias('od')->leftJoin(['o' => Order::tableName()], 'od.order_id = o.id')
  681. ->where(['o.store_id' => $store_id, 'o.is_delete' => 0]);
  682. if (isset($status)) {
  683. $query->andWhere(['o.trade_status' => $status]);
  684. }
  685. if (isset($type)) {
  686. switch ($type) {
  687. case 1:
  688. $query->andWhere(['o.is_offline' => 1]);
  689. break;
  690. case 2:
  691. $query->andWhere(['AND', ['o.is_offline' => 0], ['o.is_delivery' => 0]]);
  692. break;
  693. case 3:
  694. $query->andWhere(['o.is_delivery' => 1]);
  695. break;
  696. }
  697. }
  698. if (isset($date_start)) {
  699. $query->andWhere(['>=', 'o.created_at', strtotime($date_start)]);
  700. }
  701. if (isset($date_end)) {
  702. $query->andWhere(['<=', 'o.created_at', strtotime($date_end)]);
  703. }
  704. $query->select('od.id, od.goods_id, od.goods_name, od.attr, SUM(od.num) num, COUNT(o.id) order_count, GROUP_CONCAT(o.id) order_no')
  705. ->orderBy('od.goods_id DESC')->groupBy('od.goods_id, od.attr');
  706. $list = pagination_make($query);
  707. foreach ($list['list'] as &$item) {
  708. $goods = Goods::findOne($item['goods_id']);
  709. $item['goods_name'] = $goods->name . "(商品ID:{$goods->id})";
  710. $item['attr'] = json_decode($item['attr'], true);
  711. $attr_name = '';
  712. array_map(function ($item) use (&$attr_name) {
  713. $attr_name = $item['attr_group_name'] . ':' . $item['attr_name'] . ';';
  714. }, $item['attr']);
  715. $item['attr_name'] = $attr_name;
  716. }
  717. return [
  718. 'code' => 0,
  719. 'msg' => 'success',
  720. 'data' => [
  721. 'data' => $list['list'],
  722. 'pageNo' => $list['pageNo'],
  723. 'totalCount' => $list['totalCount'],
  724. 'trade_status' => $trade_status,
  725. 'order_type' => $order_type_arr
  726. ]
  727. ];
  728. } catch (\Exception $e) {
  729. return [
  730. 'code' => 0,
  731. 'msg' => $e->getMessage(),
  732. 'data' => [
  733. 'data' => [],
  734. 'pageNo' => 1,
  735. 'totalCount' => 0,
  736. 'trade_status' => $trade_status,
  737. 'order_type' => $order_type_arr
  738. ]
  739. ];
  740. }
  741. }
  742. }