using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Lskj.Business.Impl; using Lskj.Core; using Lskj.Util; namespace Lskj.Control { public partial class FrmAnnouncement : BaseForm { private const string AnnouncementSql = "select * from p_ErpProUpdateInfo where AnnouncementDisplay=1 order by updateDate desc"; private Panel bottomPanel; private DevExpress.XtraEditors.SimpleButton btnConfirm; public FrmAnnouncement() { InitializeComponent(); InitializeAnnouncementForm(); this.Load += FrmAnnouncement_Load; this.Shown += FrmAnnouncement_Shown; } private void InitializeAnnouncementForm() { this.FormBorderStyle = FormBorderStyle.Sizable; this.MaximizeBox = true; this.MinimizeBox = true; this.MinimumSize = new Size(500, 300); this.txtEdit.ReadOnly = true; this.txtEdit.Properties.ScrollBars = ScrollBars.Vertical; bottomPanel = new Panel(); bottomPanel.Dock = DockStyle.Bottom; bottomPanel.Height = 50; btnConfirm = new DevExpress.XtraEditors.SimpleButton(); btnConfirm.Text = "确定"; btnConfirm.Size = new Size(90, 28); btnConfirm.Click += BtnConfirm_Click; bottomPanel.Controls.Add(btnConfirm); bottomPanel.Resize += BottomPanel_Resize; this.Controls.Add(bottomPanel); bottomPanel.BringToFront(); CenterConfirmButton(); } private void FrmAnnouncement_Load(object sender, EventArgs e) { try { DataTable table = SqlHelper.ExecuteDataTable(AnnouncementSql); this.txtEdit.Text = BuildAnnouncementText(table); this.txtEdit.SelectionStart = 0; this.txtEdit.SelectionLength = 0; } catch (Exception ex) { string message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(message, ex.Message); LogHelper.Instance.WriteError(ex); } } private void FrmAnnouncement_Shown(object sender, EventArgs e) { if (btnConfirm != null) { btnConfirm.Focus(); } } private void BottomPanel_Resize(object sender, EventArgs e) { CenterConfirmButton(); } private void CenterConfirmButton() { if (bottomPanel == null || btnConfirm == null) return; btnConfirm.Location = new Point( (bottomPanel.ClientSize.Width - btnConfirm.Width) / 2, (bottomPanel.ClientSize.Height - btnConfirm.Height) / 2); } private void BtnConfirm_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.OK; this.Close(); } private string BuildAnnouncementText(DataTable table) { if (table == null || table.Rows.Count == 0) return string.Empty; // 按日期分组(SQL已按updateDate desc排序,分组后日期自然从大到小) var dateGroups = new List>>(); DateTime? currentDate = null; List currentGroup = null; foreach (DataRow row in table.Rows) { DateTime updateDate; if (!DateTime.TryParse(row["updateDate"] + "", out updateDate)) continue; DateTime dateOnly = updateDate.Date; string content = row["verDesp"] + ""; if (currentDate != dateOnly) { currentDate = dateOnly; currentGroup = new List(); dateGroups.Add(new KeyValuePair>(dateOnly, currentGroup)); } currentGroup.Add(content); } StringBuilder text = new StringBuilder(); foreach (var group in dateGroups) { if (text.Length > 0) text.AppendLine(); text.Append("时间:"); text.AppendLine(group.Key.ToString("yyyy-MM-dd")); for (int i = 0; i < group.Value.Count; i++) { text.AppendLine((i + 1) + ". " + group.Value[i]); } } return text.ToString(); } private string FormatUpdateDate(object value) { if (value == null || value == DBNull.Value) return string.Empty; DateTime updateDate; if (DateTime.TryParse(value + "", out updateDate)) { return updateDate.ToString("yyyy-MM-dd"); } return value + ""; } } }