StoreIntegralOrderDao.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\activity\integral;
  12. use app\dao\BaseDao;
  13. use app\model\activity\integral\StoreIntegralOrder;
  14. /**
  15. * 订单
  16. * Class StoreOrderDao
  17. * @package app\dao\order
  18. */
  19. class StoreIntegralOrderDao extends BaseDao
  20. {
  21. /**
  22. * 限制精确查询字段
  23. * @var string[]
  24. */
  25. protected $withField = ['uid', 'order_id', 'real_name', 'user_phone', 'store_name'];
  26. /**
  27. * @return string
  28. */
  29. protected function setModel(): string
  30. {
  31. return StoreIntegralOrder::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. return parent::search($where, $search)->when($isDel, function ($query) use ($where) {
  47. $query->where('is_del', $where['is_del']);
  48. })->when(isset($where['is_system_del']), function ($query) {
  49. $query->where('is_system_del', 0);
  50. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  51. if ($fieldKey !== 'store_name') {
  52. $query->where(trim($fieldKey), trim($realName));
  53. } else {
  54. $query->whereLike('store_name', '%' . $realName . '%');
  55. }
  56. })->when($realName && !$fieldKey, function ($query) use ($where) {
  57. $query->where(function ($que) use ($where) {
  58. $que->whereLike('order_id|real_name|store_name', '%' . $where['real_name'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  59. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  60. });
  61. });
  62. });
  63. }
  64. /**
  65. * 订单搜索列表
  66. * @param array $where
  67. * @param array $field
  68. * @param int $page
  69. * @param int $limit
  70. * @param array $with
  71. * @return array
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function getOrderList(array $where, array $field, int $page, int $limit, array $with = [])
  77. {
  78. return $this->search($where)->field($field)->with(array_merge(['user'], $with))->page($page, $limit)->order('add_time DESC,id DESC')->select()->toArray();
  79. }
  80. /**
  81. * 获取订单总数
  82. * @param array $where
  83. * @param bool $search
  84. * @return int
  85. * @throws \ReflectionException
  86. */
  87. public function count(array $where = [], bool $search = true)
  88. {
  89. return $this->search($where)->count();
  90. }
  91. /**
  92. * 查找指定条件下的订单数据以数组形式返回
  93. * @param array $where
  94. * @param string $field
  95. * @param string $key
  96. * @param string $group
  97. * @return array
  98. */
  99. public function column(array $where, string $field, string $key = '', string $group = '')
  100. {
  101. return $this->search($where)->when($group, function ($query) use ($group) {
  102. $query->group($group);
  103. })->column($field, $key);
  104. }
  105. /**
  106. * 获取订单详情
  107. * @param $uid
  108. * @param $key
  109. * @return array|\think\Model|null
  110. * @throws \think\db\exception\DataNotFoundException
  111. * @throws \think\db\exception\DbException
  112. * @throws \think\db\exception\ModelNotFoundException
  113. */
  114. public function getUserOrderDetail(string $key, int $uid)
  115. {
  116. return $this->getOne(['order_id' => $key, 'uid' => $uid, 'is_del' => 0]);
  117. }
  118. /**
  119. * 获取用户已购买此活动商品的个数
  120. * @param $uid
  121. * @param $productId
  122. * @return int
  123. */
  124. public function getBuyCount($uid, $productId): int
  125. {
  126. return $this->getModel()
  127. ->where('uid', $uid)
  128. ->where('is_del', 0)
  129. ->where('product_id', $productId)
  130. ->value('sum(total_num)') ?? 0;
  131. }
  132. }