ArticleDao.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\common\dao\article;
  3. use think\Collection;
  4. use think\db\BaseQuery;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. use think\facade\Db;
  9. use app\common\dao\BaseDao;
  10. use app\common\model\article\Article;
  11. use app\common\model\BaseModel;
  12. use think\Model;
  13. class ArticleDao extends BaseDao
  14. {
  15. /**
  16. * @return BaseModel
  17. * @author xaboy
  18. * @day 2020-03-30
  19. */
  20. protected function getModel(): string
  21. {
  22. return Article::class;
  23. }
  24. /**
  25. * @param int $mer_id
  26. * @return Collection
  27. * @throws DataNotFoundException
  28. * @throws DbException
  29. * @throws ModelNotFoundException
  30. */
  31. public function getAll($mer_id = 0)
  32. {
  33. return Article::getDB()->with('content')->where('mer_id', $mer_id)->select();
  34. }
  35. /**
  36. * 搜索列表
  37. * @param $merId
  38. * @param array $where
  39. * @return BaseQuery
  40. * @author Qinii
  41. */
  42. public function search($merId,array $where)
  43. {
  44. $query = Article::getDB();
  45. if (isset($where['cid']) && $where['cid'] !== '') $query->where('cid', (int)$where['cid']);
  46. if (isset($where['title']) && $where['title'] !== '') $query->whereLike('title', "%{$where['title']}%");
  47. if (isset($where['status']) && $where['status'] !== '') $query->where('status', $where['status']);
  48. if (isset($where['wechat_news_id']) && $where['wechat_news_id'] !== '') $query->where('wechat_news_id', $where['wechat_news_id']);
  49. $query->with(['content','articleCategory']);
  50. return $query->where('mer_id',$merId)->order('sort DESC,create_time DESC');
  51. }
  52. /**
  53. * 根据 字段名查询
  54. * @param int $merId
  55. * @param $field
  56. * @param $value
  57. * @param null $except
  58. * @return bool
  59. * @author Qinii
  60. */
  61. public function merFieldExists(int $merId, $field, $value, $except = null)
  62. {
  63. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  64. $query->where($field, '<>', $except);
  65. })->where('mer_id', $merId)->where('wechat_news_id',0)->where($field, $value)->count() > 0;
  66. }
  67. /**
  68. * 查询一条
  69. * @param int $merId
  70. * @param int $id
  71. * @return array|Model|null
  72. * @throws DataNotFoundException
  73. * @throws DbException
  74. * @throws ModelNotFoundException
  75. * @author Qinii
  76. *
  77. */
  78. public function get( $id, int $merId = 0)
  79. {
  80. return ($this->getModel())::getDB()->where('mer_id', $merId)->where('wechat_news_id',0)->with(['content','articleCategory'])->find($id);
  81. }
  82. /**
  83. * @param int $id
  84. * @param int $merId
  85. * @return int
  86. * @throws DbException
  87. * @author xaboy
  88. * @day 2020-04-20
  89. */
  90. public function delete(int $id, int $merId = 0)
  91. {
  92. $result = ($this->getModel())::getDB()->where('mer_id', $merId)
  93. ->where($this->getPk(),$id)
  94. ->with('content')
  95. ->find();
  96. return $result->together(['content'])->delete();
  97. }
  98. /**
  99. * 关联添加
  100. * @param array $data
  101. * @return BaseDao|Model|void
  102. * @author Qinii
  103. */
  104. public function create(array $data)
  105. {
  106. Db::transaction(function()use($data){
  107. $content = $data['content'];
  108. unset($data['content']);
  109. $article = $this->getModel()::create($data);
  110. $article->content()->save(['content' => $content]);
  111. });
  112. }
  113. /**
  114. * 关联更新
  115. * @param int $id
  116. * @param array $data
  117. * @return int|void
  118. * @author Qinii
  119. */
  120. public function update(int $id, array $data)
  121. {
  122. Db::transaction(function()use($id,$data){
  123. $content = $data['content'];
  124. unset($data['content']);
  125. $this->getModel()::where($this->getPk(),$id)->update($data);
  126. $article = $this->getModel()::find($id);
  127. $article->content->content = $content;
  128. $article->together(['content'])->save();
  129. });
  130. }
  131. /**
  132. * 根据字段获取 主键值
  133. * @param int $vale
  134. * @param null $field
  135. * @return array
  136. * @author Qinii
  137. */
  138. public function getKey(int $vale,$field = null)
  139. {
  140. return ($this->getModel())::getDB()->where($field,$vale)->column($this->getPk());
  141. }
  142. public function wechatNewIdByData($id)
  143. {
  144. return ($this->getModel())::getDB()->where('wechat_news_id', $id)->select();
  145. }
  146. }