StoreHouseController.cs 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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.Data;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Microsoft.AspNetCore.Mvc;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Options;
  16. using MySystem.Models;
  17. using Library;
  18. using LitJson;
  19. using MySystemLib;
  20. namespace MySystem.Areas.Admin.Controllers
  21. {
  22. [Area("Admin")]
  23. [Route("Admin/[controller]/[action]")]
  24. public class StoreHouseController : BaseController
  25. {
  26. public StoreHouseController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  27. {
  28. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["SqlConnStr"].ToString();
  29. }
  30. #region 仓库列表
  31. /// <summary>
  32. /// 根据条件查询仓库列表
  33. /// </summary>
  34. /// <returns></returns>
  35. public IActionResult Index(StoreHouse data, string right)
  36. {
  37. ViewBag.RightInfo = RightInfo;
  38. ViewBag.right = right;
  39. return View();
  40. }
  41. #endregion
  42. #region 根据条件查询仓库列表
  43. /// <summary>
  44. /// 仓库列表
  45. /// </summary>
  46. /// <returns></returns>
  47. public JsonResult IndexData(StoreHouse data, string UserIdMakerCode, string UserIdRealName, string ManageUserIdRealName, string ManageUserIdMobile, string ManageUserIdMakerCode, string StoreStatusSelect, string CreateDateData, int page = 1, int limit = 30)
  48. {
  49. Dictionary<string, string> Fields = new Dictionary<string, string>();
  50. Fields.Add("StoreNo", "1"); //仓库编号
  51. Fields.Add("StoreName", "1"); //仓库名称
  52. Fields.Add("ManagerEmail", "1"); //管理者邮箱
  53. string condition = " and Status>-1";
  54. //仓库归属人创客编号
  55. if (!string.IsNullOrEmpty(UserIdMakerCode))
  56. {
  57. condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + UserIdMakerCode + "')";
  58. }
  59. // //仓库归属人真实姓名
  60. // if (!string.IsNullOrEmpty(UserIdRealName))
  61. // {
  62. // DataTable dt = OtherMySqlConn.dtable("SELECT Id FROM Users WHERE RealName like '%" + UserIdRealName + "%'");
  63. // var Count = dt.Rows.Count;
  64. // int times = 0;
  65. // string UserIds = "";
  66. // foreach (DataRow item in dt.Rows)
  67. // {
  68. // times++;
  69. // if (times == Count)
  70. // {
  71. // UserIds += item["Id"].ToString();
  72. // }
  73. // else
  74. // {
  75. // UserIds += item["Id"].ToString() + ",";
  76. // }
  77. // }
  78. // // condition += " and UserId in (select UserId from UserForRealName where RealName='" + UserIdRealName + "')";
  79. // condition += " and UserId in (" + UserIds + ")";
  80. // }
  81. // //仓库管理员真实姓名
  82. // if (!string.IsNullOrEmpty(ManageUserIdRealName))
  83. // {
  84. // DataTable dts = OtherMySqlConn.dtable("SELECT Id FROM Users WHERE RealName like '%" + ManageUserIdRealName + "%'");
  85. // var Counts = dts.Rows.Count;
  86. // int time = 1;
  87. // string UserIdss = "";
  88. // foreach (DataRow items in dts.Rows)
  89. // {
  90. // time++;
  91. // if (time == Counts)
  92. // {
  93. // UserIdss += items["Id"].ToString();
  94. // }
  95. // else
  96. // {
  97. // UserIdss += items["Id"].ToString() + ",";
  98. // }
  99. // }
  100. // // condition += " and ManageUserId in (select ManageUserId from UserForRealName where RealName='" + ManageUserIdRealName + "')";
  101. // condition += " and UserId in (" + UserIdss + ")";
  102. // }
  103. //仓库管理员手机号
  104. if (!string.IsNullOrEmpty(ManageUserIdMobile))
  105. {
  106. condition += " and ManageUserId in (select ManageUserId from UserForMobile where Mobile='" + ManageUserIdMobile + "')";
  107. }
  108. //仓库管理员创客编号
  109. if (!string.IsNullOrEmpty(ManageUserIdMakerCode))
  110. {
  111. condition += " and ManageUserId in (select ManageUserId from UserForMakerCode where MakerCode='" + ManageUserIdMakerCode + "')";
  112. }
  113. //仓库状态
  114. if (!string.IsNullOrEmpty(StoreStatusSelect))
  115. {
  116. condition += " and StoreStatus=" + StoreStatusSelect;
  117. }
  118. if (!string.IsNullOrEmpty(CreateDateData))
  119. {
  120. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  121. string start = datelist[0];
  122. string end = datelist[1];
  123. condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
  124. }
  125. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("StoreHouse", Fields, "Id desc", "0", page, limit, condition);
  126. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  127. foreach (Dictionary<string, object> dic in diclist)
  128. {
  129. //仓库归属人
  130. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  131. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  132. dic["UserIdMakerCode"] = userid_Users.MakerCode;
  133. dic["UserIdRealName"] = userid_Users.RealName;
  134. //产品类型
  135. dic["BrandId"] = RelationClass.GetKqProductBrandList(dic["BrandId"].ToString());
  136. //仓库管理员
  137. int ManageUserId = int.Parse(function.CheckInt(dic["ManageUserId"].ToString()));
  138. Users manageuserid_Users = db.Users.FirstOrDefault(m => m.Id == ManageUserId) ?? new Users();
  139. dic["ManageUserIdRealName"] = manageuserid_Users.RealName;
  140. dic["ManageUserIdMobile"] = manageuserid_Users.Mobile;
  141. dic["ManageUserIdMakerCode"] = manageuserid_Users.MakerCode;
  142. //仓库类型
  143. int StoreType = int.Parse(dic["StoreType"].ToString());
  144. if (StoreType == 0) dic["StoreType"] = "实体仓";
  145. if (StoreType == 1) dic["StoreType"] = "虚拟仓";
  146. //仓库状态
  147. int StoreStatus = int.Parse(dic["StoreStatus"].ToString());
  148. if (StoreStatus == 1) dic["StoreStatus"] = "启用";
  149. if (StoreStatus == 0) dic["StoreStatus"] = "禁用";
  150. //仓库归属类型
  151. int StoreKind = int.Parse(dic["StoreKind"].ToString());
  152. if (StoreKind == 0) dic["StoreKind"] = "分仓";
  153. if (StoreKind == 1) dic["StoreKind"] = "总仓";
  154. //小分仓
  155. int Id = int.Parse(function.CheckInt(dic["Id"].ToString()));
  156. int PreCount = db.PreSendStockDetail.Count(m => m.FromStoreId == Id && m.ApplyFlag == 0 && m.Status >= 0 && m.Status <= 1);
  157. dic["PreNum"] = PreCount.ToString();
  158. //分仓额度
  159. var amount = db.UserAccount.FirstOrDefault(m => m.Id == UserId) ?? new UserAccount();
  160. var ValidAmount = amount.ValidAmount;
  161. var FixedAmount = amount.FixedAmount;
  162. var TempAmount = amount.TempAmount;
  163. var LeaderBalanceAmount = amount.LeaderBalanceAmount;
  164. dic["ValidAmount"] = ValidAmount.ToString();//可用额度
  165. dic["FixedAmount"] = FixedAmount.ToString();//固定额度
  166. dic["TempAmount"] = TempAmount.ToString();//临时额度
  167. dic["LeaderBalanceAmount"] = LeaderBalanceAmount.ToString();//担保金额
  168. }
  169. return Json(obj);
  170. }
  171. #endregion
  172. #region 增加仓库
  173. /// <summary>
  174. /// 增加或修改仓库信息
  175. /// </summary>
  176. /// <returns></returns>
  177. public IActionResult Add(string right)
  178. {
  179. ViewBag.RightInfo = RightInfo;
  180. ViewBag.right = right;
  181. return View();
  182. }
  183. #endregion
  184. #region 增加仓库
  185. /// <summary>
  186. /// 增加或修改仓库信息
  187. /// </summary>
  188. /// <returns></returns>
  189. [HttpPost]
  190. public string Add(StoreHouse data, string ManageMakerCode, string MakerCode)
  191. {
  192. string BrandIdString = data.BrandId;
  193. if (!string.IsNullOrEmpty(BrandIdString))
  194. {
  195. string[] BrandIdList = BrandIdString.Split(',');
  196. foreach (string BrandId in BrandIdList)
  197. {
  198. Dictionary<string, object> Fields = new Dictionary<string, object>();
  199. Fields.Add("CreateMan", SysUserName); //创建人
  200. Fields.Add("StoreName", data.StoreName); //仓库名称
  201. UserForMakerCode user = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  202. if (user == null)
  203. {
  204. return "您输入的仓库归属创客编号不存在,请重新输入正确的仓库归属创客编号";
  205. }
  206. Fields.Add("UserId", user.UserId); //仓库归属人
  207. Fields.Add("Areas", data.Areas); //所属地区
  208. Fields.Add("Address", data.Address); //仓库地址
  209. UserForMakerCode manage = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == ManageMakerCode);
  210. if (manage == null)
  211. {
  212. return "您输入的仓库负责人编号不存在,请重新输入正确的仓库负责人编号";
  213. }
  214. Fields.Add("ManageUserId", manage.UserId); //仓库管理员
  215. Fields.Add("ManagerEmail", data.ManagerEmail); //管理者邮箱
  216. Fields.Add("ManageMobile", data.ManageMobile);
  217. Fields.Add("Remark", data.Remark); //备注
  218. Fields.Add("LimitTopUserId", data.LimitTopUserId); //限制创客特殊仓库
  219. Fields.Add("StoreKind", data.StoreKind); //仓库归属类型
  220. int bid = int.Parse(BrandId);
  221. KqProducts pro = db.KqProducts.FirstOrDefault(m => m.Id == bid) ?? new KqProducts();
  222. Fields.Add("BrandId", BrandId);
  223. Fields.Add("ProductName", pro.Name);
  224. Fields.Add("StoreStatus", 1);
  225. Fields.Add("Status", 1);
  226. var name = db.StoreHouse.FirstOrDefault(m => m.UserId == user.UserId && m.BrandId == BrandId && m.StoreName == data.StoreName);
  227. if (name != null)
  228. {
  229. return "您的仓库名重复,请重新填写";
  230. }
  231. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("StoreHouse", Fields, 0);
  232. StoreHouse edit = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  233. if (edit != null)
  234. {
  235. string No = Id.ToString();
  236. for (int i = 0; i < 7 - Id.ToString().Length; i++)
  237. {
  238. No = "0" + No;
  239. }
  240. No = "S" + No;
  241. edit.StoreNo = No;
  242. db.StoreForCode.Add(new StoreForCode()
  243. {
  244. Code = No,
  245. StoreId = Id,
  246. });
  247. RedisDbconn.Instance.Set("StoreForCode:" + No, Id);
  248. }
  249. db.StoreForName.Add(new StoreForName()
  250. {
  251. Name = data.StoreName + "-" + pro.Name,
  252. StoreId = Id,
  253. });
  254. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == user.UserId);
  255. if (account == null)
  256. {
  257. account = db.UserAccount.Add(new UserAccount()
  258. {
  259. Id = user.UserId,
  260. UserId = user.UserId,
  261. }).Entity;
  262. db.SaveChanges();
  263. }
  264. if (account.FixedAmount == 0 && account.ValidAmount == 0)
  265. {
  266. account.FixedAmount = 20000;
  267. account.ValidAmount = 20000;
  268. }
  269. RedisDbconn.Instance.Set("StoreForName:" + data.StoreName, Id);
  270. AddSysLog(data.Id.ToString(), "StoreHouse", "add");
  271. db.SaveChanges();
  272. SetRedis(Id, user.UserId);
  273. }
  274. }
  275. return "success";
  276. }
  277. #endregion
  278. #region 修改仓库
  279. /// <summary>
  280. /// 增加或修改仓库信息
  281. /// </summary>
  282. /// <returns></returns>
  283. public IActionResult Edit(string right, int Id = 0)
  284. {
  285. ViewBag.RightInfo = RightInfo;
  286. ViewBag.right = right;
  287. StoreHouse editData = db.StoreHouse.FirstOrDefault(m => m.Id == Id) ?? new StoreHouse();
  288. ViewBag.data = editData;
  289. Users manage = db.Users.FirstOrDefault(m => m.Id == editData.ManageUserId) ?? new Users();
  290. ViewBag.ManageMakerCode = manage.MakerCode;
  291. ViewBag.ManageRealName = manage.RealName;
  292. Users user = db.Users.FirstOrDefault(m => m.Id == editData.ManageUserId) ?? new Users();
  293. ViewBag.MakerCode = user.MakerCode;
  294. return View();
  295. }
  296. #endregion
  297. #region 修改仓库
  298. /// <summary>
  299. /// 增加或修改仓库信息
  300. /// </summary>
  301. /// <returns></returns>
  302. [HttpPost]
  303. public string Edit(StoreHouse data, string ManageMakerCode, string MakerCode)
  304. {
  305. Dictionary<string, object> Fields = new Dictionary<string, object>();
  306. Fields.Add("CreateMan", SysUserName); //创建人
  307. Fields.Add("StoreName", data.StoreName); //仓库名称
  308. UserForMakerCode user = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  309. if (user == null)
  310. {
  311. return "您输入的仓库归属创客编号不存在,请重新输入正确的仓库归属创客编号";
  312. }
  313. Fields.Add("UserId", user.UserId); //仓库归属人
  314. Fields.Add("Areas", data.Areas); //所属地区
  315. Fields.Add("Address", data.Address); //仓库地址
  316. UserForMakerCode manage = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == ManageMakerCode);
  317. if (manage == null)
  318. {
  319. return "您输入的仓库负责人编号不存在,请重新输入正确的仓库负责人编号";
  320. }
  321. Fields.Add("ManageUserId", manage.UserId); //仓库管理员
  322. Fields.Add("ManagerEmail", data.ManagerEmail); //管理者邮箱
  323. Fields.Add("ManageMobile", data.ManageMobile);
  324. Fields.Add("Remark", data.Remark); //备注
  325. Fields.Add("LimitTopUserId", data.LimitTopUserId); //限制创客特殊仓库
  326. Fields.Add("StoreKind", data.StoreKind); //仓库归属类型
  327. Fields.Add("Status", data.Status);
  328. Fields.Add("StoreStatus", data.Status);
  329. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("StoreHouse", Fields, data.Id);
  330. AddSysLog(data.Id.ToString(), "StoreHouse", "update");
  331. db.SaveChanges();
  332. SetRedis(data.Id, user.UserId);
  333. return "success";
  334. }
  335. #endregion
  336. #region 一键添加仓库
  337. /// <summary>
  338. /// 一件添加仓库
  339. /// </summary>
  340. /// <returns></returns>
  341. public IActionResult Edits(string right, int Id = 0)
  342. {
  343. ViewBag.RightInfo = RightInfo;
  344. ViewBag.right = right;
  345. StoreHouse editData = db.StoreHouse.FirstOrDefault(m => m.Id == Id) ?? new StoreHouse();
  346. ViewBag.data = editData;
  347. Users manage = db.Users.FirstOrDefault(m => m.Id == editData.ManageUserId) ?? new Users();
  348. ViewBag.ManageMakerCode = manage.MakerCode;
  349. ViewBag.ManageRealName = manage.RealName;
  350. Users user = db.Users.FirstOrDefault(m => m.Id == editData.ManageUserId) ?? new Users();
  351. ViewBag.MakerCode = user.MakerCode;
  352. return View();
  353. }
  354. #endregion
  355. #region 一键添加仓库
  356. /// <summary>
  357. /// 一键添加仓库
  358. /// </summary>
  359. /// <returns></returns>
  360. [HttpPost]
  361. public string Edits(StoreHouse data, string ManageMakerCode, string MakerCode)
  362. {
  363. Dictionary<string, object> Fields = new Dictionary<string, object>();
  364. StoreHouse storeHouse = db.StoreHouse.FirstOrDefault(m => m.Id == data.Id);
  365. var brand = db.StoreHouse.FirstOrDefault(m => m.UserId == storeHouse.UserId && m.BrandId == data.BrandId);
  366. if (brand != null)
  367. {
  368. return "您已有该品牌类型的仓库,请重新选择";
  369. }
  370. var name = db.StoreHouse.FirstOrDefault(m => m.UserId == storeHouse.UserId && m.StoreName == data.StoreName);
  371. if (name != null)
  372. {
  373. return "您的仓库名重复,请重新填写";
  374. }
  375. Fields.Add("StoreName", data.StoreName); //仓库名称
  376. UserForMakerCode user = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  377. if (user == null)
  378. {
  379. return "您输入的仓库归属创客编号不存在,请重新输入正确的仓库归属创客编号";
  380. }
  381. Fields.Add("UserId", user.UserId); //仓库归属人
  382. Fields.Add("Areas", data.Areas); //所属地区
  383. Fields.Add("Address", data.Address); //仓库地址
  384. UserForMakerCode manage = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == ManageMakerCode);
  385. if (manage == null)
  386. {
  387. return "您输入的仓库负责人编号不存在,请重新输入正确的仓库负责人编号";
  388. }
  389. Fields.Add("CreateMan", SysUserName); //创建人
  390. Fields.Add("ManageUserId", manage.UserId); //仓库管理员
  391. Fields.Add("ManagerEmail", data.ManagerEmail); //管理者邮箱
  392. Fields.Add("ManageMobile", data.ManageMobile);
  393. Fields.Add("Remark", data.Remark); //备注
  394. Fields.Add("LimitTopUserId", data.LimitTopUserId); //限制创客特殊仓库
  395. Fields.Add("StoreKind", data.StoreKind); //仓库归属类型
  396. int bid = Convert.ToInt32(data.BrandId);
  397. KqProducts pro = db.KqProducts.FirstOrDefault(m => m.Id == bid) ?? new KqProducts();
  398. Fields.Add("BrandId", data.BrandId);
  399. Fields.Add("ProductName", pro.Name);
  400. Fields.Add("StoreStatus", 1);
  401. Fields.Add("Status", 1);
  402. int Id = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Add("StoreHouse", Fields, 0);
  403. StoreHouse edit = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  404. if (edit != null)
  405. {
  406. string No = Id.ToString();
  407. for (int i = 0; i < 7 - Id.ToString().Length; i++)
  408. {
  409. No = "0" + No;
  410. }
  411. No = "S" + No;
  412. edit.StoreNo = No;
  413. db.StoreForCode.Add(new StoreForCode()
  414. {
  415. Code = No,
  416. StoreId = Id,
  417. });
  418. RedisDbconn.Instance.Set("StoreForCode:" + No, Id);
  419. }
  420. db.StoreForName.Add(new StoreForName()
  421. {
  422. Name = data.StoreName,
  423. StoreId = Id,
  424. });
  425. RedisDbconn.Instance.Set("StoreForName:" + data.StoreName, Id);
  426. AddSysLog(data.Id.ToString(), "StoreHouse", "add");
  427. db.SaveChanges();
  428. SetRedis(Id, user.UserId);
  429. return "success";
  430. }
  431. #endregion
  432. #region 删除仓库信息
  433. /// <summary>
  434. /// 删除仓库信息
  435. /// </summary>
  436. /// <returns></returns>
  437. public string Delete(string Id)
  438. {
  439. string[] idlist = Id.Split(new char[] { ',' });
  440. AddSysLog(Id, "StoreHouse", "del");
  441. foreach (string subid in idlist)
  442. {
  443. int id = int.Parse(subid);
  444. Dictionary<string, object> Fields = new Dictionary<string, object>();
  445. Fields.Add("Status", -1);
  446. StoreHouse edit = db.StoreHouse.FirstOrDefault(m => m.Id == id);
  447. if (edit != null)
  448. {
  449. StoreForCode code = db.StoreForCode.FirstOrDefault(m => m.Code == edit.StoreNo);
  450. if (code != null)
  451. {
  452. db.StoreForCode.Remove(code);
  453. }
  454. StoreForName name = db.StoreForName.FirstOrDefault(m => m.Name == edit.StoreName);
  455. if (name != null)
  456. {
  457. db.StoreForName.Remove(name);
  458. }
  459. }
  460. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("StoreHouse", Fields, id);
  461. }
  462. db.SaveChanges();
  463. return "success";
  464. }
  465. #endregion
  466. #region 开启
  467. /// <summary>
  468. /// 开启
  469. /// </summary>
  470. /// <returns></returns>
  471. public string Open(string Id)
  472. {
  473. string[] idlist = Id.Split(new char[] { ',' });
  474. AddSysLog(Id, "StoreHouse", "open");
  475. foreach (string subid in idlist)
  476. {
  477. int id = int.Parse(subid);
  478. Dictionary<string, object> Fields = new Dictionary<string, object>();
  479. Fields.Add("Status", 1);
  480. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("StoreHouse", Fields, id);
  481. }
  482. db.SaveChanges();
  483. return "success";
  484. }
  485. #endregion
  486. #region 关闭
  487. /// <summary>
  488. /// 关闭
  489. /// </summary>
  490. /// <returns></returns>
  491. public string Close(string Id)
  492. {
  493. string[] idlist = Id.Split(new char[] { ',' });
  494. AddSysLog(Id, "StoreHouse", "close");
  495. foreach (string subid in idlist)
  496. {
  497. int id = int.Parse(subid);
  498. Dictionary<string, object> Fields = new Dictionary<string, object>();
  499. Fields.Add("Status", 0);
  500. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Edit("StoreHouse", Fields, id);
  501. }
  502. db.SaveChanges();
  503. return "success";
  504. }
  505. #endregion
  506. #region 排序
  507. /// <summary>
  508. /// 排序
  509. /// </summary>
  510. /// <param name="Id"></param>
  511. public string Sort(int Id, int Sort)
  512. {
  513. new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).Sort("StoreHouse", Sort, Id);
  514. AddSysLog(Id.ToString(), "StoreHouse", "sort");
  515. return "success";
  516. }
  517. #endregion
  518. #region 导入数据
  519. public IActionResult Import(string right, string ExcelKind)
  520. {
  521. ViewBag.RightInfo = RightInfo;
  522. ViewBag.right = right;
  523. ViewBag.ExcelKind = ExcelKind;
  524. return View();
  525. }
  526. /// <summary>
  527. /// 导入数据
  528. /// </summary>
  529. /// <param name="ExcelData"></param>
  530. [HttpPost]
  531. public string ImportPost(string ExcelPath, int Kind = 0)
  532. {
  533. // RedisDbconn.Instance.AddList("ExcelImport", ExcelPath + "#cut#" + Kind);
  534. string key = function.MD5_16(Guid.NewGuid().ToString());
  535. RedisDbconn.Instance.AddList("ExcelImportV2", ExcelPath + "#cut#" + Kind + "#cut#" + key + "#cut#" + SysUserName);
  536. return "success|" + key;
  537. }
  538. // [HttpPost]
  539. // public Dictionary<string, object> ImportPostV2(string ExcelPath, int Kind = 0)
  540. // {
  541. // Dictionary<string, object> Obj = new Dictionary<string, object>();
  542. // string key = function.MD5_16(Guid.NewGuid().ToString());
  543. // RedisDbconn.Instance.AddList("ExcelImportV2", ExcelPath + "#cut#" + Kind + "#cut#" + key);
  544. // Obj.Add("status", "success");
  545. // Obj.Add("key", key);
  546. // Obj.Add("errlist", RedisDbconn.Instance.GetList<string>("ErrList" + key));
  547. // return Obj;
  548. // }
  549. public string CheckImport(string key)
  550. {
  551. string result = RedisDbconn.Instance.Get<string>("CheckImport:" + key);
  552. if (!string.IsNullOrEmpty(result))
  553. {
  554. string[] datalist = result.Split('|');
  555. if (datalist[0] == "success")
  556. {
  557. return result;
  558. }
  559. return datalist[0];
  560. }
  561. return "0";
  562. }
  563. public Dictionary<string, object> CheckImportV2(string key)
  564. {
  565. Dictionary<string, object> Obj = new Dictionary<string, object>();
  566. string result = RedisDbconn.Instance.Get<string>("CheckImport:" + key);
  567. if (!string.IsNullOrEmpty(result))
  568. {
  569. string[] datalist = result.Split('|');
  570. if (datalist[0] == "success")
  571. {
  572. List<string> errList = RedisDbconn.Instance.GetList<string>("ErrList" + key);
  573. if (errList.Count > 0)
  574. {
  575. Obj.Add("status", 2);
  576. Obj.Add("errList", errList);
  577. }
  578. else
  579. {
  580. Obj.Add("status", 1);
  581. Obj.Add("data", result);
  582. }
  583. return Obj;
  584. }
  585. Obj.Add("status", 0);
  586. Obj.Add("data", datalist[0]);
  587. return Obj;
  588. }
  589. Obj.Add("status", -1);
  590. Obj.Add("data", "执行中...");
  591. return Obj;
  592. }
  593. #endregion
  594. #region 机具驳回仓库
  595. /// <summary>
  596. /// 机具驳回仓库
  597. /// </summary>
  598. /// <param name="ExcelData"></param>
  599. public string RejectStore(string ExcelData, int Kind = 0)
  600. {
  601. ExcelData = HttpUtility.UrlDecode(ExcelData);
  602. JsonData list = JsonMapper.ToObject(ExcelData);
  603. if (Kind == 1)
  604. {
  605. string error = "";
  606. List<string> PosSnList = new List<string>();
  607. for (int i = 1; i < list.Count; i++)
  608. {
  609. JsonData dr = list[i];
  610. string itemJson = dr.ToJson();
  611. string PosSn = itemJson.Contains("\"A\"") ? dr["A"].ToString() : "";
  612. string BrandId = itemJson.Contains("\"B\"") ? dr["B"].ToString() : "";
  613. string MakerCode = itemJson.Contains("\"C\"") ? dr["C"].ToString() : "";
  614. string StoreNo = itemJson.Contains("\"D\"") ? dr["D"].ToString() : "";
  615. string No = itemJson.Contains("\"E\"") ? dr["E"].ToString() : "";
  616. string Remark = itemJson.Contains("\"F\"") ? dr["F"].ToString() : "";
  617. UserForMakerCode userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  618. StoreForCode storeForCode = db.StoreForCode.FirstOrDefault(m => m.Code == StoreNo);
  619. var store = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCode.StoreId && m.UserId == userForMakerCode.UserId && m.BrandId == BrandId);
  620. if (store == null)
  621. {
  622. error += "以下操作失败" + StoreNo + ',' + "未找到该仓库" + '\n';
  623. }
  624. var user = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new Users();
  625. MachineForSnNo machineForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn) ?? new MachineForSnNo();
  626. var posInfo = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForSnNo.SnId && m.BrandId == Convert.ToInt32(BrandId) && m.UserId == user.Id && m.BindingState == 0);
  627. if (posInfo == null)
  628. {
  629. error += "以下操作失败" + PosSn + ',' + "未找到该品牌的机具" + '\n';
  630. }
  631. else if (PosSnList.Contains(PosSn))
  632. {
  633. error += "以下操作失败" + PosSn + ',' + "该机具号重复" + '\n';
  634. }
  635. else if (!string.IsNullOrEmpty(error))
  636. {
  637. return "Warning|" + error;
  638. }
  639. else
  640. {
  641. PosSnList.Add(PosSn);
  642. }
  643. }
  644. for (int i = 1; i < list.Count; i++)
  645. {
  646. JsonData dr = list[i];
  647. string itemJson = dr.ToJson();
  648. string PosSn = itemJson.Contains("\"A\"") ? dr["A"].ToString() : "";
  649. string BrandId = itemJson.Contains("\"B\"") ? dr["B"].ToString() : "";
  650. string MakerCode = itemJson.Contains("\"C\"") ? dr["C"].ToString() : "";
  651. string StoreNo = itemJson.Contains("\"D\"") ? dr["D"].ToString() : "";
  652. string No = itemJson.Contains("\"E\"") ? dr["E"].ToString() : "";
  653. string Remark = itemJson.Contains("\"F\"") ? dr["F"].ToString() : "";
  654. decimal amount = 0;
  655. UserForMakerCode userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  656. var user = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new Users();
  657. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new UserAccount();
  658. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == Convert.ToInt32(BrandId)) ?? new KqProducts();
  659. if (brandInfo.Name.Contains("电签"))
  660. {
  661. amount = 200;
  662. }
  663. if (brandInfo.Name.Contains("大POS"))
  664. {
  665. amount = 300;
  666. }
  667. userAccount.ValidAmount += amount;
  668. string text = string.Format("导入机具驳回仓库,UserId: '" + user.Id + "',BeforeChangeAmount:'" + userAccount.ValidAmount + "',AfterChangeAmount:'" + userAccount.ValidAmount + amount + "',ChangeAmount:'" + amount + "',Time'" + DateTime.Now + "'");
  669. function.WriteLog(text, "机具驳回仓库");//机具驳回仓库日志
  670. db.SaveChanges();
  671. MachineForSnNo machineForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn) ?? new MachineForSnNo();
  672. var posInfo = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForSnNo.SnId && m.BrandId == Convert.ToInt32(BrandId) && m.UserId == user.Id && m.BindingState == 0);
  673. if (posInfo != null)
  674. {
  675. var storehouse = db.StoreHouse.FirstOrDefault(m => m.Id == posInfo.StoreId);
  676. storehouse.TotalNum += 1;
  677. storehouse.LaveNum += 1;
  678. storehouse.OutNum -= 1;
  679. db.SaveChanges();
  680. StoreStockChange stockchange = db.StoreStockChange.Add(new StoreStockChange()
  681. {
  682. CreateDate = DateTime.Now,
  683. CreateMan = SysUserName,
  684. StoreId = storehouse.Id, //出货 仓库
  685. BrandId = Convert.ToInt32(BrandId), //产品类型
  686. ProductName = RelationClass.GetKqProductBrandInfo(Convert.ToInt32(BrandId)), //产品名称
  687. BizBatchNo = No, //业务批次号
  688. TransType = 1, //交易类型
  689. SnNo = PosSn, //SN编号
  690. StockOpDirect = 1, //库存操作方向
  691. SnStatus = 1, //SN状态
  692. DeviceVendor = posInfo.DeviceName, //设备厂商
  693. DeviceModel = posInfo.DeviceKind, //设备型号
  694. DeviceType = posInfo.DeviceType, //设备类型
  695. SourceStoreId = posInfo.SourceStoreId, //源仓库编号
  696. BrandType = posInfo.DeviceType, //品牌类型
  697. }).Entity;
  698. db.StoreChangeHistory.Add(new StoreChangeHistory()
  699. {
  700. CreateDate = DateTime.Now,
  701. UserId = user.Id, //创客
  702. BrandId = Convert.ToInt32(BrandId), //产品类型
  703. ChangeRecordNo = "JJBH" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), //变更记录单号
  704. SeoTitle = "机具驳回仓库",
  705. BizBatchNo = No, //业务批次号
  706. SnNo = PosSn, //SN编号
  707. });
  708. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  709. {
  710. CreateDate = DateTime.Now,
  711. StoreId = posInfo.StoreId, //仓库
  712. BrandId = Convert.ToInt32(BrandId), //产品类型
  713. OpStoreNum = 1, //操作库存数
  714. OpSymbol = "+", //操作符
  715. BeforeTotalNum = storehouse.TotalNum, //操作前总库存数
  716. AfterTotalNum = storehouse.TotalNum + 1, //操作后总库存数
  717. BeforeLaveNum = storehouse.LaveNum, //操作前剩余库存数
  718. AfterLaveNum = storehouse.LaveNum + 1, //操作后剩余库存数
  719. BeforeOutNum = storehouse.OutNum, //操作前出库数
  720. AfterOutNum = storehouse.OutNum - 1, //操作后出库数
  721. }).Entity;
  722. }
  723. }
  724. db.SaveChanges();
  725. }
  726. AddSysLog("0", "MachinesRejectStore", "Import");
  727. return "success";
  728. }
  729. #endregion
  730. #region 导出Excel
  731. /// <summary>
  732. /// 导出Excel
  733. /// </summary>
  734. /// <returns></returns>
  735. public JsonResult ExportExcel(StoreHouse data, string UserIdMakerCode, string UserIdRealName, string ManageUserIdRealName, string ManageUserIdMobile, string ManageUserIdMakerCode, string StoreStatusSelect)
  736. {
  737. Dictionary<string, string> Fields = new Dictionary<string, string>();
  738. Fields.Add("StoreNo", "1"); //仓库编号
  739. Fields.Add("CreateDate", "3"); //时间
  740. Fields.Add("StoreName", "1"); //仓库名称
  741. Fields.Add("ManagerEmail", "1"); //管理者邮箱
  742. string condition = " and Status>-1";
  743. //仓库归属人创客编号
  744. if (!string.IsNullOrEmpty(UserIdMakerCode))
  745. {
  746. condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + UserIdMakerCode + "')";
  747. }
  748. //仓库归属人真实姓名
  749. if (!string.IsNullOrEmpty(UserIdRealName))
  750. {
  751. condition += " and UserId in (select UserId from UserForRealName where RealName='" + UserIdRealName + "')";
  752. }
  753. //仓库管理员真实姓名
  754. if (!string.IsNullOrEmpty(ManageUserIdRealName))
  755. {
  756. condition += " and ManageUserId in (select ManageUserId from UserForRealName where RealName='" + ManageUserIdRealName + "')";
  757. }
  758. //仓库管理员手机号
  759. if (!string.IsNullOrEmpty(ManageUserIdMobile))
  760. {
  761. condition += " and ManageUserId in (select ManageUserId from UserForMobile where Mobile='" + ManageUserIdMobile + "')";
  762. }
  763. //仓库管理员创客编号
  764. if (!string.IsNullOrEmpty(ManageUserIdMakerCode))
  765. {
  766. condition += " and ManageUserId in (select ManageUserId from UserForMakerCode where MakerCode='" + ManageUserIdMakerCode + "')";
  767. }
  768. //仓库状态
  769. if (!string.IsNullOrEmpty(StoreStatusSelect))
  770. {
  771. condition += " and StoreStatus=" + StoreStatusSelect;
  772. }
  773. Dictionary<string, object> obj = new AdminContentOther(_accessor.HttpContext, PublicFunction.MainTables).IndexData("StoreHouse", Fields, "Id desc", "0", 1, 20000, condition, "StoreNo,StoreName,UserId,BrandId,Address,ManageUserId,ManagerEmail,StoreType,TotalNum,LaveNum,OutNum,LimitTopUserId,StoreStatus,StoreKind", false);
  774. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  775. foreach (Dictionary<string, object> dic in diclist)
  776. {
  777. //仓库归属人
  778. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  779. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  780. dic["UserIdMakerCode"] = userid_Users.MakerCode;
  781. dic["UserIdRealName"] = userid_Users.RealName;
  782. //产品类型
  783. dic["BrandId"] = RelationClass.GetKqProductBrandList(dic["BrandId"].ToString());
  784. //仓库管理员
  785. int ManageUserId = int.Parse(function.CheckInt(dic["ManageUserId"].ToString()));
  786. Users manageuserid_Users = db.Users.FirstOrDefault(m => m.Id == ManageUserId) ?? new Users();
  787. dic["ManageUserIdRealName"] = manageuserid_Users.RealName;
  788. dic["ManageUserIdMobile"] = manageuserid_Users.Mobile;
  789. dic["ManageUserIdMakerCode"] = manageuserid_Users.MakerCode;
  790. //仓库类型
  791. int StoreType = int.Parse(dic["StoreType"].ToString());
  792. if (StoreType == 0) dic["StoreType"] = "实体仓";
  793. if (StoreType == 1) dic["StoreType"] = "虚拟仓";
  794. //仓库状态
  795. int StoreStatus = int.Parse(dic["StoreStatus"].ToString());
  796. if (StoreStatus == 1) dic["StoreStatus"] = "启用";
  797. if (StoreStatus == 0) dic["StoreStatus"] = "禁用";
  798. //仓库归属类型
  799. int StoreKind = int.Parse(dic["StoreKind"].ToString());
  800. if (StoreKind == 0) dic["StoreKind"] = "分仓";
  801. if (StoreKind == 1) dic["StoreKind"] = "总仓";
  802. }
  803. Dictionary<string, object> result = new Dictionary<string, object>();
  804. result.Add("Status", "1");
  805. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  806. result.Add("Obj", diclist);
  807. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  808. ReturnFields.Add("StoreNo", "仓库编号");
  809. ReturnFields.Add("StoreName", "仓库名称");
  810. ReturnFields.Add("UserIdMakerCode", "仓库归属人创客编号");
  811. ReturnFields.Add("UserIdRealName", "仓库归属人真实姓名");
  812. ReturnFields.Add("BrandId", "产品类型");
  813. ReturnFields.Add("Address", "仓库地址");
  814. ReturnFields.Add("ManageUserIdRealName", "仓库管理员真实姓名");
  815. ReturnFields.Add("ManageUserIdMobile", "仓库管理员手机号");
  816. ReturnFields.Add("ManageUserIdMakerCode", "仓库管理员创客编号");
  817. ReturnFields.Add("ManagerEmail", "管理者邮箱");
  818. ReturnFields.Add("StoreType", "仓库类型");
  819. ReturnFields.Add("TotalNum", "总库存数");
  820. ReturnFields.Add("LaveNum", "剩余库存数");
  821. ReturnFields.Add("OutNum", "出库数");
  822. ReturnFields.Add("LimitTopUserId", "限制创客特殊仓库");
  823. ReturnFields.Add("StoreStatus", "仓库状态");
  824. ReturnFields.Add("StoreKind", "仓库归属类型");
  825. result.Add("Fields", ReturnFields);
  826. AddSysLog("0", "StoreHouse", "ExportExcel");
  827. return Json(result);
  828. }
  829. #endregion
  830. #region 仓库列表
  831. /// <summary>
  832. /// 根据条件查询仓库列表
  833. /// </summary>
  834. /// <returns></returns>
  835. public IActionResult Stat(StoreHouse data, string right)
  836. {
  837. ViewBag.RightInfo = RightInfo;
  838. ViewBag.right = right;
  839. return View();
  840. }
  841. #endregion
  842. #region 根据条件查询仓库列表
  843. /// <summary>
  844. /// 仓库列表
  845. /// </summary>
  846. /// <returns></returns>
  847. public JsonResult StatDo(string CreateDateData, int page = 1, int limit = 30)
  848. {
  849. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["StatSqlConnStr"].ToString();
  850. string sCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
  851. string eCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
  852. if (!string.IsNullOrEmpty(CreateDateData))
  853. {
  854. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  855. sCreateDate = datelist[0] + " 00:00:00";
  856. eCreateDate = datelist[1] + " 23:59:59";
  857. }
  858. else
  859. {
  860. Dictionary<string, object> nodata = new Dictionary<string, object>();
  861. nodata.Add("code", 0);
  862. nodata.Add("msg", "");
  863. nodata.Add("count", 0);
  864. nodata.Add("data", new List<Dictionary<string, object>>());
  865. return Json(nodata);
  866. }
  867. string minId = "0";
  868. string maxId = "0";
  869. string minPosId = "0";
  870. string maxPosId = "0";
  871. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  872. if (startDt.Rows.Count > 0)
  873. {
  874. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  875. if (minId == "0")
  876. {
  877. startDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  878. if (startDt.Rows.Count > 0)
  879. {
  880. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  881. }
  882. }
  883. }
  884. DataTable endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  885. if (endDt.Rows.Count > 0)
  886. {
  887. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  888. if (minId == "0")
  889. {
  890. endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  891. if (endDt.Rows.Count > 0)
  892. {
  893. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  894. }
  895. }
  896. }
  897. DataTable startPosDt = OtherMySqlConn.dtable("select min(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  898. if (startPosDt.Rows.Count > 0)
  899. {
  900. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  901. if (minPosId == "0")
  902. {
  903. startPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  904. if (startPosDt.Rows.Count > 0)
  905. {
  906. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  907. }
  908. }
  909. }
  910. DataTable endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  911. if (endPosDt.Rows.Count > 0)
  912. {
  913. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  914. if (maxPosId == "0")
  915. {
  916. endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  917. if (endPosDt.Rows.Count > 0)
  918. {
  919. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  920. }
  921. }
  922. }
  923. DataTable list = OtherMySqlConn.dtable("select * from (select s.StoreName,s.StoreNo,u.RealName,u.Mobile,k.Name,s.LaveNum,(select count(Id) from StoreStockChange where Id>=" + minId + " and Id<=" + maxId + " and CreateDate>='" + sCreateDate + "' and CreateDate<='" + eCreateDate + "' and StoreId=s.Id and TransType=1) as c1,(select count(Id) from StoreStockChange where Id>=" + minId + " and Id<=" + maxId + " and CreateDate>='" + sCreateDate + "' and CreateDate<='" + eCreateDate + "' and StoreId=s.Id and TransType in (10,11,2)) as c2,(select count(Id) from PosMachinesTwo where Id>=" + minPosId + " and Id<=" + maxPosId + " and ActivationState=1 and ActivationTime>='" + sCreateDate + "' and ActivationTime<='" + eCreateDate + "' and StoreId=s.Id) as c3 from StoreHouse s left join Users u on s.BrandId>0 and s.UserId=u.Id left join KqProducts k on s.BrandId=k.Id) tb where c1>0 or c2>0 or c3>0");
  924. int recordcount = list.Rows.Count;
  925. List<Dictionary<string, object>> diclist = new List<Dictionary<string, object>>();
  926. foreach (DataRow sub in list.Rows)
  927. {
  928. Dictionary<string, object> row = new Dictionary<string, object>();
  929. row.Add("StoreName", sub["StoreName"].ToString());
  930. row.Add("StoreNo", sub["StoreNo"].ToString());
  931. row.Add("Mobile", sub["Mobile"].ToString());
  932. row.Add("RealName", sub["RealName"].ToString());
  933. row.Add("Name", sub["Name"].ToString());
  934. row.Add("LaveNum", sub["LaveNum"].ToString());
  935. row.Add("c1", sub["c1"].ToString());
  936. row.Add("c2", sub["c2"].ToString());
  937. row.Add("c3", sub["c3"].ToString());
  938. diclist.Add(row);
  939. }
  940. Dictionary<string, object> obj = new Dictionary<string, object>();
  941. obj.Add("code", 0);
  942. obj.Add("msg", "");
  943. obj.Add("count", recordcount);
  944. obj.Add("data", diclist);
  945. return Json(obj);
  946. }
  947. #endregion
  948. #region 导出Excel
  949. /// <summary>
  950. /// 导出Excel
  951. /// </summary>
  952. /// <returns></returns>
  953. public JsonResult StatExportExcel(string CreateDateData)
  954. {
  955. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["StatSqlConnStr"].ToString();
  956. string sCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
  957. string eCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
  958. if (!string.IsNullOrEmpty(CreateDateData))
  959. {
  960. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  961. sCreateDate = datelist[0] + " 00:00:00";
  962. eCreateDate = datelist[1] + " 23:59:59";
  963. }
  964. else
  965. {
  966. Dictionary<string, object> nodata = new Dictionary<string, object>();
  967. nodata.Add("Status", "1");
  968. nodata.Add("Info", "分仓统计数据报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  969. nodata.Add("Obj", new List<Dictionary<string, object>>());
  970. Dictionary<string, object> NoDataReturnFields = new Dictionary<string, object>();
  971. NoDataReturnFields.Add("StoreName", "仓库名称");
  972. NoDataReturnFields.Add("StoreNo", "仓库编号");
  973. NoDataReturnFields.Add("Mobile", "仓库所属人手机号");
  974. NoDataReturnFields.Add("RealName", "仓库所属人名称");
  975. NoDataReturnFields.Add("MakerCode", "仓库所属人编码");
  976. NoDataReturnFields.Add("ManageMakerCode", "仓库管理员编码");
  977. NoDataReturnFields.Add("Name", "品牌");
  978. NoDataReturnFields.Add("LaveNum", "当前库存");
  979. NoDataReturnFields.Add("c1", "出库(调拨)量");
  980. NoDataReturnFields.Add("c2", "出货(发货到创客)量");
  981. NoDataReturnFields.Add("c3", "激活量");
  982. nodata.Add("Fields", NoDataReturnFields);
  983. return Json(nodata);
  984. }
  985. string minId = "0";
  986. string maxId = "0";
  987. string minPosId = "0";
  988. string maxPosId = "0";
  989. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  990. if (startDt.Rows.Count > 0)
  991. {
  992. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  993. if (minId == "0")
  994. {
  995. startDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  996. if (startDt.Rows.Count > 0)
  997. {
  998. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  999. }
  1000. }
  1001. }
  1002. DataTable endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  1003. if (endDt.Rows.Count > 0)
  1004. {
  1005. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  1006. if (minId == "0")
  1007. {
  1008. endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  1009. if (endDt.Rows.Count > 0)
  1010. {
  1011. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  1012. }
  1013. }
  1014. }
  1015. DataTable startPosDt = OtherMySqlConn.dtable("select min(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  1016. if (startPosDt.Rows.Count > 0)
  1017. {
  1018. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  1019. if (minPosId == "0")
  1020. {
  1021. startPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  1022. if (startPosDt.Rows.Count > 0)
  1023. {
  1024. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  1025. }
  1026. }
  1027. }
  1028. DataTable endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  1029. if (endPosDt.Rows.Count > 0)
  1030. {
  1031. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  1032. if (maxPosId == "0")
  1033. {
  1034. endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  1035. if (endPosDt.Rows.Count > 0)
  1036. {
  1037. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  1038. }
  1039. }
  1040. }
  1041. DataTable list = OtherMySqlConn.dtable("select * from (select s.StoreName,s.StoreNo,u.RealName,u.Mobile,u.MakerCode,(select MakerCode from Users where Id=s.ManageUserId) as ManageMakerCode,k.Name,s.LaveNum,(select count(Id) from StoreStockChange where Id>=" + minId + " and Id<=" + maxId + " and CreateDate>='" + sCreateDate + "' and CreateDate<='" + eCreateDate + "' and StoreId=s.Id and TransType=1) as c1,(select count(Id) from StoreStockChange where Id>=" + minId + " and Id<=" + maxId + " and CreateDate>='" + sCreateDate + "' and CreateDate<='" + eCreateDate + "' and StoreId=s.Id and TransType in (10,11,2)) as c2,(select count(Id) from PosMachinesTwo where Id>=" + minPosId + " and Id<=" + maxPosId + " and ActivationState=1 and ActivationTime>='" + sCreateDate + "' and ActivationTime<='" + eCreateDate + "' and StoreId=s.Id) as c3 from StoreHouse s left join Users u on s.BrandId>0 and s.UserId=u.Id left join KqProducts k on s.BrandId=k.Id) tb where c1>0 or c2>0 or c3>0");
  1042. int recordcount = list.Rows.Count;
  1043. List<Dictionary<string, object>> diclist = new List<Dictionary<string, object>>();
  1044. foreach (DataRow sub in list.Rows)
  1045. {
  1046. Dictionary<string, object> row = new Dictionary<string, object>();
  1047. row.Add("StoreName", sub["StoreName"].ToString());
  1048. row.Add("StoreNo", sub["StoreNo"].ToString());
  1049. row.Add("Mobile", sub["Mobile"].ToString());
  1050. row.Add("RealName", sub["RealName"].ToString());
  1051. row.Add("MakerCode", sub["MakerCode"].ToString());
  1052. row.Add("ManageMakerCode", sub["ManageMakerCode"].ToString());
  1053. row.Add("Name", sub["Name"].ToString());
  1054. row.Add("LaveNum", sub["LaveNum"].ToString());
  1055. row.Add("c1", sub["c1"].ToString());
  1056. row.Add("c2", sub["c2"].ToString());
  1057. row.Add("c3", sub["c3"].ToString());
  1058. diclist.Add(row);
  1059. }
  1060. Dictionary<string, object> result = new Dictionary<string, object>();
  1061. result.Add("Status", "1");
  1062. result.Add("Info", "分仓统计数据报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  1063. result.Add("Obj", diclist);
  1064. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  1065. ReturnFields.Add("StoreName", "仓库名称");
  1066. ReturnFields.Add("StoreNo", "仓库编号");
  1067. ReturnFields.Add("Mobile", "仓库所属人手机号");
  1068. ReturnFields.Add("RealName", "仓库所属人名称");
  1069. ReturnFields.Add("MakerCode", "仓库所属人编码");
  1070. ReturnFields.Add("ManageMakerCode", "仓库管理员编码");
  1071. ReturnFields.Add("Name", "品牌");
  1072. ReturnFields.Add("LaveNum", "当前库存");
  1073. ReturnFields.Add("c1", "出库(调拨)量");
  1074. ReturnFields.Add("c2", "出货(发货到创客)量");
  1075. ReturnFields.Add("c3", "激活量");
  1076. result.Add("Fields", ReturnFields);
  1077. AddSysLog("0", "StoreHouse", "StatExportExcel");
  1078. return Json(result);
  1079. }
  1080. #endregion
  1081. #region 同步数据
  1082. /// <summary>
  1083. /// 同步数据
  1084. /// </summary>
  1085. /// <returns></returns>
  1086. public string SycnData(int Id = 0)
  1087. {
  1088. if (Id > 0)
  1089. {
  1090. DataTable dt = OtherMySqlConn.dtable("select Id,(select count(Id) from PosMachinesTwo where StoreId=s.Id and BuyUserId=0 and PreUserId=0 and Status>-1) as CurLaveNum from StoreHouse s where Id=" + Id);
  1091. if (dt.Rows.Count > 0)
  1092. {
  1093. int CurLaveNum = int.Parse(function.CheckInt(dt.Rows[0]["CurLaveNum"].ToString()));
  1094. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  1095. if (store != null)
  1096. {
  1097. store.LaveNum = CurLaveNum;
  1098. db.SaveChanges();
  1099. }
  1100. }
  1101. }
  1102. else
  1103. {
  1104. DataTable dt = OtherMySqlConn.dtable("select s.Id,LaveNum,count(p.Id) as CurLaveNum from StoreHouse s left join PosMachinesTwo p on s.Id=p.StoreId and s.BrandId=p.BrandId and p.BuyUserId=0 and p.PreUserId=0 and p.Status>-1 group by s.Id HAVING LaveNum!=count(p.Id)");
  1105. foreach (DataRow dr in dt.Rows)
  1106. {
  1107. int StoreId = int.Parse(function.CheckInt(dr["Id"].ToString()));
  1108. int CurLaveNum = int.Parse(function.CheckInt(dr["CurLaveNum"].ToString()));
  1109. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  1110. if (store != null)
  1111. {
  1112. store.LaveNum = CurLaveNum;
  1113. db.SaveChanges();
  1114. }
  1115. }
  1116. }
  1117. AddSysLog(Id, "StoreHouse", "SycnData");
  1118. db.SaveChanges();
  1119. return "success";
  1120. }
  1121. #endregion
  1122. #region 设置缓存
  1123. private void SetRedis(int Id, int UserId)
  1124. {
  1125. if (UserId > 0)
  1126. {
  1127. RedisDbconn.Instance.Clear("StoreHouseList:" + UserId);
  1128. List<StoreHouse> stores = db.StoreHouse.Where(m => m.Status > -1 && m.UserId == UserId).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  1129. RedisDbconn.Instance.Clear("StoreHouse");
  1130. stores = db.StoreHouse.Where(m => m.Status > -1).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  1131. }
  1132. if (Id > 0)
  1133. {
  1134. StoreHouse edit = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  1135. if (edit != null)
  1136. {
  1137. RedisDbconn.Instance.Clear("StoreHouse:" + Id);
  1138. }
  1139. }
  1140. }
  1141. #endregion
  1142. }
  1143. }