StoreGroupOrderDao.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\store\order\StoreGroupOrder;
  14. /**
  15. * Class StoreGroupOrderDao
  16. * @package app\common\dao\store\order
  17. * @author xaboy
  18. * @day 2020/6/9
  19. */
  20. class StoreGroupOrderDao extends BaseDao
  21. {
  22. /**
  23. * @return string
  24. * @author xaboy
  25. * @day 2020/6/9
  26. */
  27. protected function getModel(): string
  28. {
  29. return StoreGroupOrder::class;
  30. }
  31. /**
  32. * 计算用户未支付订单的数量
  33. *
  34. * 本函数用于查询并返回指定用户(如果提供了UID)的未支付订单数量。
  35. * 未支付订单是指那些标记为未删除(is_del = 0)且尚未付款(paid = 0)的订单。
  36. *
  37. * @param int|null $uid 用户ID。如果为null,则查询所有用户的未支付订单数量;如果提供了具体的UID,则只查询该用户的未支付订单数量。
  38. * @return int 返回未支付订单的数量。注意,这个数量仅仅是未删除且未支付的订单数量。
  39. * @throws \think\db\exception\DbException
  40. */
  41. public function orderNumber($uid = null)
  42. {
  43. // 使用search方法查询满足条件(未删除、未支付)的订单,并统计数量
  44. return $this->search(['uid' => $uid,'is_del' => 0,'paid' => 0],0)->count();
  45. }
  46. /**
  47. * 根据条件搜索店铺分组订单
  48. *
  49. * 此函数用于根据提供的条件搜索店铺分组订单。它支持搜索已支付、用户ID、是否为积分订单、以及是否已删除的订单。
  50. * 通过条件构造查询语句,并根据这些条件来过滤订单数据。
  51. *
  52. * @param array $where 搜索条件数组,包含paid、uid、is_del等键值对
  53. * @param null $is_points 是否为积分订单,null表示不筛选,true/false分别表示是/否
  54. * @return \think\db\BaseQuery 查询构建器或StoreGroupOrder实例,用于进一步的查询操作或数据获取
  55. */
  56. public function search(array $where,$is_points = null)
  57. {
  58. // 获取数据库实例并根据条件逐步构建查询语句
  59. $query = StoreGroupOrder::getDB()
  60. // 当paid字段在$where数组中且不为空时,添加where条件筛选支付状态
  61. ->when(isset($where['paid']) && $where['paid'] !== '', function ($query) use ($where) {
  62. $query->where('paid', $where['paid']);
  63. })
  64. // 当uid字段在$where数组中且不为空时,添加where条件筛选用户ID
  65. ->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  66. $query->where('uid', $where['uid']);
  67. })
  68. // 当$is_points不为null时,根据$is_points的值添加where条件筛选是否为积分订单
  69. ->when(!is_null($is_points), function ($query) use ($is_points) {
  70. if ($is_points) {
  71. $query->where('activity_type', 20);
  72. } else {
  73. $query->where('activity_type', '<>',20);
  74. }
  75. })
  76. // 当is_del字段在$where数组中且不为空时,添加where条件筛选删除状态;否则,添加默认的where条件筛选未删除的订单
  77. ->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
  78. $query->where('is_del', $where['is_del']);
  79. }, function ($query) {
  80. $query->where('is_del', 0);
  81. });
  82. // 返回按照创建时间降序排序的查询构建器,用于进一步的查询操作或数据获取
  83. return $query->order('create_time DESC');
  84. }
  85. /**
  86. * 获取超时订单ID列表
  87. * 此函数用于查询并返回在指定时间之前创建的、未支付的订单ID列表。如果指定了需要提醒的订单,则只返回尚未提醒的订单ID。
  88. *
  89. * @param int $time 查询的截止时间,以时间戳形式表示。
  90. * @param bool $is_remind 是否查询需要提醒的订单,默认为false,表示不查询需要提醒的订单。
  91. * @return array 返回符合条件的订单ID列表。
  92. */
  93. public function getTimeOutIds($time, $is_remind = false)
  94. {
  95. // 根据条件构造查询语句,查询未删除、未支付的订单
  96. return StoreGroupOrder::getDB()->where('is_del', 0)->where('paid', 0)
  97. // 如果需要提醒,添加额外的查询条件,查询未提醒的订单
  98. ->when($is_remind, function ($query) {
  99. $query->where('is_remind', 0);
  100. })
  101. // 查询创建时间早于等于指定时间的订单
  102. ->where('create_time', '<=', $time)
  103. // 返回满足条件的订单的group_order_id列
  104. ->column('group_order_id');
  105. }
  106. /**
  107. * 设置团购订单的提醒状态
  108. *
  109. * 本函数用于将指定团购订单的提醒状态设置为已提醒(1)。通过传入订单ID,查询到该订单后进行状态更新。
  110. * 主要用于在用户需要时,对特定团购订单设置提醒标记,以便系统或用户知道该订单已经进行过提醒操作。
  111. *
  112. * @param int $id 团购订单的ID
  113. * @return bool 更新操作的结果,成功返回true,失败返回false
  114. * @throws \think\db\exception\DbException
  115. */
  116. public function isRemind($id)
  117. {
  118. // 使用StoreGroupOrder类的静态方法getDB来获取数据库操作对象,并根据group_order_id为$id更新is_remind字段为1
  119. return StoreGroupOrder::getDB()->where('group_order_id', $id)->update(['is_remind' => 1]);
  120. }
  121. /**
  122. * 计算用户当前未支付订单的总金额
  123. *
  124. * 本函数通过查询数据库中指定用户的未支付订单,计算出这些订单的总支付价格。
  125. * 未支付订单是指支付类型(pay_type)为0的订单。
  126. *
  127. * @param int $uid 用户ID
  128. * @return float|int 返回用户未支付订单的总金额,如果没有未支付订单则返回0。
  129. */
  130. public function totalNowMoney($uid)
  131. {
  132. // 使用StoreGroupOrder的数据库实例,并指定查询条件为支付类型为0且用户ID为$uid的订单,计算这些订单的支付价格总和
  133. return StoreGroupOrder::getDB()->where('pay_type', 0)->where('uid', $uid)->sum('pay_price') ?: 0;
  134. }
  135. }