readme.md.txt 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ![](./scrs/111.png)
  2. ## program c#
  3. ```
  4. using MySqlConnector;
  5. using Dapper;
  6. var builder = WebApplication.CreateBuilder(args);
  7. var app = builder.Build();
  8. var dbDataProvider = new DBDataProvider();
  9. app.MapGet("/product", (int? pageNum) =>
  10. {
  11. return dbDataProvider.GetProducts(pageNum ?? 1);
  12. });
  13. app.MapPost("/product", (Product newProduct) =>
  14. {
  15. dbDataProvider.saveProduct(newProduct);
  16. });
  17. app.MapDelete("/product/{id:int}", (int id) =>
  18. {
  19. dbDataProvider.removeProduct(id);
  20. });
  21. app.MapPut("/product", (Product editProduct) =>
  22. {
  23. dbDataProvider.saveProduct(editProduct);
  24. });
  25. app.MapGet("/productCount", () =>
  26. {
  27. return dbDataProvider.GetProductCount();
  28. });
  29. app.MapPut("/minCostForAgent/{minCost:decimal}", (decimal minCost, int[] ids) =>
  30. {
  31. dbDataProvider.setMinCostForAgent(minCost, ids);
  32. });
  33. app.MapGet("/articleCheck/{article}/{ID:int}", (string article, int ID) =>
  34. {
  35. return dbDataProvider.getArticleCheck(article, ID);
  36. });
  37. app.MapGet("/saleCount/{ID:int}", (int ID) =>
  38. {
  39. return dbDataProvider.saleCount(ID);
  40. });
  41. app.MapDelete("/productMaterial/{ID:int}", (int ID) =>
  42. {
  43. dbDataProvider.removeProductMaterial(ID);
  44. });
  45. app.MapDelete("/productCostHistory/{ID:int}", (int ID) =>
  46. {
  47. dbDataProvider.removeProductCostHistory(ID);
  48. });
  49. app.MapGet("/productMaterial/{ID:int}", (int ID) =>
  50. {
  51. return dbDataProvider.getProductMaterials(ID);
  52. });
  53. app.MapGet("/material", () =>
  54. {
  55. return dbDataProvider.getMaterials();
  56. });
  57. app.MapPost("/productMaterial", (ProductMaterial material) =>
  58. {
  59. dbDataProvider.addProductMaterial(material);
  60. });
  61. app.Run();
  62. ```
  63. ## dbddataprovb
  64. ```
  65. public class DBDataProvider
  66. {
  67. public string connectionString = "Server=kolei.ru; User ID=aleuhin; Password=101005; Database=aleuhin";
  68. public List<Product> GetProducts(int pageNum)
  69. {
  70. using (MySqlConnection db = new MySqlConnection(connectionString))
  71. {
  72. var builder = new SqlBuilder();
  73. // добавление сортировки в запрос
  74. if (orderCondition.Length > 0)
  75. builder.OrderBy(orderCondition);
  76. // добавление фильтрации в запрос
  77. if (filters.Count > 0)
  78. {
  79. foreach (var item in filters)
  80. builder.Where(item.Key, item.Value);
  81. }
  82. // составление шаблона запроса
  83. var template = builder.AddTemplate(
  84. "SELECT * FROM aleuhin.12 /**where**/ /**orderby**/ LIMIT @pageLen OFFSET @offset",
  85. new
  86. {
  87. pageLen = 20,
  88. offset = (pageNum - 1) * 20
  89. }
  90. );
  91. // выполнение запроса
  92. return db.Query<Product>
  93. (
  94. template.RawSql,
  95. template.Parameters).ToList();
  96. }
  97. }
  98. private string orderCondition = "";
  99. public void setOrder(string condition)
  100. {
  101. orderCondition = condition;
  102. }
  103. private Dictionary<string, object> filters = new Dictionary<string, object>();
  104. public void addFilter(string name, object value)
  105. {
  106. filters.Add(name, value);
  107. }
  108. public void clearFilter()
  109. {
  110. filters.Clear();
  111. }
  112. public void saveProduct(Product product)
  113. {
  114. using (MySqlConnection db = new MySqlConnection(connectionString))
  115. {
  116. if (product.ID == 0)
  117. {
  118. db.Execute("INSERT INTO Product" +
  119. "(`Title`, `ProductTypeID`,`ArticleNumber`,`Description`,`Image`," +
  120. "`ProductionPersonCount`,`ProductionWorkshopNumber`,`MinCostForAgent`) " +
  121. "VALUES (@Title,@ProductTypeID,@ArticleNumber,@Description,@Image," +
  122. "@ProductionPersonCount,@ProductionWorkshopNumber,@MinCostForAgent);", product);
  123. }
  124. else
  125. {
  126. db.Execute("UPDATE Product SET Title=@Title, ProductTypeID=@ProductTypeID, " +
  127. "ArticleNumber=@ArticleNumber, Description=@Description, " +
  128. "Image=@Image, ProductionPersonCount=@ProductionPersonCount, " +
  129. "ProductionWorkshopNumber=@ProductionWorkshopNumber, " +
  130. "MinCostForAgent=@MinCostForAgent " +
  131. "WHERE ID=@ID", product);
  132. }
  133. }
  134. }
  135. public void removeProduct(int ID)
  136. {
  137. using (MySqlConnection db = new MySqlConnection(connectionString))
  138. {
  139. db.Execute("DELETE FROM Product WHERE ID = @ID", new { ID = ID });
  140. }
  141. }
  142. public int GetProductCount()
  143. {
  144. using (MySqlConnection db = new MySqlConnection(connectionString))
  145. {
  146. var builder = new SqlBuilder();
  147. if (filters.Count > 0)
  148. {
  149. foreach (var item in filters)
  150. builder.Where(item.Key, item.Value);
  151. }
  152. var template = builder.AddTemplate(
  153. "SELECT count(*) FROM 12 /**where**/");
  154. return db.QuerySingle<int>(
  155. template.RawSql,
  156. template.Parameters);
  157. }
  158. }
  159. public void setMinCostForAgent(decimal minCostForAgent, int[] ids)
  160. {
  161. using (MySqlConnection db = new MySqlConnection(connectionString))
  162. {
  163. db.Execute("UPDATE Product SET MinCostForAgent=@newCost WHERE ID in @idList", new
  164. {
  165. newCost = minCostForAgent,
  166. idList = ids
  167. });
  168. }
  169. }
  170. public int getArticleCheck(string article, int ID)
  171. {
  172. using (MySqlConnection db = new MySqlConnection(connectionString))
  173. {
  174. return db.QuerySingle<int>("SELECT count(*) FROM Product " +
  175. "WHERE ArticleNumber=@article AND ID != @ID;", new
  176. {
  177. article = article,
  178. ID = ID
  179. });
  180. }
  181. }
  182. public int saleCount(int ID)
  183. {
  184. using (MySqlConnection db = new MySqlConnection(connectionString))
  185. {
  186. if (ID != 0)
  187. {
  188. return db.QuerySingle<int>("SELECT count(*) FROM ProductSale WHERE ProductID=@ID", new { ID = ID });
  189. }
  190. return 0;
  191. }
  192. }
  193. public void removeProductMaterial(int ID)
  194. {
  195. using (MySqlConnection db = new MySqlConnection(connectionString))
  196. {
  197. db.Execute("DELETE FROM ProductMaterial WHERE ProductID=@ID", new { ID = ID });
  198. }
  199. }
  200. public void removeProductCostHistory(int ID)
  201. {
  202. using (MySqlConnection db = new MySqlConnection(connectionString))
  203. {
  204. db.Execute("DELETE FROM ProductCostHistory WHERE ProductID = @ID", new { ID = ID });
  205. }
  206. }
  207. public List<ProductMaterial> getProductMaterials(int ID)
  208. {
  209. using (MySqlConnection db = new MySqlConnection(connectionString))
  210. {
  211. return db.Query<ProductMaterial>("SELECT pm.*,m.Title " +
  212. "FROM ProductMaterial as pm,Material as m " +
  213. "WHERE pm.ProductID = @ID AND m.ID = pm.MaterialID", new { ID = ID }).ToList();
  214. }
  215. }
  216. public void deleteProductMaterial(ProductMaterial material)
  217. {
  218. using (MySqlConnection db = new MySqlConnection(connectionString))
  219. {
  220. db.Execute("DELETE FROM ProductMaterial WHERE ProductID = @ProductId AND MaterialID = @MaterialId", material);
  221. }
  222. }
  223. public List<Material> getMaterials()
  224. {
  225. using (MySqlConnection db = new MySqlConnection(connectionString))
  226. {
  227. return db.Query<Material>("SELECT ID,Title FROM aleuhin.Material").ToList();
  228. }
  229. }
  230. public void addProductMaterial(ProductMaterial material)
  231. {
  232. using (MySqlConnection db = new MySqlConnection(connectionString))
  233. {
  234. db.Execute("INSERT INTO ProductMaterial (`ProductID`,`MaterialID`,`Count`) VALUES (@ProductId,@MaterialId,@Count)", material);
  235. }
  236. }
  237. }
  238. ```
  239. ## classes
  240. ```
  241. public class Product
  242. {
  243. public int ID { get; set; }
  244. public string Title { get; set; }
  245. public int ProductTypeID { get; set; }
  246. public string ProductTypeTitle { get; set; }
  247. public string ArticleNumber { get; set; }
  248. public string Description { get; set; }
  249. public string Image { get; set; }
  250. public int ProductionPersonCount { get; set; }
  251. public int ProductionWorkshopNumber { get; set; }
  252. public decimal MinCostForAgent { get; set; }
  253. public int MaterialCost { get; set; }
  254. public string MaterialString { get; set; }
  255. public int? LastMonthSaleQuantity { get; set; }
  256. }
  257. public class ProductMaterial
  258. {
  259. public int ProductId { get; set; }
  260. public int MaterialId { get; set; }
  261. public double Count { get; set; }
  262. }
  263. public class Material
  264. {
  265. public int ID { get; set; }
  266. public string Title { get; set; }
  267. }
  268. ```