You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

868 lines
45 KiB

2 years ago
  1. using Aspose.Cells;
  2. using EasyBL.WebApi.Message;
  3. using Entity.Sugar;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using SqlSugar.Base;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Data;
  11. using System.IO;
  12. using System.Linq;
  13. using EasyBL;
  14. using Entity.ViewModels;
  15. namespace EasyBL.WEBAPP.OPM
  16. {
  17. public class Exhibition_UpdService : ServiceBase
  18. {
  19. #region 獲取參加該展覽的所有廠商
  20. /// <summary>
  21. /// 獲取參加該展覽的所有廠商
  22. /// </summary>
  23. /// <param name="i_crm">todo: describe i_crm parameter on GetCustomers</param>
  24. /// <returns></returns>
  25. public ResponseMessage GetCustomers(RequestMessage i_crm)
  26. {
  27. ResponseMessage rm = null;
  28. string sMsg = null;
  29. var db = SugarBase.GetIntance();
  30. try
  31. {
  32. do
  33. {
  34. var sSN = _fetchString(i_crm, @"SN");
  35. var sFlag = _fetchString(i_crm, @"Flag");
  36. var SList = new List<ExistingCustomerInfo>();
  37. var saCustomers = new List<Dictionary<string, object>>();
  38. var saCustomersD = new List<Dictionary<string, object>>();
  39. //出口
  40. var saExport = db.Queryable<OTB_OPM_ExportExhibition, OTB_CRM_Customers>((t1, t2) => new object[] {
  41. JoinType.Left,t1.Agent==t2.guid && t1.OrgID==t2.OrgID && t2.Effective == "Y"})
  42. .Select((t1, t2) => new { t1.IsVoid, t1.OrgID, t1.ExhibitionNO, t1.Exhibitors, t1.Agent, AgentCName = t2.CustomerCName, AgentEName = t2.CustomerEName, t1.AgentContactorName, t1.AgentTelephone }).MergeTable()
  43. .Where(it => it.ExhibitionNO == sSN && it.OrgID == i_crm.ORIGID && it.IsVoid == "N").ToList();
  44. if (saExport.Count > 0)
  45. {
  46. foreach (var opm in saExport)
  47. {
  48. if (!string.IsNullOrEmpty(opm.Exhibitors))
  49. {
  50. var saExhibitors = (JArray)JsonConvert.DeserializeObject(opm.Exhibitors);
  51. foreach (JObject jo in saExhibitors)
  52. {
  53. if (jo[@"SupplierID"] != null && jo[@"SupplierID"].ToString() != @"")
  54. //if (jo[@"SupplierID"] != null && jo[@"SupplierID"].ToString() != @"" && !saCustomers.Any(x => (x[@"guid"].ToString() == jo[@"SupplierID"].ToString())))
  55. {
  56. var dic = new Dictionary<string, object>
  57. {
  58. { @"RowIndex", saCustomers.Count + 1 },
  59. { @"guid", jo[@"SupplierID"].ToString() },
  60. { @"AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  61. { @"CustomerCName", jo[@"SupplierName"] == null ? @"" : jo[@"SupplierName"].ToString() },
  62. { @"CustomerEName", jo[@"SupplierEName"] == null ? @"" : jo[@"SupplierEName"].ToString() },
  63. { @"ContactorName", jo[@"ContactorName"] == null ? @"" : jo[@"ContactorName"].ToString() },
  64. { @"Telephone", jo[@"Telephone"] == null ? @"" : jo[@"Telephone"].ToString() }
  65. };
  66. saCustomers.Add(dic);
  67. }
  68. }
  69. }
  70. //if (!string.IsNullOrEmpty(opm.Agent) && !saCustomers.Any(x => (x["guid"].ToString() == opm.Agent)))
  71. //{
  72. // Dictionary<string, object> dic = new Dictionary<string, object>
  73. // {
  74. // { "RowIndex", saCustomers.Count + 1 },
  75. // { "guid", opm.Agent },
  76. // { "AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  77. // { "CustomerCName", "" },
  78. // { "CustomerEName", "" },
  79. // { "ContactorName", opm.AgentContactorName },
  80. // { "Telephone", opm.AgentTelephone }
  81. // };
  82. // saCustomers.Add(dic);
  83. //}
  84. }
  85. }
  86. //進口
  87. var saImport = db.Queryable<OTB_OPM_ImportExhibition, OTB_CRM_Customers, OTB_CRM_Customers>((t1, t2, t3) => new object[] {
  88. JoinType.Left,t1.Supplier==t2.guid && t1.OrgID==t2.OrgID && t2.Effective == "Y",
  89. JoinType.Left,t1.Agent==t3.guid && t1.OrgID==t3.OrgID && t3.Effective == "Y" })
  90. .Select((t1, t2, t3) => new { t1.IsVoid, t1.OrgID, t1.ExhibitionNO, t1.Supplier, t1.Agent, t2.CustomerCName, t2.CustomerEName, AgentCName = t3.CustomerCName, AgentEName = t3.CustomerEName, t1.ContactorName, t1.Telephone, t1.AgentContactorName, t1.AgentTelephone }).MergeTable()
  91. .Where(it => it.ExhibitionNO == sSN && it.OrgID == i_crm.ORIGID && it.IsVoid == "N").ToList();
  92. if (saImport.Count > 0)
  93. {
  94. foreach (var opm in saImport)
  95. {
  96. if (!string.IsNullOrEmpty(opm.Supplier))
  97. //if (!string.IsNullOrEmpty(opm.Supplier) && !saCustomers.Any(x => (x[@"guid"].ToString() == opm.Supplier)))
  98. {
  99. var dic = new Dictionary<string, object>
  100. {
  101. { @"RowIndex", saCustomers.Count + 1 },
  102. { @"guid", opm.Supplier },
  103. { @"AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  104. { @"CustomerCName", opm.CustomerCName },
  105. { @"CustomerEName", opm.CustomerEName },
  106. { @"ContactorName", opm.ContactorName },
  107. { @"Telephone", opm.Telephone }
  108. };
  109. saCustomers.Add(dic);
  110. }
  111. //if (!string.IsNullOrEmpty(opm.Agent) && !saCustomers.Any(x => (x["guid"].ToString() == opm.Agent)))
  112. //{
  113. // Dictionary<string, object> dic = new Dictionary<string, object>
  114. // {
  115. // { "RowIndex", saCustomers.Count + 1 },
  116. // { "guid", opm.Agent },
  117. // { "AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  118. // { "CustomerCName", "" },
  119. // { "CustomerEName", "" },
  120. // { "ContactorName", opm.AgentContactorName },
  121. // { "Telephone", opm.AgentTelephone }
  122. // };
  123. // saCustomers.Add(dic);
  124. //}
  125. }
  126. }
  127. //其他
  128. var saOther = db.Queryable<OTB_OPM_OtherExhibition, OTB_CRM_Customers, OTB_CRM_Customers>((t1, t2, t3) => new object[] {
  129. JoinType.Left,t1.Supplier==t2.guid && t1.OrgID==t2.OrgID && t2.Effective == "Y",
  130. JoinType.Left,t1.Agent==t3.guid && t1.OrgID==t3.OrgID && t3.Effective == "Y"})
  131. .Select((t1, t2, t3) => new { t1.IsVoid, t1.OrgID, t1.ExhibitionNO, t1.Supplier, t1.Agent, t2.CustomerCName, t2.CustomerEName, AgentCName = t3.CustomerCName, AgentEName = t3.CustomerEName, t1.ContactorName, t1.Telephone, t1.AgentContactorName, t1.AgentTelephone }).MergeTable()
  132. .Where(it => it.ExhibitionNO == sSN && it.OrgID == i_crm.ORIGID && it.IsVoid == "N").ToList();
  133. if (saOther.Count > 0)
  134. {
  135. foreach (var opm in saOther)
  136. {
  137. if (!string.IsNullOrEmpty(opm.Supplier))
  138. //if (!string.IsNullOrEmpty(opm.Supplier) && !saCustomers.Any(x => (x[@"guid"].ToString() == opm.Supplier)))
  139. {
  140. var dic = new Dictionary<string, object>
  141. {
  142. { @"RowIndex", saCustomers.Count + 1 },
  143. { @"guid", opm.Supplier },
  144. { @"AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  145. { @"CustomerCName", opm.CustomerCName },
  146. { @"CustomerEName", opm.CustomerEName },
  147. { @"ContactorName", opm.ContactorName },
  148. { @"Telephone", opm.Telephone }
  149. };
  150. saCustomers.Add(dic);
  151. }
  152. //if (!string.IsNullOrEmpty(opm.Agent) && !saCustomers.Any(x => (x["guid"].ToString() == opm.Agent)))
  153. //{
  154. // Dictionary<string, object> dic = new Dictionary<string, object>
  155. // {
  156. // { "RowIndex", saCustomers.Count + 1 },
  157. // { "guid", opm.Agent },
  158. // { "AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  159. // { "CustomerCName", "" },
  160. // { "CustomerEName", "" },
  161. // { "ContactorName", opm.AgentContactorName },
  162. // { "Telephone", opm.AgentTelephone }
  163. // };
  164. // saCustomers.Add(dic);
  165. //}
  166. }
  167. }
  168. //其他(駒驛)
  169. var saOtherTG = db.Queryable<OTB_OPM_OtherExhibitionTG, OTB_CRM_Customers>((t1, t2) => new object[] {
  170. JoinType.Left,t1.Agent==t2.guid && t1.OrgID==t2.OrgID && t2.Effective == "Y"})
  171. .Select((t1, t2) => new {t1.IsVoid, t1.OrgID, t1.ExhibitionNO, t1.Exhibitors, t1.Agent, AgentCName = t2.CustomerCName, AgentEName = t2.CustomerEName, t1.AgentContactorName, t1.AgentTelephone }).MergeTable()
  172. .Where(it => it.ExhibitionNO == sSN && it.OrgID == i_crm.ORIGID && it.IsVoid == "N").ToList();
  173. if (saOtherTG.Count > 0)
  174. {
  175. foreach (var opm in saOtherTG)
  176. {
  177. if (!string.IsNullOrEmpty(opm.Exhibitors))
  178. {
  179. var saExhibitors = (JArray)JsonConvert.DeserializeObject(opm.Exhibitors);
  180. foreach (JObject jo in saExhibitors)
  181. {
  182. if (jo[@"SupplierID"] != null && jo[@"SupplierID"].ToString() != @"")
  183. //if (jo[@"SupplierID"] != null && jo[@"SupplierID"].ToString() != @"" && !saCustomers.Any(x => (x[@"guid"].ToString() == jo[@"SupplierID"].ToString())))
  184. {
  185. var dic = new Dictionary<string, object>
  186. {
  187. { @"RowIndex", saCustomers.Count + 1 },
  188. { @"guid", jo[@"SupplierID"].ToString() },
  189. { @"AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  190. { @"CustomerCName", jo[@"SupplierName"] == null ? @"" : jo[@"SupplierName"].ToString() },
  191. { @"CustomerEName", jo[@"SupplierEName"] == null ? @"" : jo[@"SupplierEName"].ToString() },
  192. { @"ContactorName", jo[@"ContactorName"] == null ? @"" : jo[@"ContactorName"].ToString() },
  193. { @"Telephone", jo[@"Telephone"] == null ? @"" : jo[@"Telephone"].ToString() }
  194. };
  195. saCustomers.Add(dic);
  196. }
  197. }
  198. }
  199. //if (!string.IsNullOrEmpty(opm.Agent) && !saCustomers.Any(x => (x["guid"].ToString() == opm.Agent)))
  200. //{
  201. // Dictionary<string, object> dic = new Dictionary<string, object>
  202. // {
  203. // { "RowIndex", saCustomers.Count + 1 },
  204. // { "guid", opm.Agent },
  205. // { "AgentCName", string.IsNullOrEmpty(opm.AgentCName) ? opm.AgentEName : opm.AgentCName },
  206. // { "CustomerCName", "" },
  207. // { "CustomerEName", "" },
  208. // { "ContactorName", opm.AgentContactorName },
  209. // { "Telephone", opm.AgentTelephone }
  210. // };
  211. // saCustomers.Add(dic);
  212. //}
  213. }
  214. }
  215. rm = new SuccessResponseMessage(null, i_crm);
  216. var saGCs = saCustomers.GroupBy(x => x["guid"].ToString());
  217. foreach (var item in saGCs)
  218. {
  219. var AllAgentCName = item.Where(c => c["AgentCName"] != null).Select(c => c["AgentCName"]).Distinct().ToList();
  220. var Customer = item.First();
  221. Dictionary<string, object> dic = new Dictionary<string, object>
  222. {
  223. { "RowIndex", saCustomersD.Count + 1 },
  224. { "guid", Customer["guid"] },
  225. { "AgentCName", string.Join(" ◆",AllAgentCName) },
  226. { "CustomerCName", Customer["CustomerCName"] },
  227. { "CustomerEName", Customer["CustomerEName"] },
  228. { "ContactorName", Customer["ContactorName"] },
  229. { "Telephone", Customer["Telephone"] }
  230. };
  231. saCustomersD.Add(dic);
  232. }
  233. if (sFlag == @"export")
  234. {
  235. var dicHeader = new Dictionary<string, object> {
  236. {@"RowIndex",@"項次" },
  237. { @"AgentCName",@"國外代理" },
  238. { @"CustomerCName",@"公司中文名稱" },
  239. { @"CustomerEName",@"公司英文名稱" },
  240. { @"ContactorName",@"聯絡人" },
  241. { @"Telephone",@"聯絡電話/手機" }
  242. };
  243. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(x => x.SN == sSN.ObjToInt());
  244. var bOk = new ExcelService().CreateExcel(saCustomersD, out string sPath, dicHeader, oExhibition.Exhibitioname_TW, oExhibition.Exhibitioname_TW);
  245. rm.DATA.Add(BLWording.REL, sPath);
  246. }
  247. else
  248. {
  249. rm.DATA.Add(BLWording.REL, saCustomersD);
  250. }
  251. } while (false);
  252. }
  253. catch (Exception ex)
  254. {
  255. sMsg = Util.GetLastExceptionMsg(ex);
  256. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"GetCustomers(獲取參加該展覽的所有廠商)", @"", @"", @"");
  257. }
  258. finally
  259. {
  260. if (null != sMsg)
  261. {
  262. rm = new ErrorResponseMessage(sMsg, i_crm);
  263. }
  264. }
  265. return rm;
  266. }
  267. #endregion 獲取參加該展覽的所有廠商
  268. #region 獲取展覽資料
  269. /// <summary>
  270. /// 獲取展覽資料
  271. /// </summary>
  272. /// <param name="i_crm">todo: describe i_crm parameter on GetExhibitions</param>
  273. /// <returns></returns>
  274. public ResponseMessage GetExhibitions(RequestMessage i_crm)
  275. {
  276. ResponseMessage rm = null;
  277. string sMsg = null;
  278. var db = SugarBase.GetIntance();
  279. try
  280. {
  281. do
  282. {
  283. var sSN = _fetchString(i_crm, @"SN");
  284. var saExhibition = db.Queryable<OTB_OPM_Exhibition>()
  285. .OrderBy(x => x.ExhibitionCode, OrderByType.Desc)
  286. .Where(x => x.OrgID == i_crm.ORIGID && x.Effective == @"Y")
  287. .WhereIF(!string.IsNullOrEmpty(sSN), x => x.SN == sSN.ObjToInt())
  288. .Select(x => new
  289. {
  290. x.SN,
  291. x.ExhibitionCode,
  292. x.Exhibitioname_TW,
  293. x.Exhibitioname_EN,
  294. ExhibitioFullName = SqlFunc.IIF(SqlFunc.HasValue(x.ExhibitioShotName_TW), "(" + SqlFunc.IsNull(x.ExhibitioShotName_TW, "") + ")", "") + x.Exhibitioname_TW,
  295. x.ExhibitioShotName_TW,
  296. x.ExhibitionDateStart,
  297. x.ExhibitionDateEnd,
  298. x.ExhibitionAddress
  299. })
  300. .ToList();
  301. rm = new SuccessResponseMessage(null, i_crm);
  302. rm.DATA.Add(BLWording.REL, saExhibition);
  303. } while (false);
  304. }
  305. catch (Exception ex)
  306. {
  307. sMsg = Util.GetLastExceptionMsg(ex);
  308. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"GetExhibitions(獲取展覽資料)", @"", @"", @"");
  309. }
  310. finally
  311. {
  312. if (null != sMsg)
  313. {
  314. rm = new ErrorResponseMessage(sMsg, i_crm);
  315. }
  316. }
  317. return rm;
  318. }
  319. /// <summary>
  320. /// 獲取展覽資料
  321. /// </summary>
  322. /// <param name="i_crm">todo: describe i_crm parameter on GetExhibitions</param>
  323. /// <returns></returns>
  324. public ResponseMessage CheckExhibitionName(RequestMessage i_crm)
  325. {
  326. ResponseMessage rm = null;
  327. string sMsg = null;
  328. var db = SugarBase.GetIntance();
  329. try
  330. {
  331. do
  332. {
  333. var Type = _fetchString(i_crm, @"Type").ToLower();
  334. var sSN = _fetchString(i_crm, @"SN");
  335. int.TryParse(sSN, out int SN);
  336. var sExhibitioname_TW = _fetchString(i_crm, @"Exhibitioname_TW");
  337. var sExhibitioname_TW_Alt = GetTWSameName(sExhibitioname_TW);
  338. var sExhibitioShotName_TW = _fetchString(i_crm, @"ExhibitioShotName_TW");
  339. var sExhibitioShotName_TW_Alt = GetTWSameName(sExhibitioShotName_TW);
  340. var saExhibitions = db.Queryable<OTB_OPM_Exhibition>().Where(x => x.OrgID == i_crm.ORIGID && x.Effective == @"Y" && x.SN != SN).ToList();
  341. var RepeatShotName = saExhibitions.Any(x => x.ExhibitioShotName_TW.Trim() == sExhibitioShotName_TW || x.ExhibitioShotName_TW.Trim() == sExhibitioShotName_TW_Alt);
  342. var RepeatNameTW = saExhibitions.Any(x => x.Exhibitioname_TW.Trim() == sExhibitioname_TW || x.Exhibitioname_TW.Trim() == sExhibitioname_TW_Alt);
  343. if (RepeatShotName && RepeatNameTW)
  344. {
  345. sMsg = "活動/展覽簡稱、展覽名稱重複,請重新輸入。";
  346. }
  347. else if (RepeatShotName)
  348. {
  349. sMsg = "活動/展覽簡稱重複,請重新輸入。";
  350. }
  351. else if (RepeatNameTW)
  352. {
  353. sMsg = "展覽名稱重複,請重新輸入。";
  354. }
  355. rm = new SuccessResponseMessage(null, i_crm);
  356. rm.DATA.Add(BLWording.REL, "OK");
  357. } while (false);
  358. }
  359. catch (Exception ex)
  360. {
  361. sMsg = Util.GetLastExceptionMsg(ex);
  362. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"GetExhibitions(獲取展覽資料)", @"", @"", @"");
  363. }
  364. finally
  365. {
  366. if (null != sMsg)
  367. {
  368. rm = new ErrorResponseMessage(sMsg, i_crm);
  369. }
  370. }
  371. return rm;
  372. }
  373. private string GetTWSameName(string OriName)
  374. {
  375. if (string.IsNullOrWhiteSpace(OriName))
  376. {
  377. return "";
  378. }
  379. if (OriName.Contains("台灣"))
  380. {
  381. return OriName.Replace("台灣", "臺灣");
  382. }
  383. else
  384. {
  385. return OriName.Replace("臺灣", "台灣");
  386. }
  387. }
  388. #endregion 獲取展覽資料
  389. #region 獲取匯入廠商資料
  390. /// <summary>
  391. /// 獲取匯入廠商資料
  392. /// </summary>
  393. /// <param name="i_crm">todo: describe i_crm parameter on GetImportCustomers</param>
  394. /// <returns></returns>
  395. public ResponseMessage GetImportCustomers(RequestMessage i_crm)
  396. {
  397. ResponseMessage rm = null;
  398. string sMsg = null;
  399. var db = SugarBase.GetIntance();
  400. try
  401. {
  402. do
  403. {
  404. var pml = new PageModel
  405. {
  406. PageIndex = _fetchInt(i_crm, @"pageIndex"),
  407. PageSize = _fetchInt(i_crm, @"pageSize")
  408. };
  409. var iPageCount = 0;
  410. var sSortField = _fetchString(i_crm, @"sortField");
  411. var sSortOrder = _fetchString(i_crm, @"sortOrder");
  412. var iSN = _fetchInt(i_crm, @"SN");
  413. var sExhibitionArea = _fetchString(i_crm, @"ExhibitionArea");
  414. var sMuseumMumber = _fetchString(i_crm, @"MuseumMumber");
  415. var sCustomerCName = _fetchString(i_crm, @"CustomerCName");
  416. var sCustomerEName = _fetchString(i_crm, @"CustomerEName");
  417. var sUniCode = _fetchString(i_crm, @"UniCode");
  418. var sContactor = _fetchString(i_crm, @"Contactor");
  419. var sTelephone = _fetchString(i_crm, @"Telephone");
  420. var sEmail = _fetchString(i_crm, @"Email");
  421. var sAddress = _fetchString(i_crm, @"Address");
  422. var sMemo = _fetchString(i_crm, @"Memo");
  423. pml.DataList = db.Queryable<OTB_CRM_ImportCustomers, OTB_OPM_Exhibition, OTB_WSM_PackingOrder>
  424. ((t1, t2, t3) => new object[]
  425. {
  426. JoinType.Inner, t1.OrgID == t2.OrgID && t1.ExhibitionNO == t2.SN,
  427. JoinType.Left, t1.OrgID == t3.OrgID && t1.guid == t3.CustomerId
  428. }
  429. )
  430. .Where((t1, t2, t3) => t1.ExhibitionNO == iSN && t1.OrgID == i_crm.ORIGID)
  431. .WhereIF(!String.IsNullOrEmpty(sExhibitionArea), (t1, t2, t3) => t1.ExhibitionArea.Contains(sExhibitionArea))
  432. .WhereIF(!String.IsNullOrEmpty(sMuseumMumber), (t1, t2, t3) => t1.MuseumMumber.Contains(sMuseumMumber))
  433. .WhereIF(!String.IsNullOrEmpty(sCustomerCName), (t1, t2, t3) => t1.CustomerCName.Contains(sCustomerCName))
  434. .WhereIF(!String.IsNullOrEmpty(sCustomerEName), (t1, t2, t3) => t1.CustomerEName.Contains(sCustomerEName))
  435. .WhereIF(!String.IsNullOrEmpty(sUniCode), (t1, t2, t3) => t1.UniCode.Contains(sUniCode))
  436. .WhereIF(!String.IsNullOrEmpty(sContactor), (t1, t2, t3) => t1.Contactor.Contains(sContactor))
  437. .WhereIF(!String.IsNullOrEmpty(sTelephone), (t1, t2, t3) => t1.Telephone.Contains(sTelephone))
  438. .WhereIF(!String.IsNullOrEmpty(sEmail), (t1, t2, t3) => t1.Email.Contains(sEmail))
  439. .WhereIF(!String.IsNullOrEmpty(sAddress), (t1, t2, t3) => t1.Address.Contains(sAddress))
  440. .WhereIF(!String.IsNullOrEmpty(sMemo), (t1, t2, t3) => t1.Memo.Contains(sMemo))
  441. .Select((t1, t2, t3) => new View_CRM_ImportCustomers
  442. {
  443. guid = SqlFunc.GetSelfAndAutoFill(t1.guid),
  444. Exhibitioname_TW = t2.Exhibitioname_TW,
  445. Exhibitioname_EN = t2.Exhibitioname_EN,
  446. IsAppoint = SqlFunc.IIF(SqlFunc.HasValue(t1.AppointNO), "Y", "N")
  447. })
  448. .MergeTable()
  449. .OrderBy(sSortField, sSortOrder)
  450. .ToPageList(pml.PageIndex, pml.PageSize, ref iPageCount);
  451. pml.Total = iPageCount;
  452. rm = new SuccessResponseMessage(null, i_crm);
  453. rm.DATA.Add(BLWording.REL, pml);
  454. } while (false);
  455. }
  456. catch (Exception ex)
  457. {
  458. sMsg = Util.GetLastExceptionMsg(ex);
  459. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"GetImportCustomers(獲取匯入廠商資料)", @"", @"", @"");
  460. }
  461. finally
  462. {
  463. if (null != sMsg)
  464. {
  465. rm = new ErrorResponseMessage(sMsg, i_crm);
  466. }
  467. }
  468. return rm;
  469. }
  470. #endregion 獲取匯入廠商資料
  471. #region 匯入廠商資料
  472. /// <summary>
  473. /// 匯入廠商資料
  474. /// </summary>
  475. /// <param name="i_crm">todo: describe i_crm parameter on ImportCustomers</param>
  476. /// <returns></returns>
  477. public ResponseMessage ImportCustomers(RequestMessage i_crm)
  478. {
  479. ResponseMessage rm = null;
  480. string sMsg = null;
  481. var db = SugarBase.GetIntance();
  482. try
  483. {
  484. do
  485. {
  486. var sFileId = _fetchString(i_crm, @"FileId");
  487. var sFileName = _fetchString(i_crm, @"FileName");
  488. var iSN = _fetchInt(i_crm, @"SN");
  489. var sRoot = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"OutFiles\Temporary\");//Word模版路徑
  490. var sfileName = sFileName.Split(new string[] { @"." }, StringSplitOptions.RemoveEmptyEntries);
  491. var sSubFileName = sfileName.LastOrDefault(); //副檔名
  492. sFileName = sRoot + sFileId + @"." + sSubFileName;
  493. var book = new Workbook(sFileName);
  494. var sheet = book.Worksheets[0];
  495. var cells = sheet.Cells;
  496. var tbFeeItems = cells.ExportDataTableAsString(1, 0, cells.MaxDataRow, cells.MaxDataColumn + 1, false);
  497. if (tbFeeItems.Rows.Count > 0)
  498. {
  499. var saImportCustomers = new List<OTB_CRM_ImportCustomers>();
  500. foreach (DataRow row in tbFeeItems.Rows)
  501. {
  502. try
  503. {
  504. var sExhibitionArea = row[@"Column1"].ToString();// 展覽地區
  505. var sMuseumMumber = row[@"Column2"].ToString();// 攤位編號
  506. var sUniCode = row[@"Column3"].ToString();// 統一編號
  507. var sCustomerCName = row[@"Column4"].ToString();// 公司中文名稱
  508. var sCustomerEName = row[@"Column5"].ToString();// 公司英文名稱
  509. var sContactor = row[@"Column7"].ToString();// 聯絡人1
  510. var sTelephone = row[@"Column8"].ToString();// 電話
  511. var sAddress = row[@"Column9"].ToString();//地址
  512. var sEmail = row[@"Column10"].ToString();// 郵箱
  513. var sMemo = row[@"Column11"].ToString();// 備註
  514. sCustomerCName = sCustomerCName.Trim();// 公司中文名字去空格
  515. var saImportCustomers_Exsit = db.Queryable<OTB_CRM_ImportCustomers>()
  516. .Where(x => x.OrgID == i_crm.ORIGID)
  517. .Where(x => x.CustomerCName == sCustomerCName)
  518. .ToList();
  519. var saCustomers_Exsit = db.Queryable<OTB_CRM_Customers>()
  520. .Where(x => x.OrgID == i_crm.ORIGID && x.CustomerCName == sCustomerCName)
  521. .ToList();
  522. var saImportCustomers_Cur = saImportCustomers_Exsit.Where(x => x.ExhibitionNO == iSN).ToList();
  523. if (saImportCustomers_Cur.Count == 0)
  524. {
  525. var oImportCustomers = new OTB_CRM_ImportCustomers
  526. {
  527. guid = Guid.NewGuid().ToString(),
  528. OrgID = i_crm.ORIGID,
  529. ExhibitionNO = iSN,
  530. MuseumMumber = sMuseumMumber,
  531. ExhibitionArea = sExhibitionArea,
  532. CustomerCName = sCustomerCName,
  533. CustomerEName = sCustomerEName,
  534. UniCode = sUniCode,
  535. Contactor = sContactor,
  536. Telephone = sTelephone,
  537. Address = sAddress,
  538. Email = sEmail,
  539. Memo = sMemo,
  540. IsFormal = saCustomers_Exsit.Count > 0 ? true : saImportCustomers_Exsit.Count > 0 ? saImportCustomers_Exsit.First().IsFormal : false,
  541. CreateUser = i_crm.USERID,
  542. CreateDate = DateTime.Now,
  543. ModifyUser = i_crm.USERID,
  544. ModifyDate = DateTime.Now
  545. };
  546. if (oImportCustomers.IsFormal == true)
  547. {
  548. oImportCustomers.FormalGuid = saCustomers_Exsit.Count > 0 ? saCustomers_Exsit.First().guid : saImportCustomers_Exsit.First().guid;
  549. }
  550. saImportCustomers.Add(oImportCustomers);
  551. }
  552. }
  553. catch { }
  554. }
  555. if (saImportCustomers.Count > 0)
  556. {
  557. db.Insertable(saImportCustomers).ExecuteCommand();
  558. }
  559. }
  560. rm = new SuccessResponseMessage(null, i_crm);
  561. rm.DATA.Add(BLWording.REL, true);
  562. } while (false);
  563. }
  564. catch (Exception ex)
  565. {
  566. sMsg = Util.GetLastExceptionMsg(ex);
  567. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"ImportCustomers(匯入廠商資料)", @"", @"", @"");
  568. }
  569. finally
  570. {
  571. if (null != sMsg)
  572. {
  573. rm = new ErrorResponseMessage(sMsg, i_crm);
  574. }
  575. }
  576. return rm;
  577. }
  578. #endregion 匯入廠商資料
  579. #region 匯入廠商資料匯出
  580. /// <summary>
  581. /// 匯入廠商資料匯出
  582. /// </summary>
  583. /// <param name="i_crm">todo: describe i_crm parameter on ImportCustomers</param>
  584. /// <returns></returns>
  585. public ResponseMessage ExportCustomers(RequestMessage i_crm)
  586. {
  587. ResponseMessage rm = null;
  588. string sMsg = null;
  589. var db = SugarBase.GetIntance();
  590. try
  591. {
  592. do
  593. {
  594. var iSN = _fetchInt(i_crm, @"SN");
  595. var saImportCustomers = db.Queryable<OTB_CRM_ImportCustomers, OTB_OPM_Exhibition, OTB_WSM_PackingOrder>
  596. ((t1, t2, t3) => new object[]
  597. {
  598. JoinType.Inner, t1.OrgID == t2.OrgID && t1.ExhibitionNO == t2.SN,
  599. JoinType.Left, t1.OrgID == t3.OrgID && t1.guid == t3.CustomerId
  600. }
  601. )
  602. .Where((t1, t2, t3) => t1.ExhibitionNO == iSN && t1.OrgID == i_crm.ORIGID)
  603. .Select((t1, t2, t3) => new View_CRM_ImportCustomers
  604. {
  605. guid = SqlFunc.GetSelfAndAutoFill(t1.guid),
  606. Exhibitioname_TW = t2.Exhibitioname_TW,
  607. Exhibitioname_EN = t2.Exhibitioname_EN,
  608. IsAppoint = SqlFunc.IIF(SqlFunc.HasValue(t1.AppointNO), "已預約", "未預約")
  609. })
  610. .ToPageList(1, 100000);
  611. foreach (var item in saImportCustomers)
  612. {
  613. item.CustomerShotEName = (bool)item.IsFormal ? "已轉正" : "未轉正";
  614. }
  615. // var dtImportCustomers = saImportCustomers.ListToDataTable();
  616. const string sFileName = "匯入廠商匯出資料";
  617. var oHeader = new Dictionary<string, string>
  618. {
  619. { "RowIndex", "項次" },
  620. { "ExhibitionArea", "展區" },
  621. { "MuseumMumber", "攤位" },
  622. { "CustomerCName", "客戶中文名稱" },
  623. { "CustomerEName", "客戶英文名稱" },
  624. { "UniCode", "統編號碼" },
  625. { "Contactor", "聯絡人" },
  626. { "Telephone", "電話" },
  627. { "Email", "郵箱" },
  628. { "Address", "地址" },
  629. { "Memo", "備註" },
  630. { "CustomerShotEName", "轉正狀態" },//暫時用英文簡稱代替
  631. { "IsAppoint", "預約狀態" }
  632. };
  633. var dicAlain = ExcelService.GetExportAlain(oHeader, "RowIndex,MuseumMumber,UniCode,Contactor,Telephone,Email,CustomerShotEName,IsAppoint");
  634. var listMerge = new List<Dictionary<string, int>>();
  635. var bOk = new ExcelService().CreateExcelByList(saImportCustomers, out string sPath, oHeader, dicAlain, sFileName);
  636. rm = new SuccessResponseMessage(null, i_crm);
  637. rm.DATA.Add(BLWording.REL, sPath);
  638. } while (false);
  639. }
  640. catch (Exception ex)
  641. {
  642. sMsg = Util.GetLastExceptionMsg(ex);
  643. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"ExportCustomers(匯入廠商資料匯出)", @"", @"", @"");
  644. }
  645. finally
  646. {
  647. if (null != sMsg)
  648. {
  649. rm = new ErrorResponseMessage(sMsg, i_crm);
  650. }
  651. }
  652. return rm;
  653. }
  654. #endregion 匯入廠商資料匯出
  655. #region 修改匯入廠商轉正標記
  656. /// <summary>
  657. /// 修改匯入廠商轉正標記
  658. /// </summary>
  659. /// <param name="i_crm">todo: describe i_crm parameter on UpdateCustomerTag</param>
  660. /// <returns></returns>
  661. public ResponseMessage UpdateCustomerTag(RequestMessage i_crm)
  662. {
  663. ResponseMessage rm = null;
  664. string sMsg = null;
  665. var db = SugarBase.GetIntance();
  666. try
  667. {
  668. do
  669. {
  670. var sPrevId = _fetchString(i_crm, @"PrevId");
  671. var sAfterId = _fetchString(i_crm, @"AfterId");
  672. var oCustomers = db.Queryable<OTB_CRM_Customers>()
  673. .Single(x => x.guid == sAfterId && x.OrgID == i_crm.ORIGID);
  674. var oImportCustomers = db.Queryable<OTB_CRM_ImportCustomers>()
  675. .Single(x => x.guid == sPrevId && x.OrgID == i_crm.ORIGID);
  676. oImportCustomers.IsFormal = true;
  677. oImportCustomers.FormalGuid = oCustomers.guid;
  678. //oImportCustomers.CustomerCName = oCustomers.CustomerCName;
  679. var iRel = db.Updateable(oImportCustomers)
  680. .UpdateColumns(x => new { x.IsFormal, x.FormalGuid, x.CustomerCName })
  681. .Where(x => x.OrgID == i_crm.ORIGID && x.CustomerCName == oImportCustomers.CustomerCName)
  682. .ExecuteCommand();
  683. rm = new SuccessResponseMessage(null, i_crm);
  684. rm.DATA.Add(BLWording.REL, iRel);
  685. } while (false);
  686. }
  687. catch (Exception ex)
  688. {
  689. sMsg = Util.GetLastExceptionMsg(ex);
  690. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"UpdateCustomerTag(修改匯入廠商轉正標記)", @"", @"", @"");
  691. }
  692. finally
  693. {
  694. if (null != sMsg)
  695. {
  696. rm = new ErrorResponseMessage(sMsg, i_crm);
  697. }
  698. }
  699. return rm;
  700. }
  701. #endregion 修改匯入廠商轉正標記
  702. #region 新增匯入廠商
  703. /// <summary>
  704. /// 新增匯入廠商
  705. /// </summary>
  706. /// <param name="i_crm">todo: describe i_crm parameter on InsertImportCustomers</param>
  707. /// <returns></returns>
  708. public ResponseMessage InsertImportCustomers(RequestMessage i_crm)
  709. {
  710. ResponseMessage rm = null;
  711. string sMsg = null;
  712. var db = SugarBase.GetIntance();
  713. try
  714. {
  715. do
  716. {
  717. var sCustomerCName = _fetchString(i_crm, @"CustomerCName");
  718. var iExhibitionNO = _fetchInt(i_crm, @"ExhibitionNO");
  719. if (db.Queryable<OTB_CRM_ImportCustomers>()
  720. .Any(x => x.OrgID == i_crm.ORIGID && x.ExhibitionNO == iExhibitionNO && x.CustomerCName == sCustomerCName))
  721. {
  722. sMsg = "該展覽已存在同名廠商資料";
  723. break;
  724. }
  725. sCustomerCName = sCustomerCName.Trim();
  726. var saImportCustomers_Exsit = db.Queryable<OTB_CRM_ImportCustomers>()
  727. .Where(x => x.OrgID == i_crm.ORIGID && x.CustomerCName == sCustomerCName)
  728. .ToList();
  729. var saCustomers_Exsit = db.Queryable<OTB_CRM_Customers>()
  730. .Where(x => x.OrgID == i_crm.ORIGID && x.CustomerCName == sCustomerCName)
  731. .ToList();
  732. var oImportCustomers_Add = _fetchEntity<OTB_CRM_ImportCustomers>(i_crm);
  733. oImportCustomers_Add.OrgID = i_crm.ORIGID;
  734. oImportCustomers_Add.guid = Guid.NewGuid().ToString();
  735. oImportCustomers_Add.ExhibitionNO = iExhibitionNO;
  736. oImportCustomers_Add.IsFormal = saCustomers_Exsit.Count > 0 ? true : saImportCustomers_Exsit.Count > 0 ? saImportCustomers_Exsit.First().IsFormal : false;
  737. if (oImportCustomers_Add.IsFormal == true)
  738. {
  739. oImportCustomers_Add.FormalGuid = saCustomers_Exsit.Count > 0 ? saCustomers_Exsit.First().guid : saImportCustomers_Exsit.First().guid;
  740. }
  741. var iRel = db.Insertable(oImportCustomers_Add).ExecuteCommand();
  742. rm = new SuccessResponseMessage(null, i_crm);
  743. rm.DATA.Add(BLWording.REL, iRel);
  744. } while (false);
  745. }
  746. catch (Exception ex)
  747. {
  748. sMsg = Util.GetLastExceptionMsg(ex);
  749. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"InsertImportCustomers(新增匯入廠商)", @"", @"", @"");
  750. }
  751. finally
  752. {
  753. if (null != sMsg)
  754. {
  755. if (i_crm.LANG == @"zh")
  756. {
  757. sMsg = ChineseStringUtility.ToSimplified(sMsg);
  758. }
  759. rm = new ErrorResponseMessage(sMsg, i_crm);
  760. }
  761. }
  762. return rm;
  763. }
  764. #endregion 新增匯入廠商
  765. #region 修改匯入廠商
  766. /// <summary>
  767. /// 修改匯入廠商
  768. /// </summary>
  769. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  770. /// <returns></returns>
  771. public ResponseMessage UpdImportCustomers(RequestMessage i_crm)
  772. {
  773. ResponseMessage rm = null;
  774. string sMsg = null;
  775. var db = SugarBase.GetIntance();
  776. try
  777. {
  778. do
  779. {
  780. var oEntity = _fetchEntity<OTB_CRM_ImportCustomers>(i_crm);
  781. var iRel = db.Updateable(oEntity)
  782. .UpdateColumns(x => new
  783. {
  784. x.UniCode,
  785. x.CustomerCName,
  786. x.CustomerEName,
  787. x.ExhibitionArea,
  788. x.Contactor,
  789. x.Telephone,
  790. x.Email,
  791. x.Address,
  792. x.Memo,
  793. x.MuseumMumber
  794. }).ExecuteCommand();
  795. rm = new SuccessResponseMessage(null, i_crm);
  796. rm.DATA.Add(BLWording.REL, iRel);
  797. } while (false);
  798. }
  799. catch (Exception ex)
  800. {
  801. sMsg = Util.GetLastExceptionMsg(ex);
  802. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Exhibition_UpdService), @"展覽管理", @"UpdImportCustomers(修改匯入廠商)", @"", @"", @"");
  803. }
  804. finally
  805. {
  806. if (null != sMsg)
  807. {
  808. if (i_crm.LANG == @"zh")
  809. {
  810. sMsg = ChineseStringUtility.ToSimplified(sMsg);
  811. }
  812. rm = new ErrorResponseMessage(sMsg, i_crm);
  813. }
  814. }
  815. return rm;
  816. }
  817. #endregion 修改匯入廠商
  818. }
  819. }