ArticleDao.php 4.6 KB

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