StoreOrderDao.php 38 KB

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