Stock.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using cuidian.Sql;
  8. using WangToTempDb;
  9. using cuidian.OpenApi;
  10. using cuidian.OpenApi.Api;
  11. using cuidian.OpenApi.Model;
  12. using cuidian.OpenApi.Utils;
  13. using cuidian.Common;
  14. namespace YlInit
  15. {
  16. public class Stock : IProcess
  17. {
  18. StringBuilder __sb = new StringBuilder();
  19. public void Process()
  20. {
  21. __WangToTemp();
  22. __TempDbToUfida();
  23. }
  24. private void __WangToTemp()
  25. {
  26. if (__Step0()) return;
  27. __Step1();
  28. __Step2();
  29. }
  30. /// <summary>
  31. /// 判断是否有记录
  32. /// </summary>
  33. /// <returns></returns>
  34. private bool __Step0()
  35. {
  36. string sql = "select count(1) from wtu..stock";
  37. int cnt = (int)DbUtils.ExecuteScalar(sql, ConnectionUtils.Instance.GetConnection());
  38. return cnt > 0;
  39. }
  40. /// <summary>
  41. /// 第一步,用仓库去抓库存
  42. /// </summary>
  43. private void __Step1()
  44. {
  45. string sql = "select cwhcode from warehouse";
  46. DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection());
  47. foreach (DataRow dr in dt.Rows)
  48. {
  49. StockUpLoad up = new StockUpLoad();
  50. Dictionary<string, string> args = new Dictionary<string, string>();
  51. args["warehouse_no"] = dr["cwhcode"].ToString();
  52. args["start_time"] = "2019-9-1 00:00:00";
  53. args["end_time"] = Convert.ToDateTime(DateTime.Now.AddMinutes(-10).ToString("yyyy-MM-dd HH:mm:ss")).ToString();
  54. up.UpLoad(args);
  55. }
  56. }
  57. /// <summary>
  58. /// 用存货去找遗漏的资料
  59. /// </summary>
  60. private void __Step2()
  61. {
  62. string sql = "select cinvcode from inventory where cinvcode not in (select distinct cinvcode from wtu..stock)";
  63. DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection());
  64. foreach (DataRow item in dt.Rows)
  65. {
  66. StockUpLoad up = new StockUpLoad();
  67. Dictionary<string, string> args = new Dictionary<string, string>();
  68. args["spec_no"] = item["cinvcode"].ToString();
  69. up.UpLoad(args);
  70. }
  71. }
  72. private void __TempDbToUfida()
  73. {
  74. string sql = "select distinct cwhcode from stock ORDER by cwhcode";
  75. DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection("TempDB"));
  76. foreach (DataRow dr in dt.Rows)
  77. {
  78. __GetStock(dr["cwhcode"].ToString());
  79. }
  80. }
  81. private void __GetStock(string cwhcode)
  82. {
  83. string sql = "select * from stock where cwhcode='" + cwhcode + "'";
  84. DataTable dt = DbUtils.Fill(sql, ConnectionUtils.Instance.GetConnection("TempDB"));
  85. List<Entry> entrys = new List<Entry>();
  86. int i = 0;
  87. List<int> ids = new List<int>();
  88. DataRow tmp = null;
  89. if (dt.Rows.Count == 0) return;
  90. foreach (DataRow item in dt.Rows)
  91. {
  92. ids.Add(Convert.ToInt32(item["sysid"]));
  93. tmp = item;
  94. if (i == 300)
  95. {
  96. __Add(entrys, item,ids);
  97. i = 0;
  98. entrys.Clear();
  99. ids.Clear();
  100. }
  101. else
  102. {
  103. Entry detail = __GetDetail(item);
  104. detail.rowno = (i + 1).ToString();
  105. entrys.Add(detail);
  106. i++;
  107. }
  108. }
  109. if (entrys.Count > 0)
  110. {
  111. __Add(entrys, tmp, ids);
  112. }
  113. if (__sb.Length > 0) throw new Exception(__sb.ToString());
  114. }
  115. /// <summary>
  116. /// 删除成功记录
  117. /// </summary>
  118. private void __Delete(List<int> ids)
  119. {
  120. string sql = "delete from stock where sysid in (" + ListUtils.ToString(ids, ",") + ")";
  121. DbUtils.ExecuteNonQuery(sql, ConnectionUtils.Instance.GetConnection("TempDB"));
  122. }
  123. private void __Add(List<Entry> entrys,DataRow dr,List<int> ids)
  124. {
  125. Otherin v = __GetHead(dr);
  126. Entry[] details = new Entry[entrys.Count];
  127. entrys.CopyTo(details);
  128. v.entry = details;
  129. OtherinRoot vr = new OtherinRoot();
  130. vr.otherin = v;
  131. OtherinApi api = new OtherinApi();
  132. BusinessObject result = api.Add(JsonUtils.GetJsonString(vr));
  133. if (result.IsError == false)
  134. {
  135. api.Audit(result.Id);
  136. __Delete(ids);
  137. }
  138. else
  139. {
  140. __sb.AppendLine(result.ErrMsg);
  141. }
  142. }
  143. private Otherin __GetHead(DataRow item)
  144. {
  145. Otherin v = new Otherin();
  146. v.date = DateTime.Now.ToString("yyyy-MM-dd");
  147. v.warehousecode = item["cwhcode"].ToString();
  148. v.define5 = "1";
  149. v.define1 = "来源旺店通";
  150. return v;
  151. }
  152. private Entry __GetDetail(DataRow dr)
  153. {
  154. Entry rtn = new Entry();
  155. rtn.inventorycode = dr["cinvcode"].ToString();
  156. rtn.quantity = dr["qty"].ToString();
  157. decimal price = Convert.ToDecimal(dr["price"]);
  158. if (price > 0)
  159. {
  160. rtn.price = price.ToString();
  161. rtn.cost = Math.Round( Convert.ToDecimal(dr["qty"]) * price,2).ToString();
  162. }
  163. return rtn;
  164. }
  165. }
  166. }