ImportDataOrExcelService.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using SqlSugar;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using wms.sqlsugar.model.fj;
  10. using WMS.BZSqlSugar;
  11. using WMS.Info;
  12. using WMS.Info.Models;
  13. using WMS.Util;
  14. namespace WMS.BZServices
  15. {
  16. public class DBCells
  17. {
  18. /// <summary>
  19. /// 表名
  20. /// </summary>
  21. public string TableName { get; set; }
  22. /// <summary>
  23. /// 字段名
  24. /// </summary>
  25. public string CelName { get; set; }
  26. /// <summary>
  27. /// 字段说明
  28. /// </summary>
  29. public string CelMemo { get; set; }
  30. /// <summary>
  31. /// 字段类型
  32. /// </summary>
  33. public string CelType { get; set; }
  34. /// <summary>
  35. /// 字段是否允许未空
  36. /// </summary>
  37. public string CelIsNull { get; set; }
  38. /// <summary>
  39. /// 字段默认值
  40. /// </summary>
  41. public string CelDefualVal { get; set; }
  42. /// <summary>
  43. /// 是否为配置项
  44. /// </summary>
  45. public bool IsConfig { get; set; }
  46. /// <summary>
  47. /// Execl名称
  48. /// </summary>
  49. public string ExeclName { get; set; }
  50. }
  51. public class DBTableCell
  52. {
  53. public string BusType { get; set; } // 业务类型
  54. public string TableType { get; set; } // 表类型
  55. public string TablesName { get; set; } // 表名
  56. public string CellsName { get; set; } // 列名
  57. public string CellsMemo { get; set; } // 备注
  58. public string CellsType { get; set; } // 字段类型
  59. public string CellsNULLABLE { get; set; } // 是否允许未空
  60. }
  61. public class ImportDataOrExcelService
  62. {
  63. private readonly Repository<BillMachinfo> _billMachinfoRepository;
  64. private readonly Repository<BaseWarehouse> _baseWarehouseRepository;
  65. private readonly Repository<BaseWarearea> _baseWareareaRepository;
  66. public ImportDataOrExcelService(Repository<BillMachinfo> billMachinfoRepository, Repository<BaseWarehouse> baseWarehouseRepository, Repository<BaseWarearea> baseWareareaRepository)
  67. {
  68. _billMachinfoRepository = billMachinfoRepository;
  69. _baseWarehouseRepository = baseWarehouseRepository;
  70. _baseWareareaRepository = baseWareareaRepository;
  71. }
  72. public JsonExecl ImportBillMatInfo(DataTable execlDt, int busType, string loginName)
  73. {
  74. JsonExecl json = new JsonExecl();
  75. json.Type = 0;
  76. string sql = "";
  77. // 保存需要操作的表名
  78. List<string> LstTable = new List<string>();
  79. // 保存数据库表字段信息
  80. List<DBTableCell> lstHeat = new List<DBTableCell>();
  81. // 保存导出的数据
  82. DataTable ImportDT = new DataTable();
  83. try
  84. {
  85. if (busType.ToString() != "100")
  86. throw new Exception("单据类型不正确");
  87. if (execlDt == null)
  88. throw new Exception("数据源不能为null");
  89. if (execlDt.Rows.Count <= 0)
  90. throw new Exception("数据源数据行数必须大于0");
  91. // 获取源数据
  92. DataTable dtExeclSouse = execlDt;
  93. var warehouselist = _baseWarehouseRepository.Queryable().ToList();
  94. var warearealist = _baseWareareaRepository.Queryable().ToList();
  95. List<MachInfoExcelModel> bills = dtExeclSouse.ToList<MachInfoExcelModel>(rowMapper: new ColumnAttributeMapper<MachInfoExcelModel>());
  96. var Machinfos = _billMachinfoRepository.Queryable().ToList();
  97. List<BillMachinfo> BillMachinfolists = new List<BillMachinfo>();
  98. foreach (var item in bills)
  99. {
  100. long? warehouseareaid = 0;
  101. var warehouse = warehouselist.FirstOrDefault(o => o.Code == (item.WearhouseNo + item.NS));
  102. if (warehouse != null)
  103. {
  104. warehouseareaid = warearealist.FirstOrDefault(o => o.WarehouseId == warehouse.Id)?.Id;
  105. }
  106. var nos = item.No.Split(' ');
  107. if (nos.Length > 1)
  108. {
  109. var first = Regex.Replace(nos[0], @"[^0-9]+", "");
  110. var sec = Regex.Replace(nos[1], @"[^0-9]+", "");
  111. var Station = Int32.Parse(first) < Int32.Parse(sec)
  112. ? item.WearhouseNo + item.NS + nos[0].Substring(1)
  113. : item.WearhouseNo + item.NS + nos[1].Substring(1);
  114. if (!Machinfos.Any(o => o.MachNo.ToLower() == nos[0].ToLower().Trim()))
  115. {
  116. BillMachinfolists.Add(new BillMachinfo()
  117. {
  118. WarehouseId = warehouse == null ? 0 : warehouse.Id,
  119. WarehouseCode = warehouse == null ? "" : warehouse.Code,
  120. WareAreaId = warehouseareaid ?? 0,
  121. Name = "",
  122. GrpCode = "",
  123. Direction = "",
  124. BillCode = "",
  125. WorkOrder = "",
  126. SetGrpCode = "",
  127. ProBillCode = "",
  128. ProWorkOrder = "",
  129. ProSetGrpCode = "",
  130. MacTypeCode = "",
  131. MacTypeName = "",
  132. MachNo = nos[0],
  133. AgvPoint = item.AgvPoint,
  134. Station = Station,
  135. WareDirect = item.NS,
  136. IsStop = 0,
  137. AddWho = loginName,
  138. EditWho = loginName,
  139. AddTime = DateTime.Now,
  140. EditTime = DateTime.Now,
  141. }) ;
  142. }
  143. if (!Machinfos.Any(o => o.MachNo.ToLower() == nos[1].ToLower().Trim()))
  144. {
  145. BillMachinfolists.Add(new BillMachinfo()
  146. {
  147. WarehouseId = warehouse == null ? 0 : warehouse.Id,
  148. WarehouseCode = warehouse == null ? "" : warehouse.Code,
  149. WareAreaId = warehouseareaid ?? 0,
  150. AgvPoint = item.AgvPoint,
  151. Name = "",
  152. GrpCode = "",
  153. Direction = "",
  154. BillCode = "",
  155. WorkOrder = "",
  156. SetGrpCode = "",
  157. ProBillCode = "",
  158. ProWorkOrder = "",
  159. ProSetGrpCode = "",
  160. MacTypeCode = "",
  161. MacTypeName = "",
  162. MachNo = nos[1],
  163. Station = Station,
  164. WareDirect = item.NS,
  165. IsStop = 0,
  166. AddWho = loginName,
  167. EditWho = loginName,
  168. AddTime = DateTime.Now,
  169. EditTime = DateTime.Now,
  170. });
  171. }
  172. }
  173. else
  174. {
  175. if (!Machinfos.Any(o => o.MachNo.ToLower() == item.No.ToLower().Trim()))
  176. {
  177. BillMachinfolists.Add(new BillMachinfo()
  178. {
  179. WarehouseId = warehouse == null ? 0 : warehouse.Id,
  180. WarehouseCode = warehouse == null ? "" : warehouse.Code,
  181. WareAreaId = warehouseareaid ?? 0,
  182. MachNo = item.No,
  183. Name = "",
  184. GrpCode="",
  185. Direction="",
  186. BillCode="",
  187. WorkOrder = "",
  188. SetGrpCode = "",
  189. ProBillCode = "",
  190. ProWorkOrder = "",
  191. ProSetGrpCode = "",
  192. MacTypeCode = "",
  193. MacTypeName = "",
  194. AgvPoint = item.AgvPoint,
  195. Station = item.WearhouseNo + item.NS + item.No.Substring(1),
  196. WareDirect = item.NS,
  197. IsStop = 0,
  198. AddWho = loginName,
  199. EditWho = loginName,
  200. AddTime = DateTime.Now,
  201. EditTime = DateTime.Now,
  202. });
  203. }
  204. }
  205. }
  206. if (BillMachinfolists.Any())
  207. {
  208. _billMachinfoRepository.Insert(BillMachinfolists);
  209. }
  210. json.Type = 2;
  211. json.Message = "导入成功";
  212. }
  213. catch (Exception ex)
  214. {
  215. json.Type = 0;
  216. json.Message = ex.Message;
  217. }
  218. return json;
  219. }
  220. }
  221. public class MachInfoExcelModel
  222. {
  223. [Column("AGV地标码")]
  224. public string AgvPoint { get; set; }
  225. [Column("帘线机台号")]
  226. public string No { get; set; }
  227. [Column("分拣库")]
  228. public string WearhouseNo { get; set; }
  229. [Column("南北侧")]
  230. public string NS { get; set; }
  231. [Column("6位地码")]
  232. public string Six { get; set; }
  233. }
  234. }