StoreOrderDao.php 40 KB

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