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.

1426 lines
70 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyNet;
  3. using Entity;
  4. using Entity.Sugar;
  5. using JumpKick.HttpLib;
  6. using SqlSugar;
  7. using SqlSugar.Base;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text.RegularExpressions;
  13. namespace EasyBL.WEBAPP.CRM
  14. {
  15. public class Customers_UpdService : ServiceBase
  16. {
  17. #region 客戶管理編輯(單筆查詢)
  18. /// <summary>
  19. /// 客戶管理編輯(單筆查詢)
  20. /// </summary>
  21. /// <param name="i_crm"></param>
  22. /// <returns></returns>
  23. public ResponseMessage QueryOne(RequestMessage i_crm)
  24. {
  25. ResponseMessage rm = null;
  26. string sMsg = null;
  27. var db = SugarBase.GetIntance();
  28. try
  29. {
  30. do
  31. {
  32. var sId = _fetchString(i_crm, @"guid");
  33. var oEntity = db.Queryable<OTB_CRM_Customers, OTB_SYS_Members, OTB_SYS_Members>
  34. ((t1, t2, t3) =>
  35. new object[] {
  36. JoinType.Left, t1.OrgID == t2.OrgID && t1.CreateUser == t2.MemberID,
  37. JoinType.Left, t1.OrgID == t3.OrgID && t1.ModifyUser == t3.MemberID
  38. }
  39. )
  40. .Where((t1, t2, t3) => t1.OrgID == i_crm.ORIGID && t1.guid == sId)
  41. .Select((t1, t2, t3) => new OTB_CRM_Customers
  42. {
  43. guid = SqlFunc.GetSelfAndAutoFill(t1.guid),
  44. CreateUserName = t2.MemberName,
  45. ModifyUserName = t3.MemberName
  46. })
  47. .Single();
  48. rm = new SuccessResponseMessage(null, i_crm);
  49. rm.DATA.Add(BLWording.REL, oEntity);
  50. } while (false);
  51. }
  52. catch (Exception ex)
  53. {
  54. sMsg = Util.GetLastExceptionMsg(ex);
  55. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_QryService), "", "QueryOne(客戶管理編輯(單筆查詢))", "", "", "");
  56. }
  57. finally
  58. {
  59. if (null != sMsg)
  60. {
  61. rm = new ErrorResponseMessage(sMsg, i_crm);
  62. }
  63. }
  64. return rm;
  65. }
  66. #endregion 客戶管理編輯(單筆查詢)
  67. #region 客戶管理編輯(單筆查詢)
  68. /// <summary>
  69. /// 客戶管理編輯(單筆查詢)
  70. /// </summary>
  71. /// <param name="i_crm"></param>
  72. /// <returns></returns>
  73. public ResponseMessage QueryCout(RequestMessage i_crm)
  74. {
  75. ResponseMessage rm = null;
  76. string sMsg = null;
  77. var db = SugarBase.GetIntance();
  78. try
  79. {
  80. do
  81. {
  82. var sId = _fetchString(i_crm, @"guid");
  83. var sCustomerShotCName = _fetchString(i_crm, @"CustomerShotCName");
  84. var sUniCode = _fetchString(i_crm, @"UniCode");
  85. var iCount = db.Queryable<OTB_CRM_Customers>()
  86. .Where(x => x.OrgID == i_crm.ORIGID)
  87. .WhereIF(!string.IsNullOrEmpty(sId), x => x.guid != sId)
  88. .WhereIF(!string.IsNullOrEmpty(sCustomerShotCName), x => x.CustomerShotCName == sCustomerShotCName)
  89. .WhereIF(!string.IsNullOrEmpty(sUniCode), x => x.UniCode == sUniCode)
  90. .Count();
  91. rm = new SuccessResponseMessage(null, i_crm);
  92. rm.DATA.Add(BLWording.REL, iCount);
  93. } while (false);
  94. }
  95. catch (Exception ex)
  96. {
  97. sMsg = Util.GetLastExceptionMsg(ex);
  98. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_QryService), "", "QueryOne(客戶管理編輯(單筆查詢))", "", "", "");
  99. }
  100. finally
  101. {
  102. if (null != sMsg)
  103. {
  104. rm = new ErrorResponseMessage(sMsg, i_crm);
  105. }
  106. }
  107. return rm;
  108. }
  109. #endregion 客戶管理編輯(單筆查詢)
  110. /// <summary>
  111. /// 檢查客戶簡稱(不能重複)與統一編號(不能重複),組織不同視為不同
  112. /// </summary>
  113. /// <param name="i_crm"></param>
  114. /// <returns></returns>
  115. private Tuple<bool, string> CheckShortNameAndUniCode(RequestMessage i_crm)
  116. {
  117. var OrgID = i_crm.ORIGID;
  118. if (i_crm.TYPE == "CopySync")
  119. OrgID = _fetchString(i_crm, OTB_CRM_Customers.CN_ORGID);
  120. var db = SugarBase.GetIntance();
  121. var iCount = -1;
  122. try
  123. {
  124. var sTransactiontype = _fetchString(i_crm, @"TransactionType");
  125. var sId = _fetchString(i_crm, @"guid");
  126. var sCustomerShotCName = _fetchString(i_crm, @"CustomerShotCName");
  127. var sUniCode = _fetchString(i_crm, @"UniCode");
  128. if (string.IsNullOrWhiteSpace(sCustomerShotCName))
  129. return new Tuple<bool, string>(false, "客戶簡稱不得為空。");
  130. var sTaxpayerOrgID = _fetchString(i_crm, @"TaxpayerOrgID");
  131. if (!string.IsNullOrWhiteSpace(sTransactiontype))
  132. {
  133. var TypeAD = sTransactiontype.Any(c => c == 'D' || c == 'A');
  134. if (OrgID == "SG")
  135. {
  136. var SqlType = @" (CustomerShotCName = @CustomerShotCName {1} )";
  137. var TaxpayerOrgIDSql = "";
  138. var ConvertedTaxpayerOrgID = sTaxpayerOrgID.Trim() ?? "";
  139. var TaxpayerOrgIDLength = ConvertedTaxpayerOrgID.Length;
  140. //Type A or D 一定要輸入納稅人組織號
  141. if (TypeAD && TaxpayerOrgIDLength > 18)
  142. {
  143. return new Tuple<bool, string>(false, "交易型態為A或D時,納稅人組織號必填且為限制18碼內。");
  144. }
  145. //有輸入統編的話 一定要8碼
  146. if (TaxpayerOrgIDLength > 0 && TaxpayerOrgIDLength < 18)
  147. return new Tuple<bool, string>(false, "納稅人組織號需要18碼。");
  148. if (TaxpayerOrgIDLength == 18)
  149. {
  150. TaxpayerOrgIDSql = "OR TaxpayerOrgID = @TaxpayerOrgID";
  151. }
  152. SqlType = SqlType.Replace("{1}", TaxpayerOrgIDSql);
  153. iCount = db.Queryable<OTB_CRM_Customers>()
  154. .Where(x => x.OrgID == i_crm.ORIGID)
  155. .WhereIF(!string.IsNullOrEmpty(sId), x => x.guid != sId)
  156. .Where(SqlType, new { CustomerShotCName = sCustomerShotCName, TaxpayerOrgID = ConvertedTaxpayerOrgID })
  157. .Count();
  158. }
  159. else
  160. {
  161. var SqlType = @" (CustomerShotCName = @CustomerShotCName {1} )";
  162. var UnicodeSql = "";
  163. var ConvertedUnicode = sUniCode.Trim() ?? "";
  164. var UnicodeLength = ConvertedUnicode.Length;
  165. //Type A or D 一定要輸入統編
  166. if (TypeAD && UnicodeLength < 8)
  167. return new Tuple<bool, string>(false, "交易型態為A或D時,統一編號必填且為8碼。");
  168. //有輸入統編的話 一定要8碼
  169. if (UnicodeLength > 0 && UnicodeLength < 8)
  170. return new Tuple<bool, string>(false, "統一編號需要8碼。");
  171. if (UnicodeLength == 8)
  172. {
  173. UnicodeSql = "OR UniCode = @UniCode";
  174. }
  175. SqlType = SqlType.Replace("{1}", UnicodeSql);
  176. iCount = db.Queryable<OTB_CRM_Customers>()
  177. .Where(x => x.OrgID == OrgID)
  178. .WhereIF(!string.IsNullOrEmpty(sId), x => x.guid != sId)
  179. .Where(SqlType, new { CustomerShotCName = sCustomerShotCName, UniCode = sUniCode })
  180. .Count();
  181. }
  182. }
  183. }
  184. catch (Exception ex)
  185. {
  186. var sMsg = Util.GetLastExceptionMsg(ex);
  187. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_QryService), "", "CheckShortNameAndUniCode(客戶管理編輯(檢查客戶簡稱與統一編號))", "", "", "");
  188. }
  189. var TaxName = "統一編號";
  190. if (OrgID == "SG")
  191. TaxName = "納稅人組織號";
  192. switch (iCount)
  193. {
  194. case 0:
  195. return new Tuple<bool, string>(true, "沒有找到重複" + TaxName + "或簡稱。");
  196. case -1:
  197. return new Tuple<bool, string>(false, "尋找過程發生錯誤,請稍後嘗試。");
  198. default:
  199. return new Tuple<bool, string>(false, TaxName + "或客戶簡稱重複。請重新檢查資料。");
  200. }
  201. }
  202. #region 客戶管理編輯(新增)
  203. /// <summary>
  204. /// 客戶管理編輯(新增)
  205. /// </summary>
  206. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  207. /// <returns></returns>
  208. public ResponseMessage Insert(RequestMessage i_crm)
  209. {
  210. ResponseMessage rm = null;
  211. string sMsg = null;
  212. ////檢查統一編號或客戶簡稱要為唯一
  213. var CheckResult = CheckShortNameAndUniCode(i_crm);
  214. if (!CheckResult.Item1)
  215. return new ErrorResponseMessage(CheckResult.Item2, i_crm);
  216. try
  217. {
  218. rm = SugarBase.ExecTran(db =>
  219. {
  220. do
  221. {
  222. string sCustomerGUID = Guid.NewGuid().ToString();
  223. //客戶資料表身
  224. var oEntity = _fetchEntity<OTB_CRM_Customers>(i_crm);
  225. _setEntityBase(oEntity, i_crm);
  226. oEntity.guid = sCustomerGUID;
  227. oEntity.IsAudit = "N";
  228. oEntity.CustomerNO = SerialNumber.GetMaxNumberByType(i_crm.ORIGID, oEntity.CustomerNO, MaxNumberType.Empty, i_crm.USERID, 3);
  229. //客戶資料表頭
  230. var oMstEntity = _fetchEntity<OTB_CRM_CustomersMST>(i_crm);
  231. _setEntityBase(oMstEntity, i_crm);
  232. oMstEntity.guid = Guid.NewGuid().ToString();
  233. oMstEntity.CustomerNO = oEntity.CustomerNO;
  234. oMstEntity.customer_guid = sCustomerGUID;
  235. oMstEntity.Effective = "Y";
  236. var iRel = db.Insertable(oEntity).ExecuteReturnEntity();
  237. var iRelMst = db.Insertable(oMstEntity).ExecuteCommand();
  238. rm = new SuccessResponseMessage(null, i_crm);
  239. rm.DATA.Add(BLWording.REL, iRel);
  240. } while (false);
  241. return rm;
  242. });
  243. }
  244. catch (Exception ex)
  245. {
  246. sMsg = Util.GetLastExceptionMsg(ex);
  247. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"Add(客戶管理編輯(新增))", @"", @"", @"");
  248. }
  249. finally
  250. {
  251. if (null != sMsg)
  252. {
  253. rm = new ErrorResponseMessage(sMsg, i_crm);
  254. }
  255. }
  256. return rm;
  257. }
  258. #endregion 客戶管理編輯(新增)
  259. #region 客戶管理編輯(修改)
  260. /// <summary>
  261. /// 客戶管理編輯(修改)
  262. /// </summary>
  263. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  264. /// <returns></returns>
  265. public ResponseMessage Update(RequestMessage i_crm)
  266. {
  267. ResponseMessage rm = null;
  268. string sMsg = null;
  269. var CheckResult = CheckShortNameAndUniCode(i_crm);
  270. if (!CheckResult.Item1)
  271. return new ErrorResponseMessage(CheckResult.Item2, i_crm);
  272. try
  273. {
  274. rm = SugarBase.ExecTran(db =>
  275. {
  276. do
  277. {
  278. var sId = _fetchString(i_crm, @"guid");
  279. var oNewEntity = _fetchEntity<OTB_CRM_Customers>(i_crm);
  280. _setEntityBase(oNewEntity, i_crm);
  281. oNewEntity.IsAudit = "N";
  282. string sOrgId = oNewEntity.OrgID;
  283. if (oNewEntity.CustomerNO.Length == 4)
  284. {
  285. oNewEntity.CustomerNO = SerialNumber.GetMaxNumberByType(i_crm.ORIGID, oNewEntity.CustomerNO, MaxNumberType.Empty, i_crm.USERID, 3);
  286. //查詢出該
  287. //更新現有其他對應表頭資料為N
  288. var oUpdMstEntity = _fetchEntity<OTB_CRM_CustomersMST>(i_crm);
  289. db.Updateable(oUpdMstEntity).UpdateColumns(p => p.Effective == "N").Where(p => p.customer_guid == sId).ExecuteCommand();
  290. //若有變更,新增一筆到表頭
  291. var oInsertMstEntity = _fetchEntity<OTB_CRM_CustomersMST>(i_crm);
  292. _setEntityBase(oInsertMstEntity, i_crm);
  293. oInsertMstEntity.guid = Guid.NewGuid().ToString();
  294. oInsertMstEntity.CustomerNO = oNewEntity.CustomerNO;
  295. oInsertMstEntity.customer_guid = sId;
  296. oInsertMstEntity.Effective = "Y";
  297. db.Insertable(oInsertMstEntity).ExecuteCommand();
  298. }
  299. var iRel = db.Updateable(oNewEntity)
  300. .IgnoreColumns(x => new
  301. {
  302. x.IsApply,
  303. x.IsAudit,
  304. x.ToAuditer,
  305. x.NotPassReason,
  306. x.CreateUser,
  307. x.CreateDate
  308. }).ExecuteCommand();
  309. var NewResult = db.Queryable<OTB_CRM_Customers>()
  310. .Where(p => p.OrgID == sOrgId && p.guid == oNewEntity.guid)
  311. .Single();
  312. rm = new SuccessResponseMessage(null, i_crm);
  313. rm.DATA.Add(BLWording.REL, NewResult);
  314. } while (false);
  315. return rm;
  316. });
  317. }
  318. catch (Exception ex)
  319. {
  320. sMsg = Util.GetLastExceptionMsg(ex);
  321. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"Update(客戶管理編輯(修改))", @"", @"", @"");
  322. }
  323. finally
  324. {
  325. if (null != sMsg)
  326. {
  327. rm = new ErrorResponseMessage(sMsg, i_crm);
  328. }
  329. }
  330. return rm;
  331. }
  332. #endregion 客戶管理編輯(修改)
  333. #region 客戶管理編輯(刪除)
  334. /// <summary>
  335. /// 客戶管理編輯(刪除)
  336. /// </summary>
  337. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  338. /// <returns></returns>
  339. public ResponseMessage Delete(RequestMessage i_crm)
  340. {
  341. ResponseMessage rm = null;
  342. string sMsg = null;
  343. try
  344. {
  345. rm = SugarBase.ExecTran(db =>
  346. {
  347. do
  348. {
  349. var sId = _fetchString(i_crm, @"guid");
  350. var iRel = db.Deleteable<OTB_CRM_Customers>().Where(x => x.guid == sId).ExecuteCommand();
  351. var iMstRel = db.Deleteable<OTB_CRM_CustomersMST>().Where(x => x.customer_guid == sId).ExecuteCommand();
  352. rm = new SuccessResponseMessage(null, i_crm);
  353. rm.DATA.Add(BLWording.REL, iRel);
  354. } while (false);
  355. return rm;
  356. });
  357. }
  358. catch (Exception ex)
  359. {
  360. sMsg = Util.GetLastExceptionMsg(ex);
  361. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"Delete(客戶管理編輯(刪除))", @"", @"", @"");
  362. }
  363. finally
  364. {
  365. if (null != sMsg)
  366. {
  367. rm = new ErrorResponseMessage(sMsg, i_crm);
  368. }
  369. }
  370. return rm;
  371. }
  372. #endregion 客戶管理編輯(刪除)
  373. #region 客戶管理編輯(提交審核)
  374. /// <summary>
  375. /// 客戶管理編輯(提交審核)
  376. /// </summary>
  377. /// <param name="i_crm">todo: describe i_crm parameter on ToAudit</param>
  378. /// <returns></returns>
  379. public ResponseMessage ToAudit(RequestMessage i_crm)
  380. {
  381. ResponseMessage rm = null;
  382. string sMsg = null;
  383. var CheckResult = CheckShortNameAndUniCode(i_crm);
  384. if (!CheckResult.Item1)
  385. return new ErrorResponseMessage(CheckResult.Item2, i_crm);
  386. try
  387. {
  388. rm = SugarBase.ExecTran(db =>
  389. {
  390. do
  391. {
  392. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  393. var sIsAudit = _fetchString(i_crm, @"IsAudit");
  394. var sdb = new SimpleClient<OTB_CRM_Customers>(db);
  395. var customer = sdb.GetById(sId);
  396. if (customer == null)
  397. {
  398. sMsg = @"系統找不到對應的客戶資料,請核查!";
  399. break;
  400. }
  401. var sTitle = @"客戶資料「" + (string.IsNullOrWhiteSpace(customer.CustomerCName) ? customer.CustomerEName : customer.CustomerCName) + @"」申請審核";
  402. if (i_crm.LANG == @"zh")
  403. {
  404. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  405. }
  406. //更新客戶資料
  407. var oCustomersUpd = new OTB_CRM_Customers
  408. {
  409. IsAudit = sIsAudit,
  410. ToAuditer = i_crm.USERID,
  411. ModifyUser = i_crm.USERID,
  412. ModifyDate = DateTime.Now
  413. };
  414. db.Updateable(oCustomersUpd)
  415. .UpdateColumns(it => new { it.IsAudit, it.ToAuditer, it.ModifyUser, it.ModifyDate })
  416. .Where(it => it.guid == sId).ExecuteCommand();
  417. //更新代辦
  418. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId);
  419. var listTips = new List<OTB_SYS_Tips>();
  420. var listTask = new List<OTB_SYS_Task>();
  421. var lstCustomersAuditUsers = new List<string>();
  422. var sCustomersAuditUsers = Common.GetSystemSetting(db, i_crm.ORIGID, @"CustomersAuditUsers");
  423. if (sCustomersAuditUsers != @"")
  424. {
  425. var saCustomersAuditUsers = sCustomersAuditUsers.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  426. lstCustomersAuditUsers = saCustomersAuditUsers.Distinct<string>().ToList();
  427. foreach (string user in lstCustomersAuditUsers)
  428. {
  429. //添加代辦
  430. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, customer.guid, user, sTitle, i_crm.CUSTOMDATA[@"program_id"], @"?Action=Upd&guid=" + customer.guid);
  431. listTask.Add(oTaskAdd);
  432. //添加提醒消息
  433. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, user, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&guid=" + customer.guid, WebAppGlobalConstWord.BELL);
  434. listTips.Add(oTipsAdd);
  435. }
  436. }
  437. if (listTips.Count > 0)
  438. {
  439. db.Insertable(listTips).ExecuteCommand();
  440. }
  441. if (listTask.Count > 0)
  442. {
  443. db.Insertable(listTask).ExecuteCommand();
  444. }
  445. rm = new SuccessResponseMessage(null, i_crm);
  446. rm.DATA.Add(BLWording.REL, lstCustomersAuditUsers);
  447. } while (false);
  448. return rm;
  449. });
  450. }
  451. catch (Exception ex)
  452. {
  453. sMsg = Util.GetLastExceptionMsg(ex);
  454. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"ToAudit(客戶管理編輯(提交審核))", @"", @"", @"");
  455. }
  456. finally
  457. {
  458. if (null != sMsg)
  459. {
  460. rm = new ErrorResponseMessage(sMsg, i_crm);
  461. }
  462. }
  463. return rm;
  464. }
  465. #endregion 客戶管理編輯(提交審核)
  466. #region 客戶管理編輯(主管會計審核)
  467. /// <summary>
  468. /// 客戶管理編輯(主管會計審核)
  469. /// </summary>
  470. /// <param name="i_crm">todo: describe i_crm parameter on Audit</param>
  471. /// <returns></returns>
  472. public ResponseMessage Audit(RequestMessage i_crm)
  473. {
  474. ResponseMessage rm = null;
  475. string sMsg = null;
  476. try
  477. {
  478. rm = SugarBase.ExecTran(db =>
  479. {
  480. do
  481. {
  482. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  483. var sIsAudit = _fetchString(i_crm, @"IsAudit");
  484. var sNotPassReason = _fetchString(i_crm, @"NotPassReason");
  485. var sdb = new SimpleClient<OTB_CRM_Customers>(db);
  486. var customer = sdb.GetById(sId);
  487. if (customer == null)
  488. {
  489. sMsg = @"系統找不到對應的客戶資料,請核查!";
  490. break;
  491. }
  492. //更新客戶資料
  493. var oCustomersUpd = new OTB_CRM_Customers
  494. {
  495. IsAudit = sIsAudit,
  496. NotPassReason = sNotPassReason,
  497. ModifyUser = i_crm.USERID,
  498. ModifyDate = DateTime.Now
  499. };
  500. db.Updateable(oCustomersUpd)
  501. .UpdateColumns(it => new { it.IsAudit, it.NotPassReason, it.ModifyUser, it.ModifyDate })
  502. .Where(it => it.guid == sId).ExecuteCommand();
  503. //更新代辦
  504. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId);
  505. var oUserInfo = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID);
  506. var sTitle = (oUserInfo.MemberName ?? i_crm.USERID) + @"審核了您創建的客戶資料「" + (string.IsNullOrWhiteSpace(customer.CustomerCName) ? customer.CustomerEName : customer.CustomerCName) + @"」,審核結果:";
  507. sTitle += sIsAudit == @"Y" ? @"通過" : @"不通過";
  508. if (i_crm.LANG == @"zh")
  509. {
  510. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  511. }
  512. //成功與否
  513. var TipsType = sIsAudit == @"Y" ? WebAppGlobalConstWord.CHECK : WebAppGlobalConstWord.FAIL;
  514. //添加提醒消息
  515. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, customer.ToAuditer, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&guid=" + customer.guid, TipsType);
  516. db.Insertable(oTipsAdd).ExecuteCommand();
  517. if (sIsAudit == @"Q")
  518. {
  519. //添加代辦
  520. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, customer.guid, customer.ToAuditer, sTitle, i_crm.CUSTOMDATA[@"program_id"], @"?Action=Upd&guid=" + customer.guid);
  521. db.Insertable(oTaskAdd).ExecuteCommand();
  522. }
  523. else
  524. {
  525. var oCustomersTransferAdd = new OTB_CRM_CustomersTransfer();
  526. var sEventID = Guid.NewGuid().ToString();
  527. oCustomersTransferAdd.OrgID = i_crm.ORIGID;
  528. oCustomersTransferAdd.Feild01 = customer.CustomerNO;
  529. oCustomersTransferAdd.Feild02 = @"0";
  530. oCustomersTransferAdd.Feild03 = Common.CutByteString(customer.CustomerShotCName, 12);
  531. oCustomersTransferAdd.Feild04 = Common.CutByteString(customer.CustomerCName == @"" ? customer.CustomerEName : customer.CustomerCName, 60);
  532. oCustomersTransferAdd.Feild05 = @"";
  533. oCustomersTransferAdd.Feild06 = @"";
  534. oCustomersTransferAdd.Feild07 = customer.UniCode;
  535. oCustomersTransferAdd.Feild08 = @"";
  536. oCustomersTransferAdd.Feild09 = @"";
  537. oCustomersTransferAdd.Feild10 = Common.CutByteString(customer.InvoiceAddress, 60);
  538. oCustomersTransferAdd.Feild11 = Common.CutByteString(customer.Address, 60);
  539. oCustomersTransferAdd.Feild12 = @"";
  540. oCustomersTransferAdd.Feild13 = @"";
  541. oCustomersTransferAdd.Feild14 = Common.CutByteString(customer.Telephone, 20);
  542. oCustomersTransferAdd.Feild15 = @"";
  543. oCustomersTransferAdd.Feild16 = Common.CutByteString(customer.FAX, 20);
  544. oCustomersTransferAdd.Feild17 = @"";
  545. oCustomersTransferAdd.Feild18 = @"";
  546. oCustomersTransferAdd.Feild19 = @"";
  547. oCustomersTransferAdd.Feild20 = @"";
  548. oCustomersTransferAdd.Feild21 = @"";
  549. oCustomersTransferAdd.Feild22 = @"";
  550. oCustomersTransferAdd.Feild23 = Common.CutByteString(customer.Memo, 30);
  551. oCustomersTransferAdd.Feild24 = @"100";
  552. oCustomersTransferAdd.Feild25 = @"";
  553. oCustomersTransferAdd.Feild26 = @"";
  554. oCustomersTransferAdd.Feild27 = @"100";
  555. oCustomersTransferAdd.Feild28 = @"";
  556. oCustomersTransferAdd.Feild29 = Common.CutByteString(customer.CreateUser.Split('.')[0], 11);
  557. oCustomersTransferAdd.Feild30 = @"";
  558. oCustomersTransferAdd.Feild31 = @"";
  559. oCustomersTransferAdd.Feild32 = @"";
  560. oCustomersTransferAdd.Feild33 = @"";
  561. oCustomersTransferAdd.Feild34 = @"";
  562. oCustomersTransferAdd.Feild35 = @"";
  563. oCustomersTransferAdd.Feild36 = @"";
  564. oCustomersTransferAdd.Feild37 = @"B,C".IndexOf(customer.TransactionType) > -1 ? @"6" : @"5";
  565. oCustomersTransferAdd.Feild38 = @"2";
  566. oCustomersTransferAdd.Feild39 = @"";
  567. oCustomersTransferAdd.Feild40 = @"";
  568. oCustomersTransferAdd.Feild41 = @"1";
  569. oCustomersTransferAdd.Feild42 = @"";
  570. oCustomersTransferAdd.Feild43 = @"";
  571. oCustomersTransferAdd.Feild44 = @"";
  572. oCustomersTransferAdd.Feild45 = @"";
  573. oCustomersTransferAdd.Feild46 = @"";
  574. oCustomersTransferAdd.Feild47 = @"";
  575. oCustomersTransferAdd.Feild48 = @"";
  576. oCustomersTransferAdd.Feild49 = @"";
  577. oCustomersTransferAdd.Feild50 = @"";
  578. oCustomersTransferAdd.Feild51 = @"";
  579. oCustomersTransferAdd.Feild52 = @"";
  580. oCustomersTransferAdd.Feild53 = @"";
  581. oCustomersTransferAdd.Feild54 = @"";
  582. oCustomersTransferAdd.Feild55 = @"";
  583. oCustomersTransferAdd.Feild56 = @"";
  584. oCustomersTransferAdd.Feild57 = @"";
  585. oCustomersTransferAdd.Feild58 = customer.CustomerNO;
  586. oCustomersTransferAdd.Feild59 = @"";
  587. oCustomersTransferAdd.Feild60 = @"";
  588. oCustomersTransferAdd.Feild61 = @"";
  589. oCustomersTransferAdd.Feild62 = @"";
  590. oCustomersTransferAdd.Feild63 = @"";
  591. oCustomersTransferAdd.Feild64 = @"";
  592. oCustomersTransferAdd.Feild65 = @"";
  593. oCustomersTransferAdd.Feild66 = @"";
  594. oCustomersTransferAdd.Feild67 = @"";
  595. oCustomersTransferAdd.Feild68 = @"";
  596. oCustomersTransferAdd.Feild69 = @"";
  597. oCustomersTransferAdd.Feild70 = @"";
  598. oCustomersTransferAdd.Feild71 = @"";
  599. oCustomersTransferAdd.Feild72 = @"";
  600. oCustomersTransferAdd.Feild73 = @"";
  601. oCustomersTransferAdd.Feild74 = @"";
  602. oCustomersTransferAdd.Feild75 = @"";
  603. oCustomersTransferAdd.Feild76 = @"";
  604. oCustomersTransferAdd.Feild77 = @"";
  605. oCustomersTransferAdd.Feild78 = @"";
  606. oCustomersTransferAdd.Feild79 = @"";
  607. oCustomersTransferAdd.Feild80 = @"";
  608. oCustomersTransferAdd.Feild81 = @"";
  609. oCustomersTransferAdd.Feild82 = Common.CutByteString(customer.CustomerEName, 120);
  610. var sAddress = customer.Address;
  611. var cn = new Regex(@"[一-龥]+");//正则表达式 表示汉字范围
  612. if (cn.IsMatch(sAddress))
  613. {
  614. sAddress = @"";
  615. }
  616. oCustomersTransferAdd.Feild83 = Common.CutByteString(sAddress, 240);
  617. oCustomersTransferAdd.Feild84 = @"";
  618. oCustomersTransferAdd.Feild85 = @"";
  619. oCustomersTransferAdd.Feild86 = @"";
  620. oCustomersTransferAdd.Feild87 = @"";
  621. oCustomersTransferAdd.Feild88 = @"";
  622. oCustomersTransferAdd.Feild89 = @"";
  623. oCustomersTransferAdd.Feild90 = @"";
  624. oCustomersTransferAdd.Feild91 = @"0";
  625. oCustomersTransferAdd.Feild92 = @"0";
  626. oCustomersTransferAdd.Feild93 = @"";
  627. oCustomersTransferAdd.Feild94 = @"";
  628. oCustomersTransferAdd.Feild95 = @"";
  629. oCustomersTransferAdd.Feild96 = @"";
  630. oCustomersTransferAdd.Feild97 = @"";
  631. oCustomersTransferAdd.Feild98 = @"";
  632. oCustomersTransferAdd.Feild99 = @"";
  633. db.Insertable(oCustomersTransferAdd).ExecuteCommand();
  634. }
  635. rm = new SuccessResponseMessage(null, i_crm);
  636. rm.DATA.Add(BLWording.REL, customer.ToAuditer);
  637. } while (false);
  638. return rm;
  639. });
  640. }
  641. catch (Exception ex)
  642. {
  643. sMsg = Util.GetLastExceptionMsg(ex);
  644. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"Audit(客戶管理編輯(主管會計審核))", @"", @"", @"");
  645. }
  646. finally
  647. {
  648. if (null != sMsg)
  649. {
  650. rm = new ErrorResponseMessage(sMsg, i_crm);
  651. }
  652. }
  653. return rm;
  654. }
  655. #endregion 客戶管理編輯(主管會計審核)
  656. #region 客戶管理編輯(取消審核)
  657. /// <summary>
  658. /// 客戶管理編輯(取消審核)
  659. /// </summary>
  660. /// <param name="i_crm">todo: describe i_crm parameter on CancelAudit</param>
  661. /// <returns></returns>
  662. public ResponseMessage CancelAudit(RequestMessage i_crm)
  663. {
  664. ResponseMessage rm = null;
  665. string sMsg = null;
  666. try
  667. {
  668. rm = SugarBase.ExecTran(db =>
  669. {
  670. do
  671. {
  672. var sId = _fetchString(i_crm, EasyNetGlobalConstWord.GUID);
  673. var sNotPassReason = _fetchString(i_crm, @"NotPassReason");
  674. var sUserName = _fetchString(i_crm, @"UserName");
  675. var sdb = new SimpleClient<OTB_CRM_Customers>(db);
  676. var customer = sdb.GetById(sId);
  677. if (customer == null)
  678. {
  679. sMsg = @"系統找不到對應的客戶資料,請核查!";
  680. break;
  681. }
  682. var sTitle = sUserName + @"客戶管理取消審核了您創建的客戶資料「" + (customer.CustomerCName.Trim() == @"" ? customer.CustomerEName : customer.CustomerCName) + @"」";
  683. if (i_crm.LANG == @"zh")
  684. {
  685. sTitle = ChineseStringUtility.ToSimplified(sTitle);
  686. }
  687. //更新客戶資料
  688. var oCustomersUpd = new OTB_CRM_Customers
  689. {
  690. IsAudit = @"N",
  691. NotPassReason = sNotPassReason,
  692. ModifyUser = i_crm.USERID,
  693. ModifyDate = DateTime.Now
  694. };
  695. db.Updateable(oCustomersUpd)
  696. .UpdateColumns(it => new { it.IsAudit, it.NotPassReason, it.ModifyUser, it.ModifyDate })
  697. .Where(it => it.guid == sId).ExecuteCommand();
  698. //更新代辦
  699. SYS.Task_QryService.TaskStatusUpd(db, i_crm.ORIGID, sId);
  700. //添加提醒消息
  701. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, customer.ToAuditer, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&guid=" + customer.guid, WebAppGlobalConstWord.BELL);
  702. db.Insertable(oTipsAdd).ExecuteCommand();
  703. //添加代辦
  704. var oTaskAdd = SYS.Task_QryService.TaskAdd(i_crm, customer.guid, customer.ToAuditer, sTitle, i_crm.CUSTOMDATA[@"program_id"], @"?Action=Upd&guid=" + customer.guid);
  705. db.Insertable(oTaskAdd).ExecuteCommand();
  706. rm = new SuccessResponseMessage(null, i_crm);
  707. rm.DATA.Add(BLWording.REL, customer.ToAuditer);
  708. } while (false);
  709. return rm;
  710. });
  711. }
  712. catch (Exception ex)
  713. {
  714. sMsg = Util.GetLastExceptionMsg(ex);
  715. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"CancelAudit(客戶管理編輯(取消審核))", @"", @"", @"");
  716. }
  717. finally
  718. {
  719. if (null != sMsg)
  720. {
  721. rm = new ErrorResponseMessage(sMsg, i_crm);
  722. }
  723. }
  724. return rm;
  725. }
  726. #endregion 客戶管理編輯(取消審核)
  727. #region 客戶管理編輯(提交申請修改)
  728. /// <summary>
  729. /// 客戶管理編輯(提交申請修改)
  730. /// </summary>
  731. /// <param name="i_crm">todo: describe i_crm parameter on ApplyforUpdate</param>
  732. /// <returns></returns>
  733. public ResponseMessage ApplyforUpdate(RequestMessage i_crm)
  734. {
  735. ResponseMessage rm = null;
  736. string sMsg = null;
  737. try
  738. {
  739. rm = SugarBase.ExecTran(db =>
  740. {
  741. do
  742. {
  743. var sId = _fetchString(i_crm, @"Guid");
  744. var sNotPassReason = _fetchString(i_crm, @"NotPassReason");
  745. var sdb = new SimpleClient<OTB_CRM_Customers>(db);
  746. var oCustomers = sdb.GetById(sId);
  747. if (oCustomers == null)
  748. {
  749. sMsg = @"系統找不到對應的請假資料,請核查!";
  750. break;
  751. }
  752. //更新客戶資料
  753. var oCustomersUpd = new OTB_CRM_Customers
  754. {
  755. IsAudit = @"Z",
  756. NotPassReason = sNotPassReason,
  757. ModifyUser = i_crm.USERID,
  758. ModifyDate = DateTime.Now
  759. };
  760. db.Updateable(oCustomersUpd)
  761. .UpdateColumns(it => new { it.IsAudit, it.NotPassReason, it.ModifyUser, it.ModifyDate }).Where(it => it.guid == sId).ExecuteCommand();
  762. var oUserInfo = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID);
  763. var sTitle = @"客戶資料「" + (oCustomers.CustomerCName.Trim() == @"" ? oCustomers.CustomerEName : oCustomers.CustomerCName) + @"」已被" + (oUserInfo.MemberName ?? @"") + @"申請修改";
  764. var listTips = new List<OTB_SYS_Tips>();
  765. //List<OTB_SYS_Task> listTask = new List<OTB_SYS_Task>();
  766. var lstCustomersAuditUsers = new List<string>();
  767. var sCustomersAuditUsers = Common.GetSystemSetting(db, i_crm.ORIGID, @"CustomersAuditUsers");
  768. if (sCustomersAuditUsers != @"")
  769. {
  770. var saCustomersAuditUsers = sCustomersAuditUsers.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  771. lstCustomersAuditUsers = saCustomersAuditUsers.Distinct<string>().ToList();
  772. foreach (string user in lstCustomersAuditUsers)
  773. {
  774. //添加代辦
  775. //OTB_SYS_Task oTaskAdd = SYS.TaskService.TaskAdd(i_crm, oCustomers.guid, user, sTitle, i_crm.CUSTOMDATA["program_id"], "?Action=Upd&guid=" + oCustomers.guid);
  776. //listTask.Add(oTaskAdd);
  777. //添加提醒消息
  778. var oTipsAdd = SYS.Task_QryService.TipsAdd(i_crm, sTitle, user, i_crm.CUSTOMDATA[@"program_id"] + @"|?Action=Upd&guid=" + oCustomers.guid, WebAppGlobalConstWord.BELL);
  779. listTips.Add(oTipsAdd);
  780. }
  781. }
  782. if (listTips.Count > 0)
  783. {
  784. db.Insertable(listTips).ExecuteCommand();
  785. }
  786. //if (listTask.Count > 0)
  787. //{
  788. // db.Insertable(listTask).ExecuteCommand();
  789. //}
  790. rm = new SuccessResponseMessage(null, i_crm);
  791. rm.DATA.Add(BLWording.REL, lstCustomersAuditUsers);
  792. } while (false);
  793. return rm;
  794. });
  795. }
  796. catch (Exception ex)
  797. {
  798. sMsg = Util.GetLastExceptionMsg(ex);
  799. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"ApplyforUpdate(客戶管理編輯(提交申請修改))", @"", @"", @"");
  800. }
  801. finally
  802. {
  803. if (null != sMsg)
  804. {
  805. rm = new ErrorResponseMessage(sMsg, i_crm);
  806. }
  807. }
  808. return rm;
  809. }
  810. #endregion 客戶管理編輯(提交申請修改)
  811. #region 客戶管理編輯(抓取參加展覽列表資料)
  812. /// <summary>
  813. /// 客戶管理編輯(抓取參加展覽列表資料)
  814. /// </summary>
  815. /// <param name="i_crm">todo: describe i_crm parameter on GetExhibitionlist</param>
  816. /// <returns></returns>
  817. public ResponseMessage GetExhibitionlist(RequestMessage i_crm)
  818. {
  819. ResponseMessage rm = null;
  820. string sMsg = null;
  821. var db = SugarBase.GetIntance();
  822. try
  823. {
  824. do
  825. {
  826. var sId = _fetchString(i_crm, @"Guid");
  827. var saExhibitions = new List<Map>();
  828. //出口
  829. var saExport = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_ExportExhibition>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID && t1.Effective == "Y" && t2.IsVoid == "N")
  830. .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Exhibitors, t2.Agent }).MergeTable()
  831. .Where(it => it.ExhibitionNO != @"" && it.OrgID == i_crm.ORIGID && (it.Exhibitors.Contains(sId) || it.Agent == sId)).ToList();
  832. if (saExport.Count > 0)
  833. {
  834. foreach (var opm in saExport)
  835. {
  836. if (!saExhibitions.Any(x => (x[@"SN"].ToString() == opm.SN.ToString())))
  837. {
  838. var m = new Map
  839. {
  840. { @"RowIndex", saExhibitions.Count + 1 },
  841. { @"SN", opm.SN },
  842. { @"ExhibitionCode", opm.ExhibitionCode },
  843. { @"Exhibitioname_TW", opm.Exhibitioname_TW },
  844. { @"Exhibitioname_CN", opm.Exhibitioname_CN }
  845. };
  846. saExhibitions.Add(m);
  847. }
  848. }
  849. }
  850. //進口
  851. var saImport = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_ImportExhibition>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID && t1.Effective == "Y" && t2.IsVoid == "N")
  852. .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Suppliers, t2.Supplier, t2.Agent }).MergeTable()
  853. .Where(it => it.ExhibitionNO != @"" && it.OrgID == i_crm.ORIGID && (it.Suppliers.Contains(sId) || it.Supplier == sId || it.Agent == sId)).ToList();
  854. if (saImport.Count > 0)
  855. {
  856. foreach (var opm in saImport)
  857. {
  858. if (!saExhibitions.Any(x => (x[@"SN"].ToString() == opm.SN.ToString())))
  859. {
  860. var m = new Map
  861. {
  862. { @"RowIndex", saExhibitions.Count + 1 },
  863. { @"SN", opm.SN },
  864. { @"ExhibitionCode", opm.ExhibitionCode },
  865. { @"Exhibitioname_TW", opm.Exhibitioname_TW },
  866. { @"Exhibitioname_CN", opm.Exhibitioname_CN }
  867. };
  868. saExhibitions.Add(m);
  869. }
  870. }
  871. }
  872. //其他
  873. var saOther = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_OtherExhibition>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID && t1.Effective == "Y" && t2.IsVoid == "N")
  874. .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Supplier, t2.Agent }).MergeTable()
  875. .Where(it => it.ExhibitionNO != @"" && it.OrgID == i_crm.ORIGID && (it.Supplier == sId || it.Agent == sId)).ToList();
  876. if (saOther.Count > 0)
  877. {
  878. foreach (var opm in saOther)
  879. {
  880. if (!saExhibitions.Any(x => (x[@"SN"].ToString() == opm.SN.ToString())))
  881. {
  882. var m = new Map
  883. {
  884. { @"RowIndex", saExhibitions.Count + 1 },
  885. { @"SN", opm.SN },
  886. { @"ExhibitionCode", opm.ExhibitionCode },
  887. { @"Exhibitioname_TW", opm.Exhibitioname_TW },
  888. { @"Exhibitioname_CN", opm.Exhibitioname_CN }
  889. };
  890. saExhibitions.Add(m);
  891. }
  892. }
  893. }
  894. //其他
  895. var saOtherTG = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_OtherExhibitionTG>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID && t1.Effective == "Y" && t2.IsVoid == "N")
  896. .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Exhibitors, t2.Agent }).MergeTable()
  897. .Where(it => it.ExhibitionNO != @"" && it.OrgID == i_crm.ORIGID && (it.Exhibitors.Contains(sId) || it.Agent == sId)).ToList();
  898. if (saOtherTG.Count > 0)
  899. {
  900. foreach (var opm in saOtherTG)
  901. {
  902. if (!saExhibitions.Any(x => (x[@"SN"].ToString() == opm.SN.ToString())))
  903. {
  904. var m = new Map
  905. {
  906. { @"RowIndex", saExhibitions.Count + 1 },
  907. { @"SN", opm.SN },
  908. { @"ExhibitionCode", opm.ExhibitionCode },
  909. { @"Exhibitioname_TW", opm.Exhibitioname_TW },
  910. { @"Exhibitioname_CN", opm.Exhibitioname_CN }
  911. };
  912. saExhibitions.Add(m);
  913. }
  914. }
  915. }
  916. rm = new SuccessResponseMessage(null, i_crm);
  917. rm.DATA.Add(BLWording.REL, saExhibitions);
  918. } while (false);
  919. }
  920. catch (Exception ex)
  921. {
  922. sMsg = Util.GetLastExceptionMsg(ex);
  923. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"GetCustomers(客戶管理編輯(抓取參加展覽列表資料))", @"", @"", @"");
  924. }
  925. finally
  926. {
  927. if (null != sMsg)
  928. {
  929. rm = new ErrorResponseMessage(sMsg, i_crm);
  930. }
  931. }
  932. return rm;
  933. }
  934. #endregion 客戶管理編輯(抓取參加展覽列表資料)
  935. #region 客戶管理編輯((通過政府API)依據客戶編碼抓取基本資料)
  936. /// <summary>
  937. /// 客戶管理編輯((通過政府API)依據客戶編碼抓取基本資料)
  938. /// </summary>
  939. /// <param name="i_crm">
  940. /// <returns></returns>
  941. public ResponseMessage GetCrmBaseDataByUniCode(RequestMessage i_crm)
  942. {
  943. ResponseMessage rm = null;
  944. string sMsg = null;
  945. var db = SugarBase.GetIntance();
  946. try
  947. {
  948. do
  949. {
  950. var sUniCode = _fetchString(i_crm, @"UniCode");
  951. var sKeyWords = _fetchString(i_crm, @"KeyWords");
  952. var sAPIUrl = @"https://data.gcis.nat.gov.tw/od/data/api/5F64D864-61CB-4D0D-8AD9-492047CC1EA6?$format=json&$filter=Business_Accounting_NO eq ";
  953. if (sUniCode != "")
  954. {
  955. sAPIUrl += sUniCode;
  956. }
  957. else
  958. {
  959. sAPIUrl = @"https://data.gcis.nat.gov.tw/od/data/api/6BBA2268-1367-4B42-9CCA-BC17499EBE8C?$format=json&$filter=Company_Name like " + sKeyWords + " and Company_Status eq 01";
  960. }
  961. var client = new HttpWebClient(sAPIUrl);
  962. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  963. var sHtml = client.GetString();
  964. rm = new SuccessResponseMessage(null, i_crm);
  965. rm.DATA.Add(BLWording.REL, sHtml);
  966. } while (false);
  967. }
  968. catch (Exception ex)
  969. {
  970. sMsg = Util.GetLastExceptionMsg(ex);
  971. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"GetCrmBaseDataByUniCode(客戶管理編輯((通過政府API)依據客戶編碼抓取基本資料))", @"", @"", @"");
  972. }
  973. finally
  974. {
  975. if (null != sMsg)
  976. {
  977. rm = new ErrorResponseMessage(sMsg, i_crm);
  978. }
  979. }
  980. return rm;
  981. }
  982. #endregion 客戶管理編輯((通過政府API)依據客戶編碼抓取基本資料)
  983. #region 客戶管理編輯(依據預約單號查詢匯入廠商)
  984. /// <summary>
  985. /// 客戶管理編輯(依據預約單號查詢匯入廠商)
  986. /// </summary>
  987. /// <param name="i_crm">todo: describe i_crm parameter on GetCrmBaseDataByUniCode</param>
  988. /// <returns></returns>
  989. public ResponseMessage GetImportCustomersByAppointNO(RequestMessage i_crm)
  990. {
  991. ResponseMessage rm = null;
  992. string sMsg = null;
  993. var db = SugarBase.GetIntance();
  994. try
  995. {
  996. do
  997. {
  998. var sAppointNO = _fetchString(i_crm, @"AppointNO");
  999. var oImportCustomers = db.Queryable<OTB_WSM_PackingOrder, OTB_CRM_ImportCustomers>(
  1000. (t1, t2) => t1.OrgID == t2.OrgID && t1.CustomerId == t2.guid)
  1001. .Where((t1, t2) => t1.OrgID == i_crm.ORIGID && t1.AppointNO == sAppointNO)
  1002. .Select((t1, t2) => t2)
  1003. .Single();
  1004. rm = new SuccessResponseMessage(null, i_crm);
  1005. rm.DATA.Add(BLWording.REL, oImportCustomers);
  1006. } while (false);
  1007. }
  1008. catch (Exception ex)
  1009. {
  1010. sMsg = Util.GetLastExceptionMsg(ex);
  1011. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"GetImportCustomersByAppointNO(客戶管理編輯(依據預約單號查詢匯入廠商))", @"", @"", @"");
  1012. }
  1013. finally
  1014. {
  1015. if (null != sMsg)
  1016. {
  1017. rm = new ErrorResponseMessage(sMsg, i_crm);
  1018. }
  1019. }
  1020. return rm;
  1021. }
  1022. #endregion 客戶管理編輯(依據預約單號查詢匯入廠商)
  1023. #region 客戶管理編輯(複製同步)
  1024. /// <summary>
  1025. /// 客戶管理編輯(複製同步)
  1026. /// </summary>
  1027. /// <param name="i_crm">todo: describe i_crm parameter on UpdImportCustomers</param>
  1028. /// <returns></returns>
  1029. public ResponseMessage CopySync(RequestMessage i_crm)
  1030. {
  1031. ResponseMessage rm = null;
  1032. string sMsg = null;
  1033. ////檢查統一編號或客戶簡稱要為唯一
  1034. var CheckResult = CheckShortNameAndUniCode(i_crm);
  1035. if (!CheckResult.Item1)
  1036. return new ErrorResponseMessage(CheckResult.Item2, i_crm);
  1037. try
  1038. {
  1039. rm = SugarBase.ExecTran(db =>
  1040. {
  1041. do
  1042. {
  1043. string sCustomerGUID = Guid.NewGuid().ToString();
  1044. string sOldCustomerGUID = string.Empty;
  1045. string sCurrentOrgID = _fetchString(i_crm, "currOrgID");
  1046. //客戶資料表身
  1047. string sOrgID = _fetchString(i_crm, OTB_CRM_Customers.CN_ORGID);
  1048. if (string.IsNullOrEmpty(sOrgID))
  1049. {
  1050. sMsg = "OrgID Error";
  1051. break;
  1052. }
  1053. var oEntity = _fetchEntity<OTB_CRM_Customers>(i_crm);
  1054. _setEntityBase(oEntity, i_crm);
  1055. sOldCustomerGUID = oEntity.guid;
  1056. oEntity.guid = sCustomerGUID;
  1057. oEntity.OrgID = sOrgID;
  1058. oEntity.CustomerNO = SerialNumber.GetMaxNumberByType(oEntity.OrgID, oEntity.CustomerNO, MaxNumberType.Empty, i_crm.USERID, 3);
  1059. //客戶資料表頭
  1060. var oMstEntity = _fetchEntity<OTB_CRM_CustomersMST>(i_crm);
  1061. _setEntityBase(oMstEntity, i_crm);
  1062. oMstEntity.guid = Guid.NewGuid().ToString();
  1063. oMstEntity.CustomerNO = oEntity.CustomerNO;
  1064. oMstEntity.customer_guid = sCustomerGUID;
  1065. oMstEntity.Effective = "Y";
  1066. //找出參展資料並複製到不同公司別
  1067. #region 出口
  1068. //var saExport = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_ExportExhibition>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID)
  1069. // .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Exhibitors, t2.Agent }).MergeTable()
  1070. // .Where(it => it.ExhibitionNO != @"" && it.OrgID == sCurrentOrgID && (it.Exhibitors.Contains(sOldCustomerGUID) || it.Agent == sOldCustomerGUID))
  1071. // .ToList();
  1072. //if (saExport.Count > 0)
  1073. //{
  1074. // List<OTB_OPM_Exhibition> lExh = db.Queryable<OTB_OPM_Exhibition>()
  1075. // .Where(p => p.SN == saExport.Select(x => x.SN).First())
  1076. // .ToList();
  1077. // foreach (OTB_OPM_Exhibition data in lExh)
  1078. // {
  1079. // data.OrgID = sOrgID;
  1080. // data.OrgID = sOrgID;
  1081. // data.CreateUser = i_crm.USERID;
  1082. // data.CreateDate = DateTime.Now;
  1083. // data.ModifyUser = i_crm.USERID;
  1084. // data.ModifyDate = DateTime.Now;
  1085. // }
  1086. // List<OTB_OPM_ExportExhibition> lExportExh = db.Queryable<OTB_OPM_ExportExhibition>()
  1087. // .Where(p => p.ExhibitionNO == saExport.Select(x => x.SN).First().ToString())
  1088. // .ToList();
  1089. // var exData = db.Insertable<OTB_OPM_Exhibition>(lExh)
  1090. // .IgnoreColumns(p => p == OTB_OPM_Exhibition.CN_SN)
  1091. // .ExecuteReturnIdentity();
  1092. // foreach (OTB_OPM_ExportExhibition data in lExportExh)
  1093. // {
  1094. // data.ExportBillNO = Guid.NewGuid().ToString();
  1095. // data.ExhibitionNO = exData.ToString();
  1096. // data.OrgID = sOrgID;
  1097. // data.CreateUser = i_crm.USERID;
  1098. // data.CreateDate = DateTime.Now;
  1099. // data.ModifyUser = i_crm.USERID;
  1100. // data.ModifyDate = DateTime.Now;
  1101. // if (data.Exhibitors.IndexOf(sOldCustomerGUID) > -1)
  1102. // {
  1103. // data.Exhibitors.Replace(sOldCustomerGUID, sCustomerGUID);
  1104. // }
  1105. // if (data.Agent == sOldCustomerGUID)
  1106. // {
  1107. // data.Agent = sCustomerGUID;
  1108. // }
  1109. // }
  1110. // db.Insertable<OTB_OPM_ExportExhibition>(lExportExh)
  1111. // .ExecuteCommand();
  1112. //}
  1113. #endregion
  1114. #region 進口
  1115. //var saImport = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_ImportExhibition>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID)
  1116. // .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Supplier, t2.Agent }).MergeTable()
  1117. // .Where(it => it.ExhibitionNO != @"" && it.OrgID == sCurrentOrgID && (it.Supplier.Contains(sOldCustomerGUID) || it.Agent == sOldCustomerGUID))
  1118. // .ToList();
  1119. //if (saImport.Count > 0)
  1120. //{
  1121. // List<OTB_OPM_Exhibition> lExh = db.Queryable<OTB_OPM_Exhibition>()
  1122. // .Where(p => p.SN == saImport.Select(x => x.SN).First())
  1123. // .ToList();
  1124. // foreach (OTB_OPM_Exhibition data in lExh)
  1125. // {
  1126. // data.OrgID = sOrgID;
  1127. // data.OrgID = sOrgID;
  1128. // data.CreateUser = i_crm.USERID;
  1129. // data.CreateDate = DateTime.Now;
  1130. // data.ModifyUser = i_crm.USERID;
  1131. // data.ModifyDate = DateTime.Now;
  1132. // }
  1133. // List<OTB_OPM_ImportExhibition> lImportExh = db.Queryable<OTB_OPM_ImportExhibition>()
  1134. // .Where(p => p.ExhibitionNO == saImport.Select(x => x.SN).First().ToString())
  1135. // .ToList();
  1136. // var exData = db.Insertable<OTB_OPM_Exhibition>(lExh)
  1137. // .IgnoreColumns(p => p == OTB_OPM_Exhibition.CN_SN)
  1138. // .ExecuteReturnIdentity();
  1139. // foreach (OTB_OPM_ImportExhibition data in lImportExh)
  1140. // {
  1141. // data.ExportBillNO = Guid.NewGuid().ToString();
  1142. // data.ExhibitionNO = exData.ToString();
  1143. // data.OrgID = sOrgID;
  1144. // data.CreateUser = i_crm.USERID;
  1145. // data.CreateDate = DateTime.Now;
  1146. // data.ModifyUser = i_crm.USERID;
  1147. // data.ModifyDate = DateTime.Now;
  1148. // if (data.Supplier.IndexOf(sOldCustomerGUID) > -1)
  1149. // {
  1150. // data.Supplier.Replace(sOldCustomerGUID, sCustomerGUID);
  1151. // }
  1152. // if (data.Agent == sOldCustomerGUID)
  1153. // {
  1154. // data.Agent = sCustomerGUID;
  1155. // }
  1156. // }
  1157. // db.Insertable<OTB_OPM_ImportExhibition>(lImportExh)
  1158. // .ExecuteCommand();
  1159. //}
  1160. #endregion
  1161. #region 其他
  1162. //var saOther = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_OtherExhibition>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID)
  1163. // .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Supplier, t2.Agent }).MergeTable()
  1164. // .Where(it => it.ExhibitionNO != @"" && it.OrgID == sCurrentOrgID && (it.Supplier == sOldCustomerGUID || it.Agent == sOldCustomerGUID))
  1165. // .ToList();
  1166. //if (saOther.Count > 0)
  1167. //{
  1168. // List<OTB_OPM_Exhibition> lExh = db.Queryable<OTB_OPM_Exhibition>()
  1169. // .Where(p => p.SN == saOther.Select(x => x.SN).First())
  1170. // .ToList();
  1171. // foreach (OTB_OPM_Exhibition data in lExh)
  1172. // {
  1173. // data.OrgID = sOrgID;
  1174. // data.CreateUser = i_crm.USERID;
  1175. // data.CreateDate = DateTime.Now;
  1176. // data.ModifyUser = i_crm.USERID;
  1177. // data.ModifyDate = DateTime.Now;
  1178. // }
  1179. // List<OTB_OPM_OtherExhibition> lOtherExh = db.Queryable<OTB_OPM_OtherExhibition>()
  1180. // .Where(p => p.ExhibitionNO == saOther.Select(x => x.SN).First().ToString())
  1181. // .ToList();
  1182. // var exData = db.Insertable<OTB_OPM_Exhibition>(lExh)
  1183. // .IgnoreColumns(p => p == OTB_OPM_Exhibition.CN_SN)
  1184. // .ExecuteReturnIdentity();
  1185. // foreach (OTB_OPM_OtherExhibition data in lOtherExh)
  1186. // {
  1187. // data.Guid = Guid.NewGuid().ToString();
  1188. // data.ExhibitionNO = exData.ToString();
  1189. // data.OrgID = sOrgID;
  1190. // data.CreateUser = i_crm.USERID;
  1191. // data.CreateDate = DateTime.Now;
  1192. // data.ModifyUser = i_crm.USERID;
  1193. // data.ModifyDate = DateTime.Now;
  1194. // if (data.Supplier == sOldCustomerGUID)
  1195. // {
  1196. // data.Supplier = sCustomerGUID;
  1197. // }
  1198. // if (data.Agent == sOldCustomerGUID)
  1199. // {
  1200. // data.Agent = sCustomerGUID;
  1201. // }
  1202. // }
  1203. // db.Insertable<OTB_OPM_OtherExhibition>(lOtherExh)
  1204. // .ExecuteCommand();
  1205. //}
  1206. #endregion
  1207. #region 其他(駒驛)
  1208. //var saOtherTG = db.Queryable<OTB_OPM_Exhibition, OTB_OPM_OtherExhibitionTG>((t1, t2) => t1.SN.ToString() == t2.ExhibitionNO && t1.OrgID == t2.OrgID)
  1209. // .Select((t1, t2) => new { t1.OrgID, t1.SN, t1.ExhibitionCode, t1.Exhibitioname_TW, t1.Exhibitioname_CN, t2.ExhibitionNO, t2.Exhibitors, t2.Agent }).MergeTable()
  1210. // .Where(it => it.ExhibitionNO != @"" && it.OrgID == sCurrentOrgID && (it.Exhibitors.Contains(sOldCustomerGUID) || it.Agent == sOldCustomerGUID))
  1211. // .ToList();
  1212. //if (saOtherTG.Count > 0)
  1213. //{
  1214. // List<OTB_OPM_Exhibition> lExh = db.Queryable<OTB_OPM_Exhibition>()
  1215. // .Where(p => p.SN == saOtherTG.Select(x => x.SN).First())
  1216. // .ToList();
  1217. // foreach (OTB_OPM_Exhibition data in lExh)
  1218. // {
  1219. // data.OrgID = sOrgID;
  1220. // data.CreateUser = i_crm.USERID;
  1221. // data.CreateDate = DateTime.Now;
  1222. // data.ModifyUser = i_crm.USERID;
  1223. // data.ModifyDate = DateTime.Now;
  1224. // }
  1225. // List<OTB_OPM_OtherExhibitionTG> lOtherTGExh = db.Queryable<OTB_OPM_OtherExhibitionTG>()
  1226. // .Where(p => p.ExhibitionNO == saOtherTG.Select(x => x.SN).First().ToString())
  1227. // .ToList();
  1228. // var exData = db.Insertable<OTB_OPM_Exhibition>(lExh)
  1229. // .IgnoreColumns(p => p == OTB_OPM_Exhibition.CN_SN)
  1230. // .ExecuteReturnIdentity();
  1231. // foreach (OTB_OPM_OtherExhibitionTG data in lOtherTGExh)
  1232. // {
  1233. // data.Guid = Guid.NewGuid().ToString();
  1234. // data.ExhibitionNO = exData.ToString();
  1235. // data.OrgID = sOrgID;
  1236. // data.CreateUser = i_crm.USERID;
  1237. // data.CreateDate = DateTime.Now;
  1238. // data.ModifyUser = i_crm.USERID;
  1239. // data.ModifyDate = DateTime.Now;
  1240. // if (data.Exhibitors.IndexOf(sOldCustomerGUID) > -1)
  1241. // {
  1242. // data.Exhibitors.Replace(sOldCustomerGUID, sCustomerGUID);
  1243. // }
  1244. // if (data.Agent == sOldCustomerGUID)
  1245. // {
  1246. // data.Agent = sCustomerGUID;
  1247. // }
  1248. // }
  1249. // db.Insertable<OTB_OPM_OtherExhibitionTG>(lOtherTGExh)
  1250. // .ExecuteCommand();
  1251. //}
  1252. #endregion
  1253. var iRel = db.Insertable(oEntity).ExecuteReturnEntity();
  1254. var iRelMst = db.Insertable(oMstEntity).ExecuteCommand();
  1255. rm = new SuccessResponseMessage(null, i_crm);
  1256. rm.DATA.Add(BLWording.REL, iRel);
  1257. } while (false);
  1258. return rm;
  1259. });
  1260. }
  1261. catch (Exception ex)
  1262. {
  1263. sMsg = Util.GetLastExceptionMsg(ex);
  1264. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(Customers_UpdService), @"客戶管理編輯", @"Update(客戶管理編輯(修改))", @"", @"", @"");
  1265. }
  1266. finally
  1267. {
  1268. if (null != sMsg)
  1269. {
  1270. rm = new ErrorResponseMessage(sMsg, i_crm);
  1271. }
  1272. }
  1273. return rm;
  1274. }
  1275. #endregion 客戶管理編輯(修改)
  1276. }
  1277. }