StoreOrderDao.php 37 KB

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