StoreOrderDao.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. }
  89. })->when(isset($where['type']), function ($query) use ($where) {
  90. switch ($where['type']) {
  91. case 1:
  92. $query->where('combination_id', 0)->where('seckill_id', 0)->where('bargain_id', 0);
  93. break;
  94. case 2:
  95. $query->where('pink_id|combination_id', ">", 0);
  96. break;
  97. case 3:
  98. $query->where('seckill_id', ">", 0);
  99. break;
  100. case 4:
  101. $query->where('bargain_id', ">", 0);
  102. break;
  103. case 5:
  104. $query->where('advance_id', ">", 0);
  105. break;
  106. }
  107. })->when(isset($where['pay_type']), function ($query) use ($where) {
  108. switch ($where['pay_type']) {
  109. case 1:
  110. $query->where('pay_type', 'weixin');
  111. break;
  112. case 2:
  113. $query->where('pay_type', 'yue');
  114. break;
  115. case 3:
  116. $query->where('pay_type', 'offline');
  117. break;
  118. case 4:
  119. $query->where('pay_type', 'alipay');
  120. break;
  121. }
  122. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  123. if ($fieldKey !== 'title') {
  124. $query->where(trim($fieldKey), trim($realName));
  125. } else {
  126. $query->where('id', 'in', function ($que) use ($where) {
  127. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  128. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  129. })->field(['oid'])->select();
  130. });
  131. }
  132. })->when($realName && !$fieldKey, function ($query) use ($where) {
  133. $query->where(function ($que) use ($where) {
  134. $que->whereLike('order_id|real_name', '%' . $where['real_name'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  135. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  136. })->whereOr('id', 'in', function ($que) use ($where) {
  137. $que->name('store_order_cart_info')->whereIn('product_id', function ($q) use ($where) {
  138. $q->name('store_product')->whereLike('store_name|keyword', '%' . $where['real_name'] . '%')->field(['id'])->select();
  139. })->field(['oid'])->select();
  140. });
  141. });
  142. })->when(isset($where['store_id']) && $where['store_id'], function ($query) use ($where) {
  143. $query->where('store_id', $where['store_id']);
  144. })->when(isset($where['unique']), function ($query) use ($where) {
  145. $query->where('unique', $where['unique']);
  146. })->when(isset($where['is_remind']), function ($query) use ($where) {
  147. $query->where('is_remind', $where['is_remind']);
  148. })->when(isset($where['refundTypes']) && $where['refundTypes'] != '', function ($query) use ($where) {
  149. switch ((int)$where['refundTypes']) {
  150. case 1:
  151. $query->where('refund_type', 'in', '1,2');
  152. break;
  153. case 2:
  154. $query->where('refund_type', 4);
  155. break;
  156. case 3:
  157. $query->where('refund_type', 5);
  158. break;
  159. case 4:
  160. $query->where('refund_type', 6);
  161. break;
  162. }
  163. });
  164. }
  165. /**
  166. * 获取某一个月订单数量
  167. * @param array $where
  168. * @param string $month
  169. * @return int
  170. */
  171. public function getMonthCount(array $where, string $month)
  172. {
  173. return $this->search($where)->whereMonth('add_time', $month)->count();
  174. }
  175. /**
  176. * 订单搜索列表
  177. * @param array $where
  178. * @param array $field
  179. * @param int $page
  180. * @param int $limit
  181. * @param array $with
  182. * @return array
  183. * @throws \think\db\exception\DataNotFoundException
  184. * @throws \think\db\exception\DbException
  185. * @throws \think\db\exception\ModelNotFoundException
  186. */
  187. public function getList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  188. {
  189. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  190. $query->page($page, $limit);
  191. })->order('pay_time DESC,id DESC')->select()->toArray();
  192. }
  193. /**
  194. * 订单搜索列表
  195. * @param array $where
  196. * @param array $field
  197. * @param int $page
  198. * @param int $limit
  199. * @param array $with
  200. * @return array
  201. * @throws \think\db\exception\DataNotFoundException
  202. * @throws \think\db\exception\DbException
  203. * @throws \think\db\exception\ModelNotFoundException
  204. */
  205. public function getOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [])
  206. {
  207. return $this->search($where)->field($field)->with(array_merge(['user', 'spread'], $with))->when($page && $limit, function ($query) use ($page, $limit) {
  208. $query->page($page, $limit);
  209. })->order('pay_time DESC,id DESC')->select()->toArray();
  210. }
  211. /**
  212. * 获取订单总数
  213. * @param array $where
  214. * @return int
  215. */
  216. public function count(array $where = []): int
  217. {
  218. return $this->search($where)->count();
  219. }
  220. /**
  221. * 聚合查询
  222. * @param array $where
  223. * @param string $field
  224. * @param string $together
  225. * @return int
  226. */
  227. public function together(array $where, string $field, string $together = 'sum')
  228. {
  229. if (!in_array($together, ['sum', 'max', 'min', 'avg'])) {
  230. return 0;
  231. }
  232. return $this->search($where)->{$together}($field);
  233. }
  234. /**
  235. * 查找指定条件下的订单数据以数组形式返回
  236. * @param array $where
  237. * @param string $field
  238. * @param string $key
  239. * @param string $group
  240. * @return array
  241. */
  242. public function column(array $where, string $field, string $key = '', string $group = '')
  243. {
  244. return $this->search($where)->when($group, function ($query) use ($group) {
  245. $query->group($group);
  246. })->column($field, $key);
  247. }
  248. /**
  249. * 获取订单id下没有删除的订单数量
  250. * @param array $ids
  251. * @return int
  252. */
  253. public function getOrderIdsCount(array $ids)
  254. {
  255. return $this->getModel()->whereIn('id', $ids)->where('is_del', 0)->count();
  256. }
  257. /**
  258. * 获取一段时间内订单列表
  259. * @param $datebefor
  260. * @param $dateafter
  261. * @return mixed
  262. */
  263. public function orderAddTimeList($datebefor, $dateafter, $timeType = "week")
  264. {
  265. return $this->getModel()->where('add_time', 'between time', [$datebefor, $dateafter])->where('paid', 1)->where('refund_status', 0)->whereIn('pid', [-1, 0])
  266. ->when($timeType, function ($query) use ($timeType) {
  267. switch ($timeType) {
  268. case "week" :
  269. $timeUnix = "%w";
  270. break;
  271. case "month" :
  272. $timeUnix = "%d";
  273. break;
  274. case "year" :
  275. $timeUnix = "%m";
  276. break;
  277. case "30" :
  278. $timeUnix = "%m-%d";
  279. break;
  280. }
  281. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
  282. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  283. })
  284. ->order('add_time asc')
  285. ->select()->toArray();
  286. }
  287. /**
  288. * 统计总数上期
  289. * @param $pre_datebefor
  290. * @param $pre_dateafter
  291. * @return array|\think\Model|null
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function preTotalFind($pre_datebefor, $pre_dateafter)
  297. {
  298. return $this->getModel()->where('add_time', 'between time', [$pre_datebefor, $pre_dateafter])
  299. ->field("count(*) as count,sum(pay_price) as price")
  300. ->find();
  301. }
  302. /**
  303. * 获取一段时间内订单列表
  304. * @param $now_datebefor
  305. * @param $now_dateafter
  306. * @return mixed
  307. */
  308. public function nowOrderList($now_datebefor, $now_dateafter, $timeType = "week")
  309. {
  310. return $this->getModel()->where('add_time', 'between time', [$now_datebefor, $now_dateafter])->where('paid', 1)->where('refund_status', 0)->whereIn('pid', [-1, 0])
  311. ->when($timeType, function ($query) use ($timeType) {
  312. switch ($timeType) {
  313. case "week" :
  314. $timeUnix = "%w";
  315. break;
  316. case "month" :
  317. $timeUnix = "%d";
  318. break;
  319. case "year" :
  320. $timeUnix = "%m";
  321. break;
  322. }
  323. $query->field("FROM_UNIXTIME(add_time,'$timeUnix') as day,count(*) as count,sum(pay_price) as price");
  324. $query->group("FROM_UNIXTIME(add_time, '$timeUnix')");
  325. })
  326. ->order('add_time asc')
  327. ->select()->toArray();
  328. }
  329. /**
  330. * 获取订单数量
  331. * @return int
  332. */
  333. public function storeOrderCount()
  334. {
  335. return $this->search(['paid' => 1, 'is_del' => 0, 'refund_status' => 0, 'status' => 1, 'shipping_type' => 1])->count();
  336. }
  337. /**
  338. * 获取特定时间内订单总价
  339. * @param $time
  340. * @return float
  341. */
  342. public function todaySales($time)
  343. {
  344. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time', 'pid' => 0])->sum('pay_price');
  345. }
  346. /**
  347. * 获取特定时间内订单总价
  348. * @param $time
  349. * @return float
  350. */
  351. public function thisWeekSales($time)
  352. {
  353. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'week', 'timeKey' => 'pay_time', 'pid' => 0])->sum('pay_price');
  354. }
  355. /**
  356. * 总销售额
  357. * @return float
  358. */
  359. public function totalSales($time)
  360. {
  361. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time', 'pid' => 0])->sum('pay_price');
  362. }
  363. public function newOrderUpdates($newOrderId)
  364. {
  365. return $this->getModel()->where('order_id', 'in', $newOrderId)->update(['is_remind' => 1]);
  366. }
  367. /**
  368. * 获取特定时间内订单量
  369. * @param $time
  370. * @return float
  371. */
  372. public function todayOrderVisit($time, $week)
  373. {
  374. switch ($week) {
  375. case 1:
  376. return $this->search(['time' => $time ?: 'today', 'timeKey' => 'add_time', 'paid' => 1, 'refund_status' => 0, 'pid' => 0])->count();
  377. case 2:
  378. return $this->search(['time' => $time ?: 'week', 'timeKey' => 'add_time', 'paid' => 1, 'refund_status' => 0, 'pid' => 0])->count();
  379. }
  380. }
  381. /**
  382. * 获取订单详情
  383. * @param $uid
  384. * @param $key
  385. * @return array|\think\Model|null
  386. * @throws \think\db\exception\DataNotFoundException
  387. * @throws \think\db\exception\DbException
  388. * @throws \think\db\exception\ModelNotFoundException
  389. */
  390. public function getUserOrderDetail(string $key, int $uid, $with = [])
  391. {
  392. return $this->getOne(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0], '*', $with);
  393. }
  394. /**
  395. * 获取用户推广订单
  396. * @param array $where
  397. * @param string $field
  398. * @param int $page
  399. * @param int $limit
  400. * @param array $with
  401. * @return array
  402. * @throws \think\db\exception\DataNotFoundException
  403. * @throws \think\db\exception\DbException
  404. * @throws \think\db\exception\ModelNotFoundException
  405. */
  406. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  407. {
  408. return $this->search($where)->with($with)->field($field)->page($page, $limit)->order('id DESC')->select()->toArray();
  409. }
  410. /**
  411. * 订单每月统计数据
  412. * @param int $page
  413. * @param int $limit
  414. * @return array
  415. */
  416. public function getOrderDataPriceCount(array $field, int $page, int $limit, $time)
  417. {
  418. return $this->search(['paid' => 1, 'refund_status' => 0, 'pid' => 0])
  419. ->when($time['start'] != 0 && $time['stop'] != 0, function ($query) use ($time) {
  420. $query->where('add_time', '>=', $time['start'])->where('add_time', '<', $time['stop']);
  421. })
  422. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  423. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  424. }
  425. /**
  426. * 获取当前时间到指定时间的支付金额 管理员
  427. * @param $start 开始时间
  428. * @param $stop 结束时间
  429. * @return mixed
  430. */
  431. public function chartTimePrice($start, $stop)
  432. {
  433. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  434. ->where('add_time', '>=', $start)
  435. ->where('add_time', '<', $stop)
  436. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  437. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  438. ->order('add_time ASC')->select()->toArray();
  439. }
  440. /**
  441. * 获取当前时间到指定时间的支付订单数 管理员
  442. * @param $start 开始时间
  443. * @param $stop 结束时间
  444. * @return mixed
  445. */
  446. public function chartTimeNumber($start, $stop)
  447. {
  448. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  449. ->where('add_time', '>=', $start)
  450. ->where('add_time', '<', $stop)
  451. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  452. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  453. ->order('add_time ASC')->select()->toArray();
  454. }
  455. /**
  456. * 获取用户已购买此活动商品的个数
  457. * @param $uid
  458. * @param $type
  459. * @param $typeId
  460. * @return int
  461. */
  462. public function getBuyCount($uid, $type, $typeId): int
  463. {
  464. return $this->getModel()
  465. ->where('uid', $uid)
  466. ->where($type, $typeId)
  467. ->where(function ($query) {
  468. $query->where('paid', 1)->whereOr(function ($query1) {
  469. $query1->where('paid', 0)->where('is_del', 0);
  470. });
  471. })->value('sum(total_num)') ?? 0;
  472. }
  473. /**
  474. * 获取没有支付的订单列表
  475. * @param array|string[] $field
  476. * @return array
  477. * @throws \think\db\exception\DataNotFoundException
  478. * @throws \think\db\exception\DbException
  479. * @throws \think\db\exception\ModelNotFoundException
  480. */
  481. public function getOrderUnPaidList(array $field = ['*'])
  482. {
  483. return $this->getModel()->where(['paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  484. ->where('pay_type', '<>', 'offline')->field($field)->select();
  485. }
  486. /** 根据时间获取营业额
  487. * @param array $where
  488. * @return float|int
  489. */
  490. public function getOrderMoneyByTime(array $where)
  491. {
  492. if (isset($where['day'])) {
  493. return $this->getModel()->where(['refund_status' => 0, 'paid' => 1])->whereDay('add_time', date("Y-m-d", strtotime($where['day'])))->sum('pay_price');
  494. }
  495. return 0;
  496. }
  497. /**
  498. * 用户趋势数据
  499. * @param $time
  500. * @param $type
  501. * @param $timeType
  502. * @return mixed
  503. */
  504. public function getTrendData($time, $type, $timeType, $str)
  505. {
  506. return $this->getModel()->when($type != '', function ($query) use ($type) {
  507. $query->where('channel_type', $type);
  508. })->where(function ($query) use ($time) {
  509. if ($time[0] == $time[1]) {
  510. $query->whereDay('pay_time', $time[0]);
  511. } else {
  512. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  513. $query->whereTime('pay_time', 'between', $time);
  514. }
  515. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  516. ->group('days')->select()->toArray();
  517. }
  518. /**
  519. * 用户地域数据
  520. * @param $time
  521. * @param $userType
  522. * @return mixed
  523. */
  524. public function getRegion($time, $userType)
  525. {
  526. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  527. $query->where('channel_type', $userType);
  528. })->where(function ($query) use ($time) {
  529. if ($time[0] == $time[1]) {
  530. $query->whereDay('pay_time', $time[0]);
  531. } else {
  532. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  533. $query->whereTime('pay_time', 'between', $time);
  534. }
  535. })->field('sum(pay_price) as payPrice,province')
  536. ->group('province')->select()->toArray();
  537. }
  538. /**
  539. * 商品趋势
  540. * @param $time
  541. * @param $timeType
  542. * @param $field
  543. * @param $str
  544. * @return mixed
  545. */
  546. public function getProductTrend($time, $timeType, $field, $str)
  547. {
  548. return $this->getModel()->where(function ($query) use ($field) {
  549. if ($field == 'pay_time') {
  550. $query->where('paid', 1);
  551. } elseif ($field == 'refund_reason_time') {
  552. $query->where('paid', 1)->where('refund_status', '>', 0);
  553. }
  554. })->where(function ($query) use ($time, $field) {
  555. if ($time[0] == $time[1]) {
  556. $query->whereDay($field, $time[0]);
  557. } else {
  558. $query->whereTime($field, 'between', $time);
  559. }
  560. })->whereIn('pid', [-1, 0])->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  561. }
  562. /** 按照支付时间统计支付金额
  563. * @param array $where
  564. * @param string $sumField
  565. * @return mixed
  566. */
  567. public function getDayTotalMoney(array $where, string $sumField)
  568. {
  569. return $this->search($where)
  570. ->when(isset($where['timeKey']), function ($query) use ($where) {
  571. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  572. })
  573. ->sum($sumField);
  574. }
  575. /**时间段订单数统计
  576. * @param array $where
  577. * @param string $countField
  578. * @return int
  579. */
  580. public function getDayOrderCount(array $where, string $countField = "*")
  581. {
  582. return $this->search($where)
  583. ->when(isset($where['timeKey']), function ($query) use ($where) {
  584. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  585. })
  586. ->count($countField);
  587. }
  588. /** 时间分组订单付款金额统计
  589. * @param array $where
  590. * @param string $sumField
  591. * @return mixed
  592. */
  593. public function getDayGroupMoney(array $where, string $sumField, string $group)
  594. {
  595. return $this->search($where)
  596. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  597. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  598. if ($where['timeKey']['days'] == 1) {
  599. $timeUinx = "%H";
  600. } elseif ($where['timeKey']['days'] == 30) {
  601. $timeUinx = "%Y-%m-%d";
  602. } elseif ($where['timeKey']['days'] == 365) {
  603. $timeUinx = "%Y-%m";
  604. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  605. $timeUinx = "%Y-%m-%d";
  606. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  607. $timeUinx = "%Y-%m";
  608. }
  609. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  610. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  611. })
  612. ->order('pay_time ASC')->select()->toArray();
  613. }
  614. /**时间分组订单数统计
  615. * @param array $where
  616. * @param string $sumField
  617. * @return mixed
  618. */
  619. public function getOrderGroupCount(array $where, string $sumField = "*")
  620. {
  621. return $this->search($where)
  622. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  623. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  624. if ($where['timeKey']['days'] == 1) {
  625. $timeUinx = "%H";
  626. } elseif ($where['timeKey']['days'] == 30) {
  627. $timeUinx = "%Y-%m-%d";
  628. } elseif ($where['timeKey']['days'] == 365) {
  629. $timeUinx = "%Y-%m";
  630. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  631. $timeUinx = "%Y-%m-%d";
  632. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  633. $timeUinx = "%Y-%m";
  634. }
  635. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  636. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  637. })
  638. ->order('pay_time ASC')->select()->toArray();
  639. }
  640. /**时间段支付订单人数
  641. * @param $where
  642. * @return mixed
  643. */
  644. public function getPayOrderPeople($where)
  645. {
  646. return $this->search($where)
  647. ->when(isset($where['timeKey']), function ($query) use ($where) {
  648. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  649. })
  650. ->field('uid')
  651. ->distinct(true)
  652. ->select()->toArray();
  653. }
  654. /**时间段分组统计支付订单人数
  655. * @param $where
  656. * @return mixed
  657. */
  658. public function getPayOrderGroupPeople($where)
  659. {
  660. return $this->search($where)
  661. ->when(isset($where['timeKey']), function ($query) use ($where) {
  662. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  663. if ($where['timeKey']['days'] == 1) {
  664. $timeUinx = "%H";
  665. } elseif ($where['timeKey']['days'] == 30) {
  666. $timeUinx = "%Y-%m-%d";
  667. } elseif ($where['timeKey']['days'] == 365) {
  668. $timeUinx = "%Y-%m";
  669. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  670. $timeUinx = "%Y-%m-%d";
  671. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  672. $timeUinx = "%Y-%m";
  673. }
  674. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  675. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  676. })
  677. ->order('pay_time ASC')->select()->toArray();
  678. }
  679. /**获取批量打印电子面单数据
  680. * @param array $where
  681. * @param string $filed
  682. * @return array
  683. * @throws \think\db\exception\DataNotFoundException
  684. * @throws \think\db\exception\DbException
  685. * @throws \think\db\exception\ModelNotFoundException
  686. */
  687. public function getOrderDumpData(array $where, $filed = "*")
  688. {
  689. $where['status'] = 1;
  690. $where['refund_status'] = 0;
  691. $where['paid'] = 1;
  692. $where['is_del'] = 0;
  693. $where['shipping_type'] = 1;
  694. $where['is_system_del'] = 0;
  695. return $this->search($where)->field($filed)->select()->toArray();
  696. }
  697. /**
  698. * @param array $where
  699. * @param string $field
  700. * @return array
  701. * @throws \think\db\exception\DataNotFoundException
  702. * @throws \think\db\exception\DbException
  703. * @throws \think\db\exception\ModelNotFoundException
  704. */
  705. public function getOrderListByWhere(array $where, $field = "*")
  706. {
  707. return $this->search($where)->field()->select($field)->toArray();
  708. }
  709. /**批量修改订单
  710. * @param array $ids
  711. * @param array $data
  712. * @param string|null $key
  713. * @return \crmeb\basic\BaseModel
  714. */
  715. public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
  716. {
  717. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  718. }
  719. /**根据orderid校验符合状态的发货数据
  720. * @param $order_ids
  721. * @return array|\crmeb\basic\BaseModel
  722. * @throws \think\db\exception\DataNotFoundException
  723. * @throws \think\db\exception\DbException
  724. * @throws \think\db\exception\ModelNotFoundException
  725. */
  726. public function getCanDevlieryOrder($key, $value)
  727. {
  728. $model = $this->getModel();
  729. if (is_array($value)) {
  730. $model = $model->whereIn($key, $value);
  731. } else {
  732. $model = $model->where($key, $value);
  733. }
  734. $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();
  735. return $model;
  736. }
  737. /**
  738. * 查询退款订单
  739. * @param $where
  740. * @param $page
  741. * @param $limit
  742. * @return array
  743. * @throws \think\db\exception\DataNotFoundException
  744. * @throws \think\db\exception\DbException
  745. * @throws \think\db\exception\ModelNotFoundException
  746. */
  747. public function getRefundList($where, $page = 0, $limit = 0)
  748. {
  749. $model = $this->getModel()
  750. ->where('paid', 1)->where('is_system_del', 0)
  751. ->when($where['refund_type'] == 0, function ($query) use ($where) {
  752. $query->where('refund_type', '>', 0);
  753. })
  754. ->when($where['order_id'] != '', function ($query) use ($where) {
  755. $query->where('order_id', $where['order_id']);
  756. })
  757. ->when($where['refund_type'], function ($query) use ($where) {
  758. $query->where('refund_type', $where['refund_type']);
  759. })
  760. ->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
  761. $query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
  762. })
  763. ->with(array_merge(['user', 'spread']));
  764. $count = $model->count();
  765. $list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  766. $query->page($page, $limit);
  767. })->order('refund_reason_time desc')->select()->toArray();
  768. return compact('list', 'count');
  769. }
  770. }