StoreOrderDao.php 32 KB

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