StoreOrderDao.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\order;
  12. use app\dao\BaseDao;
  13. use app\model\order\StoreOrder;
  14. /**
  15. * 订单
  16. * Class StoreOrderDao
  17. * @package app\dao\order
  18. */
  19. class StoreOrderDao extends BaseDao
  20. {
  21. /**
  22. * 限制精确查询字段
  23. * @var string[]
  24. */
  25. protected $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'title'];
  26. /**
  27. * @return string
  28. */
  29. protected function setModel(): string
  30. {
  31. return StoreOrder::class;
  32. }
  33. /**
  34. * 订单搜索
  35. * @param array $where
  36. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  37. */
  38. public function search(array $where = [])
  39. {
  40. $isDel = isset($where['is_del']) && $where['is_del'] !== '' && $where['is_del'] != -1;
  41. $realName = $where['real_name'] ?? '';
  42. $fieldKey = $where['field_key'] ?? '';
  43. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  44. return parent::search($where)->when($isDel, function ($query) use ($where) {
  45. $query->where('is_del', $where['is_del']);
  46. })->when(isset($where['is_system_del']), function ($query) {
  47. $query->where('is_system_del', 0);
  48. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  49. switch ((int)$where['status']) {
  50. case 0://未支付
  51. $query->where('paid', 0)->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
  52. break;
  53. case 1://已支付 未发货
  54. $query->where('paid', 1)->whereIn('status', [0, 4])->whereIn('refund_status', [0, 3])->when(isset($where['shipping_type']), function ($query) {
  55. $query->where('shipping_type', 1);
  56. })->where('is_del', 0);
  57. break;
  58. case 7://已支付 部分发货
  59. $query->where('paid', 1)->where('status', 4)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  60. break;
  61. case 2://已支付 待收货
  62. $query->where('paid', 1)->where('status', 1)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  63. break;
  64. case 3:// 已支付 已收货 待评价
  65. $query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  66. break;
  67. case 4:// 交易完成
  68. $query->where('paid', 1)->where('status', 3)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  69. break;
  70. case 5://已支付 待核销
  71. $query->where('paid', 1)->where('status', 0)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
  72. break;
  73. case 6://已支付 已核销 没有退款
  74. $query->where('paid', 1)->where('status', 2)->where('refund_status', 0)->where('shipping_type', 2)->where('is_del', 0);
  75. break;
  76. case -1://退款中
  77. $query->where('paid', 1)->whereIn('refund_status', [1, 4])->where('is_del', 0);
  78. break;
  79. case -2://已退款
  80. $query->where('paid', 1)->where('refund_status', 2)->where('is_del', 0);
  81. break;
  82. case -3://退款
  83. $query->where('paid', 1)->whereIn('refund_status', [1, 2, 4])->where('is_del', 0);
  84. break;
  85. case -4://已删除
  86. $query->where('is_del', 1);
  87. break;
  88. case 9://全部用户未删除的订单
  89. $query->where('is_del', 0);
  90. break;
  91. }
  92. })->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
  93. if (in_array($where['paid'], [0, 1])) {
  94. $query->where('paid', $where['paid']);
  95. }
  96. })->when(isset($where['order_status']) && $where['order_status'] !== '', function ($query) use ($where) {
  97. switch ((int)$where['order_status']) {
  98. case 0://未发货
  99. $query->where('status', 0)->where('refund_status', 0)->where('is_del', 0);
  100. break;
  101. case 1://已发货
  102. $query->where('paid', 1)->where('status', 1)->whereIn('refund_status', [0, 3])->when(isset($where['shipping_type']), function ($query) {
  103. $query->where('shipping_type', 1);
  104. })->where('is_del', 0);
  105. break;
  106. case 2://已收货
  107. $query->where('paid', 1)->where('status', 2)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  108. break;
  109. case 3://已完成
  110. $query->where('paid', 1)->where('status', 3)->whereIn('refund_status', [0, 3])->where('is_del', 0);
  111. break;
  112. case -2://已退款
  113. $query->where('paid', 1)->where('status', -2)->where('is_del', 0);
  114. break;
  115. }
  116. })->when(isset($where['type']), function ($query) use ($where) {
  117. switch ($where['type']) {
  118. case 1:
  119. $query->where('combination_id', 0)->where('seckill_id', 0)->where('bargain_id', 0)->where('advance_id', 0);
  120. break;
  121. case 2:
  122. $query->where('pink_id|combination_id', ">", 0);
  123. break;
  124. case 3:
  125. $query->where('seckill_id', ">", 0);
  126. break;
  127. case 4:
  128. $query->where('bargain_id', ">", 0);
  129. break;
  130. case 5:
  131. $query->where('advance_id', ">", 0);
  132. break;
  133. case 6:
  134. $query->where(function ($query) {
  135. $query->where('one_brokerage', '>', 0)->whereOr('two_brokerage', '>', 0);
  136. });
  137. break;
  138. }
  139. })->when(isset($where['pay_type']), function ($query) use ($where) {
  140. switch ($where['pay_type']) {
  141. case 1:
  142. $query->where('pay_type', 'weixin');
  143. break;
  144. case 2:
  145. $query->where('pay_type', 'yue');
  146. break;
  147. case 3:
  148. $query->where('pay_type', 'offline');
  149. break;
  150. case 4:
  151. $query->where('pay_type', 'alipay');
  152. break;
  153. }
  154. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  155. if ($fieldKey !== 'title') {
  156. $query->where(trim($fieldKey), trim($realName));
  157. } else {
  158. $query->where('id', 'in', function ($que) use ($where) {
  159. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  160. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  161. })->field(['oid'])->select();
  162. });
  163. }
  164. })->when($realName && !$fieldKey, function ($query) use ($where) {
  165. $query->where(function ($que) use ($where) {
  166. $que->whereLike('order_id|real_name', '%' . $where['real_name'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  167. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  168. })->whereOr('id', 'in', function ($que) use ($where) {
  169. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  170. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  171. })->field(['oid'])->select();
  172. });
  173. });
  174. })->when(isset($where['store_id']) && $where['store_id'], function ($query) use ($where) {
  175. $query->where('store_id', $where['store_id']);
  176. })->when(isset($where['unique']), function ($query) use ($where) {
  177. $query->where('unique', $where['unique']);
  178. })->when(isset($where['is_remind']), function ($query) use ($where) {
  179. $query->where('is_remind', $where['is_remind']);
  180. })->when(isset($where['refundTypes']) && $where['refundTypes'] != '', function ($query) use ($where) {
  181. switch ((int)$where['refundTypes']) {
  182. case 1:
  183. $query->where('refund_type', 'in', '1,2');
  184. break;
  185. case 2:
  186. $query->where('refund_type', 4);
  187. break;
  188. case 3:
  189. $query->where('refund_type', 5);
  190. break;
  191. case 4:
  192. $query->where('refund_type', 6);
  193. break;
  194. }
  195. })->when(isset($where['is_refund']) && $where['is_refund'] !== '', function ($query) use ($where) {
  196. if ($where['is_refund'] == 1) {
  197. $query->where('refund_status', 2);
  198. } else {
  199. $query->where('refund_status', 0);
  200. }
  201. });
  202. }
  203. /**
  204. * 获取某一个月订单数量
  205. * @param array $where
  206. * @param string $month
  207. * @return int
  208. */
  209. public function getMonthCount(array $where, string $month)
  210. {
  211. return $this->search($where)->whereMonth('add_time', $month)->count();
  212. }
  213. /**
  214. * 订单搜索列表
  215. * @param array $where
  216. * @param array $field
  217. * @param int $page
  218. * @param int $limit
  219. * @param array $with
  220. * @return array
  221. * @throws \think\db\exception\DataNotFoundException
  222. * @throws \think\db\exception\DbException
  223. * @throws \think\db\exception\ModelNotFoundException
  224. */
  225. public function getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  226. {
  227. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  228. $query->page($page, $limit);
  229. })->order('pay_time DESC,id DESC')->select()->toArray();
  230. }
  231. /**
  232. * 订单搜索列表
  233. * @param array $where
  234. * @param array $field
  235. * @param int $page
  236. * @param int $limit
  237. * @param array $with
  238. * @param string $order
  239. * @return array
  240. * @throws \think\db\exception\DataNotFoundException
  241. * @throws \think\db\exception\DbException
  242. * @throws \think\db\exception\ModelNotFoundException
  243. */
  244. public function getOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], $order = 'add_time DESC,id DESC')
  245. {
  246. return $this->search($where)->field($field)->with(array_merge(['user', 'spread', 'refund'], $with))->when($page && $limit, function ($query) use ($page, $limit) {
  247. $query->page($page, $limit);
  248. })->order($order)->select()->toArray();
  249. }
  250. /**
  251. * 获取订单总数
  252. * @param array $where
  253. * @return int
  254. */
  255. public function count(array $where = []): int
  256. {
  257. return $this->search($where)->count();
  258. }
  259. /**
  260. * 聚合查询
  261. * @param array $where
  262. * @param string $field
  263. * @param string $together
  264. * @return int
  265. */
  266. public function together(array $where, string $field, string $together = 'sum')
  267. {
  268. if (!in_array($together, ['sum', 'max', 'min', 'avg'])) {
  269. return 0;
  270. }
  271. return $this->search($where)->{$together}($field);
  272. }
  273. /**
  274. * 查找指定条件下的订单数据以数组形式返回
  275. * @param array $where
  276. * @param string $field
  277. * @param string $key
  278. * @param string $group
  279. * @return array
  280. */
  281. public function column(array $where, string $field, string $key = '', string $group = '')
  282. {
  283. return $this->search($where)->when($group, function ($query) use ($group) {
  284. $query->group($group);
  285. })->column($field, $key);
  286. }
  287. /**
  288. * 获取订单id下没有删除的订单数量
  289. * @param array $ids
  290. * @return int
  291. */
  292. public function getOrderIdsCount(array $ids)
  293. {
  294. return $this->getModel()->whereIn('id', $ids)->where('is_del', 0)->count();
  295. }
  296. /**
  297. * 获取一段时间内订单列表
  298. * @param $datebefor
  299. * @param $dateafter
  300. * @return mixed
  301. */
  302. public function orderAddTimeList($datebefor, $dateafter, $timeType = "week")
  303. {
  304. return $this->getModel()->where('add_time', 'between time', [$datebefor, $dateafter])->where('paid', 1)->where('refund_status', 0)->whereIn('pid', [-1, 0])
  305. ->when($timeType, function ($query) use ($timeType) {
  306. $timeUnix = "%w";
  307. switch ($timeType) {
  308. case "week" :
  309. $timeUnix = "%w";
  310. break;
  311. case "month" :
  312. $timeUnix = "%d";
  313. break;
  314. case "year" :
  315. $timeUnix = "%m";
  316. break;
  317. case "30" :
  318. $timeUnix = "%m-%d";
  319. break;
  320. }
  321. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
  322. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  323. })
  324. ->order('add_time asc')
  325. ->select()->toArray();
  326. }
  327. /**
  328. * 统计总数上期
  329. * @param $pre_datebefor
  330. * @param $pre_dateafter
  331. * @return array|\think\Model|null
  332. * @throws \think\db\exception\DataNotFoundException
  333. * @throws \think\db\exception\DbException
  334. * @throws \think\db\exception\ModelNotFoundException
  335. */
  336. public function preTotalFind($pre_datebefor, $pre_dateafter)
  337. {
  338. return $this->getModel()->where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  339. ->field("count(*) as count,sum(pay_price) as price")
  340. ->find();
  341. }
  342. /**
  343. * 获取一段时间内订单列表
  344. * @param $now_datebefor
  345. * @param $now_dateafter
  346. * @return mixed
  347. */
  348. public function nowOrderList($now_datebefor, $now_dateafter, $timeType = "week")
  349. {
  350. return $this->getModel()->where('add_time', 'between time', [$now_datebefor, $now_dateafter])->where('paid', 1)->where('refund_status', 0)->whereIn('pid', [-1, 0])
  351. ->when($timeType, function ($query) use ($timeType) {
  352. $timeUnix = "%w";
  353. switch ($timeType) {
  354. case "week" :
  355. $timeUnix = "%w";
  356. break;
  357. case "month" :
  358. $timeUnix = "%d";
  359. break;
  360. case "year" :
  361. $timeUnix = "%m";
  362. break;
  363. }
  364. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
  365. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  366. })
  367. ->order('add_time asc')
  368. ->select()->toArray();
  369. }
  370. /**
  371. * 获取订单数量
  372. * @return int
  373. */
  374. public function storeOrderCount()
  375. {
  376. return $this->search(['paid' => 1, 'is_del' => 0, 'refund_status' => 0, 'status' => 1, 'shipping_type' => 1, 'pid' => 0])->count();
  377. }
  378. /**
  379. * 获取特定时间内订单总价
  380. * @param $time
  381. * @return float
  382. */
  383. public function todaySales($time)
  384. {
  385. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time', 'pid' => 0])->sum('pay_price');
  386. }
  387. /**
  388. * 获取特定时间内订单总价
  389. * @param $time
  390. * @return float
  391. */
  392. public function thisWeekSales($time)
  393. {
  394. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'week', 'timeKey' => 'pay_time', 'pid' => 0])->sum('pay_price');
  395. }
  396. /**
  397. * 总销售额
  398. * @return float
  399. */
  400. public function totalSales($time)
  401. {
  402. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time', 'pid' => 0])->sum('pay_price');
  403. }
  404. public function newOrderUpdates($newOrderId)
  405. {
  406. return $this->getModel()->where('order_id', 'in', $newOrderId)->update(['is_remind' => 1]);
  407. }
  408. /**
  409. * 获取特定时间内订单量
  410. * @param $time
  411. * @return float
  412. */
  413. public function todayOrderVisit($time, $week)
  414. {
  415. switch ($week) {
  416. case 1:
  417. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time', 'paid' => 1, 'refund_status' => 0, 'pid' => 0])->count();
  418. case 2:
  419. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'add_time', 'paid' => 1, 'refund_status' => 0, 'pid' => 0])->count();
  420. }
  421. }
  422. /**
  423. * 获取订单详情
  424. * @param $uid
  425. * @param $key
  426. * @return array|\think\Model|null
  427. * @throws \think\db\exception\DataNotFoundException
  428. * @throws \think\db\exception\DbException
  429. * @throws \think\db\exception\ModelNotFoundException
  430. */
  431. public function getUserOrderDetail(string $key, int $uid, $with = [])
  432. {
  433. return $this->getOne(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0], '*', $with);
  434. }
  435. /**
  436. * 获取用户推广订单
  437. * @param array $where
  438. * @param string $field
  439. * @param int $page
  440. * @param int $limit
  441. * @param array $with
  442. * @return array
  443. * @throws \think\db\exception\DataNotFoundException
  444. * @throws \think\db\exception\DbException
  445. * @throws \think\db\exception\ModelNotFoundException
  446. */
  447. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  448. {
  449. return $this->search($where)->with($with)->field($field)->page($page, $limit)->order('id DESC')->select()->toArray();
  450. }
  451. /**
  452. * 订单每月统计数据
  453. * @param int $page
  454. * @param int $limit
  455. * @return array
  456. */
  457. public function getOrderDataPriceCount(array $where, array $field, int $page, int $limit)
  458. {
  459. return $this->search($where)
  460. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  461. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  462. }
  463. /**
  464. * 获取当前时间到指定时间的支付金额 管理员
  465. * @param $start 开始时间
  466. * @param $stop 结束时间
  467. * @return mixed
  468. */
  469. public function chartTimePrice($start, $stop)
  470. {
  471. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
  472. ->where('add_time', '>=', $start)
  473. ->where('add_time', '<', $stop)
  474. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  475. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  476. ->order('add_time ASC')->select()->toArray();
  477. }
  478. /**
  479. * 获取当前时间到指定时间的支付订单数 管理员
  480. * @param $start 开始时间
  481. * @param $stop 结束时间
  482. * @return mixed
  483. */
  484. public function chartTimeNumber($start, $stop)
  485. {
  486. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
  487. ->where('add_time', '>=', $start)
  488. ->where('add_time', '<', $stop)
  489. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  490. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  491. ->order('add_time ASC')->select()->toArray();
  492. }
  493. /**
  494. * 获取用户已购买此活动商品的个数
  495. * @param $uid
  496. * @param $type
  497. * @param $typeId
  498. * @return int
  499. */
  500. public function getBuyCount($uid, $type, $typeId): int
  501. {
  502. return $this->getModel()
  503. ->where('uid', $uid)
  504. ->where($type, $typeId)
  505. ->where(function ($query) {
  506. $query->where('paid', 1)->whereOr(function ($query1) {
  507. $query1->where('paid', 0)->where('is_del', 0);
  508. });
  509. })->value('sum(total_num)') ?? 0;
  510. }
  511. /**
  512. * 获取没有支付的订单列表
  513. * @param array|string[] $field
  514. * @return array
  515. * @throws \think\db\exception\DataNotFoundException
  516. * @throws \think\db\exception\DbException
  517. * @throws \think\db\exception\ModelNotFoundException
  518. */
  519. public function getOrderUnPaidList(array $field = ['*'])
  520. {
  521. return $this->getModel()->where(['paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  522. ->where('pay_type', '<>', 'offline')->field($field)->select();
  523. }
  524. /** 根据时间获取营业额
  525. * @param array $where
  526. * @return float|int
  527. */
  528. public function getOrderMoneyByTime(array $where)
  529. {
  530. if (isset($where['day'])) {
  531. return $this->getModel()->where(['refund_status' => 0, 'paid' => 1])->whereDay('add_time', date("Y-m-d", strtotime($where['day'])))->sum('pay_price');
  532. }
  533. return 0;
  534. }
  535. /**
  536. * 用户趋势数据
  537. * @param $time
  538. * @param $type
  539. * @param $timeType
  540. * @return mixed
  541. */
  542. public function getTrendData($time, $type, $timeType, $str)
  543. {
  544. return $this->getModel()->when($type != '', function ($query) use ($type) {
  545. $query->where('channel_type', $type);
  546. })->where(function ($query) use ($time) {
  547. if ($time[0] == $time[1]) {
  548. $query->whereDay('pay_time', $time[0]);
  549. } else {
  550. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  551. $query->whereTime('pay_time', 'between', $time);
  552. }
  553. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  554. ->group('days')->select()->toArray();
  555. }
  556. /**
  557. * 用户地域数据
  558. * @param $time
  559. * @param $userType
  560. * @return mixed
  561. */
  562. public function getRegion($time, $userType)
  563. {
  564. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  565. $query->where('channel_type', $userType);
  566. })->where(function ($query) use ($time) {
  567. if ($time[0] == $time[1]) {
  568. $query->whereDay('pay_time', $time[0]);
  569. } else {
  570. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  571. $query->whereTime('pay_time', 'between', $time);
  572. }
  573. })->field('sum(pay_price) as payPrice,province')
  574. ->group('province')->select()->toArray();
  575. }
  576. /**
  577. * 商品趋势
  578. * @param $time
  579. * @param $timeType
  580. * @param $field
  581. * @param $str
  582. * @return mixed
  583. */
  584. public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '')
  585. {
  586. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  587. if ($field == 'pay_time') {
  588. $query->where('paid', 1)->where('pid', '>=', 0);
  589. } elseif ($field == 'refund_reason_time') {
  590. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  591. } elseif ($field == 'add_time') {
  592. if ($orderStatus == 'pay') {
  593. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', 0);
  594. } elseif ($orderStatus == 'refund') {
  595. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  596. }
  597. }
  598. })->where(function ($query) use ($time, $field) {
  599. if ($time[0] == $time[1]) {
  600. $query->whereDay($field, $time[0]);
  601. } else {
  602. $query->whereTime($field, 'between', $time);
  603. }
  604. })->where('pid', '>=', 0)->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  605. }
  606. /** 按照支付时间统计支付金额
  607. * @param array $where
  608. * @param string $sumField
  609. * @return mixed
  610. */
  611. public function getDayTotalMoney(array $where, string $sumField)
  612. {
  613. return $this->search($where)
  614. ->when(isset($where['timeKey']), function ($query) use ($where) {
  615. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  616. })
  617. ->sum($sumField);
  618. }
  619. /**时间段订单数统计
  620. * @param array $where
  621. * @param string $countField
  622. * @return int
  623. */
  624. public function getDayOrderCount(array $where, string $countField = "*")
  625. {
  626. return $this->search($where)
  627. ->when(isset($where['timeKey']), function ($query) use ($where) {
  628. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  629. })
  630. ->count($countField);
  631. }
  632. /** 时间分组订单付款金额统计
  633. * @param array $where
  634. * @param string $sumField
  635. * @return mixed
  636. */
  637. public function getDayGroupMoney(array $where, string $sumField, string $group)
  638. {
  639. return $this->search($where)
  640. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  641. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  642. $timeUinx = "%H";
  643. if ($where['timeKey']['days'] == 1) {
  644. $timeUinx = "%H";
  645. } elseif ($where['timeKey']['days'] == 30) {
  646. $timeUinx = "%Y-%m-%d";
  647. } elseif ($where['timeKey']['days'] == 365) {
  648. $timeUinx = "%Y-%m";
  649. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  650. $timeUinx = "%Y-%m-%d";
  651. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  652. $timeUinx = "%Y-%m";
  653. }
  654. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  655. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  656. })
  657. ->order('pay_time ASC')->select()->toArray();
  658. }
  659. /**时间分组订单数统计
  660. * @param array $where
  661. * @param string $sumField
  662. * @return mixed
  663. */
  664. public function getOrderGroupCount(array $where, string $sumField = "*")
  665. {
  666. return $this->search($where)
  667. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  668. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  669. $timeUinx = "%H";
  670. if ($where['timeKey']['days'] == 1) {
  671. $timeUinx = "%H";
  672. } elseif ($where['timeKey']['days'] == 30) {
  673. $timeUinx = "%Y-%m-%d";
  674. } elseif ($where['timeKey']['days'] == 365) {
  675. $timeUinx = "%Y-%m";
  676. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  677. $timeUinx = "%Y-%m-%d";
  678. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  679. $timeUinx = "%Y-%m";
  680. }
  681. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  682. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  683. })
  684. ->order('pay_time ASC')->select()->toArray();
  685. }
  686. /**时间段支付订单人数
  687. * @param $where
  688. * @return mixed
  689. */
  690. public function getPayOrderPeople($where)
  691. {
  692. return $this->search($where)
  693. ->when(isset($where['timeKey']), function ($query) use ($where) {
  694. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  695. })
  696. ->field('uid')
  697. ->distinct(true)
  698. ->select()->toArray();
  699. }
  700. /**时间段分组统计支付订单人数
  701. * @param $where
  702. * @return mixed
  703. */
  704. public function getPayOrderGroupPeople($where)
  705. {
  706. return $this->search($where)
  707. ->when(isset($where['timeKey']), function ($query) use ($where) {
  708. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  709. if ($where['timeKey']['days'] == 1) {
  710. $timeUinx = "%H";
  711. } elseif ($where['timeKey']['days'] == 30) {
  712. $timeUinx = "%Y-%m-%d";
  713. } elseif ($where['timeKey']['days'] == 365) {
  714. $timeUinx = "%Y-%m";
  715. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  716. $timeUinx = "%Y-%m-%d";
  717. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  718. $timeUinx = "%Y-%m";
  719. } else {
  720. $timeUinx = "%H";
  721. }
  722. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  723. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  724. })
  725. ->order('pay_time ASC')->select()->toArray();
  726. }
  727. /**获取批量打印电子面单数据
  728. * @param array $where
  729. * @param string $filed
  730. * @return array
  731. * @throws \think\db\exception\DataNotFoundException
  732. * @throws \think\db\exception\DbException
  733. * @throws \think\db\exception\ModelNotFoundException
  734. */
  735. public function getOrderDumpData(array $where, $filed = "*")
  736. {
  737. $where['status'] = 1;
  738. $where['refund_status'] = 0;
  739. $where['paid'] = 1;
  740. $where['is_del'] = 0;
  741. $where['shipping_type'] = 1;
  742. $where['is_system_del'] = 0;
  743. return $this->search($where)->field($filed)->select()->toArray();
  744. }
  745. /**
  746. * @param array $where
  747. * @param string $field
  748. * @return array
  749. * @throws \think\db\exception\DataNotFoundException
  750. * @throws \think\db\exception\DbException
  751. * @throws \think\db\exception\ModelNotFoundException
  752. */
  753. public function getOrderListByWhere(array $where, $field = "*")
  754. {
  755. return $this->search($where)->field($field)->select()->toArray();
  756. }
  757. /**批量修改订单
  758. * @param array $ids
  759. * @param array $data
  760. * @param string|null $key
  761. * @return \crmeb\basic\BaseModel
  762. */
  763. public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
  764. {
  765. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  766. }
  767. /**根据orderid校验符合状态的发货数据
  768. * @param $order_ids
  769. * @return array|\crmeb\basic\BaseModel
  770. * @throws \think\db\exception\DataNotFoundException
  771. * @throws \think\db\exception\DbException
  772. * @throws \think\db\exception\ModelNotFoundException
  773. */
  774. public function getCanDevlieryOrder($key, $value)
  775. {
  776. $model = $this->getModel();
  777. if (is_array($value)) {
  778. $model = $model->whereIn($key, $value);
  779. } else {
  780. $model = $model->where($key, $value);
  781. }
  782. $model = $model->where(['status' => 0, 'is_del' => 0, 'paid' => 1, 'shipping_type' => 1, 'is_system_del' => 0, 'refund_status' => 0])->field('id, order_id')->select()->toArray();
  783. return $model;
  784. }
  785. /**
  786. * 查询退款订单
  787. * @param $where
  788. * @param $page
  789. * @param $limit
  790. * @return array
  791. * @throws \think\db\exception\DataNotFoundException
  792. * @throws \think\db\exception\DbException
  793. * @throws \think\db\exception\ModelNotFoundException
  794. */
  795. public function getRefundList($where, $page = 0, $limit = 0)
  796. {
  797. $model = $this->getModel()
  798. ->where('paid', 1)->where('is_system_del', 0)
  799. ->when($where['refund_type'] == 0, function ($query) use ($where) {
  800. $query->where('refund_type', '>', 0);
  801. })
  802. ->when($where['order_id'] != '', function ($query) use ($where) {
  803. $query->where('order_id', $where['order_id']);
  804. })
  805. ->when($where['refund_type'], function ($query) use ($where) {
  806. $query->where('refund_type', $where['refund_type']);
  807. })
  808. ->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
  809. $query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
  810. })
  811. ->with(array_merge(['user', 'spread']));
  812. $count = $model->count();
  813. $list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  814. $query->page($page, $limit);
  815. })->order('refund_reason_time desc')->select()->toArray();
  816. return compact('list', 'count');
  817. }
  818. /**
  819. * 订单搜索列表
  820. * @param array $where
  821. * @param array $field
  822. * @param int $page
  823. * @param int $limit
  824. * @param array $with
  825. * @param string $order
  826. * @return array
  827. * @throws \think\db\exception\DataNotFoundException
  828. * @throws \think\db\exception\DbException
  829. * @throws \think\db\exception\ModelNotFoundException
  830. */
  831. public function getOutOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC'): array
  832. {
  833. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  834. $query->page($page, $limit);
  835. })->order($order)->select()->toArray();
  836. }
  837. /**
  838. * 秒杀参与人统计
  839. * @param $id
  840. * @param $keyword
  841. * @param int $page
  842. * @param int $limit
  843. * @return mixed
  844. */
  845. public function seckillPeople($id, $keyword, $page = 0, $limit = 0)
  846. {
  847. return $this->getModel()->where('paid', 1)->whereIn('refund_type', [0, 3])
  848. ->when($id != 0, function ($query) use ($id) {
  849. $query->where('seckill_id', $id);
  850. })->when($keyword != '', function ($query) use ($keyword) {
  851. $query->where('real_name|uid|user_phone', 'like', '%' . $keyword . '%');
  852. })->where('paid', 1)->field([
  853. 'real_name',
  854. 'uid',
  855. 'user_phone',
  856. 'SUM(total_num) as goods_num',
  857. 'COUNT(id) as order_num',
  858. 'SUM(pay_price) as total_price',
  859. 'add_time'
  860. ])->group('uid')->order("add_time desc")->when($page && $limit, function ($query) use ($page, $limit) {
  861. $query->page($page, $limit);
  862. })->select()->toArray();
  863. }
  864. /**
  865. * 秒杀订单统计
  866. * @param $id
  867. * @param $where
  868. * @param int $page
  869. * @param int $limit
  870. * @return array
  871. * @throws \think\db\exception\DataNotFoundException
  872. * @throws \think\db\exception\DbException
  873. * @throws \think\db\exception\ModelNotFoundException
  874. */
  875. public function seckillOrder($id, $where, $page = 0, $limit = 0)
  876. {
  877. return $this->search($where)->where('seckill_id', $id)
  878. ->when($page && $limit, function ($query) use ($page, $limit) {
  879. $query->page($page, $limit);
  880. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->select()->toArray();
  881. }
  882. /**
  883. * 砍价订单统计
  884. * @param $id
  885. * @param $where
  886. * @param int $page
  887. * @param int $limit
  888. * @return array
  889. * @throws \think\db\exception\DataNotFoundException
  890. * @throws \think\db\exception\DbException
  891. * @throws \think\db\exception\ModelNotFoundException
  892. */
  893. public function bargainStatisticsOrder($id, $where, $page = 0, $limit = 0)
  894. {
  895. return $this->search($where)->where('bargain_id', $id)
  896. ->when($page && $limit, function ($query) use ($page, $limit) {
  897. $query->page($page, $limit);
  898. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->select()->toArray();
  899. }
  900. /**
  901. * 拼团订单统计
  902. * @param $id
  903. * @param $where
  904. * @param int $page
  905. * @param int $limit
  906. * @return array
  907. * @throws \think\db\exception\DataNotFoundException
  908. * @throws \think\db\exception\DbException
  909. * @throws \think\db\exception\ModelNotFoundException
  910. */
  911. public function combinationStatisticsOrder($id, $where, $page = 0, $limit = 0)
  912. {
  913. return $this->search($where)->where('combination_id', $id)
  914. ->when($page && $limit, function ($query) use ($page, $limit) {
  915. $query->page($page, $limit);
  916. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->select()->toArray();
  917. }
  918. }