StoreOrderDao.php 37 KB

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