StoreHouseController.cs 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  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. StoreId = storehouse.Id, //出货 仓库
  684. BrandId = Convert.ToInt32(BrandId), //产品类型
  685. ProductName = RelationClass.GetKqProductBrandInfo(Convert.ToInt32(BrandId)), //产品名称
  686. BizBatchNo = No, //业务批次号
  687. TransType = 1, //交易类型
  688. SnNo = PosSn, //SN编号
  689. StockOpDirect = 1, //库存操作方向
  690. SnStatus = 1, //SN状态
  691. DeviceVendor = posInfo.DeviceName, //设备厂商
  692. DeviceModel = posInfo.DeviceKind, //设备型号
  693. DeviceType = posInfo.DeviceType, //设备类型
  694. SourceStoreId = posInfo.SourceStoreId, //源仓库编号
  695. BrandType = posInfo.DeviceType, //品牌类型
  696. }).Entity;
  697. db.StoreChangeHistory.Add(new StoreChangeHistory()
  698. {
  699. CreateDate = DateTime.Now,
  700. UserId = user.Id, //创客
  701. BrandId = Convert.ToInt32(BrandId), //产品类型
  702. ChangeRecordNo = "JJBH" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8), //变更记录单号
  703. SeoTitle = "机具驳回仓库",
  704. BizBatchNo = No, //业务批次号
  705. SnNo = PosSn, //SN编号
  706. });
  707. StoreBalance balance = db.StoreBalance.Add(new StoreBalance()
  708. {
  709. CreateDate = DateTime.Now,
  710. StoreId = posInfo.StoreId, //仓库
  711. BrandId = Convert.ToInt32(BrandId), //产品类型
  712. OpStoreNum = 1, //操作库存数
  713. OpSymbol = "+", //操作符
  714. BeforeTotalNum = storehouse.TotalNum, //操作前总库存数
  715. AfterTotalNum = storehouse.TotalNum + 1, //操作后总库存数
  716. BeforeLaveNum = storehouse.LaveNum, //操作前剩余库存数
  717. AfterLaveNum = storehouse.LaveNum + 1, //操作后剩余库存数
  718. BeforeOutNum = storehouse.OutNum, //操作前出库数
  719. AfterOutNum = storehouse.OutNum - 1, //操作后出库数
  720. }).Entity;
  721. }
  722. }
  723. db.SaveChanges();
  724. }
  725. AddSysLog("0", "MachinesRejectStore", "Import");
  726. return "success";
  727. }
  728. #endregion
  729. #region 导出Excel
  730. /// <summary>
  731. /// 导出Excel
  732. /// </summary>
  733. /// <returns></returns>
  734. public JsonResult ExportExcel(StoreHouse data, string UserIdMakerCode, string UserIdRealName, string ManageUserIdRealName, string ManageUserIdMobile, string ManageUserIdMakerCode, string StoreStatusSelect)
  735. {
  736. Dictionary<string, string> Fields = new Dictionary<string, string>();
  737. Fields.Add("StoreNo", "1"); //仓库编号
  738. Fields.Add("CreateDate", "3"); //时间
  739. Fields.Add("StoreName", "1"); //仓库名称
  740. Fields.Add("ManagerEmail", "1"); //管理者邮箱
  741. string condition = " and Status>-1";
  742. //仓库归属人创客编号
  743. if (!string.IsNullOrEmpty(UserIdMakerCode))
  744. {
  745. condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + UserIdMakerCode + "')";
  746. }
  747. //仓库归属人真实姓名
  748. if (!string.IsNullOrEmpty(UserIdRealName))
  749. {
  750. condition += " and UserId in (select UserId from UserForRealName where RealName='" + UserIdRealName + "')";
  751. }
  752. //仓库管理员真实姓名
  753. if (!string.IsNullOrEmpty(ManageUserIdRealName))
  754. {
  755. condition += " and ManageUserId in (select ManageUserId from UserForRealName where RealName='" + ManageUserIdRealName + "')";
  756. }
  757. //仓库管理员手机号
  758. if (!string.IsNullOrEmpty(ManageUserIdMobile))
  759. {
  760. condition += " and ManageUserId in (select ManageUserId from UserForMobile where Mobile='" + ManageUserIdMobile + "')";
  761. }
  762. //仓库管理员创客编号
  763. if (!string.IsNullOrEmpty(ManageUserIdMakerCode))
  764. {
  765. condition += " and ManageUserId in (select ManageUserId from UserForMakerCode where MakerCode='" + ManageUserIdMakerCode + "')";
  766. }
  767. //仓库状态
  768. if (!string.IsNullOrEmpty(StoreStatusSelect))
  769. {
  770. condition += " and StoreStatus=" + StoreStatusSelect;
  771. }
  772. 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);
  773. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  774. foreach (Dictionary<string, object> dic in diclist)
  775. {
  776. //仓库归属人
  777. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  778. Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  779. dic["UserIdMakerCode"] = userid_Users.MakerCode;
  780. dic["UserIdRealName"] = userid_Users.RealName;
  781. //产品类型
  782. dic["BrandId"] = RelationClass.GetKqProductBrandList(dic["BrandId"].ToString());
  783. //仓库管理员
  784. int ManageUserId = int.Parse(function.CheckInt(dic["ManageUserId"].ToString()));
  785. Users manageuserid_Users = db.Users.FirstOrDefault(m => m.Id == ManageUserId) ?? new Users();
  786. dic["ManageUserIdRealName"] = manageuserid_Users.RealName;
  787. dic["ManageUserIdMobile"] = manageuserid_Users.Mobile;
  788. dic["ManageUserIdMakerCode"] = manageuserid_Users.MakerCode;
  789. //仓库类型
  790. int StoreType = int.Parse(dic["StoreType"].ToString());
  791. if (StoreType == 0) dic["StoreType"] = "实体仓";
  792. if (StoreType == 1) dic["StoreType"] = "虚拟仓";
  793. //仓库状态
  794. int StoreStatus = int.Parse(dic["StoreStatus"].ToString());
  795. if (StoreStatus == 1) dic["StoreStatus"] = "启用";
  796. if (StoreStatus == 0) dic["StoreStatus"] = "禁用";
  797. //仓库归属类型
  798. int StoreKind = int.Parse(dic["StoreKind"].ToString());
  799. if (StoreKind == 0) dic["StoreKind"] = "分仓";
  800. if (StoreKind == 1) dic["StoreKind"] = "总仓";
  801. }
  802. Dictionary<string, object> result = new Dictionary<string, object>();
  803. result.Add("Status", "1");
  804. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  805. result.Add("Obj", diclist);
  806. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  807. ReturnFields.Add("StoreNo", "仓库编号");
  808. ReturnFields.Add("StoreName", "仓库名称");
  809. ReturnFields.Add("UserIdMakerCode", "仓库归属人创客编号");
  810. ReturnFields.Add("UserIdRealName", "仓库归属人真实姓名");
  811. ReturnFields.Add("BrandId", "产品类型");
  812. ReturnFields.Add("Address", "仓库地址");
  813. ReturnFields.Add("ManageUserIdRealName", "仓库管理员真实姓名");
  814. ReturnFields.Add("ManageUserIdMobile", "仓库管理员手机号");
  815. ReturnFields.Add("ManageUserIdMakerCode", "仓库管理员创客编号");
  816. ReturnFields.Add("ManagerEmail", "管理者邮箱");
  817. ReturnFields.Add("StoreType", "仓库类型");
  818. ReturnFields.Add("TotalNum", "总库存数");
  819. ReturnFields.Add("LaveNum", "剩余库存数");
  820. ReturnFields.Add("OutNum", "出库数");
  821. ReturnFields.Add("LimitTopUserId", "限制创客特殊仓库");
  822. ReturnFields.Add("StoreStatus", "仓库状态");
  823. ReturnFields.Add("StoreKind", "仓库归属类型");
  824. result.Add("Fields", ReturnFields);
  825. AddSysLog("0", "StoreHouse", "ExportExcel");
  826. return Json(result);
  827. }
  828. #endregion
  829. #region 仓库列表
  830. /// <summary>
  831. /// 根据条件查询仓库列表
  832. /// </summary>
  833. /// <returns></returns>
  834. public IActionResult Stat(StoreHouse data, string right)
  835. {
  836. ViewBag.RightInfo = RightInfo;
  837. ViewBag.right = right;
  838. return View();
  839. }
  840. #endregion
  841. #region 根据条件查询仓库列表
  842. /// <summary>
  843. /// 仓库列表
  844. /// </summary>
  845. /// <returns></returns>
  846. public JsonResult StatDo(string CreateDateData, int page = 1, int limit = 30)
  847. {
  848. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["StatSqlConnStr"].ToString();
  849. string sCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
  850. string eCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
  851. if (!string.IsNullOrEmpty(CreateDateData))
  852. {
  853. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  854. sCreateDate = datelist[0] + " 00:00:00";
  855. eCreateDate = datelist[1] + " 23:59:59";
  856. }
  857. else
  858. {
  859. Dictionary<string, object> nodata = new Dictionary<string, object>();
  860. nodata.Add("code", 0);
  861. nodata.Add("msg", "");
  862. nodata.Add("count", 0);
  863. nodata.Add("data", new List<Dictionary<string, object>>());
  864. return Json(nodata);
  865. }
  866. string minId = "0";
  867. string maxId = "0";
  868. string minPosId = "0";
  869. string maxPosId = "0";
  870. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  871. if (startDt.Rows.Count > 0)
  872. {
  873. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  874. if (minId == "0")
  875. {
  876. startDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  877. if (startDt.Rows.Count > 0)
  878. {
  879. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  880. }
  881. }
  882. }
  883. DataTable endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  884. if (endDt.Rows.Count > 0)
  885. {
  886. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  887. if (minId == "0")
  888. {
  889. endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  890. if (endDt.Rows.Count > 0)
  891. {
  892. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  893. }
  894. }
  895. }
  896. DataTable startPosDt = OtherMySqlConn.dtable("select min(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  897. if (startPosDt.Rows.Count > 0)
  898. {
  899. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  900. if (minPosId == "0")
  901. {
  902. startPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  903. if (startPosDt.Rows.Count > 0)
  904. {
  905. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  906. }
  907. }
  908. }
  909. DataTable endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  910. if (endPosDt.Rows.Count > 0)
  911. {
  912. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  913. if (maxPosId == "0")
  914. {
  915. endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  916. if (endPosDt.Rows.Count > 0)
  917. {
  918. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  919. }
  920. }
  921. }
  922. 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");
  923. int recordcount = list.Rows.Count;
  924. List<Dictionary<string, object>> diclist = new List<Dictionary<string, object>>();
  925. foreach (DataRow sub in list.Rows)
  926. {
  927. Dictionary<string, object> row = new Dictionary<string, object>();
  928. row.Add("StoreName", sub["StoreName"].ToString());
  929. row.Add("StoreNo", sub["StoreNo"].ToString());
  930. row.Add("Mobile", sub["Mobile"].ToString());
  931. row.Add("RealName", sub["RealName"].ToString());
  932. row.Add("Name", sub["Name"].ToString());
  933. row.Add("LaveNum", sub["LaveNum"].ToString());
  934. row.Add("c1", sub["c1"].ToString());
  935. row.Add("c2", sub["c2"].ToString());
  936. row.Add("c3", sub["c3"].ToString());
  937. diclist.Add(row);
  938. }
  939. Dictionary<string, object> obj = new Dictionary<string, object>();
  940. obj.Add("code", 0);
  941. obj.Add("msg", "");
  942. obj.Add("count", recordcount);
  943. obj.Add("data", diclist);
  944. return Json(obj);
  945. }
  946. #endregion
  947. #region 导出Excel
  948. /// <summary>
  949. /// 导出Excel
  950. /// </summary>
  951. /// <returns></returns>
  952. public JsonResult StatExportExcel(string CreateDateData)
  953. {
  954. OtherMySqlConn.connstr = ConfigurationManager.AppSettings["StatSqlConnStr"].ToString();
  955. string sCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00";
  956. string eCreateDate = DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59";
  957. if (!string.IsNullOrEmpty(CreateDateData))
  958. {
  959. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  960. sCreateDate = datelist[0] + " 00:00:00";
  961. eCreateDate = datelist[1] + " 23:59:59";
  962. }
  963. else
  964. {
  965. Dictionary<string, object> nodata = new Dictionary<string, object>();
  966. nodata.Add("Status", "1");
  967. nodata.Add("Info", "分仓统计数据报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  968. nodata.Add("Obj", new List<Dictionary<string, object>>());
  969. Dictionary<string, object> NoDataReturnFields = new Dictionary<string, object>();
  970. NoDataReturnFields.Add("StoreName", "仓库名称");
  971. NoDataReturnFields.Add("StoreNo", "仓库编号");
  972. NoDataReturnFields.Add("Mobile", "仓库所属人手机号");
  973. NoDataReturnFields.Add("RealName", "仓库所属人名称");
  974. NoDataReturnFields.Add("MakerCode", "仓库所属人编码");
  975. NoDataReturnFields.Add("ManageMakerCode", "仓库管理员编码");
  976. NoDataReturnFields.Add("Name", "品牌");
  977. NoDataReturnFields.Add("LaveNum", "当前库存");
  978. NoDataReturnFields.Add("c1", "出库(调拨)量");
  979. NoDataReturnFields.Add("c2", "出货(发货到创客)量");
  980. NoDataReturnFields.Add("c3", "激活量");
  981. nodata.Add("Fields", NoDataReturnFields);
  982. return Json(nodata);
  983. }
  984. string minId = "0";
  985. string maxId = "0";
  986. string minPosId = "0";
  987. string maxPosId = "0";
  988. DataTable startDt = OtherMySqlConn.dtable("select min(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  989. if (startDt.Rows.Count > 0)
  990. {
  991. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  992. if (minId == "0")
  993. {
  994. startDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  995. if (startDt.Rows.Count > 0)
  996. {
  997. minId = function.CheckInt(startDt.Rows[0][0].ToString());
  998. }
  999. }
  1000. }
  1001. DataTable endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange where CreateDate>='" + sCreateDate + "'");
  1002. if (endDt.Rows.Count > 0)
  1003. {
  1004. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  1005. if (minId == "0")
  1006. {
  1007. endDt = OtherMySqlConn.dtable("select max(Id) from StoreStockChange");
  1008. if (endDt.Rows.Count > 0)
  1009. {
  1010. maxId = function.CheckInt(endDt.Rows[0][0].ToString());
  1011. }
  1012. }
  1013. }
  1014. DataTable startPosDt = OtherMySqlConn.dtable("select min(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  1015. if (startPosDt.Rows.Count > 0)
  1016. {
  1017. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  1018. if (minPosId == "0")
  1019. {
  1020. startPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  1021. if (startPosDt.Rows.Count > 0)
  1022. {
  1023. minPosId = function.CheckInt(startPosDt.Rows[0][0].ToString());
  1024. }
  1025. }
  1026. }
  1027. DataTable endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo where ActivationTime>='" + sCreateDate + "'");
  1028. if (endPosDt.Rows.Count > 0)
  1029. {
  1030. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  1031. if (maxPosId == "0")
  1032. {
  1033. endPosDt = OtherMySqlConn.dtable("select max(Id) from PosMachinesTwo");
  1034. if (endPosDt.Rows.Count > 0)
  1035. {
  1036. maxPosId = function.CheckInt(endPosDt.Rows[0][0].ToString());
  1037. }
  1038. }
  1039. }
  1040. 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");
  1041. int recordcount = list.Rows.Count;
  1042. List<Dictionary<string, object>> diclist = new List<Dictionary<string, object>>();
  1043. foreach (DataRow sub in list.Rows)
  1044. {
  1045. Dictionary<string, object> row = new Dictionary<string, object>();
  1046. row.Add("StoreName", sub["StoreName"].ToString());
  1047. row.Add("StoreNo", sub["StoreNo"].ToString());
  1048. row.Add("Mobile", sub["Mobile"].ToString());
  1049. row.Add("RealName", sub["RealName"].ToString());
  1050. row.Add("MakerCode", sub["MakerCode"].ToString());
  1051. row.Add("ManageMakerCode", sub["ManageMakerCode"].ToString());
  1052. row.Add("Name", sub["Name"].ToString());
  1053. row.Add("LaveNum", sub["LaveNum"].ToString());
  1054. row.Add("c1", sub["c1"].ToString());
  1055. row.Add("c2", sub["c2"].ToString());
  1056. row.Add("c3", sub["c3"].ToString());
  1057. diclist.Add(row);
  1058. }
  1059. Dictionary<string, object> result = new Dictionary<string, object>();
  1060. result.Add("Status", "1");
  1061. result.Add("Info", "分仓统计数据报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  1062. result.Add("Obj", diclist);
  1063. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  1064. ReturnFields.Add("StoreName", "仓库名称");
  1065. ReturnFields.Add("StoreNo", "仓库编号");
  1066. ReturnFields.Add("Mobile", "仓库所属人手机号");
  1067. ReturnFields.Add("RealName", "仓库所属人名称");
  1068. ReturnFields.Add("MakerCode", "仓库所属人编码");
  1069. ReturnFields.Add("ManageMakerCode", "仓库管理员编码");
  1070. ReturnFields.Add("Name", "品牌");
  1071. ReturnFields.Add("LaveNum", "当前库存");
  1072. ReturnFields.Add("c1", "出库(调拨)量");
  1073. ReturnFields.Add("c2", "出货(发货到创客)量");
  1074. ReturnFields.Add("c3", "激活量");
  1075. result.Add("Fields", ReturnFields);
  1076. AddSysLog("0", "StoreHouse", "StatExportExcel");
  1077. return Json(result);
  1078. }
  1079. #endregion
  1080. #region 同步数据
  1081. /// <summary>
  1082. /// 同步数据
  1083. /// </summary>
  1084. /// <returns></returns>
  1085. public string SycnData(int Id = 0)
  1086. {
  1087. if (Id > 0)
  1088. {
  1089. 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);
  1090. if (dt.Rows.Count > 0)
  1091. {
  1092. int CurLaveNum = int.Parse(function.CheckInt(dt.Rows[0]["CurLaveNum"].ToString()));
  1093. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  1094. if (store != null)
  1095. {
  1096. store.LaveNum = CurLaveNum;
  1097. db.SaveChanges();
  1098. }
  1099. }
  1100. }
  1101. else
  1102. {
  1103. 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)");
  1104. foreach (DataRow dr in dt.Rows)
  1105. {
  1106. int StoreId = int.Parse(function.CheckInt(dr["Id"].ToString()));
  1107. int CurLaveNum = int.Parse(function.CheckInt(dr["CurLaveNum"].ToString()));
  1108. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == StoreId);
  1109. if (store != null)
  1110. {
  1111. store.LaveNum = CurLaveNum;
  1112. db.SaveChanges();
  1113. }
  1114. }
  1115. }
  1116. AddSysLog(Id, "StoreHouse", "SycnData");
  1117. db.SaveChanges();
  1118. return "success";
  1119. }
  1120. #endregion
  1121. #region 设置缓存
  1122. private void SetRedis(int Id, int UserId)
  1123. {
  1124. if (UserId > 0)
  1125. {
  1126. RedisDbconn.Instance.Clear("StoreHouseList:" + UserId);
  1127. List<StoreHouse> stores = db.StoreHouse.Where(m => m.Status > -1 && m.UserId == UserId).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  1128. RedisDbconn.Instance.Clear("StoreHouse");
  1129. stores = db.StoreHouse.Where(m => m.Status > -1).OrderByDescending(m => m.Sort).ThenByDescending(m => m.Id).ToList();
  1130. }
  1131. if (Id > 0)
  1132. {
  1133. StoreHouse edit = db.StoreHouse.FirstOrDefault(m => m.Id == Id);
  1134. if (edit != null)
  1135. {
  1136. RedisDbconn.Instance.Clear("StoreHouse:" + Id);
  1137. }
  1138. }
  1139. }
  1140. #endregion
  1141. }
  1142. }