package dailyreport import ( "encoding/json" "fmt" "strings" "time" "unicode/utf8" "server/models" "github.com/beego/beego/v2/client/orm" ) type Attachment struct { ID uint64 `json:"id"` URL string `json:"url"` Name string `json:"name"` Size int64 `json:"size"` } type ReportItem struct { ID uint64 `json:"id"` UserID uint64 `json:"user_id"` UserName string `json:"user_name"` OrgID uint64 `json:"org_id"` OrgName string `json:"org_name"` ReportDate string `json:"report_date"` TodayDone string `json:"today_done"` TomorrowPlan string `json:"tomorrow_plan"` Issues string `json:"issues"` Attachments []Attachment `json:"attachments"` Status int8 `json:"status"` SubmitTime string `json:"submit_time"` CreateTime string `json:"create_time"` UpdateTime string `json:"update_time"` CommentCount int `json:"comment_count"` } type Comment struct { ID uint64 `json:"id"` UserID uint64 `json:"user_id"` UserName string `json:"user_name"` Content string `json:"content"` ReplyToID uint64 `json:"reply_to_id"` ReplyToName string `json:"reply_to_name"` CreateTime string `json:"create_time"` IsMine bool `json:"is_mine"` CanDelete bool `json:"can_delete"` } type Detail struct { Report ReportItem `json:"report"` Comments []Comment `json:"comments"` Histories []HistoryItem `json:"histories"` IsAuthor bool `json:"is_author"` CanEdit bool `json:"can_edit"` CanDelete bool `json:"can_delete"` CanComment bool `json:"can_comment"` CanWithdraw bool `json:"can_withdraw"` } type HistoryItem struct { ID uint64 `json:"id"` Action int8 `json:"action"` UserName string `json:"user_name"` TodayDone string `json:"today_done"` TomorrowPlan string `json:"tomorrow_plan"` Issues string `json:"issues"` Attachments []Attachment `json:"attachments"` SubmitTime string `json:"submit_time"` CreateTime string `json:"create_time"` } type SaveInput struct { Date string `json:"date"` TodayDone string `json:"today_done"` TomorrowPlan string `json:"tomorrow_plan"` Issues string `json:"issues"` Attachments string `json:"attachments"` Submit bool `json:"submit"` } type MineQuery struct { StartDate string EndDate string Page int PageSize int } type MineSummary struct { MonthTotal int `json:"month_total"` MonthSubmitted int `json:"month_submitted"` TodayStatus int8 `json:"today_status"` TodayReportID uint64 `json:"today_report_id"` WindowStart string `json:"window_start"` WindowEnd string `json:"window_end"` } type MineResult struct { List []ReportItem `json:"list"` Total int64 `json:"total"` Summary MineSummary `json:"summary"` } func Save(u *UserContext, in SaveInput) (*ReportItem, error) { date, err := ParseDate(in.Date) if err != nil { return nil, err } if !InWindow(date) { return nil, errBad(fmt.Sprintf("只能补写最近%d天(%s 至 %s)的日报", BackfillDays, WindowStart(), Today())) } in.TodayDone = strings.TrimSpace(in.TodayDone) in.TomorrowPlan = strings.TrimSpace(in.TomorrowPlan) in.Issues = strings.TrimSpace(in.Issues) for name, text := range map[string]string{ "今日完成": in.TodayDone, "明日计划": in.TomorrowPlan, "问题与建议": in.Issues, } { if utf8.RuneCountInString(text) > 5000 { return nil, errBad(name + "不能超过5000字") } } if in.TodayDone == "" && in.TomorrowPlan == "" && in.Issues == "" { return nil, errBad("请至少填写今日完成、明日计划、问题与建议中的一项") } attachmentJSON, _, err := normalizeAttachments(in.Attachments) if err != nil { return nil, err } var existing models.OaDailyReport findErr := models.Orm.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid). Filter("user_id", u.UID). Filter("report_date", date). Filter("is_deleted", 0). One(&existing) status := int8(0) if in.Submit { status = 1 } now := time.Now() if findErr == orm.ErrNoRows { report := models.OaDailyReport{ Tid: u.Tid, UserID: u.UID, UserName: u.Name, OrgID: u.OrgID, OrgName: u.OrgName, ReportDate: date, TodayDone: in.TodayDone, TomorrowPlan: in.TomorrowPlan, Issues: in.Issues, Attachments: attachmentJSON, Status: status, } if status == 1 { report.SubmitTime = &now } if _, err := models.Orm.Insert(&report); err != nil { return nil, errBad("保存失败,该日期可能已存在日报,请刷新后重试") } return buildItem(&report, 0), nil } if findErr != nil { return nil, findErr } if existing.Status == 1 { return nil, errBad("日报已提交并锁定,如需修改请先撤销提交") } resubmit := false if in.Submit { if existing.SubmitTime != nil { resubmit = true } else { resubmit = hasWithdrawHistory(u.Tid, existing.ID) } existing.SubmitTime = &now } existing.TodayDone = in.TodayDone existing.TomorrowPlan = in.TomorrowPlan existing.Issues = in.Issues existing.Attachments = attachmentJSON existing.Status = status existing.UserName = u.Name existing.OrgID = u.OrgID existing.OrgName = u.OrgName tx, err := models.Orm.Begin() if err != nil { return nil, err } if _, err := tx.Update(&existing, "today_done", "tomorrow_plan", "issues", "attachments", "status", "submit_time", "user_name", "org_id", "org_name"); err != nil { _ = tx.Rollback() return nil, err } if resubmit { history := models.OaDailyReportHistory{ Tid: u.Tid, ReportID: existing.ID, UserID: u.UID, UserName: u.Name, ReportDate: date, Action: 2, TodayDone: existing.TodayDone, TomorrowPlan: existing.TomorrowPlan, Issues: existing.Issues, Attachments: existing.Attachments, SubmitTime: existing.SubmitTime, } if _, err := tx.Insert(&history); err != nil { _ = tx.Rollback() return nil, err } } if err := tx.Commit(); err != nil { return nil, err } count := commentCount(u.Tid, existing.ID) return buildItem(&existing, count), nil } func ListMine(u *UserContext, q MineQuery) (*MineResult, error) { if q.Page < 1 { q.Page = 1 } if q.PageSize < 1 || q.PageSize > 100 { q.PageSize = 20 } base := models.Orm.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid). Filter("user_id", u.UID). Filter("is_deleted", 0) start, err := optionalDate(q.StartDate) if err != nil { return nil, err } end, err := optionalDate(q.EndDate) if err != nil { return nil, err } if start != "" { base = base.Filter("report_date__gte", start) } if end != "" { base = base.Filter("report_date__lte", end) } total, _ := base.Count() var rows []models.OaDailyReport if _, err := base.OrderBy("-report_date", "-id"). Limit(q.PageSize).Offset((q.Page - 1) * q.PageSize). All(&rows); err != nil { return nil, err } ids := make([]uint64, 0, len(rows)) for i := range rows { ids = append(ids, rows[i].ID) } counts := commentCounts(u.Tid, ids) list := make([]ReportItem, 0, len(rows)) for i := range rows { list = append(list, *buildItem(&rows[i], counts[rows[i].ID])) } summary, err := mineSummary(u) if err != nil { return nil, err } return &MineResult{List: list, Total: total, Summary: *summary}, nil } type CalendarDay struct { Date string `json:"date"` Status int8 `json:"status"` ID uint64 `json:"id"` } type CalendarResult struct { StartDate string `json:"start_date"` EndDate string `json:"end_date"` Total int `json:"total"` Submitted int `json:"submitted"` Days []CalendarDay `json:"days"` } func Calendar(u *UserContext, months int) (*CalendarResult, error) { if months < 1 || months > 12 { months = 3 } now := time.Now() monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()) start := monthStart.AddDate(0, -(months - 1), 0) end := monthStart.AddDate(0, 1, -1) startDate := start.Format("2006-01-02") endDate := end.Format("2006-01-02") var rows []models.OaDailyReport if _, err := models.Orm.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid). Filter("user_id", u.UID). Filter("is_deleted", 0). Filter("report_date__gte", startDate). Filter("report_date__lte", endDate). OrderBy("report_date"). All(&rows, "id", "report_date", "status"); err != nil { return nil, err } res := &CalendarResult{ StartDate: startDate, EndDate: endDate, Days: make([]CalendarDay, 0, len(rows)), } for i := range rows { res.Days = append(res.Days, CalendarDay{ Date: dateOnly(rows[i].ReportDate), Status: rows[i].Status, ID: rows[i].ID, }) if rows[i].Status == 1 { res.Submitted++ } } res.Total = len(rows) return res, nil } func mineSummary(u *UserContext) (*MineSummary, error) { now := time.Now() monthStart := now.Format("2006-01") + "-01" monthEnd := now.AddDate(0, 1, -1).Format("2006-01-02") out := &MineSummary{ TodayStatus: -1, WindowStart: WindowStart(), WindowEnd: Today(), } base := models.Orm.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid). Filter("user_id", u.UID). Filter("is_deleted", 0) out.MonthTotal = int(countWhere(base, "report_date__gte", monthStart, "report_date__lte", monthEnd)) out.MonthSubmitted = int(countWhere(base.Filter("status", 1), "report_date__gte", monthStart, "report_date__lte", monthEnd)) var today models.OaDailyReport if err := models.Orm.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid).Filter("user_id", u.UID). Filter("report_date", Today()). Filter("is_deleted", 0).One(&today); err == nil { out.TodayStatus = today.Status out.TodayReportID = today.ID } return out, nil } func GetDetail(u *UserContext, id uint64) (*Detail, error) { report, err := loadReport(u.Tid, id) if err != nil { return nil, err } if !canViewReport(u, report) { return nil, errForbidden("无权查看该日报") } comments := make([]Comment, 0) if report.Status == 1 { comments, err = loadComments(u, report.ID) if err != nil { return nil, err } } author := report.UserID == u.UID canEdit := author && report.Status == 0 && InWindow(report.ReportDate) canWithdraw := author && report.Status == 1 && InWindow(report.ReportDate) canDelete := report.Status == 0 && (u.IsAdmin || (author && InWindow(report.ReportDate))) histories := make([]HistoryItem, 0) if author || u.IsAdmin { histories, err = loadHistories(u.Tid, report.ID) if err != nil { histories = make([]HistoryItem, 0) } } return &Detail{ Report: *buildItem(report, len(comments)), Comments: comments, Histories: histories, IsAuthor: author, CanEdit: canEdit, CanDelete: canDelete, CanComment: report.Status == 1 && (author || u.IsAdmin || u.InTeamOrg(report.OrgID)), CanWithdraw: canWithdraw, }, nil } func Withdraw(u *UserContext, id uint64) (*ReportItem, error) { report, err := loadReport(u.Tid, id) if err != nil { return nil, err } if report.UserID != u.UID { return nil, errForbidden("只能撤销自己提交的日报") } if report.Status != 1 { return nil, errBad("日报尚未提交,无需撤销") } if !InWindow(report.ReportDate) { return nil, errBad(fmt.Sprintf("只能撤销最近%d天(%s 至 %s)的日报", BackfillDays, WindowStart(), Today())) } tx, err := models.Orm.Begin() if err != nil { return nil, err } history := models.OaDailyReportHistory{ Tid: u.Tid, ReportID: report.ID, UserID: u.UID, UserName: u.Name, ReportDate: report.ReportDate, Action: 1, TodayDone: report.TodayDone, TomorrowPlan: report.TomorrowPlan, Issues: report.Issues, Attachments: report.Attachments, SubmitTime: report.SubmitTime, } if _, err := tx.Insert(&history); err != nil { _ = tx.Rollback() return nil, err } if _, err := tx.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid).Filter("id", report.ID). Update(map[string]interface{}{"status": 0, "submit_time": nil}); err != nil { _ = tx.Rollback() return nil, err } if err := tx.Commit(); err != nil { return nil, err } report.Status = 0 count := commentCount(u.Tid, report.ID) return buildItem(report, count), nil } func Delete(u *UserContext, id uint64) error { report, err := loadReport(u.Tid, id) if err != nil { return err } if report.Status == 1 { return errBad("日报已提交并锁定,请先撤销提交后再删除") } author := report.UserID == u.UID if !author && !u.IsAdmin { return errForbidden("无权删除该日报") } if author && !u.IsAdmin && !InWindow(report.ReportDate) { return errBad(fmt.Sprintf("只能删除最近%d天(%s 至 %s)的日报", BackfillDays, WindowStart(), Today())) } now := time.Now() tx, err := models.Orm.Begin() if err != nil { return err } if _, err := tx.QueryTable(new(models.OaDailyReportComment)). Filter("tid", u.Tid).Filter("report_id", report.ID).Filter("is_deleted", 0). Update(map[string]interface{}{"is_deleted": 1, "delete_time": now}); err != nil { _ = tx.Rollback() return err } if _, err := tx.QueryTable(new(models.OaDailyReport)). Filter("tid", u.Tid).Filter("id", report.ID). Update(map[string]interface{}{"is_deleted": report.ID, "delete_time": now}); err != nil { _ = tx.Rollback() return err } return tx.Commit() } func AddComment(u *UserContext, reportID uint64, content string, replyToID uint64) (*Comment, error) { content = strings.TrimSpace(content) if content == "" { return nil, errBad("请输入评论内容") } if utf8.RuneCountInString(content) > 1000 { return nil, errBad("评论不能超过1000字") } report, err := loadReport(u.Tid, reportID) if err != nil { return nil, err } if !canViewReport(u, report) { return nil, errForbidden("无权评论该日报") } if report.Status != 1 { return nil, errBad("日报提交后才能评论") } replyName := "" if replyToID > 0 { var parent models.OaDailyReportComment if err := models.Orm.QueryTable(new(models.OaDailyReportComment)). Filter("tid", u.Tid).Filter("id", replyToID).Filter("report_id", reportID). Filter("is_deleted", 0). One(&parent); err != nil { return nil, errBad("回复的评论不存在") } replyName = parent.UserName } row := models.OaDailyReportComment{ Tid: u.Tid, ReportID: reportID, UserID: u.UID, UserName: u.Name, Content: content, ReplyToID: replyToID, ReplyToName: replyName, } if _, err := models.Orm.Insert(&row); err != nil { return nil, err } c := buildComment(u, &row) return &c, nil } func DeleteComment(u *UserContext, commentID uint64) error { var row models.OaDailyReportComment if err := models.Orm.QueryTable(new(models.OaDailyReportComment)). Filter("tid", u.Tid).Filter("id", commentID).Filter("is_deleted", 0).One(&row); err != nil { return errNotFound("评论不存在或已删除") } if row.UserID != u.UID && !u.IsAdmin { return errForbidden("只能删除自己的评论") } _, err := models.Orm.QueryTable(new(models.OaDailyReportComment)). Filter("tid", u.Tid).Filter("id", commentID).Filter("is_deleted", 0). Update(map[string]interface{}{"is_deleted": 1, "delete_time": time.Now()}) return err } func loadReport(tid, id uint64) (*models.OaDailyReport, error) { var report models.OaDailyReport if err := models.Orm.QueryTable(new(models.OaDailyReport)). Filter("tid", tid).Filter("id", id).Filter("is_deleted", 0).One(&report); err != nil { if err == orm.ErrNoRows { return nil, errNotFound("日报不存在或已删除") } return nil, err } report.ReportDate = dateOnly(report.ReportDate) return &report, nil } func canViewReport(u *UserContext, r *models.OaDailyReport) bool { if r.UserID == u.UID || u.IsAdmin { return true } if r.Status != 1 { return false } return u.InTeamOrg(r.OrgID) } func loadComments(u *UserContext, reportID uint64) ([]Comment, error) { var rows []models.OaDailyReportComment if _, err := models.Orm.QueryTable(new(models.OaDailyReportComment)). Filter("tid", u.Tid).Filter("report_id", reportID). Filter("is_deleted", 0). OrderBy("id").All(&rows); err != nil { return nil, err } out := make([]Comment, 0, len(rows)) for i := range rows { out = append(out, buildComment(u, &rows[i])) } if out == nil { out = []Comment{} } return out, nil } func buildComment(u *UserContext, r *models.OaDailyReportComment) Comment { return Comment{ ID: r.ID, UserID: r.UserID, UserName: r.UserName, Content: r.Content, ReplyToID: r.ReplyToID, ReplyToName: r.ReplyToName, CreateTime: r.CreateTime.Format("2006-01-02 15:04"), IsMine: r.UserID == u.UID, CanDelete: r.UserID == u.UID || u.IsAdmin, } } func hasWithdrawHistory(tid, reportID uint64) bool { n, err := models.Orm.QueryTable(new(models.OaDailyReportHistory)). Filter("tid", tid).Filter("report_id", reportID).Filter("action", 1). Count() return err == nil && n > 0 } func loadHistories(tid, reportID uint64) ([]HistoryItem, error) { var rows []models.OaDailyReportHistory if _, err := models.Orm.QueryTable(new(models.OaDailyReportHistory)). Filter("tid", tid).Filter("report_id", reportID). OrderBy("id").All(&rows); err != nil { return nil, err } out := make([]HistoryItem, 0, len(rows)) for i := range rows { out = append(out, buildHistory(&rows[i])) } return out, nil } func buildHistory(h *models.OaDailyReportHistory) HistoryItem { attachments := make([]Attachment, 0) if strings.TrimSpace(h.Attachments) != "" { _ = json.Unmarshal([]byte(h.Attachments), &attachments) } return HistoryItem{ ID: h.ID, Action: h.Action, UserName: h.UserName, TodayDone: h.TodayDone, TomorrowPlan: h.TomorrowPlan, Issues: h.Issues, Attachments: attachments, SubmitTime: formatTime(h.SubmitTime), CreateTime: h.CreateTime.Format("2006-01-02 15:04"), } } func buildItem(r *models.OaDailyReport, commentCount int) *ReportItem { attachments := make([]Attachment, 0) if strings.TrimSpace(r.Attachments) != "" { _ = json.Unmarshal([]byte(r.Attachments), &attachments) } return &ReportItem{ ID: r.ID, UserID: r.UserID, UserName: r.UserName, OrgID: r.OrgID, OrgName: r.OrgName, ReportDate: dateOnly(r.ReportDate), TodayDone: r.TodayDone, TomorrowPlan: r.TomorrowPlan, Issues: r.Issues, Attachments: attachments, Status: r.Status, SubmitTime: formatTime(r.SubmitTime), CreateTime: r.CreateTime.Format("2006-01-02 15:04"), UpdateTime: formatTime(r.UpdateTime), CommentCount: commentCount, } } func formatTime(t *time.Time) string { if t == nil || t.IsZero() { return "" } return t.Format("2006-01-02 15:04") } func normalizeAttachments(raw string) (string, []Attachment, error) { raw = strings.TrimSpace(raw) if raw == "" || raw == "null" { return "[]", []Attachment{}, nil } var items []Attachment if err := json.Unmarshal([]byte(raw), &items); err != nil { return "", nil, errBad("附件数据格式错误") } out := make([]Attachment, 0, len(items)) for _, it := range items { if strings.TrimSpace(it.URL) == "" { continue } if len(out) >= 20 { return "", nil, errBad("附件最多20个") } out = append(out, it) } data, _ := json.Marshal(out) return string(data), out, nil } func optionalDate(v string) (string, error) { v = strings.TrimSpace(v) if v == "" { return "", nil } return ParseDate(v) } func countWhere(qs orm.QuerySeter, kv ...string) int64 { for i := 0; i+1 < len(kv); i += 2 { qs = qs.Filter(kv[i], kv[i+1]) } n, _ := qs.Count() return n } func commentCount(tid, reportID uint64) int { counts := commentCounts(tid, []uint64{reportID}) return counts[reportID] } func commentCounts(tid uint64, ids []uint64) map[uint64]int { out := make(map[uint64]int, len(ids)) if len(ids) == 0 { return out } holders := make([]string, 0, len(ids)) args := make([]interface{}, 0, len(ids)+1) args = append(args, tid) for _, id := range ids { holders = append(holders, "?") args = append(args, id) } var rows []struct { ReportID uint64 `orm:"column(report_id)"` Cnt int `orm:"column(cnt)"` } sql := "SELECT report_id, COUNT(*) AS cnt FROM yz_backend_oa_daily_report_comment" + " WHERE tid = ? AND report_id IN (" + strings.Join(holders, ",") + ") AND is_deleted = 0 GROUP BY report_id" if _, err := models.Orm.Raw(sql, args...).QueryRows(&rows); err != nil { return out } for _, r := range rows { out[r.ReportID] = r.Cnt } return out }