StoreOrderDao.php 42 KB

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