Replacement for JDIS Importer
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.

199 rivejä
11 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. using jdis_import.RESTObjects;
  8. namespace jdis_import
  9. {
  10. public class CSVObject
  11. {
  12. public string CustomerNumber { get; set; }
  13. public string CustomerName { get; set; }
  14. public string Address { get; set; }
  15. public string Address2 { get; set; }
  16. public string City { get; set; }
  17. public string State { get; set; }
  18. public string ZipCode { get; set; }
  19. public string EquipmentNumber { get; set; }
  20. public string SerialNumber { get; set; }
  21. public string Model { get; set; }
  22. public string MeterReading { get; set; }
  23. public string Year { get; set; }
  24. public string FamilyCode { get; set; }
  25. public string FamilyDescription { get; set; }
  26. public string Make { get; set; }
  27. public string LastServiceDate { get; set; }
  28. public string DBSWorkOrderNumber { get; set; }
  29. public string DateActualStart { get; set; }
  30. public string CodeAndDescriptionOriginal { get; set; }
  31. public string CodeAndDescription { get; set; }
  32. public string Store { get; set; }
  33. public string StoreDescription { get; set; }
  34. public string ContractTypeCode { get; set; }
  35. public string JobSiteContactName { get; set; }
  36. public string JobSiteContactPhone { get; set; }
  37. public string PriorityCode { get; set; }
  38. public string SpecialInstructions { get; set; }
  39. public string PartNumber { get; set; }
  40. public string PartNumberDescription { get; set; }
  41. public string ReferenceDocNumber { get; set; }
  42. public string Quantity { get; set; }
  43. public string SegmentNumber { get; set; }
  44. public string ServiceTech { get; set; }
  45. public string TechName { get; set; }
  46. public string InspectionType { get; set; }
  47. public string ScheduledStartDate { get; set; }
  48. public string SQLStatement { get; set; }
  49. public static int MachineExists(string serialnumber, string connStr)
  50. {
  51. //int? id = 0;
  52. Dictionary<string, string> search = new Dictionary<string, string>();
  53. search.Add("search", serialnumber);
  54. RESTService svc = new RESTService();
  55. List<Equipment> machines = svc.RestGet<Equipment>("Equipments.svc", search);
  56. if (machines.Count > 0) return machines.LastOrDefault().ID;
  57. else return 0;
  58. // string sql = string.Format("Select ID from equipment where serialnumber = '{0}'", serialnumber);
  59. // using (SqlConnection conn = new SqlConnection(connStr))
  60. // {
  61. // conn.Open();
  62. // SqlCommand cmd = conn.CreateCommand();
  63. // cmd.CommandText = sql;
  64. // cmd.CommandType = CommandType.Text;
  65. // id = Convert.ToInt32(cmd.ExecuteScalar());
  66. // if (id > 0)
  67. // {
  68. // cmd.CommandText = string.Format("Select ModelID from Equipment where ID = {0}", id);
  69. // id = Convert.ToInt32(cmd.ExecuteScalar());
  70. // }
  71. // }
  72. // return Convert.ToInt32(id);
  73. }
  74. public void Validate()
  75. {
  76. if (this.CustomerNumber.Length > 50) throw new ApplicationException(string.Format("Customer Number too long. Text: {0}", this.CustomerNumber));
  77. if (this.CustomerName.Length > 50) throw new ApplicationException("Customer Name too long.");
  78. if (this.Address.Length > 50) throw new ApplicationException("Address too long.");
  79. if (this.Address2.Length > 50) throw new ApplicationException("Address2 too long.");
  80. if (this.City.Length > 50) throw new ApplicationException("City too long.");
  81. if (this.State.Length > 3) throw new ApplicationException("State too long.");
  82. if (this.ZipCode.Length > 50) throw new ApplicationException("ZipCode too long.");
  83. if (this.EquipmentNumber.Length > 50) throw new ApplicationException("Equipment Number too long.");
  84. if (this.SerialNumber.Length > 50) throw new ApplicationException("Serial Number too long.");
  85. if (this.Model.Length > 50) throw new ApplicationException("Model too long.");
  86. if (this.MeterReading.Length > 50) throw new ApplicationException("Meter Reading too long.");
  87. if (this.Year.Length > 50) throw new ApplicationException("Year too long.");
  88. if (this.FamilyCode.Length > 50) throw new ApplicationException("Family Code too long.");
  89. if (this.FamilyDescription.Length > 50) throw new ApplicationException("Family Description too long.");
  90. if (this.Make.Length > 50) throw new ApplicationException("Make too long.");
  91. if (this.LastServiceDate.Length > 50) throw new ApplicationException("Last Service Date too long.");
  92. if (this.DBSWorkOrderNumber.Length > 50) throw new ApplicationException("DBS Work Order Number too long.");
  93. if (this.DateActualStart.Length > 50) throw new ApplicationException("Date Actual Start too long.");
  94. if (this.CodeAndDescriptionOriginal.Length > 50) throw new ApplicationException("Code and Description Original too long.");
  95. if (this.Store.Length > 50) throw new ApplicationException("Store too long.");
  96. if (this.StoreDescription.Length > 50) throw new ApplicationException("Store Description too long.");
  97. if (this.ContractTypeCode.Length > 50) throw new ApplicationException("Contact Type Code too long.");
  98. if (this.JobSiteContactName.Length > 50) throw new ApplicationException("Job Site Contact Name too long.");
  99. if (this.JobSiteContactPhone.Length > 50) throw new ApplicationException("job Site contact Phone too long.");
  100. if (this.PriorityCode.Length > 50) throw new ApplicationException("Priority Code too long.");
  101. if (this.PartNumber.Length > 50) throw new ApplicationException("Part Number too long.");
  102. if (this.PartNumberDescription.Length > 50) throw new ApplicationException("Part Number Description too long.");
  103. if (this.ReferenceDocNumber.Length > 50) throw new ApplicationException("Reference Document Number too long.");
  104. if (this.Quantity.Length > 50) throw new ApplicationException("Quantity too long.");
  105. if (this.SegmentNumber.Length > 50) throw new ApplicationException("Segment too long.");
  106. if (this.ServiceTech.Length > 50) throw new ApplicationException("Service Tech too long.");
  107. if (this.TechName.Length > 50) throw new ApplicationException("Tech Name too long.");
  108. if (this.InspectionType.Length > 50) throw new ApplicationException("Inspection Type too long.");
  109. }
  110. public static string WriteToDB(List<CSVObject> items, SqlConnection conn)
  111. {
  112. SqlCommand cmd = conn.CreateCommand();
  113. cmd.Connection = conn;
  114. cmd.CommandType = CommandType.Text;
  115. string insertcmd = @"Insert Into Results_Brandt(CustomerNumber, CustomerName, [Address], Address2, City, [State], ZipCode, EquipmentNumber, SerialNumber, [Model], MeterReading, [Year], FamilyCode, FamilyDescription, Make, LastServiceDate, DBSWorkOrderNumber, DateActualStart, CodeAndDescriptionOriginal, CodeAndDescription, Store, StoreDescription, ContractTypeCode, JobSiteContactName, JobSiteContactPhone, PriorityCode, SpecialInstructions, PartNumber, PartNumberDescription, ReferenceDocNumber, Quantity, SegmentNumber, ServiceTech, TechName, InspectionType) Values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}','{27}','{28}','{29}','{30}','{31}','{32}','{33}', '{34}')";
  116. List<string> msg = new List<string>();
  117. string msgs = string.Empty;
  118. foreach (var item in items)
  119. {
  120. cmd.CommandText = string.Format(insertcmd,
  121. item.CustomerNumber,
  122. item.CustomerName,
  123. item.Address,
  124. item.Address2,
  125. item.City,
  126. item.State,
  127. item.ZipCode,
  128. item.EquipmentNumber,
  129. item.SerialNumber,
  130. item.Model,
  131. item.MeterReading,
  132. item.Year,
  133. item.FamilyCode,
  134. item.FamilyDescription,
  135. item.Make,
  136. item.LastServiceDate,
  137. item.DBSWorkOrderNumber,
  138. item.ScheduledStartDate,
  139. item.CodeAndDescriptionOriginal,
  140. item.CodeAndDescription,
  141. item.Store,
  142. item.StoreDescription,
  143. item.ContractTypeCode,
  144. item.JobSiteContactName,
  145. item.JobSiteContactPhone,
  146. item.PriorityCode,
  147. string.IsNullOrEmpty(item.SpecialInstructions) ? string.Empty : item.SpecialInstructions,
  148. item.PartNumber,
  149. item.PartNumberDescription,
  150. item.ReferenceDocNumber,
  151. item.Quantity,
  152. item.SegmentNumber,
  153. item.ServiceTech,
  154. item.TechName,
  155. item.InspectionType);
  156. try
  157. {
  158. cmd.ExecuteNonQuery();
  159. cmd.Dispose();
  160. }
  161. catch (Exception ex)
  162. {
  163. item.SQLStatement = cmd.CommandText;
  164. msg.Add(ex.Message);
  165. }
  166. }
  167. if (msg.Count > 0) return string.Join("\r\n", msg);
  168. else return string.Empty;
  169. }
  170. public static void WriteCSV<T>(IEnumerable<T> items, string path)
  171. {
  172. if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
  173. Type itemType = typeof(T);
  174. var props = itemType.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
  175. using (var writer = new System.IO.StreamWriter(path))
  176. {
  177. string[] clmns = props.Select(p => p.Name).ToArray();
  178. string header = string.Join(",", clmns);
  179. writer.WriteLine(header);
  180. foreach (var item in items)
  181. {
  182. List<string> vals = new List<string>();
  183. foreach (string prop in clmns)
  184. vals.Add(item.GetType().GetProperty(prop).GetValue(item, null).ToString());
  185. string valueline = string.Join(",", vals);
  186. writer.WriteLine(valueline);
  187. }
  188. }
  189. }
  190. }
  191. }