ProductNormController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * 商品规格
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using MySystem.Models;
  15. using Library;
  16. using LitJson;
  17. using MySystemLib;
  18. namespace MySystem.Areas.Admin.Controllers
  19. {
  20. [Area("Admin")]
  21. [Route("Admin/[controller]/[action]")]
  22. public class ProductNormController : BaseController
  23. {
  24. public ProductNormController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  27. }
  28. #region 商品规格列表
  29. /// <summary>
  30. /// 根据条件查询商品规格列表
  31. /// </summary>
  32. /// <returns></returns>
  33. public IActionResult Index(ProductNorm data, string right)
  34. {
  35. ViewBag.RightInfo = RightInfo;
  36. ViewBag.right = right;
  37. string Condition = "";
  38. Condition += "ColName:\"" + data.ColName + "\",";
  39. if (!string.IsNullOrEmpty(Condition))
  40. {
  41. Condition = Condition.TrimEnd(',');
  42. Condition = ", where: {" + Condition + "}";
  43. }
  44. ViewBag.Condition = Condition;
  45. return View();
  46. }
  47. #endregion
  48. #region 根据条件查询商品规格列表
  49. /// <summary>
  50. /// 商品规格列表
  51. /// </summary>
  52. /// <returns></returns>
  53. public JsonResult IndexData(ProductNorm data, int page = 1, int limit = 30)
  54. {
  55. Dictionary<string, string> Fields = new Dictionary<string, string>();
  56. Fields.Add("ColName", "2"); //名称
  57. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("ProductNorm", Fields, "Id desc", "False", page, limit);
  58. return Json(obj);
  59. }
  60. #endregion
  61. #region 增加商品规格
  62. /// <summary>
  63. /// 增加或修改商品规格信息
  64. /// </summary>
  65. /// <returns></returns>
  66. public IActionResult Add(string right)
  67. {
  68. ViewBag.RightInfo = RightInfo;
  69. ViewBag.right = right;
  70. return View();
  71. }
  72. #endregion
  73. #region 增加商品规格
  74. /// <summary>
  75. /// 增加或修改商品规格信息
  76. /// </summary>
  77. /// <returns></returns>
  78. [HttpPost]
  79. public string Add(ProductNorm data)
  80. {
  81. Dictionary<string, object> Fields = new Dictionary<string, object>();
  82. Fields.Add("ColName", data.ColName); //名称
  83. Fields.Add("IdList", data.IdList); //Id集合
  84. Fields.Add("Price", data.Price); //价格
  85. Fields.Add("Integral", data.Integral); //抵扣积分
  86. Fields.Add("UserPrice", data.UserPrice); //创客价
  87. Fields.Add("UserIntegral", data.UserIntegral); //创客抵扣积分
  88. Fields.Add("Stock", data.Stock); //库存
  89. Fields.Add("BuyCount", data.BuyCount); //已购买数
  90. Fields.Add("CostPrice", data.CostPrice); //成本价
  91. Fields.Add("Code", data.Code); //编码
  92. Fields.Add("StartBuyCount", data.StartBuyCount); //起购数量
  93. Fields.Add("LimitCount", data.LimitCount); //限购数量
  94. Fields.Add("SeoTitle", data.SeoTitle);
  95. Fields.Add("SeoKeyword", data.SeoKeyword);
  96. Fields.Add("SeoDescription", data.SeoDescription);
  97. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("ProductNorm", Fields, 0);
  98. AddSysLog(data.Id.ToString(), "ProductNorm", "add");
  99. db.SaveChanges();
  100. return "success";
  101. }
  102. #endregion
  103. #region 修改商品规格
  104. /// <summary>
  105. /// 增加或修改商品规格信息
  106. /// </summary>
  107. /// <returns></returns>
  108. public IActionResult Edit(string right, int Id, string MerchantId)
  109. {
  110. ViewBag.RightInfo = RightInfo;
  111. ViewBag.right = right;
  112. ViewBag.Id = Id.ToString();
  113. ViewBag.MerchantId = MerchantId;
  114. string NormJson = "[]#cut#[]";
  115. Products edit = db.Products.FirstOrDefault(m => m.Id == Id);
  116. if (edit != null)
  117. {
  118. if (!string.IsNullOrEmpty(edit.NormJson))
  119. {
  120. NormJson = edit.NormJson;
  121. }
  122. }
  123. string[] NormJsonArray = NormJson.Split(new string[] { "#cut#" }, StringSplitOptions.None);
  124. ViewBag.ItemList = NormJsonArray[0];
  125. ViewBag.DetailList = NormJsonArray[1];
  126. return View();
  127. }
  128. #endregion
  129. #region 修改商品规格
  130. /// <summary>
  131. /// 增加或修改商品规格信息
  132. /// </summary>
  133. /// <returns></returns>
  134. [HttpPost]
  135. public string EditPost(string ItemList, string DetailList, int Id, int MerchantId)
  136. {
  137. Products edit = db.Products.FirstOrDefault(m => m.Id == Id);
  138. if (edit != null)
  139. {
  140. IQueryable<ProductNormItem> items = db.ProductNormItem.Where(m => m.ProductId == Id);
  141. foreach (ProductNormItem item in items)
  142. {
  143. db.ProductNormItem.Remove(item);
  144. }
  145. db.SaveChanges();
  146. edit.NormJson = ItemList + "#cut#" + DetailList;
  147. Dictionary<string, string> ids = new Dictionary<string, string>();
  148. JsonData jsonItemObj = JsonMapper.ToObject(ItemList);
  149. for (int i = 0; i < jsonItemObj.Count; i++)
  150. {
  151. JsonData item = jsonItemObj[i];
  152. string name = item["name"].ToString();
  153. ProductNormItem add = db.ProductNormItem.FirstOrDefault(m => m.ColName == name && m.MerchantId == MerchantId && m.ProductId == Id && m.ParentId == 0);
  154. if (add == null)
  155. {
  156. add = db.ProductNormItem.Add(new ProductNormItem()
  157. {
  158. CreateDate = DateTime.Now,
  159. UpdateDate = DateTime.Now,
  160. ColName = name,
  161. MerchantId = MerchantId,
  162. ProductId = Id,
  163. }).Entity;
  164. db.SaveChanges();
  165. }
  166. JsonData subItemObj = item["vallist"];
  167. for (int j = 0; j < subItemObj.Count; j++)
  168. {
  169. JsonData subitem = subItemObj[j];
  170. string subname = subitem["name"].ToString();
  171. ProductNormItem subadd = db.ProductNormItem.FirstOrDefault(m => m.ColName == subname && m.MerchantId == MerchantId && m.ProductId == Id && m.ParentId == add.Id);
  172. if (subadd == null)
  173. {
  174. subadd = db.ProductNormItem.Add(new ProductNormItem()
  175. {
  176. CreateDate = DateTime.Now,
  177. UpdateDate = DateTime.Now,
  178. ColName = subname,
  179. MerchantId = MerchantId,
  180. ProductId = Id,
  181. ParentId = add.Id,
  182. }).Entity;
  183. db.SaveChanges();
  184. }
  185. ids.Add(subname, subadd.Id.ToString());
  186. }
  187. }
  188. string NormIds = "";
  189. JsonData jsonObj = JsonMapper.ToObject(DetailList);
  190. for (int i = 0; i < jsonObj.Count; i++)
  191. {
  192. JsonData titles = jsonObj[i]["title"];
  193. string idlist = "";
  194. string namelist = "";
  195. for (int j = 0; j < titles.Count; j++)
  196. {
  197. string name = titles[j]["name"].ToString();
  198. idlist += ids[name] + ",";
  199. namelist += name + ",";
  200. }
  201. decimal price = decimal.Parse(function.CheckNum(jsonObj[i]["price"].ToString()));
  202. decimal integral = decimal.Parse(function.CheckNum(jsonObj[i]["integral"].ToString()));
  203. decimal userprice = decimal.Parse(function.CheckNum(jsonObj[i]["userprice"].ToString()));
  204. decimal userintegral = decimal.Parse(function.CheckNum(jsonObj[i]["userintegral"].ToString()));
  205. int stock = int.Parse(function.CheckInt(jsonObj[i]["stock"].ToString()));
  206. string code = jsonObj[i]["code"].ToString();
  207. decimal costprice = decimal.Parse(function.CheckNum(jsonObj[i]["costprice"].ToString()));
  208. int startbuycount = int.Parse(function.CheckInt(jsonObj[i]["startbuycount"].ToString()));
  209. int limitcount = int.Parse(function.CheckInt(jsonObj[i]["limitcount"].ToString()));
  210. idlist = idlist.TrimEnd(',');
  211. namelist = namelist.TrimEnd(',');
  212. ProductNorm check = db.ProductNorm.FirstOrDefault(m => m.MerchantId == MerchantId && m.ProductId == Id && m.ColName == namelist);
  213. if (check == null)
  214. {
  215. check = db.ProductNorm.Add(new ProductNorm()
  216. {
  217. CreateDate = DateTime.Now,
  218. UpdateDate = DateTime.Now,
  219. ProductId = Id,
  220. MerchantId = MerchantId,
  221. ColName = namelist,
  222. IdList = idlist,
  223. Price = price,
  224. Integral = integral,
  225. UserPrice = userprice,
  226. UserIntegral = userintegral,
  227. Stock = stock,
  228. CostPrice = costprice,
  229. Code = code,
  230. StartBuyCount = startbuycount,
  231. LimitCount = limitcount,
  232. }).Entity;
  233. db.SaveChanges();
  234. AddSysLog(check.Id, "ProductNorm", "add");
  235. }
  236. else
  237. {
  238. check.Price = price;
  239. check.Integral = integral;
  240. check.UserPrice = userprice;
  241. check.UserIntegral = userintegral;
  242. check.Stock = stock;
  243. check.CostPrice = costprice;
  244. check.Code = code;
  245. check.StartBuyCount = startbuycount;
  246. check.LimitCount = limitcount;
  247. check.IdList = idlist;
  248. AddSysLog(check.Id, "ProductNorm", "update");
  249. }
  250. NormIds += check.Id + ",";
  251. }
  252. edit.NormIds = NormIds.TrimEnd(',');
  253. db.SaveChanges();
  254. //检测多余的规格数,并清除
  255. if (!string.IsNullOrEmpty(edit.NormIds))
  256. {
  257. List<int> nids = new List<int>();
  258. string[] nidlist = edit.NormIds.Split(',');
  259. foreach (string nidstring in nidlist)
  260. {
  261. nids.Add(int.Parse(nidstring));
  262. }
  263. var eidts = db.ProductNorm.Select(m => new { m.Id, m.ProductId }).Where(m => m.ProductId == Id && !nids.Contains(m.Id)).ToList();
  264. foreach (var eidt in eidts)
  265. {
  266. ProductNorm del = db.ProductNorm.FirstOrDefault(m => m.Id == eidt.Id);
  267. if (del != null)
  268. {
  269. db.ProductNorm.Remove(del);
  270. }
  271. }
  272. db.SaveChanges();
  273. }
  274. }
  275. return "success";
  276. }
  277. #endregion
  278. #region 删除商品规格信息
  279. /// <summary>
  280. /// 删除商品规格信息
  281. /// </summary>
  282. /// <returns></returns>
  283. public string Delete(string Id)
  284. {
  285. string[] idlist = Id.Split(new char[] { ',' });
  286. AddSysLog(Id, "ProductNorm", "del");
  287. foreach (string subid in idlist)
  288. {
  289. int id = int.Parse(subid);
  290. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Delete("ProductNorm", id);
  291. }
  292. db.SaveChanges();
  293. return "success";
  294. }
  295. #endregion
  296. #region 开启
  297. /// <summary>
  298. /// 开启
  299. /// </summary>
  300. /// <returns></returns>
  301. public string Open(string Id)
  302. {
  303. string[] idlist = Id.Split(new char[] { ',' });
  304. AddSysLog(Id, "ProductNorm", "open");
  305. foreach (string subid in idlist)
  306. {
  307. int id = int.Parse(subid);
  308. Dictionary<string, object> Fields = new Dictionary<string, object>();
  309. Fields.Add("Status", 1);
  310. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("ProductNorm", Fields, id);
  311. }
  312. db.SaveChanges();
  313. return "success";
  314. }
  315. #endregion
  316. #region 关闭
  317. /// <summary>
  318. /// 关闭
  319. /// </summary>
  320. /// <returns></returns>
  321. public string Close(string Id)
  322. {
  323. string[] idlist = Id.Split(new char[] { ',' });
  324. AddSysLog(Id, "ProductNorm", "close");
  325. foreach (string subid in idlist)
  326. {
  327. int id = int.Parse(subid);
  328. Dictionary<string, object> Fields = new Dictionary<string, object>();
  329. Fields.Add("Status", 0);
  330. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("ProductNorm", Fields, id);
  331. }
  332. db.SaveChanges();
  333. return "success";
  334. }
  335. #endregion
  336. #region 排序
  337. /// <summary>
  338. /// 排序
  339. /// </summary>
  340. /// <param name="Id"></param>
  341. public string Sort(int Id, int Sort)
  342. {
  343. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Sort("ProductNorm", Sort, Id);
  344. AddSysLog(Id.ToString(), "ProductNorm", "sort");
  345. return "success";
  346. }
  347. #endregion
  348. #region 导入数据
  349. /// <summary>
  350. /// 导入数据
  351. /// </summary>
  352. /// <param name="ExcelData"></param>
  353. public string Import(string ExcelData)
  354. {
  355. ExcelData = HttpUtility.UrlDecode(ExcelData);
  356. JsonData list = JsonMapper.ToObject(ExcelData);
  357. for (int i = 1; i < list.Count; i++)
  358. {
  359. JsonData dr = list[i];
  360. db.ProductNorm.Add(new ProductNorm()
  361. {
  362. CreateDate = DateTime.Now,
  363. UpdateDate = DateTime.Now,
  364. });
  365. db.SaveChanges();
  366. }
  367. AddSysLog("0", "ProductNorm", "Import");
  368. return "success";
  369. }
  370. #endregion
  371. #region 导出Excel
  372. /// <summary>
  373. /// 导出Excel
  374. /// </summary>
  375. /// <returns></returns>
  376. public JsonResult ExportExcel(ProductNorm data)
  377. {
  378. Dictionary<string, string> Fields = new Dictionary<string, string>();
  379. Fields.Add("ColName", "2"); //名称
  380. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("ProductNorm", Fields, "Id desc", "False", 1, 20000, "", "", false);
  381. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  382. Dictionary<string, object> result = new Dictionary<string, object>();
  383. result.Add("Status", "1");
  384. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  385. result.Add("Obj", diclist);
  386. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  387. result.Add("Fields", ReturnFields);
  388. AddSysLog("0", "ProductNorm", "ExportExcel");
  389. return Json(result);
  390. }
  391. #endregion
  392. public JsonResult GetTemplate(int MerchantId)
  393. {
  394. List<ProductNormTemp> list = db.ProductNormTemp.Where(m => m.MerchantId == MerchantId).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  395. return Json(list);
  396. }
  397. public JsonResult GetTemplateDetail(int Id)
  398. {
  399. ProductNormTemp detail = db.ProductNormTemp.FirstOrDefault(m => m.Id == Id) ?? new ProductNormTemp();
  400. return Json(detail);
  401. }
  402. }
  403. }