SysToolsController.cs 129 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  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.Models;
  15. using Library;
  16. using LitJson;
  17. using MySystemLib;
  18. using System.Text.RegularExpressions;
  19. using System.Security.Cryptography;
  20. using System.Text;
  21. namespace MySystem.Areas.Admin.Controllers
  22. {
  23. [Area("Admin")]
  24. [Route("Admin/[controller]/[action]")]
  25. public class SysToolsController : BaseController
  26. {
  27. public SysToolsController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  28. {
  29. }
  30. #region 关联上下级
  31. public IActionResult RelationForUser(string right)
  32. {
  33. ViewBag.RightInfo = RightInfo;
  34. ViewBag.right = right;
  35. return View();
  36. }
  37. [HttpPost]
  38. public string RelationForUserDo(string MakerCode, string ParentMakerCode, int IsTeam = 1)
  39. {
  40. if (string.IsNullOrEmpty(MakerCode))
  41. {
  42. return "请输入创客编号";
  43. }
  44. if (string.IsNullOrEmpty(ParentMakerCode))
  45. {
  46. return "请输入上级创客编号";
  47. }
  48. MakerCode = MakerCode.ToUpper();
  49. ParentMakerCode = ParentMakerCode.ToUpper();
  50. Users puser = db.Users.FirstOrDefault(m => m.MakerCode == ParentMakerCode);
  51. if (puser != null)
  52. {
  53. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  54. if (user != null)
  55. {
  56. if (IsTeam == 1)
  57. {
  58. RelationForUserSub(user.Id, user);
  59. }
  60. user.ParentUserId = puser.Id;
  61. user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
  62. db.SaveChanges();
  63. function.WriteLog(DateTime.Now.ToString() + ":下级:" + MakerCode + ", 上级:" + ParentMakerCode, "换线日志");
  64. return "success";
  65. }
  66. return "创客编号不存在";
  67. }
  68. return "上级创客编号不存在";
  69. }
  70. private void RelationForUserSub(int ParentUserId, Users puser)
  71. {
  72. var subusers = db.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav }).Where(m => m.ParentUserId == ParentUserId).ToList();
  73. foreach (var subuser in subusers)
  74. {
  75. Users user = db.Users.FirstOrDefault(m => m.Id == subuser.Id);
  76. if (user != null)
  77. {
  78. user.ParentUserId = puser.Id;
  79. user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
  80. db.SaveChanges();
  81. RelationForUserSub(user.Id, user);
  82. }
  83. }
  84. }
  85. #endregion
  86. #region 机具补录
  87. public IActionResult CheckMachine(string right)
  88. {
  89. ViewBag.RightInfo = RightInfo;
  90. ViewBag.right = right;
  91. return View();
  92. }
  93. [HttpPost]
  94. public string CheckMachineDo(string MakerCode, string PosSn, string MerNo = "", int AddActPrize = 0, int IsPre = 0, int IsSend = 0)
  95. {
  96. if (string.IsNullOrEmpty(MakerCode))
  97. {
  98. return "请输入创客编号";
  99. }
  100. if (string.IsNullOrEmpty(PosSn))
  101. {
  102. return "请输入机具SN";
  103. }
  104. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  105. KqProducts kqProducts = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId);
  106. if (pos == null)
  107. {
  108. return "机具SN不存在";
  109. }
  110. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  111. if (user == null)
  112. {
  113. return "创客编号不存在";
  114. }
  115. if (IsPre == 1)
  116. {
  117. if (pos.BuyUserId == 0)
  118. {
  119. pos.BuyUserId = user.Id;
  120. }
  121. if (pos.UserId == 0)
  122. {
  123. pos.UserId = user.Id;
  124. }
  125. }
  126. if (pos.BindingState == 0)
  127. {
  128. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == PosSn);
  129. if (bind != null)
  130. {
  131. pos.BindingState = 1;
  132. pos.BindingTime = bind.CreateTime;
  133. db.SaveChanges();
  134. MerNo = bind.MerNo;
  135. }
  136. }
  137. decimal CreditTrade = 0;
  138. bool check = spdb.TradeRecord.Any(m => m.TradeSnNo == pos.PosSn);
  139. if (check)
  140. {
  141. if (pos.BrandId == 1)
  142. {
  143. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M700")).Sum(m => m.TradeAmount) / 100;
  144. }
  145. else if (pos.BrandId == 2)
  146. {
  147. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "2").Sum(m => m.TradeAmount);
  148. }
  149. else if (pos.BrandId == 3)
  150. {
  151. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M900")).Sum(m => m.TradeAmount) / 100;
  152. }
  153. else if (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9)
  154. {
  155. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "4").Sum(m => m.TradeAmount) / 100;
  156. }
  157. else if (pos.BrandId == 6)
  158. {
  159. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "6").Sum(m => m.TradeAmount) / 100;
  160. }
  161. else if (pos.BrandId == 7)
  162. {
  163. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "7" && m.TradeStatus == "00").Sum(m => m.TradeAmount) / 100;
  164. }
  165. pos.CreditTrade = CreditTrade;
  166. db.SaveChanges();
  167. StatTradeAmount(pos.PosSn);
  168. }
  169. if (AddActPrize == 1)
  170. {
  171. if (pos.BuyUserId == 0)
  172. {
  173. return "机具未划拨,无法补录激活奖励";
  174. }
  175. if (string.IsNullOrEmpty(pos.SeoKeyword) || pos.SeoKeyword == "0")
  176. {
  177. bool has = spdb.ActivateRecord.Any(m => m.SnNo == pos.PosSn);
  178. if (has)
  179. {
  180. SpModels.ActivateRecord chkAct = spdb.ActivateRecord.Where(m => m.SnNo == pos.PosSn).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SpModels.ActivateRecord();
  181. pos.SeoKeyword = chkAct.SeoTitle;
  182. db.SaveChanges();
  183. }
  184. }
  185. if (pos.BindingTime != null && pos.ActivationTime != null)
  186. {
  187. TimeSpan ts = pos.ActivationTime.Value - pos.BindingTime.Value;
  188. if (ts.TotalDays <= 30)
  189. {
  190. AddAct(pos.Id);
  191. }
  192. }
  193. }
  194. decimal CheckMoney = 1000;
  195. if (pos.BrandId == 6)
  196. {
  197. CheckMoney = 5000;
  198. }
  199. if (pos.BuyUserId > 0)
  200. {
  201. if (pos.CreditTrade < CheckMoney)
  202. {
  203. return "该机具贷记卡累计交易不足" + CheckMoney;
  204. }
  205. if (pos.BindingTime != null && pos.ActivationTime != null)
  206. {
  207. TimeSpan ts = pos.ActivationTime.Value - pos.BindingTime.Value;
  208. if (ts.TotalDays > 30)
  209. {
  210. return "机具划拨后已超过30天";
  211. }
  212. }
  213. if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0)
  214. {
  215. pos.ActivationState = 1;
  216. pos.ActivationTime = DateTime.Now;
  217. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  218. if (pos.ActivationState == 1 && mer != null)
  219. {
  220. mer.ActiveStatus = 1;
  221. mer.MerStandardDate = DateTime.Now;
  222. }
  223. }
  224. else if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 1)
  225. {
  226. if (AddActPrize == 1)
  227. {
  228. if (IsSend == 1)
  229. {
  230. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  231. {
  232. UserId = pos.BuyUserId, //接收创客
  233. MsgType = 2,
  234. Title = "补录成功通知", //标题
  235. Summary = "您的 " + kqProducts.Name + " SN:" + pos.PosSn + "已经成功补录,请查收。",
  236. CreateDate = DateTime.Now,
  237. }));
  238. }
  239. return "激活奖励已补录";
  240. }
  241. else
  242. {
  243. return "数据正常,无需补录";
  244. }
  245. }
  246. db.SaveChanges();
  247. return "success";
  248. }
  249. pos.UserId = user.Id;
  250. pos.BuyUserId = user.Id;
  251. if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0 && pos.BindingTime > DateTime.Now.AddDays(-30))
  252. {
  253. // pos.IsPurchase = 0;
  254. pos.ActivationState = 1;
  255. pos.ActivationTime = DateTime.Now;
  256. }
  257. PosMerchantInfo merchant = new PosMerchantInfo();
  258. if (!string.IsNullOrEmpty(MerNo))
  259. {
  260. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
  261. if (merchant == null)
  262. {
  263. int TopUserId = 0;
  264. if (!string.IsNullOrEmpty(user.ParentNav))
  265. {
  266. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  267. }
  268. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerNo == MerNo);
  269. if (bind == null)
  270. {
  271. return "该机具尚未绑定";
  272. }
  273. SpModels.Merchants Mer = spdb.Merchants.FirstOrDefault(m => m.MerNo == MerNo);
  274. if (Mer == null)
  275. {
  276. return "该机具尚未绑定";
  277. }
  278. PosMerchantInfo add = db.PosMerchantInfo.Add(new PosMerchantInfo()
  279. {
  280. CreateDate = Mer.CreateTime,
  281. UpdateDate = Mer.UpdateTime,
  282. TopUserId = TopUserId,
  283. MerUserType = user.MerchantType,
  284. BrandId = int.Parse(Mer.ProductType),
  285. SnStoreId = pos.StoreId,
  286. SnType = pos.PosSnType,
  287. UserId = user.Id,
  288. MgrName = Mer.AgentName,
  289. MerStatus = 1,
  290. KqSnNo = Mer.SnNo,
  291. KqMerNo = Mer.MerNo,
  292. MerIdcardNo = Mer.MerIdcardNo,
  293. MerRealName = Mer.MerRealName,
  294. MerchantMobile = Mer.MerMobile,
  295. MerchantName = Mer.MerName,
  296. MerchantNo = Mer.MerNo,
  297. }).Entity;
  298. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  299. if (forMerNo == null)
  300. {
  301. forMerNo = db.MachineForMerNo.Add(new MachineForMerNo()
  302. {
  303. MerNo = Mer.MerNo,
  304. SnId = pos.Id,
  305. }).Entity;
  306. }
  307. else
  308. {
  309. forMerNo.SnId = pos.Id;
  310. }
  311. }
  312. }
  313. else
  314. {
  315. if (pos.BindMerchantId > 0)
  316. {
  317. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  318. }
  319. else
  320. {
  321. return "找不到机具所属商户";
  322. }
  323. }
  324. merchant.UserId = user.Id;
  325. if (pos.ActivationState == 1)
  326. {
  327. merchant.ActiveStatus = 1;
  328. merchant.MerStandardDate = DateTime.Now;
  329. }
  330. if (pos.BindMerchantId == 0)
  331. {
  332. pos.BindMerchantId = merchant.Id;
  333. }
  334. db.SaveChanges();
  335. function.WriteLog(DateTime.Now.ToString() + "\n" + SysUserName + "\nMakerCode:" + MakerCode + ", PosSn:" + PosSn + ", MerNo:" + MerNo + ", AddActPrize:" + AddActPrize + ", IsPre:" + IsPre, "机具补录日志");
  336. return "success";
  337. }
  338. public IActionResult CheckMachine2(string right)
  339. {
  340. ViewBag.RightInfo = RightInfo;
  341. ViewBag.right = right;
  342. return View();
  343. }
  344. [HttpPost]
  345. public string CheckMachine2Do(string MakerCode, string PosSn, string MerNo = "", int AddActPrize = 0)
  346. {
  347. if (string.IsNullOrEmpty(MakerCode))
  348. {
  349. return "请输入创客编号";
  350. }
  351. if (string.IsNullOrEmpty(PosSn))
  352. {
  353. return "请输入机具SN";
  354. }
  355. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  356. if (pos == null)
  357. {
  358. return "机具SN不存在";
  359. }
  360. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  361. if (user == null)
  362. {
  363. return "创客编号不存在";
  364. }
  365. if (pos.BindingState == 0)
  366. {
  367. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == PosSn);
  368. if (bind != null)
  369. {
  370. pos.BindingState = 1;
  371. pos.BindingTime = bind.CreateTime;
  372. db.SaveChanges();
  373. MerNo = bind.MerNo;
  374. }
  375. }
  376. decimal CreditTrade = 0;
  377. bool check = spdb.TradeRecord.Any(m => m.TradeSnNo == pos.PosSn);
  378. if (check)
  379. {
  380. if (pos.BrandId == 1)
  381. {
  382. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M700")).Sum(m => m.TradeAmount) / 100;
  383. }
  384. else if (pos.BrandId == 2)
  385. {
  386. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "2").Sum(m => m.TradeAmount);
  387. }
  388. else if (pos.BrandId == 3)
  389. {
  390. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M900")).Sum(m => m.TradeAmount) / 100;
  391. }
  392. else if (pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 8 || pos.BrandId == 9)
  393. {
  394. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "4").Sum(m => m.TradeAmount) / 100;
  395. }
  396. else if (pos.BrandId == 6)
  397. {
  398. CreditTrade = spdb.TradeRecord.Where(m => m.TradeSnNo == pos.PosSn && m.ProductType == "6").Sum(m => m.TradeAmount) / 100;
  399. }
  400. pos.CreditTrade = CreditTrade;
  401. db.SaveChanges();
  402. StatTradeAmount(pos.PosSn);
  403. }
  404. if (AddActPrize == 1)
  405. {
  406. if (string.IsNullOrEmpty(pos.SeoKeyword) || pos.SeoKeyword == "0")
  407. {
  408. bool has = spdb.ActivateRecord.Any(m => m.SnNo == pos.PosSn);
  409. if (has)
  410. {
  411. SpModels.ActivateRecord chkAct = spdb.ActivateRecord.Where(m => m.SnNo == pos.PosSn).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SpModels.ActivateRecord();
  412. pos.SeoKeyword = chkAct.SeoTitle;
  413. db.SaveChanges();
  414. }
  415. }
  416. AddAct(pos.Id);
  417. }
  418. if (pos.BuyUserId > 0)
  419. {
  420. if (pos.CreditTrade < 1000)
  421. {
  422. // pos.IsPurchase = 99;
  423. db.SaveChanges();
  424. return "该机具贷记卡累计交易不足1000";
  425. }
  426. if (pos.CreditTrade >= 1000 && pos.ActivationState == 0)
  427. {
  428. // pos.IsPurchase = 0;
  429. pos.ActivationState = 1;
  430. pos.ActivationTime = DateTime.Now;
  431. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  432. if (pos.ActivationState == 1 && mer != null)
  433. {
  434. mer.ActiveStatus = 1;
  435. mer.MerStandardDate = DateTime.Now;
  436. RedisDbconn.Instance.Clear("PosMerchantInfo:" + mer.Id);
  437. }
  438. }
  439. else if (pos.CreditTrade >= 1000 && pos.ActivationState == 1)
  440. {
  441. if (AddActPrize == 1)
  442. {
  443. return "激活奖励已补录";
  444. }
  445. else
  446. {
  447. return "数据正常,无需补录";
  448. }
  449. }
  450. db.SaveChanges();
  451. RedisDbconn.Instance.Clear("PosMachinesTwo:" + pos.Id);
  452. return "success";
  453. }
  454. pos.UserId = user.Id;
  455. pos.BuyUserId = user.Id;
  456. // if (pos.CreditTrade < 1000)
  457. // {
  458. // pos.IsPurchase = 99;
  459. // }
  460. if (pos.CreditTrade >= 1000 && pos.ActivationState == 0)
  461. {
  462. // pos.IsPurchase = 0;
  463. pos.ActivationState = 1;
  464. pos.ActivationTime = DateTime.Now;
  465. }
  466. PosMerchantInfo merchant = new PosMerchantInfo();
  467. if (!string.IsNullOrEmpty(MerNo))
  468. {
  469. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
  470. if (merchant == null)
  471. {
  472. int TopUserId = 0;
  473. if (!string.IsNullOrEmpty(user.ParentNav))
  474. {
  475. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  476. }
  477. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerNo == MerNo);
  478. if (bind == null)
  479. {
  480. return "该机具尚未绑定";
  481. }
  482. SpModels.Merchants Mer = spdb.Merchants.FirstOrDefault(m => m.MerNo == MerNo);
  483. if (Mer == null)
  484. {
  485. return "该机具尚未绑定";
  486. }
  487. PosMerchantInfo add = db.PosMerchantInfo.Add(new PosMerchantInfo()
  488. {
  489. CreateDate = Mer.CreateTime,
  490. UpdateDate = Mer.UpdateTime,
  491. TopUserId = TopUserId,
  492. MerUserType = user.MerchantType,
  493. BrandId = int.Parse(Mer.ProductType),
  494. SnStoreId = pos.StoreId,
  495. SnType = pos.PosSnType,
  496. UserId = user.Id,
  497. MgrName = Mer.AgentName,
  498. MerStatus = 1,
  499. KqSnNo = Mer.SnNo,
  500. KqMerNo = Mer.MerNo,
  501. MerIdcardNo = Mer.MerIdcardNo,
  502. MerRealName = Mer.MerRealName,
  503. MerchantMobile = Mer.MerMobile,
  504. MerchantName = Mer.MerName,
  505. MerchantNo = Mer.MerNo,
  506. }).Entity;
  507. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  508. if (forMerNo == null)
  509. {
  510. forMerNo = db.MachineForMerNo.Add(new MachineForMerNo()
  511. {
  512. MerNo = Mer.MerNo,
  513. SnId = pos.Id,
  514. }).Entity;
  515. }
  516. else
  517. {
  518. forMerNo.SnId = pos.Id;
  519. }
  520. }
  521. }
  522. else
  523. {
  524. if (pos.BindMerchantId > 0)
  525. {
  526. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  527. }
  528. else
  529. {
  530. return "找不到机具所属商户";
  531. }
  532. }
  533. merchant.UserId = user.Id;
  534. if (pos.ActivationState == 1)
  535. {
  536. merchant.ActiveStatus = 1;
  537. merchant.MerStandardDate = DateTime.Now;
  538. }
  539. if (pos.BindMerchantId == 0)
  540. {
  541. pos.BindMerchantId = merchant.Id;
  542. }
  543. db.SaveChanges();
  544. RedisDbconn.Instance.Clear("PosMerchantInfo:" + merchant.Id);
  545. RedisDbconn.Instance.Clear("PosMachinesTwo:" + pos.Id);
  546. return "success";
  547. }
  548. public void AddAct(int posid = 0)
  549. {
  550. WebCMSEntities db = new WebCMSEntities();
  551. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.ActivationState == 1 && !string.IsNullOrEmpty(m.SeoKeyword) && m.Id == posid);
  552. if (pos != null)
  553. {
  554. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  555. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  556. if (merchant != null)
  557. {
  558. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  559. // if (otherInfo != null)
  560. // {
  561. // otherInfo.PrizeFlag1 = 1;
  562. // db.SaveChanges();
  563. string ParentNav = "";
  564. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  565. if (user != null)
  566. {
  567. int GetUserId = user.Id;
  568. ParentNav = user.ParentNav;
  569. int TopUserId = 1;
  570. if (!string.IsNullOrEmpty(ParentNav))
  571. {
  572. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  573. if (ParentNavList.Length > 1)
  574. {
  575. TopUserId = int.Parse(ParentNavList[1]);
  576. }
  577. else if (ParentNavList.Length == 1)
  578. {
  579. TopUserId = int.Parse(ParentNavList[0]);
  580. }
  581. }
  582. List<decimal> prizes = new List<decimal>();
  583. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  584. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 6 || pos.BrandId == 8 || pos.BrandId == 9)
  585. {
  586. ActPrize = ActPrize / 100;
  587. }
  588. if (ActPrize > 0)
  589. {
  590. if (ActPrize == 99)
  591. {
  592. ActPrize = 100;
  593. }
  594. else if (ActPrize == 199)
  595. {
  596. ActPrize = 200;
  597. }
  598. else if (ActPrize == 299)
  599. {
  600. ActPrize = 300;
  601. }
  602. else if (ActPrize == 249)
  603. {
  604. ActPrize = 260;
  605. }
  606. prizes.Add(ActPrize);
  607. if (pos.BrandId == 6 && pos.ActivationState == 1 && pos.CreditTrade >= 10000 && pos.BindingTime > DateTime.Now.AddDays(-60))
  608. {
  609. prizes.Add(40);
  610. }
  611. }
  612. else if (pos.BrandId == 6 && ActPrize == 0)
  613. {
  614. prizes.Add(50);
  615. }
  616. foreach (decimal prize in prizes)
  617. {
  618. int ChangeType = prize == 40 ? 12 : 0;
  619. bool check = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn && m.RewardAmount == prize);
  620. if (!check)
  621. {
  622. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  623. db.ActiveReward.Add(new ActiveReward()
  624. {
  625. CreateDate = DateTime.Now,
  626. UpdateDate = DateTime.Now,
  627. UserId = GetUserId, //创客
  628. MerchantId = pos.BindMerchantId, //商户
  629. StandardDate = pos.ActivationTime, //达标日期
  630. RewardAmount = prize, //奖励金额
  631. BrandId = pos.BrandId, //品牌
  632. UserNav = ParentNav, //创客父级
  633. DirectBuddyNo = merchant.UserId, //商户直属创客
  634. KqMerNo = merchant.KqMerNo, //渠道商户编号
  635. KqSnNo = pos.PosSn, //渠道SN号
  636. SnType = pos.PosSnType, //机具类型
  637. SnApplyUserId = pos.BuyUserId, //机具申请创客
  638. ActType = 0, //激活类型
  639. SnStoreId = pos.StoreId, //SN仓库
  640. RewardTips = "激活奖励", //奖励描述
  641. Remark = "激活奖励", //备注
  642. ActDate = pos.ActivationTime, //激活时间
  643. TopUserId = TopUserId, //顶级创客
  644. SeoTitle = machineUser.RealName,
  645. });
  646. db.SaveChanges();
  647. string IdBrand = GetUserId + "_" + pos.BrandId;
  648. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  649. if (userData == null)
  650. {
  651. userData = db.UserMachineData.Add(new UserMachineData()
  652. {
  653. IdBrand = IdBrand,
  654. }).Entity;
  655. db.SaveChanges();
  656. }
  657. userData.ActProfit += prize;
  658. db.SaveChanges();
  659. RedisDbconn.Instance.Set("UserMachineData:" + IdBrand, userData);
  660. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  661. if (account == null)
  662. {
  663. account = db.UserAccount.Add(new UserAccount()
  664. {
  665. Id = GetUserId,
  666. UserId = GetUserId,
  667. }).Entity;
  668. db.SaveChanges();
  669. }
  670. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  671. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  672. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  673. account.BalanceAmount += prize;
  674. account.TotalAmount += prize;
  675. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  676. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  677. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  678. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  679. {
  680. CreateDate = DateTime.Now,
  681. UpdateDate = DateTime.Now,
  682. UserId = GetUserId, //创客
  683. ChangeType = ChangeType, //变动类型
  684. ProductType = pos.BrandId, //产品类型
  685. ChangeAmount = prize, //变更金额
  686. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  687. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  688. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  689. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  690. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  691. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  692. }).Entity;
  693. db.SaveChanges();
  694. RedisDbconn.Instance.Set("UserAccount:" + GetUserId, account);
  695. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  696. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  697. // 激活奖励列表
  698. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  699. if (!dates.Contains(dateString))
  700. {
  701. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  702. }
  703. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, prize);
  704. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  705. if (!months.Contains(monthString))
  706. {
  707. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  708. }
  709. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, prize);
  710. // 激活奖励详情
  711. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  712. if (!actPrizeList.Contains(pos.BindMerchantId))
  713. {
  714. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  715. }
  716. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, prize);
  717. //收支明细
  718. RedisDbconn.Instance.AddList("UserAccountRecord:" + GetUserId + ":1:" + monthString, userAccountRecord);
  719. RedisDbconn.Instance.AddNumber("UserAccount:" + GetUserId + ":1:" + monthString, prize);
  720. }
  721. }
  722. }
  723. // }
  724. }
  725. }
  726. db.Dispose();
  727. }
  728. //重置交易额
  729. public void StatTradeAmount(string PosSn)
  730. {
  731. DateTime start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  732. if (DateTime.Now.Day == 1)
  733. {
  734. start = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00");
  735. }
  736. DateTime end = DateTime.Now;
  737. SycnSpTradeService.Instance.Start(PosSn, start, end);
  738. RedisDbconn.Instance.AddList("ResetTradeAmountList", PosSn);
  739. }
  740. #endregion
  741. #region 设置商户代理
  742. public IActionResult SetMerAgent(string right)
  743. {
  744. ViewBag.RightInfo = RightInfo;
  745. ViewBag.right = right;
  746. return View();
  747. }
  748. [HttpPost]
  749. public string SetMerAgentDo(string MakerCode, string PosSn, string MerNo, int IsSend = 0)
  750. {
  751. if (string.IsNullOrEmpty(MakerCode))
  752. {
  753. return "请输入创客编号";
  754. }
  755. if (string.IsNullOrEmpty(PosSn))
  756. {
  757. return "请输入机具SN";
  758. }
  759. if (string.IsNullOrEmpty(MerNo))
  760. {
  761. return "请输入商户编号";
  762. }
  763. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  764. KqProducts kqProducts = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId);
  765. if (pos == null)
  766. {
  767. return "机具SN不存在";
  768. }
  769. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  770. if (user == null)
  771. {
  772. return "创客编号不存在";
  773. }
  774. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
  775. if (merchant == null)
  776. {
  777. return "商户编号不存在";
  778. }
  779. pos.SeoTitle = user.Id.ToString();
  780. pos.UserId = user.Id;
  781. if (pos.BindMerchantId == 0)
  782. {
  783. pos.BindMerchantId = merchant.Id;
  784. if (IsSend == 1)
  785. {
  786. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  787. {
  788. UserId = pos.BuyUserId, //接收创客
  789. MsgType = 2,
  790. Title = "商户型代理设置成功", //标题
  791. Summary = "您的 " + kqProducts.Name + " SN:" + pos.PosSn + "已经成功给下级代理" + user.RealName + "" + MakerCode + ",设置为商户型代理,设置成功后的新增交易的分润归商户型代理所有",
  792. CreateDate = DateTime.Now,
  793. }));
  794. }
  795. }
  796. user.MerchantType = 1;
  797. merchant.UserId = user.Id;
  798. merchant.MerUserType = 1;
  799. db.SetMerchantTypeRecord.Add(new SetMerchantTypeRecord()
  800. {
  801. CreateDate = DateTime.Now,
  802. IsRecyc = (ulong)pos.IsPurchase,
  803. CreditAmount = pos.CreditTrade,
  804. PosSnType = pos.PosSnType,
  805. ActDate = pos.ActivationTime,
  806. BindDate = pos.BindingTime,
  807. ActStatus = (ulong)pos.ActivationState,
  808. BindStatus = (ulong)pos.BindingState,
  809. MerNo = MerNo,
  810. PosSn = pos.PosSn,
  811. Note = "后台设置商户型创客",
  812. ToUserId = user.Id,
  813. FromUserId = pos.BuyUserId,
  814. });
  815. db.SaveChanges();
  816. return "success";
  817. }
  818. #endregion
  819. #region 取消商户代理
  820. public IActionResult CancelMerAgent(string right)
  821. {
  822. ViewBag.RightInfo = RightInfo;
  823. ViewBag.right = right;
  824. return View();
  825. }
  826. [HttpPost]
  827. public string CancelMerAgentDo(string MakerCode, string PosSn, int IsSend = 0)
  828. {
  829. if (string.IsNullOrEmpty(MakerCode))
  830. {
  831. return "请输入创客编号";
  832. }
  833. if (string.IsNullOrEmpty(PosSn))
  834. {
  835. return "请输入机具SN";
  836. }
  837. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  838. KqProducts kqProducts = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId);
  839. if (pos == null)
  840. {
  841. return "机具SN不存在";
  842. }
  843. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  844. if (user == null)
  845. {
  846. return "创客编号不存在";
  847. }
  848. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == PosSn);
  849. pos.UserId = pos.BuyUserId;
  850. merchant.UserId = pos.BuyUserId;
  851. merchant.MerUserType = 0;
  852. user.MerchantType = 0;
  853. db.SaveChanges();
  854. if (pos.BindMerchantId == 0)
  855. {
  856. pos.BindMerchantId = merchant.Id;
  857. if (IsSend == 1)
  858. {
  859. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  860. {
  861. UserId = pos.BuyUserId, //接收创客
  862. MsgType = 2,
  863. Title = "商户型代理取消成功", //标题
  864. Summary = "您的 " + kqProducts.Name + " SN:" + pos.PosSn + "已给下级代理" + user.RealName + "" + MakerCode + "取消成功",
  865. CreateDate = DateTime.Now,
  866. }));
  867. }
  868. }
  869. string text = string.Format("取消商户代理,创客编号: " + user.MakerCode + ",机具SN:" + pos.PosSn + ",操作人:" + SysRealName + ",Time:" + DateTime.Now + "");
  870. function.WriteLog(text, "CancelMerAgent");//取消商户型代理日志
  871. return "success";
  872. }
  873. #endregion
  874. #region 查看SP原始数据日志
  875. public IActionResult SeeSpLog(string right)
  876. {
  877. ViewBag.RightInfo = RightInfo;
  878. ViewBag.right = right;
  879. return View();
  880. }
  881. #endregion
  882. #region 查询机具信息
  883. public IActionResult SeeSnDetail(string right)
  884. {
  885. ViewBag.RightInfo = RightInfo;
  886. ViewBag.right = right;
  887. return View();
  888. }
  889. [HttpPost]
  890. public string SeeSnDetailDo(string PosSn)
  891. {
  892. if (string.IsNullOrEmpty(PosSn))
  893. {
  894. return "请输入机具SN";
  895. }
  896. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  897. if (pos == null)
  898. {
  899. return "机具SN不存在";
  900. }
  901. if (pos.Status == -1)
  902. {
  903. Users posUsers = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  904. decimal Deposits = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  905. if (pos.BrandId != 2 && pos.BrandId != 7)
  906. {
  907. Deposits = Deposits / 100;
  908. }
  909. string DepositGets = "未领取";
  910. string DepositUsers = "";
  911. ActiveReward rewards = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.RewardAmount != 40) ?? new ActiveReward();
  912. if (rewards.Id > 0)
  913. {
  914. Users user = db.Users.FirstOrDefault(m => m.Id == rewards.UserId) ?? new Users();
  915. DepositGets = "已领取";
  916. DepositUsers = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  917. }
  918. string ActiveTimes = rewards.CreateDate == null ? "" : rewards.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  919. string results = "该机具为故障机" + "\n";
  920. results += "押金返现实际返现对象:" + DepositUsers + "\n";
  921. results += "押金:" + Deposits + "\n";
  922. results += "是否返现:" + DepositGets + ",领取时间:" + ActiveTimes + "\n";
  923. return results;
  924. }
  925. string merInfo = "";
  926. string merName = "";
  927. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  928. if (mer != null)
  929. {
  930. merInfo = mer.KqMerNo;
  931. merName = mer.MerchantName;
  932. }
  933. else
  934. {
  935. merInfo = "未找到商户";
  936. }
  937. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == pos.StoreId) ?? new StoreHouse();
  938. Users posUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  939. Users posSubUser = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  940. int TopUserId = 0;
  941. if (!string.IsNullOrEmpty(posUser.ParentNav))
  942. {
  943. string[] ParentNavs = posUser.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  944. if (ParentNavs.Length > 1)
  945. {
  946. TopUserId = int.Parse(ParentNavs[1]);
  947. }
  948. if (ParentNavs.Length == 1)
  949. {
  950. TopUserId = int.Parse(ParentNavs[0]);
  951. }
  952. }
  953. Users posTopUser = db.Users.FirstOrDefault(m => m.Id == TopUserId) ?? new Users();
  954. string bindResult = "", bindTime = "";
  955. if (pos.BindingState == 0)
  956. {
  957. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == pos.PosSn);
  958. if (bind != null)
  959. {
  960. if (bind.Status != 1)
  961. {
  962. bindResult = "执行中";
  963. }
  964. }
  965. else
  966. {
  967. bindResult = "未绑定";
  968. }
  969. }
  970. else
  971. {
  972. bindResult = "已绑定";
  973. bindTime = pos.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  974. }
  975. string activeResult = "", activeTime = "";
  976. if (pos.ActivationState == 0)
  977. {
  978. if (pos.CreditTrade < 1000)
  979. {
  980. activeResult = "未刷满1000";
  981. }
  982. else if (pos.TransferTime == null)
  983. {
  984. activeResult = "未激活, 需补录";
  985. }
  986. }
  987. else
  988. {
  989. activeResult = "已激活";
  990. activeTime = pos.ActivationTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  991. }
  992. string tradeAmount = pos.CreditTrade.ToString();
  993. decimal Deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  994. if (pos.BrandId != 2 && pos.BrandId != 7)
  995. {
  996. Deposit = Deposit / 100;
  997. }
  998. string DepositGet = "未领取";
  999. string DepositUser = "";
  1000. string DepositToUser = "";
  1001. decimal headAmount = 0;
  1002. decimal footAmount = 0;
  1003. ActiveReward reward = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.RewardAmount != 40) ?? new ActiveReward();
  1004. if (reward.Id > 0)
  1005. {
  1006. Users user = db.Users.FirstOrDefault(m => m.Id == reward.UserId) ?? new Users();
  1007. DepositGet = "已领取";
  1008. DepositUser = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1009. var fAmount = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.ActType == 0);
  1010. var lAmount = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.ActType == 1);
  1011. if (fAmount != null && lAmount != null)
  1012. {
  1013. headAmount = fAmount.RewardAmount;
  1014. footAmount = lAmount.RewardAmount;
  1015. }
  1016. }
  1017. string DepositGet6 = "未领取";
  1018. string DepositUser6 = "";
  1019. ActiveReward lisreward = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.BrandId == 6 && m.RewardAmount == 40) ?? new ActiveReward();
  1020. if (lisreward.Id > 0)
  1021. {
  1022. Users user = db.Users.FirstOrDefault(m => m.Id == lisreward.UserId) ?? new Users();
  1023. DepositGet6 = "已领取";
  1024. DepositUser6 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1025. }
  1026. string FluxGet6 = "未领取";
  1027. string FluxUser6 = "";
  1028. FluxProfitDetail flux = db.FluxProfitDetail.FirstOrDefault(m => m.SnNo == pos.PosSn) ?? new FluxProfitDetail();
  1029. if (flux.Id > 0)
  1030. {
  1031. Users user = db.Users.FirstOrDefault(m => m.Id == flux.UserId) ?? new Users();
  1032. FluxGet6 = "已领取(领取时间:" + flux.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + ")";
  1033. FluxUser6 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1034. }
  1035. Users touser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  1036. DepositToUser = "创客编号:" + touser.MakerCode + ",姓名:" + touser.RealName + ",手机号:" + touser.Mobile;
  1037. string OpenUser10 = "未领取";
  1038. string OpenUser20 = "未领取";
  1039. var OpenRewardDetail = db.OpenRewardDetail.Where(m => m.SnNo == pos.PosSn).ToList();
  1040. OpenRewardDetail openreward = OpenRewardDetail.FirstOrDefault(m => m.CreditRewardAmount == 20) ?? new OpenRewardDetail();
  1041. if (openreward.Id > 0)
  1042. {
  1043. Users user = db.Users.FirstOrDefault(m => m.Id == openreward.UserId) ?? new Users();
  1044. OpenUser20 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1045. }
  1046. openreward = OpenRewardDetail.FirstOrDefault(m => m.CreditRewardAmount == 10) ?? new OpenRewardDetail();
  1047. if (openreward.Id > 0)
  1048. {
  1049. Users user = db.Users.FirstOrDefault(m => m.Id == openreward.UserId) ?? new Users();
  1050. OpenUser10 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1051. }
  1052. MachineApply apply = db.MachineApply.FirstOrDefault(m => m.SwapSnExpand.Contains(pos.PosSn)) ?? new MachineApply();
  1053. Orders applyorder = db.Orders.FirstOrDefault(m => m.Id == apply.QueryCount) ?? new Orders();
  1054. MachineChangeDetail change = db.MachineChangeDetail.FirstOrDefault(m => m.OutSnNo == pos.PosSn) ?? new MachineChangeDetail();
  1055. string RecycFlag = pos.IsPurchase == 1 ? "已申请循环" : "未申请循环";
  1056. string SendStatus = applyorder.SendStatus == 1 ? "已发货" : "未发货";
  1057. string ApplyDate = apply.CreateDate == null ? "" : apply.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1058. string SendDate = applyorder.SendDate == null ? "" : applyorder.SendDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1059. string CreateDate = pos.CreateDate == null ? "" : pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1060. string TransferTime = pos.TransferTime == null ? "" : pos.TransferTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1061. string ActiveTime = reward.CreateDate == null ? "" : reward.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1062. string ActiveTime6 = lisreward.CreateDate == null ? "" : lisreward.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1063. string result = "";
  1064. result += "机具SN:" + pos.PosSn + "\n";
  1065. result += "商户编号:" + merInfo + ",商户姓名:" + merName + "\n";
  1066. result += "商户型机器/直拓机器:创客编号:" + posSubUser.MakerCode + ",姓名:" + posSubUser.RealName + ",手机号:" + posSubUser.Mobile + "\n";
  1067. if (change.Id > 0)
  1068. {
  1069. result += "换机来源机具SN:" + change.BackSnNo + "\n";
  1070. }
  1071. result += "机具所属人:创客编号:" + posUser.MakerCode + ",姓名:" + posUser.RealName + ",手机号:" + posUser.Mobile + "\n";
  1072. result += "机器持有人顶级:创客编号:" + posTopUser.MakerCode + ",姓名:" + posTopUser.RealName + ",手机号:" + posTopUser.Mobile + "\n";
  1073. result += "机器所属仓库:仓库编号:" + store.StoreNo + ",仓库名称:" + store.StoreName + "\n";
  1074. if (pos.PreUserId > 0)
  1075. {
  1076. Users smallStoreUser = db.Users.FirstOrDefault(m => m.Id == pos.PreUserId) ?? new Users();
  1077. PreSendStockDetail preSend = db.PreSendStockDetail.FirstOrDefault(m => m.SnId == pos.Id && m.ToUserId == pos.PreUserId && m.Status == 1) ?? new PreSendStockDetail();
  1078. string preSendDate = preSend.CreateDate == null ? "" : preSend.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1079. result += "机具所属小分仓:创客编号:" + smallStoreUser.MakerCode + ",姓名:" + smallStoreUser.RealName + ",手机号:" + smallStoreUser.Mobile + ",预发时间:" + preSendDate + "\n";
  1080. }
  1081. else
  1082. {
  1083. result += "机具所属小分仓:暂无小分仓\n";
  1084. }
  1085. result += "入库时间:" + CreateDate + "\n";
  1086. result += "划拨时间:" + TransferTime + "\n";
  1087. result += "绑定状态:" + bindResult + "(绑定时间:" + bindTime + ")\n";
  1088. result += "激活状态:" + activeResult + "(激活时间:" + activeTime + ")\n";
  1089. result += "贷记卡(总)交易额:" + tradeAmount + "\n";
  1090. result += "是否循环:" + RecycFlag + ",申请循环时间:" + ApplyDate + "\n";
  1091. result += "申请循环后的SN:" + applyorder.SnNos + "\n";
  1092. result += "是否发货:" + SendStatus + ",发货时间:" + SendDate + "\n";
  1093. result += "押金:" + Deposit + "\n";
  1094. result += "是否返现:" + DepositGet + ",领取时间:" + ActiveTime + "\n";
  1095. result += "返现金额:" + headAmount + " " + " " + footAmount + "\n";//TODO: 添加返现金额和额外奖励
  1096. result += "押金返现应返现对象:" + DepositToUser + "\n";
  1097. result += "押金返现实际返现对象:" + DepositUser + "\n";
  1098. if (pos.BrandId == 6)
  1099. {
  1100. result += "立刷10000奖励:" + DepositGet6 + ",领取时间:" + ActiveTime6 + "\n";
  1101. result += "立刷10000奖励发放人:" + DepositUser6 + "\n";
  1102. }
  1103. result += "流量费:" + FluxGet6 + "\n";
  1104. result += "流量费发放对象:" + FluxUser6 + "\n";
  1105. result += "开机奖20:" + OpenUser20 + "\n";
  1106. result += "开机奖10:" + OpenUser10 + "\n";
  1107. return result;
  1108. }
  1109. //解析金控日志
  1110. // public decimal CheckLog(DateTime startDate, string PosSn)
  1111. // {
  1112. // bool op = true;
  1113. // decimal result = 0;
  1114. // DateTime today = DateTime.Now;
  1115. // int i = 0;
  1116. // while (op)
  1117. // {
  1118. // DateTime check = DateTime.Parse(startDate.ToString("yyyy-MM-dd") + " 00:00:00").AddDays(i);
  1119. // if (check <= today)
  1120. // {
  1121. // string source = function.GetWebRequest("http://sp.kexiaoshuang.com/api/test/GetLog?date=" + check.Year + "-" + check.Month + "-" + check.Day);
  1122. // MatchCollection mc = Regex.Matches(source, "{.*?2022年飞天系列-小宝电签.*?}"); //激活
  1123. // foreach (Match sub in mc)
  1124. // {
  1125. // string content = sub.Value;
  1126. // JsonData jsonObj = JsonMapper.ToObject(content);
  1127. // decimal serviceAmount = decimal.Parse(jsonObj["serviceAmount"].ToString());
  1128. // string posSnCheck = jsonObj["posSn"].ToString();
  1129. // if (posSnCheck == PosSn && serviceAmount > 0)
  1130. // {
  1131. // result = serviceAmount / 100;
  1132. // op = false;
  1133. // }
  1134. // }
  1135. // }
  1136. // else
  1137. // {
  1138. // op = false;
  1139. // }
  1140. // i += 1;
  1141. // }
  1142. // return result;
  1143. // }
  1144. #endregion
  1145. #region 生成机具兑换码
  1146. public IActionResult MakeCoupons(string right)
  1147. {
  1148. ViewBag.RightInfo = RightInfo;
  1149. ViewBag.right = right;
  1150. return View();
  1151. }
  1152. [HttpPost]
  1153. public string MakeCouponsDo(int Kind, int Number)
  1154. {
  1155. DateTime checkTime = DateTime.Parse("1900-01-01");
  1156. string checkTimeString = RedisDbconn.Instance.Get<string>("MakerCouponsQueueCheckTime");
  1157. if (!string.IsNullOrEmpty(checkTimeString))
  1158. {
  1159. checkTime = DateTime.Parse(checkTimeString);
  1160. }
  1161. if (checkTime.AddHours(1) < DateTime.Now)
  1162. {
  1163. RedisDbconn.Instance.Set("MakerCouponsQueueCheckTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  1164. RedisDbconn.Instance.AddList("MakerCouponsQueue:" + Kind, Number);
  1165. return "生成程序已执行";
  1166. }
  1167. return "请稍后再试";
  1168. }
  1169. #endregion
  1170. #region 导出没有刷满1000的机具
  1171. public IActionResult ExportPos(string right)
  1172. {
  1173. ViewBag.RightInfo = RightInfo;
  1174. ViewBag.right = right;
  1175. return View();
  1176. }
  1177. [HttpPost]
  1178. public JsonResult ExportPosDo()
  1179. {
  1180. WebCMSEntities db = new WebCMSEntities();
  1181. List<PosMachinesTwo> poses = db.PosMachinesTwo.Where(m => m.CreditTrade < 1000 && m.BindingState == 1).ToList();
  1182. int total = poses.Count;
  1183. int index = 0;
  1184. List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
  1185. foreach (PosMachinesTwo pos in poses)
  1186. {
  1187. index += 1;
  1188. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
  1189. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  1190. Users puser = db.Users.FirstOrDefault(m => m.Id == user.ParentUserId) ?? new Users();
  1191. Users ppuser = db.Users.FirstOrDefault(m => m.Id == puser.ParentUserId) ?? new Users();
  1192. Dictionary<string, object> item = new Dictionary<string, object>();
  1193. string TopRealName = "";
  1194. string TopMakerCode = "";
  1195. string TopMobile = "";
  1196. string ParentNav = user.ParentNav;
  1197. if (!string.IsNullOrEmpty(ParentNav))
  1198. {
  1199. string[] navlist = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1200. if (navlist.Length > 1)
  1201. {
  1202. int TopId = int.Parse(function.CheckInt(navlist[1]));
  1203. Users tuser = db.Users.FirstOrDefault(m => m.Id == TopId) ?? new Users();
  1204. TopRealName = tuser.RealName;
  1205. TopMakerCode = tuser.MakerCode;
  1206. TopMobile = tuser.Mobile;
  1207. }
  1208. }
  1209. item.Add("TopRealName", TopRealName); //顶级姓名
  1210. item.Add("TopMakerCode", TopMakerCode); //顶级编号
  1211. item.Add("UpUpRealName", ppuser.RealName); //上上级姓名
  1212. item.Add("UpRealName", puser.RealName); //上级姓名
  1213. item.Add("RealName", user.RealName); //代理姓名
  1214. item.Add("MakerCode", user.MakerCode); //代理编号
  1215. item.Add("Mobile", user.Mobile); //代理手机号
  1216. item.Add("MerchantName", merchant.MerchantName); //客户姓名
  1217. item.Add("MerchantMobile", merchant.MerchantMobile); //客户电话
  1218. item.Add("OpenTime", pos.BindingTime == null ? "" : pos.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //开机日期
  1219. item.Add("Sn", pos.PosSn); //SN
  1220. list.Add(item);
  1221. RedisDbconn.Instance.Set("ExportPosCheck", index + " / " + total);
  1222. }
  1223. db.Dispose();
  1224. Dictionary<string, object> result = new Dictionary<string, object>();
  1225. result.Add("Status", "1");
  1226. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  1227. result.Add("Obj", list);
  1228. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  1229. ReturnFields.Add("TopRealName", "顶级姓名"); //顶级姓名
  1230. ReturnFields.Add("TopMakerCode", "顶级编号"); //顶级编号
  1231. ReturnFields.Add("UpUpRealName", "上上级姓名"); //上上级姓名
  1232. ReturnFields.Add("UpRealName", "上级姓名"); //上级姓名
  1233. ReturnFields.Add("RealName", "代理姓名"); //代理姓名
  1234. ReturnFields.Add("MakerCode", "代理编号"); //代理编号
  1235. ReturnFields.Add("Mobile", "代理手机号"); //代理手机号
  1236. ReturnFields.Add("MerchantName", "客户姓名"); //客户姓名
  1237. ReturnFields.Add("MerchantMobile", "客户电话"); //客户电话
  1238. ReturnFields.Add("OpenTime", "开机日期"); //开机日期
  1239. ReturnFields.Add("Sn", "SN"); //SN
  1240. result.Add("Fields", ReturnFields);
  1241. RedisDbconn.Instance.Set("ExportPosCheck", "finish");
  1242. return Json(result);
  1243. }
  1244. public string ExportPosCheck()
  1245. {
  1246. string content = RedisDbconn.Instance.Get<string>("ExportPosCheck");
  1247. if (content == "finish")
  1248. {
  1249. RedisDbconn.Instance.Clear("ExportPosCheck");
  1250. }
  1251. return content;
  1252. }
  1253. #endregion
  1254. #region 更换机具
  1255. public IActionResult ChangePos(string right)
  1256. {
  1257. ViewBag.RightInfo = RightInfo;
  1258. ViewBag.right = right;
  1259. return View();
  1260. }
  1261. [HttpPost]
  1262. public string ChangePosDo(string OldSn, string NewSn, string BackStoreNo, string OutStoreNo, string MerNo = "", int IsSend = 0)
  1263. {
  1264. if (string.IsNullOrEmpty(OldSn))
  1265. {
  1266. return "请输入原机具SN";
  1267. }
  1268. if (string.IsNullOrEmpty(NewSn))
  1269. {
  1270. return "请输入新机具SN";
  1271. }
  1272. string[] OldSnList = OldSn.Split('\n');
  1273. string[] NewSnList = NewSn.Split('\n');
  1274. if (OldSnList.Length != NewSnList.Length)
  1275. {
  1276. return "原机具SN数量和新机具SN数量不一致";
  1277. }
  1278. WebCMSEntities db = new WebCMSEntities();
  1279. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == OldSn);
  1280. StoreForCode storeForBack = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1281. StoreForCode storeForOut = db.StoreForCode.FirstOrDefault(m => m.Code == OutStoreNo) ?? new StoreForCode();
  1282. StoreHouse BackStore = db.StoreHouse.FirstOrDefault(m => m.Id == storeForBack.StoreId) ?? new StoreHouse();
  1283. StoreHouse OutStore = db.StoreHouse.FirstOrDefault(m => m.Id == storeForOut.StoreId) ?? new StoreHouse();
  1284. string ChangeNo = "BMC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3);
  1285. KqProducts oldPosBrand = new KqProducts();
  1286. KqProducts newPosBrand = new KqProducts();
  1287. MachineChange add = db.MachineChange.Add(new MachineChange()
  1288. {
  1289. CreateDate = DateTime.Now,
  1290. UpdateDate = DateTime.Now,
  1291. CreateMan = SysUserName + "-" + SysRealName,
  1292. ChangeNo = ChangeNo, //转换单号
  1293. UserId = 0, //创客
  1294. ChangeTime = DateTime.Now, //转换时间
  1295. BackStoreId = BackStore.Id, //退回仓库
  1296. BackStoreName = BackStore.StoreName, //退回仓库名称
  1297. Remark = "机具更换", //订单备注
  1298. BackStoreUserId = BackStore.UserId, //退回仓库归属人
  1299. OutProductType = int.Parse(OutStore.BrandId), //出库产品类型
  1300. OutProductName = OutStore.ProductName, //出库产品名称
  1301. OutStoreId = OutStore.Id, //出库仓库
  1302. OutStoreName = OutStore.StoreName, //出库仓库名称
  1303. OutStoreAreas = OutStore.Areas, //出库仓库所在地区
  1304. OutStoreAddress = OutStore.Address, //出库仓库地址
  1305. OutStoreManager = "", //出库仓库联系人
  1306. OutStoreManagerMobile = OutStore.ManageMobile, //出库仓库联系人手机号
  1307. }).Entity;
  1308. db.SaveChanges();
  1309. int BackProductType = 0;
  1310. string BackProductName = "";
  1311. string ChangeDeviceName = "";
  1312. string ChangeSnExpand = "";
  1313. for (int i = 0; i < OldSnList.Length; i++)
  1314. {
  1315. string OldSnNum = OldSnList[i];
  1316. string NewSnNum = NewSnList[i];
  1317. MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1318. MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1319. PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId);
  1320. if (oldpos == null)
  1321. {
  1322. return "原机具SN不正确";
  1323. }
  1324. // if (oldpos.BindingState != 1)
  1325. // {
  1326. // return "原机具未绑定";
  1327. // }
  1328. PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId);
  1329. if (newpos == null)
  1330. {
  1331. return "新机具SN不正确";
  1332. }
  1333. oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1334. newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
  1335. PosMerchantInfo merchant = new PosMerchantInfo();
  1336. if (!string.IsNullOrEmpty(MerNo))
  1337. {
  1338. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PosMerchantInfo();
  1339. }
  1340. else
  1341. {
  1342. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == oldpos.BindMerchantId) ?? new PosMerchantInfo();
  1343. }
  1344. newpos.BindMerchantId = merchant.Id;
  1345. newpos.BuyUserId = oldpos.BuyUserId;
  1346. newpos.UserId = oldpos.UserId;
  1347. newpos.RecycEndDate = oldpos.RecycEndDate;
  1348. newpos.ScanQrTrade = oldpos.ScanQrTrade;
  1349. newpos.DebitCardTrade = oldpos.DebitCardTrade;
  1350. newpos.CreditTrade = oldpos.CreditTrade;
  1351. newpos.PosSnType = oldpos.PosSnType;
  1352. newpos.TransferTime = oldpos.TransferTime;
  1353. newpos.IsPurchase = oldpos.IsPurchase;
  1354. newpos.BindingState = oldpos.BindingState;
  1355. newpos.ActivationState = oldpos.ActivationState;
  1356. newpos.BindingTime = oldpos.BindingTime;
  1357. newpos.ActivationTime = oldpos.ActivationTime;
  1358. newpos.IsFirst = oldpos.IsFirst;
  1359. // bool checkActReward = db.ActiveReward.Any(m => m.KqMerNo == merchant.KqMerNo);
  1360. // if (!checkActReward)
  1361. // {
  1362. newpos.SeoKeyword = oldpos.SeoKeyword;
  1363. newpos.PrizeParams = oldpos.PrizeParams;
  1364. newpos.LeaderUserId = oldpos.LeaderUserId;
  1365. // }
  1366. // oldpos.BuyUserId = 0;
  1367. // oldpos.UserId = 0;
  1368. oldpos.Status = -1;
  1369. //对应调整客小爽企业版数据
  1370. var posInfo = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == oldpos.Id);
  1371. if (posInfo != null)
  1372. {
  1373. posInfo.PosSn = newpos.PosSn;
  1374. posInfo.PosId = newpos.Id;
  1375. db.SaveChanges();
  1376. }
  1377. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo);
  1378. if (forMerNo != null)
  1379. {
  1380. forMerNo.SnId = newpos.Id;
  1381. }
  1382. merchant.KqSnNo = NewSn;
  1383. db.MachineChangeDetail.Add(new MachineChangeDetail()
  1384. {
  1385. CreateDate = DateTime.Now,
  1386. UpdateDate = DateTime.Now,
  1387. CreateMan = SysUserName + "-" + SysRealName,
  1388. ChangeNo = ChangeNo, //订单号
  1389. ChangeId = add.Id, //订单Id
  1390. BackProductType = oldpos.BrandId, //退回产品类型
  1391. BackProductName = oldPosBrand.Name, //退回产品名称
  1392. UserId = 0, //创客
  1393. BackSnNo = oldpos.PosSn, //设备SN编号
  1394. OutProductType = newpos.BrandId, //出库产品类型
  1395. OutProductName = newPosBrand.Name, //出库产品名称
  1396. OutSnNo = newpos.PosSn, //出库设备SN编号
  1397. OutSnType = newpos.PosSnType, //出库SN机具类型
  1398. Remark = "机具更换", //备注
  1399. BackSnType = oldpos.PosSnType, //退回SN机具类型
  1400. });
  1401. db.SaveChanges();
  1402. BackProductType = oldpos.BrandId;
  1403. BackProductName = newPosBrand.Name;
  1404. ChangeDeviceName = oldpos.DeviceName;
  1405. ChangeSnExpand += oldpos.PosSn + "\n";
  1406. PublicFunction.SycnMachineCount(oldpos.BuyUserId, oldpos.BrandId);
  1407. }
  1408. add.BackProductType = BackProductType; //退回产品类型
  1409. add.BackProductName = BackProductName; //退回产品名称
  1410. add.ChangeDeviceName = ChangeDeviceName; //转换机具名称
  1411. add.ChangeDeviceNum = OldSnList.Length; //转换机具数量
  1412. add.ChangeSnExpand = ChangeSnExpand; //机具SN
  1413. BackStore.LaveNum += OldSnList.Length; //退回仓库库存
  1414. OutStore.LaveNum -= OldSnList.Length; //出货仓库库存
  1415. db.SaveChanges();
  1416. db.Dispose();
  1417. if (IsSend == 1)
  1418. {
  1419. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1420. {
  1421. UserId = pos.BuyUserId, //接收创客
  1422. MsgType = 2,
  1423. Title = "换绑成功通知", //标题
  1424. Summary = "您的 " + oldPosBrand.Name + " SN:" + OldSn + "已经成功为客户换绑为" + newPosBrand.Name + "SN:" + NewSn + "请告知客户进行绑定。",
  1425. CreateDate = DateTime.Now,
  1426. }));
  1427. }
  1428. return "success";
  1429. }
  1430. #endregion
  1431. #region 坏机换新
  1432. public IActionResult ChangeBadPos(string right)
  1433. {
  1434. ViewBag.RightInfo = RightInfo;
  1435. ViewBag.right = right;
  1436. return View();
  1437. }
  1438. [HttpPost]
  1439. public string ChangeBadPosDo(string ChangeType, string MakerCode, string StoreCode, string OldSn, string NewSn, string FromStoreNo, string BackStoreNo, int IsSend = 0)
  1440. {
  1441. string[] OldSnList;
  1442. string[] NewSnList;
  1443. decimal amount = 0;
  1444. //创客坏机换新
  1445. if (ChangeType == "0")
  1446. {
  1447. if (string.IsNullOrEmpty(MakerCode))
  1448. {
  1449. return "请输入创客编号";
  1450. }
  1451. if (string.IsNullOrEmpty(OldSn))
  1452. {
  1453. return "请输入坏机SN";
  1454. }
  1455. if (string.IsNullOrEmpty(NewSn))
  1456. {
  1457. return "请输入新机SN";
  1458. }
  1459. if (string.IsNullOrEmpty(FromStoreNo))
  1460. {
  1461. return "请输入新机发货仓库编号";
  1462. }
  1463. if (string.IsNullOrEmpty(BackStoreNo))
  1464. {
  1465. return "请输入坏机退回仓库编号";
  1466. }
  1467. OldSnList = OldSn.Split('\n');
  1468. NewSnList = NewSn.Split('\n');
  1469. if (OldSnList.Length != NewSnList.Length)
  1470. {
  1471. return "原机具SN数量和新机具SN数量不一致";
  1472. }
  1473. for (int i = 0; i < OldSnList.Length; i++)
  1474. {
  1475. string OldSnNum = OldSnList[i];
  1476. string NewSnNum = NewSnList[i];
  1477. UserForMakerCode userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  1478. StoreForCode storeForCodeFrom = db.StoreForCode.FirstOrDefault(m => m.Code == FromStoreNo) ?? new StoreForCode();
  1479. if (storeForCodeFrom.StoreId == 0)
  1480. {
  1481. return "您输入的新机发货仓库编号不存在";
  1482. }
  1483. StoreForCode storeForCodeBack = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1484. if (storeForCodeBack.StoreId == 0)
  1485. {
  1486. return "您输入的坏机退回仓库编号不存在";
  1487. }
  1488. MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1489. MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1490. PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId && m.BindingState == 0 && m.BuyUserId == userForMakerCode.UserId) ?? new PosMachinesTwo();
  1491. if (oldpos.Id == 0)
  1492. {
  1493. return "您输入的坏机SN不存在或者不在" + MakerCode + "名下";
  1494. }
  1495. PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId && m.BindingState == 0) ?? new PosMachinesTwo();
  1496. if (newpos.Id == 0)
  1497. {
  1498. return "您输入的新机SN不存在";
  1499. }
  1500. StoreHouse storeFrom = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeFrom.StoreId);
  1501. if (Convert.ToInt32(storeFrom.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1502. {
  1503. return "您输入的新机发货仓库不符新机机具对应品牌";
  1504. }
  1505. if (newpos.StoreId != storeFrom.Id)
  1506. {
  1507. return "您输入的新机SN不在新机发货仓库";
  1508. }
  1509. StoreHouse storeBack = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeBack.StoreId);
  1510. if (Convert.ToInt32(storeBack.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1511. {
  1512. return "您输入的坏机退回仓库不符坏机机具对应品牌";
  1513. }
  1514. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1515. if (brandInfo.Name.Contains("大POS"))
  1516. {
  1517. amount = 300;
  1518. }
  1519. if (brandInfo.Name.Contains("电签"))
  1520. {
  1521. amount = 200;
  1522. }
  1523. // var storeUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == store.UserId) ?? new UserAccount();
  1524. var storeFromUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeFrom.UserId) ?? new UserAccount();
  1525. var storeBackUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeBack.UserId) ?? new UserAccount();
  1526. // storeUserAcount.ValidAmount += amount;
  1527. storeFromUserAcount.ValidAmount += amount;
  1528. storeBackUserAcount.ValidAmount -= amount;
  1529. newpos.BuyUserId = oldpos.BuyUserId;
  1530. newpos.UserId = oldpos.UserId;
  1531. newpos.RecycEndDate = oldpos.RecycEndDate;
  1532. newpos.ScanQrTrade = oldpos.ScanQrTrade;
  1533. newpos.DebitCardTrade = oldpos.DebitCardTrade;
  1534. newpos.CreditTrade = oldpos.CreditTrade;
  1535. newpos.PosSnType = oldpos.PosSnType;
  1536. newpos.TransferTime = oldpos.TransferTime;
  1537. newpos.IsPurchase = oldpos.IsPurchase;
  1538. newpos.BindingState = oldpos.BindingState;
  1539. newpos.ActivationState = oldpos.ActivationState;
  1540. newpos.BindingTime = oldpos.BindingTime;
  1541. newpos.ActivationTime = oldpos.ActivationTime;
  1542. newpos.IsFirst = oldpos.IsFirst;
  1543. newpos.SeoKeyword = oldpos.SeoKeyword;
  1544. newpos.PrizeParams = oldpos.PrizeParams;
  1545. newpos.LeaderUserId = oldpos.LeaderUserId;
  1546. newpos.StoreId = oldpos.StoreId;
  1547. newpos.UserId = oldpos.UserId;
  1548. newpos.PreUserId = oldpos.PreUserId;
  1549. oldpos.StoreId = storeBack.Id;
  1550. oldpos.BuyUserId = 0;
  1551. oldpos.UserId = 0;
  1552. oldpos.Status = -1;
  1553. db.SaveChanges();
  1554. BrokenMachineChange add = db.BrokenMachineChange.Add(new BrokenMachineChange()
  1555. {
  1556. CreateDate = DateTime.Now,
  1557. CreateMan = SysUserName + "-" + SysRealName,
  1558. ChangeNo = "BPC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3), //转换单号
  1559. OutStoreId = storeFrom.Id,
  1560. BackStoreId = storeBack.Id,
  1561. OutProductType = newpos.BrandId,
  1562. BackProductType = oldpos.BrandId,
  1563. UserId = userForMakerCode.UserId, //创客
  1564. }).Entity;
  1565. db.SaveChanges();
  1566. BrokenMachineChangeDetail adds = db.BrokenMachineChangeDetail.Add(new BrokenMachineChangeDetail()
  1567. {
  1568. CreateDate = DateTime.Now,
  1569. CreateMan = SysUserName + "-" + SysRealName,
  1570. OutSnNo = OldSnNum,
  1571. BackSnNo = NewSnNum,
  1572. ChangeNo = add.ChangeNo, //转换单号
  1573. ChangeId = add.Id, //转换记录Id
  1574. UserId = add.UserId, //创客
  1575. }).Entity;
  1576. db.SaveChanges();
  1577. var oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1578. if (IsSend == 1)
  1579. {
  1580. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1581. {
  1582. UserId = userForMakerCode.UserId, //接收创客
  1583. MsgType = 2,
  1584. Title = "坏机换新成功通知", //标题
  1585. Summary = "您的坏机 " + oldPosBrand.Name + " SN:" + OldSnNum + "已经成功为您换为" + oldPosBrand.Name + "SN:" + NewSnNum + "。",
  1586. CreateDate = DateTime.Now,
  1587. }));
  1588. }
  1589. }
  1590. }
  1591. //仓库坏机换新
  1592. else
  1593. {
  1594. if (string.IsNullOrEmpty(StoreCode))
  1595. {
  1596. return "请输入仓库编号";
  1597. }
  1598. if (string.IsNullOrEmpty(OldSn))
  1599. {
  1600. return "请输入坏机SN";
  1601. }
  1602. if (string.IsNullOrEmpty(NewSn))
  1603. {
  1604. return "请输入新机SN";
  1605. }
  1606. if (string.IsNullOrEmpty(FromStoreNo))
  1607. {
  1608. return "请输入新机发货仓库编号";
  1609. }
  1610. if (string.IsNullOrEmpty(BackStoreNo))
  1611. {
  1612. return "请输入坏机退回仓库编号";
  1613. }
  1614. OldSnList = OldSn.Split('\n');
  1615. NewSnList = NewSn.Split('\n');
  1616. if (OldSnList.Length != NewSnList.Length)
  1617. {
  1618. return "原机具SN数量和新机具SN数量不一致";
  1619. }
  1620. for (int i = 0; i < OldSnList.Length; i++)
  1621. {
  1622. string OldSnNum = OldSnList[i];
  1623. string NewSnNum = NewSnList[i];
  1624. StoreHouse store = new StoreHouse();
  1625. StoreForCode storeForBadCode = db.StoreForCode.FirstOrDefault(m => m.Code == StoreCode) ?? new StoreForCode();
  1626. if (storeForBadCode.StoreId > 0)
  1627. {
  1628. store = db.StoreHouse.FirstOrDefault(m => m.Id == storeForBadCode.StoreId);
  1629. }
  1630. StoreForCode storeForCodeFrom = db.StoreForCode.FirstOrDefault(m => m.Code == FromStoreNo) ?? new StoreForCode();
  1631. if (storeForCodeFrom.StoreId == 0)
  1632. {
  1633. return "您输入的新机发货仓库编号不存在";
  1634. }
  1635. StoreForCode storeForCodeBack = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1636. if (storeForCodeBack.StoreId == 0)
  1637. {
  1638. return "您输入的坏机退回仓库编号不存在";
  1639. }
  1640. MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1641. MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1642. PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId && m.BindingState == 0 && m.StoreId == storeForBadCode.StoreId) ?? new PosMachinesTwo();
  1643. if (oldpos.Id == 0)
  1644. {
  1645. return "您输入的坏机SN不存在或者不在仓库" + StoreCode + "中";
  1646. }
  1647. PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId && m.BindingState == 0) ?? new PosMachinesTwo();
  1648. if (newpos.Id == 0)
  1649. {
  1650. return "您输入的新机SN不存在";
  1651. }
  1652. StoreHouse storeFrom = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeFrom.StoreId);
  1653. if (Convert.ToInt32(storeFrom.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1654. {
  1655. return "您输入的新机发货仓库不符新机机具对应品牌";
  1656. }
  1657. if (newpos.StoreId != storeFrom.Id)
  1658. {
  1659. return "您输入的新机SN不在新机发货仓库";
  1660. }
  1661. StoreHouse storeBack = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeBack.StoreId);
  1662. if (Convert.ToInt32(storeBack.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1663. {
  1664. return "您输入的坏机退回仓库不符坏机机具对应品牌";
  1665. }
  1666. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1667. if (brandInfo.Name.Contains("大POS"))
  1668. {
  1669. amount = 300;
  1670. }
  1671. if (brandInfo.Name.Contains("电签"))
  1672. {
  1673. amount = 200;
  1674. }
  1675. var storeUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == store.UserId) ?? new UserAccount();
  1676. var storeFromUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeFrom.UserId) ?? new UserAccount();
  1677. var storeBackUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeBack.UserId) ?? new UserAccount();
  1678. // storeUserAcount.ValidAmount += amount;
  1679. storeFromUserAcount.ValidAmount += amount;
  1680. storeBackUserAcount.ValidAmount -= amount;
  1681. newpos.StoreId = oldpos.StoreId;
  1682. newpos.BuyUserId = oldpos.BuyUserId;
  1683. newpos.UserId = oldpos.UserId;
  1684. newpos.LeaderUserId = oldpos.LeaderUserId;
  1685. newpos.PreUserId = oldpos.PreUserId;
  1686. newpos.IsFirst = oldpos.IsFirst;
  1687. newpos.TransferTime = oldpos.TransferTime;
  1688. oldpos.StoreId = storeBack.Id;
  1689. oldpos.BuyUserId = 0;
  1690. oldpos.UserId = 0;
  1691. db.SaveChanges();
  1692. BrokenMachineChange add = db.BrokenMachineChange.Add(new BrokenMachineChange()
  1693. {
  1694. CreateDate = DateTime.Now,
  1695. CreateMan = SysUserName + "-" + SysRealName,
  1696. ChangeNo = "BPC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3), //转换单号
  1697. OutStoreId = storeFrom.Id,
  1698. BackStoreId = storeBack.Id,
  1699. OutProductType = newpos.BrandId,
  1700. BackProductType = oldpos.BrandId,
  1701. UserId = store.UserId, //创客
  1702. }).Entity;
  1703. db.SaveChanges();
  1704. BrokenMachineChangeDetail adds = db.BrokenMachineChangeDetail.Add(new BrokenMachineChangeDetail()
  1705. {
  1706. CreateDate = DateTime.Now,
  1707. CreateMan = SysUserName + "-" + SysRealName,
  1708. OutSnNo = OldSnNum,
  1709. BackSnNo = NewSnNum,
  1710. ChangeNo = add.ChangeNo, //转换单号
  1711. ChangeId = add.Id, //转换记录Id
  1712. UserId = add.UserId, //创客
  1713. }).Entity;
  1714. db.SaveChanges();
  1715. var oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1716. if (IsSend == 1)
  1717. {
  1718. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1719. {
  1720. UserId = store.UserId, //接收创客
  1721. MsgType = 2,
  1722. Title = "坏机换新成功通知", //标题
  1723. Summary = "您的坏机 " + oldPosBrand.Name + " SN:" + OldSnNum + "已经成功为您换为" + oldPosBrand.Name + "SN:" + NewSnNum + "。",
  1724. CreateDate = DateTime.Now,
  1725. }));
  1726. }
  1727. }
  1728. }
  1729. // for (int i = 0; i < OldSnList.Length; i++)
  1730. // {
  1731. // string OldSnNum = OldSnList[i];
  1732. // string NewSnNum = NewSnList[i];
  1733. // StoreForCode storeForCode = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1734. // MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1735. // MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1736. // StoreHouse oldStore = new StoreHouse();
  1737. // PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId && m.BindingState == 0);
  1738. // if (storeForCode.StoreId > 0)
  1739. // {
  1740. // StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCode.StoreId);
  1741. // if (Convert.ToInt32(store.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1742. // {
  1743. // return "您输入的仓库不符机具对应品牌";
  1744. // }
  1745. // oldStore = db.StoreHouse.FirstOrDefault(m => m.Id == oldpos.StoreId);
  1746. // oldpos.Status = -1;
  1747. // oldpos.StoreId = store.Id;
  1748. // }
  1749. // if (oldpos == null)
  1750. // {
  1751. // return "原机具SN不正确或者不符合换机条件";
  1752. // }
  1753. // PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId && m.BuyUserId == 0 && m.UserId == 0 && m.BindingState == 0);
  1754. // if (newpos == null)
  1755. // {
  1756. // return "新机具SN不正确或者不为仓库机";
  1757. // }
  1758. // if (oldpos.BrandId != newpos.BrandId)
  1759. // {
  1760. // return "新机具和旧机具品牌不相同";
  1761. // }
  1762. // var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
  1763. // if (brandInfo.Name.Contains("大POS"))
  1764. // {
  1765. // amount = 300;
  1766. // }
  1767. // if (brandInfo.Name.Contains("电签"))
  1768. // {
  1769. // amount = 200;
  1770. // }
  1771. // var storehouse = db.StoreHouse.FirstOrDefault(m => m.Id == newpos.StoreId) ?? new StoreHouse();
  1772. // storehouse.LaveNum -= 1;
  1773. // var userAcount = db.UserAccount.FirstOrDefault(m => m.Id == storehouse.UserId) ?? new UserAccount();
  1774. // if (oldpos.StoreId != newpos.StoreId)
  1775. // {
  1776. // userAcount.ValidAmount += amount;
  1777. // newpos.StoreId = oldpos.StoreId;
  1778. // newpos.BuyUserId = oldpos.BuyUserId;
  1779. // newpos.UserId = oldpos.UserId;
  1780. // newpos.LeaderUserId = oldpos.LeaderUserId;
  1781. // newpos.PreUserId = oldpos.PreUserId;
  1782. // newpos.IsFirst = oldpos.IsFirst;
  1783. // newpos.TransferTime = oldpos.TransferTime;
  1784. // }
  1785. // else
  1786. // {
  1787. // newpos.BuyUserId = oldpos.BuyUserId;
  1788. // newpos.UserId = oldpos.UserId;
  1789. // newpos.LeaderUserId = oldpos.LeaderUserId;
  1790. // newpos.PreUserId = oldpos.PreUserId;
  1791. // newpos.IsFirst = oldpos.IsFirst;
  1792. // newpos.TransferTime = oldpos.TransferTime;
  1793. // }
  1794. // db.SaveChanges();
  1795. // var oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1796. // var newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
  1797. // var UserId = 0;
  1798. // if (oldpos.BuyUserId == 0)
  1799. // {
  1800. // UserId = oldStore.UserId;
  1801. // }
  1802. // else
  1803. // {
  1804. // UserId = oldpos.BuyUserId;
  1805. // }
  1806. // BrokenMachineChange add = db.BrokenMachineChange.Add(new BrokenMachineChange()
  1807. // {
  1808. // CreateDate = DateTime.Now,
  1809. // CreateMan = SysUserName + "-" + SysRealName,
  1810. // ChangeNo = "BPC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3), //转换单号
  1811. // UserId = UserId, //创客
  1812. // }).Entity;
  1813. // db.SaveChanges();
  1814. // BrokenMachineChangeDetail adds = db.BrokenMachineChangeDetail.Add(new BrokenMachineChangeDetail()
  1815. // {
  1816. // CreateDate = DateTime.Now,
  1817. // CreateMan = SysUserName + "-" + SysRealName,
  1818. // OutSnNo = OldSnNum,
  1819. // BackSnNo = NewSnNum,
  1820. // ChangeNo = add.ChangeNo, //转换单号
  1821. // ChangeId = add.Id, //转换记录Id
  1822. // UserId = add.UserId, //创客
  1823. // }).Entity;
  1824. // db.SaveChanges();
  1825. // if (IsSend == 1)
  1826. // {
  1827. // RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1828. // {
  1829. // UserId = UserId, //接收创客
  1830. // MsgType = 2,
  1831. // Title = "坏机换新成功通知", //标题
  1832. // Summary = "您的坏机 " + oldPosBrand.Name + " SN:" + OldSn + "已经成功为您换为" + newPosBrand.Name + "SN:" + NewSn + "。",
  1833. // CreateDate = DateTime.Now,
  1834. // }));
  1835. // }
  1836. // }
  1837. return "success";
  1838. }
  1839. #endregion
  1840. #region 机具解绑
  1841. public IActionResult Unbind(string right)
  1842. {
  1843. ViewBag.RightInfo = RightInfo;
  1844. ViewBag.right = right;
  1845. return View();
  1846. }
  1847. [HttpPost]
  1848. public string UnbindDo(string PosSn, string MakerCode, string MerNo, int IsSend = 0)
  1849. {
  1850. if (string.IsNullOrEmpty(PosSn))
  1851. {
  1852. return "请输入机具SN";
  1853. }
  1854. if (string.IsNullOrEmpty(MakerCode))
  1855. {
  1856. return "请输入创客编号";
  1857. }
  1858. MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn);
  1859. if (forSnNo == null)
  1860. {
  1861. return "机具SN不存在";
  1862. }
  1863. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId);
  1864. if (pos == null)
  1865. {
  1866. return "机具SN不存在";
  1867. }
  1868. if (string.IsNullOrEmpty(MerNo) && pos.BrandId != 1 && pos.BrandId != 3)
  1869. {
  1870. return "请输入商户编号";
  1871. }
  1872. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  1873. if (forMakerCode == null)
  1874. {
  1875. return "创客不存在";
  1876. }
  1877. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
  1878. if (forMerNo == null && pos.BrandId != 1 && pos.BrandId != 3)
  1879. {
  1880. return "商户编号不存在";
  1881. }
  1882. if (pos.BuyUserId != forMakerCode.UserId)
  1883. {
  1884. return "机具SN和创客不匹配";
  1885. }
  1886. if (pos.ActivationState == 1)
  1887. {
  1888. return "机具已激活,不能解绑";
  1889. }
  1890. if (pos.BuyUserId == 0)
  1891. {
  1892. return "机具是仓库机,不支持解绑";
  1893. }
  1894. if (pos.BindingState == 0)
  1895. {
  1896. return "机具未绑定,不支持解绑";
  1897. }
  1898. if (pos.BindingTime < DateTime.Now.AddDays(-30))
  1899. {
  1900. return "机具绑定已超过30天,不支持解绑";
  1901. }
  1902. if (forMerNo != null)
  1903. {
  1904. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  1905. if (merchant != null)
  1906. {
  1907. //机具解绑删除客小爽企业版对应数据
  1908. var merInfo = db.BusinessPartnerMerchant.FirstOrDefault(m => m.MerchantId == merchant.Id);
  1909. if (merInfo != null)
  1910. {
  1911. db.BusinessPartnerMerchant.Remove(merInfo);
  1912. }
  1913. var posInfo = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == pos.Id);
  1914. if (posInfo != null)
  1915. {
  1916. db.BusinessPartnerPos.Remove(posInfo);
  1917. }
  1918. db.PosMerchantInfo.Remove(merchant);
  1919. }
  1920. db.MachineForMerNo.Remove(forMerNo);
  1921. db.SaveChanges();
  1922. }
  1923. db.MachineUnBind.Add(new MachineUnBind()
  1924. {
  1925. CreateDate = DateTime.Now,
  1926. SeoTitle = SysUserName + "-" + SysRealName,
  1927. MerchantId = pos.BindMerchantId,
  1928. AuditDate = DateTime.Now,
  1929. AuditDesc = "平台操作解绑",
  1930. AuditStatus = 1,
  1931. SnNo = pos.PosSn,
  1932. BrandId = pos.BrandId,
  1933. UserId = pos.BuyUserId,
  1934. ApplyNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  1935. });
  1936. pos.BindMerchantId = 0;
  1937. pos.BindingState = 0;
  1938. pos.BindingTime = DateTime.Parse("1900-01-01");
  1939. pos.UserId = pos.BuyUserId;
  1940. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  1941. db.SaveChanges();
  1942. PublicFunction.SycnMachineCount(pos.BuyUserId, pos.BrandId);
  1943. if (IsSend == 1)
  1944. {
  1945. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1946. {
  1947. UserId = pos.BuyUserId, //接收创客
  1948. MsgType = 2,
  1949. Title = "解绑成功通知", //标题
  1950. Summary = "" + PosSn + "机具已成功解绑,当前机具如需绑定同一客户请再2个工作日后再进行绑定,直接绑定会导致数控异常,相关损失由您本人承担。",
  1951. CreateDate = DateTime.Now,
  1952. }));
  1953. }
  1954. return "success";
  1955. }
  1956. #endregion
  1957. #region 通过券码修改盟主标记
  1958. public IActionResult ChangeSignQuan(string right)
  1959. {
  1960. ViewBag.RightInfo = RightInfo;
  1961. ViewBag.right = right;
  1962. return View();
  1963. }
  1964. [HttpPost]
  1965. public string ChangeSignQuanDo(string Coupons, string MakerCode)
  1966. {
  1967. if (string.IsNullOrEmpty(Coupons))
  1968. {
  1969. return "请输入机具券码,多个券码用回车分隔";
  1970. }
  1971. if (string.IsNullOrEmpty(MakerCode))
  1972. {
  1973. return "请输入创客编号";
  1974. }
  1975. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  1976. if (forMakerCode == null)
  1977. {
  1978. return "创客不存在";
  1979. }
  1980. string[] SnList = Coupons.Split('\n');
  1981. foreach (var item in SnList)
  1982. {
  1983. var coupos = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == item && m.LeaderUserId == 0);
  1984. if (coupos == null)
  1985. {
  1986. return "机具券不存在";
  1987. }
  1988. else
  1989. {
  1990. coupos.LeaderUserId = forMakerCode.UserId;
  1991. }
  1992. }
  1993. db.SaveChanges();
  1994. return "success";
  1995. }
  1996. #endregion
  1997. #region 通过机具SN修改盟主标记
  1998. public IActionResult ChangeSignSn(string right)
  1999. {
  2000. ViewBag.RightInfo = RightInfo;
  2001. ViewBag.right = right;
  2002. return View();
  2003. }
  2004. [HttpPost]
  2005. public string ChangeSignSnDo(string PosSn, string MakerCode)
  2006. {
  2007. if (string.IsNullOrEmpty(PosSn))
  2008. {
  2009. return "请输入机具SN,多个SN用回车分隔";
  2010. }
  2011. if (string.IsNullOrEmpty(MakerCode))
  2012. {
  2013. return "请输入创客编号";
  2014. }
  2015. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  2016. if (forMakerCode == null)
  2017. {
  2018. return "创客不存在";
  2019. }
  2020. string[] SnList = PosSn.Split('\n');
  2021. foreach (var item in SnList)
  2022. {
  2023. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == item && m.LeaderUserId == 0);
  2024. if (pos == null)
  2025. {
  2026. return "该机具不存在";
  2027. }
  2028. else
  2029. {
  2030. pos.LeaderUserId = forMakerCode.UserId;
  2031. }
  2032. }
  2033. db.SaveChanges();
  2034. return "success";
  2035. }
  2036. #endregion
  2037. #region 机具循环列表
  2038. /// <summary>
  2039. /// 根据条件查询机具循环列表
  2040. /// </summary>
  2041. /// <returns></returns>
  2042. public IActionResult Loop(string right, string SwapSnExpand, string SnNos)
  2043. {
  2044. ViewBag.RightInfo = RightInfo;
  2045. ViewBag.right = right;
  2046. ViewBag.SwapSnExpand = SwapSnExpand;
  2047. ViewBag.SnNos = SnNos;
  2048. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  2049. var machineApplie = db.MachineApply.Where(m => m.Status > -1).ToList();
  2050. var orders = db.Orders.Where(m => m.Status > -1).ToList();
  2051. if (!string.IsNullOrEmpty(SwapSnExpand))
  2052. {
  2053. var machineApplie1 = machineApplie.Where(m => m.Status > -1 && m.SwapSnExpand.Contains(SwapSnExpand)).ToList();
  2054. foreach (var item in machineApplie1)
  2055. {
  2056. var orders1 = orders.FirstOrDefault(m => m.Sort == item.Id && m.Id == item.QueryCount);
  2057. Dictionary<string, object> data = new Dictionary<string, object>();
  2058. data["ApplyNo"] = item.ApplyNo.ToString();
  2059. data["OrderNo"] = orders1.OrderNo.ToString();
  2060. data["MakerCode"] = item.ApplyNo.ToString();
  2061. data["RealName"] = item.ApplyNo.ToString();
  2062. data["ComeSn"] = item.ApplyNo.ToString();
  2063. data["SendSn"] = item.ApplyNo.ToString();
  2064. data["CreateDate"] = item.ApplyNo.ToString();
  2065. data["Status"] = item.ApplyNo.ToString();
  2066. dataList.Add(data);
  2067. }
  2068. }
  2069. if (!string.IsNullOrEmpty(SnNos))
  2070. {
  2071. var machineApplie1 = machineApplie.Where(m => m.Status > -1 && m.SwapSnExpand.Contains(SnNos)).ToList();
  2072. foreach (var item in machineApplie1)
  2073. {
  2074. var orders1 = orders.FirstOrDefault(m => m.Sort == item.Id && m.Id == item.QueryCount);
  2075. Dictionary<string, object> data = new Dictionary<string, object>();
  2076. data["ApplyNo"] = item.ApplyNo.ToString();
  2077. data["OrderNo"] = orders1.OrderNo.ToString();
  2078. data["MakerCode"] = item.ApplyNo.ToString();
  2079. data["RealName"] = item.ApplyNo.ToString();
  2080. data["ComeSn"] = item.ApplyNo.ToString();
  2081. data["SendSn"] = item.ApplyNo.ToString();
  2082. data["CreateDate"] = item.ApplyNo.ToString();
  2083. data["Status"] = item.ApplyNo.ToString();
  2084. dataList.Add(data);
  2085. }
  2086. }
  2087. return View(dataList);
  2088. // return View();
  2089. }
  2090. #endregion
  2091. #region 根据条件查询机具循环列表
  2092. /// <summary>
  2093. /// 机具循环列表
  2094. /// </summary>
  2095. /// <returns></returns>
  2096. public IActionResult LoopData(string SwapSnExpand, string SnNos, int page = 1, int limit = 30)
  2097. {
  2098. Dictionary<string, string> Fields = new Dictionary<string, string>();
  2099. Fields.Add("SwapSnExpand", "1"); //申请机具SN
  2100. Fields.Add("SnNos", "1"); //发货机具SN
  2101. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  2102. var machineApplie = db.MachineApply.Where(m => m.Status > -1).ToList();
  2103. var orders = db.Orders.Where(m => m.Status > -1).ToList();
  2104. if (!string.IsNullOrEmpty(SwapSnExpand))
  2105. {
  2106. var machineApplie1 = machineApplie.Where(m => m.Status > -1 && m.SwapSnExpand.Contains(SwapSnExpand)).ToList();
  2107. foreach (var item in machineApplie1)
  2108. {
  2109. var orders1 = orders.FirstOrDefault(m => m.Sort == item.Id && m.Id == item.QueryCount);
  2110. Dictionary<string, object> data = new Dictionary<string, object>();
  2111. data["ApplyNo"] = item.ApplyNo.ToString();
  2112. data["OrderNo"] = orders1.OrderNo.ToString();
  2113. data["MakerCode"] = item.ApplyNo.ToString();
  2114. data["RealName"] = item.ApplyNo.ToString();
  2115. data["ComeSn"] = item.ApplyNo.ToString();
  2116. data["SendSn"] = item.ApplyNo.ToString();
  2117. data["CreateDate"] = item.ApplyNo.ToString();
  2118. data["Status"] = item.ApplyNo.ToString();
  2119. dataList.Add(data);
  2120. }
  2121. }
  2122. else if (!string.IsNullOrEmpty(SnNos))
  2123. {
  2124. var machineApplie1 = machineApplie.Where(m => m.Status > -1 && m.SwapSnExpand.Contains(SnNos)).ToList();
  2125. foreach (var item in machineApplie1)
  2126. {
  2127. var orders1 = orders.FirstOrDefault(m => m.Sort == item.Id && m.Id == item.QueryCount);
  2128. Dictionary<string, object> data = new Dictionary<string, object>();
  2129. data["ApplyNo"] = item.ApplyNo.ToString();
  2130. data["OrderNo"] = orders1.OrderNo.ToString();
  2131. data["MakerCode"] = item.ApplyNo.ToString();
  2132. data["RealName"] = item.ApplyNo.ToString();
  2133. data["ComeSn"] = item.ApplyNo.ToString();
  2134. data["SendSn"] = item.ApplyNo.ToString();
  2135. data["CreateDate"] = item.ApplyNo.ToString();
  2136. data["Status"] = item.ApplyNo.ToString();
  2137. dataList.Add(data);
  2138. }
  2139. }
  2140. else
  2141. {
  2142. foreach (var item in machineApplie)
  2143. {
  2144. var orders1 = orders.FirstOrDefault(m => m.Sort == item.Id && m.Id == item.QueryCount) ?? new Orders();
  2145. var user = db.Users.FirstOrDefault(m => m.Id == item.UserId) ?? new Users();
  2146. Dictionary<string, object> data = new Dictionary<string, object>();
  2147. data["ApplyNo"] = item.ApplyNo.ToString();
  2148. if (!string.IsNullOrEmpty(orders1.OrderNo))
  2149. {
  2150. data["OrderNo"] = orders1.OrderNo.ToString();
  2151. }
  2152. if (string.IsNullOrEmpty(orders1.OrderNo))
  2153. {
  2154. data["OrderNo"] = "";
  2155. }
  2156. if (!string.IsNullOrEmpty(orders1.SnNos))
  2157. {
  2158. data["SendSn"] = orders1.SnNos.ToString();
  2159. }
  2160. if (string.IsNullOrEmpty(orders1.SnNos))
  2161. {
  2162. data["SendSn"] = "";
  2163. }
  2164. if (!string.IsNullOrEmpty(user.MakerCode))
  2165. {
  2166. data["MakerCode"] = user.MakerCode.ToString();
  2167. }
  2168. if (string.IsNullOrEmpty(user.MakerCode))
  2169. {
  2170. data["MakerCode"] = "";
  2171. }
  2172. if (!string.IsNullOrEmpty(user.RealName))
  2173. {
  2174. data["RealName"] = user.RealName.ToString();
  2175. }
  2176. if (string.IsNullOrEmpty(user.RealName))
  2177. {
  2178. data["RealName"] = "";
  2179. }
  2180. data["ComeSn"] = item.SwapSnExpand.ToString();
  2181. data["CreateDate"] = item.CreateDate.ToString();
  2182. if (item.ApplyNo.ToString() == "0")
  2183. {
  2184. data["Status"] = "待处理";
  2185. }
  2186. if (item.ApplyNo.ToString() == "1")
  2187. {
  2188. data["Status"] = "成功";
  2189. }
  2190. dataList.Add(data);
  2191. }
  2192. }
  2193. dataList = dataList.Skip((page - 1) * limit).Take(limit).ToList();//分页的重点
  2194. return Json(dataList);
  2195. }
  2196. #endregion
  2197. #region 查询创客直推奖
  2198. public IActionResult Prize100(string right)
  2199. {
  2200. ViewBag.RightInfo = RightInfo;
  2201. ViewBag.right = right;
  2202. return View();
  2203. }
  2204. [HttpPost]
  2205. public Dictionary<string, string> Prize100Do(string MakerCode)
  2206. {
  2207. Dictionary<string, string> dic = new Dictionary<string, string>();
  2208. string result = "";
  2209. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  2210. if (forMakerCode == null)
  2211. {
  2212. result += "创客编号不存在\n";
  2213. dic.Add("code", "1");
  2214. dic.Add("result", result);
  2215. return dic;
  2216. }
  2217. int OrderCount = db.Orders.Count(m => m.UserId == forMakerCode.UserId && m.PayStatus == 1 && m.TotalPrice == 600);
  2218. if (OrderCount == 0)
  2219. {
  2220. result += "该创客未下单\n";
  2221. dic.Add("code", "1");
  2222. dic.Add("result", result);
  2223. return dic;
  2224. }
  2225. if (OrderCount > 1)
  2226. {
  2227. result += "该创客已不是首笔下单\n";
  2228. dic.Add("code", "1");
  2229. dic.Add("result", result);
  2230. return dic;
  2231. }
  2232. Orders order = db.Orders.FirstOrDefault(m => m.UserId == forMakerCode.UserId && m.PayStatus == 1 && m.TotalPrice == 600) ?? new Orders();
  2233. if (order.PayStatus < 1)
  2234. {
  2235. result += "未付款\n";
  2236. dic.Add("code", "1");
  2237. dic.Add("result", result);
  2238. return dic;
  2239. }
  2240. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  2241. Users puser = db.Users.FirstOrDefault(m => m.Id == user.ParentUserId) ?? new Users();
  2242. result += "订单号:" + order.OrderNo + "\n";
  2243. result += "下单创客\n创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile + "\n";
  2244. result += "上级创客\n创客编号:" + puser.MakerCode + ",姓名:" + puser.RealName + ",手机号:" + puser.Mobile + "\n";
  2245. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  2246. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  2247. if (machineCount + ActiveCount >= 3)
  2248. {
  2249. string IsGet = "未发放";
  2250. int ToUserId = 0;
  2251. UserAccountRecord item = db.UserAccountRecord.FirstOrDefault(m => m.QueryCount == order.Id && m.ChangeType == 112);
  2252. if (item == null)
  2253. {
  2254. DateTime start = order.PayDate.Value.AddMinutes(-2);
  2255. DateTime end = order.PayDate.Value.AddMinutes(2);
  2256. item = db.UserAccountRecord.FirstOrDefault(m => m.UserId == puser.Id && m.ChangeType == 112 && m.ChangeAmount == 100 && m.CreateDate >= start && m.CreateDate <= end);
  2257. }
  2258. if (item != null)
  2259. {
  2260. IsGet = "已发放(发放时间:" + item.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + ")";
  2261. ToUserId = item.UserId;
  2262. dic.Add("code", "1");
  2263. }
  2264. else
  2265. {
  2266. dic.Add("id", order.Id.ToString());
  2267. dic.Add("code", "0");
  2268. }
  2269. Users touser = db.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Users();
  2270. result += "是否发放:" + IsGet + "\n";
  2271. result += "应该发放对象\n创客编号:" + puser.MakerCode + ",姓名:" + puser.RealName + ",手机号:" + puser.Mobile + "\n";
  2272. result += "实际发放对象\n创客编号:" + touser.MakerCode + ",姓名:" + touser.RealName + ",手机号:" + touser.Mobile + "\n";
  2273. }
  2274. else
  2275. {
  2276. result += "上级创客未满足条件\n购买机:" + machineCount + "台,激活机:" + ActiveCount + "台\n";
  2277. dic.Add("code", "1");
  2278. }
  2279. dic.Add("result", result);
  2280. return dic;
  2281. }
  2282. public string AddPrize100(int Id)
  2283. {
  2284. Orders order = db.Orders.FirstOrDefault(m => m.Id == Id);
  2285. if (order != null)
  2286. {
  2287. //机具券逻辑
  2288. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == Id);
  2289. if (pro != null)
  2290. {
  2291. //推荐下单奖励
  2292. if (pro.ProductId == 10 || pro.ProductId == 11)
  2293. {
  2294. int OrderId = order.Id;
  2295. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
  2296. if (!checkPrize)
  2297. {
  2298. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  2299. if (user != null)
  2300. {
  2301. bool directPrize = false; //直推奖标记
  2302. int ParentUserId = user.ParentUserId;
  2303. // List<int> proids = new List<int>();
  2304. // proids.Add(10);
  2305. // proids.Add(11);
  2306. while (ParentUserId > 0)
  2307. {
  2308. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  2309. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  2310. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  2311. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  2312. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  2313. {
  2314. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  2315. directPrize = true;
  2316. db.SaveChanges();
  2317. return "发放成功";
  2318. }
  2319. ParentUserId = puser.ParentUserId;
  2320. }
  2321. }
  2322. }
  2323. return "已发放,请勿重复发放";
  2324. }
  2325. }
  2326. }
  2327. return "操作失败";
  2328. }
  2329. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  2330. {
  2331. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  2332. if (account == null)
  2333. {
  2334. account = db.UserAccount.Add(new UserAccount()
  2335. {
  2336. Id = UserId,
  2337. UserId = UserId,
  2338. }).Entity;
  2339. db.SaveChanges();
  2340. }
  2341. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  2342. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  2343. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  2344. account.BalanceAmount += 100 * Count;
  2345. account.TotalAmount += 100 * Count;
  2346. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  2347. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  2348. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  2349. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  2350. {
  2351. CreateDate = DateTime.Now,
  2352. UpdateDate = DateTime.Now,
  2353. UserId = UserId, //创客
  2354. ChangeType = 112, //变动类型
  2355. ChangeAmount = 100 * Count, //变更金额
  2356. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  2357. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  2358. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  2359. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  2360. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  2361. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  2362. QueryCount = OrderId,
  2363. }).Entity;
  2364. db.SaveChanges();
  2365. }
  2366. #endregion
  2367. #region 设置立刷费率
  2368. public IActionResult LisFee(string right)
  2369. {
  2370. ViewBag.RightInfo = RightInfo;
  2371. ViewBag.right = right;
  2372. return View();
  2373. }
  2374. [HttpPost]
  2375. public string LisFeeDo(string PosSns, decimal Fee, string Kind, string Fix)
  2376. {
  2377. if (string.IsNullOrEmpty(PosSns))
  2378. {
  2379. return "请输入机具号";
  2380. }
  2381. string result = SysUserName + "设置\n";
  2382. string[] PosSnList = PosSns.Split('\n');
  2383. int total = PosSnList.Length;
  2384. int index = 0;
  2385. foreach (string SubPosSn in PosSnList)
  2386. {
  2387. index += 1;
  2388. string PosSn = SubPosSn.Replace("\r", "");
  2389. result += PosSn + ":" + Fee + "\n";
  2390. if (Kind == "M5")
  2391. {
  2392. result += SetLiSFee(PosSn, Fee) + "\n";
  2393. }
  2394. else if (Kind == "N1")
  2395. {
  2396. result += SetLiSDeposit(PosSn, (int)Fee);
  2397. }
  2398. RedisDbconn.Instance.Set("LisFeeProcess:" + SysId, index + " / " + total);
  2399. }
  2400. result += "\n\n";
  2401. function.WriteLog(result, "设置立刷费率");
  2402. return result;
  2403. }
  2404. public string LisFeeProcess()
  2405. {
  2406. return RedisDbconn.Instance.Get<string>("LisFeeProcess:" + SysId);
  2407. }
  2408. #region 立刷
  2409. string LiSAgentId = "50814834";
  2410. string LiSPrivateKey = "MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDTdeLXqQmfVJM/ATVnq+TdhgpKKPp3hXcuY2s9qrN1Uws7I7jqnq/gfoBHBWBu/PnQp/z65okBoz4PEpWzJYZDF4csgh3E45He46ialNzUlOPWv6yLzgQPHuneq2pXOb7dbN4kZx+g3nEbBjH+/P9xp+lU3HCwjNxIFSr3EyBAQf2HgvNqh70u9T13fGVx3Jtd2xbIKYhvGNPH9ZCGiZNiCy+m+F+TKywgKK/xify1KQgR6otVUOVIGYvPYlXAgLJM2/V54166KX1UUFMsKRj4Kt2j4GqoHzDfRk9ibYJ/8iSDGX4+WH5+8mq/raqA77VcAD+bMrt3Bm59XmaWtjZwDY1zh5sP5jEaxu4lde2xZD80kQ6p1ZydpqnjmWm6NKXSz8j85gRkTQqNALKNANnJb6f1Yy30WirnrhMkqU2dqBzzXxtk3k/2zv9BENjYs1igrRYpwX3QRx87aTOrIk/PO6cxMaWl249LPigah0OqVyIg5yOvtnUCdU12OqaVELIw5GnZ7rTRn3tAsJ+u1om+3XktbN3GUEpw5RWAT3UAsAU7Vxt9xPCss+JXF+ogXYgo9QpEjanXOfLo5KPv2CkBuzy87d85OId71gXpYcbp0zbKz5eXKZanVP9pwn43jqiPkqtam8N1TLCCmZ4t2cd+gpW2CX/+W7bmc25HJtRPuwIDAQABAoICAE7B9GfwTGYsYN8xewuOySH8n255jrffXMFJRBLmtgo3kNJI/sFbsxPA6dQJ253a2MxvFNj57gNPInkIqfrcOgJZC/52w9GmFJlYX16bnRgMEEPWhdRYOcjL8bz+SRANsNnVh3M1eWE4GBZBm3NmFVnmeRAjPlFzVUP/pk35sKReL6g+DJ0by0lnC6qqCTp8ON2fEQAh/++o5o/uFRMWwG7ezbO5hBdTGA8uOEGU7CspChlqKtTu2UaeRSVLcbbN+BqKPgZUud5KP6Scei3TbLpZdM1pczAE1fYCwORe9yDiNdkiMJPV798mOL4FV6DIJtsKOKZ8Ibg2DAbjlwABA4rQsgBUAA4Nj+skqLE7gGnJViHzKw87zve+v3CV9NEldHV7g2a78yKye489vlmmxcLj+Ln5O/Y3OQnDwsO8mVua5Ddyp26N88oB1wW62VLUn68ePclDd3Bdub/ayiBqFOqA0cTlqI5Dvz0rqrK9dkq4Ij295EK3OfYHCjuoHN+mJP0K0+o+5ItPXu7KWRz/Bf+KPneAAgvTRaDMww+HpfbcM6sxfiZqVE0yGux39i6wjEOCcIbfO8O7wgCEk4GE/98Z6/+lp1NEaQLHcu24FlUeCgThTVaJsmcY9aHB+jFn11yBiJ6+N+RYONWto0xLDyt71qM5KXEEngYambCK8D0JAoIBAQD5tYwpTNfcessxPcYAXkdJWNAP3/8kwz97rFi9gZYsxhZJ/JCU8TUCYlpJDf91mCe0i7uayGb3FbtzoWEwHxKL4DdlL47ZaaGA2dIVNMkxksXDehEE6/bVRAcFH1fT9mZr7uB2uX1YjesFRKEEd3UXpfKSPwtm3QUQz7P7WxzL0CWpfC11vAK2biCG72inE3MelCw/z1Y7M7oo2fyflObMPKkqyuy5NGtZ5t9xpMvLaZrOzJkYbwPaIqxIvISg9rxilZhjMhIFHOW8xM0lVHMEkk2NrwwsMihxFmDaUohZf0GiyYzNEEVBmVEhAZSOV5d1JlWYQStyvDas195/IHjNAoIBAQDYyakyJbbEhgcLSrmXKtJY584y6SYFE1mOZKKkON2EfDpAlwFmJYV26dWiPb77mUsf1HUU/aIT6sfZsrh6axvs0rqqp5ltIr6AzXnJce590EMJnASWLTAdhYXK6e5Y1W8AhylP4LzZk/dy4LOaF09pMi3ugogFFJAWV8j/cv2niUsCXg/I6cQz/Co6cwVhBzOnAMIjdvZbJ2ONJ1Jy/4yS17DBFRyHxMbGVPckXEVodjZ73TAfUQIM83Z8YILxyym2d2f7lvuCyT+ZjmpEJQztdxPO8qT9MaROs4c/qVjJZndoTZ3z4i2eke9Bf3Qcd6Bfy6jPq/r73hl0w0kBo4qnAoIBAQCC8fgZJEnCPcZWAR3WzZm2VdNtr7n9TkH5GazDW7QjsJC5pa8v27k/kdHko2mGAg84N1pe1Z5hDc1p2oO7l1/DaAXEkwbm7eSaTB9FrZOf4mbAlj9chFzBA1ZnEkOdTIXzLaYZvDWF/Y30CkQhsn79srEcjeDtxRNF+JVaRzI3kuRdF1hDeB84CVIXUG5hxupEa9E0eQawLvT9PSqjiJLrIRhY72bpzBpLX61my9NuzOc4cMQLsxOjDTGpRuqJ+jlZvTbXNceLVNcIgW9OlxVlnU778IKCnp7khqatdkA4JmqF8gla86BNwRdxJoH/juFGtp6GJA6jsR0+0EjGA4I1AoIBAGl2ITNeGX2dXgvmPEcH8/CARFWMCozpjuWwm1acu1baQFRvoaSZmEXzF5SnySMIvf0sFBE0GgbqbnK/ePNE6/7XvSuMAR2Qi3CJhTe0jzrxd8tS9QBB12hlLmM6TlM08Z0mI7jSMcz/KJ/gJD9QG7enS6T+J0GhcT8NUaDDxhYcujram384CdToVXq9vd4n7L7PpaIvG0XY8ZsXiAqduzssZrZnmMuLtM+DQf+US8e3ymWTgveZJxA52GhFYy5RkWdMy28fUheb467GE3g3NgTHqwFAFXd/ENmgiKk1rxW7HVfGepLMczjy8ZvBbzDyGTjBEMyGc8DXmTauUfPUJX0CggEBAKEWyNHhEBcEfW6ppPYsaKTlIquA2Chr15KsZcSFOMIcsF/6D7TDZg2NPDYFmlALF/cdqDjRIAP/v5IMRiu3UtHyfDbRyEHx0prvucFMY0HyX9VOJnticwTb39awHi/uFJLs7txERw89eJFcLaxRRRRSpPq3PCalGFpC2i8HXsPnv3gyt+bfE92h6E2YTyP41NYmcuuXkyWM83s11YSC9ZgeilbJuvdajePHrtIAxy8cny+yTBcbnJnWw1yHtzWCA5FAw5OWSaeotbWaFEAHaOW1VG9e1I54pgmVTc5n+WixsgH1fpiz04ojlWRWRyP7hqS+GbAyGgg00KyErRrPcwc=";
  2411. string LiSReqUrl = "https://openapi.jlpay.com/access/charge/"; //请求url
  2412. public string LiSSign(string data)
  2413. {
  2414. var rsa = RSA.Create();
  2415. var key = Convert.FromBase64String(LiSPrivateKey);
  2416. var content = Encoding.UTF8.GetBytes(data);
  2417. rsa.ImportPkcs8PrivateKey(key, out _);
  2418. var result = rsa.SignData(content, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  2419. return Convert.ToBase64String(result);
  2420. }
  2421. // 设置费率
  2422. public string SetLiSFee(string sn, decimal serviceFee)
  2423. {
  2424. Dictionary<string, object> dic = new Dictionary<string, object>();
  2425. dic.Add("agentId", LiSAgentId);
  2426. dic.Add("deviceSn", sn);
  2427. List<Dictionary<string, object>> feeList = new List<Dictionary<string, object>>();
  2428. Dictionary<string, object> item = new Dictionary<string, object>();
  2429. item.Add("feeCalcType", "M5");
  2430. item.Add("fixed", "0");
  2431. item.Add("rate", serviceFee);
  2432. feeList.Add(item);
  2433. dic.Add("feeList", feeList);
  2434. dic.Add("signMethod", "02");
  2435. string content = LiSAgentId + sn + "M50" + serviceFee + "02";
  2436. string signstr = LiSSign(content);
  2437. dic.Add("signData", signstr);
  2438. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  2439. string result = function.PostWebRequest(LiSReqUrl + "FEE003", req, new Dictionary<string, string>(), "application/json");
  2440. return result;
  2441. }
  2442. // 设置押金
  2443. public string SetLiSDeposit(string sn, int serviceFee)
  2444. {
  2445. serviceFee = serviceFee * 100;
  2446. Dictionary<string, object> dic = new Dictionary<string, object>();
  2447. dic.Add("agentId", LiSAgentId);
  2448. dic.Add("deviceSn", sn);
  2449. List<Dictionary<string, object>> feeList = new List<Dictionary<string, object>>();
  2450. Dictionary<string, object> item = new Dictionary<string, object>();
  2451. item.Add("feeCalcType", "N1");
  2452. item.Add("fixed", "1");
  2453. item.Add("rate", serviceFee);
  2454. feeList.Add(item);
  2455. dic.Add("feeList", feeList);
  2456. dic.Add("signMethod", "02");
  2457. string content = LiSAgentId + sn + "N11" + serviceFee + "02";
  2458. string signstr = LiSSign(content);
  2459. dic.Add("signData", signstr);
  2460. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  2461. string result = function.PostWebRequest(LiSReqUrl + "FEE003", req, new Dictionary<string, string>(), "application/json");
  2462. return result;
  2463. }
  2464. #endregion
  2465. #endregion
  2466. #region 查询商户型创客设置信息
  2467. public IActionResult SeeMerchantType(string right)
  2468. {
  2469. ViewBag.RightInfo = RightInfo;
  2470. ViewBag.right = right;
  2471. return View();
  2472. }
  2473. [HttpPost]
  2474. public string SeeMerchantTypeDo(string PosSn)
  2475. {
  2476. if (string.IsNullOrEmpty(PosSn))
  2477. {
  2478. return "请输入机具SN或者创客编号";
  2479. }
  2480. SetMerchantTypeRecord pos = new SetMerchantTypeRecord();
  2481. if (PosSn.StartsWith("K"))
  2482. {
  2483. UserForMakerCode userfor = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == PosSn) ?? new UserForMakerCode();
  2484. bool check = db.SetMerchantTypeRecord.Any(m => m.ToUserId == userfor.UserId);
  2485. if (check)
  2486. {
  2487. pos = db.SetMerchantTypeRecord.Where(m => m.ToUserId == userfor.UserId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SetMerchantTypeRecord();
  2488. }
  2489. }
  2490. else
  2491. {
  2492. bool check = db.SetMerchantTypeRecord.Any(m => m.PosSn == PosSn);
  2493. if (check)
  2494. {
  2495. pos = db.SetMerchantTypeRecord.Where(m => m.PosSn == PosSn).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SetMerchantTypeRecord();
  2496. }
  2497. }
  2498. if (pos.Id == 0)
  2499. {
  2500. return "查无信息";
  2501. }
  2502. Users setUser = db.Users.FirstOrDefault(m => m.Id == pos.FromUserId) ?? new Users();
  2503. Users toUser = db.Users.FirstOrDefault(m => m.Id == pos.ToUserId) ?? new Users();
  2504. string CreateDate = pos.CreateDate == null ? "" : pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  2505. string bindTime = pos.BindDate == null ? "" : pos.BindDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  2506. string activeTime = pos.ActDate == null ? "" : pos.ActDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  2507. string bindResult = pos.BindStatus == 1 ? "已绑定" : "未绑定";
  2508. string activeResult = pos.ActStatus == 1 ? "已激活" : "未激活";
  2509. string RecycFlag = pos.IsRecyc == 1 ? "已循环" : "未循环";
  2510. string result = "";
  2511. result += "机具SN:" + pos.PosSn + "\n";
  2512. result += "操作人:创客编号:" + setUser.MakerCode + ",姓名:" + setUser.RealName + ",手机号:" + setUser.Mobile + "\n";
  2513. result += "操作对象:创客编号:" + toUser.MakerCode + ",姓名:" + toUser.RealName + ",手机号:" + toUser.Mobile + "\n";
  2514. result += "商户编号:" + pos.MerNo + "\n";
  2515. result += "操作时间:" + pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
  2516. result += "绑定状态:" + bindResult + "(绑定时间:" + bindTime + ")\n";
  2517. result += "激活状态:" + activeResult + "(激活时间:" + activeTime + ")\n";
  2518. result += "贷记卡交易额:" + pos.CreditAmount + "\n";
  2519. result += "是否循环:" + RecycFlag + "\n";
  2520. return result;
  2521. }
  2522. #endregion
  2523. }
  2524. }