quality_dao.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * @description: 安全巡检数据库操作相关
  3. * @Author: LanJianRong
  4. * @Date: 2020-11-20
  5. * @FilePath: \construction_management\dao\Quality_dao.go
  6. */
  7. package dao
  8. import (
  9. "fmt"
  10. "github.com/go-xorm/xorm"
  11. "go.mod/models"
  12. )
  13. //数据库操作引擎
  14. type QualityDao struct {
  15. engine *xorm.Engine
  16. }
  17. //获得一个DAO对象
  18. func NewQualityDao(engine *xorm.Engine) *QualityDao {
  19. return &QualityDao{
  20. engine: engine,
  21. }
  22. }
  23. func (d *QualityDao) FindById(id int) *models.CmQuality {
  24. data := &models.CmQuality{Id: id}
  25. ok, err := d.engine.Get(data)
  26. if ok && err == nil {
  27. return data
  28. } else {
  29. data.Id = 0
  30. return data
  31. }
  32. }
  33. // id获得数据
  34. func (d *QualityDao) GetListByBid(id int, pageNo int, pageSize int) ([]models.CmQuality, int64) {
  35. dataList := make([]models.CmQuality, 0)
  36. start := (pageNo - 1) * pageSize
  37. total, err := d.engine.
  38. Where("bidsection_id=?", id).
  39. Asc("id").
  40. Limit(pageSize, start).
  41. FindAndCount(&dataList)
  42. if err != nil {
  43. return dataList, 0
  44. }
  45. return dataList, total
  46. }
  47. // 插入单条记录
  48. func (d *QualityDao) InsertRecord(data models.CmQuality) (bool, error) {
  49. affected, err := d.engine.InsertOne(data)
  50. return affected > 0, err
  51. }
  52. // 删除记录
  53. func (d *QualityDao) DeleteRecord(id int) (bool, error) {
  54. Quality := models.CmQuality{}
  55. affected, err := d.engine.Id(id).Delete(Quality)
  56. return affected > 0, err
  57. }
  58. // 根据code获取记录
  59. func (d *QualityDao) FindByCode(code string) bool {
  60. data := &models.CmQuality{Code: code}
  61. has, _ := d.engine.Get(data)
  62. return has
  63. }
  64. // 筛选出应用了当前规则的条数
  65. func (d *QualityDao) CountRuleCode(bid int) (int64, error) {
  66. data := &models.CmQuality{}
  67. total, err := d.engine.Where("`bidsection_id` = ?", bid).Count(data)
  68. if err != nil {
  69. total = 0
  70. }
  71. return total, err
  72. }
  73. // 更改status
  74. func (d *QualityDao) ChangeStatus(id int, status int) error {
  75. data := &models.CmQuality{Status: status}
  76. _, err := d.engine.ID(id).Update(data)
  77. return err
  78. }
  79. // 获得某状态下的安全
  80. func (d *QualityDao) GetStatus(bidsectionId int, status int) []models.CmQuality {
  81. datalist := make([]models.CmQuality, 0)
  82. _ = d.engine.
  83. Where("bidsection_id = ? and status=? ", bidsectionId, status).
  84. Desc("id").
  85. Find(&datalist)
  86. return datalist
  87. }
  88. // 获得某年份下的安全
  89. func (d *QualityDao) GetTypeYear(bidsectionId int, year int) []models.CmQuality {
  90. startYear := fmt.Sprintf("%d-01-01:00.00.00", year)
  91. endYear := fmt.Sprintf("%d-12-31:23.59.59", year)
  92. datalist := make([]models.CmQuality, 0)
  93. _ = d.engine.
  94. Where("bidsection_id = ? and create_time>='"+startYear+"' and create_time<='"+endYear+"' ", bidsectionId).
  95. Desc("id").
  96. Find(&datalist)
  97. return datalist
  98. }