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.

246 lines
14 KiB

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