safe_dao.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "fmt"
  10. "time"
  11. "github.com/go-xorm/xorm"
  12. "go.mod/models"
  13. "go.mod/web/viewmodels"
  14. )
  15. //数据库操作引擎
  16. type SafeDao struct {
  17. engine *xorm.Engine
  18. }
  19. //获得一个DAO对象
  20. func NewSafeDao(engine *xorm.Engine) *SafeDao {
  21. return &SafeDao{
  22. engine: engine,
  23. }
  24. }
  25. func (d *SafeDao) FindById(id int) *models.CmSafe {
  26. data := &models.CmSafe{Id: id}
  27. ok, err := d.engine.Get(data)
  28. if ok && err == nil {
  29. return data
  30. } else {
  31. data.Id = 0
  32. return data
  33. }
  34. }
  35. // id获得数据
  36. func (d *SafeDao) GetListByBid(id int, pageNo int, pageSize int) ([]models.CmSafe, int64) {
  37. dataList := make([]models.CmSafe, 0)
  38. start := (pageNo - 1) * pageSize
  39. total, err := d.engine.
  40. Where("bidsection_id=?", id).
  41. Asc("id").
  42. Limit(pageSize, start).
  43. FindAndCount(&dataList)
  44. if err != nil {
  45. return dataList, 0
  46. }
  47. return dataList, total
  48. }
  49. // 插入单条记录
  50. func (d *SafeDao) InsertRecord(data models.CmSafe) (bool, error) {
  51. affected, err := d.engine.InsertOne(data)
  52. return affected > 0, err
  53. }
  54. // 删除记录
  55. func (d *SafeDao) DeleteRecord(id int) (bool, error) {
  56. safe := models.CmSafe{}
  57. affected, err := d.engine.Id(id).Delete(safe)
  58. return affected > 0, err
  59. }
  60. // 根据code获取记录
  61. func (d *SafeDao) FindByCode(code string) bool {
  62. data := &models.CmSafe{Code: code}
  63. has, _ := d.engine.Get(data)
  64. return has
  65. }
  66. // 筛选出应用了当前规则的条数
  67. func (d *SafeDao) CountRuleCode(bid int) (int64, error) {
  68. data := &models.CmSafe{}
  69. total, err := d.engine.Where("`bidsection_id` = ?", bid).Count(data)
  70. if err != nil {
  71. total = 0
  72. }
  73. return total, err
  74. }
  75. // 更改status
  76. func (d *SafeDao) ChangeStatus(id int, status int, inspection string, inspectionDetail string, demand string, createTime time.Time) error {
  77. data := &models.CmSafe{Status: status, Inspection: inspection, InspectionDetail: inspectionDetail, Demand: demand, CreateTime: createTime}
  78. _, err := d.engine.ID(id).Cols("status", " inspection", "inspection_detail", "demand", "create_time").Update(data)
  79. return err
  80. }
  81. // 获得某状态下的安全
  82. func (d *SafeDao) GetStatus(bidsectionId int, status int) []models.CmSafe {
  83. datalist := make([]models.CmSafe, 0)
  84. _ = d.engine.
  85. Where("bidsection_id = ? and status=? ", bidsectionId, status).
  86. Desc("id").
  87. Find(&datalist)
  88. return datalist
  89. }
  90. // 获得某状态下的安全
  91. func (d *SafeDao) GetStatusByProjectAndAccount(projectId int, projectAccountId int, status int) []models.CmSafe {
  92. datalist := make([]models.CmSafe, 0)
  93. _ = d.engine.
  94. Where("project_id = ? and audit_id= ? and status=? ", projectId, projectAccountId, status).
  95. Desc("id").
  96. Find(&datalist)
  97. return datalist
  98. }
  99. // 获得某年份下的安全
  100. func (d *SafeDao) GetTypeYear(bidsectionId int, year int) []viewmodels.SafeSurveyList {
  101. startYear := fmt.Sprintf("%d-01-01:00.00.00", year)
  102. endYear := fmt.Sprintf("%d-12-31:23.59.59", year)
  103. datalist := make([]viewmodels.SafeSurveyList, 0)
  104. _ = d.engine.Table("`cm_safe` as cs").
  105. Select("cs.id, cs.`create_time`, cs.`inspection_detail`, cs.status, pa.`name` as `audit_name`").
  106. Where("cs.bidsection_id = ? and cs.create_time>='"+startYear+"' and cs.create_time<='"+endYear+"' ", bidsectionId).
  107. Join("left", "cm_project_account as pa", "pa.id = cs.uid").
  108. Desc("id").
  109. Find(&datalist)
  110. return datalist
  111. }