修改时间格式,修改周报查询逻辑

master
xiao.ming 2021-11-13 11:33:04 +08:00
parent f432b51a84
commit 05427941b3
3 changed files with 57 additions and 27 deletions

View File

@ -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())
}

View File

@ -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

View File

@ -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)