package service import ( "fmt" "github.com/xuri/excelize/v2" "gorm.io/gorm" "insure/dto/request" "insure/dto/request/manage" "insure/global" "insure/model" "insure/utils/excel" "time" ) var StudentInsureService = studentInsureService{} type studentInsureService struct { } func (s *studentInsureService) Save(r request.StudentInsureInfoRequest) (*model.StudentInsureInfo, error) { info := model.StudentInsureInfo{} info.SchoolName = r.SchoolName info.StudentName = r.StudentName info.StudentIdentityNo = r.StudentIdentityNo info.StudentIdentityType = r.StudentIdentityType info.College = r.College info.Speciality = r.Speciality info.Class = r.Class info.Relation = r.Relation info.ParentName = r.ParentName info.ParentIdentityNo = r.ParentIdentityNo info.ParentIdentityType = r.ParentIdentityType info.ParentMobile = r.ParentMobile err := global.DB.Save(&info).Error if err != nil { return nil, err } return &info, nil } func (s *studentInsureService) GetInfos(req manage.StudentInsureInfoQueryRequest) ([]model.StudentInsureInfo, int64) { records := make([]model.StudentInsureInfo, req.PageSize) offset := (req.Page - 1) * req.PageSize var total int64 s.buildQuery(req).Order("id desc").Offset(offset).Limit(req.PageSize).Find(&records) s.buildQuery(req).Model(&model.Order{}).Count(&total) return records, total } func (s *studentInsureService) buildQuery(req manage.StudentInsureInfoQueryRequest) *gorm.DB { tx := global.DB if req.SchoolName != "" { tx = tx.Where("school_name", req.SchoolName) } if req.StudentName != "" { tx = tx.Where("student_name", req.StudentName) } if req.StudentIdentityNo != "" { tx = tx.Where("student_identity_no", req.StudentIdentityNo) } if req.ParentName != "" { tx = tx.Where("parent_name", req.ParentName) } if req.ParentIdentityNo != "" { tx = tx.Where("parent_identity_no", req.ParentIdentityNo) } if req.ParentMobile != "" { tx = tx.Where("parent_mobile", req.ParentMobile) } if req.College != "" { tx = tx.Where("college", req.College) } if req.Speciality != "" { tx = tx.Where("speciality", req.Speciality) } if req.Class != "" { tx = tx.Where("class", req.Class) } if req.UpdatedAtStart != "" && req.UpdatedAtEnd != "" { updatedAtStart := req.UpdatedAtStart + " 00:00:00" updatedAtEnd := req.UpdatedAtEnd + " 23:59:59" tx = tx.Where("updated_at >= ?", updatedAtStart).Where("updated_at <= ?", updatedAtEnd) } return tx } func (s *studentInsureService) GenerateInfosExcel(req manage.StudentInsureInfoQueryRequest) *excelize.File { pageSize := 200 lastID := 0 f := excel.NewFile() headers := []string{ "学校名称", "学生姓名", "学生证件号", "学生证件类型", "学院", "专业", "班级", "家长与学生关系", "家长姓名", "家长证件号", "家长证件类型", "家长电话", "提交时间", } excel.SetSimpleHeaders(headers, "Sheet1", f) var allRecords []model.StudentInsureInfo for { records := make([]model.StudentInsureInfo, pageSize) s.buildQuery(req). Where("id > ?", lastID). Order("id asc"). Limit(pageSize). Find(&records) count := len(records) if count == 0 { break } lastIndex := count - 1 lastID = records[lastIndex].ID fmt.Println("lastID", lastID) allRecords = append(allRecords, records...) } s.buildOrdersExcel(allRecords, "Sheet1", f) return f } func (studentInsureService) buildOrdersExcel(records []model.StudentInsureInfo, sheet string, f *excelize.File) { row := 2 for _, record := range records { col := 0 f.SetCellValue(sheet, excel.CellKey(row, &col), record.SchoolName) f.SetCellValue(sheet, excel.CellKey(row, &col), record.StudentName) f.SetCellValue(sheet, excel.CellKey(row, &col), record.StudentIdentityNo) f.SetCellValue(sheet, excel.CellKey(row, &col), "身份证") f.SetCellValue(sheet, excel.CellKey(row, &col), record.College) f.SetCellValue(sheet, excel.CellKey(row, &col), record.Speciality) f.SetCellValue(sheet, excel.CellKey(row, &col), record.Class) f.SetCellValue(sheet, excel.CellKey(row, &col), record.Relation) f.SetCellValue(sheet, excel.CellKey(row, &col), record.ParentName) f.SetCellValue(sheet, excel.CellKey(row, &col), record.ParentIdentityNo) f.SetCellValue(sheet, excel.CellKey(row, &col), "身份证") f.SetCellValue(sheet, excel.CellKey(row, &col), record.ParentMobile) f.SetCellValue(sheet, excel.CellKey(row, &col), record.UpdatedAt.Format(time.DateTime)) row++ } }