FeedbackDao.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\Feedback;
  15. use think\db\BaseQuery;
  16. /**
  17. * Class FeedbackDao
  18. * @package app\common\dao\user
  19. * @author xaboy
  20. * @day 2020/5/28
  21. */
  22. class FeedbackDao extends BaseDao
  23. {
  24. /**
  25. * @return string
  26. * @author xaboy
  27. * @day 2020/5/28
  28. */
  29. protected function getModel(): string
  30. {
  31. return Feedback::class;
  32. }
  33. /**
  34. * 根据条件搜索反馈信息
  35. *
  36. * 本函数用于根据提供的条件数组搜索反馈信息。它支持多种条件搜索,包括用户ID、关键词、类型、状态、姓名和删除状态等。
  37. * 搜索条件是可选的,只有当条件存在且不为空时,才会应用相应的查询条件。
  38. *
  39. * @param array $where 搜索条件数组,包含各种可能的搜索参数。
  40. * @return \think\Collection 返回搜索结果集合。
  41. */
  42. public function search(array $where)
  43. {
  44. // 从Feedback类中获取数据库实例
  45. return Feedback::getDB()->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  46. // 如果提供了用户ID,则按用户ID查询
  47. $query->where('uid', $where['uid']);
  48. })->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  49. // 如果提供了关键词,则按内容、回复、备注、真实姓名和联系方式模糊查询
  50. $query->whereLike('content|reply|remake|realname|contact', '%'.$where['keyword'].'%');
  51. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  52. // 如果提供了类型,则按类型查询
  53. $query->where('type',$where['type']);
  54. })->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  55. // 如果提供了状态,则按状态查询
  56. $query->where('status', $where['status']);
  57. })->when(isset($where['realname']) && $where['realname'] !== '', function ($query) use ($where) {
  58. // 如果提供了真实姓名,则按真实姓名模糊查询
  59. $query->where('realname','like', '%'.$where['realname'].'%');
  60. })->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
  61. // 如果提供了删除状态,则按删除状态查询
  62. $query->where('is_del',$where['is_del']);
  63. })->order('create_time DESC'); // 按创建时间降序排序
  64. }
  65. /**
  66. * 检查给定的ID和UID是否存在对应的反馈信息。
  67. *
  68. * 本函数通过查询数据库来确定是否存在一条反馈信息,其主键(ID)与给定的ID匹配,
  69. * 并且用户ID(uid)与给定的UID匹配,同时该反馈信息未被删除(is_del为0)。
  70. * 这对于确保操作的反馈信息是有效和可用的非常重要。
  71. *
  72. * @param int $id 主键ID,用于查询特定的反馈信息。
  73. * @param int $uid 用户ID,用于查询属于特定用户的反馈信息。
  74. * @return bool 如果存在匹配的反馈信息,则返回true;否则返回false。
  75. */
  76. public function uidExists($id, $uid): bool
  77. {
  78. // 使用Feedback类的静态方法getDB来获取数据库实例,并构建查询条件,
  79. // 查询条件包括主键ID、用户ID和删除状态,最后通过count方法统计符合条件的记录数,
  80. // 如果记录数大于0,则表示存在匹配的反馈信息。
  81. return Feedback::getDB()->where($this->getPk(), $id)->where('uid', $uid)->where('is_del', 0)->count($this->getPk()) > 0;
  82. }
  83. /**
  84. * 检查指定ID的商品是否存在且未被删除。
  85. *
  86. * 该方法通过查询数据库来确定给定ID的商品是否存在,同时确保该商品的删除标记(is_del)为0,即未被删除。
  87. * 这是对商品管理中常见操作的封装,用于在进行进一步的操作前验证商品的有效性。
  88. *
  89. * @param int $id 商品的唯一标识ID。
  90. * @return bool 如果商品存在且未被删除,返回true;否则返回false。
  91. */
  92. public function merExists(int $id)
  93. {
  94. // 使用模型获取数据库实例,并构造查询条件:主键为$id且is_del为0,然后统计符合条件的记录数。
  95. // 如果记录数大于0,说明商品存在且未被删除,返回true;否则返回false。
  96. return $this->getModel()::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count() > 0;
  97. }
  98. }