DataManager.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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) { keyword = null; }
  364. this.Cursor = Cursors.WaitCursor;
  365. // 只搜私募
  366. fundDataTable = DataAccess.Search_dm_fund(RaiseType.FirstOrDefault(x=>x.Value== "私募").Key, keyword);
  367. grdFundList.DataSource = fundDataTable;
  368. grdFundList.DefaultCellStyle.ForeColor = Color.FromArgb(255, 64, 64, 64);
  369. grdFundList.Columns["fund_id"].Visible = false;
  370. grdFundList.Columns["fund_name"].HeaderText = "基金全名";
  371. grdFundList.Columns["fund_short_name"].HeaderText = "基金简称";
  372. grdFundList.Columns["company_id"].Visible = false;
  373. grdFundList.Columns["company_short_name"].HeaderText = "公司简称";
  374. grdFundList.Columns["strategy"].HeaderText = "策略分类";
  375. grdFundList.Columns["substrategy"].HeaderText = "二级分类";
  376. grdFundList.Columns["max_price_date"].HeaderText = "最新净值";
  377. grdFundList.Columns["company_short_name"].DefaultCellStyle.ForeColor = Color.DodgerBlue;
  378. grdFundList.Columns["max_price_date"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  379. grdFundList.Columns["fund_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  380. ToolStripStatusLabel label = new ToolStripStatusLabel();
  381. label.Text = "记录条数:" + fundDataTable.Rows.Count.ToString();
  382. sstData.Items.Clear();
  383. sstData.Items.Add(label);
  384. this.Cursor = Cursors.Default;
  385. }
  386. private void btnSearchFund_Click(object sender, EventArgs e)
  387. {
  388. LoadFundGrid();
  389. }
  390. private void txtFundKeyword_KeyPress(object sender, KeyPressEventArgs e)
  391. {
  392. if (e.KeyChar == 13)
  393. {
  394. btnSearchFund_Click(sender, e);
  395. }
  396. }
  397. private void grdFundList_CellClick(object sender, DataGridViewCellEventArgs e)
  398. {
  399. int rowIndex = e.RowIndex;
  400. int columnIndex = e.ColumnIndex;
  401. if (columnIndex < 0 || rowIndex < 0) return;
  402. DataGridViewRow row = grdFundList.Rows[rowIndex];
  403. if (columnIndex == grdFundList.Columns["company_short_name"].Index)
  404. // 弹出公司页面
  405. {
  406. string entityId = row.Cells["company_id"].Value.ToString().ToUpper();
  407. if (entityId.Length == 10 && Regex.Match(entityId, "^CO").Success)
  408. {
  409. UICompany frm = new UICompany(entityId, UserId);
  410. frm.Show();
  411. }
  412. }
  413. else
  414. // 弹出基金页面
  415. {
  416. string entityId = row.Cells["fund_id"].Value.ToString().ToUpper();
  417. if (entityId.Length == 10 && Regex.Match(entityId, "^[M|H]F").Success)
  418. {
  419. UIFund frm = new UIFund(entityId, UserId);
  420. frm.Show();
  421. }
  422. }
  423. }
  424. private void pctFundExport_Click(object sender, EventArgs e)
  425. {
  426. ExportDataTable(fundDataTable);
  427. }
  428. #endregion
  429. #region Data - Company
  430. private void LoadCompanyGrid()
  431. {
  432. string keyword = txtCompanyKeyword.Text.Trim();
  433. if (keyword == string.Empty) { keyword = null; }
  434. this.Cursor = Cursors.WaitCursor;
  435. // 只搜私募
  436. companyDataTable = DataAccess.Search_dm_company(1, keyword);
  437. grdCompanyList.DataSource = companyDataTable;
  438. grdCompanyList.DefaultCellStyle.ForeColor = Color.FromArgb(255, 64, 64, 64);
  439. grdCompanyList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  440. //grdCompanyList.Columns["company_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  441. grdCompanyList.Columns["company_id"].Visible = false;
  442. grdCompanyList.Columns["company_name"].HeaderText = "公司全名";
  443. grdCompanyList.Columns["company_short_name"].HeaderText = "公司简称";
  444. grdCompanyList.Columns["company_asset_size"].Visible = false;
  445. grdCompanyList.Columns["company_asset_size_name"].HeaderText = "管理规模";
  446. grdCompanyList.Columns["register_number"].HeaderText = "协会编号";
  447. grdCompanyList.Columns["establish_date"].HeaderText = "成立日";
  448. grdCompanyList.Columns["company_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  449. ToolStripStatusLabel label = new ToolStripStatusLabel();
  450. label.Text = "记录条数:" + companyDataTable.Rows.Count.ToString();
  451. sstData.Items.Clear();
  452. sstData.Items.Add(label);
  453. this.Cursor = Cursors.Default;
  454. }
  455. private void btnSearchCompany_Click(object sender, EventArgs e)
  456. {
  457. LoadCompanyGrid();
  458. }
  459. private void txtCompanyKeyword_KeyPress(object sender, KeyPressEventArgs e)
  460. {
  461. if(e.KeyChar == 13)
  462. {
  463. btnSearchCompany_Click(sender, e);
  464. }
  465. }
  466. private void grdCompanyList_CellClick(object sender, DataGridViewCellEventArgs e)
  467. {
  468. int rowIndex = e.RowIndex;
  469. int columnIndex = e.ColumnIndex;
  470. if (columnIndex < 0 || rowIndex < 0) return;
  471. DataGridViewRow row = grdCompanyList.Rows[rowIndex];
  472. string entityId = row.Cells["company_id"].Value.ToString().ToUpper();
  473. if (entityId.Length == 10 && Regex.Match(entityId, "^CO").Success)
  474. {
  475. UICompany frm = new UICompany(entityId, UserId);
  476. frm.Show();
  477. }
  478. }
  479. private void pctCompanyExport_Click(object sender, EventArgs e)
  480. {
  481. ExportDataTable(companyDataTable);
  482. }
  483. #endregion
  484. #region Reports
  485. #region Orphan Fund
  486. private void LoadOrphanFund()
  487. {
  488. DataTable dt = null;
  489. string providerName = txtOrphanProvider.Text.Trim();
  490. sbyte isOrphan = 1;
  491. if (!chkIsOrphan.Checked) isOrphan = 0;
  492. orphanFundTable = DataAccess.Get_dm_external_fund_mapping(isOrphan, UserId);
  493. if (providerName != string.Empty)
  494. {
  495. DataRow[] rows = orphanFundTable.Select("provider_name LIKE '%" + providerName + "%'", "id DESC");
  496. if(rows != null && rows.Length > 0) dt = rows.CopyToDataTable();
  497. }
  498. else
  499. dt = orphanFundTable;
  500. if (dt != null)
  501. {
  502. grdOrphanFund.DataSource = dt;
  503. grdOrphanFund.Columns["id"].Visible = false;
  504. grdOrphanFund.Columns["fund_id"].HeaderText = "基金ID";
  505. grdOrphanFund.Columns["provider_id"].Visible = false;
  506. grdOrphanFund.Columns["provider_name"].HeaderText = "数据源";
  507. grdOrphanFund.Columns["external_id"].HeaderText = "外部ID";
  508. grdOrphanFund.Columns["isvalid"].HeaderText = "状态";
  509. grdOrphanFund.Columns["creatorid"].Visible = false;
  510. grdOrphanFund.Columns["creator_name"].HeaderText = "创建";
  511. grdOrphanFund.Columns["createtime"].HeaderText = "创建时间";
  512. grdOrphanFund.Columns["updaterid"].Visible = false;
  513. grdOrphanFund.Columns["updater_name"].HeaderText = "更新";
  514. grdOrphanFund.Columns["updatetime"].HeaderText = "更新时间";
  515. grdOrphanFund.Columns["provider_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  516. grdOrphanFund.Columns["external_id"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  517. }
  518. }
  519. private void btnOrphanFundSearch_Click(object sender, EventArgs e)
  520. {
  521. LoadOrphanFund();
  522. }
  523. private void grdOrphanFund_CellClick(object sender, DataGridViewCellEventArgs e)
  524. {
  525. int rowIndex = e.RowIndex;
  526. if (rowIndex < 0) return;
  527. DataGridViewRow row = grdOrphanFund.Rows[rowIndex];
  528. // 弹出基金页面
  529. string provider_id = row.Cells["provider_id"].Value.ToString();
  530. string external_id = row.Cells["external_id"].Value.ToString();
  531. string fund_id = row.Cells["fund_id"].Value.ToString();
  532. UIFund frmFund = new UIFund(provider_id, external_id, fund_id, UserId);
  533. frmFund.ShowDialog();
  534. if (frmFund.DialogResult == DialogResult.OK)
  535. {
  536. LoadOrphanFund();
  537. }
  538. }
  539. private void txtOrphanProvider_KeyPress(object sender, KeyPressEventArgs e)
  540. {
  541. if (e.KeyChar == 13)
  542. {
  543. btnOrphanFundSearch_Click(sender, e);
  544. }
  545. }
  546. #endregion
  547. #region Authorization Coverage
  548. private void LoadCompanyCoverageChart()
  549. {
  550. int coveredCnt = 0;
  551. int totalCnt = 0;
  552. DataRow[] rows = companyCoverageTable.Select(CompanySizeFilter());
  553. for (int i = 0; i < rows.Length; i ++)
  554. {
  555. coveredCnt += int.Parse(rows[i]["covered_cnt"].ToString());
  556. totalCnt += int.Parse(rows[i]["total_cnt"].ToString());
  557. }
  558. string[] pieX = { "合作#", "未合作#" };
  559. int[] pieY = { coveredCnt, totalCnt - coveredCnt };
  560. chtEntityCoverage.Series.Clear();
  561. Series series = new Series();
  562. series.ChartType = SeriesChartType.Pie;
  563. series.Points.DataBindXY(pieX, pieY);
  564. series.Points[0].Label = $"{series.Points[0].AxisLabel}: {coveredCnt:N0}";
  565. series.Points[0].ToolTip = $"{series.Points[0].AxisLabel}: {(100.0 * coveredCnt / totalCnt):F2}%";
  566. series.Points[1].Label = $"{series.Points[1].AxisLabel}: {(totalCnt - coveredCnt):N0}";
  567. series.Points[1].ToolTip = $"{series.Points[1].AxisLabel}: {(100.0 * ( 1.0 - coveredCnt / totalCnt)):F2}%";
  568. series.Points[1].Color = Color.LightGray;
  569. chtEntityCoverage.Series.Add(series);
  570. }
  571. private void LoadFundCoverageChart()
  572. {
  573. chtEntityCoverage.Series.Clear();
  574. Series series = new Series();
  575. series.Name = "合作%";
  576. series.ChartType = SeriesChartType.StackedColumn100;
  577. Series series2 = new Series();
  578. series2.Name = "未合作%";
  579. series2.ChartType = SeriesChartType.StackedColumn100;
  580. series2.Color = Color.LightGray;
  581. int coveredCnt = 0;
  582. int totalCnt = 0;
  583. DataRow[] rows = fundCoverageTable.Select(CompanySizeFilter());
  584. for (int i = 0; i < rows.Length; i++)
  585. {
  586. coveredCnt += int.Parse(rows[i]["covered_cnt"].ToString());
  587. totalCnt += int.Parse(rows[i]["total_cnt"].ToString());
  588. series.Points.AddXY(rows[i]["company_asset_size_name"].ToString(), coveredCnt);
  589. series2.Points.AddXY(rows[i]["company_asset_size_name"].ToString(), totalCnt - coveredCnt);
  590. }
  591. chtEntityCoverage.Series.Add(series);
  592. chtEntityCoverage.Series.Add(series2);
  593. chtEntityCoverage.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
  594. chtEntityCoverage.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;
  595. }
  596. private string CompanySizeFilter()
  597. {
  598. string selectExp = "company_asset_size in (";
  599. if (chk100.Checked) selectExp += "6,";
  600. if (chk50.Checked) selectExp += "5,";
  601. if (chk20.Checked) selectExp += "4,";
  602. if (chk10.Checked) selectExp += "3,";
  603. if (chk5.Checked) selectExp += "2,";
  604. if (chk0.Checked) selectExp += "1,";
  605. // 没有checkbox背选中时表示规模未知
  606. if (selectExp.Substring(selectExp.Length - 1) == "(") selectExp = selectExp + " -1)";
  607. // 去掉最后一个逗号
  608. if (selectExp.Substring(selectExp.Length - 1) == ",") selectExp = selectExp.Substring(0, selectExp.Length - 1) + ")";
  609. return selectExp;
  610. }
  611. private void LoadEntityCoverageGrid()
  612. {
  613. // 这个sp有点慢
  614. this.Cursor = Cursors.WaitCursor;
  615. if (rdbCompany.Checked)
  616. {
  617. companyCoverageTable = DataAccess.Report_company_coverage(null);
  618. grdEntityCoverage.DataSource = companyCoverageTable;
  619. }
  620. else
  621. {
  622. fundCoverageTable = DataAccess.Report_fund_coverage(null);
  623. grdEntityCoverage.DataSource = fundCoverageTable;
  624. }
  625. this.Cursor = Cursors.Default;
  626. grdEntityCoverage.Columns["company_asset_size"].Visible = false;
  627. grdEntityCoverage.Columns["company_asset_size_name"].HeaderText = "规模";
  628. grdEntityCoverage.Columns["covered_cnt"].HeaderText = "覆盖数";
  629. grdEntityCoverage.Columns["covered_cnt"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  630. grdEntityCoverage.Columns["covered_cnt"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  631. grdEntityCoverage.Columns["total_cnt"].HeaderText = "总数";
  632. grdEntityCoverage.Columns["total_cnt"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  633. grdEntityCoverage.Columns["total_cnt"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  634. grdEntityCoverage.Columns["cover_pct"].HeaderText = "覆盖率%";
  635. grdEntityCoverage.Columns["cover_pct"].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
  636. grdEntityCoverage.Columns["cover_pct"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
  637. grdEntityCoverage.Columns["company_asset_size_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  638. grdEntityCoverage.Columns["cover_pct"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  639. }
  640. private void LoadAuthrizedCompanyList()
  641. {
  642. authorizedCompanyTable = DataAccess.Get_dm_company_authorization(null, null);
  643. }
  644. private void LoadAuthrizedFundList()
  645. {
  646. authorizedFundTable = DataAccess.Get_dm_fund_authorization(null);
  647. }
  648. private void LoadAuthorizedCompanyGrid()
  649. {
  650. if (authorizedCompanyTable != null)
  651. {
  652. DataTable dt = null;
  653. string selectExp = CompanySizeFilter();
  654. string nameExp = txtLocateEntity.Text.Trim();
  655. if (nameExp.Length > 0) selectExp += " and company_short_name LIKE '%" + nameExp + "%'";
  656. DataRow[] dr = authorizedCompanyTable.Select(selectExp, "company_asset_size DESC");
  657. if (dr != null && dr.Length > 0) dt = dr.CopyToDataTable();
  658. grdAuthorizedEntity.DataSource = dt;
  659. if (dt != null)
  660. {
  661. grdAuthorizedEntity.Columns["company_id"].Visible = false;
  662. grdAuthorizedEntity.Columns["company_name"].Visible = false;
  663. grdAuthorizedEntity.Columns["company_short_name"].HeaderText = "公司简称";
  664. grdAuthorizedEntity.Columns["company_asset_size"].Visible = false;
  665. grdAuthorizedEntity.Columns["company_asset_size_name"].HeaderText = "规模";
  666. grdAuthorizedEntity.Columns["register_number"].HeaderText = "协会编号";
  667. grdAuthorizedEntity.Columns["establish_date"].HeaderText = "成立日";
  668. grdAuthorizedEntity.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  669. grdAuthorizedEntity.Columns["company_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  670. }
  671. }
  672. }
  673. private void LoadAuthorizedFundGrid()
  674. {
  675. if (authorizedFundTable != null)
  676. {
  677. DataTable dt = null;
  678. string selectExp = CompanySizeFilter();
  679. string nameExp = txtLocateEntity.Text.Trim();
  680. if (nameExp.Length > 0) selectExp += " and fund_short_name LIKE '%" + nameExp + "%'";
  681. DataRow[] dr = authorizedFundTable.Select(selectExp, "company_asset_size DESC");
  682. if (dr != null && dr.Length > 0) dt = dr.CopyToDataTable();
  683. grdAuthorizedEntity.DataSource = dt;
  684. if (dt != null)
  685. {
  686. grdAuthorizedEntity.Columns["fund_id"].Visible = false;
  687. grdAuthorizedEntity.Columns["fund_name"].Visible = false;
  688. grdAuthorizedEntity.Columns["fund_short_name"].HeaderText = "基金简称";
  689. grdAuthorizedEntity.Columns["company_asset_size"].Visible = false;
  690. grdAuthorizedEntity.Columns["strategy"].Visible = false;
  691. grdAuthorizedEntity.Columns["substrategy"].HeaderText = "二级分类";
  692. grdAuthorizedEntity.Columns["inception_date"].HeaderText = "成立日";
  693. grdAuthorizedEntity.Columns["register_number"].HeaderText = "备案编码";
  694. grdAuthorizedEntity.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  695. grdAuthorizedEntity.Columns["fund_short_name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
  696. }
  697. }
  698. }
  699. private void btnEntityCoverage_Click(object sender, EventArgs e)
  700. {
  701. // 左下角的固定格式覆盖率表
  702. LoadEntityCoverageGrid();
  703. if (rdbCompany.Checked)
  704. {
  705. // 授权公司数据
  706. LoadAuthrizedCompanyList();
  707. // 对公司覆盖率表进行可视化 (PIE)
  708. LoadCompanyCoverageChart();
  709. // 授权公司列表
  710. LoadAuthorizedCompanyGrid();
  711. }
  712. else
  713. {
  714. // 授权基金数据
  715. LoadAuthrizedFundList();
  716. // 对基金覆盖率表进行可视化(BAR)
  717. LoadFundCoverageChart();
  718. // 授权基金列表
  719. LoadAuthorizedFundGrid();
  720. }
  721. }
  722. private void rdbCompany_CheckedChanged(object sender, EventArgs e)
  723. {
  724. btnEntityCoverage_Click(sender, e);
  725. }
  726. private void chk100_CheckedChanged(object sender, EventArgs e)
  727. {
  728. if (((Control)sender).Focus())
  729. {
  730. if (rdbCompany.Checked)
  731. {
  732. // 重新画图
  733. LoadCompanyCoverageChart();
  734. // 刷新公司列表
  735. LoadAuthorizedCompanyGrid();
  736. }
  737. else
  738. {
  739. // 重新画图
  740. LoadFundCoverageChart();
  741. // 刷新基金列表
  742. LoadAuthorizedFundGrid();
  743. }
  744. }
  745. }
  746. private void chk50_CheckedChanged(object sender, EventArgs e)
  747. {
  748. chk100_CheckedChanged(sender, e);
  749. }
  750. private void chk20_CheckedChanged(object sender, EventArgs e)
  751. {
  752. chk100_CheckedChanged(sender, e);
  753. }
  754. private void chk10_CheckedChanged(object sender, EventArgs e)
  755. {
  756. chk100_CheckedChanged(sender, e);
  757. }
  758. private void chk5_CheckedChanged(object sender, EventArgs e)
  759. {
  760. chk100_CheckedChanged(sender, e);
  761. }
  762. private void chk0_CheckedChanged(object sender, EventArgs e)
  763. {
  764. chk100_CheckedChanged(sender, e);
  765. }
  766. private void txtLocateEntity_TextChanged(object sender, EventArgs e)
  767. {
  768. if (rdbCompany.Checked) LoadAuthorizedCompanyGrid();
  769. else LoadAuthorizedFundGrid();
  770. }
  771. private void grdAuthorizedEntity_CellClick(object sender, DataGridViewCellEventArgs e)
  772. {
  773. int rowIndex = e.RowIndex;
  774. int columnIndex = e.ColumnIndex;
  775. if (columnIndex < 0 || rowIndex < 0) return;
  776. DataGridViewRow row = grdAuthorizedEntity.Rows[rowIndex];
  777. if (rdbCompany.Checked)
  778. {
  779. string entityId = row.Cells["company_id"].Value.ToString().ToUpper();
  780. if (entityId.Length == 10 && Regex.Match(entityId, "^CO").Success)
  781. {
  782. UICompany frm = new UICompany(entityId, UserId);
  783. frm.Show();
  784. }
  785. }
  786. else
  787. {
  788. string entityId = row.Cells["fund_id"].Value.ToString().ToUpper();
  789. if (entityId.Length == 10 && Regex.Match(entityId, "^[H|M]F").Success)
  790. {
  791. UIFund frm = new UIFund(entityId, UserId);
  792. frm.Show();
  793. }
  794. }
  795. }
  796. private void rdbFund_CheckedChanged(object sender, EventArgs e)
  797. {
  798. // DO NOTHING, because same actions were triggered by rdbCompany_CheckedChanged anyway
  799. }
  800. #endregion
  801. #endregion
  802. public static void ExportDataTable(DataTable dt)
  803. {
  804. SaveFileDialog saveFileDialog = new SaveFileDialog();
  805. saveFileDialog.Filter = "CSV 文件 (*.csv)|*.csv|TXT 文件|*.txt";
  806. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  807. {
  808. string filePath = saveFileDialog.FileName;
  809. int ret = DataTableToCSV(dt, filePath);
  810. MessageBox.Show(ret < 0 ? "数据导出有问题,得问问程序员师傅" : "数据导出后,可以用Excel直接打开", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
  811. }
  812. }
  813. private static int DataTableToCSV(DataTable dt, string fileName)
  814. {
  815. int ret = -1;
  816. if (dt == null || dt.Columns == null) return ret;
  817. StringBuilder sb = new StringBuilder();
  818. foreach (DataColumn col in dt.Columns)
  819. {
  820. sb.Append(col.ColumnName + ",");
  821. }
  822. sb.AppendLine();
  823. if (dt.Rows != null && dt.Rows.Count > 0)
  824. {
  825. foreach (DataRow row in dt.Rows)
  826. {
  827. foreach(DataColumn col in dt.Columns)
  828. {
  829. sb.Append(row[col].ToString() + ",");
  830. }
  831. sb.AppendLine();
  832. }
  833. }
  834. using (StreamWriter writer = new StreamWriter(fileName))
  835. {
  836. writer.Write(sb.ToString());
  837. ret = 1;
  838. }
  839. return ret;
  840. }
  841. }
  842. }