ImportProjectInfoJob.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. using Microsoft.Data.SqlClient;
  2. using Quartz;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Threading.Tasks;
  7. using wispro.sp.entity;
  8. using wispro.sp.utility;
  9. using System.Linq;
  10. using System.IO;
  11. using System.Net.Http;
  12. using System.Xml.Serialization;
  13. namespace wispro.sp.api.Job
  14. {
  15. public class ImportProjectInfoJob : Quartz.IJob
  16. {
  17. public Task Execute(IJobExecutionContext context)
  18. {
  19. DownloadProject_SQL();
  20. return Task.CompletedTask;
  21. }
  22. private void DownloadProject_SQL()
  23. {
  24. List<PerformanceItem> pfItems = new List<PerformanceItem>();
  25. DataTable data = GetDataFromIPEasy();
  26. var Items = GetProjectItem(data);
  27. foreach (var item in Items)
  28. {
  29. var temObj = pfItems.FirstOrDefault<PerformanceItem>(s => s.CaseNo == item.CaseNo);
  30. if (temObj == null)
  31. {
  32. item.Type = "专案";
  33. pfItems.Add(item);
  34. }
  35. }
  36. //List<BasePointRule> rules = spDb.BasePointRules.ToList<BasePointRule>();
  37. foreach (var item in pfItems)
  38. {
  39. SaveProjectItem(item);
  40. }
  41. }
  42. private void SaveProjectItem(PerformanceItem item)
  43. {
  44. spDbContext spDb = new spDbContext();
  45. var temObj = spDb.ProjectInfos.FirstOrDefault(p => p.CaseNo == item.CaseNo);
  46. if (temObj == null)
  47. {
  48. ProjectInfo project = new ProjectInfo();
  49. project.CaseNo = item.CaseNo;
  50. project.CaseName = item.CaseName;
  51. project.BusinessType = item.BusinessType;
  52. if (item.Customer != null)
  53. {
  54. var temCustomer = spDb.Customers.FirstOrDefault(c => c.Name == item.Customer.Name);
  55. if (temCustomer != null)
  56. {
  57. project.CustomerId = temCustomer.Id;
  58. }
  59. }
  60. project.CaseState = 0;
  61. project.CaseType = item.CaseType;
  62. project.ReviewerId = item.ReviewerId;
  63. project.WorkflowUserId = item.WorkflowUserId;
  64. spDb.ProjectInfos.Add(project);
  65. }
  66. spDb.SaveChanges();
  67. }
  68. private List<PerformanceItem> GetProjectItem(DataTable dt)
  69. {
  70. #region 删除重复行
  71. DataTable temdt = new DataTable();
  72. foreach (DataColumn col in dt.Columns)
  73. {
  74. DataColumn temCol = new DataColumn();
  75. temCol.ColumnName = col.ColumnName;
  76. temCol.DataType = col.DataType;
  77. temCol.Caption = col.Caption;
  78. temdt.Columns.Add(temCol);
  79. }
  80. new ExcelHelper().MerageDataTable(temdt, dt);
  81. #endregion
  82. List<PerformanceItem> Items = new List<PerformanceItem>();
  83. int iRow = 0;
  84. foreach (DataRow row in temdt.Rows)
  85. {
  86. string strDebug = $"{++iRow}\t{row["我方文号"]}";
  87. PerformanceItem item = null;
  88. item = Row2Item_3(row);
  89. if (item != null)
  90. {
  91. Items.Add(item);
  92. }
  93. }
  94. return Items;
  95. }
  96. private PerformanceItem Row2Item_3(DataRow row)
  97. {
  98. PerformanceItem item = new PerformanceItem();
  99. item.CaseNo = row["我方文号"].ToString().Trim();
  100. item.CaseName = row["案件名称"].ToString().Trim();
  101. item.CaseType = row["案件类型"].ToString().Trim();
  102. item.BusinessType = row["业务类型"].ToString().Trim();
  103. item.Customer = new Customer();
  104. item.Customer.Name = row["客户名称"].ToString().Trim();
  105. item.CaseState = row["案件状态"].ToString().Trim();
  106. DateTime temDate = new DateTime();
  107. if (DateTime.TryParse(row["委案日期"].ToString().Trim(), out temDate))
  108. {
  109. item.EntrustingDate = temDate;
  110. }
  111. string strHandler = "";
  112. if (row.Table.Columns.Contains("处理人"))
  113. {
  114. strHandler = row["处理人"].ToString().Trim();
  115. }
  116. else
  117. {
  118. if (row.Table.Columns.Contains("案件处理人"))
  119. {
  120. strHandler = row["案件处理人"].ToString().Trim();
  121. }
  122. }
  123. string[] temHandlers = strHandler.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  124. item.ItemStaffs = new List<ItemStaff>();
  125. foreach (string name in temHandlers)
  126. {
  127. ItemStaff itemStaff = new ItemStaff();
  128. string temName = name.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)[0];
  129. if (!name.Contains("君龙"))
  130. {
  131. temName = name.Trim();
  132. }
  133. int? iTem = GetStaff(name);
  134. if ((iTem != null))
  135. {
  136. //itemStaff.Item = item;
  137. itemStaff.DoPersonId = iTem.Value;
  138. item.ItemStaffs.Add(itemStaff);
  139. }
  140. else
  141. {
  142. itemStaff.DoPerson = new Staff()
  143. {
  144. Name = temName,
  145. Account = temName,
  146. Password = utility.MD5Utility.GetMD5("12345678"),
  147. IsCalPerformsnce = false,
  148. Status = "试用期",
  149. StaffGradeId = 4
  150. };
  151. item.ItemStaffs.Add(itemStaff);
  152. }
  153. }
  154. if (item.ItemStaffs.Count == 0)
  155. {
  156. System.Diagnostics.Debug.WriteLine($"没有处理人: {item.CaseNo}\t{item.DoItem}");
  157. }
  158. if (row.Table.Columns.Contains("核稿人"))
  159. {
  160. item.ReviewerId = GetStaff(row["核稿人"].ToString().Trim());
  161. }
  162. else
  163. {
  164. if (row.Table.Columns.Contains("案件核稿人"))
  165. {
  166. item.ReviewerId = GetStaff(row["案件核稿人"].ToString().Trim());
  167. }
  168. }
  169. if (row.Table.Columns.Contains("业务人员"))
  170. {
  171. item.WorkflowUserId = GetStaff(row["业务人员"].ToString().Trim());
  172. }
  173. if (row.Table.Columns.Contains("国家(地区)"))
  174. {
  175. item.Country = row["国家(地区)"].ToString().Trim();
  176. }
  177. return item;
  178. }
  179. private int? GetStaff(string v)
  180. {
  181. using (spDbContext spDb = new spDbContext())
  182. {
  183. if (!string.IsNullOrEmpty(v))
  184. {
  185. string temName = v.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)[0];
  186. if (!v.Contains("君龙"))
  187. {
  188. temName = v;
  189. }
  190. var staff = spDb.Staffs.Where<Staff>(s => s.Name == temName).FirstOrDefault();
  191. if (staff != null)
  192. {
  193. return staff.Id;
  194. }
  195. }
  196. return null;
  197. }
  198. }
  199. private PerformanceItem Row2Item_2(DataRow row, CalMonth calMonth)
  200. {
  201. PerformanceItem item = new PerformanceItem();
  202. item.CaseNo = row["我方文号"].ToString().Trim();
  203. item.CaseName = row["案件名称"].ToString().Trim();
  204. item.CaseType = row["案件类型"].ToString().Trim();
  205. item.BusinessType = row["业务类型"].ToString().Trim();
  206. item.Customer = new Customer();
  207. item.Customer.Name = row["客户名称"].ToString().Trim();
  208. item.CaseState = row["案件状态"].ToString().Trim();
  209. item.DoItem = row["处理事项"].ToString().Trim();
  210. if (row.Table.Columns.Contains("国家(地区)"))
  211. {
  212. item.Country = row["国家(地区)"].ToString().Trim();
  213. }
  214. DateTime temDate = new DateTime();
  215. if (DateTime.TryParse(row["内部期限"].ToString().Trim(), out temDate))
  216. {
  217. item.InternalDate = temDate;
  218. }
  219. if (DateTime.TryParse(row["客户期限"].ToString().Trim(), out temDate))
  220. {
  221. item.CustomerLimitDate = temDate;
  222. }
  223. if (DateTime.TryParse(row["初稿日"].ToString().Trim(), out temDate))
  224. {
  225. item.FirstDraftDate = temDate;
  226. }
  227. if (DateTime.TryParse(row["定稿日"].ToString().Trim(), out temDate))
  228. {
  229. item.FinalizationDate = temDate;
  230. }
  231. if (DateTime.TryParse(row["委案日期"].ToString().Trim(), out temDate))
  232. {
  233. item.EntrustingDate = temDate;
  234. }
  235. if (DateTime.TryParse(row["处理事项完成日"].ToString().Trim(), out temDate))
  236. {
  237. item.FinishedDate = temDate;
  238. }
  239. item.DoItemState = row["处理状态"].ToString().Trim();
  240. string strHandler = "";
  241. if (row.Table.Columns.Contains("处理人"))
  242. {
  243. strHandler = row["处理人"].ToString().Trim();
  244. }
  245. else
  246. {
  247. if (row.Table.Columns.Contains("案件处理人"))
  248. {
  249. strHandler = row["案件处理人"].ToString().Trim();
  250. }
  251. }
  252. string[] temHandlers = strHandler.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  253. item.ItemStaffs = new List<ItemStaff>();
  254. foreach (string name in temHandlers)
  255. {
  256. ItemStaff itemStaff = new ItemStaff();
  257. string temName = name.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)[0];
  258. if (!name.Contains("君龙"))
  259. {
  260. temName = name.Trim();
  261. }
  262. int? iTem = GetStaff(temName);
  263. if ((iTem != null))
  264. {
  265. //itemStaff.Item = item;
  266. itemStaff.DoPersonId = iTem.Value;
  267. item.ItemStaffs.Add(itemStaff);
  268. }
  269. else
  270. {
  271. itemStaff.DoPerson = new Staff()
  272. {
  273. Name = temName,
  274. Account = temName,
  275. Password = utility.MD5Utility.GetMD5("12345678"),
  276. IsCalPerformsnce = false,
  277. Status = "试用期",
  278. StaffGradeId = 4
  279. };
  280. item.ItemStaffs.Add(itemStaff);
  281. }
  282. }
  283. if (item.ItemStaffs.Count == 0)
  284. {
  285. System.Diagnostics.Debug.WriteLine($"没有处理人: {item.CaseNo}\t{item.DoItem}");
  286. }
  287. if (row.Table.Columns.Contains("核稿人"))
  288. {
  289. item.ReviewerId = GetStaff(row["核稿人"].ToString().Trim());
  290. }
  291. else
  292. {
  293. if (row.Table.Columns.Contains("案件核稿人"))
  294. {
  295. item.ReviewerId = GetStaff(row["案件核稿人"].ToString().Trim());
  296. }
  297. }
  298. if (row.Table.Columns.Contains("业务人员"))
  299. {
  300. if (!string.IsNullOrEmpty(row["业务人员"].ToString().Trim()))
  301. {
  302. string name = row["业务人员"].ToString().Trim();
  303. string temName = name.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)[0];
  304. if (!name.Contains("君龙"))
  305. {
  306. temName = name.Trim();
  307. }
  308. item.WorkflowUserId = GetStaff(temName);
  309. }
  310. }
  311. return item;
  312. }
  313. private DataTable GetDataFromIPEasy()
  314. {
  315. HttpClient http = new HttpClient();
  316. var bytRespon = http.GetAsync($"{ConfigHelper.GetSectionValue("IPEasySetting:ipEeasyApi")}/api/ipEasy/getProjectDataFromIPEasy").Result;
  317. if (bytRespon.IsSuccessStatusCode)
  318. {
  319. string result = bytRespon.Content.ReadAsStringAsync().Result.Replace("\"", string.Empty);
  320. byte[] data = Convert.FromBase64String(result);
  321. //byte[] data = bytRespon.Content.ReadAsByteArrayAsync().Result;//Here is the problem
  322. XmlSerializer serializer = new XmlSerializer(typeof(System.Data.DataTable));
  323. MemoryStream memory = new MemoryStream(data);
  324. DataTable dt = (DataTable)serializer.Deserialize(memory);
  325. return dt;
  326. }
  327. else
  328. {
  329. return null;
  330. }
  331. #region old Code
  332. // DataTable dt = new DataTable();
  333. //string strSQL = @"SELECT p_case_info.case_volume as 我方文号,
  334. // p_case_info.case_volume_customer as 客户文号,
  335. // p_case_info.case_name as 案件名称,
  336. // p_case_info.charge_date as 委案日期,
  337. // i_case_status.case_status_zh_cn as 案件状态,
  338. // i_case_type.case_type_zh_cn as 案件类型,
  339. // p_case_info.app_no as 申请号,
  340. // p_case_info.app_date as 申请日,
  341. // i_business_type.business_type_zh_cn as 业务类型,
  342. // STUFF((SELECT ',' + u.cn_name from c_customer_user as cu
  343. // inner join s_user_info as u on u.user_id = cu.user_id where cu.customer_id = p_case_info.customer_id FOR XML PATH('') ),1,1,'') as 流程负责人,
  344. // c_customer.customer_name as 客户名称,
  345. // STUFF((SELECT ',' + u.cn_name from p_revise_user_list as pl
  346. // inner join s_user_info as u on u.user_id = pl.revise_user_id where pl.obj_id = p_case_info.case_id FOR XML PATH('') ),1,1,'') as 案件核稿人,
  347. // STUFF((SELECT ',' + u.cn_name from p_proc_pic_list as pl
  348. // inner join s_user_info as u on u.user_id = pl.pic_id where pl.obj_id = p_case_info.case_id FOR XML PATH('') ),1,1,'') as 案件处理人,
  349. // p_case_info.create_time as 开卷日期,
  350. // STUFF((SELECT ',' + ui.cn_name from p_sales_list as sl
  351. // inner join dbo.s_user_info as ui on ui.user_id = sl.sales_user_id
  352. // where sl.obj_id = p_case_info.case_id AND sl.is_enabled=1 order by sl.seq FOR XML PATH('') ),1,1,'') as 业务人员,
  353. // i_country.country_zh_cn as '国家(地区)'
  354. // from p_case_info
  355. // inner join p_case_advance_info with(nolock) on p_case_info.case_id=p_case_advance_info.case_id
  356. // inner join i_apply_type with(nolock) on i_apply_type.apply_type_id=p_case_info.apply_type_id
  357. // inner join i_case_type with(nolock) on i_case_type.case_type_id=p_case_info.case_type_id
  358. // inner join i_country with(nolock) on i_country.country_id=p_case_info.country_id
  359. // inner join i_case_status with(nolock) on i_case_status.case_status_id=p_case_info.case_status_id
  360. // inner join c_customer with(nolock) on c_customer.customer_id=p_case_info.customer_id
  361. // inner join i_business_type on i_business_type.business_type_id = p_case_info.business_type_id
  362. // inner join s_dept_info on s_dept_info.dept_id = p_case_info.charge_dept_id
  363. // where p_case_info.is_enabled=1 and p_case_info.case_volume like 'S%' and not p_case_info.case_volume like 'SCN%'
  364. // and i_case_status.case_status_zh_cn<>'已完成' and i_case_status.case_status_zh_cn<>'结案'
  365. // order by 我方文号";
  366. //using (var conn = new SqlConnection(ConfigHelper.GetSectionValue("IPEasySetting:ConnectionStrings")))
  367. //{
  368. // try
  369. // {
  370. // conn.Open();
  371. // using (var cmd = conn.CreateCommand())
  372. // {
  373. // cmd.CommandText = strSQL;
  374. // cmd.CommandType = CommandType.Text;
  375. // using (var reader = cmd.ExecuteReader())
  376. // {
  377. // dt.Load(reader);
  378. // }
  379. // }
  380. // }
  381. // catch (Exception ex)
  382. // {
  383. // throw ex;
  384. // }
  385. //}
  386. //return dt;
  387. #endregion
  388. }
  389. }
  390. }