WechatNewsDao.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\wechat;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\wechat\WechatNews;
  14. class WechatNewsDao extends BaseDao
  15. {
  16. protected function getModel(): string
  17. {
  18. return WechatNews::class;
  19. }
  20. /**
  21. * 根据主键查询
  22. * @param int $merId
  23. * @param int $id
  24. * @param null $except
  25. * @return bool
  26. * @author Qinii
  27. */
  28. public function merExists(int $merId, int $id, $except = null)
  29. {
  30. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  31. }
  32. /**
  33. * 根据 字段名查询
  34. * @param int $merId
  35. * @param $field
  36. * @param $value
  37. * @param null $except
  38. * @return bool
  39. * @author Qinii
  40. */
  41. public function merFieldExists(int $merId, $field, $value, $except = null)
  42. {
  43. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  44. $query->where($field, '<>', $except);
  45. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  46. }
  47. /**
  48. * 根据条件获取所有的微信新闻信息。
  49. *
  50. * 此方法主要用于查询微信新闻及相关文章的信息。通过接收一个数组参数$where,
  51. * 来确定查询条件。特别地,如果数组中包含'cate_name'键,并且其值不为空,
  52. * 则会进一步根据 cate_name 来模糊查询文章标题。
  53. *
  54. * @param array $where 查询条件数组,可以包含任何用于筛选新闻的条件。
  55. * @return \Illuminate\Database\Eloquent\Builder|WechatNews 查询结果,包含微信新闻及其相关文章。
  56. */
  57. public function getAll(array $where)
  58. {
  59. // 当$where数组中包含'cate_name'且不为空时,进行模糊查询
  60. if(isset($where['cate_name']) && $where['cate_name'] !== ''){
  61. $query = WechatNews::hasWhere('article',function ($query)use($where){
  62. // 在文章中模糊搜索标题包含$cate_name的记录
  63. $query->whereLike('title',"%{$where['cate_name']}%");
  64. });
  65. }else{
  66. // 如果没有指定cate_name,则直接查询微信新闻表
  67. $query = WechatNews::alias('WechatNews');
  68. }
  69. // 带上相关文章信息
  70. $query->with('article');
  71. // 按照创建时间降序排序
  72. return $query->order('WechatNews.create_time DESC');
  73. }
  74. /**
  75. * 根据ID和商家ID获取数据
  76. *
  77. * 本函数用于从数据库中检索指定ID和商家ID对应的数据。它通过调用getModel方法获取数据模型实例,
  78. * 并使用该实例的getDB方法来访问数据库。查询时,会指定条件为mer_id等于传入的商家ID,
  79. * 并且通过with方法加载关联的article内容。最后,使用find方法根据指定的ID查找数据。
  80. *
  81. * @param int $id 需要获取的数据的ID
  82. * @param int $merId 商家ID,用于限定查询的数据范围,默认为0,表示不进行商家ID的限定
  83. * @return \think\Model|null 返回符合查询条件的数据模型对象,如果未找到数据则返回null
  84. */
  85. public function get( $id, int $merId = 0)
  86. {
  87. // 根据ID和商家ID查询数据,同时加载关联的article内容
  88. return ($this->getModel())::getDB()->where('mer_id',$merId)->with('article.content')->find($id);
  89. }
  90. }