ArticleDao.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. $query->with(['content','articleCategory']);
  59. return $query->where('mer_id',$merId)->order('sort DESC,create_time DESC');
  60. }
  61. /**
  62. * 根据 字段名查询
  63. * @param int $merId
  64. * @param $field
  65. * @param $value
  66. * @param null $except
  67. * @return bool
  68. * @author Qinii
  69. */
  70. public function merFieldExists(int $merId, $field, $value, $except = null)
  71. {
  72. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  73. $query->where($field, '<>', $except);
  74. })->where('mer_id', $merId)->where('wechat_news_id',0)->where($field, $value)->count() > 0;
  75. }
  76. /**
  77. * 查询一条
  78. * @param int $merId
  79. * @param int $id
  80. * @return array|Model|null
  81. * @throws DataNotFoundException
  82. * @throws DbException
  83. * @throws ModelNotFoundException
  84. * @author Qinii
  85. *
  86. */
  87. public function get( $id, int $merId = 0)
  88. {
  89. return ($this->getModel())::getDB()->where('mer_id', $merId)->where('wechat_news_id',0)->with(['content','articleCategory'])->find($id);
  90. }
  91. /**
  92. * @param int $id
  93. * @param int $merId
  94. * @return int
  95. * @throws DbException
  96. * @author xaboy
  97. * @day 2020-04-20
  98. */
  99. public function delete(int $id, int $merId = 0)
  100. {
  101. $result = ($this->getModel())::getDB()->where('mer_id', $merId)
  102. ->where($this->getPk(),$id)
  103. ->with('content')
  104. ->find();
  105. return $result->together(['content'])->delete();
  106. }
  107. /**
  108. * 关联添加
  109. * @param array $data
  110. * @return BaseDao|Model|void
  111. * @author Qinii
  112. */
  113. public function create(array $data)
  114. {
  115. Db::transaction(function()use($data){
  116. $content = $data['content'];
  117. unset($data['content']);
  118. $article = $this->getModel()::create($data);
  119. $article->content()->save(['content' => $content]);
  120. });
  121. }
  122. /**
  123. * 关联更新
  124. * @param int $id
  125. * @param array $data
  126. * @return int|void
  127. * @author Qinii
  128. */
  129. public function update(int $id, array $data)
  130. {
  131. Db::transaction(function()use($id,$data){
  132. $content = $data['content'];
  133. unset($data['content']);
  134. $this->getModel()::where($this->getPk(),$id)->update($data);
  135. $article = $this->getModel()::find($id);
  136. $article->content->content = $content;
  137. $article->together(['content'])->save();
  138. });
  139. }
  140. /**
  141. * 根据字段获取 主键值
  142. * @param int $vale
  143. * @param null $field
  144. * @return array
  145. * @author Qinii
  146. */
  147. public function getKey(int $vale,$field = null)
  148. {
  149. return ($this->getModel())::getDB()->where($field,$vale)->column($this->getPk());
  150. }
  151. public function wechatNewIdByData($id)
  152. {
  153. return ($this->getModel())::getDB()->where('wechat_news_id', $id)->select();
  154. }
  155. }