ProductController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using Ant.Data;
  2. using Ant.ORM;
  3. using Ant.SuperSocket.Common;
  4. using Ant.Service.Common;
  5. using Ant.Service.Common.Enums;
  6. using Central.Control.Domain;
  7. using MES.Production.Entity;
  8. using MES.Production.Entity.Entity;
  9. using MES.Production.Entity.Enum;
  10. using MES.Production.Entity.Extensions;
  11. using Ant.Service.Mongodb;
  12. using MES.Production.Service.IService;
  13. using MES.Production.Service.ServiceImp;
  14. using Ant.Service.Utility;
  15. using ChangFa.Machinery.WebPage.Areas.SysManage.Models;
  16. using ChangFa.Machinery.WebPage.Controllers;
  17. using MongoDB.Driver.Builders;
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Data;
  21. using System.IO;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Web;
  25. using System.Web.Mvc;
  26. using JsonHelper = Ant.Service.Common.JsonHelper;
  27. namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
  28. {
  29. public class ProductController : BaseController
  30. {
  31. [UserAuthorizeAttribute(ModuleAlias = "productList", OperaAction = "View")]
  32. public ActionResult ProductList()
  33. {
  34. try
  35. {
  36. PageInfo pageInfo = new PageInfo(base.page, base.pagesize, 0, JsonConverter.JsonClass(new List<EntYW_Bicycle>()));
  37. #region 处理查询参数
  38. var nameOrCode = Request.QueryString["nameOrCode"];
  39. int saleStatus = -1;
  40. if (!string.IsNullOrWhiteSpace(Request.QueryString["saleStatus"]))
  41. {
  42. saleStatus = Convert.ToInt32(Request.QueryString["saleStatus"]);
  43. }
  44. #endregion
  45. #region 加载列表
  46. using (AntORM orm = new AntORM())
  47. {
  48. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  49. RequestModel res = new RequestModel();
  50. res.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取运动员列表" };
  51. int pageno = (base.page - 1) * base.pagesize;
  52. var productQuery = orm.Queryable<YW_Product>().Where(p => p.IsDelete == 0);
  53. productQuery = productQuery.WhereIf(!string.IsNullOrWhiteSpace(nameOrCode), p => p.Name.Contains(nameOrCode));
  54. productQuery = productQuery.WhereIf(saleStatus >= 0, p => p.Sale == (SaleEnum)saleStatus);
  55. var reslut = productQuery.OrderByDesc(p => p.ModifyDT).Skip(pageno).Top(pagesize).ToList(res);
  56. if (reslut.IsSuccess)
  57. {
  58. reslut.RecordNum = productQuery.Count();
  59. pageInfo = new PageInfo(base.page, base.pagesize, reslut.RecordNum, JsonConverter.JsonClass(reslut.ResultModel));
  60. }
  61. }
  62. ViewBag.Search = base.keywords;
  63. ViewData["nameOrCode"] = nameOrCode;
  64. ViewData["saleStatus"] = saleStatus;
  65. return View(pageInfo);
  66. #endregion
  67. }
  68. catch (Exception e)
  69. {
  70. WriteLog(enumOperator.Select, "获取裁判员列表:", e);
  71. throw e.InnerException;
  72. }
  73. }
  74. /// <summary>
  75. /// 删除
  76. /// </summary>
  77. /// <param name="idList"></param>
  78. /// <returns></returns>
  79. [UserAuthorize(ModuleAlias = "productList", OperaAction = "Remove")]
  80. public ActionResult Delete(string idList)
  81. {
  82. JsonHelper json = new JsonHelper() { Status = "n", Msg = "删除商品成功" };
  83. try
  84. {
  85. //是否为空
  86. if (string.IsNullOrEmpty(idList)) { json.Msg = "未找到要删除的商品"; return Json(json); }
  87. string[] id = idList.Trim(',').Split(',');
  88. using (AntORM orm = new AntORM())
  89. {
  90. RequestModel request = new RequestModel
  91. {
  92. newSt = new SqlNote() { Author = "smj", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "删除商品" }
  93. };
  94. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  95. List<YW_Product> olds = new List<YW_Product>();
  96. for (int i = 0; i < id.Length; i++)
  97. {
  98. var userId = id[i];
  99. request.Oid = userId;
  100. var oldResult = orm.GetEntity<YW_Product>(request);
  101. YW_Product old = oldResult?.ResultModel;
  102. if (old != null)
  103. {
  104. if (old.Sale == SaleEnum.On)
  105. {
  106. json.Msg = "已上架的商品不能修改";
  107. return Json(json);
  108. }
  109. olds.Add(old);
  110. }
  111. }
  112. olds.ForEach(old => {
  113. old.IsDelete = 1;
  114. old.ModifyBY = CurrentUser.Id.ToString();
  115. old.ModifyDT = DateTime.Now;
  116. request.Oid = string.Empty;
  117. var updateResult = orm.UpdateByLambda(old,p=>p.Id == old.Id, request);
  118. if (updateResult.IsSuccess)
  119. {
  120. json.Status = "y";
  121. WriteLog(enumOperator.Remove, json.Msg, enumLog4net.WARN);
  122. }
  123. });
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. json.Msg = "删除商品发生内部错误!";
  129. WriteLog(enumOperator.Remove, "删除商品:", e);
  130. }
  131. return Json(json);
  132. }
  133. [UserAuthorize(ModuleAlias = "productList", OperaAction = "Add,Edit")]
  134. [ValidateInput(false)]
  135. [HttpPost]
  136. public ActionResult Save(YW_Product entity)
  137. {
  138. JsonHelper json = new JsonHelper() { Msg = "保存商品成功", Status = "n", ReUrl = "/Sys/Product/ProductList" };
  139. try
  140. {
  141. using (AntORM orm = new AntORM())
  142. {
  143. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  144. RequestModel request = new RequestModel();
  145. request.newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "保存商品" };
  146. entity.Name = entity.Name.Trim();
  147. entity.Code = entity.Code.Trim();
  148. entity.Name = entity.Name.Trim();
  149. entity.Sale = SaleEnum.Off;
  150. entity.Info = entity.Info?.Trim() ?? string.Empty;
  151. entity.IsDelete = 0;
  152. entity.Img = Request.Form["hidfilename"];
  153. if (!string.IsNullOrEmpty(entity.Id))
  154. {
  155. // 修改
  156. var resultProductCount = orm.Queryable<YW_Product>().Where(p => p.Code == entity.Code && p.IsDelete == 0 && p.Id != entity.Id).Count();
  157. if (resultProductCount > 0)
  158. {
  159. json.Status = "n";
  160. json.Msg = "商品编码不能重复!";
  161. return Json(json);
  162. }
  163. var onSaleCount = orm.Queryable<YW_Product>().Where(p => p.Id == entity.Id && p.Sale == SaleEnum.On).Count();
  164. if (onSaleCount > 0)
  165. {
  166. json.Status = "n";
  167. json.Msg = "已上架的商品不能修改!";
  168. return Json(json);
  169. }
  170. request.Oid = entity.Id;
  171. entity.ModifyBY = CurrentUser.Id.ToString();
  172. entity.ModifyDT = DateTime.Now;
  173. var reslut = orm.UpdateById(entity, request);
  174. if (reslut.IsSuccess)
  175. {
  176. json.Status = "y";
  177. }
  178. else
  179. {
  180. json.Msg = "保存商品失败";
  181. }
  182. }
  183. else
  184. {
  185. // 删除
  186. var resultSportCount = orm.Queryable<YW_Product>().Where(p => p.Code == entity.Code && p.IsDelete == 0).Count();
  187. if (resultSportCount > 0)
  188. {
  189. json.Status = "n";
  190. json.Msg = "商品编码不能重复!";
  191. return Json(json);
  192. }
  193. entity.Id = IdGenerator.NewId();
  194. entity.CreateDT = DateTime.Now;
  195. entity.CreateBY = CurrentUser.Id.ToString();
  196. entity.ModifyDT = DateTime.Now;
  197. var reslut = orm.Save(entity, request);
  198. if (reslut.IsSuccess)
  199. {
  200. json.Status = "y";
  201. }
  202. else
  203. {
  204. json.Msg = "保存商品失败";
  205. }
  206. }
  207. }
  208. }
  209. catch (Exception ex)
  210. {
  211. json.Msg = "保存商品发生内部错误!";
  212. WriteLog(enumOperator.None, "保存商品员:", ex);
  213. }
  214. return Json(json);
  215. }
  216. /// <summary>
  217. /// 查询详情
  218. /// </summary>
  219. /// <param name="id"></param>
  220. /// <returns></returns>
  221. [UserAuthorize(ModuleAlias = "productList", OperaAction = "Detail")]
  222. public ActionResult Detail(string id)
  223. {
  224. YW_Product mod = new YW_Product();
  225. if (id != null)
  226. {
  227. using (AntORM orm = new AntORM())
  228. {
  229. try
  230. {
  231. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  232. RequestModel request = new RequestModel
  233. {
  234. newSt = new SqlNote() { Author = "smj", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "查询单个商品" },
  235. Oid = id.ToString()
  236. };
  237. var reslut = orm.GetEntity<YW_Product>(request);
  238. if (reslut.IsSuccess)
  239. {
  240. mod = reslut.ResultModel;
  241. }
  242. }
  243. catch (Exception e)
  244. {
  245. }
  246. }
  247. }
  248. return View(mod);
  249. }
  250. /// <summary>
  251. /// 上架
  252. /// </summary>
  253. /// <param name="idList"></param>
  254. /// <returns></returns>
  255. [UserAuthorize(ModuleAlias = "productList", OperaAction = "OnSale")]
  256. public ActionResult OnSale(string idList)
  257. {
  258. return OnOffSale(SaleEnum.On, idList);
  259. }
  260. /// <summary>
  261. /// 下架
  262. /// </summary>
  263. /// <param name="idList"></param>
  264. /// <returns></returns>
  265. [UserAuthorize(ModuleAlias = "productList", OperaAction = "OffSale")]
  266. public ActionResult OffSale(string idList)
  267. {
  268. return OnOffSale(SaleEnum.Off, idList);
  269. }
  270. /// <summary>
  271. /// 上下架功能
  272. /// </summary>
  273. /// <param name="idList"></param>
  274. /// <returns></returns>
  275. private ActionResult OnOffSale(SaleEnum saleEnum, string idList)
  276. {
  277. string msg = "上架";
  278. if (saleEnum == SaleEnum.Off)
  279. {
  280. msg = "下架";
  281. }
  282. JsonHelper json = new JsonHelper() { Status = "n", Msg = $"{msg}商品成功" };
  283. try
  284. {
  285. //是否为空
  286. if (string.IsNullOrEmpty(idList)) { json.Msg = $"未找到要{msg}的商品"; return Json(json); }
  287. string[] id = idList.Trim(',').Split(',');
  288. using (AntORM orm = new AntORM())
  289. {
  290. RequestModel request = new RequestModel
  291. {
  292. newSt = new SqlNote() { Author = "smj", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = $"{msg}商品" }
  293. };
  294. orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
  295. List<YW_Product> olds = new List<YW_Product>();
  296. for (int i = 0; i < id.Length; i++)
  297. {
  298. var userId = id[i];
  299. request.Oid = userId;
  300. var oldResult = orm.GetEntity<YW_Product>(request);
  301. YW_Product old = oldResult?.ResultModel;
  302. if (old != null)
  303. {
  304. olds.Add(old);
  305. }
  306. }
  307. olds.ForEach(old =>
  308. {
  309. old.Sale = saleEnum;
  310. old.ModifyBY = CurrentUser.Id.ToString();
  311. old.ModifyDT = DateTime.Now;
  312. request.Oid = string.Empty;
  313. var updateResult = orm.UpdateByLambda(old, p => p.Id == old.Id, request);
  314. if (updateResult.IsSuccess)
  315. {
  316. json.Status = "y";
  317. WriteLog(enumOperator.Edit, json.Msg, enumLog4net.WARN);
  318. }
  319. });
  320. }
  321. }
  322. catch (Exception e)
  323. {
  324. json.Msg = "删除裁判发生内部错误!";
  325. WriteLog(enumOperator.Remove, "删除教练:", e);
  326. }
  327. return Json(json);
  328. }
  329. }
  330. }