ImportProjectInfoJob.cs 15 KB

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