checkrecordservice.go 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package service
  2. import (
  3. "strconv"
  4. "strings"
  5. "sws_xcx/models"
  6. "sws_xcx/utils"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type CheckRecordService struct {
  10. rdb *gorm.DB
  11. wdb *gorm.DB
  12. }
  13. func NewCheckRecordService() *CheckRecordService {
  14. return &CheckRecordService{
  15. rdb: ReadDB(),
  16. wdb: WriteDB(),
  17. }
  18. }
  19. func (s *CheckRecordService) GetCheckRecordList(pageNum, pageSize int, userid uint64) (vos []models.AppCheckRecordVO, total int, err error) {
  20. offset := (pageNum - 1) * pageSize
  21. records := make([]*models.CheckRecord, pageSize)
  22. err = s.rdb.Model(&models.CheckRecord{}).Where("user_id= ? and delete_flag = ?", 0).Count(&total).Order("id desc").Offset(offset).Limit(pageSize).Find(&records).Error
  23. if len(records) > 0 {
  24. is := NewCheckItemService()
  25. cnItems, err1 := is.GetCheckItems("cn", "1")
  26. if err1 != nil {
  27. utils.ErrorLog("GetCheckItems err:%v ", err1)
  28. }
  29. mcn := make(map[int]models.CheckItem, len(cnItems))
  30. for _, item := range cnItems {
  31. mcn[item.Id] = *item
  32. }
  33. vos = make([]models.AppCheckRecordVO, len(records))
  34. for i, r := range records {
  35. vo := models.AppCheckRecordVO{}
  36. vo.CheckRecordId = r.Id
  37. vo.UserId = r.UserId
  38. if r.UserId > 0 {
  39. vo.Bind = 1
  40. }
  41. vo.CheckDate = models.Time(r.Ctime)
  42. vo.View = r.View
  43. vo.AlertCount = 0
  44. //vo.AlertGrade = 0
  45. if r.AlertItemIds != "" {
  46. ids := strings.Split(r.AlertItemIds, ",")
  47. names := make([]string, len(ids))
  48. for j, id := range ids {
  49. ii, _ := strconv.Atoi(id)
  50. if item, ok := mcn[ii]; ok {
  51. names[j] = item.NameCn
  52. }
  53. }
  54. vo.AlertItems = names
  55. vo.AlertCount = len(ids)
  56. }
  57. vo.NormalCount = len(cnItems) - vo.AlertCount
  58. vos[i] = vo
  59. }
  60. }
  61. return
  62. }
  63. func (s *CheckRecordService) CreateCheckRecord(checkRecord *models.CheckRecord) error {
  64. return s.wdb.Model(checkRecord).Create(checkRecord).Error
  65. }
  66. func (s *CheckRecordService) CreateCheckRecordItem(checkRecordItem *models.CheckRecordItem) error {
  67. return s.wdb.Model(checkRecordItem).Create(checkRecordItem).Error
  68. }
  69. func (s *CheckRecordService) UpdateCheckRecordAlertItems(id int64, alertItemIds string) error {
  70. return s.wdb.Model(&models.CheckRecord{}).Where("id = ?", id).Update("alert_item_ids", alertItemIds).Error
  71. }