From 05427941b37621575364e0720472eb5364e22dda Mon Sep 17 00:00:00 2001 From: "xiao.ming" <1210919685@qq.com> Date: Sat, 13 Nov 2021 11:33:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=B6=E9=97=B4=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F,=E4=BF=AE=E6=94=B9=E5=91=A8=E6=8A=A5=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- global/model.go | 53 +++++++++++++++++++++++++++++++++++++-- service/wt/wt_comments.go | 4 +-- service/wt/wt_report.go | 27 +++----------------- 3 files changed, 57 insertions(+), 27 deletions(-) diff --git a/global/model.go b/global/model.go index ada292d..51988ec 100644 --- a/global/model.go +++ b/global/model.go @@ -1,13 +1,62 @@ package global import ( + "database/sql/driver" + "errors" + "fmt" "gorm.io/gorm" + "strings" "time" ) type GLOBAL_MODEL struct { ID uint `gorm:"primarykey"` // 主键ID - CreatedAt time.Time // 创建时间 - UpdatedAt time.Time // 更新时间 + CreatedAt FormatTime // 创建时间 + UpdatedAt FormatTime // 更新时间 DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 删除时间 } + +//FormatTime 自定义时间 +type FormatTime time.Time + +const timeLayout = "2006-01-02 15:04:05" + +func (t *FormatTime) UnmarshalJSON(data []byte) error { + if string(data) == "null" { + return nil + } + var err error + //前端接收的时间字符串 + str := string(data) + //去除接收的str收尾多余的" + timeStr := strings.Trim(str, "\"") + t1, err := time.Parse(timeLayout, timeStr) + *t = FormatTime(t1) + return err +} + +func (t FormatTime) MarshalJSON() ([]byte, error) { + formatted := fmt.Sprintf("\"%v\"", time.Time(t).Format(timeLayout)) + return []byte(formatted), nil +} + +func (t FormatTime) Value() (driver.Value, error) { + // FormatTime 转换成 time.Time 类型 + tTime := time.Time(t) + return tTime.Format(timeLayout), nil +} + +func (t *FormatTime) Scan(v interface{}) error { + switch vt := v.(type) { + case time.Time: + // 字符串转成 time.Time 类型 + *t = FormatTime(vt) + default: + return errors.New("类型处理错误") + } + return nil +} + +func (t *FormatTime) String() string { + return fmt.Sprintf("hhh:%s", time.Time(*t).String()) +} diff --git a/service/wt/wt_comments.go b/service/wt/wt_comments.go index 644f639..17d622c 100755 --- a/service/wt/wt_comments.go +++ b/service/wt/wt_comments.go @@ -54,9 +54,9 @@ func (wtCommentService *WtCommentService)GetWtCommentInfoList(info wtReq.WtComme } if info.ReportId > 0 { - err = db.Where("report_id=?", info.ReportId).Limit(limit).Offset(offset).Find(&wtComments).Error + err = db.Where("report_id=?", info.ReportId).Limit(limit).Offset(offset).Order("created_at desc").Find(&wtComments).Error }else { - err = db.Limit(limit).Offset(offset).Find(&wtComments).Error + err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&wtComments).Error } return err, wtComments, total diff --git a/service/wt/wt_report.go b/service/wt/wt_report.go index 5a7a10f..cb1f29f 100644 --- a/service/wt/wt_report.go +++ b/service/wt/wt_report.go @@ -60,33 +60,14 @@ func (wtReportsService *WtReportsService) GetWtReportsInfoList(info wtReq.WtRepo limit = int(total) } - //首选获取周报的ids - var reportIds []uint - reportTable.Select("id").Offset(offset).Limit(limit).Scan(&reportIds) - querySql := "SELECT id, user_name, user_id, send_to, header, contents, pictures, attachments, created_at, updated_at, cmc.comment_count " + "FROM wt_reports " + - "left join (SELECT report_id, count(report_id) as comment_count FROM wt_comments WHERE report_id in ? GROUP BY report_id) as cmc " + + "left join (SELECT report_id, count(report_id) as comment_count FROM wt_comments GROUP BY report_id) as cmc " + "on cmc.report_id = wt_reports.id " + "WHERE 1=1 " if info.CurrUserId > 0 { - //查询当前user的统计规则 - var WtServiceGroup WtServiceGroup - err, ruleRes := WtServiceGroup.WtRuleService.GetWtRuleByUserId(info.CurrUserId) - - reportUserIds := " ( " - if err == nil { - reportUserList := ruleRes.Reporters - - for _, report := range reportUserList { - reportUserIds += strconv.Itoa(int(report.ID)) + ", " - } - } - - reportUserIds += strconv.Itoa(int(info.CurrUserId)) + " ) " - - querySql += " and user_id in " + reportUserIds + querySql += " and send_to LIKE '%\"id\":" + strconv.Itoa(int(info.CurrUserId)) + "%'" } // 条件高级查询 @@ -102,8 +83,8 @@ func (wtReportsService *WtReportsService) GetWtReportsInfoList(info wtReq.WtRepo querySql += " and created_at >= '" + info.StartTime + "' and created_at <= '" + info.EndTime + "'" } - querySql += " LIMIT ? OFFSET ? " - err = global.GLOBAL_DB.Raw(querySql, reportIds, limit, offset).Scan(&reportsSearchBOList).Error + querySql += "ORDER BY created_at DESC LIMIT ? OFFSET ? " + err = global.GLOBAL_DB.Raw(querySql, limit, offset).Scan(&reportsSearchBOList).Error reportsSearchResultList := reportsToSearchResult(reportsSearchBOList)