WechatNewsDao.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace app\common\dao\wechat;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\wechat\WechatNews;
  5. class WechatNewsDao extends BaseDao
  6. {
  7. protected function getModel(): string
  8. {
  9. return WechatNews::class;
  10. }
  11. /**
  12. * 根据主键查询
  13. * @param int $merId
  14. * @param int $id
  15. * @param null $except
  16. * @return bool
  17. * @author Qinii
  18. */
  19. public function merExists(int $merId, int $id, $except = null)
  20. {
  21. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  22. }
  23. /**
  24. * 根据 字段名查询
  25. * @param int $merId
  26. * @param $field
  27. * @param $value
  28. * @param null $except
  29. * @return bool
  30. * @author Qinii
  31. */
  32. public function merFieldExists(int $merId, $field, $value, $except = null)
  33. {
  34. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  35. $query->where($field, '<>', $except);
  36. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  37. }
  38. public function getAll(array $where)
  39. {
  40. if(isset($where['cate_name']) && $where['cate_name'] !== ''){
  41. $query = WechatNews::hasWhere('article',function ($query)use($where){
  42. $query->whereLike('title',"%{$where['cate_name']}%");
  43. });
  44. }else{
  45. $query = WechatNews::alias('WechatNews');
  46. }
  47. $query->with('article');
  48. return $query->order('WechatNews.create_time DESC');
  49. }
  50. public function get( $id, int $merId = 0)
  51. {
  52. return ($this->getModel())::getDB()->where('mer_id',$merId)->with('article.content')->find($id);
  53. }
  54. }