/* * @description: 安全巡检数据库操作相关 * @Author: LanJianRong * @Date: 2020-11-20 * @FilePath: \construction_management\dao\safe_dao.go */ package dao import ( "fmt" "github.com/go-xorm/xorm" "go.mod/models" ) //数据库操作引擎 type SafeDao struct { engine *xorm.Engine } //获得一个DAO对象 func NewSafeDao(engine *xorm.Engine) *SafeDao { return &SafeDao{ engine: engine, } } func (d *SafeDao) FindById(id int) *models.CmSafe { data := &models.CmSafe{Id: id} ok, err := d.engine.Get(data) if ok && err == nil { return data } else { data.Id = 0 return data } } // id获得数据 func (d *SafeDao) GetListByBid(id int, pageNo int, pageSize int) ([]models.CmSafe, int64) { dataList := make([]models.CmSafe, 0) start := (pageNo - 1) * pageSize total, err := d.engine. Where("bidsection_id=?", id). Asc("id"). Limit(pageSize, start). FindAndCount(&dataList) if err != nil { return dataList, 0 } return dataList, total } // 插入单条记录 func (d *SafeDao) InsertRecord(data models.CmSafe) (bool, error) { affected, err := d.engine.InsertOne(data) return affected > 0, err } // 删除记录 func (d *SafeDao) DeleteRecord(id int) (bool, error) { safe := models.CmSafe{} affected, err := d.engine.Id(id).Delete(safe) return affected > 0, err } // 根据code获取记录 func (d *SafeDao) FindByCode(code string) bool { data := &models.CmSafe{Code: code} has, _ := d.engine.Get(data) return has } // 筛选出应用了当前规则的条数 func (d *SafeDao) CountRuleCode(bid int) (int64, error) { data := &models.CmSafe{} total, err := d.engine.Where("`bidsection_id` = ?", bid).Count(data) if err != nil { total = 0 } return total, err } // 更改status func (d *SafeDao) ChangeStatus(id int, status int, times int) error { data := &models.CmSafe{Status: status, Times: times} _, err := d.engine.ID(id).Update(data) return err } // 获得某状态下的安全 func (d *SafeDao) GetStatus(bidsectionId int, status int) []models.CmSafe { datalist := make([]models.CmSafe, 0) _ = d.engine. Where("bidsection_id = ? and status=? ", bidsectionId, status). Desc("id"). Find(&datalist) return datalist } // 获得某年份下的安全 func (d *SafeDao) GetTypeYear(bidsectionId int, year int) []models.CmSafe { startYear := fmt.Sprintf("%d-01-01:00.00.00", year) endYear := fmt.Sprintf("%d-12-31:23.59.59", year) datalist := make([]models.CmSafe, 0) _ = d.engine. Where("bidsection_id = ? and create_time>='"+startYear+"' and create_time<='"+endYear+"' ", bidsectionId). Desc("id"). Find(&datalist) return datalist }