StoreOrderDao.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  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)->where('is_cancel', 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 $time
  443. * @return float
  444. */
  445. public function todayLbSales($time)
  446. {
  447. return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time', 'pid' => 0,'is_lb'=>1])->sum('pay_price');
  448. } /**
  449. * 礼包订单总销售额
  450. * @return float
  451. */
  452. public function totalLbSales($time)
  453. {
  454. // return $this->search(['paid' => 1, 'refund_status' => 0, 'time' => $time ?: 'today', 'timekey' => 'pay_time', 'pid' => 0,'is_lb'=>1])->sum('pay_price');
  455. return $this->search(['paid' => 1, 'refund_status' => 0, 'timekey' => 'pay_time', 'pid' => 0,'is_lb'=>1])->sum('pay_price');
  456. }
  457. /**
  458. * 获取订单详情
  459. * @param string $key
  460. * @param int $uid
  461. * @param array $with
  462. * @return array|\think\Model|null
  463. * @throws \think\db\exception\DataNotFoundException
  464. * @throws \think\db\exception\DbException
  465. * @throws \think\db\exception\ModelNotFoundException
  466. */
  467. public function getUserOrderDetail(string $key, int $uid, $with = [])
  468. {
  469. $where = ['order_id|unique' => $key, 'is_del' => 0];
  470. if ($uid > 0) $where = $where + ['uid|gift_uid' => $uid];
  471. return $this->getOne($where, '*', $with);
  472. }
  473. /**
  474. * 获取用户推广订单
  475. * @param array $where
  476. * @param string $field
  477. * @param int $page
  478. * @param int $limit
  479. * @param array $with
  480. * @return array
  481. * @throws \think\db\exception\DataNotFoundException
  482. * @throws \think\db\exception\DbException
  483. * @throws \think\db\exception\ModelNotFoundException
  484. */
  485. public function getStairOrderList(array $where, string $field, int $page, int $limit, array $with = [])
  486. {
  487. return $this->search($where)->with($with)->field($field)->page($page, $limit)->order('id DESC')->select()->toArray();
  488. }
  489. /**
  490. * 订单每月统计数据
  491. * @param int $page
  492. * @param int $limit
  493. * @return array
  494. */
  495. public function getOrderDataPriceCount(array $where, array $field, int $page, int $limit)
  496. {
  497. return $this->search($where)
  498. ->field($field)->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  499. ->order('add_time DESC')->page($page, $limit)->select()->toArray();
  500. }
  501. /**
  502. * 获取当前时间到指定时间的支付金额 管理员
  503. * @param $start 开始时间
  504. * @param $stop 结束时间
  505. * @return mixed
  506. */
  507. public function chartTimePrice($start, $stop)
  508. {
  509. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
  510. ->where('add_time', '>=', $start)
  511. ->where('add_time', '<', $stop)
  512. ->field('sum(pay_price) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  513. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  514. ->order('add_time ASC')->select()->toArray();
  515. }
  516. /**
  517. * 获取当前时间到指定时间的支付订单数 管理员
  518. * @param $start 开始时间
  519. * @param $stop 结束时间
  520. * @return mixed
  521. */
  522. public function chartTimeNumber($start, $stop)
  523. {
  524. return $this->search(['pid' => 0, 'is_del' => 0, 'paid' => 1, 'refund_status' => [0, 3]])
  525. ->where('add_time', '>=', $start)
  526. ->where('add_time', '<', $stop)
  527. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m-%d\') as time')
  528. ->group("FROM_UNIXTIME(add_time, '%Y-%m-%d')")
  529. ->order('add_time ASC')->select()->toArray();
  530. }
  531. /**
  532. * 获取用户已购买此活动商品的个数
  533. * @param $uid
  534. * @param $type
  535. * @param $typeId
  536. * @return int
  537. */
  538. public function getBuyCount($uid, $type, $typeId): int
  539. {
  540. return $this->getModel()
  541. ->where('uid', $uid)
  542. ->where($type, $typeId)
  543. ->where(function ($query) {
  544. $query->where('paid', 1)->whereOr(function ($query1) {
  545. $query1->where('paid', 0)->where('is_del', 0);
  546. });
  547. })->value('sum(total_num)') ?? 0;
  548. }
  549. /**
  550. * 获取没有支付的订单列表
  551. * @param array|string[] $field
  552. * @return array
  553. * @throws \think\db\exception\DataNotFoundException
  554. * @throws \think\db\exception\DbException
  555. * @throws \think\db\exception\ModelNotFoundException
  556. */
  557. public function getOrderUnPaidList(array $field = ['*'])
  558. {
  559. return $this->getModel()->where(['paid' => 0, 'is_cancel' => 0, 'is_del' => 0, 'status' => 0, 'refund_status' => 0])
  560. ->where('pay_type', '<>', 'offline')->field($field)->select();
  561. }
  562. /** 根据时间获取营业额
  563. * @param array $where
  564. * @return float|int
  565. */
  566. public function getOrderMoneyByTime(array $where)
  567. {
  568. if (isset($where['day'])) {
  569. return $this->getModel()->where(['refund_status' => 0, 'paid' => 1])->whereDay('add_time', date("Y-m-d", strtotime($where['day'])))->sum('pay_price');
  570. }
  571. return 0;
  572. }
  573. /**
  574. * 用户趋势数据
  575. * @param $time
  576. * @param $type
  577. * @param $timeType
  578. * @return mixed
  579. */
  580. public function getTrendData($time, $type, $timeType, $str)
  581. {
  582. return $this->getModel()->when($type != '', function ($query) use ($type) {
  583. $query->where('channel_type', $type);
  584. })->where(function ($query) use ($time) {
  585. if ($time[0] == $time[1]) {
  586. $query->whereDay('pay_time', $time[0]);
  587. } else {
  588. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  589. $query->whereTime('pay_time', 'between', $time);
  590. }
  591. })->field("FROM_UNIXTIME(pay_time,'$timeType') as days,$str as num")
  592. ->group('days')->select()->toArray();
  593. }
  594. /**
  595. * 用户地域数据
  596. * @param $time
  597. * @param $userType
  598. * @return mixed
  599. */
  600. public function getRegion($time, $userType)
  601. {
  602. return $this->getModel()->when($userType != '', function ($query) use ($userType) {
  603. $query->where('channel_type', $userType);
  604. })->where(function ($query) use ($time) {
  605. if ($time[0] == $time[1]) {
  606. $query->whereDay('pay_time', $time[0]);
  607. } else {
  608. // $time[1] = date('Y/m/d', strtotime($time[1]) + 86400);
  609. $query->whereTime('pay_time', 'between', $time);
  610. }
  611. })->field('sum(pay_price) as payPrice,province')
  612. ->group('province')->select()->toArray();
  613. }
  614. /**
  615. * 商品趋势
  616. * @param $time
  617. * @param $timeType
  618. * @param $field
  619. * @param $str
  620. * @return mixed
  621. */
  622. public function getProductTrend($time, $timeType, $field, $str, $orderStatus = '')
  623. {
  624. return $this->getModel()->where(function ($query) use ($field, $orderStatus) {
  625. if ($field == 'pay_time') {
  626. $query->where('paid', 1)->where('pid', '>=', 0);
  627. } elseif ($field == 'refund_reason_time') {
  628. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  629. } elseif ($field == 'add_time') {
  630. if ($orderStatus == 'pay') {
  631. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', 0);
  632. } elseif ($orderStatus == 'refund') {
  633. $query->where('paid', 1)->where('pid', '>=', 0)->where('refund_status', '>', 0);
  634. }
  635. }
  636. })->where(function ($query) use ($time, $field) {
  637. if ($time[0] == $time[1]) {
  638. $query->whereDay($field, $time[0]);
  639. } else {
  640. $query->whereTime($field, 'between', $time);
  641. }
  642. })->where('pid', '>=', 0)->field("FROM_UNIXTIME($field,'$timeType') as days,$str as num")->group('days')->select()->toArray();
  643. }
  644. /** 按照支付时间统计支付金额
  645. * @param array $where
  646. * @param string $sumField
  647. * @return mixed
  648. */
  649. public function getDayTotalMoney(array $where, string $sumField)
  650. {
  651. return $this->search($where)
  652. ->when(isset($where['timeKey']), function ($query) use ($where) {
  653. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  654. })
  655. ->sum($sumField);
  656. }
  657. /**时间段订单数统计
  658. * @param array $where
  659. * @param string $countField
  660. * @return int
  661. */
  662. public function getDayOrderCount(array $where, string $countField = "*")
  663. {
  664. return $this->search($where)
  665. ->when(isset($where['timeKey']), function ($query) use ($where) {
  666. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  667. })
  668. ->count($countField);
  669. }
  670. /** 时间分组订单付款金额统计
  671. * @param array $where
  672. * @param string $sumField
  673. * @return mixed
  674. */
  675. public function getDayGroupMoney(array $where, string $sumField, string $group)
  676. {
  677. return $this->search($where)
  678. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField, $group) {
  679. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  680. $timeUinx = "%H";
  681. if ($where['timeKey']['days'] == 1) {
  682. $timeUinx = "%H";
  683. } elseif ($where['timeKey']['days'] == 30) {
  684. $timeUinx = "%Y-%m-%d";
  685. } elseif ($where['timeKey']['days'] == 365) {
  686. $timeUinx = "%Y-%m";
  687. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  688. $timeUinx = "%Y-%m-%d";
  689. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  690. $timeUinx = "%Y-%m";
  691. }
  692. $query->field("sum($sumField) as number,FROM_UNIXTIME($group, '$timeUinx') as time");
  693. $query->group("FROM_UNIXTIME($group, '$timeUinx')");
  694. })
  695. ->order('pay_time ASC,id DESC')->select()->toArray();
  696. }
  697. /**时间分组订单数统计
  698. * @param array $where
  699. * @param string $sumField
  700. * @return mixed
  701. */
  702. public function getOrderGroupCount(array $where, string $sumField = "*")
  703. {
  704. return $this->search($where)
  705. ->when(isset($where['timeKey']), function ($query) use ($where, $sumField) {
  706. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  707. $timeUinx = "%H";
  708. if ($where['timeKey']['days'] == 1) {
  709. $timeUinx = "%H";
  710. } elseif ($where['timeKey']['days'] == 30) {
  711. $timeUinx = "%Y-%m-%d";
  712. } elseif ($where['timeKey']['days'] == 365) {
  713. $timeUinx = "%Y-%m";
  714. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  715. $timeUinx = "%Y-%m-%d";
  716. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  717. $timeUinx = "%Y-%m";
  718. }
  719. $query->field("count($sumField) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  720. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  721. })
  722. ->order('pay_time ASC,id DESC')->select()->toArray();
  723. }
  724. /**时间段支付订单人数
  725. * @param $where
  726. * @return mixed
  727. */
  728. public function getPayOrderPeople($where)
  729. {
  730. return $this->search($where)
  731. ->when(isset($where['timeKey']), function ($query) use ($where) {
  732. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  733. })
  734. ->field('uid')
  735. ->distinct(true)
  736. ->select()->toArray();
  737. }
  738. /**时间段分组统计支付订单人数
  739. * @param $where
  740. * @return mixed
  741. */
  742. public function getPayOrderGroupPeople($where)
  743. {
  744. return $this->search($where)
  745. ->when(isset($where['timeKey']), function ($query) use ($where) {
  746. $query->whereBetweenTime('pay_time', $where['timeKey']['start_time'], $where['timeKey']['end_time']);
  747. if ($where['timeKey']['days'] == 1) {
  748. $timeUinx = "%H";
  749. } elseif ($where['timeKey']['days'] == 30) {
  750. $timeUinx = "%Y-%m-%d";
  751. } elseif ($where['timeKey']['days'] == 365) {
  752. $timeUinx = "%Y-%m";
  753. } elseif ($where['timeKey']['days'] > 1 && $where['timeKey']['days'] < 30) {
  754. $timeUinx = "%Y-%m-%d";
  755. } elseif ($where['timeKey']['days'] > 30 && $where['timeKey']['days'] < 365) {
  756. $timeUinx = "%Y-%m";
  757. } else {
  758. $timeUinx = "%H";
  759. }
  760. $query->field("count(distinct uid) as number,FROM_UNIXTIME(pay_time, '$timeUinx') as time");
  761. $query->group("FROM_UNIXTIME(pay_time, '$timeUinx')");
  762. })
  763. ->order('pay_time ASC,id DESC')->select()->toArray();
  764. }
  765. /**获取批量打印电子面单数据
  766. * @param array $where
  767. * @param string $filed
  768. * @return array
  769. * @throws \think\db\exception\DataNotFoundException
  770. * @throws \think\db\exception\DbException
  771. * @throws \think\db\exception\ModelNotFoundException
  772. */
  773. public function getOrderDumpData(array $where, $filed = "*")
  774. {
  775. $where['status'] = 1;
  776. $where['refund_status'] = 0;
  777. $where['paid'] = 1;
  778. $where['is_del'] = 0;
  779. $where['shipping_type'] = 1;
  780. $where['is_system_del'] = 0;
  781. return $this->search($where)->field($filed)->select()->toArray();
  782. }
  783. /**
  784. * @param array $where
  785. * @param string $field
  786. * @return array
  787. * @throws \think\db\exception\DataNotFoundException
  788. * @throws \think\db\exception\DbException
  789. * @throws \think\db\exception\ModelNotFoundException
  790. */
  791. public function getOrderListByWhere(array $where, $field = "*")
  792. {
  793. return $this->search($where)->field($field)->select()->toArray();
  794. }
  795. /**批量修改订单
  796. * @param array $ids
  797. * @param array $data
  798. * @param string|null $key
  799. * @return \crmeb\basic\BaseModel
  800. */
  801. public function batchUpdateOrder(array $ids, array $data, ?string $key = null)
  802. {
  803. return $this->getModel()::whereIn(is_null($key) ? $this->getPk() : $key, $ids)->update($data);
  804. }
  805. /**根据orderid校验符合状态的发货数据
  806. * @param $order_ids
  807. * @return array|\crmeb\basic\BaseModel
  808. * @throws \think\db\exception\DataNotFoundException
  809. * @throws \think\db\exception\DbException
  810. * @throws \think\db\exception\ModelNotFoundException
  811. */
  812. public function getCanDevlieryOrder($key, $value)
  813. {
  814. $model = $this->getModel();
  815. if (is_array($value)) {
  816. $model = $model->whereIn($key, $value);
  817. } else {
  818. $model = $model->where($key, $value);
  819. }
  820. $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();
  821. return $model;
  822. }
  823. /**
  824. * 查询退款订单
  825. * @param $where
  826. * @param $page
  827. * @param $limit
  828. * @return array
  829. * @throws \think\db\exception\DataNotFoundException
  830. * @throws \think\db\exception\DbException
  831. * @throws \think\db\exception\ModelNotFoundException
  832. */
  833. public function getRefundList($where, $page = 0, $limit = 0)
  834. {
  835. $model = $this->getModel()
  836. ->where('paid', 1)->where('is_system_del', 0)
  837. ->when($where['refund_type'] == 0, function ($query) use ($where) {
  838. $query->where('refund_type', '>', 0);
  839. })
  840. ->when($where['order_id'] != '', function ($query) use ($where) {
  841. $query->where('order_id', $where['order_id']);
  842. })
  843. ->when($where['refund_type'], function ($query) use ($where) {
  844. $query->where('refund_type', $where['refund_type']);
  845. })
  846. ->when(is_array($where['refund_reason_time']), function ($query) use ($where) {
  847. $query->whereBetween('refund_reason_time', [strtotime($where['refund_reason_time'][0]), strtotime($where['refund_reason_time'][1]) + 86400]);
  848. })
  849. ->with(array_merge(['user', 'spread']));
  850. $count = $model->count();
  851. $list = $model->when($page != 0 && $limit != 0, function ($query) use ($page, $limit) {
  852. $query->page($page, $limit);
  853. })->order('refund_reason_time desc')->select()->toArray();
  854. return compact('list', 'count');
  855. }
  856. /**
  857. * 订单搜索列表
  858. * @param array $where
  859. * @param array $field
  860. * @param int $page
  861. * @param int $limit
  862. * @param array $with
  863. * @param string $order
  864. * @return array
  865. * @throws \think\db\exception\DataNotFoundException
  866. * @throws \think\db\exception\DbException
  867. * @throws \think\db\exception\ModelNotFoundException
  868. */
  869. public function getOutOrderList(array $where, array $field, int $page = 0, int $limit = 0, array $with = [], string $order = 'add_time DESC,id DESC'): array
  870. {
  871. return $this->search($where)->field($field)->with($with)->when($page && $limit, function ($query) use ($page, $limit) {
  872. $query->page($page, $limit);
  873. })->order($order)->select()->toArray();
  874. }
  875. /**
  876. * 秒杀参与人统计
  877. * @param $id
  878. * @param $keyword
  879. * @param int $page
  880. * @param int $limit
  881. * @return mixed
  882. */
  883. public function seckillPeople($id, $keyword, $page = 0, $limit = 0)
  884. {
  885. return $this->getModel()->where('paid', 1)->where('pid', '<>', -1)->whereIn('refund_type', [0, 3])->where('is_del', 0)
  886. ->when($id != 0, function ($query) use ($id) {
  887. $query->where('seckill_id', $id);
  888. })->when($keyword != '', function ($query) use ($keyword) {
  889. $query->where('real_name|uid|user_phone', 'like', '%' . $keyword . '%');
  890. })->where('paid', 1)->field([
  891. 'real_name',
  892. 'uid',
  893. 'user_phone',
  894. 'SUM(total_num) as goods_num',
  895. 'COUNT(id) as order_num',
  896. 'SUM(pay_price) as total_price',
  897. 'add_time'
  898. ])->group('uid')->order("add_time desc")->when($page && $limit, function ($query) use ($page, $limit) {
  899. $query->page($page, $limit);
  900. })->select()->toArray();
  901. }
  902. /**
  903. * 秒杀订单统计
  904. * @param $id
  905. * @param $where
  906. * @param int $page
  907. * @param int $limit
  908. * @return array
  909. * @throws \think\db\exception\DataNotFoundException
  910. * @throws \think\db\exception\DbException
  911. * @throws \think\db\exception\ModelNotFoundException
  912. */
  913. public function seckillOrder($id, $where, $page = 0, $limit = 0)
  914. {
  915. return $this->search($where)->where('seckill_id', $id)
  916. ->when($page && $limit, function ($query) use ($page, $limit) {
  917. $query->page($page, $limit);
  918. })->field(['order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->order('add_time desc')->select()->toArray();
  919. }
  920. /**
  921. * 秒杀订单统计总数
  922. * @param $id
  923. * @param $where
  924. * @return int
  925. * @throws \ReflectionException
  926. * @author: 吴汐
  927. * @email: 442384644@qq.com
  928. * @date: 2023/8/31
  929. */
  930. public function seckillCount($id, $where)
  931. {
  932. return $this->search($where)->where('seckill_id', $id)->count();
  933. }
  934. /**
  935. * 砍价订单统计
  936. * @param $id
  937. * @param $where
  938. * @param int $page
  939. * @param int $limit
  940. * @return array
  941. * @throws \think\db\exception\DataNotFoundException
  942. * @throws \think\db\exception\DbException
  943. * @throws \think\db\exception\ModelNotFoundException
  944. */
  945. public function bargainStatisticsOrder($id, $where, $page = 0, $limit = 0)
  946. {
  947. return $this->search($where)->where('bargain_id', $id)
  948. ->when($page && $limit, function ($query) use ($page, $limit) {
  949. $query->page($page, $limit);
  950. })->field(['uid', 'order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->order('add_time desc')->select()->toArray();
  951. }
  952. /**
  953. * 砍价订单统计数量
  954. * @param $id
  955. * @param $where
  956. * @return int
  957. * @throws \ReflectionException
  958. * @author: 吴汐
  959. * @email: 442384644@qq.com
  960. * @date: 2023/8/31
  961. */
  962. public function bargainStatisticsOrderCount($id, $where)
  963. {
  964. return $this->search($where)->where('bargain_id', $id)->count();
  965. }
  966. /**
  967. * 拼团订单统计
  968. * @param $id
  969. * @param $where
  970. * @param int $page
  971. * @param int $limit
  972. * @return array
  973. * @throws \think\db\exception\DataNotFoundException
  974. * @throws \think\db\exception\DbException
  975. * @throws \think\db\exception\ModelNotFoundException
  976. */
  977. public function combinationStatisticsOrder($id, $where, $page = 0, $limit = 0)
  978. {
  979. return $this->search($where)->where('combination_id', $id)->where('pid', '<>', -1)
  980. ->when($page && $limit, function ($query) use ($page, $limit) {
  981. $query->page($page, $limit);
  982. })->field(['uid', 'order_id', 'real_name', 'status', 'pay_price', 'total_num', 'add_time', 'pay_time', 'paid'])->order('add_time desc')->select()->toArray();
  983. }
  984. /**
  985. * 拼团订单统计数量
  986. * @param $id
  987. * @param $where
  988. * @param int $page
  989. * @param int $limit
  990. * @return int
  991. * @throws \ReflectionException
  992. * @author: 吴汐
  993. * @email: 442384644@qq.com
  994. * @date: 2023/8/31
  995. */
  996. public function combinationStatisticsCount($id, $where)
  997. {
  998. return $this->search($where)->where('combination_id', $id)->count();
  999. }
  1000. /**
  1001. * 查找待收货的子订单
  1002. * @param int $pid
  1003. * @return array
  1004. * @throws \think\db\exception\DataNotFoundException
  1005. * @throws \think\db\exception\DbException
  1006. * @throws \think\db\exception\ModelNotFoundException
  1007. * @author: 吴汐
  1008. * @email: 442384644@qq.com
  1009. * @date: 2023/8/31
  1010. */
  1011. public function getSubOrderNotSendList(int $pid)
  1012. {
  1013. return $this->getModel()->where('pid', $pid)->where('status', 1)->select()->toArray();
  1014. }
  1015. /**
  1016. * 判断订单是否全部发货
  1017. * @param int $pid
  1018. * @param int $order_id
  1019. * @return int
  1020. * @author: 吴汐
  1021. * @email: 442384644@qq.com
  1022. * @date: 2023/8/31
  1023. */
  1024. public function getSubOrderNotSend(int $pid, int $order_id)
  1025. {
  1026. return $this->getModel()->where('pid', $pid)->where('status', 0)->where('id', '<>', $order_id)->count();
  1027. }
  1028. /**
  1029. * 判断是否存在子未收货子订单
  1030. * @param int $pid
  1031. * @param int $order_id
  1032. * @return int
  1033. * @author: 吴汐
  1034. * @email: 442384644@qq.com
  1035. * @date: 2023/8/31
  1036. */
  1037. public function getSubOrderNotTake(int $pid, int $order_id)
  1038. {
  1039. return $this->getModel()->where('pid', $pid)->where('status', 1)->where('id', '<>', $order_id)->count();
  1040. }
  1041. /**
  1042. * 分销订单统计
  1043. * @param $field
  1044. * @param $time
  1045. * @param $page
  1046. * @param $limit
  1047. * @param $sort
  1048. * @param $order
  1049. * @return array
  1050. * @throws \think\db\exception\DataNotFoundException
  1051. * @throws \think\db\exception\DbException
  1052. * @throws \think\db\exception\ModelNotFoundException
  1053. * @author wuhaotian
  1054. * @email 442384644@qq.com
  1055. * @date 2025/4/8
  1056. */
  1057. public function divisionStatistics($field, $time, $page, $limit, $sort, $order)
  1058. {
  1059. $model = $this->getModel()
  1060. ->where('paid', 1)
  1061. ->where('pid', '>=', 0)
  1062. ->where('refund_status', 0)
  1063. ->where($field, '>', 0)->group($field)
  1064. ->when(!empty($time), function ($query) use ($time) {
  1065. $query->whereBetween('add_time', [strtotime($time[0]), strtotime($time[1] . ' 23:59:59')]);
  1066. });
  1067. $count = $model->count();
  1068. $orderStr = $sort == '' ? 'order_sum desc' : $sort . ' ' . $order;
  1069. $list = $model->field([
  1070. $field,
  1071. 'COUNT(*) AS order_sum',
  1072. 'SUM(pay_price) AS order_sum_price'
  1073. ])->order($orderStr)->page($page, $limit)->select()->toArray();
  1074. return compact('count', 'list');
  1075. }
  1076. }