StoreServiceDao.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\common\dao\store\service;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\service\StoreService;
  14. use think\db\BaseQuery;
  15. use think\db\exception\DataNotFoundException;
  16. use think\db\exception\DbException;
  17. use think\db\exception\ModelNotFoundException;
  18. use think\Model;
  19. /**
  20. * Class StoreServiceDao
  21. * @package app\common\dao\store\service
  22. * @author xaboy
  23. * @day 2020/5/29
  24. */
  25. class StoreServiceDao extends BaseDao
  26. {
  27. /**
  28. * @return string
  29. * @author xaboy
  30. * @day 2020/5/29
  31. */
  32. protected function getModel(): string
  33. {
  34. return StoreService::class;
  35. }
  36. /**
  37. * @param array $where
  38. * @return BaseQuery
  39. * @author xaboy
  40. * @day 2020/5/29
  41. */
  42. public function search(array $where)
  43. {
  44. return StoreService::getDB()->where('is_del', 0)->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  45. $query->where('status', $where['status']);
  46. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  47. $query->whereLike('nickname', "%{$where['keyword']}%");
  48. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  49. $query->whereLike('mer_id', $where['mer_id']);
  50. })->order('sort DESC');
  51. }
  52. public function getService($uid, $merId = null)
  53. {
  54. return StoreService::getDB()->where('uid', $uid)->when($merId, function ($query, $merId) {
  55. $query->where('mer_id', $merId);
  56. })->where('is_del', 0)->find();
  57. }
  58. /**
  59. * @param int $merId
  60. * @param int $id
  61. * @return bool
  62. * @author xaboy
  63. * @day 2020-05-13
  64. */
  65. public function merExists(int $merId, int $id)
  66. {
  67. return StoreService::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->where('is_del', 0)->count($this->getPk()) > 0;
  68. }
  69. /**
  70. * @param $merId
  71. * @param $uid
  72. * @param int|null $except
  73. * @return bool
  74. * @author xaboy
  75. * @day 2020/5/29
  76. */
  77. public function issetService($merId, $uid, ?int $except = null)
  78. {
  79. return StoreService::getDB()->where('uid', $uid)->when($except, function ($query, $except) {
  80. $query->where($this->getPk(), '<>', $except);
  81. })->where('mer_id', $merId)->where('is_del', 0)->count($this->getPk()) > 0;
  82. }
  83. /**
  84. * @param $uid
  85. * @param int|null $except
  86. * @return bool
  87. * @author xaboy
  88. * @day 2020/5/29
  89. */
  90. public function isBindService($uid, ?int $except = null)
  91. {
  92. return StoreService::getDB()->where('uid', $uid)->when($except, function ($query, $except) {
  93. $query->where($this->getPk(), '<>', $except);
  94. })->where('is_del', 0)->count($this->getPk()) > 0;
  95. }
  96. /**
  97. * @param int $id
  98. * @return int
  99. * @throws DbException
  100. * @author xaboy
  101. * @day 2020/5/29
  102. */
  103. public function delete(int $id)
  104. {
  105. return StoreService::getDB()->where($this->getPk(), $id)->update(['is_del' => 1]);
  106. }
  107. /**
  108. * @param $merId
  109. * @return array|Model|null
  110. * @throws DbException
  111. * @throws DataNotFoundException
  112. * @throws ModelNotFoundException
  113. * @author xaboy
  114. * @day 2020/5/29
  115. */
  116. public function getChatService($merId)
  117. {
  118. return StoreService::getDB()->where('mer_id', $merId)->where('is_del', 0)->where('status', 1)->order('status DESC, sort DESC, create_time ASC')
  119. ->hidden(['is_del'])->find();
  120. }
  121. public function getRandService($merId)
  122. {
  123. $services = StoreService::getDB()->where('mer_id', $merId)->where('is_del', 0)->where('status', 1)->order('status DESC, sort DESC, create_time ASC')
  124. ->hidden(['is_del'])->select();
  125. if (!$services || !count($services)) return null;
  126. if (count($services) === 1) $services[0];
  127. return $services[max(random_int(0, count($services) - 1), 0)];
  128. }
  129. /**
  130. * @param $id
  131. * @return array|Model|null
  132. * @throws DataNotFoundException
  133. * @throws DbException
  134. * @throws ModelNotFoundException
  135. * @author xaboy
  136. * @day 2020/5/29
  137. */
  138. public function getValidServiceInfo($id)
  139. {
  140. return StoreService::getDB()->where('service_id', $id)->where('status', 1)->where('is_del', 0)->hidden(['is_del'])->find();
  141. }
  142. /**
  143. * @param $merId
  144. * @return array
  145. * @author xaboy
  146. * @day 2020/7/1
  147. */
  148. public function getNoticeServiceInfo($merId)
  149. {
  150. return StoreService::getDB()->where('mer_id', $merId)->where('status', 1)->where('notify', 1)
  151. ->where('is_del', 0)->column('uid,phone,nickname');
  152. }
  153. }