DataManager.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Threading.Tasks;
  11. using System.Windows.Automation.Peers;
  12. using System.Windows.Forms;
  13. using System.Windows.Forms.DataVisualization.Charting;
  14. using static DataManager.DataAccess;
  15. using static DataManager.UIConstants;
  16. namespace DataManager
  17. {
  18. public partial class frmDataManager : Form
  19. {
  20. private DateTime defaultDatetime = DateTime.Now.AddDays(-30);
  21. private DataTable collectionTaskTable = new DataTable();
  22. private DataTable contactTaskTable;
  23. private DataTable orphanFundTable;
  24. private DataTable companyCoverageTable;
  25. private DataTable authorizedCompanyTable;
  26. private DataTable fundCoverageTable;
  27. private DataTable authorizedFundTable;
  28. private DataTable companyDataTable;
  29. private DataTable fundDataTable;
  30. private static int userId;
  31. public static int UserId { get => userId; set => userId = value; }
  32. public frmDataManager(int userid)
  33. {
  34. UserId = userid;
  35. InitializeComponent();
  36. InitializeData();
  37. }
  38. private void InitializeData()
  39. {
  40. #region Contact Task Tab
  41. DataTable dt_dm_user_1 = DataAccess.Get_dm_user(null, 1);
  42. dt_dm_user_1.Rows.Add(DataAccess.DM_NULL, 1, "全部");
  43. cmbContactor.DataSource = dt_dm_user_1;
  44. cmbContactor.DisplayMember = "username";
  45. cmbContactor.ValueMember = "userid";
  46. cmbContactor.SelectedValue = UserId;
  47. dtpContactTaskDate.Value = defaultDatetime;
  48. BindingSource bs4 = new BindingSource();
  49. Dictionary<int, string> d4 = new Dictionary<int, string>(ContactTaskType);
  50. d4.Add(DM_NULL, "全部");
  51. bs4.DataSource = d4;
  52. cmbContactTaskType.DataSource = bs4;
  53. cmbContactTaskType.DisplayMember = "Value";
  54. cmbContactTaskType.ValueMember = "Key";
  55. cmbContactTaskType.SelectedValue = (int)DM_NULL;
  56. BindingSource bs = new BindingSource();
  57. Dictionary<int, string> d = new Dictionary<int, string>(ContactTaskStatus);
  58. d.Add(DM_NULL, "全部");
  59. bs.DataSource = d;
  60. cmbContactTaskStatus.DataSource = bs;
  61. cmbContactTaskStatus.DisplayMember = "Value";
  62. cmbContactTaskStatus.ValueMember = "Key";
  63. cmbContactTaskStatus.SelectedValue = (int)DM_NULL;
  64. BindingSource bs2 = new BindingSource();
  65. Dictionary<int, string> d2 = new Dictionary<int, string>(TaskPriority);
  66. d2.Add(DM_NULL, "全部");
  67. bs2.DataSource = d2;
  68. cmbContactPriority.DataSource = bs2;
  69. cmbContactPriority.DisplayMember = "Value";
  70. cmbContactPriority.ValueMember = "Key";
  71. cmbContactPriority.SelectedValue = (int)DM_NULL;
  72. BindingSource bs3 = new BindingSource();
  73. Dictionary<int, string> d3 = new Dictionary<int, string>(CompanyAssetSize);
  74. d3.Add(DM_NULL, "全部");
  75. bs3.DataSource = d3;
  76. cmbContactCompanySize.DataSource = bs3;
  77. cmbContactCompanySize.DisplayMember = "Value";
  78. cmbContactCompanySize.ValueMember = "Key";
  79. cmbContactCompanySize.SelectedValue = (int)DM_NULL;
  80. dtpContactFollowUpDate.ShowCheckBox = true;
  81. dtpContactFollowUpDate.Checked = false;
  82. #endregion
  83. #region Collection Task Tab
  84. DataTable dt_dm_user_2 = DataAccess.Get_dm_user(null, 2);
  85. dt_dm_user_2.Rows.Add(DataAccess.DM_NULL, 2, "全部");
  86. cmbCollector.DataSource = dt_dm_user_2;
  87. cmbCollector.DisplayMember = "username";
  88. cmbCollector.ValueMember = "userid";
  89. cmbCollector.SelectedValue = UserId;
  90. dtpCollectionTaskDate.Value = defaultDatetime;
  91. BindingSource bs5 = new BindingSource();
  92. Dictionary<int, string> d5 = new Dictionary<int, string>(CollectionTaskType);
  93. d5.Add(DM_NULL, "全部");
  94. bs5.DataSource = d5;
  95. cmbCollectionTaskType.DataSource = bs5;
  96. cmbCollectionTaskType.DisplayMember = "Value";
  97. cmbCollectionTaskType.ValueMember = "Key";
  98. cmbCollectionTaskType.SelectedValue = (int)DM_NULL;
  99. BindingSource bs6 = new BindingSource();
  100. Dictionary<int, string> d6 = new Dictionary<int, string>(CollectionTaskStatus);
  101. d6.Add(DM_NULL, "全部");
  102. bs6.DataSource = d6;
  103. cmbCollectionTaskStatus.DataSource = bs6;
  104. cmbCollectionTaskStatus.DisplayMember = "Value";
  105. cmbCollectionTaskStatus.ValueMember = "Key";
  106. cmbCollectionTaskStatus.SelectedValue = (int)DM_NULL;
  107. #endregion
  108. }
  109. #region Contact Tab
  110. private void LoadContactTaskGrid()
  111. {
  112. ContactTask task = CreateContactTaskFromUI();
  113. int? userid = task.UserId;
  114. if (userid == DM_NULL) userid = null;
  115. short? taskType = task.TaskType;
  116. if(taskType == DM_NULL) taskType = null;
  117. sbyte? isvalid = task.Isvalid;
  118. if(isvalid == DM_NULL) isvalid = null;
  119. sbyte? priority = task.Priority;
  120. if (priority == DM_NULL) priority = null;
  121. sbyte? companyAssetSize = task.CompanyAssetSize;
  122. if(companyAssetSize == DM_NULL) companyAssetSize = null;
  123. contactTaskTable = DataAccess.Get_dm_contact_task(userid, null, task.CompanyId, task.CompanyShortName,
  124. task.TaskDate, taskType, isvalid, priority,
  125. task.FollowUpDate, companyAssetSize);
  126. grdContactTask.DataSource = contactTaskTable;
  127. grdContactTask.Columns["task_id"].HeaderText = "ID";
  128. grdContactTask.Columns["company_short_name"].HeaderText = "公司";
  129. grdContactTask.Columns["task_date"].HeaderText = "日期";
  130. grdContactTask.Columns["task_type_name"].HeaderText = "任务";
  131. grdContactTask.Columns["isvalid_name"].HeaderText = "状态";
  132. grdContactTask.Columns["priority_name"].HeaderText = "优先级";
  133. grdContactTask.Columns["follow_up_date"].HeaderText = "跟进日";
  134. grdContactTask.Columns["company_asset_size_name"].HeaderText = "规模";
  135. grdContactTask.Columns["creator_name"].HeaderText = "创建";
  136. grdContactTask.Columns["updater_name"].HeaderText = "更新";
  137. grdContactTask.Columns["updatetime"].HeaderText = "更新时间";
  138. grdContactTask.Columns["company_id"].Visible = false;
  139. grdContactTask.Columns["task_type"].Visible = false;
  140. grdContactTask.Columns["isvalid"].Visible = false;
  141. grdContactTask.Columns["priority"].Visible = false;
  142. grdContactTask.Columns["company_asset_size"].Visible = false;
  143. grdContactTask.Columns["creatorid"].Visible = false;
  144. grdContactTask.Columns["createtime"].Visible = false;
  145. grdContactTask.Columns["updaterid"].Visible = false;
  146. grdContactTask.Columns["company_short_name"].DefaultCellStyle.ForeColor = Color.DodgerBlue;
  147. grdContactTask.Columns["company_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  148. lblContactTaskCount.Text = contactTaskTable.Rows.Count.ToString();
  149. }
  150. private ContactTask CreateContactTaskFromUI()
  151. {
  152. ContactTask task = new ContactTask();
  153. task.UserId = int.Parse(cmbContactor.SelectedValue.ToString());
  154. // 支持输 company_id或简称关键字的搜索
  155. string keywrod = txtContactCompany.Text.Trim();
  156. if (keywrod == String.Empty)
  157. {
  158. task.CompanyId = null;
  159. task.CompanyShortName = null;
  160. } else if (keywrod.Length == 10 && keywrod.ToUpper().Substring(0, 2) == "CO")
  161. {
  162. task.CompanyId = keywrod.ToUpper();
  163. task.CompanyShortName = null;
  164. } else
  165. {
  166. task.CompanyId = null;
  167. task.CompanyShortName = keywrod;
  168. }
  169. task.TaskDate = DateTime.Parse(dtpContactTaskDate.Text.ToString());
  170. task.TaskType = short.Parse(cmbContactTaskType.SelectedValue.ToString());
  171. task.Isvalid = sbyte.Parse(cmbContactTaskStatus.SelectedValue.ToString());
  172. task.Priority = sbyte.Parse(cmbContactPriority.SelectedValue.ToString());
  173. task.CompanyAssetSize = sbyte.Parse(cmbContactCompanySize.SelectedValue.ToString());
  174. if (dtpContactFollowUpDate.Checked == true)
  175. {
  176. task.FollowUpDate = DateTime.Parse(dtpContactFollowUpDate.Text.ToString());
  177. } else {
  178. task.FollowUpDate = null;
  179. };
  180. return task;
  181. }
  182. private void btnContactSearch_Click(object sender, EventArgs e)
  183. {
  184. LoadContactTaskGrid();
  185. }
  186. private void grdContactTask_CellClick(object sender, DataGridViewCellEventArgs e)
  187. {
  188. int rowIndex = e.RowIndex;
  189. int columnIndex = e.ColumnIndex;
  190. if (columnIndex < 0 || rowIndex < 0) return;
  191. DataRow row = contactTaskTable.Rows[rowIndex];
  192. if (columnIndex == grdContactTask.Columns["company_short_name"].Index)
  193. // 弹出公司页面
  194. {
  195. UICompany frmCompany = new UICompany(row.Field<string>("company_id"), UserId);
  196. frmCompany.Show();
  197. }
  198. else
  199. // 弹出联系任务编辑框
  200. {
  201. ContactTask task = new ContactTask(UserId, row.Field<int>("task_id"), row.Field<string>("company_id"), row.Field<string>("company_short_name"),
  202. row.Field<DateTime>("task_date"), row.Field<short>("task_type"), row.Field<sbyte>("isvalid"), row.Field<sbyte>("priority"),
  203. row.Field<DateTime?>("follow_up_date"), row.Field<sbyte?>("company_asset_size"),
  204. row.Field<int?>("creatorid"), row.Field<string>("creator_name"), row.Field<DateTime?>("createtime"),
  205. row.Field<int?>("updaterid"), row.Field<string>("updater_name"), row.Field<DateTime?>("updatetime"));
  206. if (task != null)
  207. {
  208. frmContactTask ctk = new frmContactTask(task);
  209. ctk.ShowDialog();
  210. // 刷新 grid 数据
  211. if (ctk.DialogResult == DialogResult.OK)
  212. {
  213. LoadContactTaskGrid();
  214. }
  215. }
  216. }
  217. }
  218. private void btnAddContactTask_Click(object sender, EventArgs e)
  219. {
  220. frmContactTask ctk = new frmContactTask(UserId);
  221. ctk.ShowDialog();
  222. // 刷新 grid 数据
  223. if (ctk.DialogResult == DialogResult.OK)
  224. {
  225. LoadContactTaskGrid();
  226. }
  227. }
  228. #endregion
  229. #region Collection Tab
  230. private void LoadCollectionTaskGrid()
  231. {
  232. CollectionTask task = CreatecCollectionTaskFromUI();
  233. int? userId = task.UserId;
  234. if (userId == DM_NULL) userId = null;
  235. short? taskType = task.TaskType;
  236. if (taskType == DM_NULL) taskType = null;
  237. sbyte? isvalid = task.Isvalid;
  238. if (isvalid == DM_NULL) isvalid = null;
  239. collectionTaskTable = DataAccess.Get_dm_collection_task(userId, null, task.ProviderId, task.ProviderName,
  240. task.TaskDate, taskType, task.EntityId, task.EntityName,
  241. isvalid);
  242. grdCollectionTask.DataSource = collectionTaskTable;
  243. grdCollectionTask.Columns["task_id"].HeaderText = "ID";
  244. grdCollectionTask.Columns["provider_name"].HeaderText = "来源";
  245. grdCollectionTask.Columns["task_date"].HeaderText = "日期";
  246. grdCollectionTask.Columns["task_type_name"].HeaderText = "任务";
  247. grdCollectionTask.Columns["entity_name"].HeaderText = "主题";
  248. grdCollectionTask.Columns["err_message"].HeaderText = "详情";
  249. grdCollectionTask.Columns["isvalid_name"].HeaderText = "状态";
  250. grdCollectionTask.Columns["creator_name"].HeaderText = "创建";
  251. grdCollectionTask.Columns["updater_name"].HeaderText = "更新";
  252. grdCollectionTask.Columns["updatetime"].HeaderText = "更新时间";
  253. grdCollectionTask.Columns["provider_id"].Visible = false;
  254. grdCollectionTask.Columns["task_type"].Visible = false;
  255. grdCollectionTask.Columns["entity_id"].Visible = false;
  256. grdCollectionTask.Columns["isvalid"].Visible = false;
  257. grdCollectionTask.Columns["creatorid"].Visible = false;
  258. grdCollectionTask.Columns["createtime"].Visible = false;
  259. grdCollectionTask.Columns["updaterid"].Visible = false;
  260. //grdCollectionTask.Columns["provider_name"].DefaultCellStyle.ForeColor = Color.DodgerBlue;
  261. grdCollectionTask.Columns["entity_name"].DefaultCellStyle.ForeColor = Color.DodgerBlue;
  262. grdCollectionTask.Columns["entity_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  263. lblCollectionTaskCount.Text = collectionTaskTable.Rows.Count.ToString();
  264. }
  265. private CollectionTask CreatecCollectionTaskFromUI()
  266. {
  267. CollectionTask task = new CollectionTask();
  268. task.UserId = int.Parse(cmbCollector.SelectedValue.ToString());
  269. string providerKeyword = txtCollectionProvider.Text.Trim();
  270. if (providerKeyword == string.Empty)
  271. {
  272. task.ProviderId = null;
  273. task.ProviderName = null;
  274. }
  275. else if (providerKeyword.Length == 10 && providerKeyword.Substring(0, 2).ToUpper() == "CO")
  276. {
  277. task.ProviderId = providerKeyword.ToUpper();
  278. task.ProviderName = null;
  279. }
  280. else
  281. {
  282. task.ProviderId = null;
  283. task.ProviderName = providerKeyword;
  284. }
  285. task.TaskDate = DateTime.Parse(dtpCollectionTaskDate.Text.ToString());
  286. task.TaskType = short.Parse(cmbCollectionTaskType.SelectedValue.ToString());
  287. // 支持输入fund_id或简称关键字的搜索
  288. string entityKeyword = txtCollectionEntity.Text.Trim();
  289. if (entityKeyword == string.Empty)
  290. {
  291. task.EntityId = null;
  292. task.EntityName = null;
  293. }
  294. else if (entityKeyword.Length == 10 && Regex.Match(entityKeyword.ToUpper(), "^[M|H]F").Success)
  295. {
  296. task.EntityId = entityKeyword.ToUpper();
  297. task.EntityName = null;
  298. }
  299. else
  300. {
  301. task.EntityId = null;
  302. task.EntityName = entityKeyword;
  303. }
  304. task.Isvalid = sbyte.Parse(cmbCollectionTaskStatus.SelectedValue.ToString());
  305. return task;
  306. }
  307. private void btnCollectionSearch_Click(object sender, EventArgs e)
  308. {
  309. LoadCollectionTaskGrid();
  310. }
  311. private void grdCollectionTask_CellClick(object sender, DataGridViewCellEventArgs e)
  312. {
  313. int rowIndex = e.RowIndex;
  314. int columnIndex = e.ColumnIndex;
  315. if (columnIndex < 0 || rowIndex < 0) return;
  316. DataRow row = collectionTaskTable.Rows[rowIndex];
  317. if (columnIndex == grdCollectionTask.Columns["entity_name"].Index )
  318. // 弹出基金页面
  319. {
  320. string entityId = row["entity_id"].ToString();
  321. if (entityId.Length == 10 && Regex.Match(entityId, "^[HM]F").Success)
  322. {
  323. UIFund frmFund = new UIFund(entityId, UserId);
  324. frmFund.Show();
  325. }
  326. }
  327. else
  328. // 弹出采集任务编辑框
  329. {
  330. CollectionTask task = new CollectionTask(UserId, row.Field<int>("task_id"), row.Field<string>("provider_id"), row.Field<string>("provider_name"),
  331. row.Field<DateTime>("task_date"), row.Field<short>("task_type"),
  332. row.Field<string>("entity_id"), row.Field<string>("entity_name"),
  333. row.Field<string>("err_message"), row.Field<sbyte>("isvalid"),
  334. row.Field<int?>("creatorid"), row.Field<string>("creator_name"), row.Field<DateTime?>("createtime"),
  335. row.Field<int?>("updaterid"), row.Field<string>("updater_name"), row.Field<DateTime?>("updatetime"));
  336. if (task != null)
  337. {
  338. UICollectionTask ctk = new UICollectionTask(task);
  339. ctk.ShowDialog();
  340. // 刷新 grid 数据
  341. if (ctk.DialogResult == DialogResult.OK)
  342. {
  343. LoadCollectionTaskGrid();
  344. }
  345. }
  346. }
  347. }
  348. private void btnAddCollectionTask_Click(object sender, EventArgs e)
  349. {
  350. UICollectionTask ctk = new UICollectionTask(UserId);
  351. ctk.ShowDialog();
  352. // 刷新 grid 数据
  353. if (ctk.DialogResult == DialogResult.OK)
  354. {
  355. LoadCollectionTaskGrid();
  356. }
  357. }
  358. #endregion
  359. #region Data - Fund
  360. private void LoadFundGrid()
  361. {
  362. string keyword = txtFundKeyword.Text.Trim();
  363. if (keyword == string.Empty)
  364. {
  365. MessageBox.Show("搜索至少得输入一个字吧", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  366. txtFundKeyword.Focus();
  367. return;
  368. }
  369. this.Cursor = Cursors.WaitCursor;
  370. // 只搜私募
  371. fundDataTable = DataAccess.Search_dm_fund(RaiseType.FirstOrDefault(x=>x.Value== "私募").Key, keyword);
  372. grdFundList.DataSource = fundDataTable;
  373. grdFundList.DefaultCellStyle.ForeColor = Color.FromArgb(255, 64, 64, 64);
  374. grdFundList.Columns["fund_id"].Visible = false;
  375. grdFundList.Columns["fund_name"].HeaderText = "基金全名";
  376. grdFundList.Columns["fund_short_name"].HeaderText = "基金简称";
  377. grdFundList.Columns["company_id"].Visible = false;
  378. grdFundList.Columns["company_short_name"].HeaderText = "公司简称";
  379. grdFundList.Columns["strategy"].HeaderText = "策略分类";
  380. grdFundList.Columns["substrategy"].HeaderText = "二级分类";
  381. grdFundList.Columns["max_price_date"].HeaderText = "最新净值";
  382. grdFundList.Columns["company_short_name"].DefaultCellStyle.ForeColor = Color.DodgerBlue;
  383. grdFundList.Columns["max_price_date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  384. grdFundList.Columns["fund_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  385. ToolStripStatusLabel label = new ToolStripStatusLabel();
  386. label.Text = "记录条数:" + fundDataTable.Rows.Count.ToString();
  387. sstData.Items.Clear();
  388. sstData.Items.Add(label);
  389. this.Cursor = Cursors.Default;
  390. }
  391. private void btnSearchFund_Click(object sender, EventArgs e)
  392. {
  393. LoadFundGrid();
  394. }
  395. private void txtFundKeyword_KeyPress(object sender, KeyPressEventArgs e)
  396. {
  397. if (e.KeyChar == 13)
  398. {
  399. btnSearchFund_Click(sender, e);
  400. }
  401. }
  402. private void grdFundList_CellClick(object sender, DataGridViewCellEventArgs e)
  403. {
  404. int rowIndex = e.RowIndex;
  405. int columnIndex = e.ColumnIndex;
  406. if (columnIndex < 0 || rowIndex < 0) return;
  407. DataGridViewRow row = grdFundList.Rows[rowIndex];
  408. if (columnIndex == grdFundList.Columns["company_short_name"].Index)
  409. // 弹出公司页面
  410. {
  411. string entityId = row.Cells["company_id"].Value.ToString().ToUpper();
  412. if (entityId.Length == 10 && Regex.Match(entityId, "^CO").Success)
  413. {
  414. UICompany frm = new UICompany(entityId, UserId);
  415. frm.Show();
  416. }
  417. }
  418. else
  419. // 弹出基金页面
  420. {
  421. string entityId = row.Cells["fund_id"].Value.ToString().ToUpper();
  422. if (entityId.Length == 10 && Regex.Match(entityId, "^[M|H]F").Success)
  423. {
  424. UIFund frm = new UIFund(entityId, UserId);
  425. frm.Show();
  426. }
  427. }
  428. }
  429. private void pctFundExport_Click(object sender, EventArgs e)
  430. {
  431. ExportDataTable(fundDataTable);
  432. }
  433. #endregion
  434. #region Data - Company
  435. private void LoadCompanyGrid()
  436. {
  437. string keyword = txtCompanyKeyword.Text.Trim();
  438. if (keyword == string.Empty)
  439. {
  440. MessageBox.Show("搜索至少得输入一个字吧", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  441. txtFundKeyword.Focus();
  442. return;
  443. }
  444. this.Cursor = Cursors.WaitCursor;
  445. // 只搜私募
  446. companyDataTable = DataAccess.Search_dm_company(1, keyword);
  447. grdCompanyList.DataSource = companyDataTable;
  448. grdCompanyList.DefaultCellStyle.ForeColor = Color.FromArgb(255, 64, 64, 64);
  449. grdCompanyList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  450. //grdCompanyList.Columns["company_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  451. grdCompanyList.Columns["company_id"].Visible = false;
  452. grdCompanyList.Columns["company_name"].HeaderText = "公司全名";
  453. grdCompanyList.Columns["company_short_name"].HeaderText = "公司简称";
  454. grdCompanyList.Columns["company_asset_size"].Visible = false;
  455. grdCompanyList.Columns["company_asset_size_name"].HeaderText = "管理规模";
  456. grdCompanyList.Columns["register_number"].HeaderText = "协会编号";
  457. grdCompanyList.Columns["establish_date"].HeaderText = "成立日";
  458. grdCompanyList.Columns["company_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  459. ToolStripStatusLabel label = new ToolStripStatusLabel();
  460. label.Text = "记录条数:" + companyDataTable.Rows.Count.ToString();
  461. sstData.Items.Clear();
  462. sstData.Items.Add(label);
  463. this.Cursor = Cursors.Default;
  464. }
  465. private void btnSearchCompany_Click(object sender, EventArgs e)
  466. {
  467. LoadCompanyGrid();
  468. }
  469. private void txtCompanyKeyword_KeyPress(object sender, KeyPressEventArgs e)
  470. {
  471. if(e.KeyChar == 13)
  472. {
  473. btnSearchCompany_Click(sender, e);
  474. }
  475. }
  476. private void grdCompanyList_CellClick(object sender, DataGridViewCellEventArgs e)
  477. {
  478. int rowIndex = e.RowIndex;
  479. int columnIndex = e.ColumnIndex;
  480. if (columnIndex < 0 || rowIndex < 0) return;
  481. DataGridViewRow row = grdCompanyList.Rows[rowIndex];
  482. string entityId = row.Cells["company_id"].Value.ToString().ToUpper();
  483. if (entityId.Length == 10 && Regex.Match(entityId, "^CO").Success)
  484. {
  485. UICompany frm = new UICompany(entityId, UserId);
  486. frm.Show();
  487. }
  488. }
  489. private void pctCompanyExport_Click(object sender, EventArgs e)
  490. {
  491. ExportDataTable(companyDataTable);
  492. }
  493. #endregion
  494. #region Reports
  495. #region Orphan Fund
  496. private void LoadOrphanFund()
  497. {
  498. DataTable dt = null;
  499. string providerName = txtOrphanProvider.Text.Trim();
  500. sbyte isOrphan = 1;
  501. if (!chkIsOrphan.Checked) isOrphan = 0;
  502. orphanFundTable = DataAccess.Get_dm_external_fund_mapping(isOrphan, UserId);
  503. if (providerName != string.Empty)
  504. {
  505. DataRow[] rows = orphanFundTable.Select("provider_name LIKE '%" + providerName + "%'", "id DESC");
  506. if(rows != null && rows.Length > 0) dt = rows.CopyToDataTable();
  507. }
  508. else
  509. dt = orphanFundTable;
  510. if (dt != null)
  511. {
  512. grdOrphanFund.DataSource = dt;
  513. grdOrphanFund.Columns["id"].Visible = false;
  514. grdOrphanFund.Columns["fund_id"].HeaderText = "基金ID";
  515. grdOrphanFund.Columns["provider_id"].Visible = false;
  516. grdOrphanFund.Columns["provider_name"].HeaderText = "数据源";
  517. grdOrphanFund.Columns["external_id"].HeaderText = "外部ID";
  518. grdOrphanFund.Columns["isvalid"].HeaderText = "状态";
  519. grdOrphanFund.Columns["creatorid"].Visible = false;
  520. grdOrphanFund.Columns["creator_name"].HeaderText = "创建";
  521. grdOrphanFund.Columns["createtime"].HeaderText = "创建时间";
  522. grdOrphanFund.Columns["updaterid"].Visible = false;
  523. grdOrphanFund.Columns["updater_name"].HeaderText = "更新";
  524. grdOrphanFund.Columns["updatetime"].HeaderText = "更新时间";
  525. grdOrphanFund.Columns["provider_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  526. grdOrphanFund.Columns["external_id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  527. }
  528. }
  529. private void btnOrphanFundSearch_Click(object sender, EventArgs e)
  530. {
  531. LoadOrphanFund();
  532. }
  533. private void grdOrphanFund_CellClick(object sender, DataGridViewCellEventArgs e)
  534. {
  535. int rowIndex = e.RowIndex;
  536. if (rowIndex < 0) return;
  537. DataGridViewRow row = grdOrphanFund.Rows[rowIndex];
  538. // 弹出基金页面
  539. string provider_id = row.Cells["provider_id"].Value.ToString();
  540. string external_id = row.Cells["external_id"].Value.ToString();
  541. string fund_id = row.Cells["fund_id"].Value.ToString();
  542. UIFund frmFund = new UIFund(provider_id, external_id, fund_id, UserId);
  543. frmFund.ShowDialog();
  544. if (frmFund.DialogResult == DialogResult.OK)
  545. {
  546. LoadOrphanFund();
  547. }
  548. }
  549. private void txtOrphanProvider_KeyPress(object sender, KeyPressEventArgs e)
  550. {
  551. if (e.KeyChar == 13)
  552. {
  553. btnOrphanFundSearch_Click(sender, e);
  554. }
  555. }
  556. #endregion
  557. #region Authorization Coverage
  558. private void LoadCompanyCoverageChart()
  559. {
  560. int coveredCnt = 0;
  561. int totalCnt = 0;
  562. DataRow[] rows = companyCoverageTable.Select(CompanySizeFilter());
  563. for (int i = 0; i < rows.Length; i ++)
  564. {
  565. coveredCnt += int.Parse(rows[i]["covered_cnt"].ToString());
  566. totalCnt += int.Parse(rows[i]["total_cnt"].ToString());
  567. }
  568. string[] pieX = { "合作#", "未合作#" };
  569. int[] pieY = { coveredCnt, totalCnt - coveredCnt };
  570. chtEntityCoverage.Series.Clear();
  571. Series series = new Series();
  572. series.ChartType = SeriesChartType.Pie;
  573. series.Points.DataBindXY(pieX, pieY);
  574. series.Points[0].Label = $"{series.Points[0].AxisLabel}: {coveredCnt:N0}";
  575. series.Points[0].ToolTip = $"{series.Points[0].AxisLabel}: {(100.0 * coveredCnt / totalCnt):F2}%";
  576. series.Points[1].Label = $"{series.Points[1].AxisLabel}: {(totalCnt - coveredCnt):N0}";
  577. series.Points[1].ToolTip = $"{series.Points[1].AxisLabel}: {(100.0 * ( 1.0 - coveredCnt / totalCnt)):F2}%";
  578. series.Points[1].Color = Color.LightGray;
  579. chtEntityCoverage.Series.Add(series);
  580. }
  581. private void LoadFundCoverageChart()
  582. {
  583. chtEntityCoverage.Series.Clear();
  584. Series series = new Series();
  585. series.Name = "合作%";
  586. series.ChartType = SeriesChartType.StackedColumn100;
  587. Series series2 = new Series();
  588. series2.Name = "未合作%";
  589. series2.ChartType = SeriesChartType.StackedColumn100;
  590. series2.Color = Color.LightGray;
  591. int coveredCnt = 0;
  592. int totalCnt = 0;
  593. DataRow[] rows = fundCoverageTable.Select(CompanySizeFilter());
  594. for (int i = 0; i < rows.Length; i++)
  595. {
  596. coveredCnt += int.Parse(rows[i]["covered_cnt"].ToString());
  597. totalCnt += int.Parse(rows[i]["total_cnt"].ToString());
  598. series.Points.AddXY(rows[i]["company_asset_size_name"].ToString(), coveredCnt);
  599. series2.Points.AddXY(rows[i]["company_asset_size_name"].ToString(), totalCnt - coveredCnt);
  600. }
  601. chtEntityCoverage.Series.Add(series);
  602. chtEntityCoverage.Series.Add(series2);
  603. chtEntityCoverage.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
  604. chtEntityCoverage.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;
  605. }
  606. private string CompanySizeFilter()
  607. {
  608. string selectExp = "company_asset_size in (";
  609. if (chk100.Checked) selectExp += "6,";
  610. if (chk50.Checked) selectExp += "5,";
  611. if (chk20.Checked) selectExp += "4,";
  612. if (chk10.Checked) selectExp += "3,";
  613. if (chk5.Checked) selectExp += "2,";
  614. if (chk0.Checked) selectExp += "1,";
  615. // 没有checkbox背选中时表示规模未知
  616. if (selectExp.Substring(selectExp.Length - 1) == "(") selectExp = selectExp + " -1)";
  617. // 去掉最后一个逗号
  618. if (selectExp.Substring(selectExp.Length - 1) == ",") selectExp = selectExp.Substring(0, selectExp.Length - 1) + ")";
  619. return selectExp;
  620. }
  621. private void LoadEntityCoverageGrid()
  622. {
  623. // 这个sp有点慢
  624. this.Cursor = Cursors.WaitCursor;
  625. if (rdbCompany.Checked)
  626. {
  627. companyCoverageTable = DataAccess.Report_company_coverage(null);
  628. grdEntityCoverage.DataSource = companyCoverageTable;
  629. }
  630. else
  631. {
  632. fundCoverageTable = DataAccess.Report_fund_coverage(null);
  633. grdEntityCoverage.DataSource = fundCoverageTable;
  634. }
  635. this.Cursor = Cursors.Default;
  636. grdEntityCoverage.Columns["company_asset_size"].Visible = false;
  637. grdEntityCoverage.Columns["company_asset_size_name"].HeaderText = "规模";
  638. grdEntityCoverage.Columns["covered_cnt"].HeaderText = "覆盖数";
  639. grdEntityCoverage.Columns["covered_cnt"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  640. grdEntityCoverage.Columns["covered_cnt"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  641. grdEntityCoverage.Columns["total_cnt"].HeaderText = "总数";
  642. grdEntityCoverage.Columns["total_cnt"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  643. grdEntityCoverage.Columns["total_cnt"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  644. grdEntityCoverage.Columns["cover_pct"].HeaderText = "覆盖率%";
  645. grdEntityCoverage.Columns["cover_pct"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  646. grdEntityCoverage.Columns["cover_pct"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  647. grdEntityCoverage.Columns["company_asset_size_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  648. grdEntityCoverage.Columns["cover_pct"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  649. }
  650. private void LoadAuthrizedCompanyList()
  651. {
  652. authorizedCompanyTable = DataAccess.Get_dm_company_authorization(null, null);
  653. }
  654. private void LoadAuthrizedFundList()
  655. {
  656. authorizedFundTable = DataAccess.Get_dm_fund_authorization(null);
  657. }
  658. private void LoadAuthorizedCompanyGrid()
  659. {
  660. if (authorizedCompanyTable != null)
  661. {
  662. DataTable dt = null;
  663. string selectExp = CompanySizeFilter();
  664. string nameExp = txtLocateEntity.Text.Trim();
  665. if (nameExp.Length > 0) selectExp += " and company_short_name LIKE '%" + nameExp + "%'";
  666. DataRow[] dr = authorizedCompanyTable.Select(selectExp, "company_asset_size DESC");
  667. if (dr != null && dr.Length > 0) dt = dr.CopyToDataTable();
  668. grdAuthorizedEntity.DataSource = dt;
  669. if (dt != null)
  670. {
  671. grdAuthorizedEntity.Columns["company_id"].Visible = false;
  672. grdAuthorizedEntity.Columns["company_name"].Visible = false;
  673. grdAuthorizedEntity.Columns["company_short_name"].HeaderText = "公司简称";
  674. grdAuthorizedEntity.Columns["company_asset_size"].Visible = false;
  675. grdAuthorizedEntity.Columns["company_asset_size_name"].HeaderText = "规模";
  676. grdAuthorizedEntity.Columns["register_number"].HeaderText = "协会编号";
  677. grdAuthorizedEntity.Columns["establish_date"].HeaderText = "成立日";
  678. grdAuthorizedEntity.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  679. grdAuthorizedEntity.Columns["company_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  680. }
  681. }
  682. }
  683. private void LoadAuthorizedFundGrid()
  684. {
  685. if (authorizedFundTable != null)
  686. {
  687. DataTable dt = null;
  688. string selectExp = CompanySizeFilter();
  689. string nameExp = txtLocateEntity.Text.Trim();
  690. if (nameExp.Length > 0) selectExp += " and fund_short_name LIKE '%" + nameExp + "%'";
  691. DataRow[] dr = authorizedFundTable.Select(selectExp, "company_asset_size DESC");
  692. if (dr != null && dr.Length > 0) dt = dr.CopyToDataTable();
  693. grdAuthorizedEntity.DataSource = dt;
  694. if (dt != null)
  695. {
  696. grdAuthorizedEntity.Columns["fund_id"].Visible = false;
  697. grdAuthorizedEntity.Columns["fund_name"].Visible = false;
  698. grdAuthorizedEntity.Columns["fund_short_name"].HeaderText = "基金简称";
  699. grdAuthorizedEntity.Columns["company_asset_size"].Visible = false;
  700. grdAuthorizedEntity.Columns["strategy"].Visible = false;
  701. grdAuthorizedEntity.Columns["substrategy"].HeaderText = "二级分类";
  702. grdAuthorizedEntity.Columns["inception_date"].HeaderText = "成立日";
  703. grdAuthorizedEntity.Columns["register_number"].HeaderText = "备案编码";
  704. grdAuthorizedEntity.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  705. grdAuthorizedEntity.Columns["fund_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  706. }
  707. }
  708. }
  709. private void btnEntityCoverage_Click(object sender, EventArgs e)
  710. {
  711. // 左下角的固定格式覆盖率表
  712. LoadEntityCoverageGrid();
  713. if (rdbCompany.Checked)
  714. {
  715. // 授权公司数据
  716. LoadAuthrizedCompanyList();
  717. // 对公司覆盖率表进行可视化 (PIE)
  718. LoadCompanyCoverageChart();
  719. // 授权公司列表
  720. LoadAuthorizedCompanyGrid();
  721. }
  722. else
  723. {
  724. // 授权基金数据
  725. LoadAuthrizedFundList();
  726. // 对基金覆盖率表进行可视化(BAR)
  727. LoadFundCoverageChart();
  728. // 授权基金列表
  729. LoadAuthorizedFundGrid();
  730. }
  731. }
  732. private void rdbCompany_CheckedChanged(object sender, EventArgs e)
  733. {
  734. btnEntityCoverage_Click(sender, e);
  735. }
  736. private void chk100_CheckedChanged(object sender, EventArgs e)
  737. {
  738. if (((Control)sender).Focus())
  739. {
  740. if (rdbCompany.Checked)
  741. {
  742. // 重新画图
  743. LoadCompanyCoverageChart();
  744. // 刷新公司列表
  745. LoadAuthorizedCompanyGrid();
  746. }
  747. else
  748. {
  749. // 重新画图
  750. LoadFundCoverageChart();
  751. // 刷新基金列表
  752. LoadAuthorizedFundGrid();
  753. }
  754. }
  755. }
  756. private void chk50_CheckedChanged(object sender, EventArgs e)
  757. {
  758. chk100_CheckedChanged(sender, e);
  759. }
  760. private void chk20_CheckedChanged(object sender, EventArgs e)
  761. {
  762. chk100_CheckedChanged(sender, e);
  763. }
  764. private void chk10_CheckedChanged(object sender, EventArgs e)
  765. {
  766. chk100_CheckedChanged(sender, e);
  767. }
  768. private void chk5_CheckedChanged(object sender, EventArgs e)
  769. {
  770. chk100_CheckedChanged(sender, e);
  771. }
  772. private void chk0_CheckedChanged(object sender, EventArgs e)
  773. {
  774. chk100_CheckedChanged(sender, e);
  775. }
  776. private void txtLocateEntity_TextChanged(object sender, EventArgs e)
  777. {
  778. if (rdbCompany.Checked) LoadAuthorizedCompanyGrid();
  779. else LoadAuthorizedFundGrid();
  780. }
  781. private void grdAuthorizedEntity_CellClick(object sender, DataGridViewCellEventArgs e)
  782. {
  783. int rowIndex = e.RowIndex;
  784. int columnIndex = e.ColumnIndex;
  785. if (columnIndex < 0 || rowIndex < 0) return;
  786. DataGridViewRow row = grdAuthorizedEntity.Rows[rowIndex];
  787. if (rdbCompany.Checked)
  788. {
  789. string entityId = row.Cells["company_id"].Value.ToString().ToUpper();
  790. if (entityId.Length == 10 && Regex.Match(entityId, "^CO").Success)
  791. {
  792. UICompany frm = new UICompany(entityId, UserId);
  793. frm.Show();
  794. }
  795. }
  796. else
  797. {
  798. string entityId = row.Cells["fund_id"].Value.ToString().ToUpper();
  799. if (entityId.Length == 10 && Regex.Match(entityId, "^[H|M]F").Success)
  800. {
  801. UIFund frm = new UIFund(entityId, UserId);
  802. frm.Show();
  803. }
  804. }
  805. }
  806. private void rdbFund_CheckedChanged(object sender, EventArgs e)
  807. {
  808. // DO NOTHING, because same actions were triggered by rdbCompany_CheckedChanged anyway
  809. }
  810. #endregion
  811. #endregion
  812. public static void ExportDataTable(DataTable dt)
  813. {
  814. SaveFileDialog saveFileDialog = new SaveFileDialog();
  815. saveFileDialog.Filter = "CSV 文件 (*.csv)|*.csv|TXT 文件|*.txt";
  816. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  817. {
  818. string filePath = saveFileDialog.FileName;
  819. int ret = DataTableToCSV(dt, filePath);
  820. MessageBox.Show(ret < 0 ? "数据导出有问题,得问问程序员师傅" : "数据导出后,可以用Excel直接打开", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  821. }
  822. }
  823. private static int DataTableToCSV(DataTable dt, string fileName)
  824. {
  825. int ret = -1;
  826. if (dt == null || dt.Columns == null) return ret;
  827. StringBuilder sb = new StringBuilder();
  828. foreach (DataColumn col in dt.Columns)
  829. {
  830. sb.Append(col.ColumnName + ",");
  831. }
  832. sb.AppendLine();
  833. if (dt.Rows != null && dt.Rows.Count > 0)
  834. {
  835. foreach (DataRow row in dt.Rows)
  836. {
  837. foreach(DataColumn col in dt.Columns)
  838. {
  839. sb.Append(row[col].ToString() + ",");
  840. }
  841. sb.AppendLine();
  842. }
  843. }
  844. using (StreamWriter writer = new StreamWriter(fileName))
  845. {
  846. writer.Write(sb.ToString());
  847. ret = 1;
  848. }
  849. return ret;
  850. }
  851. }
  852. }