StoreHouseController.cs 59 KB

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