FundQ.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Text.Json;
  11. using System.Text.Json.Serialization;
  12. using System.Drawing.Text;
  13. using System.IO;
  14. namespace ddq
  15. {
  16. public partial class FundQ : Form
  17. {
  18. private string fundId;
  19. private string companyId;
  20. private int userId;
  21. private DataTable fundInfoTable;
  22. public FundQ(string fundId, string companyId, int userId)
  23. {
  24. InitializeComponent();
  25. this.fundId = fundId;
  26. this.companyId = companyId;
  27. this.userId = userId;
  28. InitializeData();
  29. }
  30. private void InitializeData()
  31. {
  32. DataTable dt = DataAccess.Get_fund_info(fundId, null);
  33. if (dt == null || dt.Rows.Count <= 0) return;
  34. this.lblFundName.Text = dt.Rows[0].Field<string>("fund_short_name");
  35. //this.lblMainCode.Text = "Code: " + dt.Rows[0].Field<string>("register_number");
  36. this.lblMainCode.Text = "Code: " + fundId;
  37. this.lblCategory.Text = "Category: " + dt.Rows[0].Field<string>("strategy");
  38. this.lblInceptionDate.Text = "Launched: " + dt.Rows[0].Field<DateTime?>("inception_date")?.ToString("yyyy-MM-dd");
  39. this.lblDomicile.Text = "Domicile: ";
  40. fundInfoTable = DataAccess.Get_dd_fund_info(fundId, null, 1);
  41. bool hasPM = false;
  42. DataTable dtPM;
  43. bool hasTER = false;
  44. DataTable dtTER;
  45. if (fundInfoTable != null && fundInfoTable.Rows.Count > 0)
  46. {
  47. string jsonString = fundInfoTable.Rows[0].Field<string>("info").Trim();
  48. JsonDocument document = JsonDocument.Parse(jsonString);
  49. JsonElement root = document.RootElement;
  50. if (root.ValueKind != JsonValueKind.Undefined)
  51. {
  52. //
  53. // General Info
  54. //
  55. this.txtInvestmentObjective.Text = Utility.Json2Text(root, "investmentObjective");
  56. this.txtBenchmark.Text = Utility.Json2Text(root, "benchmark");
  57. this.txtInvestmentPhilosophy.Text = Utility.Json2Text(root, "investmentPhilosophy");
  58. JsonElement elmPM;
  59. hasPM = root.TryGetProperty("portfolioManagers", out elmPM);
  60. if (hasPM == true)
  61. {
  62. dtPM = Utility.Json2Table(elmPM);
  63. InitPortfolioManagerGrid(dtPM);
  64. }
  65. JsonElement elmTER;
  66. hasTER = root.TryGetProperty("ter", out elmTER);
  67. if (hasTER == true)
  68. {
  69. dtTER = Utility.Json2Table(elmTER);
  70. InitTERGrid(dtTER);
  71. }
  72. //
  73. // Process
  74. //
  75. this.chkAllowDerivatives.Checked = Utility.Json2Text(root, "allowDerivatives").ToLower() == "true" ? true : false;
  76. this.chkUseDerivatives.Checked = Utility.Json2Text(root, "useDerivatives").ToLower() == "true" ? true : false;
  77. this.chkDerivativesForEfficent.Checked = this.chkUseDerivatives.Checked == true ? Utility.Json2Text(root, "drvForEffPM").ToLower() == "true" ? true : false : false;
  78. if (this.chkUseDerivatives.Checked == true)
  79. {
  80. this.chkDerivativesForEfficent.Checked = Utility.Json2Text(root, "drvForEffPM").ToLower() == "true" ? true : false;
  81. this.chkDerivatesForHedging.Checked = Utility.Json2Text(root, "drvForHedging").ToLower() == "true" ? true : false;
  82. this.chkDerivativesForMarket.Checked = Utility.Json2Text(root, "drvForMktAcs").ToLower() == "true" ? true : false;
  83. this.chkDerivatesForLeverage.Checked = Utility.Json2Text(root, "drvForLeverage").ToLower() == "true" ? true : false;
  84. }
  85. this.chkPriceTarget.Checked = Utility.Json2Text(root, "priceTarget").ToLower() == "true" ? true : false;
  86. if (this.chkPriceTarget.Checked == true)
  87. {
  88. this.txtOverrunPriceTarget.Text = Utility.Json2Text(root, "overrunPriceTarget");
  89. }
  90. }
  91. }
  92. if (hasPM == false)
  93. {
  94. dtPM = new DataTable();
  95. // 没有基金经理数据时,初始化表
  96. dtPM.Columns.Add("personnel_id", typeof(string));
  97. dtPM.Columns.Add("name", typeof(string));
  98. dtPM.Columns.Add("startDate", typeof(DateTime));
  99. dtPM.Columns.Add("endDate", typeof(DateTime));
  100. InitPortfolioManagerGrid(dtPM);
  101. }
  102. if (hasTER == false)
  103. {
  104. dtTER = new DataTable();
  105. dtTER.Columns.Add("year", typeof(int));
  106. dtTER.Columns.Add("ter", typeof(decimal));
  107. int year = DateTime.Today.Year - 1;
  108. for (int i = 0; i < 5; i++)
  109. {
  110. DataRow row = dtTER.NewRow();
  111. row["year"] = year - i;
  112. dtTER.Rows.Add(row);
  113. }
  114. InitTERGrid(dtTER);
  115. }
  116. }
  117. private void InitPortfolioManagerGrid(DataTable dt)
  118. {
  119. this.grdPortfolioManager.DataSource = dt;
  120. this.grdPortfolioManager.Columns["personnel_id"].Visible = false;
  121. this.grdPortfolioManager.Columns["name"].HeaderText = "Manager Name";
  122. this.grdPortfolioManager.Columns["name"].DisplayIndex = 0;
  123. this.grdPortfolioManager.Columns["startDate"].HeaderText = "Start Date";
  124. this.grdPortfolioManager.Columns["startDate"].DefaultCellStyle.Format = "d";
  125. this.grdPortfolioManager.Columns["startDate"].DisplayIndex = 1;
  126. this.grdPortfolioManager.Columns["endDate"].HeaderText = "End Date";
  127. this.grdPortfolioManager.Columns["endDate"].DefaultCellStyle.Format = "yyyy-MM-dd";
  128. this.grdPortfolioManager.Columns["endDate"].DisplayIndex = 2;
  129. }
  130. private void InitTERGrid(DataTable dt)
  131. {
  132. this.grdTER.DataSource = dt;
  133. this.grdTER.Columns["year"].HeaderText = "Year";
  134. this.grdTER.Columns["year"].ReadOnly = true;
  135. this.grdTER.Columns["year"].DisplayIndex = 0;
  136. this.grdTER.Columns["ter"].HeaderText = "Total Expense Ratio %";
  137. this.grdTER.Columns["ter"].ValueType = typeof(decimal);
  138. this.grdTER.Columns["ter"].DefaultCellStyle.Format = "N3";
  139. this.grdTER.Columns["ter"].DisplayIndex = 1;
  140. }
  141. private void FundQ_Load(object sender, EventArgs e)
  142. {
  143. }
  144. private void btnSaveGeneralInfo_Click(object sender, EventArgs e)
  145. {
  146. try
  147. {
  148. // 数据对象
  149. var textData = new
  150. {
  151. fundId = this.fundId,
  152. investmentObjective = this.txtInvestmentObjective.Text,
  153. benchmark = this.txtBenchmark.Text,
  154. investmentPhilosophy = this.txtInvestmentPhilosophy.Text,
  155. portfolioManagers = Utility.DataTable2List((DataTable)this.grdPortfolioManager.DataSource),
  156. ter = Utility.DataTable2List((DataTable)this.grdTER.DataSource),
  157. allowDerivatives = this.chkAllowDerivatives.Checked,
  158. useDerivatives = this.chkUseDerivatives.Checked,
  159. drvForEffPM = this.chkDerivativesForEfficent.Checked,
  160. drvForHedging = this.chkDerivatesForHedging.Checked,
  161. drvForMktAcs = this.chkDerivativesForMarket.Checked,
  162. drvForLeverage = this.chkDerivatesForLeverage.Checked,
  163. priceTarget = this.chkPriceTarget.Checked,
  164. overrunPriceTarget = this.txtOverrunPriceTarget.Text,
  165. updateTime = DateTime.Now
  166. };
  167. // 序列化选项
  168. var options = new JsonSerializerOptions
  169. {
  170. WriteIndented = true,
  171. PropertyNamingPolicy = JsonNamingPolicy.CamelCase
  172. };
  173. string jsonString = JsonSerializer.Serialize(textData, options);
  174. //MessageBox.Show($"Save JSON: \n{jsonString}", "Json Result");
  175. int ret = DataAccess.Set_dd_fund_info(this.fundId, DateTime.Today, jsonString, 1, 1, userId);
  176. if (ret == 1)
  177. {
  178. this.Close();
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. MessageBox.Show($"Error: {ex.Message}", "Error");
  184. }
  185. }
  186. private void grdTER_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  187. {
  188. string errMsg = "Please enter a number between 0 and 100, with up to 3 decimal digits";
  189. if (grdTER.Columns[e.ColumnIndex].Name == "ter" && e.RowIndex >= 0)
  190. {
  191. // 先清除可能存在的旧错误提示
  192. grdTER.Rows[e.RowIndex].ErrorText = "";
  193. if (e.FormattedValue.ToString().Trim() == "") return;
  194. // 尝试将输入的值转换为小数
  195. if (!decimal.TryParse(e.FormattedValue.ToString(), out decimal newValue))
  196. {
  197. // 如果转换失败,说明输入的不是有效数字
  198. e.Cancel = true;
  199. grdTER.Rows[e.RowIndex].ErrorText = errMsg;
  200. return;
  201. }
  202. // 验证数值范围(例如限制在0到100之间)
  203. if (newValue < 0 || newValue > 100)
  204. {
  205. e.Cancel = true;
  206. grdTER.Rows[e.RowIndex].ErrorText = errMsg;
  207. return;
  208. }
  209. // (可选) 自定义验证小数位数
  210. string[] parts = e.FormattedValue.ToString().Split('.');
  211. if (parts.Length > 1 && parts[1].Length > 3) // 检查小数点后的位数
  212. {
  213. e.Cancel = true;
  214. grdTER.Rows[e.RowIndex].ErrorText = errMsg;
  215. }
  216. }
  217. }
  218. private void btnSaveFundProcess_Click(object sender, EventArgs e)
  219. {
  220. }
  221. private void chkUseDerivatives_CheckedChanged(object sender, EventArgs e)
  222. {
  223. bool isChecked = chkUseDerivatives.Checked;
  224. this.chkDerivativesForEfficent.Enabled = isChecked;
  225. this.chkDerivatesForHedging.Enabled = isChecked;
  226. this.chkDerivativesForMarket.Enabled = isChecked;
  227. this.chkDerivatesForLeverage.Enabled = isChecked;
  228. if (isChecked == false)
  229. {
  230. this.chkDerivativesForEfficent.Checked = false;
  231. this.chkDerivatesForHedging.Checked = false;
  232. this.chkDerivativesForMarket.Checked = false;
  233. this.chkDerivatesForLeverage.Checked = false;
  234. }
  235. }
  236. private void chkPriceTarget_CheckedChanged(object sender, EventArgs e)
  237. {
  238. bool isChecked = chkPriceTarget.Checked;
  239. if (!isChecked) { this.txtOverrunPriceTarget.Text = ""; }
  240. }
  241. private void btnUploadProcessDiagram_Click(object sender, EventArgs e)
  242. {
  243. this.ofdProcessDiagram.Title = "Upload Diagram File -- FAKE";
  244. if (this.ofdProcessDiagram.ShowDialog() == DialogResult.OK)
  245. {
  246. // TODO: upload files to server. let's fake it for now
  247. string filePath = ofdProcessDiagram.FileName;
  248. string fileName = Path.GetFileName(filePath);
  249. try
  250. {
  251. }
  252. catch (Exception ex)
  253. {
  254. MessageBox.Show("File was not able to be uploaded:" + ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
  255. }
  256. }
  257. }
  258. private void btnAddManager_Click(object sender, EventArgs e)
  259. {
  260. using (PersonSelectionDialog dialog = new PersonSelectionDialog(companyId))
  261. {
  262. dialog.StartPosition = FormStartPosition.CenterParent;
  263. DialogResult result = dialog.ShowDialog(this);
  264. if (result == DialogResult.OK && dialog.PersonnelId != "")
  265. {
  266. DataTable dt = (DataTable)this.grdPortfolioManager.DataSource;
  267. DataRow row = dt.NewRow();
  268. row["personnel_id"] = dialog.PersonnelId;
  269. row["name"] = dialog.PersonnelName;
  270. row["startDate"] = dialog.StartDate.Date;
  271. dt.Rows.Add(row);
  272. }
  273. }
  274. }
  275. private void grdPortfolioManager_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
  276. {
  277. int rowIndex = e.RowIndex;
  278. int columnIndex = e.ColumnIndex;
  279. if (rowIndex < 0 || columnIndex < 0) { return; }
  280. // 先清除可能存在的旧错误提示
  281. grdPortfolioManager.Rows[rowIndex].ErrorText = "";
  282. // 如果转换失败,说明输入的不是有效日期
  283. bool isPass = DateTime.TryParse(grdPortfolioManager.Rows[rowIndex].Cells["startDate"].Value.ToString(), out DateTime newDate);
  284. if (!isPass || newDate < new DateTime(1900, 1, 1) || newDate > DateTime.Today)
  285. {
  286. e.Cancel = true;
  287. grdPortfolioManager.Rows[columnIndex].ErrorText = "Please enter a proper start date";
  288. return;
  289. }
  290. if (grdPortfolioManager.Rows[rowIndex].Cells["endDate"].Value.ToString() == "") return;
  291. isPass = DateTime.TryParse(grdPortfolioManager.Rows[rowIndex].Cells["endDate"].Value.ToString(), out newDate);
  292. if (!isPass || newDate < new DateTime(1900, 1, 1) || newDate > DateTime.Today)
  293. {
  294. e.Cancel = true;
  295. grdPortfolioManager.Rows[columnIndex].ErrorText = "Please enter a proper end date"; ;
  296. return;
  297. }
  298. }
  299. private void grdPortfolioManager_Validating(object sender, CancelEventArgs e)
  300. {
  301. }
  302. }
  303. }