using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Text.Json; using System.Text.Json.Serialization; using System.Drawing.Text; using System.IO; namespace ddq { public partial class FundQ : Form { private string fundId; private string companyId; private int userId; private DataTable fundInfoTable; public FundQ(string fundId, string companyId, int userId) { InitializeComponent(); this.fundId = fundId; this.companyId = companyId; this.userId = userId; InitializeData(); } private void InitializeData() { DataTable dt = DataAccess.Get_fund_info(fundId, null); if (dt == null || dt.Rows.Count <= 0) return; this.lblFundName.Text = dt.Rows[0].Field("fund_short_name"); //this.lblMainCode.Text = "Code: " + dt.Rows[0].Field("register_number"); this.lblMainCode.Text = "Code: " + fundId; this.lblCategory.Text = "Category: " + dt.Rows[0].Field("strategy"); this.lblInceptionDate.Text = "Launched: " + dt.Rows[0].Field("inception_date")?.ToString("yyyy-MM-dd"); this.lblDomicile.Text = "Domicile: "; fundInfoTable = DataAccess.Get_dd_fund_info(fundId, null, 1); bool hasPM = false; DataTable dtPM; bool hasTER = false; DataTable dtTER; if (fundInfoTable != null && fundInfoTable.Rows.Count > 0) { string jsonString = fundInfoTable.Rows[0].Field("info").Trim(); JsonDocument document = JsonDocument.Parse(jsonString); JsonElement root = document.RootElement; if (root.ValueKind != JsonValueKind.Undefined) { // // General Info // this.txtInvestmentObjective.Text = Utility.Json2Text(root, "investmentObjective"); this.txtBenchmark.Text = Utility.Json2Text(root, "benchmark"); this.txtInvestmentPhilosophy.Text = Utility.Json2Text(root, "investmentPhilosophy"); JsonElement elmPM; hasPM = root.TryGetProperty("portfolioManagers", out elmPM); if (hasPM == true) { dtPM = Utility.Json2Table(elmPM); InitPortfolioManagerGrid(dtPM); } JsonElement elmTER; hasTER = root.TryGetProperty("ter", out elmTER); if (hasTER == true) { dtTER = Utility.Json2Table(elmTER); InitTERGrid(dtTER); } // // Process // this.chkAllowDerivatives.Checked = Utility.Json2Text(root, "allowDerivatives").ToLower() == "true" ? true : false; this.chkUseDerivatives.Checked = Utility.Json2Text(root, "useDerivatives").ToLower() == "true" ? true : false; this.chkDerivativesForEfficent.Checked = this.chkUseDerivatives.Checked == true ? Utility.Json2Text(root, "drvForEffPM").ToLower() == "true" ? true : false : false; if (this.chkUseDerivatives.Checked == true) { this.chkDerivativesForEfficent.Checked = Utility.Json2Text(root, "drvForEffPM").ToLower() == "true" ? true : false; this.chkDerivatesForHedging.Checked = Utility.Json2Text(root, "drvForHedging").ToLower() == "true" ? true : false; this.chkDerivativesForMarket.Checked = Utility.Json2Text(root, "drvForMktAcs").ToLower() == "true" ? true : false; this.chkDerivatesForLeverage.Checked = Utility.Json2Text(root, "drvForLeverage").ToLower() == "true" ? true : false; } this.chkPriceTarget.Checked = Utility.Json2Text(root, "priceTarget").ToLower() == "true" ? true : false; if (this.chkPriceTarget.Checked == true) { this.txtOverrunPriceTarget.Text = Utility.Json2Text(root, "overrunPriceTarget"); } } } if (hasPM == false) { dtPM = new DataTable(); // 没有基金经理数据时,初始化表 dtPM.Columns.Add("personnel_id", typeof(string)); dtPM.Columns.Add("name", typeof(string)); dtPM.Columns.Add("startDate", typeof(DateTime)); dtPM.Columns.Add("endDate", typeof(DateTime)); InitPortfolioManagerGrid(dtPM); } if (hasTER == false) { dtTER = new DataTable(); dtTER.Columns.Add("year", typeof(int)); dtTER.Columns.Add("ter", typeof(decimal)); int year = DateTime.Today.Year - 1; for (int i = 0; i < 5; i++) { DataRow row = dtTER.NewRow(); row["year"] = year - i; dtTER.Rows.Add(row); } InitTERGrid(dtTER); } } private void InitPortfolioManagerGrid(DataTable dt) { this.grdPortfolioManager.DataSource = dt; this.grdPortfolioManager.Columns["personnel_id"].Visible = false; this.grdPortfolioManager.Columns["name"].HeaderText = "Manager Name"; this.grdPortfolioManager.Columns["name"].DisplayIndex = 0; this.grdPortfolioManager.Columns["startDate"].HeaderText = "Start Date"; this.grdPortfolioManager.Columns["startDate"].DefaultCellStyle.Format = "d"; this.grdPortfolioManager.Columns["startDate"].DisplayIndex = 1; this.grdPortfolioManager.Columns["endDate"].HeaderText = "End Date"; this.grdPortfolioManager.Columns["endDate"].DefaultCellStyle.Format = "yyyy-MM-dd"; this.grdPortfolioManager.Columns["endDate"].DisplayIndex = 2; } private void InitTERGrid(DataTable dt) { this.grdTER.DataSource = dt; this.grdTER.Columns["year"].HeaderText = "Year"; this.grdTER.Columns["year"].ReadOnly = true; this.grdTER.Columns["year"].DisplayIndex = 0; this.grdTER.Columns["ter"].HeaderText = "Total Expense Ratio %"; this.grdTER.Columns["ter"].ValueType = typeof(decimal); this.grdTER.Columns["ter"].DefaultCellStyle.Format = "N3"; this.grdTER.Columns["ter"].DisplayIndex = 1; } private void FundQ_Load(object sender, EventArgs e) { } private void btnSaveGeneralInfo_Click(object sender, EventArgs e) { try { // 数据对象 var textData = new { fundId = this.fundId, investmentObjective = this.txtInvestmentObjective.Text, benchmark = this.txtBenchmark.Text, investmentPhilosophy = this.txtInvestmentPhilosophy.Text, portfolioManagers = Utility.DataTable2List((DataTable)this.grdPortfolioManager.DataSource), ter = Utility.DataTable2List((DataTable)this.grdTER.DataSource), allowDerivatives = this.chkAllowDerivatives.Checked, useDerivatives = this.chkUseDerivatives.Checked, drvForEffPM = this.chkDerivativesForEfficent.Checked, drvForHedging = this.chkDerivatesForHedging.Checked, drvForMktAcs = this.chkDerivativesForMarket.Checked, drvForLeverage = this.chkDerivatesForLeverage.Checked, priceTarget = this.chkPriceTarget.Checked, overrunPriceTarget = this.txtOverrunPriceTarget.Text, updateTime = DateTime.Now }; // 序列化选项 var options = new JsonSerializerOptions { WriteIndented = true, PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; string jsonString = JsonSerializer.Serialize(textData, options); //MessageBox.Show($"Save JSON: \n{jsonString}", "Json Result"); int ret = DataAccess.Set_dd_fund_info(this.fundId, DateTime.Today, jsonString, 1, 1, userId); if (ret == 1) { this.Close(); } } catch (Exception ex) { MessageBox.Show($"Error: {ex.Message}", "Error"); } } private void grdTER_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { string errMsg = "Please enter a number between 0 and 100, with up to 3 decimal digits"; if (grdTER.Columns[e.ColumnIndex].Name == "ter" && e.RowIndex >= 0) { // 先清除可能存在的旧错误提示 grdTER.Rows[e.RowIndex].ErrorText = ""; if (e.FormattedValue.ToString().Trim() == "") return; // 尝试将输入的值转换为小数 if (!decimal.TryParse(e.FormattedValue.ToString(), out decimal newValue)) { // 如果转换失败,说明输入的不是有效数字 e.Cancel = true; grdTER.Rows[e.RowIndex].ErrorText = errMsg; return; } // 验证数值范围(例如限制在0到100之间) if (newValue < 0 || newValue > 100) { e.Cancel = true; grdTER.Rows[e.RowIndex].ErrorText = errMsg; return; } // (可选) 自定义验证小数位数 string[] parts = e.FormattedValue.ToString().Split('.'); if (parts.Length > 1 && parts[1].Length > 3) // 检查小数点后的位数 { e.Cancel = true; grdTER.Rows[e.RowIndex].ErrorText = errMsg; } } } private void btnSaveFundProcess_Click(object sender, EventArgs e) { } private void chkUseDerivatives_CheckedChanged(object sender, EventArgs e) { bool isChecked = chkUseDerivatives.Checked; this.chkDerivativesForEfficent.Enabled = isChecked; this.chkDerivatesForHedging.Enabled = isChecked; this.chkDerivativesForMarket.Enabled = isChecked; this.chkDerivatesForLeverage.Enabled = isChecked; if (isChecked == false) { this.chkDerivativesForEfficent.Checked = false; this.chkDerivatesForHedging.Checked = false; this.chkDerivativesForMarket.Checked = false; this.chkDerivatesForLeverage.Checked = false; } } private void chkPriceTarget_CheckedChanged(object sender, EventArgs e) { bool isChecked = chkPriceTarget.Checked; if (!isChecked) { this.txtOverrunPriceTarget.Text = ""; } } private void btnUploadProcessDiagram_Click(object sender, EventArgs e) { this.ofdProcessDiagram.Title = "Upload Diagram File -- FAKE"; if (this.ofdProcessDiagram.ShowDialog() == DialogResult.OK) { // TODO: upload files to server. let's fake it for now string filePath = ofdProcessDiagram.FileName; string fileName = Path.GetFileName(filePath); try { } catch (Exception ex) { MessageBox.Show("File was not able to be uploaded:" + ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void btnAddManager_Click(object sender, EventArgs e) { using (PersonSelectionDialog dialog = new PersonSelectionDialog(companyId)) { dialog.StartPosition = FormStartPosition.CenterParent; DialogResult result = dialog.ShowDialog(this); if (result == DialogResult.OK && dialog.PersonnelId != "") { DataTable dt = (DataTable)this.grdPortfolioManager.DataSource; DataRow row = dt.NewRow(); row["personnel_id"] = dialog.PersonnelId; row["name"] = dialog.PersonnelName; row["startDate"] = dialog.StartDate.Date; dt.Rows.Add(row); } } } private void grdPortfolioManager_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { int rowIndex = e.RowIndex; int columnIndex = e.ColumnIndex; if (rowIndex < 0 || columnIndex < 0) { return; } // 先清除可能存在的旧错误提示 grdPortfolioManager.Rows[rowIndex].ErrorText = ""; // 如果转换失败,说明输入的不是有效日期 bool isPass = DateTime.TryParse(grdPortfolioManager.Rows[rowIndex].Cells["startDate"].Value.ToString(), out DateTime newDate); if (!isPass || newDate < new DateTime(1900, 1, 1) || newDate > DateTime.Today) { e.Cancel = true; grdPortfolioManager.Rows[columnIndex].ErrorText = "Please enter a proper start date"; return; } if (grdPortfolioManager.Rows[rowIndex].Cells["endDate"].Value.ToString() == "") return; isPass = DateTime.TryParse(grdPortfolioManager.Rows[rowIndex].Cells["endDate"].Value.ToString(), out newDate); if (!isPass || newDate < new DateTime(1900, 1, 1) || newDate > DateTime.Today) { e.Cancel = true; grdPortfolioManager.Rows[columnIndex].ErrorText = "Please enter a proper end date"; ; return; } } private void grdPortfolioManager_Validating(object sender, CancelEventArgs e) { } } }