| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- /**
- * @package merchant
- *
- * @author xaboy
- * @day 2020-04-20
- *
- *
- */
- namespace app\common\dao\article;
- use think\facade\Db;
- use app\common\dao\BaseDao;
- use app\common\model\article\Article;
- use app\common\model\BaseModel;
- class ArticleDao extends BaseDao
- {
- /**
- * @return BaseModel
- * @author xaboy
- * @day 2020-03-30
- */
- protected function getModel(): string
- {
- return Article::class;
- }
- /**
- * @param int $mer_id
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getAll($mer_id = 0)
- {
- return Article::getDB()->with('content')->where('mer_id', $mer_id)->select();
- }
- /**
- * 搜索列表
- * @param $merId
- * @param array $where
- * @return \think\db\BaseQuery
- * @author Qinii
- */
- public function search($merId,array $where)
- {
- $query = Article::getDB()
- ->field('article_id,cid,title,image_input,author,mer_id,sort,status,is_banner,is_hot,synopsis,is_top,is_banner,url,create_time')
- ->order('is_top,is_hot DESC');
- if($where['cid'] == 23){
- $query->whereIn('cid',[23,24]);
- }else{
- if (isset($where['cid']) && $where['cid'])
- $query->where('cid',(int) $where['cid']);
- }
- if (isset($where['title']) && $where['title']) $query->where('title',(string) $where['title']);
- if (isset($where['status']) && $where['status']) $query->where('status',$where['status']);
- $query->with(['content','articleCategory']);
- return $query->where('mer_id',$merId)->where('wechat_news_id',0);
- }
- /**
- * 根据 字段名查询
- * @param int $merId
- * @param $field
- * @param $value
- * @param null $except
- * @return bool
- * @author Qinii
- */
- public function merFieldExists(int $merId, $field, $value, $except = null)
- {
- return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
- $query->where($field, '<>', $except);
- })->where('mer_id', $merId)->where('wechat_news_id',0)->where($field, $value)->count() > 0;
- }
- /**
- * 查询一条
- * @param int $merId
- * @param int $id
- * @return array|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @author Qinii
- *
- */
- public function get( $id, int $merId = 0)
- {
- return ($this->getModel())::getDB()->where('mer_id', $merId)->where('wechat_news_id',0)->with(['content','articleCategory'])->find($id);
- }
- /**
- * @param int $id
- * @param int $merId
- * @return int
- * @throws DbException
- * @author xaboy
- * @day 2020-04-20
- */
- public function delete(int $id, int $merId = 0)
- {
- $result = ($this->getModel())::getDB()->where('mer_id', $merId)
- ->where($this->getPk(),$id)
- ->with('content')
- ->find();
- return $result->together(['content'])->delete();
- }
- /**
- * 关联添加
- * @param array $data
- * @return BaseDao|\think\Model|void
- * @author Qinii
- */
- public function create(array $data)
- {
- Db::transaction(function()use($data){
- $content['content'] = $data['content'];
- unset($data['content']);
- $article = $this->getModel()::create($data);
- $article->content()->save($content);
- });
- }
- /**
- * 关联更新
- * @param int $id
- * @param array $data
- * @return int|void
- * @author Qinii
- */
- public function update(int $id, array $data)
- {
- Db::transaction(function()use($id,$data){
- $content = $data['content'];
- unset($data['content']);
- $this->getModel()::where($this->getPk(),$id)->update($data);
- $article = $this->getModel()::find($id);
- $article->content->content = $content;
- $article->together(['content'])->save();
- });
- }
- /**
- * 根据字段获取 主键值
- * @param int $vale
- * @param null $field
- * @return array
- * @author Qinii
- */
- public function getKey(int $vale,$field = null)
- {
- return ($this->getModel())::getDB()->where($field,$vale)->column($this->getPk());
- }
- public function wechatNewIdByData($id)
- {
- return ($this->getModel())::getDB()->where('wechat_news_id', $id)->select();
- }
- }
|