WL_SyncService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using HH.YiDaSyncNC.Dtos;
  2. using HH.YiDaSyncNC.Dtos.NC;
  3. using HH.YiDaSyncNC.Helper;
  4. using HH.YiDaSyncNC.Models;
  5. using Newtonsoft.Json;
  6. using SqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Configuration;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Xml.Linq;
  14. namespace HH.YiDaSyncNC.DataSync
  15. {
  16. class WL_SyncService
  17. {
  18. public string _AppType;
  19. public string _SystemToken;
  20. public string _UserId;
  21. public string _FormId;
  22. public SqlSugarClient _db;
  23. public SqlSugarClient _db_nc;
  24. public string _NCApiUrl;
  25. public static string _Static_PK_xx_idcontrast = ConfigurationManager.AppSettings["xx_idcontrast"];
  26. public static string _Static_pk_org_jt = ConfigurationManager.AppSettings["pk_org_jt"];
  27. public WL_SyncService(string AppType, string SystemToken, string UserId, string FormId, SqlSugarClient db, SqlSugarClient db_nc, string NCApiUrl)
  28. {
  29. _AppType = AppType;
  30. _SystemToken = SystemToken;
  31. _UserId = UserId;
  32. _FormId = FormId;
  33. _db = db;
  34. _db_nc = db_nc;
  35. _NCApiUrl = NCApiUrl;
  36. }
  37. public class IdCodeModel
  38. {
  39. public int sourceId { get; set; }
  40. public string Id { get; set; }
  41. }
  42. public void DataSync()
  43. {
  44. DingTalkService _dingTalkService = new DingTalkService(_AppType, _SystemToken, _UserId, _FormId);
  45. var codeList = new string[] { "01130209", "02130200", "04010192", "04010193" };
  46. var ncNo = new string[] { "1001G1100000000EMRSE", "1001G1100000000EMRSK", "1001G1100000000EMRSR", "1001G1100000000EMRST" };
  47. for (int i = 0; i < codeList.Count(); i++)
  48. {
  49. var idList1 = _dingTalkService.GetFormIdList("{\"textField_lr3axfje\":\"" + codeList[i] + "\"}");
  50. var ncResData1 = new YiDaResDto()
  51. {
  52. syncTime = DateTime.Now,
  53. };
  54. ncResData1.bSuccess = true;
  55. ncResData1.ncNo = ncNo[i];
  56. ncResData1.log = "同步成功!";
  57. var ydRes1 = _dingTalkService.UpdateFormDataSyncDataById(ncResData1, idList1.FirstOrDefault());
  58. }
  59. //DBHelper.WriteLog($"进入程序");
  60. //DingTalkService _dingTalkService = new DingTalkService(_AppType, _SystemToken, _UserId, _FormId);
  61. //// 宜搭全量数据Id
  62. //var idList = _dingTalkService.GetFormIdList("{\"radioField_lycrvvtk\":\"Y\"}");
  63. //DBHelper.WriteLog(JsonConvert.SerializeObject(idList), "idList");
  64. //// NC ID对照表数据
  65. //var ncDataIdList = _db_nc.Queryable<xx_idcontrast>().Where(w => w.bill_type == "material").ToList();
  66. //// 获取当前最大Id
  67. //var newId = 100000001;
  68. //var maxData = ncDataIdList.OrderByDescending(obd => Convert.ToDateTime(obd.ts)).FirstOrDefault();
  69. //if (maxData != null && maxData.pk_idcontra.Contains(_Static_PK_xx_idcontrast))
  70. //{
  71. // newId = Convert.ToInt32(maxData.pk_idcontra.Replace(_Static_PK_xx_idcontrast, "")) + 1;
  72. //}
  73. //#region 获取NC中选项数据的Id
  74. //var pk_marbasclassDic = _db_nc.Queryable<bd_marbasclass>().ToList();
  75. //var pk_measdocDic = _db_nc.Queryable<bd_measdoc>().ToList();
  76. //var pk_mattaxesDic = _db_nc.Queryable<bd_mattaxes>().ToList();
  77. //var pk_brandDic = _db_nc.Queryable<bd_branddoc>().ToList();
  78. //#endregion
  79. //#region 循环数据处理业务逻辑
  80. //foreach (var item in idList.Where(w => !ncDataIdList.Select(s => s.file_id).ToList().Contains(w)))
  81. //{
  82. // DBHelper.WriteLog($"进入程序1");
  83. // // 获取此条数据详情
  84. // string createTime = "";
  85. // string modifyTime = "";
  86. // var ydDataSource = _dingTalkService.GetFormDataById(item, out createTime, out modifyTime);
  87. // DBHelper.WriteLog(ydDataSource["radioField_lycrvvtk"].ToString(),"222");
  88. // if (ydDataSource["radioField_lycrvvtk"].ToString() == "Y")
  89. // {
  90. // DBHelper.WriteLog($"进入程序2");
  91. // #region 构建数据体
  92. // var dataSource = new WL_SyncServiceModel.DataModel()
  93. // {
  94. // pk_group = "0",
  95. // pk_org = "0",
  96. // code = ydDataSource["textField_lr3axfje"].ToString(),
  97. // name = ydDataSource["textField_lr383i4h"].ToString(),
  98. // ename = ydDataSource["textField_lwitq6vu"].ToString(),
  99. // materialshortname = ydDataSource["textField_lwitq6vv"].ToString(),
  100. // materialspec = ydDataSource["textField_ltxy3kh3"].ToString(),
  101. // materialtype = ydDataSource["textField_ltxy3kh4"].ToString(),
  102. // ematerialspec = ydDataSource["textField_lwitq6vw"].ToString(),
  103. // materialmnecode = ydDataSource["textField_lwitq6vx"].ToString(),
  104. // };
  105. // if (ydDataSource.FirstOrDefault(f => f.Key == "selectField_lwitq6w4").Value != null && !string.IsNullOrWhiteSpace(ydDataSource["selectField_lwitq6w4"].ToString()))
  106. // {
  107. // var data = pk_marbasclassDic.FirstOrDefault(f => f.name.Trim() == ydDataSource["selectField_lwitq6w4"].ToString());
  108. // if (data != null)
  109. // {
  110. // dataSource.pk_marbasclass = data.code; // 分类
  111. // }
  112. // }
  113. // else
  114. // {
  115. // var resData = new YiDaResDto();
  116. // resData.bSuccess = false;
  117. // resData.syncTime = DateTime.Now;
  118. // resData.ncNo = "";
  119. // resData.log = "物料分类为空,无法传入NC";
  120. // _dingTalkService.UpdateFormDataSyncDataById(resData, item);
  121. // continue;
  122. // }
  123. // if (!string.IsNullOrWhiteSpace(ydDataSource["selectField_lwitq6w5"].ToString()))
  124. // {
  125. // dataSource.pk_measdoc = pk_measdocDic.FirstOrDefault(f => f.name == ydDataSource["selectField_lwitq6w5"].ToString()).code; // 主计量单位
  126. // }
  127. // if (ydDataSource.FirstOrDefault(f => f.Key == "selectField_lwitq6w6").Value != null && !string.IsNullOrWhiteSpace(ydDataSource["selectField_lwitq6w6"].ToString()))
  128. // {
  129. // dataSource.pk_mattaxes = pk_mattaxesDic.FirstOrDefault(f => f.mattaxesname == ydDataSource["selectField_lwitq6w6"].ToString()).mattaxescode; // 税类
  130. // }
  131. // //if (!string.IsNullOrWhiteSpace(ydDataSource["selectField_lwitq6we"].ToString()))
  132. // //{
  133. // // dataSource.pk_brand = pk_brandDic.FirstOrDefault(f => f.name == ydDataSource["selectField_lwitq6we"].ToString()).code; // 品牌
  134. // //}
  135. // if (!string.IsNullOrWhiteSpace(ydDataSource["selectField_ltxy3kh6"].ToString()))
  136. // {
  137. // switch (ydDataSource["selectField_ltxy3kh6"].ToString())
  138. // {
  139. // case "启用":
  140. // dataSource.enablestate = ((int)EnumType.enablestate.已启用).ToString();
  141. // break;
  142. // case "停用":
  143. // dataSource.enablestate = ((int)EnumType.enablestate.已停用).ToString();
  144. // break;
  145. // }
  146. // }
  147. // #endregion
  148. // #region 数据传入NC并获取结果
  149. // var xmlData = WL_SyncServiceModel.ImportXml.Header(dataSource, item);
  150. // XElement tempData = WL_SyncServiceModel.ImportXml.TemplateHeade();
  151. // var results = NCImportFunc.NCImport(tempData, xmlData, _NCApiUrl + "?account=LY01&groupcode=0");
  152. // DBHelper.WriteLog($"进入程序3");
  153. // XElement xE = XElement.Parse(results);
  154. // string resultcode = xE.Element("sendresult").Element("resultcode").Value;
  155. // string resultdescription = xE.Element("sendresult").Element("resultdescription")?.Value;
  156. // #endregion
  157. // #region 回写状态
  158. // var ncResData = new YiDaResDto()
  159. // {
  160. // syncTime = DateTime.Now,
  161. // };
  162. // if (resultcode == "1")
  163. // {
  164. // DBHelper.WriteLog($"Id为【{JsonConvert.SerializeObject(item)}】的单据操作成功!");
  165. // ncResData.bSuccess = true;
  166. // ncResData.ncNo = xE.Element("sendresult").Element("content").Value;
  167. // ncResData.log = "同步成功!";
  168. // }
  169. // else
  170. // {
  171. // if (resultdescription.Contains("不允许重复"))
  172. // {
  173. // // 已存在数据,绑定Id
  174. // var nowData = _db_nc.Queryable<bd_material>().First(f => f.code == dataSource.code && f.pk_org == _Static_pk_org_jt);
  175. // var idcData = new xx_idcontrast()
  176. // {
  177. // bill_type = "material",
  178. // dr = 0,
  179. // file_id = item,
  180. // pk_bill = nowData.pk_material,
  181. // pk_idcontra = _Static_PK_xx_idcontrast + newId
  182. // };
  183. // _db_nc.Insertable(idcData).ExecuteCommand();
  184. // newId++;
  185. // ncResData.bSuccess = true;
  186. // ncResData.ncNo = nowData.pk_material;
  187. // ncResData.log = "数据已存在,无需同步,已自动对照!";
  188. // }
  189. // else
  190. // {
  191. // DBHelper.WriteLog($"Id为【{JsonConvert.SerializeObject(item)}】的单据操作失败!" + resultdescription + ";");
  192. // ncResData.bSuccess = false;
  193. // ncResData.ncNo = "";
  194. // ncResData.log = "同步失败!原因为【" + resultdescription + "】";
  195. // }
  196. // }
  197. // var ydRes = _dingTalkService.UpdateFormDataSyncDataById(ncResData, item);
  198. // #endregion
  199. // }
  200. //}
  201. //#endregion
  202. }
  203. }
  204. }