StoreIntegralOrderDao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. * @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, false)->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($realName && $fieldKey && in_array($fieldKey, $this->withField), function ($query) use ($where, $realName, $fieldKey) {
  49. if ($fieldKey !== 'store_name') {
  50. $query->where(trim($fieldKey), trim($realName));
  51. } else {
  52. $query->whereLike('store_name', '%' . $realName . '%');
  53. }
  54. })->when($realName && !$fieldKey, function ($query) use ($where) {
  55. $query->where(function ($que) use ($where) {
  56. $que->whereLike('order_id|real_name|store_name', '%' . $where['real_name'] . '%')->whereOr('uid', 'in', function ($q) use ($where) {
  57. $q->name('user')->whereLike('nickname|uid|phone', '%' . $where['real_name'] . '%')->field(['uid'])->select();
  58. });
  59. });
  60. });
  61. }
  62. /**
  63. * 订单搜索列表
  64. * @param array $where
  65. * @param array $field
  66. * @param int $page
  67. * @param int $limit
  68. * @param array $with
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function getOrderList(array $where, array $field, int $page, int $limit, array $with = [])
  75. {
  76. return $this->search($where)->field($field)->with(array_merge(['user'], $with))->page($page, $limit)->order('add_time DESC,id DESC')->select()->toArray();
  77. }
  78. /**
  79. * 获取订单总数
  80. * @param array $where
  81. * @return int
  82. */
  83. public function count(array $where = []): int
  84. {
  85. return $this->search($where)->count();
  86. }
  87. /**
  88. * 查找指定条件下的订单数据以数组形式返回
  89. * @param array $where
  90. * @param string $field
  91. * @param string $key
  92. * @param string $group
  93. * @return array
  94. */
  95. public function column(array $where, string $field, string $key = '', string $group = '')
  96. {
  97. return $this->search($where)->when($group, function ($query) use ($group) {
  98. $query->group($group);
  99. })->column($field, $key);
  100. }
  101. /**
  102. * 获取订单详情
  103. * @param $uid
  104. * @param $key
  105. * @return array|\think\Model|null
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\DbException
  108. * @throws \think\db\exception\ModelNotFoundException
  109. */
  110. public function getUserOrderDetail(string $key, int $uid)
  111. {
  112. return $this->getOne(['order_id' => $key, 'uid' => $uid, 'is_del' => 0]);
  113. }
  114. /**
  115. * 获取用户已购买此活动商品的个数
  116. * @param $uid
  117. * @param $productId
  118. * @return int
  119. */
  120. public function getBuyCount($uid, $productId): int
  121. {
  122. return $this->getModel()
  123. ->where('uid', $uid)
  124. ->where('is_del', 0)
  125. ->where('product_id', $productId)
  126. ->value('sum(total_num)') ?? 0;
  127. }
  128. }