package controllers import ( "sort" "time" "server/models" beego "github.com/beego/beego/v2/server/web" "server/pkg/identity" ) type BackendCrmBiddingReminderController struct { beego.Controller } type biddingReminderItem struct { BizType string `json:"biz_type"` BizID uint64 `json:"biz_id"` Kind string `json:"kind"` KindName string `json:"kind_name"` Title string `json:"title"` DueDate string `json:"due_date"` RemainDays int `json:"remain_days"` Level string `json:"level"` OwnerName string `json:"owner_user_name"` } func (c *BackendCrmBiddingReminderController) List() { claims, err := pipelineClaims(&c.Controller) if err != nil { code := identity.Status(err) pipelineErr(&c.Controller, code, code, err.Error()) return } tid := pipelineTenantID(claims) days, _ := c.GetInt("days", 7) if days <= 0 || days > 365 { days = 7 } now := time.Now() deadline := now.AddDate(0, 0, days) items := make([]biddingReminderItem, 0, 32) var bids []models.TenantCrmBiddingBid _, _ = models.Orm.QueryTable(new(models.TenantCrmBiddingBid)). Filter("tenant_id", tid). Filter("delete_time__isnull", true). Filter("status__in", 0, 1, 2, 3). All(&bids) for _, b := range bids { if !canAccessCrmRecord(claims, b.OwnerUserID, b.CreateUserID) { continue } items = append(items, pickBidReminders(b, now, deadline)...) } var tenders []models.TenantCrmBiddingTender _, _ = models.Orm.QueryTable(new(models.TenantCrmBiddingTender)). Filter("tenant_id", tid). Filter("delete_time__isnull", true). Filter("status__in", 0, 1, 2, 3, 4, 5). All(&tenders) for _, t := range tenders { if !canAccessCrmRecord(claims, t.OwnerUserID, t.CreateUserID) { continue } items = append(items, pickTenderReminders(t, now, deadline)...) } var quals []models.TenantCrmBiddingBidQualification _, _ = models.Orm.QueryTable(new(models.TenantCrmBiddingBidQualification)). Filter("tenant_id", tid). Filter("delete_time__isnull", true). All(&quals) for _, q := range quals { if q.ExpireDate == nil { continue } due := *q.ExpireDate if due.After(deadline) { continue } items = append(items, biddingReminderItem{ BizType: "qualification", BizID: q.ID, Kind: "expire", KindName: "资质到期", Title: q.QualificationName, DueDate: due.Format("2006-01-02"), RemainDays: daysBetween(now, due), Level: levelOf(daysBetween(now, due)), OwnerName: q.OwnerUserName, }) } sort.SliceStable(items, func(i, j int) bool { if items[i].RemainDays != items[j].RemainDays { return items[i].RemainDays < items[j].RemainDays } return items[i].DueDate < items[j].DueDate }) pipelineOk(&c.Controller, map[string]interface{}{ "list": items, "total": len(items), "days": days, }) } func pickBidReminders(b models.TenantCrmBiddingBid, now, deadline time.Time) []biddingReminderItem { out := make([]biddingReminderItem, 0, 3) add := func(kind, kindName string, due *time.Time) { if due == nil { return } d := *due if d.Before(now) || d.After(deadline) { return } out = append(out, biddingReminderItem{ BizType: "bid", BizID: b.ID, Kind: kind, KindName: kindName, Title: b.Title, DueDate: d.Format("2006-01-02 15:04:05"), RemainDays: daysBetween(now, d), Level: levelOf(daysBetween(now, d)), OwnerName: b.OwnerUserName, }) } add("register_end", "报名截止", b.RegisterEndDate) add("bid_end", "投标截止", b.BidEndDate) add("open_bid", "开标时间", b.OpenBidDate) return out } func pickTenderReminders(t models.TenantCrmBiddingTender, now, deadline time.Time) []biddingReminderItem { out := make([]biddingReminderItem, 0, 3) add := func(kind, kindName string, due *time.Time) { if due == nil { return } d := *due if d.Before(now) || d.After(deadline) { return } out = append(out, biddingReminderItem{ BizType: "tender", BizID: t.ID, Kind: kind, KindName: kindName, Title: t.Title, DueDate: d.Format("2006-01-02 15:04:05"), RemainDays: daysBetween(now, d), Level: levelOf(daysBetween(now, d)), OwnerName: t.OwnerUserName, }) } add("register_end", "报名截止", t.RegisterEndDate) add("bid_end", "投标截止", t.BidEndDate) add("open_bid", "开标时间", t.OpenBidDate) return out } func daysBetween(now, target time.Time) int { n := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) t := time.Date(target.Year(), target.Month(), target.Day(), 0, 0, 0, 0, target.Location()) return int(t.Sub(n).Hours() / 24) } func levelOf(days int) string { if days <= 1 { return "danger" } if days <= 3 { return "warning" } return "info" }