ArticleDao.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\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. * 获取全部文章
  35. * @param int $mer_id
  36. * @return Collection
  37. * @throws DataNotFoundException
  38. * @throws DbException
  39. * @throws ModelNotFoundException
  40. */
  41. public function getAll($mer_id = 0)
  42. {
  43. return Article::getDB()->with('content')->where('mer_id', $mer_id)->select();
  44. }
  45. /**
  46. * 搜索列表
  47. * @param $merId
  48. * @param array $where
  49. * @return BaseQuery
  50. * @author Qinii
  51. */
  52. public function search($merId,array $where)
  53. {
  54. $query = Article::getDB();
  55. if (isset($where['cid']) && $where['cid'] !== '') $query->where('cid', (int)$where['cid']);
  56. if (isset($where['title']) && $where['title'] !== '') $query->whereLike('title', "%{$where['title']}%");
  57. if (isset($where['status']) && $where['status'] !== '') $query->where('status', $where['status']);
  58. if (isset($where['wechat_news_id']) && $where['wechat_news_id'] !== '') $query->where('wechat_news_id', $where['wechat_news_id']);
  59. if (isset($where['article_id']) && $where['article_id'] !== ''){
  60. if (is_array($where['article_id'])) {
  61. $query->whereIn('article_id', $where['article_id']);
  62. } else {
  63. $query->where('article_id', $where['article_id']);
  64. }
  65. }
  66. $query->with(['content','articleCategory']);
  67. return $query->where('mer_id',$merId)->order('sort DESC,create_time DESC');
  68. }
  69. /**
  70. * 根据 字段名查询
  71. * @param int $merId
  72. * @param $field
  73. * @param $value
  74. * @param null $except
  75. * @return bool
  76. * @author Qinii
  77. */
  78. public function merFieldExists(int $merId, $field, $value, $except = null)
  79. {
  80. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  81. $query->where($field, '<>', $except);
  82. })->where('mer_id', $merId)->where('wechat_news_id',0)->where($field, $value)->count() > 0;
  83. }
  84. /**
  85. * @param int $id
  86. * @param int $merId
  87. * @return int
  88. * @throws DbException
  89. * @author xaboy
  90. * @day 2020-04-20
  91. */
  92. public function delete(int $id, int $merId = 0)
  93. {
  94. $result = ($this->getModel())::getDB()->where('mer_id', $merId)
  95. ->where($this->getPk(),$id)
  96. ->with('content')
  97. ->find();
  98. return $result->together(['content'])->delete();
  99. }
  100. /**
  101. * 关联添加
  102. * @param array $data
  103. * @return BaseDao|Model|void
  104. * @author Qinii
  105. */
  106. public function create(array $data)
  107. {
  108. Db::transaction(function()use($data){
  109. $content = $data['content'];
  110. unset($data['content']);
  111. $article = $this->getModel()::create($data);
  112. $article->content()->save(['content' => $content]);
  113. });
  114. }
  115. /**
  116. * 关联更新
  117. * @param int $id
  118. * @param array $data
  119. * @return int|void
  120. * @author Qinii
  121. */
  122. public function update(int $id, array $data)
  123. {
  124. Db::transaction(function()use($id,$data){
  125. $content = $data['content'];
  126. unset($data['content']);
  127. $this->getModel()::where($this->getPk(),$id)->update($data);
  128. $article = $this->getModel()::find($id);
  129. $article->content->content = $content;
  130. $article->together(['content'])->save();
  131. });
  132. }
  133. /**
  134. * 根据字段获取 主键值
  135. * @param int $vale
  136. * @param null $field
  137. * @return array
  138. * @author Qinii
  139. */
  140. public function getKey(int $vale,$field = null)
  141. {
  142. return ($this->getModel())::getDB()->where($field,$vale)->column($this->getPk());
  143. }
  144. /**
  145. * 查询微信图文消息
  146. * @param $id
  147. * @return array|Collection|BaseQuery[]
  148. * @throws DataNotFoundException
  149. * @throws DbException
  150. * @throws ModelNotFoundException
  151. * @author wuhaotian
  152. * @email 442384644@qq.com
  153. * @date 2024/7/12
  154. */
  155. public function wechatNewIdByData($id)
  156. {
  157. return ($this->getModel())::getDB()->where('wechat_news_id', $id)->select();
  158. }
  159. /**
  160. * 切换状态
  161. * @param $id
  162. * @param $data
  163. * @return int
  164. * @throws DbException
  165. * @author wuhaotian
  166. * @email 442384644@qq.com
  167. * @date 2024/7/12
  168. */
  169. public function switchStatus($id, $data)
  170. {
  171. return ($this->getModel())::getDB()->where($this->getPk(),$id)->update($data);
  172. }
  173. /**
  174. * 增加阅读量
  175. * @param $id
  176. * @author wuhaotian
  177. * @email 442384644@qq.com
  178. * @date 2024/7/12
  179. */
  180. public function incVisit($id)
  181. {
  182. $this->getModel()::getDB()->where($this->getPk(),$id)->inc('visit',1);
  183. }
  184. }