safe_dao.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * @description: 安全巡检数据库操作相关
  3. * @Author: LanJianRong
  4. * @Date: 2020-11-20
  5. * @FilePath: \construction_management\dao\safe_dao.go
  6. */
  7. package dao
  8. import (
  9. "github.com/go-xorm/xorm"
  10. "go.mod/models"
  11. )
  12. //数据库操作引擎
  13. type SafeDao struct {
  14. engine *xorm.Engine
  15. }
  16. //获得一个DAO对象
  17. func NewSafeDao(engine *xorm.Engine) *SafeDao {
  18. return &SafeDao{
  19. engine: engine,
  20. }
  21. }
  22. func (d *SafeDao) FindById(id int) *models.CmSafe {
  23. data := &models.CmSafe{Id: id}
  24. ok, err := d.engine.Get(data)
  25. if ok && err == nil {
  26. return data
  27. } else {
  28. data.Id = 0
  29. return data
  30. }
  31. }
  32. // id获得数据
  33. func (d *SafeDao) GetListByBid(id int, pageNo int, pageSize int) ([]models.CmSafe, int64) {
  34. dataList := make([]models.CmSafe, 0)
  35. start := (pageNo - 1) * pageSize
  36. total, err := d.engine.
  37. Where("bidsection_id=?", id).
  38. Asc("id").
  39. Limit(pageSize, start).
  40. FindAndCount(&dataList)
  41. if err != nil {
  42. return dataList, 0
  43. }
  44. return dataList, total
  45. }
  46. // 插入单条记录
  47. func (d *SafeDao) InsertRecord(data models.CmSafe) (bool, error) {
  48. affected, err := d.engine.InsertOne(data)
  49. return affected > 0, err
  50. }
  51. // 删除记录
  52. func (d *SafeDao) DeleteRecord(id int) (bool, error) {
  53. safe := models.CmSafe{}
  54. affected, err := d.engine.Id(id).Delete(safe)
  55. return affected > 0, err
  56. }
  57. // 根据code获取记录
  58. func (d *SafeDao) FindByCode(code string) bool {
  59. data := &models.CmSafe{Code: code}
  60. has, _ := d.engine.Get(data)
  61. return has
  62. }
  63. // 筛选出应用了当前规则的条数
  64. func (d *SafeDao) CountRuleCode(bid int) (int64, error) {
  65. data := &models.CmSafe{}
  66. total, err := d.engine.Where("`bidsection_id` = ?", bid).Count(data)
  67. if err != nil {
  68. total = 0
  69. }
  70. return total, err
  71. }
  72. // 更改status
  73. func (d *SafeDao) ChangeStatus(id int, status int, times int) error {
  74. data := &models.CmSafe{Status: status, Times: times}
  75. _, err := d.engine.ID(id).Update(data)
  76. return err
  77. }