StoreOrderDao.php 38 KB

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