123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- package dao
- import (
- "github.com/druidcaesa/gotool"
- "reflect"
- "strings"
- "ulink-admin/modules/system/models/model"
- "ulink-admin/modules/system/models/req"
- "ulink-admin/pkg/base"
- "ulink-admin/pkg/page"
- "ulink-admin/utils"
- "xorm.io/xorm"
- )
- type SysAttachmentDao struct {
- }
- func (d SysAttachmentDao) selectSql(session *xorm.Session) *xorm.Session {
- return session.Table([]string{"sys_attachment", "o"})
- }
- // SelectAll 查询所有附件数据,数据库操作
- func (d SysAttachmentDao) SelectAll() []*model.SysAttachment {
- session := base.SqlDB.NewSession()
- sysAttachment := make([]*model.SysAttachment, 0)
- err := session.Find(&sysAttachment)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return sysAttachment
- }
- // Find 查询附件分页数据
- func (d SysAttachmentDao) Find(query *req.SysAttachmentQuery) (*[]model.SysAttachment, int64) {
- sysAttachment := make([]model.SysAttachment, 0)
- session := base.SqlDB.NewSession().Table(model.SysAttachment{}.TableName())
- if query.Id > 0 {
- session.And("id = ?", query.Id)
- }
- if !gotool.StrUtils.HasEmpty(query.Label) {
- session.And("label = ?", query.Label)
- }
- if !gotool.StrUtils.HasEmpty(query.Path) {
- session.And("path = ?", query.Path)
- }
- if !gotool.StrUtils.HasEmpty(query.Url) {
- session.And("url = ?", query.Url)
- }
- if !gotool.StrUtils.HasEmpty(query.Type) {
- session.And("type = ?", query.Type)
- }
- if !gotool.StrUtils.HasEmpty(query.Ext) {
- session.And("ext = ?", query.Ext)
- }
- 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.Remark) {
- session.And("remark = ?", query.Remark)
- }
- if query.No > 0 {
- session.And("no = ?", query.No)
- }
- 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(&sysAttachment)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil, 0
- }
- return &sysAttachment, total
- }
- // Insert 添加附件数据
- func (d SysAttachmentDao) Insert(sysAttachment *model.SysAttachment) int64 {
- session := base.SqlDB.NewSession()
- session.Begin()
- insert, err := session.Insert(sysAttachment)
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return 0
- }
- session.Commit()
- return insert
- }
- // GetSysAttachmentById 根据id查询附件数据
- func (d SysAttachmentDao) GetSysAttachmentById(sysAttachment model.SysAttachment) *model.SysAttachment {
- _, err := base.SqlDB.NewSession().Where("id= ?", sysAttachment.Id).Get(&sysAttachment)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- return nil
- }
- return &sysAttachment
- }
- // Delete 批量删除附件
- func (d SysAttachmentDao) Delete(sysAttachment []int64) int64 {
- session := base.SqlDB.NewSession()
- session.Begin()
- i, err := session.In("id", sysAttachment).Delete(&model.SysAttachment{})
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return 0
- }
- session.Commit()
- return i
- }
- // Update 修改附件数据
- func (d SysAttachmentDao) Update(sysAttachment *model.SysAttachment) bool {
- session := base.SqlDB.NewSession()
- session.Begin()
- _, err := session.Where("id= ?", sysAttachment.Id).Update(sysAttachment)
- if err != nil {
- session.Rollback()
- gotool.Logs.ErrorLog().Println(err)
- return false
- }
- session.Commit()
- return true
- }
- // CheckUnique 唯一性检查
- func (d SysAttachmentDao) CheckUnique(sysAttachment model.SysAttachment, condition []string) int64 {
- session := base.SqlDB.Table(sysAttachment.TableName())
- if sysAttachment.Id > 0 {
- session.And("id != ?", sysAttachment.Id)
- }
- for _, item := range condition {
- fieldValue := reflect.ValueOf(sysAttachment).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
- }
|