StoreOrderStatusDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\order;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\order\StoreOrderStatus;
  15. use app\common\repositories\store\order\StoreOrderStatusRepository;
  16. /**
  17. * Class StoreOrderStatusDao
  18. * @package app\common\dao\store\order
  19. * @author xaboy
  20. * @day 2020/6/12
  21. */
  22. class StoreOrderStatusDao extends BaseDao
  23. {
  24. /**
  25. * @return string
  26. * @author xaboy
  27. * @day 2020/6/12
  28. */
  29. protected function getModel(): string
  30. {
  31. return StoreOrderStatus::class;
  32. }
  33. /**
  34. * 根据条件搜索数据。
  35. *
  36. * 本函数用于构建查询条件,根据传入的$where数组,动态地添加查询条件到数据库查询中。
  37. * 这样做的目的是为了提高代码的灵活性和可维护性,避免硬编码的查询条件,同时允许根据不同的条件进行数据检索。
  38. *
  39. * @param array $where 包含搜索条件的数组。数组的键是条件的字段名,值是条件的值。
  40. * 如果值为空字符串或者未设置,该条件将被忽略。
  41. * @return \Illuminate\Database\Query\Builder|static 返回构建好的查询对象,可以进一步调用其他查询方法,比如获取数据。
  42. */
  43. public function search($where)
  44. {
  45. // 获取数据库实例,并通过链式调用进行条件构建
  46. $query = ($this->getModel()::getDB())
  47. // 当'id'字段在$where数组中存在且不为空时,添加where条件查询订单号
  48. ->when(isset($where['id']) && $where['id'] !== '', function ($query) use ($where) {
  49. $query->where('order_id', $where['id']);
  50. })
  51. // 当'type'字段在$where数组中存在且不为空时,添加where条件查询类型
  52. ->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  53. $query->where('type', $where['type']);
  54. })
  55. // 当'user_type'字段在$where数组中存在且不为空时,添加where条件查询用户类型
  56. ->when(isset($where['user_type']) && $where['user_type'] !== '', function ($query) use ($where) {
  57. $query->where('user_type', $where['user_type']);
  58. })
  59. // 当'user_type'字段在$where数组中存在且不为空时,添加where条件查询用户类型
  60. ->when(isset($where['change_type']) && $where['change_type'] !== '', function ($query) use ($where) {
  61. is_array($where['change_type']) ? $query->whereIn('change_type', $where['change_type']) : $query->where('change_type', $where['change_type']);
  62. })
  63. // 当'date'字段在$where数组中存在且不为空时,调用getModelTime函数动态添加时间查询条件
  64. ->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
  65. getModelTime($query, $where['date'], 'change_time');
  66. });
  67. // 返回构建好的查询对象
  68. return $query;
  69. }
  70. /**
  71. * 获取超时未配送的订单ID列表
  72. *
  73. * 本函数通过查询订单状态数据库,筛选出在指定时间内,订单状态为配送中、待配送或已配送但未支付的订单ID。
  74. * 主要用于统计或处理超时未完成配送的订单。
  75. *
  76. * @param string $start 查询开始时间,格式为日期字符串
  77. * @param string $end 查询结束时间,格式为日期字符串
  78. * @return array 返回符合条件的订单ID列表
  79. */
  80. public function getTimeoutDeliveryOrder($start, $end)
  81. {
  82. // 使用数据库查询语言构造查询语句
  83. return StoreOrderStatus::getDB()->alias('A')->leftJoin('StoreOrder B', 'A.order_id = B.order_id')
  84. ->whereIn('A.change_type', [StoreOrderStatusRepository::ORDER_DELIVERY_SELF, StoreOrderStatusRepository::ORDER_DELIVERY_NOTHING, StoreOrderStatusRepository::ORDER_DELIVERY_COURIER])
  85. ->where('A.type', 'order')
  86. ->whereBetweenTime('A.change_time', $start, $end)
  87. ->where('B.paid', 1)->where('B.status', 1)
  88. ->column('A.order_id');
  89. }
  90. }