StoreServiceDao.php 5.1 KB

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