using System; using System.Collections.Generic; using System.Windows.Forms; namespace MyAppPdf { /// /// 新建项目 窗口(简化版,不依赖企业信息)。 /// UI 布局见 新建项目.ui.cs,本文件只含交互逻辑。 /// public partial class 新建项目 : Form { // 价格格式化选项(与主项目 价格格式化.cs 保持一致) private static readonly string[] 小数位选项 = { "保留整数", "保留1位小数", "保留2位小数", "保留3位小数", "保留4位小数", "保留5位小数", "保留6位小数" }; private static readonly string[] 取整方式选项 = { "不取整", "四舍五入", "四舍五入到角", "四舍五入到元", "四舍五入到十元", "向上取整到元", "最近5元", "最近10元" }; private readonly string _dbPath; private readonly string _tableName; public 新建项目() : this(null, null) { } /// 带数据库路径的构造函数(用于自动生成项目编号)。 public 新建项目(string dbPath, string tableName) { InitializeComponent(); _dbPath = dbPath; _tableName = tableName; InitPriceFormatCombos(); this.Load += 新建项目_Load; } private void InitPriceFormatCombos() { cmb单台价格.Items.Clear(); cmb单台价格.Items.AddRange(小数位选项); cmb项目总价.Items.Clear(); cmb项目总价.Items.AddRange(小数位选项); comboBox1.Items.Clear(); comboBox1.Items.AddRange(取整方式选项); comboBox2.Items.Clear(); comboBox2.Items.AddRange(取整方式选项); } private void 新建项目_Load(object sender, EventArgs e) { FillAutoFields(); } /// 自动填充项目日期(当天)和项目编号(yyyyMMdd+3位序号)。 private void FillAutoFields() { DateTime today = DateTime.Today; txt项目日期.Text = today.ToString("yyyy-MM-dd"); string datePart = today.ToString("yyyyMMdd"); int seq = GetNextSeqForDate(datePart); txt项目编号.Text = datePart + seq.ToString("000"); txt项目编号.ReadOnly = true; txt项目编号.TabStop = false; txt项目编号.BackColor = System.Drawing.SystemColors.Control; } /// 查数据库当天最大序号+1,失败返回1。 private int GetNextSeqForDate(string datePart) { if (string.IsNullOrEmpty(datePart) || string.IsNullOrEmpty(_dbPath) || string.IsNullOrEmpty(_tableName) || !System.IO.File.Exists(_dbPath)) { return 1; } try { string connStr = "Data Source=" + _dbPath + ";Version=3;"; using (var conn = new System.Data.SQLite.SQLiteConnection(connStr)) { conn.Open(); string sql = "SELECT MAX(CAST(SUBSTR(项目编号, 9) AS INTEGER)) " + "FROM " + _tableName + " " + "WHERE SUBSTR(项目编号, 1, 8) = '" + datePart + "';"; using (var cmd = new System.Data.SQLite.SQLiteCommand(sql, conn)) { object result = cmd.ExecuteScalar(); if (result == null || result == DBNull.Value) return 1; int maxSeq; if (int.TryParse(result.ToString(), out maxSeq)) return maxSeq + 1; return 1; } } } catch { return 1; } } /// 收集对话框字段,以列名为键返回(供 Form3 写入数据库)。 public Dictionary GetInputValues() { var values = new Dictionary(); AddIfNotEmpty(values, "项目编号", txt项目编号.Text); AddIfNotEmpty(values, "项目日期", txt项目日期.Text); AddIfNotEmpty(values, "项目名称", txt项目名称.Text); AddIfNotEmpty(values, "客户名称", txt客户名称.Text); AddIfNotEmpty(values, "客户地址", txt客户地址.Text); AddIfNotEmpty(values, "联系人", txt联系人.Text); AddIfNotEmpty(values, "联系电话", txt联系电话.Text); AddIfNotEmpty(values, "备注", txt备注.Text); AddIfNotEmpty(values, "成套厂名称", txt成套厂名称.Text); AddIfNotEmpty(values, "成套厂地址", txt成套厂地址.Text); AddIfNotEmpty(values, "成套厂联系人", txt成套厂联系人.Text); AddIfNotEmpty(values, "成套厂联系电话", txt成套厂联系电话.Text); values["单台小数位"] = (cmb单台价格.SelectedItem ?? cmb单台价格.Text ?? "").ToString(); values["项目小数位"] = (cmb项目总价.SelectedItem ?? cmb项目总价.Text ?? "").ToString(); values["单台取整方式"] = (comboBox1.SelectedItem ?? comboBox1.Text ?? "").ToString(); values["项目取整方式"] = (comboBox2.SelectedItem ?? comboBox2.Text ?? "").ToString(); return values; } private static void AddIfNotEmpty(Dictionary dict, string key, string value) { if (!string.IsNullOrWhiteSpace(value)) dict[key] = value.Trim(); } } }