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.

266 lines
15 KiB

2 years ago
  1. using EasyBL.WebApi.Message;
  2. using EasyNet;
  3. using Entity;
  4. using Entity.Sugar;
  5. using HtmlAgilityPack;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using SqlSugar;
  9. using SqlSugar.Base;
  10. using System;
  11. using System.Collections.Generic;
  12. namespace EasyBL.WEBSITE.TG
  13. {
  14. public class TGAPIService : ServiceBase
  15. {
  16. #region 在線預約
  17. /// <summary>
  18. /// 在線預約
  19. /// </summary>
  20. /// <param name="i_crm"></param>
  21. /// <returns></returns>
  22. public ResponseMessage Appoint(RequestMessage i_crm)
  23. {
  24. ResponseMessage rm = null;
  25. string sMsg = null;
  26. var bSend = false;
  27. var db = SugarBase.GetIntance();
  28. try
  29. {
  30. do
  31. {
  32. var sExhibitionNO = _fetchString(i_crm, "ExhibitionNO");
  33. var sCompName = _fetchString(i_crm, "CompName");
  34. var oExhibition = db.Queryable<OTB_OPM_Exhibition>().Single(x => x.SN == int.Parse(sExhibitionNO));
  35. var sCurDate = DateTime.Now.ToString("yyyyMMdd");
  36. var oImportCustomers = db.Queryable<OTB_CRM_ImportCustomers>()
  37. .First(x => x.OrgID == i_crm.ORIGID && x.ExhibitionNO.ToString() == sExhibitionNO && x.CustomerCName == sCompName);
  38. var oPackingOrder = new OTB_WSM_PackingOrder
  39. {
  40. AppointNO = SerialNumber.GetMaxNumberByType(i_crm.ORIGID, oExhibition.ExhibitionCode, MaxNumberType.Empty, i_crm.USERID, 3, sCurDate),
  41. OrgID = i_crm.ORIGID,
  42. ExhibitionNO = sExhibitionNO,
  43. CustomerId = oImportCustomers?.guid,
  44. CompName = sCompName,
  45. MuseumMumber = _fetchString(i_crm, "MuseumMumber"),
  46. AppointUser = _fetchString(i_crm, "AppointUser"),
  47. AppointTel = _fetchString(i_crm, "AppointTel"),
  48. AppointEmail = _fetchString(i_crm, "AppointEmail"),
  49. Contactor = _fetchString(i_crm, "Contactor"),
  50. ContactTel = _fetchString(i_crm, "ContactTel"),
  51. ApproachTime = Convert.ToDateTime(_fetchString(i_crm, "ApproachTime") + " " + _fetchString(i_crm, "ApproachTime_Hour") + ":" + _fetchString(i_crm, "ApproachTime_Min")),
  52. ExitTime = Convert.ToDateTime(_fetchString(i_crm, "ExitTime") + " " + _fetchString(i_crm, "ExitTime_Hour") + ":" + _fetchString(i_crm, "ExitTime_Min")),
  53. PackingInfo = _fetchString(i_crm, "PackingInfo"),
  54. Total = Convert.ToDecimal(_fetchString(i_crm, "Total")),
  55. AppointDateTime = DateTime.Now,
  56. CreateDate = DateTime.Now,
  57. ModifyDate = DateTime.Now
  58. };
  59. //獲取Email郵件格式
  60. var sTemplId = "Appoint_TG" + (i_crm.LANG == "en" ? "_en" : "");
  61. var oEmailTempl = db.Queryable<OTB_SYS_Email>().Single(it => it.OrgID == i_crm.ORIGID && it.EmailID == sTemplId);
  62. if (oEmailTempl != null)
  63. {
  64. //寄信開始
  65. var sEmailBody = oEmailTempl.BodyHtml
  66. .Replace("{{:AppointNO}}", oPackingOrder.AppointNO)
  67. .Replace("{{:CompName}}", oPackingOrder.CompName)
  68. .Replace("{{:ExpoName}}", i_crm.LANG == "en" ? oExhibition.Exhibitioname_EN : oExhibition.Exhibitioname_TW)
  69. .Replace("{{:MuseumMumber}}", oPackingOrder.MuseumMumber)
  70. .Replace("{{:AppointUser}}", oPackingOrder.AppointUser)
  71. .Replace("{{:AppointTel}}", oPackingOrder.AppointTel)
  72. .Replace("{{:AppointEmail}}", oPackingOrder.AppointEmail)
  73. .Replace("{{:Contactor}}", oPackingOrder.Contactor)
  74. .Replace("{{:ContactTel}}", oPackingOrder.ContactTel)
  75. .Replace("{{:ApproachTime}}", Convert.ToDateTime(oPackingOrder.ApproachTime).ToString("yyyy/MM/dd HH:mm"))
  76. .Replace("{{:ExitTime}}", Convert.ToDateTime(oPackingOrder.ExitTime).ToString("yyyy/MM/dd HH:mm"))
  77. .Replace("{{:Total}}", String.Format("{0:N0}", oPackingOrder.Total));
  78. if (!string.IsNullOrEmpty(oExhibition.CostRulesId))
  79. {
  80. var oExhibitionRules = db.Queryable<OTB_WSM_ExhibitionRules>().Single(x => x.Guid == oExhibition.CostRulesId);
  81. sEmailBody = sEmailBody.Replace("{{:ServiceInstruction}}", i_crm.LANG == "en" ? oExhibitionRules.ServiceInstruction_EN : oExhibitionRules.ServiceInstruction);
  82. }
  83. var doc = new HtmlDocument();
  84. doc.LoadHtml(sEmailBody);
  85. HtmlNode hService_Temple = null; //航班信息模版
  86. foreach (HtmlNode NodeTb in doc.DocumentNode.SelectNodes("//tr")) //按照<table>節點尋找
  87. {
  88. if (NodeTb.Attributes["data-repeat"] != null && NodeTb.Attributes["data-repeat"].Value == "Y")
  89. {
  90. hService_Temple = NodeTb;
  91. var hReplace = HtmlNode.CreateNode("[servicetemple]");
  92. NodeTb.ParentNode.InsertAfter(hReplace, NodeTb);
  93. NodeTb.Remove();
  94. break;
  95. }
  96. }
  97. sEmailBody = doc.DocumentNode.OuterHtml; //總模版
  98. var sService_Html = "";
  99. var sService_Temple = hService_Temple.OuterHtml; //服務信息模板
  100. var ja = (JArray)JsonConvert.DeserializeObject(oPackingOrder.PackingInfo);
  101. var oExpoType_TW = new Map { { "01", "裸機" }, { "02", "木箱" }, { "03", "散貨" }, { "04", "打板" }, { "05", "其他" } };
  102. var oExpoType_EN = new Map { { "01", "Unwrapped" }, { "02", "Wooden Crate" }, { "03", "Bulk Cargo" }, { "04", "Pallet" }, { "05", "Other" } };
  103. var saService_TW = new List<string> { "堆高機服務", "拆箱", "裝箱", "空箱收送與儲存(展覽期間)" };
  104. var saService_EN = new List<string> { "Forklift", "Unpacking", "Packing", "'Empty Crate Transport And StorageEmpty Crate Transport and Storage During the Exhibition" };
  105. var builder = new System.Text.StringBuilder();
  106. builder.Append(sService_Html);
  107. foreach (JObject jo in ja)
  108. {
  109. var sExpoType = jo["ExpoType"].ToString();
  110. var sExpoLen = jo["ExpoLen"].ToString();
  111. var sExpoWidth = jo["ExpoWidth"].ToString();
  112. var sExpoHeight = jo["ExpoHeight"].ToString();
  113. var sExpoWeight = jo["ExpoWeight"].ToString();
  114. var sExpoNumber = jo["ExpoNumber"].ToString();
  115. var sExpoStack = jo["ExpoStack"].ToString();//堆高機
  116. var sExpoSplit = jo["ExpoSplit"].ToString();//拆箱
  117. var sExpoPack = jo["ExpoPack"].ToString();//裝箱
  118. var sExpoFeed = jo["ExpoFeed"].ToString();//空箱收送與儲存(展覽期間)
  119. var sSubTotal = jo["SubTotal"].ToString();
  120. var dExpoLen = Convert.ToDecimal(sExpoLen == "" ? "0" : sExpoLen);
  121. var dExpoWidth = Convert.ToDecimal(sExpoWidth == "" ? "0" : sExpoWidth);
  122. var dExpoHeight = Convert.ToDecimal(sExpoHeight == "" ? "0" : sExpoHeight);
  123. var dExpoWeight = Convert.ToDecimal(sExpoWeight == "" ? "0" : sExpoWeight);
  124. var dSubTotal = Convert.ToDecimal(sSubTotal);
  125. var saText = new List<string>();
  126. if (sExpoStack == "True")
  127. {
  128. saText.Add(i_crm.LANG == "en" ? saService_EN[0].ToString() : saService_TW[0].ToString());
  129. }
  130. if (sExpoSplit == "True")
  131. {
  132. saText.Add(i_crm.LANG == "en" ? saService_EN[1].ToString() : saService_TW[1].ToString());
  133. }
  134. if (sExpoPack == "True")
  135. {
  136. saText.Add(i_crm.LANG == "en" ? saService_EN[2].ToString() : saService_TW[2].ToString());
  137. }
  138. if (sExpoFeed == "True")
  139. {
  140. saText.Add(i_crm.LANG == "en" ? saService_EN[3].ToString() : saService_TW[3].ToString());
  141. }
  142. builder.Append(sService_Temple
  143. .Replace("{{:ExpoType}}", i_crm.LANG == "en" ? oExpoType_EN[sExpoType].ToString() : oExpoType_TW[sExpoType].ToString())
  144. .Replace("{{:ExpoSize}}", String.Format("{0:N0}", dExpoLen) + "*" + String.Format("{0:N0}", dExpoWidth) + "*" + String.Format("{0:N0}", dExpoHeight))
  145. .Replace("{{:ExpoWeight}}", String.Format("{0:N0}", dExpoWeight))
  146. .Replace("{{:ExpoNumber}}", sExpoNumber)
  147. .Replace("{{:ServiceText}}", string.Join(",", saText))
  148. .Replace("{{:SubTotal}}", String.Format("{0:N0}", dSubTotal)));
  149. }
  150. sService_Html = builder.ToString();
  151. sEmailBody = sEmailBody.Replace("[servicetemple]", sService_Html);
  152. var oEmail = new Emails();
  153. var saEmailTo = new List<EmailTo>(); //收件人
  154. var oEmailTo = new EmailTo
  155. {
  156. ToUserID = "",
  157. ToUserName = oPackingOrder.AppointUser,
  158. ToEmail = oPackingOrder.AppointEmail,
  159. Type = "to"
  160. };
  161. saEmailTo.Add(oEmailTo);
  162. var sServiceEmail = Common.GetSystemSetting(db, i_crm.ORIGID, "ServiceEmail");
  163. var saServiceEmail = sServiceEmail.Split(new string[] { @";", @",", @",", @"|" }, StringSplitOptions.RemoveEmptyEntries);
  164. foreach (var _email in saServiceEmail)
  165. {
  166. var oEmailBc = new EmailTo
  167. {
  168. ToUserID = "",
  169. ToUserName = _email,
  170. ToEmail = _email,
  171. Type = "bcc"
  172. };
  173. saEmailTo.Add(oEmailBc);
  174. }
  175. oEmail.FromUserName = i_crm.LANG == "en" ? "Online Booking" : "線上預約";
  176. oEmail.Title = oEmailTempl.EmailSubject + (i_crm.LANG == "en" ? "(Booking No.:" : "(單號:") + oPackingOrder.AppointNO + ")";
  177. oEmail.EmailBody = sEmailBody;
  178. oEmail.IsCCSelf = false;
  179. oEmail.Attachments = null;
  180. oEmail.EmailTo = saEmailTo;
  181. //bSend = new MailService(i_crm.ORIGID, true).MailFactory(oEmail, out sMsg);
  182. bSend = new MailService(i_crm.ORIGID).MailFactory(oEmail, out sMsg);
  183. if (bSend || oPackingOrder.AppointUser.IndexOf("***TEST***") > -1)
  184. {
  185. db.Insertable(oPackingOrder).ExecuteCommand();
  186. }
  187. }
  188. rm = new SuccessResponseMessage(null, i_crm);
  189. rm.DATA.Add(BLWording.REL, bSend);
  190. rm.DATA.Add("AppointNO", oPackingOrder.AppointNO);
  191. } while (false);
  192. }
  193. catch (Exception ex)
  194. {
  195. sMsg = Util.GetLastExceptionMsg(ex);
  196. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WebSite.TG.TGAPIService", "", "Appoint(在線預約)", "", "", "");
  197. }
  198. finally
  199. {
  200. if (null != sMsg)
  201. {
  202. rm = new ErrorResponseMessage(sMsg, i_crm);
  203. }
  204. }
  205. return rm;
  206. }
  207. #endregion 在線預約
  208. #region 獲取預約明細
  209. /// <summary>
  210. /// 獲取預約明細
  211. /// </summary>
  212. /// <param name="i_crm"></param>
  213. /// <returns></returns>
  214. public ResponseMessage GetAppointInfo(RequestMessage i_crm)
  215. {
  216. ResponseMessage rm = null;
  217. string sMsg = null;
  218. var db = SugarBase.GetIntance();
  219. try
  220. {
  221. do
  222. {
  223. var sAppointNO = _fetchString(i_crm, "AppointNO");
  224. var sdb = new SimpleClient<OTB_WSM_PackingOrder>(db);
  225. var oPackingOrder = sdb.GetById(sAppointNO);
  226. var oRules = db.Queryable<OTB_WSM_ExhibitionRules, OTB_OPM_Exhibition>((t1, t2) => t1.OrgID == t2.OrgID && t1.Guid == t2.CostRulesId)
  227. .Where((t1, t2) => t2.SN == int.Parse(oPackingOrder.ExhibitionNO))
  228. .Select((t1, t2) => new { Info = t1, ExpoName = t2.Exhibitioname_TW })
  229. .Single();
  230. rm = new SuccessResponseMessage(null, i_crm);
  231. rm.DATA.Add(BLWording.REL, oPackingOrder);
  232. rm.DATA.Add("rule", oRules);
  233. } while (false);
  234. }
  235. catch (Exception ex)
  236. {
  237. sMsg = Util.GetLastExceptionMsg(ex);
  238. LogAndSendEmail(sMsg + "Params:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, "EasyBL.WebSite.TG.TGAPIService", "", "GetAppointInfo(獲取預約明細)", "", "", "");
  239. }
  240. finally
  241. {
  242. if (null != sMsg)
  243. {
  244. rm = new ErrorResponseMessage(sMsg, i_crm);
  245. }
  246. }
  247. return rm;
  248. }
  249. #endregion 獲取預約明細
  250. }
  251. }