StoreMachineApplyOperateController.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * 运营中心机具申请记录
  3. */
  4. using System;
  5. using System.Web;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. using Microsoft.AspNetCore.Mvc;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.Extensions.Logging;
  13. using Microsoft.Extensions.Options;
  14. using MySystem.OpModels;
  15. using Library;
  16. using LitJson;
  17. using MySystemLib;
  18. namespace MySystem.Areas.Admin.Controllers
  19. {
  20. [Area("Admin")]
  21. [Route("Admin/[controller]/[action]")]
  22. public class StoreMachineApplyOperateController : BaseController
  23. {
  24. public StoreMachineApplyOperateController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  25. {
  26. }
  27. #region 运营中心机具申请记录列表
  28. /// <summary>
  29. /// 根据条件查询运营中心机具申请记录列表
  30. /// </summary>
  31. /// <returns></returns>
  32. public IActionResult Index(StoreMachineApply data, string right)
  33. {
  34. ViewBag.RightInfo = RightInfo;
  35. ViewBag.right = right;
  36. return View();
  37. }
  38. #endregion
  39. #region 根据条件查询运营中心机具申请记录列表
  40. /// <summary>
  41. /// 运营中心机具申请记录列表
  42. /// </summary>
  43. /// <returns></returns>
  44. public JsonResult IndexData(StoreMachineApply data, string MakerCode, string OpCode, string CreateDateData, string StatusSelect, int page = 1, int limit = 30)
  45. {
  46. Dictionary<string, string> Fields = new Dictionary<string, string>();
  47. Fields.Add("ApplyNo", "1"); //申请单号
  48. Fields.Add("SendMode", "1"); //发货方式
  49. string condition = " and Status>-1 and Sort=0";
  50. if (!string.IsNullOrEmpty(CreateDateData))
  51. {
  52. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  53. string start = datelist[0];
  54. string end = datelist[1];
  55. condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
  56. }
  57. //创客编号
  58. if (!string.IsNullOrEmpty(MakerCode))
  59. {
  60. condition += " and UserId in (select UserId from SysAdmin where MakerCode='" + MakerCode + "')";
  61. }
  62. //运营中心编号
  63. if (!string.IsNullOrEmpty(OpCode))
  64. {
  65. condition += " and UserId in (select UserId from SysAdmin where OpCode='" + OpCode + "')";
  66. }
  67. if (!string.IsNullOrEmpty(StatusSelect))
  68. {
  69. condition += " and Status=" + StatusSelect + "";
  70. }
  71. Dictionary<string, object> obj = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).IndexData("StoreMachineApply", Fields, "Id desc", "0", page, limit, condition);
  72. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  73. foreach (Dictionary<string, object> dic in diclist)
  74. {
  75. //品牌
  76. dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
  77. int Status = int.Parse(dic["Status"].ToString());
  78. if (Status == 0) dic["StatusName"] = "待配货";
  79. if (Status == 1) dic["StatusName"] = "已发货";
  80. if (Status == 2) dic["StatusName"] = "已驳回";
  81. //创客
  82. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  83. SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.UserId == UserId) ?? new SysAdmin();
  84. dic["MakerName"] = sysAdmin.MakerName;
  85. dic["MakerCode"] = sysAdmin.MakerCode;
  86. dic["OpCode"] = sysAdmin.OpCode;
  87. dic.Remove("UserId");
  88. }
  89. return Json(obj);
  90. }
  91. #endregion
  92. #region 运营中心机具券申请记录列表
  93. /// <summary>
  94. /// 根据条件查询运营中心机具券申请记录列表
  95. /// </summary>
  96. /// <returns></returns>
  97. public IActionResult Indexs(StoreMachineApply data, string right)
  98. {
  99. ViewBag.RightInfo = RightInfo;
  100. ViewBag.right = right;
  101. return View();
  102. }
  103. #endregion
  104. #region 根据条件查询运营中心机具券申请记录列表
  105. /// <summary>
  106. /// 运营中心机具券申请记录列表
  107. /// </summary>
  108. /// <returns></returns>
  109. public JsonResult IndexsData(StoreMachineApply data, string MakerCode, string OpCode, string CreateDateData, string StatusSelect, int page = 1, int limit = 30)
  110. {
  111. Dictionary<string, string> Fields = new Dictionary<string, string>();
  112. Fields.Add("ApplyNo", "1"); //申请单号
  113. Fields.Add("SendMode", "1"); //发货方式
  114. string condition = " and Status>-1 and Sort=1";
  115. if (!string.IsNullOrEmpty(CreateDateData))
  116. {
  117. string[] datelist = CreateDateData.Split(new string[] { " - " }, StringSplitOptions.None);
  118. string start = datelist[0];
  119. string end = datelist[1];
  120. condition += " and CreateDate>='" + start + " 00:00:00' and CreateDate<='" + end + " 23:59:59'";
  121. }
  122. //创客编号
  123. if (!string.IsNullOrEmpty(MakerCode))
  124. {
  125. condition += " and UserId in (select UserId from SysAdmin where MakerCode='" + MakerCode + "')";
  126. }
  127. //运营中心编号
  128. if (!string.IsNullOrEmpty(OpCode))
  129. {
  130. condition += " and UserId in (select UserId from SysAdmin where OpCode='" + OpCode + "')";
  131. }
  132. if (!string.IsNullOrEmpty(StatusSelect))
  133. {
  134. condition += " and Status=" + StatusSelect + "";
  135. }
  136. Dictionary<string, object> obj = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).IndexData("StoreMachineApply", Fields, "Id desc", "0", page, limit, condition);
  137. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  138. foreach (Dictionary<string, object> dic in diclist)
  139. {
  140. //品牌
  141. dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
  142. int Status = int.Parse(dic["Status"].ToString());
  143. if (Status == 0) dic["StatusName"] = "待配货";
  144. if (Status == 1) dic["StatusName"] = "已发货";
  145. if (Status == 2) dic["StatusName"] = "已驳回";
  146. //创客
  147. int UserId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  148. SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.UserId == UserId) ?? new SysAdmin();
  149. dic["MakerName"] = sysAdmin.MakerName;
  150. dic["MakerCode"] = sysAdmin.MakerCode;
  151. dic["OpCode"] = sysAdmin.OpCode;
  152. dic.Remove("UserId");
  153. }
  154. return Json(obj);
  155. }
  156. #endregion
  157. #region 增加分仓机具申请记录
  158. /// <summary>
  159. /// 增加或修改分仓机具申请记录信息
  160. /// </summary>
  161. /// <returns></returns>
  162. public IActionResult Add(string right)
  163. {
  164. ViewBag.RightInfo = RightInfo;
  165. ViewBag.right = right;
  166. return View();
  167. }
  168. #endregion
  169. #region 增加分仓机具申请记录
  170. /// <summary>
  171. /// 增加或修改分仓机具申请记录信息
  172. /// </summary>
  173. /// <returns></returns>
  174. [HttpPost]
  175. public string Add(StoreMachineApply data)
  176. {
  177. Dictionary<string, object> Fields = new Dictionary<string, object>();
  178. Fields.Add("BrandId", data.BrandId); //品牌
  179. Fields.Add("ApplyNo", data.ApplyNo); //申请单号
  180. Fields.Add("ApplyNum", data.ApplyNum); //申请台数
  181. Fields.Add("SendNum", data.SendNum); //发货台数
  182. Fields.Add("UseAmount", data.UseAmount); //使用额度
  183. Fields.Add("SendMode", data.SendMode); //发货方式
  184. Fields.Add("ErpCode", data.ErpCode); //快递单号
  185. Fields.Add("SendSn", data.SendSn); //发货SN数据
  186. Fields.Add("UserId", data.UserId); //创客
  187. Fields.Add("SeoTitle", data.SeoTitle);
  188. Fields.Add("SeoKeyword", data.SeoKeyword);
  189. Fields.Add("SeoDescription", data.SeoDescription);
  190. int Id = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Add("StoreMachineApply", Fields, 0);
  191. AddSysLog(data.Id.ToString(), "StoreMachineApply", "add");
  192. opdb.SaveChanges();
  193. return "success";
  194. }
  195. #endregion
  196. #region 修改分仓机具申请记录
  197. /// <summary>
  198. /// 增加或修改分仓机具申请记录信息
  199. /// </summary>
  200. /// <returns></returns>
  201. public IActionResult Edit(string right, int Id = 0)
  202. {
  203. ViewBag.RightInfo = RightInfo;
  204. ViewBag.right = right;
  205. StoreMachineApply editData = opdb.StoreMachineApply.FirstOrDefault(m => m.Id == Id) ?? new StoreMachineApply();
  206. ViewBag.data = editData;
  207. Models.WebCMSEntities db = new Models.WebCMSEntities();
  208. // Users users = db.Users.FirstOrDefault(m => m.Id == editData.UserId) ?? new Users();
  209. SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Status == 0 && m.UserId == editData.UserId) ?? new SysAdmin();
  210. ViewBag.UserId = sysAdmin.UserId;
  211. ViewBag.MakerCode = sysAdmin.MakerCode;
  212. ViewBag.MakerName = sysAdmin.MakerName;
  213. ViewBag.UseAmount = editData.UseAmount;
  214. JsonData SendSnList = new JsonData();
  215. SendSnList = JsonMapper.ToObject(editData.SendSn);//申请数据
  216. List<Dictionary<string, object>> KsProductList = new List<Dictionary<string, object>>();
  217. for (int i = 0; i < SendSnList.Count; i++)
  218. {
  219. int BrandId = Convert.ToInt32(SendSnList[i]["BrandId"].ToString());
  220. int Num = Convert.ToInt32(SendSnList[i]["ApplyNum"].ToString());
  221. var products = db.KqProducts.Where(m => m.Id == BrandId).ToList();
  222. foreach (var product in products)
  223. {
  224. Dictionary<string, object> item = new Dictionary<string, object>();
  225. item.Add("BrandId", BrandId); //商品Id
  226. item.Add("Name", product.Name); //商品名称
  227. item.Add("Num", Num); //商品数量
  228. KsProductList.Add(item);
  229. }
  230. }
  231. ViewBag.KsProductList = KsProductList;
  232. return View();
  233. }
  234. #endregion
  235. #region 修改分仓机具申请记录
  236. /// <summary>
  237. /// 增加或修改分仓机具申请记录信息
  238. /// </summary>
  239. /// <returns></returns>
  240. [HttpPost]
  241. public string Edit(StoreMachineApply data)
  242. {
  243. Dictionary<string, object> Fields = new Dictionary<string, object>();
  244. Fields.Add("SendSn", data.SendSn); //发货SN数据
  245. JsonData SendSnList = new JsonData();
  246. SendSnList = JsonMapper.ToObject(data.SendSn);//申请数据
  247. int ApplyNum = 0;
  248. for (int i = 0; i < SendSnList.Count; i++)
  249. {
  250. int num = Convert.ToInt32(SendSnList[i]["ApplyNum"].ToString());
  251. ApplyNum += num;
  252. }
  253. Fields.Add("BrandId", data.BrandId); //品牌
  254. Fields.Add("ApplyNo", data.ApplyNo); //申请单号
  255. Fields.Add("ApplyNum", ApplyNum); //申请台数
  256. Fields.Add("SendNum", data.SendNum); //发货台数
  257. Fields.Add("UseAmount", data.UseAmount); //使用额度
  258. Fields.Add("SendMode", data.SendMode); //发货方式
  259. Fields.Add("ErpCode", data.ErpCode); //快递单号
  260. Fields.Add("UserId", data.UserId); //创客
  261. Fields.Add("SeoKeyword", data.SeoKeyword);
  262. Fields.Add("SeoDescription", data.SeoDescription);
  263. new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("StoreMachineApply", Fields, data.Id);
  264. AddSysLog(data.Id.ToString(), "OpStoreMachineApply", "update");
  265. db.SaveChanges();
  266. return "success";
  267. }
  268. #endregion
  269. #region 删除分仓机具申请记录信息
  270. /// <summary>
  271. /// 删除分仓机具申请记录信息
  272. /// </summary>
  273. /// <returns></returns>
  274. public string Delete(string Id)
  275. {
  276. string[] idlist = Id.Split(new char[] { ',' });
  277. AddSysLog(Id, "StoreMachineApply", "del");
  278. foreach (string subid in idlist)
  279. {
  280. int id = int.Parse(subid);
  281. Dictionary<string, object> Fields = new Dictionary<string, object>();
  282. Fields.Add("Status", -1);
  283. new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("StoreMachineApply", Fields, id);
  284. }
  285. db.SaveChanges();
  286. return "success";
  287. }
  288. #endregion
  289. #region 开启
  290. /// <summary>
  291. /// 开启
  292. /// </summary>
  293. /// <returns></returns>
  294. public string Open(string Id)
  295. {
  296. string[] idlist = Id.Split(new char[] { ',' });
  297. AddSysLog(Id, "StoreMachineApply", "open");
  298. foreach (string subid in idlist)
  299. {
  300. int id = int.Parse(subid);
  301. Dictionary<string, object> Fields = new Dictionary<string, object>();
  302. Fields.Add("Status", 1);
  303. new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("StoreMachineApply", Fields, id);
  304. }
  305. db.SaveChanges();
  306. return "success";
  307. }
  308. #endregion
  309. #region 关闭
  310. /// <summary>
  311. /// 关闭
  312. /// </summary>
  313. /// <returns></returns>
  314. public string Close(string Id)
  315. {
  316. string[] idlist = Id.Split(new char[] { ',' });
  317. AddSysLog(Id, "StoreMachineApplyOperate", "close");
  318. foreach (string subid in idlist)
  319. {
  320. int id = int.Parse(subid);
  321. Dictionary<string, object> Fields = new Dictionary<string, object>();
  322. Fields.Add("Status", 2);
  323. var query = opdb.StoreMachineApply.FirstOrDefault(m => m.Id == id);
  324. string[] amountlist = query.SeoTitle.Split(',');
  325. decimal ValidAmount = decimal.Parse(amountlist[0]);
  326. decimal TotalAmt = decimal.Parse(amountlist[1]);
  327. decimal ValidForGetAmount = decimal.Parse(amountlist[2]);
  328. var users = opdb.UserAccount.FirstOrDefault(m => m.Status == 0 && m.UserId == query.UserId) ?? new UserAccount();
  329. if (users.Id > 0)
  330. {
  331. var amountRecord = opdb.AmountRecord.Add(new AmountRecord()
  332. {
  333. CreateDate = DateTime.Now,
  334. CreateMan = SysUserName + "-" + SysRealName,
  335. SeoDescription = "机具申请驳回",
  336. OperateType = 1,//增加
  337. BeforeAmount = users.ValidAmount + users.TotalAmt + users.ValidForGetAmount,
  338. AfterAmount = users.ValidAmount + users.TotalAmt + users.ValidForGetAmount + query.UseAmount,
  339. UseAmount = query.UseAmount,
  340. UserId = query.UserId,
  341. ApplyId = query.Id,
  342. }).Entity;
  343. users.ValidAmount += ValidAmount;
  344. users.TotalAmt += TotalAmt;
  345. users.ValidForGetAmount += ValidForGetAmount;
  346. opdb.SaveChanges();
  347. }
  348. new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("StoreMachineApply", Fields, id);
  349. }
  350. db.SaveChanges();
  351. return "success";
  352. }
  353. #endregion
  354. #region 排序
  355. /// <summary>
  356. /// 排序
  357. /// </summary>
  358. /// <param name="Id"></param>
  359. public string Sort(int Id, int Sort)
  360. {
  361. new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Sort("StoreMachineApply", Sort, Id);
  362. AddSysLog(Id.ToString(), "StoreMachineApply", "sort");
  363. return "success";
  364. }
  365. #endregion
  366. #region 导入数据
  367. /// <summary>
  368. /// 导入数据
  369. /// </summary>
  370. /// <param name="ExcelData"></param>
  371. public string Import(string ExcelData)
  372. {
  373. ExcelData = HttpUtility.UrlDecode(ExcelData);
  374. JsonData list = JsonMapper.ToObject(ExcelData);
  375. for (int i = 1; i < list.Count; i++)
  376. {
  377. JsonData dr = list[i];
  378. opdb.StoreMachineApply.Add(new StoreMachineApply()
  379. {
  380. CreateDate = DateTime.Now,
  381. UpdateDate = DateTime.Now,
  382. });
  383. db.SaveChanges();
  384. }
  385. AddSysLog("0", "OpStoreMachineApply", "Import");
  386. return "success";
  387. }
  388. #endregion
  389. #region 导出Excel
  390. /// <summary>
  391. /// 导出Excel
  392. /// </summary>
  393. /// <returns></returns>
  394. public JsonResult ExportExcel(StoreMachineApply data, string BrandIdSelect, string UserIdRealName, string UserIdMakerCode)
  395. {
  396. Dictionary<string, string> Fields = new Dictionary<string, string>();
  397. Fields.Add("CreateDate", "3"); //时间
  398. Fields.Add("ApplyNo", "1"); //申请单号
  399. Fields.Add("SendMode", "1"); //发货方式
  400. string condition = " and Status>-1";
  401. //品牌
  402. if (!string.IsNullOrEmpty(BrandIdSelect))
  403. {
  404. condition += " and BrandId=" + BrandIdSelect;
  405. }
  406. //创客真实姓名
  407. if (!string.IsNullOrEmpty(UserIdRealName))
  408. {
  409. condition += " and UserId in (select UserId from UserForRealName where RealName='" + UserIdRealName + "')";
  410. }
  411. //创客编号
  412. if (!string.IsNullOrEmpty(UserIdMakerCode))
  413. {
  414. condition += " and UserId in (select UserId from UserForMakerCode where MakerCode='" + UserIdMakerCode + "')";
  415. }
  416. Dictionary<string, object> obj = new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).IndexData("StoreMachineApply", Fields, "Id desc", "0", 1, 20000, condition, "BrandId,ApplyNo,ApplyNum,SendNum,UseAmount,SendMode,ErpCode,UserId", false);
  417. List<Dictionary<string, object>> diclist = obj["data"] as List<Dictionary<string, object>>;
  418. foreach (Dictionary<string, object> dic in diclist)
  419. {
  420. //品牌
  421. dic["BrandId"] = RelationClass.GetKqProductBrandInfo(int.Parse(dic["BrandId"].ToString()));
  422. dic["StatusName"] = dic["Status"].ToString() == "1" ? "正常" : "关闭";
  423. //运营中心
  424. int OpId = int.Parse(function.CheckInt(dic["UserId"].ToString()));
  425. SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Id == OpId) ?? new SysAdmin();
  426. // Users userid_Users = db.Users.FirstOrDefault(m => m.Id == UserId) ?? new Users();
  427. dic["MakerName"] = sysAdmin.MakerName;
  428. dic["MakerCode"] = sysAdmin.MakerCode;
  429. dic["OpCode"] = sysAdmin.OpCode;
  430. dic.Remove("OpId");
  431. }
  432. Dictionary<string, object> result = new Dictionary<string, object>();
  433. result.Add("Status", "1");
  434. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  435. result.Add("Obj", diclist);
  436. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  437. ReturnFields.Add("BrandId", "品牌");
  438. ReturnFields.Add("ApplyNo", "申请单号");
  439. ReturnFields.Add("ApplyNum", "申请台数");
  440. ReturnFields.Add("SendNum", "发货台数");
  441. ReturnFields.Add("UseAmount", "使用额度");
  442. ReturnFields.Add("SendMode", "发货方式");
  443. ReturnFields.Add("ErpCode", "快递单号");
  444. ReturnFields.Add("MakerName", "创客真实姓名");
  445. ReturnFields.Add("MakerName", "创客编号");
  446. ReturnFields.Add("OpCode", "运营中心编号");
  447. result.Add("Fields", ReturnFields);
  448. AddSysLog("0", "OpStoreMachineApply", "ExportExcel");
  449. return Json(result);
  450. }
  451. #endregion
  452. #region 审核发货
  453. public IActionResult AuditSend(string right, int Id = 0)
  454. {
  455. ViewBag.RightInfo = RightInfo;
  456. ViewBag.right = right;
  457. StoreMachineApply editData = opdb.StoreMachineApply.FirstOrDefault(m => m.Id == Id) ?? new StoreMachineApply();
  458. ViewBag.data = editData;
  459. SysAdmin sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.Status == 0 && m.UserId == editData.UserId) ?? new SysAdmin();
  460. ViewBag.OpCode = sysAdmin.OpCode;
  461. ViewBag.MakerCode = sysAdmin.MakerCode;
  462. ViewBag.MakerName = sysAdmin.MakerName;
  463. return View();
  464. }
  465. #endregion
  466. #region 审核发货
  467. [HttpPost]
  468. public string AuditSend(StoreMachineApply data, string ExcelData)
  469. {
  470. Dictionary<string, object> Fields = new Dictionary<string, object>();
  471. Fields.Add("ApplyNum", data.ApplyNum); //申请数
  472. Fields.Add("SendNum", data.ApplyNum); //发货数
  473. Fields.Add("ErpCode", data.ErpCode); //快递单号
  474. Fields.Add("SendMode", data.SendMode); //发货方式
  475. Fields.Add("SeoKeyword", data.SeoKeyword); //申请描述
  476. Fields.Add("Status", 1);
  477. AddSysLog(data.Id.ToString(), "StoreMachineApply", "AuditSend");
  478. Dictionary<string, object> Obj = new Dictionary<string, object>();
  479. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  480. List<SendInfo> sendInfos = new List<SendInfo>();
  481. List<CheckSendInfo> checksendInfos = new List<CheckSendInfo>();
  482. StoreMachineApply apply = opdb.StoreMachineApply.FirstOrDefault(m => m.Id == data.Id) ?? new StoreMachineApply();
  483. string[] amountlist = apply.SeoTitle.Split(',');
  484. decimal ValidAmount = decimal.Parse(amountlist[0]);
  485. decimal TotalAmt = decimal.Parse(amountlist[1]);
  486. decimal ValidForGetAmount = decimal.Parse(amountlist[2]);
  487. JsonData ApplyList = JsonMapper.ToObject(apply.SendSn);
  488. decimal ApplyAmount = 0;
  489. decimal Amount = 0;
  490. for (int i = 0; i < ApplyList.Count; i++)
  491. {
  492. int num = Convert.ToInt32(ApplyList[i]["ApplyNum"].ToString());
  493. int BrandIds = Convert.ToInt32(ApplyList[i]["BrandId"].ToString());
  494. //自动匹配品牌
  495. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == BrandIds);
  496. if (brandInfo.Name.Contains("电签"))
  497. {
  498. ApplyAmount += num * 100;
  499. }
  500. if (brandInfo.Name.Contains("大POS"))
  501. {
  502. ApplyAmount += num * 150;
  503. }
  504. }
  505. Amount = apply.UseAmount - ApplyAmount;
  506. Models.StoreHouse tostore = new Models.StoreHouse();
  507. Models.StoreHouse fromstore = new Models.StoreHouse();
  508. var BrandId = 0;
  509. var FromStoreId = 0;
  510. string error = "";
  511. if (string.IsNullOrEmpty(ExcelData))
  512. {
  513. return "Warning|" + "请选择要导入的数据";
  514. }
  515. ExcelData = HttpUtility.UrlDecode(ExcelData);
  516. JsonData list = JsonMapper.ToObject(ExcelData);
  517. //判断各品牌发货数量是否符合
  518. if (list.Count - 1 > apply.ApplyNum)
  519. {
  520. return "Warning|" + "机具发货数量过多!";
  521. }
  522. if (list.Count - 1 < apply.ApplyNum)
  523. {
  524. return "Warning|" + "机具发货数量不足!";
  525. }
  526. for (int index = 1; index < list.Count; index++)
  527. {
  528. JsonData dr = list[index];
  529. string itemJson = dr.ToJson();
  530. string SnNo = itemJson.Contains("\"A\"") ? dr["A"].ToString() : "";
  531. string Brand = itemJson.Contains("\"B\"") ? dr["B"].ToString() : "";
  532. string BatchNo = itemJson.Contains("\"C\"") ? dr["C"].ToString() : "";
  533. string OutNote = itemJson.Contains("\"D\"") ? dr["D"].ToString() : "";
  534. BrandId = Convert.ToInt32(Brand);
  535. if (BrandId == 1) FromStoreId = 7;
  536. if (BrandId == 2) FromStoreId = 721;
  537. if (BrandId == 3) FromStoreId = 697;
  538. if (BrandId == 4) FromStoreId = 774;
  539. if (BrandId == 5) FromStoreId = 775;
  540. if (BrandId == 6) FromStoreId = 871;
  541. if (BrandId == 7) FromStoreId = 1047;
  542. if (BrandId == 8) FromStoreId = 4831;
  543. if (BrandId == 9) FromStoreId = 4832;
  544. CheckSendInfo items = checksendInfos.FirstOrDefault(m => m.BrandId == BrandId);
  545. if (items == null)
  546. {
  547. CheckSendInfo rows = new CheckSendInfo()
  548. {
  549. BrandId = BrandId,
  550. Num = 1
  551. };
  552. checksendInfos.Add(rows);
  553. }
  554. else
  555. {
  556. items.Num += 1;
  557. }
  558. }
  559. for (int i = 0; i < ApplyList.Count; i++)
  560. {
  561. int num = Convert.ToInt32(ApplyList[i]["ApplyNum"].ToString());
  562. int BrandIds = Convert.ToInt32(ApplyList[i]["BrandId"].ToString());
  563. if (BrandIds == 1) FromStoreId = 7;
  564. if (BrandIds == 2) FromStoreId = 721;
  565. if (BrandIds == 3) FromStoreId = 697;
  566. if (BrandIds == 4) FromStoreId = 774;
  567. if (BrandIds == 5) FromStoreId = 775;
  568. if (BrandIds == 6) FromStoreId = 871;
  569. if (BrandIds == 7) FromStoreId = 1047;
  570. if (BrandIds == 8) FromStoreId = 4831;
  571. if (BrandIds == 9) FromStoreId = 4832;
  572. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == BrandIds);
  573. CheckSendInfo items = checksendInfos.FirstOrDefault(m => m.BrandId == BrandIds) ?? new CheckSendInfo();
  574. if (items.Num > num)
  575. {
  576. return "Warning|" + "该条发货记录的" + brandInfo.Name + "发货数量过多!";
  577. }
  578. if (items.Num < num)
  579. {
  580. return "Warning|" + "该条发货记录的" + brandInfo.Name + "发货数量不足!";
  581. }
  582. }
  583. List<string> PosSnList = new List<string>();
  584. for (int index = 1; index < list.Count; index++)
  585. {
  586. JsonData dr = list[index];
  587. string itemJson = dr.ToJson();
  588. string SnNo = itemJson.Contains("\"A\"") ? dr["A"].ToString() : "";
  589. string Brand = itemJson.Contains("\"B\"") ? dr["B"].ToString() : "";
  590. BrandId = Convert.ToInt32(Brand);
  591. if (BrandId == 1) FromStoreId = 7;
  592. if (BrandId == 2) FromStoreId = 721;
  593. if (BrandId == 3) FromStoreId = 697;
  594. if (BrandId == 4) FromStoreId = 774;
  595. if (BrandId == 5) FromStoreId = 775;
  596. if (BrandId == 6) FromStoreId = 871;
  597. if (BrandId == 7) FromStoreId = 1047;
  598. if (BrandId == 8) FromStoreId = 4831;
  599. if (BrandId == 9) FromStoreId = 4832;
  600. Models.MachineForSnNo posInfo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SnNo) ?? new Models.MachineForSnNo();
  601. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posInfo.SnId && m.StoreId == FromStoreId && m.UserId == 0 && m.BuyUserId == 0 && m.PreUserId == 0) ?? new Models.PosMachinesTwo();
  602. if (posInfo.SnId > 0)
  603. {
  604. if (pos.Id > 0)
  605. {
  606. if (pos.BrandId != BrandId)
  607. {
  608. error += "以下操作失败" + SnNo + ',' + "该机具品牌填写错误" + '\n';
  609. }
  610. }
  611. if (PosSnList.Contains(SnNo))
  612. {
  613. error += "以下操作失败" + SnNo + ',' + "该机具重复发货" + '\n';
  614. }
  615. PosSnList.Add(SnNo);
  616. }
  617. else
  618. {
  619. error += "以下操作失败" + SnNo + ',' + "未找到该机具或者该机具不符合发货条件" + '\n';
  620. }
  621. }
  622. if (!string.IsNullOrEmpty(error))
  623. {
  624. return "Warning|" + error;
  625. }
  626. Dictionary<string, int> storeData = new Dictionary<string, int>();
  627. Dictionary<string, int> toStoreData = new Dictionary<string, int>();
  628. var amount = 0;
  629. for (int index = 1; index < list.Count; index++)
  630. {
  631. JsonData dr = list[index];
  632. string itemJson = dr.ToJson();
  633. string SnNo = itemJson.Contains("\"A\"") ? dr["A"].ToString() : "";
  634. string Brand = itemJson.Contains("\"B\"") ? dr["B"].ToString() : "";
  635. string BatchNo = itemJson.Contains("\"C\"") ? dr["C"].ToString() : "";
  636. string OutNote = itemJson.Contains("\"D\"") ? dr["D"].ToString() : "";
  637. BrandId = Convert.ToInt32(Brand);
  638. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == BrandId);
  639. if (brandInfo != null)
  640. {
  641. if (brandInfo.Name.Contains("电签"))
  642. {
  643. amount = 100;
  644. }
  645. if (brandInfo.Name.Contains("大POS"))
  646. {
  647. amount = 150;
  648. }
  649. }
  650. if (BrandId == 1) FromStoreId = 7;
  651. if (BrandId == 2) FromStoreId = 721;
  652. if (BrandId == 3) FromStoreId = 697;
  653. if (BrandId == 4) FromStoreId = 774;
  654. if (BrandId == 5) FromStoreId = 775;
  655. if (BrandId == 6) FromStoreId = 871;
  656. if (BrandId == 7) FromStoreId = 1047;
  657. if (BrandId == 8) FromStoreId = 4831;
  658. if (BrandId == 9) FromStoreId = 4832;
  659. tostore = db.StoreHouse.FirstOrDefault(m => m.Sort > 0 && m.UserId == apply.UserId && m.BrandId == BrandId.ToString() && m.Status == 1);
  660. SendInfo item = sendInfos.FirstOrDefault(m => m.FromStoreId == FromStoreId && m.ToStoreId == tostore.Id && m.BrandId == BrandId);
  661. if (item == null)
  662. {
  663. SendInfo row = new SendInfo()
  664. {
  665. FromStoreId = FromStoreId,
  666. ToStoreId = tostore.Id,
  667. BrandId = BrandId,
  668. Num = 1
  669. };
  670. sendInfos.Add(row);
  671. }
  672. else
  673. {
  674. item.Num += 1;
  675. }
  676. apply.SeoDescription = function.CheckString(SnNo) + ',';
  677. fromstore = db.StoreHouse.FirstOrDefault(m => m.Id == FromStoreId);
  678. Models.MachineForSnNo machinefor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SnNo) ?? new Models.MachineForSnNo();
  679. Models.PosMachinesTwo machine = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machinefor.SnId && m.BuyUserId == 0 && m.UserId == 0) ?? new Models.PosMachinesTwo();
  680. if (machine.Id > 0)
  681. {
  682. if (machine.BrandId != BrandId)
  683. {
  684. return machine.PosSn + "与表格里填写的品牌不一致";
  685. }
  686. else
  687. {
  688. Models.StoreStockChange stockchange = db.StoreStockChange.Add(new Models.StoreStockChange()
  689. {
  690. CreateDate = DateTime.Now,
  691. CreateMan = SysUserName,
  692. Sort = data.Id,//库存变动关联申请记录表
  693. StoreId = FromStoreId, //出货仓库
  694. BrandId = BrandId, //产品类型
  695. ProductName = RelationClass.GetKqProductBrandInfo(BrandId), //产品名称
  696. BizBatchNo = BatchNo, //业务批次号
  697. TransType = 1, //交易类型
  698. SnNo = SnNo, //SN编号
  699. StockOpDirect = 1, //库存操作方向
  700. SnStatus = 1, //SN状态
  701. DeviceVendor = machine.DeviceName, //设备厂商
  702. DeviceModel = machine.DeviceKind, //设备型号
  703. DeviceType = machine.DeviceType, //设备类型
  704. FromUserId = fromstore.UserId, //出货人
  705. FromDate = DateTime.Now, //出库时间
  706. FromRemark = OutNote, //出库备注
  707. ToUserId = tostore.UserId, //收货人
  708. ToStoreId = fromstore.Id, //退货收货仓库
  709. SourceStoreId = machine.SourceStoreId, //源仓库编号
  710. BrandType = machine.DeviceType, //品牌类型
  711. }).Entity;
  712. string ChangeNo = "CS";
  713. int StoreStockChangeId = stockchange.Id;
  714. string StoreStockChangeIdString = StoreStockChangeId.ToString();
  715. for (int j = 0; j < 18 - StoreStockChangeId.ToString().Length; j++)
  716. {
  717. StoreStockChangeIdString = "0" + StoreStockChangeIdString;
  718. }
  719. ChangeNo += StoreStockChangeIdString;
  720. stockchange.ChangeNo = ChangeNo; //交易流水编号
  721. machine.StoreId = tostore.Id;
  722. db.StoreChangeHistory.Add(new Models.StoreChangeHistory()
  723. {
  724. CreateDate = DateTime.Now,
  725. UserId = fromstore.UserId, //创客
  726. BrandId = BrandId, //产品类型
  727. ChangeRecordNo = ChangeNo, //变更记录单号
  728. BizBatchNo = BatchNo, //业务批次号
  729. TransType = 1, //交易类型
  730. SnNo = SnNo, //SN编号
  731. SnType = machine.PosSnType, //SN机具类型
  732. StockOpDirect = 1, //库存操作方向
  733. DeviceVendor = machine.DeviceName, //设备厂商
  734. DeviceModel = machine.DeviceKind, //设备型号
  735. DeviceType = machine.DeviceType, //设备类型
  736. FromUserId = fromstore.UserId, //出货创客
  737. FromDate = DateTime.Now, //出库时间
  738. FromRemark = OutNote, //出库备注
  739. SourceStoreId = machine.SourceStoreId, //源仓库
  740. StoreId = fromstore.Id, //仓库
  741. ToUserId = tostore.UserId,//收货创客
  742. });
  743. //修改机具所属仓库
  744. machine.StoreId = tostore.Id;
  745. machine.UpdateDate = DateTime.Now;
  746. //添加机具所属运营中心
  747. machine.OpId = tostore.UserId;
  748. // //额度标记
  749. // if (ValidAmount >= amount)
  750. // {
  751. // ValidAmount -= amount;
  752. // // machine.OpReserveKind = 3;
  753. // }
  754. // else if (TotalAmt >= amount)
  755. // {
  756. // TotalAmt -= amount;
  757. // // machine.OpReserveKind = 1;
  758. // }
  759. // else if (ValidForGetAmount >= amount)
  760. // {
  761. // ValidForGetAmount -= amount;
  762. // // machine.OpReserveKind = 2;
  763. // }
  764. if (ValidAmount >= amount)
  765. {
  766. ValidAmount -= amount;
  767. machine.OpReserve3 = amount;
  768. machine.OpReserve1 = 0;
  769. machine.OpReserve2 = 0;
  770. }
  771. else
  772. {
  773. if (TotalAmt >= amount)
  774. {
  775. TotalAmt -= amount - ValidAmount;
  776. machine.OpReserve3 = ValidAmount;
  777. machine.OpReserve1 = amount - ValidAmount;
  778. machine.OpReserve2 = 0;
  779. }
  780. else
  781. {
  782. if (ValidForGetAmount >= amount)
  783. {
  784. ValidForGetAmount -= amount - TotalAmt;
  785. machine.OpReserve3 = 0;
  786. machine.OpReserve1 = TotalAmt;
  787. machine.OpReserve2 = amount - TotalAmt;
  788. }
  789. }
  790. }
  791. db.SaveChanges();
  792. }
  793. }
  794. else
  795. {
  796. return "总仓无该机具";
  797. }
  798. }
  799. foreach (var item in sendInfos)
  800. {
  801. //出货仓库数据调整
  802. Models.StoreHouse storeCH = db.StoreHouse.FirstOrDefault(m => m.Id == item.FromStoreId) ?? new Models.StoreHouse();
  803. Models.StoreBalance balance = db.StoreBalance.Add(new Models.StoreBalance()
  804. {
  805. CreateDate = DateTime.Now,
  806. StoreId = storeCH.Id, //仓库
  807. TransType = 1, //交易类型
  808. BrandId = BrandId, //产品类型
  809. OpStoreNum = item.Num, //操作库存数
  810. OpSymbol = "-", //操作符
  811. BeforeTotalNum = storeCH.TotalNum, //操作前总库存数
  812. AfterTotalNum = storeCH.TotalNum - item.Num, //操作后总库存数
  813. BeforeLaveNum = storeCH.LaveNum, //操作前剩余库存数
  814. AfterLaveNum = storeCH.LaveNum - item.Num, //操作后剩余库存数
  815. BeforeOutNum = storeCH.OutNum, //操作前出库数
  816. AfterOutNum = storeCH.OutNum + item.Num, //操作后出库数
  817. }).Entity;
  818. string ChangeNo1 = "CS";
  819. int StoreStockChangeId1 = balance.Id;
  820. string StoreStockChangeId1String = StoreStockChangeId1.ToString();
  821. for (int i = 0; i < 18 - StoreStockChangeId1.ToString().Length; i++)
  822. {
  823. StoreStockChangeId1String = "0" + StoreStockChangeId1String;
  824. }
  825. ChangeNo1 += StoreStockChangeId1String;
  826. balance.TransRecordNo = ChangeNo1; //交易流水编号
  827. storeCH.LaveNum -= item.Num;
  828. storeCH.OutNum += item.Num;
  829. //收货仓库数据调整
  830. Models.StoreHouse storeSH = db.StoreHouse.FirstOrDefault(m => m.Id == item.ToStoreId) ?? new Models.StoreHouse();
  831. balance = db.StoreBalance.Add(new Models.StoreBalance()
  832. {
  833. CreateDate = DateTime.Now,
  834. StoreId = storeSH.Id, //仓库
  835. TransType = 0, //交易类型
  836. BrandId = BrandId, //产品类型
  837. OpStoreNum = item.Num, //操作库存数
  838. OpSymbol = "+", //操作符
  839. BeforeTotalNum = storeSH.TotalNum, //操作前总库存数
  840. AfterTotalNum = storeSH.TotalNum + item.Num, //操作后总库存数
  841. BeforeLaveNum = storeSH.LaveNum, //操作前剩余库存数
  842. AfterLaveNum = storeSH.LaveNum + item.Num, //操作后剩余库存数
  843. BeforeOutNum = storeSH.OutNum, //操作前出库数
  844. AfterOutNum = storeSH.OutNum, //操作后出库数
  845. }).Entity;
  846. string ChangeNo2 = "CS";
  847. int StoreStockChangeId2 = balance.Id;
  848. string StoreStockChangeId2String = StoreStockChangeId2.ToString();
  849. for (int i = 0; i < 18 - StoreStockChangeId2.ToString().Length; i++)
  850. {
  851. StoreStockChangeId2String = "0" + StoreStockChangeId2String;
  852. }
  853. ChangeNo2 += StoreStockChangeId2String;
  854. balance.TransRecordNo = ChangeNo2; //交易流水编号
  855. storeSH.TotalNum += item.Num;
  856. storeSH.LaveNum += item.Num;
  857. var sysAdmin = opdb.SysAdmin.FirstOrDefault(m => m.QueryCount == 1 && m.Status == 0 && m.UserId == storeSH.UserId) ?? new SysAdmin();
  858. if (sysAdmin.Id > 0)
  859. {
  860. sysAdmin.TotalMachineCount += item.Num;
  861. opdb.SaveChanges();
  862. }
  863. }
  864. apply.SeoTitle = SysUserName + '-' + SysRealName;//添加操作人信息
  865. apply.UseAmount = ApplyAmount;
  866. db.SaveChanges();
  867. new AdminContentByConn(_accessor.HttpContext, PublicFunction.OpTables, OpConn).Edit("StoreMachineApply", Fields, data.Id);
  868. string SendData = "{\"Kind\":\"2\",\"Data\":{\"UserId\":\"" + apply.UserId + "\",\"Amount\":\"" + Amount + "\",\"OperateType\":\"1\"}}";
  869. RedisDbconn.Instance.AddList("StoreApplyQueue", SendData);
  870. return "success";
  871. }
  872. #endregion
  873. //发货信息实体类
  874. private class SendInfo
  875. {
  876. public int FromStoreId { get; set; }
  877. public int ToStoreId { get; set; }
  878. public int BrandId { get; set; }
  879. public int Num { get; set; }
  880. }
  881. //检查发货信息实体类
  882. private class CheckSendInfo
  883. {
  884. public int BrandId { get; set; }
  885. public int Num { get; set; }
  886. }
  887. }
  888. }