StoreIntegralOrderDao.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @return int
  84. */
  85. public function count(array $where = []): int
  86. {
  87. return $this->search($where)->count();
  88. }
  89. /**
  90. * 查找指定条件下的订单数据以数组形式返回
  91. * @param array $where
  92. * @param string $field
  93. * @param string $key
  94. * @param string $group
  95. * @return array
  96. */
  97. public function column(array $where, string $field, string $key = '', string $group = '')
  98. {
  99. return $this->search($where)->when($group, function ($query) use ($group) {
  100. $query->group($group);
  101. })->column($field, $key);
  102. }
  103. /**
  104. * 获取订单详情
  105. * @param $uid
  106. * @param $key
  107. * @return array|\think\Model|null
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function getUserOrderDetail(string $key, int $uid)
  113. {
  114. return $this->getOne(['order_id' => $key, 'uid' => $uid, 'is_del' => 0]);
  115. }
  116. /**
  117. * 获取用户已购买此活动商品的个数
  118. * @param $uid
  119. * @param $productId
  120. * @return int
  121. */
  122. public function getBuyCount($uid, $productId): int
  123. {
  124. return $this->getModel()
  125. ->where('uid', $uid)
  126. ->where('is_del', 0)
  127. ->where('product_id', $productId)
  128. ->value('sum(total_num)') ?? 0;
  129. }
  130. }