ProductNormController.cs 17 KB

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