FundQ.cs 16 KB

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