123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package dao
- import (
- "github.com/druidcaesa/gotool"
- "reflect"
- "strings"
- "ulink-admin/modules/doc/models/model"
- "ulink-admin/modules/doc/models/req"
- "ulink-admin/pkg/base"
- "ulink-admin/pkg/page"
- "ulink-admin/utils"
- "xorm.io/xorm"
- )
- type DocArticleDao struct {
- }
- func (d DocArticleDao) selectSql(session *xorm.Session) *xorm.Session {
- return session.Table([]string{"doc_article", "o"})
- }
- // SelectAll 查询所有文章数据,数据库操作
- func (d DocArticleDao) SelectAll() []*model.DocArticle {
- session := base.SqlDB.NewSession()
- docArticle := make([]*model.DocArticle, 0)
- err := session.Find(&docArticle)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return docArticle
- }
- // Find 查询文章分页数据
- func (d DocArticleDao) Find(query req.DocArticleQuery) (*[]model.DocArticle, int64) {
- docArticle := make([]model.DocArticle, 0)
- session := base.SqlDB.NewSession().Table(model.DocArticle{}.TableName())
- if query.Id > 0 {
- session.And("id = ?", query.Id)
- }
- if !gotool.StrUtils.HasEmpty(query.Label) {
- session.And("label = ?", query.Label)
- }
- if query.CatId > 0 {
- session.And("cat_id = ?", query.CatId)
- }
- if !gotool.StrUtils.HasEmpty(query.Profile) {
- session.And("profile = ?", query.Profile)
- }
- if !gotool.StrUtils.HasEmpty(query.Url) {
- session.And("url = ?", query.Url)
- }
- if query.IsTop > 0 {
- session.And("is_top = ?", query.IsTop)
- }
- if query.IsHot > 0 {
- session.And("is_hot = ?", query.IsHot)
- }
- if !gotool.StrUtils.HasEmpty(query.Content) {
- session.And("content = ?", query.Content)
- }
- if query.Sort > 0 {
- session.And("sort = ?", query.Sort)
- }
- if !gotool.StrUtils.HasEmpty(query.CreateBy) {
- session.And("create_by = ?", query.CreateBy)
- }
- if !gotool.StrUtils.HasEmpty(query.UpdateBy) {
- session.And("update_by = ?", query.UpdateBy)
- }
- if !gotool.StrUtils.HasEmpty(query.BeginTime) {
- session.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
- }
- if !gotool.StrUtils.HasEmpty(query.EndTime) {
- session.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
- }
- total, err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).FindAndCount(&docArticle)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil, 0
- }
- return &docArticle, total
- }
- // Insert 添加文章数据
- func (d DocArticleDao) Insert(docArticle *model.DocArticle) int64 {
- session := base.SqlDB.NewSession()
- session.Begin()
- insert, err := session.Insert(docArticle)
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return 0
- }
- session.Commit()
- return insert
- }
- // GetDocArticleById 根据id查询文章数据
- func (d DocArticleDao) GetDocArticleById(docArticle model.DocArticle) *model.DocArticle {
- _, err := base.SqlDB.NewSession().Where("id= ?", docArticle.Id).Get(&docArticle)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return &docArticle
- }
- // Delete 批量删除文章
- func (d DocArticleDao) Delete(docArticle []int64) int64 {
- session := base.SqlDB.NewSession()
- session.Begin()
- i, err := session.In("id", docArticle).Delete(&model.DocArticle{})
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return 0
- }
- session.Commit()
- return i
- }
- // Update 修改文章数据
- func (d DocArticleDao) Update(docArticle *model.DocArticle) bool {
- session := base.SqlDB.NewSession()
- session.Begin()
- _, err := session.Where("id= ?", docArticle.Id).Update(docArticle)
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return false
- }
- session.Commit()
- return true
- }
- // CheckUnique 唯一性检查
- func (d DocArticleDao) CheckUnique(docArticle model.DocArticle, condition []string) int64 {
- session := base.SqlDB.Table(docArticle.TableName())
- if docArticle.Id > 0 {
- session.And("id != ?", docArticle.Id)
- }
- for _, item := range condition {
- fieldValue := reflect.ValueOf(docArticle).FieldByName(utils.Ucfirst(item))
- if strings.Contains(fieldValue.Type().String(), "int") {
- session.And(utils.Camel2Case(item)+" = ?", fieldValue.Int())
- } else if strings.Contains(fieldValue.Type().String(), "string") {
- session.And(utils.Camel2Case(item)+" = ?", fieldValue.String())
- } else {
- session.And(utils.Camel2Case(item)+" = ?", fieldValue.String())
- }
- }
- count, err := session.Count()
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- }
- return count
- }
- func (d DocArticleDao) FindCountByCatId(id int64) int64 {
- session := base.SqlDB.Table(model.DocArticle{}.TableName())
- session.And("cat_id = ?", id)
- count, err := session.Count()
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- }
- return count
- }
|