StoreServiceReplyDao.php 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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\store\service;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\service\StoreServiceReply;
  14. /**
  15. * Class StoreServiceDao
  16. * @package app\common\dao\store\service
  17. * @author xaboy
  18. * @day 2020/5/29
  19. */
  20. class StoreServiceReplyDao extends BaseDao
  21. {
  22. /**
  23. * @return string
  24. * @author xaboy
  25. * @day 2020/5/29
  26. */
  27. protected function getModel(): string
  28. {
  29. return StoreServiceReply::class;
  30. }
  31. /**
  32. * 根据条件搜索商店服务回复信息。
  33. *
  34. * 该方法通过接收一个包含搜索条件的数组,动态地构造数据库查询语句,以筛选出符合条件的商店服务回复记录。
  35. * 支持的搜索条件包括:商家ID(mer_id)、关键词(keyword)和状态(status)。
  36. *
  37. * @param array $where 搜索条件数组,包含可能的字段:mer_id、keyword、status。
  38. * @return \Illuminate\Database\Query\Builder|StoreServiceReply 查询构造器或者StoreServiceReply实例。
  39. */
  40. public function search(array $where)
  41. {
  42. // 获取数据库查询构建器
  43. return StoreServiceReply::getDB()
  44. // 当'mer_id'字段存在且不为空时,添加where条件筛选商家ID
  45. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  46. $query->where('mer_id', $where['mer_id']);
  47. })
  48. // 当'keyword'字段存在且不为空时,添加like条件筛选关键词
  49. ->when(isset($where['keyword']) && $where['keyword'] !== '', function ($query) use ($where) {
  50. $query->whereLike('keyword', "%{$where['keyword']}%");
  51. })
  52. // 当'status'字段存在且不为空时,添加where条件筛选状态
  53. ->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  54. $query->where('status', $where['status']);
  55. });
  56. }
  57. /**
  58. * 根据有效数据查询关键词
  59. * 本函数旨在通过特定的关键词和商家ID,从数据库中检索有效的关键词信息。
  60. * 这里的“有效”指的是关键词处于激活状态(status=1)且属于指定的商家(mer_id)。
  61. *
  62. * @param string $key 关键词,用于搜索匹配。
  63. * @param int $merId 商家ID,用于限定搜索范围。
  64. * @return array|null 返回匹配条件的关键词数据数组,如果找不到则返回null。
  65. */
  66. public function keywordByValidData($key, $merId)
  67. {
  68. // 从StoreServiceReply的数据库实例中查询
  69. // 使用where函数构建复杂的查询条件,首先搜索包含关键词的关键字
  70. // 然后搜索关键词列表中包含该关键词的数据
  71. // 最后限定查询条件为状态为1且属于指定商家ID的数据
  72. return StoreServiceReply::getDB()->where(function ($query) use ($key) {
  73. // 搜索关键词字段中包含$key的数据
  74. $query->where('keyword', 'like', "%{$key}%")
  75. // 搜索关键词以逗号分隔的列表中包含$key的数据
  76. ->whereFieldRaw('CONCAT(\',\',`keyword`,\',\')', 'LIKE', '%,' . $key . ',%', 'OR');
  77. })->where('status', 1)->where('mer_id', $merId)->find();
  78. }
  79. }