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.

288 lines
14 KiB

6 months ago
  1. using ICSSoft.ERPWMS.Entity;
  2. using Microsoft.Data.SqlClient;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ICSSoft.ERPWMS.SQL
  11. {
  12. public class UpdateVendorInfo
  13. {
  14. public static Result Update(List<UpdateVendorEntity> list)
  15. {
  16. Result res = new Result();
  17. try
  18. {
  19. string jsonstr = JsonConvert.SerializeObject(list);
  20. Log.WriteLogFile(jsonstr, "更新供应商日志");
  21. StringBuilder sb = new StringBuilder();//接口返回Message
  22. foreach (UpdateVendorEntity entity in list)
  23. {
  24. SqlConnection conn = new SqlConnection(ICSHelper.ReadConfig(ICSHelper.FileNameCompanyCon)["ERP"].ToString());
  25. conn.Open();
  26. SqlTransaction sqlTran = conn.BeginTransaction();
  27. SqlCommand cmd = new SqlCommand();
  28. cmd.Transaction = sqlTran;
  29. cmd.Connection = conn;
  30. try
  31. {
  32. if (string.IsNullOrWhiteSpace(entity.cVenCode))
  33. {
  34. throw new Exception("存在表头供应商编码为空!!!");
  35. }
  36. else
  37. {
  38. if (string.IsNullOrWhiteSpace(entity.WorkPoint))
  39. {
  40. throw new Exception("站点参数为空!!!");
  41. }
  42. string WorkPoint = ICSHelper.GetConnectStringTest(entity.WorkPoint);
  43. if (WorkPoint == "NotExit")
  44. {
  45. throw new Exception("站点编码不存在!");
  46. }
  47. #region 验证供应商编码是否存在
  48. string sqlCheck = "Select cVenCode from {0}.dbo.Vendor where cVenCode ='{1}'";
  49. sqlCheck = string.Format(sqlCheck, WorkPoint, entity.cVenCode);
  50. DataTable dtCheck = ICSHelper.SQlReturnData(sqlCheck, cmd);
  51. if (dtCheck.Rows.Count < 1)
  52. {
  53. throw new Exception("供应商编码不存在!!!");
  54. }
  55. #endregion
  56. #region 表头数据
  57. string sql = @"Update {0}.dbo.Vendor Set ";
  58. sql = string.Format(sql, WorkPoint);
  59. if (!string.IsNullOrWhiteSpace(entity.cVenName))
  60. {
  61. sql += " cVenName='{0}',";
  62. sql = string.Format(sql, entity.cVenName);
  63. }
  64. if (!string.IsNullOrWhiteSpace(entity.cVenAbbName))
  65. {
  66. sql += " cVenAbbName='{0}',";
  67. sql = string.Format(sql, entity.cVenAbbName);
  68. }
  69. if (!string.IsNullOrWhiteSpace(entity.cVCCode))
  70. {
  71. sql += " cVCCode='{0}',";
  72. sql = string.Format(sql, entity.cVCCode);
  73. }
  74. if (!string.IsNullOrWhiteSpace(entity.cVenHeadCode))
  75. {
  76. sql += " cVenHeadCode='{0}',";
  77. sql = string.Format(sql, entity.cVenHeadCode);
  78. }
  79. if (!string.IsNullOrWhiteSpace(entity.cVenExch_name))
  80. {
  81. sql += " cVenExch_name='{0}',";
  82. sql = string.Format(sql, entity.cVenExch_name);
  83. }
  84. if (entity.dVenDevDate!=null)
  85. {
  86. sql += " dVenDevDate='{0}',";
  87. sql = string.Format(sql, entity.dVenDevDate);
  88. }
  89. if (entity.dStopDate!=null)
  90. {
  91. sql += " dEndDate='{0}',";
  92. sql = string.Format(sql, entity.dStopDate);
  93. }
  94. if (entity.bVenTax != null)
  95. {
  96. sql += " bVenTax='{0}',";
  97. sql = string.Format(sql, entity.bVenTax);
  98. }
  99. if (entity.iVenTaxRate != null)
  100. {
  101. sql += " iVenTaxRate='{0}',";
  102. sql = string.Format(sql, entity.iVenTaxRate);
  103. }
  104. if (!string.IsNullOrWhiteSpace(entity.cVenPUOMProtocol))
  105. {
  106. sql += " cVenPUOMProtocol='{0}',";
  107. sql = string.Format(sql, entity.cVenPUOMProtocol);
  108. }
  109. if (!string.IsNullOrWhiteSpace(entity.cVenIType))
  110. {
  111. sql += " cVenIType='{0}',";
  112. sql = string.Format(sql, entity.cVenIType);
  113. }
  114. if (!string.IsNullOrWhiteSpace(entity.cDCCode))
  115. {
  116. sql += " cDCCode='{0}',";
  117. sql = string.Format(sql, entity.cDCCode);
  118. }
  119. if (!string.IsNullOrWhiteSpace(entity.cModifyPerson))
  120. {
  121. sql += " cModifyPerson='{0}',";
  122. sql = string.Format(sql, entity.cModifyPerson);
  123. }
  124. if (entity.dModifyDate != null)
  125. {
  126. sql += " dModifyDate='{0}',";
  127. sql = string.Format(sql, entity.dModifyDate);
  128. }
  129. sql = sql.TrimEnd(',');
  130. sql += " Where cVenCode ='{0}'";
  131. sql = string.Format(sql, entity.cVenCode);
  132. Log.WriteLogFile(sql, "更新供应商SQL日志");
  133. if (!ICSHelper.ExecuteNonQuery(sql, cmd))
  134. {
  135. throw new Exception("更新供应商表头失败!");
  136. }
  137. #endregion
  138. #region 表体数据
  139. if (entity.list.Count > 0)
  140. {
  141. //验证是否有银行信息
  142. string VenCheck = "Select * from {0}.dbo.VendorBank Where cVenCode ='{1}'";
  143. VenCheck = string.Format(VenCheck, WorkPoint, entity.cVenCode);
  144. DataTable dtVenCheck = ICSHelper.SQlReturnData(VenCheck, cmd);
  145. if (dtVenCheck!=null&&dtVenCheck.Rows.Count>0)
  146. {
  147. string sqlDelete = "Delete {0}.dbo.VendorBank Where cVenCode ='{1}'";
  148. sqlDelete = string.Format(sqlDelete, WorkPoint, entity.cVenCode);
  149. Log.WriteLogFile(sqlDelete, "更新供应商SQL日志");
  150. if (!ICSHelper.ExecuteNonQuery(sqlDelete, cmd))
  151. {
  152. throw new Exception("删除供应商表体失败!");
  153. }
  154. }
  155. //验证表体中是否存在默认银行设置
  156. List<UpdateVendorDetailEntity> listBool = entity.list.Where(x => x.bDefault == true).ToList();
  157. if (listBool.Count == 0)
  158. {
  159. throw new Exception("默认银行未设置!");
  160. }
  161. //验证表体中默认银行设置是否大于1个
  162. if (listBool.Count > 1)
  163. {
  164. throw new Exception("默认银行设置数大于1!");
  165. }
  166. string sqlBankUpt = "Update {0}.dbo.Vendor Set cVenAccount='{1}',cVenBank ='{3}' Where cVenCode ='{2}'";
  167. sqlBankUpt = string.Format(sqlBankUpt, WorkPoint, listBool[0].cAccountNum, entity.cVenCode, listBool[0].cBranch);
  168. Log.WriteLogFile(sqlBankUpt, "更新供应商SQL日志");
  169. if (!ICSHelper.ExecuteNonQuery(sqlBankUpt, cmd))
  170. {
  171. throw new Exception("更新供应商表头银行信息失败!");
  172. }
  173. string sqlDetail = "";
  174. bool InserFlag = true;
  175. foreach (UpdateVendorDetailEntity detail in entity.list)
  176. {
  177. if (string.IsNullOrWhiteSpace(detail.cVenCode))
  178. {
  179. InserFlag = false;
  180. sb.Append("存在表体供应商编码为空!!!");
  181. break;
  182. }
  183. if (detail.cVenCode != entity.cVenCode)
  184. {
  185. InserFlag = false;
  186. sb.Append("表体供应商编码与表头不一致,表头供应商编码为:" + entity.cVenCode + ",表体供应商编码为:" + detail.cVenCode + "!!!");
  187. break;
  188. }
  189. if (string.IsNullOrWhiteSpace(detail.cAccountNum))
  190. {
  191. InserFlag = false;
  192. sb.Append("银行账号为空,供应商编码为:" + detail.cVenCode + "!!!");
  193. break;
  194. }
  195. //验证所属银行
  196. string sqlBank = "Select cbankcode from {0}.dbo.AA_Bank Where cbankname ='{1}'";
  197. sqlBank = string.Format(sqlBank, WorkPoint, detail.cBank);
  198. DataTable dtBank = ICSHelper.SQlReturnData(sqlBank, cmd);
  199. if (dtBank != null && dtBank.Rows.Count > 0)
  200. {
  201. detail.cBank = dtBank.Rows[0]["cbankcode"].ToString();
  202. }
  203. else
  204. {
  205. InserFlag = false;
  206. sb.Append("所属银行不存在,供应商编码为:" + detail.cVenCode + "!!!");
  207. break;
  208. }
  209. sqlDetail += " Insert into {0}.dbo.VendorBank(cVenCode,cBranch,cBank,cAccountNum,cAccountName,bDefault) Values ('{1}','{2}','{3}','{4}','{5}','{6}');";
  210. sqlDetail = string.Format(sqlDetail, WorkPoint, detail.cVenCode, detail.cBranch, detail.cBank, detail.cAccountNum, detail.cAccountName, detail.bDefault);
  211. //Sort++;
  212. }
  213. if (InserFlag)
  214. {
  215. Log.WriteLogFile(sqlDetail, "更新供应商SQL日志");
  216. if (!ICSHelper.ExecuteNonQuery(sqlDetail, cmd))
  217. {
  218. throw new Exception("新增供应商表体失败!");
  219. }
  220. cmd.Transaction.Commit();
  221. }
  222. else
  223. {
  224. cmd.Transaction.Rollback();
  225. }
  226. }
  227. #endregion
  228. else
  229. {
  230. cmd.Transaction.Commit();
  231. }
  232. }
  233. }
  234. catch (Exception ex)
  235. {
  236. cmd.Transaction.Rollback();
  237. sb.Append("执行报错,供应商编码为:" + entity.cVenCode + ",报错信息:" + ex.Message + "!!!");
  238. continue;
  239. }
  240. finally
  241. {
  242. if (conn.State == ConnectionState.Open)
  243. {
  244. conn.Close();
  245. }
  246. conn.Dispose();
  247. }
  248. }
  249. if (sb.Length > 0)
  250. {
  251. res.IsSuccess = false;
  252. res.Message = sb.ToString();
  253. }
  254. else
  255. {
  256. res.IsSuccess = true;
  257. res.Message = "执行成功!";
  258. }
  259. return res;
  260. }
  261. catch (Exception ex)
  262. {
  263. res.IsSuccess = false;
  264. res.Message = ex.Message;
  265. return res;
  266. }
  267. }
  268. }
  269. }