StoreIntegralOrderDao.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 StoreIntegralOrderDao
  17. * @package app\dao\activity\integral
  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. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  37. */
  38. public function search(array $where = [])
  39. {
  40. $isDel = isset($where['is_del']) && $where['is_del'] !== '' && $where['is_del'] != -1;
  41. $realName = $where['real_name'] ?? '';
  42. $fieldKey = $where['field_key'] ?? '';
  43. $fieldKey = $fieldKey == 'all' ? '' : $fieldKey;
  44. return parent::search($where)->when($isDel, function ($query) use ($where) {
  45. $query->where('is_del', $where['is_del']);
  46. })->when(isset($where['is_system_del']), function ($query) {
  47. $query->where('is_system_del', 0);
  48. })->when(isset($where['paid']) && $where['paid'] == 1, function ($query) {
  49. $query->where('paid', 1);
  50. })->when(isset($where['is_price']) && $where['is_price'] == 1, function ($query) {
  51. $query->where('price', '>', 0);
  52. })->when(isset($where['is_integral']) && $where['is_integral'] == 1, function ($query) {
  53. $query->where('integral', '>', 0);
  54. })->when($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  55. if ($fieldKey !== 'store_name') {
  56. $query->where(trim($fieldKey), trim($realName));
  57. } else {
  58. $query->whereLike('store_name', '%' . $realName . '%');
  59. }
  60. })->when($realName && !$fieldKey, function ($query) use ($where) {
  61. $query->where(function ($que) use ($where) {
  62. $que->whereLike('order_id|real_name|store_name', '%' . $where['real_name'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  63. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  64. });
  65. });
  66. });
  67. }
  68. /**
  69. * 订单搜索列表
  70. * @param array $where
  71. * @param array $field
  72. * @param int $page
  73. * @param int $limit
  74. * @param array $with
  75. * @return array
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getOrderList(array $where, array $field, int $page, int $limit, array $with = [])
  81. {
  82. return $this->search($where)->field($field)->with(array_merge(['user'], $with))->page($page, $limit)->order('add_time DESC,id DESC')->select()->toArray();
  83. }
  84. /**
  85. * 根据条件获取订单列表
  86. * @param array $where
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function getIntegralOrderList(array $where)
  93. {
  94. return $this->search($where)->order('id asc')->select()->toArray();
  95. }
  96. /**
  97. * 获取订单总数
  98. * @param array $where
  99. * @return int
  100. */
  101. public function count(array $where = []): int
  102. {
  103. return $this->search($where)->count();
  104. }
  105. /**
  106. * 查找指定条件下的订单数据以数组形式返回
  107. * @param array $where
  108. * @param string $field
  109. * @param string $key
  110. * @param string $group
  111. * @return array
  112. */
  113. public function column(array $where, string $field, string $key = '', string $group = '')
  114. {
  115. return $this->search($where)->when($group, function ($query) use ($group) {
  116. $query->group($group);
  117. })->column($field, $key);
  118. }
  119. /**
  120. * 获取订单详情
  121. * @param $uid
  122. * @param $key
  123. * @return array|\think\Model|null
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\DbException
  126. * @throws \think\db\exception\ModelNotFoundException
  127. */
  128. public function getUserOrderDetail(string $key, int $uid)
  129. {
  130. return $this->getOne(['order_id' => $key, 'uid' => $uid, 'is_del' => 0]);
  131. }
  132. /**
  133. * 获取用户已购买此活动商品的个数
  134. * @param $uid
  135. * @param $productId
  136. * @return int
  137. */
  138. public function getBuyCount($uid, $productId): int
  139. {
  140. return $this->getModel()
  141. ->where('uid', $uid)
  142. ->where('is_del', 0)
  143. ->where('product_id', $productId)
  144. ->value('sum(total_num)') ?? 0;
  145. }
  146. }