ArticleDao.php 5.5 KB

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