StoreOrderDao.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\order;
  12. use app\dao\BaseDao;
  13. use app\model\order\StoreOrder;
  14. /**
  15. * 订单
  16. * Class StoreOrderDao
  17. * @package app\dao\order
  18. */
  19. class StoreOrderDao extends BaseDao
  20. {
  21. /**
  22. * 限制精确查询字段
  23. * @var string[]
  24. */
  25. protected $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'title'];
  26. /**
  27. * @return string
  28. */
  29. protected function setModel(): string
  30. {
  31. return StoreOrder::class;
  32. }
  33. /**
  34. * 订单搜索
  35. * @param array $where
  36. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  37. * @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, false)->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 $uid
  426. * @param $key
  427. * @return array|\think\Model|null
  428. * @throws \think\db\exception\DataNotFoundException
  429. * @throws \think\db\exception\DbException
  430. * @throws \think\db\exception\ModelNotFoundException
  431. */
  432. public function getUserOrderDetail(string $key, int $uid, $with = [])
  433. {
  434. return $this->getOne(['order_id|unique' => $key, 'uid' => $uid, 'is_del' => 0], '*', $with);
  435. }
  436. /**
  437. * 获取用户推广订单
  438. * @param array $where
  439. * @param string $field
  440. * @param int $page
  441. * @param int $limit
  442. * @param array $with
  443. * @return array
  444. * @throws \think\db\exception\DataNotFoundException
  445. * @throws \think\db\exception\DbException
  446. * @throws \think\db\exception\ModelNotFoundException
  447. */
  448. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  449. {
  450. return $this->search($where)->with($with)->field($field)->page($page, $limit)->order('id DESC')->select()->toArray();
  451. }
  452. /**
  453. * 订单每月统计数据
  454. * @param int $page
  455. * @param int $limit
  456. * @return array
  457. */
  458. public function getOrderDataPriceCount(array $where, array $field, int $page, int $limit)
  459. {
  460. return $this->search($where)
  461. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  462. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  463. }
  464. /**
  465. * 获取当前时间到指定时间的支付金额 管理员
  466. * @param $start 开始时间
  467. * @param $stop 结束时间
  468. * @return mixed
  469. */
  470. public function chartTimePrice($start, $stop)
  471. {
  472. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  473. ->where('add_time', '>=', $start)
  474. ->where('add_time', '<', $stop)
  475. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  476. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  477. ->order('add_time ASC')->select()->toArray();
  478. }
  479. /**
  480. * 获取当前时间到指定时间的支付订单数 管理员
  481. * @param $start 开始时间
  482. * @param $stop 结束时间
  483. * @return mixed
  484. */
  485. public function chartTimeNumber($start, $stop)
  486. {
  487. return $this->search(['is_del' => 0, 'paid' => 1, 'refund_status' => 0])
  488. ->where('add_time', '>=', $start)
  489. ->where('add_time', '<', $stop)
  490. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  491. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  492. ->order('add_time ASC')->select()->toArray();
  493. }
  494. /**
  495. * 获取用户已购买此活动商品的个数
  496. * @param $uid
  497. * @param $type
  498. * @param $typeId
  499. * @return int
  500. */
  501. public function getBuyCount($uid, $type, $typeId): int
  502. {
  503. return $this->getModel()
  504. ->where('uid', $uid)
  505. ->where($type, $typeId)
  506. ->where(function ($query) {
  507. $query->where('paid', 1)->whereOr(function ($query1) {
  508. $query1->where('paid', 0)->where('is_del', 0);
  509. });
  510. })->value('sum(total_num)') ?? 0;
  511. }
  512. /**
  513. * 获取没有支付的订单列表
  514. * @param array|string[] $field
  515. * @return array
  516. * @throws \think\db\exception\DataNotFoundException
  517. * @throws \think\db\exception\DbException
  518. * @throws \think\db\exception\ModelNotFoundException
  519. */
  520. public function getOrderUnPaidList(array $field = ['*'])
  521. {
  522. return $this->getModel()->where(['paid' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  523. ->where('pay_type', '<>', 'offline')->field($field)->select();
  524. }
  525. /** 根据时间获取营业额
  526. * @param array $where
  527. * @return float|int
  528. */
  529. public function getOrderMoneyByTime(array $where)
  530. {
  531. if (isset($where['day'])) {
  532. return $this->getModel()->where(['refund_status' => 0, 'paid' => 1])->whereDay('add_time', date("Y-m-d", strtotime($where['day'])))->sum('pay_price');
  533. }
  534. return 0;
  535. }
  536. /**
  537. * 用户趋势数据
  538. * @param $time
  539. * @param $type
  540. * @param $timeType
  541. * @return mixed
  542. */
  543. public function getTrendData($time, $type, $timeType, $str)
  544. {
  545. return $this->getModel()->when($type != '', function ($query) use ($type) {
  546. $query->where('channel_type', $type);
  547. })->where(function ($query) use ($time) {
  548. if ($time[0] == $time[1]) {
  549. $query->whereDay('pay_time', $time[0]);
  550. } else {
  551. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  552. $query->whereTime('pay_time', 'between', $time);
  553. }
  554. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  555. ->group('days')->select()->toArray();
  556. }
  557. /**
  558. * 用户地域数据
  559. * @param $time
  560. * @param $userType
  561. * @return mixed
  562. */
  563. public function getRegion($time, $userType)
  564. {
  565. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  566. $query->where('channel_type', $userType);
  567. })->where(function ($query) use ($time) {
  568. if ($time[0] == $time[1]) {
  569. $query->whereDay('pay_time', $time[0]);
  570. } else {
  571. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  572. $query->whereTime('pay_time', 'between', $time);
  573. }
  574. })->field('sum(pay_price) as payPrice,province')
  575. ->group('province')->select()->toArray();
  576. }
  577. /**
  578. * 商品趋势
  579. * @param $time
  580. * @param $timeType
  581. * @param $field
  582. * @param $str
  583. * @return mixed
  584. */
  585. public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '')
  586. {
  587. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  588. if ($field == 'pay_time') {
  589. $query->where('paid', 1)->where('pid', '>=', 0);
  590. } elseif ($field == 'refund_reason_time') {
  591. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  592. } elseif ($field == 'add_time') {
  593. if ($orderStatus == 'pay') {
  594. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', 0);
  595. } elseif ($orderStatus == 'refund') {
  596. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  597. }
  598. }
  599. })->where(function ($query) use ($time, $field) {
  600. if ($time[0] == $time[1]) {
  601. $query->whereDay($field, $time[0]);
  602. } else {
  603. $query->whereTime($field, 'between', $time);
  604. }
  605. })->where('pid', '>=', 0)->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  606. }
  607. /** 按照支付时间统计支付金额
  608. * @param array $where
  609. * @param string $sumField
  610. * @return mixed
  611. */
  612. public function getDayTotalMoney(array $where, string $sumField)
  613. {
  614. return $this->search($where)
  615. ->when(isset($where['timeKey']), function ($query) use ($where) {
  616. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  617. })
  618. ->sum($sumField);
  619. }
  620. /**时间段订单数统计
  621. * @param array $where
  622. * @param string $countField
  623. * @return int
  624. */
  625. public function getDayOrderCount(array $where, string $countField = "*")
  626. {
  627. return $this->search($where)
  628. ->when(isset($where['timeKey']), function ($query) use ($where) {
  629. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  630. })
  631. ->count($countField);
  632. }
  633. /** 时间分组订单付款金额统计
  634. * @param array $where
  635. * @param string $sumField
  636. * @return mixed
  637. */
  638. public function getDayGroupMoney(array $where, string $sumField, string $group)
  639. {
  640. return $this->search($where)
  641. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  642. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  643. $timeUinx = "%H";
  644. if ($where['timeKey']['days'] == 1) {
  645. $timeUinx = "%H";
  646. } elseif ($where['timeKey']['days'] == 30) {
  647. $timeUinx = "%Y-%m-%d";
  648. } elseif ($where['timeKey']['days'] == 365) {
  649. $timeUinx = "%Y-%m";
  650. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  651. $timeUinx = "%Y-%m-%d";
  652. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  653. $timeUinx = "%Y-%m";
  654. }
  655. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  656. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  657. })
  658. ->order('pay_time ASC')->select()->toArray();
  659. }
  660. /**时间分组订单数统计
  661. * @param array $where
  662. * @param string $sumField
  663. * @return mixed
  664. */
  665. public function getOrderGroupCount(array $where, string $sumField = "*")
  666. {
  667. return $this->search($where)
  668. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  669. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  670. $timeUinx = "%H";
  671. if ($where['timeKey']['days'] == 1) {
  672. $timeUinx = "%H";
  673. } elseif ($where['timeKey']['days'] == 30) {
  674. $timeUinx = "%Y-%m-%d";
  675. } elseif ($where['timeKey']['days'] == 365) {
  676. $timeUinx = "%Y-%m";
  677. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  678. $timeUinx = "%Y-%m-%d";
  679. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  680. $timeUinx = "%Y-%m";
  681. }
  682. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  683. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  684. })
  685. ->order('pay_time ASC')->select()->toArray();
  686. }
  687. /**时间段支付订单人数
  688. * @param $where
  689. * @return mixed
  690. */
  691. public function getPayOrderPeople($where)
  692. {
  693. return $this->search($where)
  694. ->when(isset($where['timeKey']), function ($query) use ($where) {
  695. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  696. })
  697. ->field('uid')
  698. ->distinct(true)
  699. ->select()->toArray();
  700. }
  701. /**时间段分组统计支付订单人数
  702. * @param $where
  703. * @return mixed
  704. */
  705. public function getPayOrderGroupPeople($where)
  706. {
  707. return $this->search($where)
  708. ->when(isset($where['timeKey']), function ($query) use ($where) {
  709. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  710. if ($where['timeKey']['days'] == 1) {
  711. $timeUinx = "%H";
  712. } elseif ($where['timeKey']['days'] == 30) {
  713. $timeUinx = "%Y-%m-%d";
  714. } elseif ($where['timeKey']['days'] == 365) {
  715. $timeUinx = "%Y-%m";
  716. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  717. $timeUinx = "%Y-%m-%d";
  718. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  719. $timeUinx = "%Y-%m";
  720. } else {
  721. $timeUinx = "%H";
  722. }
  723. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  724. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  725. })
  726. ->order('pay_time ASC')->select()->toArray();
  727. }
  728. /**获取批量打印电子面单数据
  729. * @param array $where
  730. * @param string $filed
  731. * @return array
  732. * @throws \think\db\exception\DataNotFoundException
  733. * @throws \think\db\exception\DbException
  734. * @throws \think\db\exception\ModelNotFoundException
  735. */
  736. public function getOrderDumpData(array $where, $filed = "*")
  737. {
  738. $where['status'] = 1;
  739. $where['refund_status'] = 0;
  740. $where['paid'] = 1;
  741. $where['is_del'] = 0;
  742. $where['shipping_type'] = 1;
  743. $where['is_system_del'] = 0;
  744. return $this->search($where)->field($filed)->select()->toArray();
  745. }
  746. /**
  747. * @param array $where
  748. * @param string $field
  749. * @return array
  750. * @throws \think\db\exception\DataNotFoundException
  751. * @throws \think\db\exception\DbException
  752. * @throws \think\db\exception\ModelNotFoundException
  753. */
  754. public function getOrderListByWhere(array $where, $field = "*")
  755. {
  756. return $this->search($where)->field($field)->select()->toArray();
  757. }
  758. /**批量修改订单
  759. * @param array $ids
  760. * @param array $data
  761. * @param string|null $key
  762. * @return \crmeb\basic\BaseModel
  763. */
  764. public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
  765. {
  766. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  767. }
  768. /**根据orderid校验符合状态的发货数据
  769. * @param $order_ids
  770. * @return array|\crmeb\basic\BaseModel
  771. * @throws \think\db\exception\DataNotFoundException
  772. * @throws \think\db\exception\DbException
  773. * @throws \think\db\exception\ModelNotFoundException
  774. */
  775. public function getCanDevlieryOrder($key, $value)
  776. {
  777. $model = $this->getModel();
  778. if (is_array($value)) {
  779. $model = $model->whereIn($key, $value);
  780. } else {
  781. $model = $model->where($key, $value);
  782. }
  783. $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();
  784. return $model;
  785. }
  786. /**
  787. * 查询退款订单
  788. * @param $where
  789. * @param $page
  790. * @param $limit
  791. * @return array
  792. * @throws \think\db\exception\DataNotFoundException
  793. * @throws \think\db\exception\DbException
  794. * @throws \think\db\exception\ModelNotFoundException
  795. */
  796. public function getRefundList($where, $page = 0, $limit = 0)
  797. {
  798. $model = $this->getModel()
  799. ->where('paid', 1)->where('is_system_del', 0)
  800. ->when($where['refund_type'] == 0, function ($query) use ($where) {
  801. $query->where('refund_type', '>', 0);
  802. })
  803. ->when($where['order_id'] != '', function ($query) use ($where) {
  804. $query->where('order_id', $where['order_id']);
  805. })
  806. ->when($where['refund_type'], function ($query) use ($where) {
  807. $query->where('refund_type', $where['refund_type']);
  808. })
  809. ->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
  810. $query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
  811. })
  812. ->with(array_merge(['user', 'spread']));
  813. $count = $model->count();
  814. $list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  815. $query->page($page, $limit);
  816. })->order('refund_reason_time desc')->select()->toArray();
  817. return compact('list', 'count');
  818. }
  819. /**
  820. * 订单搜索列表
  821. * @param array $where
  822. * @param array $field
  823. * @param int $page
  824. * @param int $limit
  825. * @param array $with
  826. * @param string $order
  827. * @return array
  828. * @throws \think\db\exception\DataNotFoundException
  829. * @throws \think\db\exception\DbException
  830. * @throws \think\db\exception\ModelNotFoundException
  831. */
  832. public function getOutOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC'): array
  833. {
  834. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  835. $query->page($page, $limit);
  836. })->order($order)->select()->toArray();
  837. }
  838. /**
  839. * 秒杀参与人统计
  840. * @param $id
  841. * @param $keyword
  842. * @param int $page
  843. * @param int $limit
  844. * @return mixed
  845. */
  846. public function seckillPeople($id, $keyword, $page = 0, $limit = 0)
  847. {
  848. return $this->getModel()
  849. ->when($id != 0, function ($query) use ($id) {
  850. $query->where('seckill_id', $id);
  851. })->when($keyword != '', function ($query) use ($keyword) {
  852. $query->where('real_name|uid|user_phone', 'like', '%' . $keyword . '%');
  853. })->where('paid', 1)->field([
  854. 'real_name',
  855. 'uid',
  856. 'SUM(total_num) as goods_num',
  857. 'COUNT(id) as order_num',
  858. 'SUM(pay_price) as total_price',
  859. 'add_time'
  860. ])->group('uid')->order("add_time desc")->when($page && $limit, function ($query) use ($page, $limit) {
  861. $query->page($page, $limit);
  862. })->select()->toArray();
  863. }
  864. /**
  865. * 秒杀订单统计
  866. * @param $id
  867. * @param $where
  868. * @param int $page
  869. * @param int $limit
  870. * @return array
  871. * @throws \think\db\exception\DataNotFoundException
  872. * @throws \think\db\exception\DbException
  873. * @throws \think\db\exception\ModelNotFoundException
  874. */
  875. public function seckillOrder($id, $where, $page = 0, $limit = 0)
  876. {
  877. return $this->search($where)->where('seckill_id', $id)
  878. ->when($page && $limit, function ($query) use ($page, $limit) {
  879. $query->page($page, $limit);
  880. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->select()->toArray();
  881. }
  882. /**
  883. * 砍价订单统计
  884. * @param $id
  885. * @param $where
  886. * @param int $page
  887. * @param int $limit
  888. * @return array
  889. * @throws \think\db\exception\DataNotFoundException
  890. * @throws \think\db\exception\DbException
  891. * @throws \think\db\exception\ModelNotFoundException
  892. */
  893. public function bargainStatisticsOrder($id, $where, $page = 0, $limit = 0)
  894. {
  895. return $this->search($where)->where('bargain_id', $id)
  896. ->when($page && $limit, function ($query) use ($page, $limit) {
  897. $query->page($page, $limit);
  898. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->select()->toArray();
  899. }
  900. /**
  901. * 拼团订单统计
  902. * @param $id
  903. * @param $where
  904. * @param int $page
  905. * @param int $limit
  906. * @return array
  907. * @throws \think\db\exception\DataNotFoundException
  908. * @throws \think\db\exception\DbException
  909. * @throws \think\db\exception\ModelNotFoundException
  910. */
  911. public function combinationStatisticsOrder($id, $where, $page = 0, $limit = 0)
  912. {
  913. return $this->search($where)->where('combination_id', $id)
  914. ->when($page && $limit, function ($query) use ($page, $limit) {
  915. $query->page($page, $limit);
  916. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->select()->toArray();
  917. }
  918. }