SysToolsController.cs 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762
  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.Security.Cryptography.X509Certificates;
  21. using System.Text;
  22. using System.Net;
  23. using System.Net.Security;
  24. using System.IO;
  25. namespace MySystem.Areas.Admin.Controllers
  26. {
  27. [Area("Admin")]
  28. [Route("Admin/[controller]/[action]")]
  29. public class SysToolsController : BaseController
  30. {
  31. public SysToolsController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
  32. {
  33. }
  34. #region 关联上下级
  35. public IActionResult RelationForUser(string right)
  36. {
  37. ViewBag.RightInfo = RightInfo;
  38. ViewBag.right = right;
  39. return View();
  40. }
  41. [HttpPost]
  42. public string RelationForUserDo(string MakerCode, string ParentMakerCode, int IsTeam = 1)
  43. {
  44. if (string.IsNullOrEmpty(MakerCode))
  45. {
  46. return "请输入创客编号";
  47. }
  48. if (string.IsNullOrEmpty(ParentMakerCode))
  49. {
  50. return "请输入上级创客编号";
  51. }
  52. MakerCode = MakerCode.ToUpper();
  53. ParentMakerCode = ParentMakerCode.ToUpper();
  54. Users puser = db.Users.FirstOrDefault(m => m.MakerCode == ParentMakerCode);
  55. if (puser != null)
  56. {
  57. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  58. if (user != null)
  59. {
  60. user.ParentUserId = puser.Id;
  61. user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
  62. if (IsTeam == 1)
  63. {
  64. RelationForUserSub(db, user.Id, user);
  65. }
  66. db.SaveChanges();
  67. function.WriteLog(DateTime.Now.ToString() + ":下级:" + MakerCode + ", 上级:" + ParentMakerCode, "换线日志");
  68. return "success";
  69. }
  70. return "创客编号不存在";
  71. }
  72. return "上级创客编号不存在";
  73. }
  74. private void RelationForUserSub(WebCMSEntities dbnew, int ParentUserId, Users puser)
  75. {
  76. var subusers = dbnew.Users.Select(m => new { m.Id, m.ParentUserId, m.ParentNav }).Where(m => m.ParentUserId == ParentUserId).ToList();
  77. foreach (var subuser in subusers)
  78. {
  79. Users user = dbnew.Users.FirstOrDefault(m => m.Id == subuser.Id);
  80. if (user != null)
  81. {
  82. user.ParentUserId = puser.Id;
  83. user.ParentNav = puser.ParentNav + "," + puser.Id + ",";
  84. RelationForUserSub(dbnew, user.Id, user);
  85. }
  86. }
  87. }
  88. #endregion
  89. #region 机具补录
  90. public IActionResult CheckMachine(string right)
  91. {
  92. ViewBag.RightInfo = RightInfo;
  93. ViewBag.right = right;
  94. return View();
  95. }
  96. [HttpPost]
  97. public string CheckMachineDo(string MakerCode, string PosSn, string MerNo = "", int AddActPrize = 0, int IsPre = 0, int IsSend = 0)
  98. {
  99. if (string.IsNullOrEmpty(MakerCode))
  100. {
  101. return "请输入创客编号";
  102. }
  103. if (string.IsNullOrEmpty(PosSn))
  104. {
  105. return "请输入机具SN";
  106. }
  107. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  108. KqProducts kqProducts = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId);
  109. if (pos == null)
  110. {
  111. return "机具SN不存在";
  112. }
  113. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  114. if (user == null)
  115. {
  116. return "创客编号不存在";
  117. }
  118. if (IsPre == 1)
  119. {
  120. if (pos.BuyUserId == 0)
  121. {
  122. pos.BuyUserId = user.Id;
  123. }
  124. if (pos.UserId == 0)
  125. {
  126. pos.UserId = user.Id;
  127. }
  128. }
  129. if (pos.BindingState == 0)
  130. {
  131. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == PosSn);
  132. if (bind != null)
  133. {
  134. pos.BindingState = 1;
  135. pos.BindingTime = bind.CreateTime;
  136. db.SaveChanges();
  137. MerNo = bind.MerNo;
  138. }
  139. }
  140. decimal CreditTrade = 0;
  141. string BindingTime = pos.BindingTime == null ? DateTime.Now.AddMonths(-1).ToString("yyyyMM") : pos.BindingTime.Value.ToString("yyyyMM");
  142. string SpTradeRecordId = function.ReadInstance("/PublicParams/SpTradeRecordId" + BindingTime + ".txt");
  143. int StartId = int.Parse(function.CheckInt(SpTradeRecordId));
  144. bool check = spdb.TradeRecord.Any(m => m.Id > StartId && m.TradeSnNo == pos.PosSn);
  145. if (check)
  146. {
  147. if (pos.BrandId == 1)
  148. {
  149. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M700")).Sum(m => m.TradeAmount) / 100;
  150. }
  151. else if (pos.BrandId == 2)
  152. {
  153. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "2").Sum(m => m.TradeAmount);
  154. }
  155. else if (pos.BrandId == 3)
  156. {
  157. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M900")).Sum(m => m.TradeAmount) / 100;
  158. }
  159. else if (pos.BrandId == 4 || pos.BrandId == 5)
  160. {
  161. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "4").Sum(m => m.TradeAmount) / 100;
  162. }
  163. else if (pos.BrandId == 6)
  164. {
  165. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "6").Sum(m => m.TradeAmount) / 100;
  166. }
  167. else if (pos.BrandId == 7)
  168. {
  169. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "7" && m.TradeStatus == "00").Sum(m => m.TradeAmount) / 100;
  170. }
  171. else if (pos.BrandId == 8)
  172. {
  173. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "8").Sum(m => m.TradeAmount) / 100;
  174. }
  175. else if (pos.BrandId == 9)
  176. {
  177. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "9").Sum(m => m.TradeAmount) / 100;
  178. }
  179. pos.CreditTrade = CreditTrade;
  180. db.SaveChanges();
  181. StatTradeAmount(pos.PosSn, StartId);
  182. }
  183. if (AddActPrize == 1)
  184. {
  185. if (pos.BuyUserId == 0)
  186. {
  187. return "机具未划拨,无法补录激活奖励";
  188. }
  189. if (string.IsNullOrEmpty(pos.SeoKeyword) || pos.SeoKeyword == "0")
  190. {
  191. bool has = spdb.ActivateRecord.Any(m => m.SnNo == pos.PosSn);
  192. if (has)
  193. {
  194. SpModels.ActivateRecord chkAct = spdb.ActivateRecord.Where(m => m.SnNo == pos.PosSn).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SpModels.ActivateRecord();
  195. pos.SeoKeyword = chkAct.SeoTitle;
  196. db.SaveChanges();
  197. }
  198. }
  199. if (pos.BindingTime != null && pos.ActivationTime != null)
  200. {
  201. TimeSpan ts = pos.ActivationTime.Value - pos.BindingTime.Value;
  202. if (ts.TotalDays <= 30 || SysUserName == "admin")
  203. {
  204. AddAct(pos.Id);
  205. }
  206. }
  207. }
  208. decimal CheckMoney = 1000;
  209. if (pos.BrandId == 6)
  210. {
  211. CheckMoney = 5000;
  212. }
  213. if (pos.BuyUserId > 0)
  214. {
  215. if (pos.CreditTrade < CheckMoney)
  216. {
  217. return "该机具贷记卡累计交易不足" + CheckMoney;
  218. }
  219. if (pos.BindingTime != null && pos.ActivationTime != null)
  220. {
  221. TimeSpan ts = pos.ActivationTime.Value - pos.BindingTime.Value;
  222. if (ts.TotalDays > 30 && SysUserName != "admin")
  223. {
  224. return "机具划拨后已超过30天";
  225. }
  226. }
  227. if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0)
  228. {
  229. pos.ActivationState = 1;
  230. pos.ActivationTime = DateTime.Now;
  231. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  232. if (pos.ActivationState == 1 && mer != null)
  233. {
  234. mer.ActiveStatus = 1;
  235. mer.MerStandardDate = DateTime.Now;
  236. }
  237. }
  238. else if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 1)
  239. {
  240. if (AddActPrize == 1)
  241. {
  242. if (IsSend == 1)
  243. {
  244. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  245. {
  246. UserId = pos.BuyUserId, //接收创客
  247. MsgType = 2,
  248. Title = "补录成功通知", //标题
  249. Summary = "您的 " + kqProducts.Name + " SN:" + pos.PosSn + "已经成功补录,请查收。",
  250. CreateDate = DateTime.Now,
  251. }));
  252. }
  253. return "激活奖励已补录";
  254. }
  255. else
  256. {
  257. return "数据正常,无需补录";
  258. }
  259. }
  260. db.SaveChanges();
  261. return "success";
  262. }
  263. pos.UserId = user.Id;
  264. pos.BuyUserId = user.Id;
  265. if (pos.CreditTrade >= CheckMoney && pos.ActivationState == 0 && (pos.BindingTime > DateTime.Now.AddDays(-30) || SysUserName == "admin"))
  266. {
  267. // pos.IsPurchase = 0;
  268. pos.ActivationState = 1;
  269. pos.ActivationTime = DateTime.Now;
  270. }
  271. PosMerchantInfo merchant = new PosMerchantInfo();
  272. if (!string.IsNullOrEmpty(MerNo))
  273. {
  274. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
  275. if (merchant == null)
  276. {
  277. int TopUserId = 0;
  278. if (!string.IsNullOrEmpty(user.ParentNav))
  279. {
  280. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  281. }
  282. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerNo == MerNo);
  283. if (bind == null)
  284. {
  285. return "该机具尚未绑定";
  286. }
  287. SpModels.Merchants Mer = spdb.Merchants.FirstOrDefault(m => m.MerNo == MerNo);
  288. if (Mer == null)
  289. {
  290. return "该机具尚未绑定";
  291. }
  292. PosMerchantInfo add = db.PosMerchantInfo.Add(new PosMerchantInfo()
  293. {
  294. CreateDate = Mer.CreateTime,
  295. UpdateDate = Mer.UpdateTime,
  296. TopUserId = TopUserId,
  297. MerUserType = user.MerchantType,
  298. BrandId = int.Parse(Mer.ProductType),
  299. SnStoreId = pos.StoreId,
  300. SnType = pos.PosSnType,
  301. UserId = user.Id,
  302. MgrName = Mer.AgentName,
  303. MerStatus = 1,
  304. KqSnNo = Mer.SnNo,
  305. KqMerNo = Mer.MerNo,
  306. MerIdcardNo = Mer.MerIdcardNo,
  307. MerRealName = Mer.MerRealName,
  308. MerchantMobile = Mer.MerMobile,
  309. MerchantName = Mer.MerName,
  310. MerchantNo = Mer.MerNo,
  311. }).Entity;
  312. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  313. if (forMerNo == null)
  314. {
  315. forMerNo = db.MachineForMerNo.Add(new MachineForMerNo()
  316. {
  317. MerNo = Mer.MerNo,
  318. SnId = pos.Id,
  319. }).Entity;
  320. }
  321. else
  322. {
  323. forMerNo.SnId = pos.Id;
  324. }
  325. }
  326. }
  327. else
  328. {
  329. if (pos.BindMerchantId > 0)
  330. {
  331. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  332. }
  333. else
  334. {
  335. return "找不到机具所属商户";
  336. }
  337. }
  338. merchant.UserId = user.Id;
  339. if (pos.ActivationState == 1)
  340. {
  341. merchant.ActiveStatus = 1;
  342. merchant.MerStandardDate = DateTime.Now;
  343. }
  344. if (pos.BindMerchantId == 0)
  345. {
  346. pos.BindMerchantId = merchant.Id;
  347. }
  348. // var makerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  349. // var prePos = db.PreSendStockDetail.FirstOrDefault(m => m.ToUserId == makerCode.UserId && m.SnId == pos.Id && m.CreateDate < DateTime.Now.AddDays(-30) && m.AuthFlag == 1 && m.ApplyFlag == 0) ?? new PreSendStockDetail();
  350. // if (makerCode.UserId > 0 && prePos.Id > 0)
  351. // {
  352. // if (prePos.CreateDate <= DateTime.Now.AddDays(-30))
  353. // {
  354. // var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == prePos.BrandId);
  355. // var amount = 0;
  356. // if (brandInfo.Name.Contains("电签"))
  357. // {
  358. // amount = 200;
  359. // }
  360. // if (brandInfo.Name.Contains("大POS"))
  361. // {
  362. // amount = 300;
  363. // }
  364. // var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == makerCode.UserId) ?? new UserAccount();
  365. // if (userAccount.Id > 0)
  366. // {
  367. // userAccount.SmallStoreDeposit -= amount;
  368. // var add = db.UserAccountRecord.Add(new UserAccountRecord()
  369. // {
  370. // CreateDate = DateTime.Now,
  371. // Remark = "小分仓押金退还",
  372. // ChangeType = 65,
  373. // BeforeBalanceAmount = userAccount.SmallStoreDeposit + amount, //变更前小分仓押金
  374. // AfterBalanceAmount = userAccount.SmallStoreDeposit, //变更后小分仓押金
  375. // ChangeAmount = amount,//变动金额
  376. // UserId = userAccount.Id,
  377. // }).Entity;
  378. // db.SaveChanges();
  379. // }
  380. // }
  381. // }
  382. db.SaveChanges();
  383. function.WriteLog(DateTime.Now.ToString() + "\n" + SysUserName + "\nMakerCode:" + MakerCode + ", PosSn:" + PosSn + ", MerNo:" + MerNo + ", AddActPrize:" + AddActPrize + ", IsPre:" + IsPre, "机具补录日志");
  384. return "success";
  385. }
  386. public IActionResult CheckMachine2(string right)
  387. {
  388. ViewBag.RightInfo = RightInfo;
  389. ViewBag.right = right;
  390. return View();
  391. }
  392. [HttpPost]
  393. public string CheckMachine2Do(string MakerCode, string PosSn, string MerNo = "", int AddActPrize = 0)
  394. {
  395. if (string.IsNullOrEmpty(MakerCode))
  396. {
  397. return "请输入创客编号";
  398. }
  399. if (string.IsNullOrEmpty(PosSn))
  400. {
  401. return "请输入机具SN";
  402. }
  403. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  404. if (pos == null)
  405. {
  406. return "机具SN不存在";
  407. }
  408. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  409. if (user == null)
  410. {
  411. return "创客编号不存在";
  412. }
  413. if (pos.BindingState == 0)
  414. {
  415. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == PosSn);
  416. if (bind != null)
  417. {
  418. pos.BindingState = 1;
  419. pos.BindingTime = bind.CreateTime;
  420. db.SaveChanges();
  421. MerNo = bind.MerNo;
  422. }
  423. }
  424. decimal CreditTrade = 0;
  425. bool check = spdb.TradeRecord.Any(m => m.TradeSnNo == pos.PosSn);
  426. if (check)
  427. {
  428. string BindingTime = pos.BindingTime == null ? DateTime.Now.AddMonths(-1).ToString("yyyyMM") : pos.BindingTime.Value.ToString("yyyyMM");
  429. string SpTradeRecordId = function.ReadInstance("/PublicParams/SpTradeRecordId" + BindingTime + ".txt");
  430. int StartId = int.Parse(function.CheckInt(SpTradeRecordId));
  431. if (pos.BrandId == 1)
  432. {
  433. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M700")).Sum(m => m.TradeAmount) / 100;
  434. }
  435. else if (pos.BrandId == 2)
  436. {
  437. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "2").Sum(m => m.TradeAmount);
  438. }
  439. else if (pos.BrandId == 3)
  440. {
  441. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "1" && m.SerEntryMode != "N" && m.MerNo.StartsWith("M900")).Sum(m => m.TradeAmount) / 100;
  442. }
  443. else if (pos.BrandId == 4 || pos.BrandId == 5)
  444. {
  445. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "4").Sum(m => m.TradeAmount) / 100;
  446. }
  447. else if (pos.BrandId == 6)
  448. {
  449. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "6").Sum(m => m.TradeAmount) / 100;
  450. }
  451. else if (pos.BrandId == 8)
  452. {
  453. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "8").Sum(m => m.TradeAmount) / 100;
  454. }
  455. else if (pos.BrandId == 9)
  456. {
  457. CreditTrade = spdb.TradeRecord.Where(m => m.Id > StartId && m.TradeSnNo == pos.PosSn && m.ProductType == "9").Sum(m => m.TradeAmount) / 100;
  458. }
  459. pos.CreditTrade = CreditTrade;
  460. db.SaveChanges();
  461. StatTradeAmount(pos.PosSn);
  462. }
  463. if (AddActPrize == 1)
  464. {
  465. if (string.IsNullOrEmpty(pos.SeoKeyword) || pos.SeoKeyword == "0")
  466. {
  467. bool has = spdb.ActivateRecord.Any(m => m.SnNo == pos.PosSn);
  468. if (has)
  469. {
  470. SpModels.ActivateRecord chkAct = spdb.ActivateRecord.Where(m => m.SnNo == pos.PosSn).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SpModels.ActivateRecord();
  471. pos.SeoKeyword = chkAct.SeoTitle;
  472. db.SaveChanges();
  473. }
  474. }
  475. AddAct(pos.Id);
  476. }
  477. if (pos.BuyUserId > 0)
  478. {
  479. if (pos.CreditTrade < 1000)
  480. {
  481. // pos.IsPurchase = 99;
  482. db.SaveChanges();
  483. return "该机具贷记卡累计交易不足1000";
  484. }
  485. if (pos.CreditTrade >= 1000 && pos.ActivationState == 0)
  486. {
  487. // pos.IsPurchase = 0;
  488. pos.ActivationState = 1;
  489. pos.ActivationTime = DateTime.Now;
  490. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  491. if (pos.ActivationState == 1 && mer != null)
  492. {
  493. mer.ActiveStatus = 1;
  494. mer.MerStandardDate = DateTime.Now;
  495. }
  496. }
  497. else if (pos.CreditTrade >= 1000 && pos.ActivationState == 1)
  498. {
  499. if (AddActPrize == 1)
  500. {
  501. return "激活奖励已补录";
  502. }
  503. else
  504. {
  505. return "数据正常,无需补录";
  506. }
  507. }
  508. db.SaveChanges();
  509. return "success";
  510. }
  511. pos.UserId = user.Id;
  512. pos.BuyUserId = user.Id;
  513. // if (pos.CreditTrade < 1000)
  514. // {
  515. // pos.IsPurchase = 99;
  516. // }
  517. if (pos.CreditTrade >= 1000 && pos.ActivationState == 0)
  518. {
  519. // pos.IsPurchase = 0;
  520. pos.ActivationState = 1;
  521. pos.ActivationTime = DateTime.Now;
  522. }
  523. PosMerchantInfo merchant = new PosMerchantInfo();
  524. if (!string.IsNullOrEmpty(MerNo))
  525. {
  526. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
  527. if (merchant == null)
  528. {
  529. int TopUserId = 0;
  530. if (!string.IsNullOrEmpty(user.ParentNav))
  531. {
  532. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  533. }
  534. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerNo == MerNo);
  535. if (bind == null)
  536. {
  537. return "该机具尚未绑定";
  538. }
  539. SpModels.Merchants Mer = spdb.Merchants.FirstOrDefault(m => m.MerNo == MerNo);
  540. if (Mer == null)
  541. {
  542. return "该机具尚未绑定";
  543. }
  544. PosMerchantInfo add = db.PosMerchantInfo.Add(new PosMerchantInfo()
  545. {
  546. CreateDate = Mer.CreateTime,
  547. UpdateDate = Mer.UpdateTime,
  548. TopUserId = TopUserId,
  549. MerUserType = user.MerchantType,
  550. BrandId = int.Parse(Mer.ProductType),
  551. SnStoreId = pos.StoreId,
  552. SnType = pos.PosSnType,
  553. UserId = user.Id,
  554. MgrName = Mer.AgentName,
  555. MerStatus = 1,
  556. KqSnNo = Mer.SnNo,
  557. KqMerNo = Mer.MerNo,
  558. MerIdcardNo = Mer.MerIdcardNo,
  559. MerRealName = Mer.MerRealName,
  560. MerchantMobile = Mer.MerMobile,
  561. MerchantName = Mer.MerName,
  562. MerchantNo = Mer.MerNo,
  563. }).Entity;
  564. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  565. if (forMerNo == null)
  566. {
  567. forMerNo = db.MachineForMerNo.Add(new MachineForMerNo()
  568. {
  569. MerNo = Mer.MerNo,
  570. SnId = pos.Id,
  571. }).Entity;
  572. }
  573. else
  574. {
  575. forMerNo.SnId = pos.Id;
  576. }
  577. }
  578. }
  579. else
  580. {
  581. if (pos.BindMerchantId > 0)
  582. {
  583. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  584. }
  585. else
  586. {
  587. return "找不到机具所属商户";
  588. }
  589. }
  590. merchant.UserId = user.Id;
  591. if (pos.ActivationState == 1)
  592. {
  593. merchant.ActiveStatus = 1;
  594. merchant.MerStandardDate = DateTime.Now;
  595. }
  596. if (pos.BindMerchantId == 0)
  597. {
  598. pos.BindMerchantId = merchant.Id;
  599. }
  600. db.SaveChanges();
  601. return "success";
  602. }
  603. public void AddAct(int posid = 0)
  604. {
  605. WebCMSEntities db = new WebCMSEntities();
  606. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.ActivationState == 1 && !string.IsNullOrEmpty(m.SeoKeyword) && m.Id == posid);
  607. if (pos != null)
  608. {
  609. // PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId && !string.IsNullOrEmpty(m.MerIdcardNo));
  610. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  611. if (merchant != null)
  612. {
  613. // PosMerchantOtherInfo otherInfo = db.PosMerchantOtherInfo.FirstOrDefault(m => m.CertId == merchant.MerIdcardNo && m.PrizeFlag1 == 0);
  614. // if (otherInfo != null)
  615. // {
  616. // otherInfo.PrizeFlag1 = 1;
  617. // db.SaveChanges();
  618. string ParentNav = "";
  619. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  620. if (user != null)
  621. {
  622. int GetUserId = user.Id;
  623. ParentNav = user.ParentNav;
  624. int TopUserId = 1;
  625. if (!string.IsNullOrEmpty(ParentNav))
  626. {
  627. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  628. if (ParentNavList.Length > 1)
  629. {
  630. TopUserId = int.Parse(ParentNavList[1]);
  631. }
  632. else if (ParentNavList.Length == 1)
  633. {
  634. TopUserId = int.Parse(ParentNavList[0]);
  635. }
  636. }
  637. List<decimal> prizes = new List<decimal>();
  638. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  639. if (pos.BrandId == 1 || pos.BrandId == 3 || pos.BrandId == 4 || pos.BrandId == 5 || pos.BrandId == 6 || pos.BrandId == 8 || pos.BrandId == 9)
  640. {
  641. ActPrize = ActPrize / 100;
  642. }
  643. if (ActPrize > 0)
  644. {
  645. if (ActPrize == 99)
  646. {
  647. ActPrize = 100;
  648. }
  649. else if (ActPrize == 199)
  650. {
  651. ActPrize = 200;
  652. }
  653. else if (ActPrize == 299)
  654. {
  655. ActPrize = 300;
  656. }
  657. else if (ActPrize == 249)
  658. {
  659. ActPrize = 260;
  660. }
  661. prizes.Add(ActPrize);
  662. if (pos.BrandId == 6 && pos.ActivationState == 1 && pos.CreditTrade >= 10000 && pos.BindingTime > DateTime.Now.AddDays(-60))
  663. {
  664. prizes.Add(40);
  665. }
  666. }
  667. else if (pos.BrandId == 6 && ActPrize == 0)
  668. {
  669. prizes.Add(50);
  670. }
  671. foreach (decimal prize in prizes)
  672. {
  673. int ChangeType = prize == 40 ? 12 : 0;
  674. bool check = db.ActiveReward.Any(m => m.KqSnNo == pos.PosSn && m.RewardAmount == prize);
  675. if (!check)
  676. {
  677. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  678. db.ActiveReward.Add(new ActiveReward()
  679. {
  680. CreateDate = DateTime.Now,
  681. UpdateDate = DateTime.Now,
  682. UserId = GetUserId, //创客
  683. MerchantId = pos.BindMerchantId, //商户
  684. StandardDate = pos.ActivationTime, //达标日期
  685. RewardAmount = prize, //奖励金额
  686. BrandId = pos.BrandId, //品牌
  687. UserNav = ParentNav, //创客父级
  688. DirectBuddyNo = merchant.UserId, //商户直属创客
  689. KqMerNo = merchant.KqMerNo, //渠道商户编号
  690. KqSnNo = pos.PosSn, //渠道SN号
  691. SnType = pos.PosSnType, //机具类型
  692. SnApplyUserId = pos.BuyUserId, //机具申请创客
  693. ActType = 0, //激活类型
  694. SnStoreId = pos.StoreId, //SN仓库
  695. RewardTips = "激活奖励", //奖励描述
  696. Remark = "激活奖励", //备注
  697. ActDate = pos.ActivationTime, //激活时间
  698. TopUserId = TopUserId, //顶级创客
  699. SeoTitle = machineUser.RealName,
  700. });
  701. db.SaveChanges();
  702. string IdBrand = GetUserId + "_" + pos.BrandId;
  703. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  704. if (userData == null)
  705. {
  706. userData = db.UserMachineData.Add(new UserMachineData()
  707. {
  708. IdBrand = IdBrand,
  709. }).Entity;
  710. db.SaveChanges();
  711. }
  712. userData.ActProfit += prize;
  713. db.SaveChanges();
  714. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  715. if (account == null)
  716. {
  717. account = db.UserAccount.Add(new UserAccount()
  718. {
  719. Id = GetUserId,
  720. UserId = GetUserId,
  721. }).Entity;
  722. db.SaveChanges();
  723. }
  724. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  725. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  726. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  727. account.BalanceAmount += prize;
  728. account.TotalAmount += prize;
  729. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  730. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  731. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  732. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  733. {
  734. CreateDate = DateTime.Now,
  735. UpdateDate = DateTime.Now,
  736. UserId = GetUserId, //创客
  737. ChangeType = ChangeType, //变动类型
  738. ProductType = pos.BrandId, //产品类型
  739. ChangeAmount = prize, //变更金额
  740. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  741. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  742. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  743. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  744. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  745. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  746. }).Entity;
  747. db.SaveChanges();
  748. RedisDbconn.Instance.Set("UserAccount:" + GetUserId, account);
  749. string dateString = pos.ActivationTime.Value.ToString("yyyyMMdd");
  750. string monthString = pos.ActivationTime.Value.ToString("yyyyMM");
  751. // 激活奖励列表
  752. List<string> dates = RedisDbconn.Instance.GetList<string>("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId);
  753. if (!dates.Contains(dateString))
  754. {
  755. RedisDbconn.Instance.AddList("ActiveRewardDay:" + GetUserId + ":" + pos.BrandId, dateString);
  756. }
  757. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + dateString, prize);
  758. List<string> months = RedisDbconn.Instance.GetList<string>("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId);
  759. if (!months.Contains(monthString))
  760. {
  761. RedisDbconn.Instance.AddList("ActiveRewardMonth:" + GetUserId + ":" + pos.BrandId, monthString);
  762. }
  763. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:" + GetUserId + ":" + pos.BrandId + ":" + monthString, prize);
  764. // 激活奖励详情
  765. List<int> actPrizeList = RedisDbconn.Instance.GetList<int>("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString);
  766. if (!actPrizeList.Contains(pos.BindMerchantId))
  767. {
  768. RedisDbconn.Instance.AddList("ActiveRewardDetail:" + GetUserId + ":" + pos.BrandId + ":" + dateString, pos.BindMerchantId);
  769. }
  770. RedisDbconn.Instance.AddNumber("ActiveRewardAmt:mer:" + pos.BindMerchantId + ":" + pos.BrandId + ":" + dateString, prize);
  771. //收支明细
  772. RedisDbconn.Instance.AddList("UserAccountRecord:" + GetUserId + ":1:" + monthString, userAccountRecord);
  773. RedisDbconn.Instance.AddNumber("UserAccount:" + GetUserId + ":1:" + monthString, prize);
  774. }
  775. }
  776. }
  777. // }
  778. }
  779. }
  780. db.Dispose();
  781. }
  782. //重置交易额
  783. public void StatTradeAmount(string PosSn, int StartId = 0)
  784. {
  785. DateTime start = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  786. if (DateTime.Now.Day == 1)
  787. {
  788. start = DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM") + "-01 00:00:00");
  789. }
  790. DateTime end = DateTime.Now;
  791. SycnSpTradeService.Instance.Start(PosSn, start, end, StartId);
  792. RedisDbconn.Instance.AddList("ResetTradeAmountList", PosSn);
  793. }
  794. #endregion
  795. #region 设置商户代理
  796. public IActionResult SetMerAgent(string right)
  797. {
  798. ViewBag.RightInfo = RightInfo;
  799. ViewBag.right = right;
  800. return View();
  801. }
  802. [HttpPost]
  803. public string SetMerAgentDo(string MakerCode, string PosSn, string MerNo, int IsSend = 0)
  804. {
  805. if (string.IsNullOrEmpty(MakerCode))
  806. {
  807. return "请输入创客编号";
  808. }
  809. if (string.IsNullOrEmpty(PosSn))
  810. {
  811. return "请输入机具SN";
  812. }
  813. if (string.IsNullOrEmpty(MerNo))
  814. {
  815. return "请输入商户编号";
  816. }
  817. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  818. KqProducts kqProducts = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId);
  819. if (pos == null)
  820. {
  821. return "机具SN不存在";
  822. }
  823. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  824. if (user == null)
  825. {
  826. return "创客编号不存在";
  827. }
  828. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo);
  829. if (merchant == null)
  830. {
  831. return "商户编号不存在";
  832. }
  833. pos.SeoTitle = user.Id.ToString();
  834. pos.UserId = user.Id;
  835. if (pos.BindMerchantId == 0)
  836. {
  837. pos.BindMerchantId = merchant.Id;
  838. if (IsSend == 1)
  839. {
  840. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  841. {
  842. UserId = pos.BuyUserId, //接收创客
  843. MsgType = 2,
  844. Title = "商户型代理设置成功", //标题
  845. Summary = "您的 " + kqProducts.Name + " SN:" + pos.PosSn + "已经成功给下级代理" + user.RealName + "" + MakerCode + ",设置为商户型代理,设置成功后的新增交易的分润归商户型代理所有",
  846. CreateDate = DateTime.Now,
  847. }));
  848. }
  849. }
  850. user.MerchantType = 1;
  851. merchant.UserId = user.Id;
  852. merchant.MerUserType = 1;
  853. db.SetMerchantTypeRecord.Add(new SetMerchantTypeRecord()
  854. {
  855. CreateDate = DateTime.Now,
  856. IsRecyc = (ulong)pos.IsPurchase,
  857. CreditAmount = pos.CreditTrade,
  858. PosSnType = pos.PosSnType,
  859. ActDate = pos.ActivationTime,
  860. BindDate = pos.BindingTime,
  861. ActStatus = (ulong)pos.ActivationState,
  862. BindStatus = (ulong)pos.BindingState,
  863. MerNo = MerNo,
  864. PosSn = pos.PosSn,
  865. Note = "后台设置商户型创客",
  866. ToUserId = user.Id,
  867. FromUserId = pos.BuyUserId,
  868. });
  869. db.SaveChanges();
  870. return "success";
  871. }
  872. #endregion
  873. #region 取消商户代理
  874. public IActionResult CancelMerAgent(string right)
  875. {
  876. ViewBag.RightInfo = RightInfo;
  877. ViewBag.right = right;
  878. return View();
  879. }
  880. [HttpPost]
  881. public string CancelMerAgentDo(string MakerCode, string PosSn, int IsSend = 0)
  882. {
  883. if (string.IsNullOrEmpty(MakerCode))
  884. {
  885. return "请输入创客编号";
  886. }
  887. if (string.IsNullOrEmpty(PosSn))
  888. {
  889. return "请输入机具SN";
  890. }
  891. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  892. KqProducts kqProducts = db.KqProducts.FirstOrDefault(m => m.Id == pos.BrandId);
  893. if (pos == null)
  894. {
  895. return "机具SN不存在";
  896. }
  897. Users user = db.Users.FirstOrDefault(m => m.MakerCode == MakerCode);
  898. if (user == null)
  899. {
  900. return "创客编号不存在";
  901. }
  902. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == PosSn);
  903. pos.UserId = pos.BuyUserId;
  904. merchant.UserId = pos.BuyUserId;
  905. merchant.MerUserType = 0;
  906. user.MerchantType = 0;
  907. db.SaveChanges();
  908. if (pos.BindMerchantId == 0)
  909. {
  910. pos.BindMerchantId = merchant.Id;
  911. if (IsSend == 1)
  912. {
  913. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  914. {
  915. UserId = pos.BuyUserId, //接收创客
  916. MsgType = 2,
  917. Title = "商户型代理取消成功", //标题
  918. Summary = "您的 " + kqProducts.Name + " SN:" + pos.PosSn + "已给下级代理" + user.RealName + "" + MakerCode + "取消成功",
  919. CreateDate = DateTime.Now,
  920. }));
  921. }
  922. }
  923. string text = string.Format("取消商户代理,创客编号: " + user.MakerCode + ",机具SN:" + pos.PosSn + ",操作人:" + SysRealName + ",Time:" + DateTime.Now + "");
  924. function.WriteLog(text, "CancelMerAgent");//取消商户型代理日志
  925. return "success";
  926. }
  927. #endregion
  928. #region 查看SP原始数据日志
  929. public IActionResult SeeSpLog(string right)
  930. {
  931. ViewBag.RightInfo = RightInfo;
  932. ViewBag.right = right;
  933. return View();
  934. }
  935. #endregion
  936. #region 查询机具信息
  937. public IActionResult SeeSnDetail(string right)
  938. {
  939. ViewBag.RightInfo = RightInfo;
  940. ViewBag.right = right;
  941. return View();
  942. }
  943. [HttpPost]
  944. public string SeeSnDetailDo(string PosSn)
  945. {
  946. if (string.IsNullOrEmpty(PosSn))
  947. {
  948. return "请输入机具SN";
  949. }
  950. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  951. if (pos == null)
  952. {
  953. return "机具SN不存在";
  954. }
  955. if (pos.Status == -1)
  956. {
  957. Users posUsers = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  958. decimal Deposits = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  959. if (pos.BrandId != 2 && pos.BrandId != 7)
  960. {
  961. Deposits = Deposits / 100;
  962. }
  963. string DepositGets = "未领取";
  964. string DepositUsers = "";
  965. ActiveReward rewards = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.RewardAmount != 40) ?? new ActiveReward();
  966. if (rewards.Id > 0)
  967. {
  968. Users user = db.Users.FirstOrDefault(m => m.Id == rewards.UserId) ?? new Users();
  969. DepositGets = "已领取";
  970. DepositUsers = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  971. }
  972. string ActiveTimes = rewards.CreateDate == null ? "" : rewards.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  973. var storeInfo = db.StoreHouse.FirstOrDefault(m => m.Id == pos.StoreId) ?? new StoreHouse();
  974. string results = "该机具为故障机" + "\n";
  975. results += "所在仓库:" + storeInfo.StoreNo + "\n";
  976. results += "仓库名称:" + storeInfo.StoreName + "\n";
  977. results += "押金返现实际返现对象:" + DepositUsers + "\n";
  978. results += "押金:" + Deposits + "\n";
  979. results += "是否返现:" + DepositGets + ",领取时间:" + ActiveTimes + "\n";
  980. return results;
  981. }
  982. string merInfo = "";
  983. string merName = "";
  984. PosMerchantInfo mer = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  985. if (mer != null)
  986. {
  987. merInfo = mer.KqMerNo;
  988. merName = mer.MerchantName;
  989. }
  990. else
  991. {
  992. merInfo = "未找到商户";
  993. }
  994. StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == pos.StoreId) ?? new StoreHouse();
  995. Users posUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  996. Users posSubUser = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new Users();
  997. int TopUserId = 0;
  998. if (!string.IsNullOrEmpty(posUser.ParentNav))
  999. {
  1000. string[] ParentNavs = posUser.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1001. if (ParentNavs.Length > 1)
  1002. {
  1003. TopUserId = int.Parse(ParentNavs[1]);
  1004. }
  1005. if (ParentNavs.Length == 1)
  1006. {
  1007. TopUserId = int.Parse(ParentNavs[0]);
  1008. }
  1009. }
  1010. Users posTopUser = db.Users.FirstOrDefault(m => m.Id == TopUserId) ?? new Users();
  1011. string bindResult = "", bindTime = "";
  1012. if (pos.BindingState == 0)
  1013. {
  1014. SpModels.BindRecord bind = spdb.BindRecord.FirstOrDefault(m => m.MerSnNo == pos.PosSn);
  1015. if (bind != null)
  1016. {
  1017. if (bind.Status != 1)
  1018. {
  1019. bindResult = "执行中";
  1020. }
  1021. }
  1022. else
  1023. {
  1024. bindResult = "未绑定";
  1025. }
  1026. }
  1027. else
  1028. {
  1029. bindResult = "已绑定";
  1030. bindTime = pos.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1031. }
  1032. string activeResult = "", activeTime = "";
  1033. if (pos.ActivationState == 0)
  1034. {
  1035. if (pos.CreditTrade < 1000)
  1036. {
  1037. activeResult = "未刷满1000";
  1038. }
  1039. else if (pos.TransferTime == null)
  1040. {
  1041. activeResult = "未激活, 需补录";
  1042. }
  1043. }
  1044. else
  1045. {
  1046. activeResult = "已激活";
  1047. activeTime = pos.ActivationTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1048. }
  1049. string tradeAmount = pos.CreditTrade.ToString();
  1050. decimal Deposit = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  1051. if (pos.BrandId != 2 && pos.BrandId != 7)
  1052. {
  1053. Deposit = Deposit / 100;
  1054. }
  1055. string DepositGet = "未领取";
  1056. string DepositUser = "";
  1057. string DepositToUser = "";
  1058. decimal headAmount = 0;
  1059. decimal footAmount = 0;
  1060. ActiveReward reward = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.RewardAmount != 40) ?? new ActiveReward();
  1061. if (reward.Id > 0)
  1062. {
  1063. Users user = db.Users.FirstOrDefault(m => m.Id == reward.UserId) ?? new Users();
  1064. DepositGet = "已领取";
  1065. DepositUser = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1066. var fAmount = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.ActType == 0);
  1067. if (fAmount != null)
  1068. {
  1069. headAmount = fAmount.RewardAmount;
  1070. }
  1071. var lAmount = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.ActType == 1);
  1072. if (lAmount != null)
  1073. {
  1074. footAmount = lAmount.RewardAmount;
  1075. }
  1076. }
  1077. string DepositGet6 = "未领取";
  1078. string DepositUser6 = "";
  1079. ActiveReward lisreward = db.ActiveReward.FirstOrDefault(m => m.KqSnNo == pos.PosSn && m.BrandId == 6 && m.RewardAmount == 40) ?? new ActiveReward();
  1080. if (lisreward.Id > 0)
  1081. {
  1082. Users user = db.Users.FirstOrDefault(m => m.Id == lisreward.UserId) ?? new Users();
  1083. DepositGet6 = "已领取";
  1084. DepositUser6 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1085. }
  1086. string FluxGet6 = "未领取";
  1087. string FluxUser6 = "";
  1088. FluxProfitDetail flux = db.FluxProfitDetail.FirstOrDefault(m => m.SnNo == pos.PosSn) ?? new FluxProfitDetail();
  1089. if (flux.Id > 0)
  1090. {
  1091. Users user = db.Users.FirstOrDefault(m => m.Id == flux.UserId) ?? new Users();
  1092. FluxGet6 = "已领取(领取时间:" + flux.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + ")";
  1093. FluxUser6 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1094. }
  1095. Users touser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  1096. DepositToUser = "创客编号:" + touser.MakerCode + ",姓名:" + touser.RealName + ",手机号:" + touser.Mobile;
  1097. string OpenUser10 = "未领取";
  1098. string OpenUser20 = "未领取";
  1099. var OpenRewardDetail = db.OpenRewardDetail.Where(m => m.SnNo == pos.PosSn).ToList();
  1100. OpenRewardDetail openreward = OpenRewardDetail.FirstOrDefault(m => m.CreditRewardAmount == 20) ?? new OpenRewardDetail();
  1101. if (openreward.Id > 0)
  1102. {
  1103. Users user = db.Users.FirstOrDefault(m => m.Id == openreward.UserId) ?? new Users();
  1104. OpenUser20 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1105. }
  1106. openreward = OpenRewardDetail.FirstOrDefault(m => m.CreditRewardAmount == 10) ?? new OpenRewardDetail();
  1107. if (openreward.Id > 0)
  1108. {
  1109. Users user = db.Users.FirstOrDefault(m => m.Id == openreward.UserId) ?? new Users();
  1110. OpenUser10 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  1111. }
  1112. MachineApply apply = db.MachineApply.FirstOrDefault(m => m.SwapSnExpand.Contains(pos.PosSn)) ?? new MachineApply();
  1113. Orders applyorder = db.Orders.FirstOrDefault(m => m.Id == apply.QueryCount) ?? new Orders();
  1114. MachineChangeDetail change = db.MachineChangeDetail.FirstOrDefault(m => m.OutSnNo == pos.PosSn) ?? new MachineChangeDetail();
  1115. string RecycFlag = pos.IsPurchase == 1 ? "已申请循环" : "未申请循环";
  1116. string SendStatus = applyorder.SendStatus == 1 ? "已发货" : "未发货";
  1117. string ApplyDate = apply.CreateDate == null ? "" : apply.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1118. string SendDate = applyorder.SendDate == null ? "" : applyorder.SendDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1119. string CreateDate = pos.CreateDate == null ? "" : pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1120. string TransferTime = pos.TransferTime == null ? "" : pos.TransferTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1121. string RecycEndDate = pos.RecycEndDate == null ? "" : pos.RecycEndDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1122. string ActiveTime = reward.CreateDate == null ? "" : reward.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1123. string ActiveTime6 = lisreward.CreateDate == null ? "" : lisreward.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1124. string result = "";
  1125. result += "机具SN:" + pos.PosSn + "\n";
  1126. result += "商户编号:" + merInfo + ",商户姓名:" + merName + "\n";
  1127. result += "商户型机器/直拓机器:创客编号:" + posSubUser.MakerCode + ",姓名:" + posSubUser.RealName + ",手机号:" + posSubUser.Mobile + "\n";
  1128. if (change.Id > 0)
  1129. {
  1130. result += "换机来源机具SN:" + change.BackSnNo + "\n";
  1131. }
  1132. result += "机具所属人:创客编号:" + posUser.MakerCode + ",姓名:" + posUser.RealName + ",手机号:" + posUser.Mobile + "\n";
  1133. result += "机器持有人顶级:创客编号:" + posTopUser.MakerCode + ",姓名:" + posTopUser.RealName + ",手机号:" + posTopUser.Mobile + "\n";
  1134. result += "机器所属仓库:仓库编号:" + store.StoreNo + ",仓库名称:" + store.StoreName + "\n";
  1135. if (pos.PreUserId > 0)
  1136. {
  1137. Users smallStoreUser = db.Users.FirstOrDefault(m => m.Id == pos.PreUserId) ?? new Users();
  1138. PreSendStockDetail preSend = db.PreSendStockDetail.FirstOrDefault(m => m.SnId == pos.Id && m.ToUserId == pos.PreUserId && m.Status == 1) ?? new PreSendStockDetail();
  1139. string preSendDate = preSend.CreateDate == null ? "" : preSend.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  1140. result += "机具所属小分仓:创客编号:" + smallStoreUser.MakerCode + ",姓名:" + smallStoreUser.RealName + ",手机号:" + smallStoreUser.Mobile + ",预发时间:" + preSendDate + "\n";
  1141. }
  1142. else
  1143. {
  1144. result += "机具所属小分仓:暂无小分仓\n";
  1145. }
  1146. result += "入库时间:" + CreateDate + "\n";
  1147. result += "循环到期时间:" + RecycEndDate + "\n";
  1148. result += "划拨时间:" + TransferTime + "\n";
  1149. result += "绑定状态:" + bindResult + "(绑定时间:" + bindTime + ")\n";
  1150. result += "激活状态:" + activeResult + "(激活时间:" + activeTime + ")\n";
  1151. result += "贷记卡(总)交易额:" + tradeAmount + "\n";
  1152. result += "是否循环:" + RecycFlag + ",申请循环时间:" + ApplyDate + "\n";
  1153. result += "申请循环后的SN:" + applyorder.SnNos + "\n";
  1154. result += "是否发货:" + SendStatus + ",发货时间:" + SendDate + "\n";
  1155. result += "押金:" + Deposit + "\n";
  1156. result += "是否返现:" + DepositGet + ",领取时间:" + ActiveTime + "\n";
  1157. result += "返现金额:" + headAmount + " " + " " + footAmount + "\n";//TODO: 添加返现金额和额外奖励
  1158. result += "押金返现应返现对象:" + DepositToUser + "\n";
  1159. result += "押金返现实际返现对象:" + DepositUser + "\n";
  1160. if (pos.BrandId == 6)
  1161. {
  1162. result += "立刷10000奖励:" + DepositGet6 + ",领取时间:" + ActiveTime6 + "\n";
  1163. result += "立刷10000奖励发放人:" + DepositUser6 + "\n";
  1164. }
  1165. result += "流量费:" + FluxGet6 + "\n";
  1166. result += "流量费发放对象:" + FluxUser6 + "\n";
  1167. result += "开机奖20:" + OpenUser20 + "\n";
  1168. result += "开机奖10:" + OpenUser10 + "\n";
  1169. return result;
  1170. }
  1171. //解析金控日志
  1172. // public decimal CheckLog(DateTime startDate, string PosSn)
  1173. // {
  1174. // bool op = true;
  1175. // decimal result = 0;
  1176. // DateTime today = DateTime.Now;
  1177. // int i = 0;
  1178. // while (op)
  1179. // {
  1180. // DateTime check = DateTime.Parse(startDate.ToString("yyyy-MM-dd") + " 00:00:00").AddDays(i);
  1181. // if (check <= today)
  1182. // {
  1183. // string source = function.GetWebRequest("http://sp.kexiaoshuang.com/api/test/GetLog?date=" + check.Year + "-" + check.Month + "-" + check.Day);
  1184. // MatchCollection mc = Regex.Matches(source, "{.*?2022年飞天系列-小宝电签.*?}"); //激活
  1185. // foreach (Match sub in mc)
  1186. // {
  1187. // string content = sub.Value;
  1188. // JsonData jsonObj = JsonMapper.ToObject(content);
  1189. // decimal serviceAmount = decimal.Parse(jsonObj["serviceAmount"].ToString());
  1190. // string posSnCheck = jsonObj["posSn"].ToString();
  1191. // if (posSnCheck == PosSn && serviceAmount > 0)
  1192. // {
  1193. // result = serviceAmount / 100;
  1194. // op = false;
  1195. // }
  1196. // }
  1197. // }
  1198. // else
  1199. // {
  1200. // op = false;
  1201. // }
  1202. // i += 1;
  1203. // }
  1204. // return result;
  1205. // }
  1206. #endregion
  1207. #region 生成机具兑换码
  1208. public IActionResult MakeCoupons(string right)
  1209. {
  1210. ViewBag.RightInfo = RightInfo;
  1211. ViewBag.right = right;
  1212. return View();
  1213. }
  1214. [HttpPost]
  1215. public string MakeCouponsDo(int Kind, int Number)
  1216. {
  1217. DateTime checkTime = DateTime.Parse("1900-01-01");
  1218. string checkTimeString = RedisDbconn.Instance.Get<string>("MakerCouponsQueueCheckTime");
  1219. if (!string.IsNullOrEmpty(checkTimeString))
  1220. {
  1221. checkTime = DateTime.Parse(checkTimeString);
  1222. }
  1223. if (checkTime.AddHours(1) < DateTime.Now)
  1224. {
  1225. RedisDbconn.Instance.Set("MakerCouponsQueueCheckTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  1226. RedisDbconn.Instance.AddList("MakerCouponsQueue:" + Kind, Number);
  1227. return "生成程序已执行";
  1228. }
  1229. return "请稍后再试";
  1230. }
  1231. #endregion
  1232. #region 导出没有刷满1000的机具
  1233. public IActionResult ExportPos(string right)
  1234. {
  1235. ViewBag.RightInfo = RightInfo;
  1236. ViewBag.right = right;
  1237. return View();
  1238. }
  1239. [HttpPost]
  1240. public JsonResult ExportPosDo()
  1241. {
  1242. WebCMSEntities db = new WebCMSEntities();
  1243. List<PosMachinesTwo> poses = db.PosMachinesTwo.Where(m => m.CreditTrade < 1000 && m.BindingState == 1).ToList();
  1244. int total = poses.Count;
  1245. int index = 0;
  1246. List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
  1247. foreach (PosMachinesTwo pos in poses)
  1248. {
  1249. index += 1;
  1250. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId) ?? new PosMerchantInfo();
  1251. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  1252. Users puser = db.Users.FirstOrDefault(m => m.Id == user.ParentUserId) ?? new Users();
  1253. Users ppuser = db.Users.FirstOrDefault(m => m.Id == puser.ParentUserId) ?? new Users();
  1254. Dictionary<string, object> item = new Dictionary<string, object>();
  1255. string TopRealName = "";
  1256. string TopMakerCode = "";
  1257. string TopMobile = "";
  1258. string ParentNav = user.ParentNav;
  1259. if (!string.IsNullOrEmpty(ParentNav))
  1260. {
  1261. string[] navlist = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  1262. if (navlist.Length > 1)
  1263. {
  1264. int TopId = int.Parse(function.CheckInt(navlist[1]));
  1265. Users tuser = db.Users.FirstOrDefault(m => m.Id == TopId) ?? new Users();
  1266. TopRealName = tuser.RealName;
  1267. TopMakerCode = tuser.MakerCode;
  1268. TopMobile = tuser.Mobile;
  1269. }
  1270. }
  1271. item.Add("TopRealName", TopRealName); //顶级姓名
  1272. item.Add("TopMakerCode", TopMakerCode); //顶级编号
  1273. item.Add("UpUpRealName", ppuser.RealName); //上上级姓名
  1274. item.Add("UpRealName", puser.RealName); //上级姓名
  1275. item.Add("RealName", user.RealName); //代理姓名
  1276. item.Add("MakerCode", user.MakerCode); //代理编号
  1277. item.Add("Mobile", user.Mobile); //代理手机号
  1278. item.Add("MerchantName", merchant.MerchantName); //客户姓名
  1279. item.Add("MerchantMobile", merchant.MerchantMobile); //客户电话
  1280. item.Add("OpenTime", pos.BindingTime == null ? "" : pos.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //开机日期
  1281. item.Add("Sn", pos.PosSn); //SN
  1282. list.Add(item);
  1283. RedisDbconn.Instance.Set("ExportPosCheck", index + " / " + total);
  1284. }
  1285. db.Dispose();
  1286. Dictionary<string, object> result = new Dictionary<string, object>();
  1287. result.Add("Status", "1");
  1288. result.Add("Info", "Excel报表-" + DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss") + ".xlsx");
  1289. result.Add("Obj", list);
  1290. Dictionary<string, object> ReturnFields = new Dictionary<string, object>();
  1291. ReturnFields.Add("TopRealName", "顶级姓名"); //顶级姓名
  1292. ReturnFields.Add("TopMakerCode", "顶级编号"); //顶级编号
  1293. ReturnFields.Add("UpUpRealName", "上上级姓名"); //上上级姓名
  1294. ReturnFields.Add("UpRealName", "上级姓名"); //上级姓名
  1295. ReturnFields.Add("RealName", "代理姓名"); //代理姓名
  1296. ReturnFields.Add("MakerCode", "代理编号"); //代理编号
  1297. ReturnFields.Add("Mobile", "代理手机号"); //代理手机号
  1298. ReturnFields.Add("MerchantName", "客户姓名"); //客户姓名
  1299. ReturnFields.Add("MerchantMobile", "客户电话"); //客户电话
  1300. ReturnFields.Add("OpenTime", "开机日期"); //开机日期
  1301. ReturnFields.Add("Sn", "SN"); //SN
  1302. result.Add("Fields", ReturnFields);
  1303. RedisDbconn.Instance.Set("ExportPosCheck", "finish");
  1304. return Json(result);
  1305. }
  1306. public string ExportPosCheck()
  1307. {
  1308. string content = RedisDbconn.Instance.Get<string>("ExportPosCheck");
  1309. if (content == "finish")
  1310. {
  1311. RedisDbconn.Instance.Clear("ExportPosCheck");
  1312. }
  1313. return content;
  1314. }
  1315. #endregion
  1316. #region 更换机具
  1317. public IActionResult ChangePos(string right)
  1318. {
  1319. ViewBag.RightInfo = RightInfo;
  1320. ViewBag.right = right;
  1321. return View();
  1322. }
  1323. [HttpPost]
  1324. public string ChangePosDo(string OldSn, string NewSn, string BackStoreNo, string OutStoreNo, string MerNo = "", int IsSend = 0)
  1325. {
  1326. if (string.IsNullOrEmpty(OldSn))
  1327. {
  1328. return "请输入原机具SN";
  1329. }
  1330. if (string.IsNullOrEmpty(NewSn))
  1331. {
  1332. return "请输入新机具SN";
  1333. }
  1334. string[] OldSnList = OldSn.Split('\n');
  1335. string[] NewSnList = NewSn.Split('\n');
  1336. if (OldSnList.Length != NewSnList.Length)
  1337. {
  1338. return "原机具SN数量和新机具SN数量不一致";
  1339. }
  1340. WebCMSEntities db = new WebCMSEntities();
  1341. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == OldSn);
  1342. StoreForCode storeForBack = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1343. StoreForCode storeForOut = db.StoreForCode.FirstOrDefault(m => m.Code == OutStoreNo) ?? new StoreForCode();
  1344. StoreHouse BackStore = db.StoreHouse.FirstOrDefault(m => m.Id == storeForBack.StoreId) ?? new StoreHouse();
  1345. StoreHouse OutStore = db.StoreHouse.FirstOrDefault(m => m.Id == storeForOut.StoreId) ?? new StoreHouse();
  1346. string ChangeNo = "BMC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3);
  1347. KqProducts oldPosBrand = new KqProducts();
  1348. KqProducts newPosBrand = new KqProducts();
  1349. MachineChange add = db.MachineChange.Add(new MachineChange()
  1350. {
  1351. CreateDate = DateTime.Now,
  1352. UpdateDate = DateTime.Now,
  1353. CreateMan = SysUserName + "-" + SysRealName,
  1354. ChangeNo = ChangeNo, //转换单号
  1355. UserId = 0, //创客
  1356. ChangeTime = DateTime.Now, //转换时间
  1357. BackStoreId = BackStore.Id, //退回仓库
  1358. BackStoreName = BackStore.StoreName, //退回仓库名称
  1359. Remark = "机具更换", //订单备注
  1360. BackStoreUserId = BackStore.UserId, //退回仓库归属人
  1361. OutProductType = int.Parse(OutStore.BrandId), //出库产品类型
  1362. OutProductName = OutStore.ProductName, //出库产品名称
  1363. OutStoreId = OutStore.Id, //出库仓库
  1364. OutStoreName = OutStore.StoreName, //出库仓库名称
  1365. OutStoreAreas = OutStore.Areas, //出库仓库所在地区
  1366. OutStoreAddress = OutStore.Address, //出库仓库地址
  1367. OutStoreManager = "", //出库仓库联系人
  1368. OutStoreManagerMobile = OutStore.ManageMobile, //出库仓库联系人手机号
  1369. }).Entity;
  1370. db.SaveChanges();
  1371. int BackProductType = 0;
  1372. string BackProductName = "";
  1373. string ChangeDeviceName = "";
  1374. string ChangeSnExpand = "";
  1375. for (int i = 0; i < OldSnList.Length; i++)
  1376. {
  1377. string OldSnNum = OldSnList[i];
  1378. string NewSnNum = NewSnList[i];
  1379. MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1380. MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1381. PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId);
  1382. if (oldpos == null)
  1383. {
  1384. return "原机具SN不正确";
  1385. }
  1386. // if (oldpos.BindingState != 1)
  1387. // {
  1388. // return "原机具未绑定";
  1389. // }
  1390. PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId);
  1391. if (newpos == null)
  1392. {
  1393. return "新机具SN不正确";
  1394. }
  1395. oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1396. newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
  1397. PosMerchantInfo merchant = new PosMerchantInfo();
  1398. if (!string.IsNullOrEmpty(MerNo))
  1399. {
  1400. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PosMerchantInfo();
  1401. }
  1402. else
  1403. {
  1404. merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == oldpos.BindMerchantId) ?? new PosMerchantInfo();
  1405. }
  1406. newpos.BindMerchantId = merchant.Id;
  1407. newpos.BuyUserId = oldpos.BuyUserId;
  1408. newpos.UserId = oldpos.UserId;
  1409. newpos.RecycEndDate = oldpos.RecycEndDate;
  1410. newpos.ScanQrTrade = oldpos.ScanQrTrade;
  1411. newpos.DebitCardTrade = oldpos.DebitCardTrade;
  1412. newpos.CreditTrade = oldpos.CreditTrade;
  1413. newpos.PosSnType = oldpos.PosSnType;
  1414. newpos.TransferTime = oldpos.TransferTime;
  1415. newpos.IsPurchase = oldpos.IsPurchase;
  1416. newpos.BindingState = oldpos.BindingState;
  1417. newpos.ActivationState = oldpos.ActivationState;
  1418. newpos.BindingTime = oldpos.BindingTime;
  1419. newpos.ActivationTime = oldpos.ActivationTime;
  1420. newpos.IsFirst = oldpos.IsFirst;
  1421. // bool checkActReward = db.ActiveReward.Any(m => m.KqMerNo == merchant.KqMerNo);
  1422. // if (!checkActReward)
  1423. // {
  1424. newpos.SeoKeyword = oldpos.SeoKeyword;
  1425. newpos.PrizeParams = oldpos.PrizeParams;
  1426. newpos.LeaderUserId = oldpos.LeaderUserId;
  1427. // }
  1428. // oldpos.BuyUserId = 0;
  1429. // oldpos.UserId = 0;
  1430. oldpos.Status = -1;
  1431. //对应调整客小爽企业版数据
  1432. var posInfo = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == oldpos.Id);
  1433. if (posInfo != null)
  1434. {
  1435. posInfo.PosSn = newpos.PosSn;
  1436. posInfo.PosId = newpos.Id;
  1437. db.SaveChanges();
  1438. }
  1439. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo);
  1440. if (forMerNo != null)
  1441. {
  1442. forMerNo.SnId = newpos.Id;
  1443. }
  1444. merchant.KqSnNo = NewSn;
  1445. db.MachineChangeDetail.Add(new MachineChangeDetail()
  1446. {
  1447. CreateDate = DateTime.Now,
  1448. UpdateDate = DateTime.Now,
  1449. CreateMan = SysUserName + "-" + SysRealName,
  1450. ChangeNo = ChangeNo, //订单号
  1451. ChangeId = add.Id, //订单Id
  1452. BackProductType = oldpos.BrandId, //退回产品类型
  1453. BackProductName = oldPosBrand.Name, //退回产品名称
  1454. UserId = 0, //创客
  1455. BackSnNo = oldpos.PosSn, //设备SN编号
  1456. OutProductType = newpos.BrandId, //出库产品类型
  1457. OutProductName = newPosBrand.Name, //出库产品名称
  1458. OutSnNo = newpos.PosSn, //出库设备SN编号
  1459. OutSnType = newpos.PosSnType, //出库SN机具类型
  1460. Remark = "机具更换", //备注
  1461. BackSnType = oldpos.PosSnType, //退回SN机具类型
  1462. });
  1463. db.SaveChanges();
  1464. BackProductType = oldpos.BrandId;
  1465. BackProductName = newPosBrand.Name;
  1466. ChangeDeviceName = oldpos.DeviceName;
  1467. ChangeSnExpand += oldpos.PosSn + "\n";
  1468. PublicFunction.SycnMachineCount(oldpos.BuyUserId, oldpos.BrandId);
  1469. }
  1470. add.BackProductType = BackProductType; //退回产品类型
  1471. add.BackProductName = BackProductName; //退回产品名称
  1472. add.ChangeDeviceName = ChangeDeviceName; //转换机具名称
  1473. add.ChangeDeviceNum = OldSnList.Length; //转换机具数量
  1474. add.ChangeSnExpand = ChangeSnExpand; //机具SN
  1475. BackStore.LaveNum += OldSnList.Length; //退回仓库库存
  1476. OutStore.LaveNum -= OldSnList.Length; //出货仓库库存
  1477. db.SaveChanges();
  1478. db.Dispose();
  1479. if (IsSend == 1)
  1480. {
  1481. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1482. {
  1483. UserId = pos.BuyUserId, //接收创客
  1484. MsgType = 2,
  1485. Title = "换绑成功通知", //标题
  1486. Summary = "您的 " + oldPosBrand.Name + " SN:" + OldSn + "已经成功为客户换绑为" + newPosBrand.Name + "SN:" + NewSn + "请告知客户进行绑定。",
  1487. CreateDate = DateTime.Now,
  1488. }));
  1489. }
  1490. return "success";
  1491. }
  1492. #endregion
  1493. #region 坏机换新
  1494. public IActionResult ChangeBadPos(string right)
  1495. {
  1496. ViewBag.RightInfo = RightInfo;
  1497. ViewBag.right = right;
  1498. return View();
  1499. }
  1500. [HttpPost]
  1501. public string ChangeBadPosDo(string ChangeType, string MakerCode, string StoreCode, string OldSn, string NewSn, string FromStoreNo, string BackStoreNo, int IsSend = 0)
  1502. {
  1503. string[] OldSnList;
  1504. string[] NewSnList;
  1505. decimal amount = 0;
  1506. //创客坏机换新
  1507. if (ChangeType == "0")
  1508. {
  1509. if (string.IsNullOrEmpty(MakerCode))
  1510. {
  1511. return "请输入创客编号";
  1512. }
  1513. if (string.IsNullOrEmpty(OldSn))
  1514. {
  1515. return "请输入坏机SN";
  1516. }
  1517. if (string.IsNullOrEmpty(NewSn))
  1518. {
  1519. return "请输入新机SN";
  1520. }
  1521. if (string.IsNullOrEmpty(FromStoreNo))
  1522. {
  1523. return "请输入新机发货仓库编号";
  1524. }
  1525. if (string.IsNullOrEmpty(BackStoreNo))
  1526. {
  1527. return "请输入坏机退回仓库编号";
  1528. }
  1529. OldSnList = OldSn.Split('\n');
  1530. NewSnList = NewSn.Split('\n');
  1531. if (OldSnList.Length != NewSnList.Length)
  1532. {
  1533. return "原机具SN数量和新机具SN数量不一致";
  1534. }
  1535. for (int i = 0; i < OldSnList.Length; i++)
  1536. {
  1537. string OldSnNum = OldSnList[i];
  1538. string NewSnNum = NewSnList[i];
  1539. UserForMakerCode userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  1540. StoreForCode storeForCodeFrom = db.StoreForCode.FirstOrDefault(m => m.Code == FromStoreNo) ?? new StoreForCode();
  1541. if (storeForCodeFrom.StoreId == 0)
  1542. {
  1543. return "您输入的新机发货仓库编号不存在";
  1544. }
  1545. StoreForCode storeForCodeBack = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1546. if (storeForCodeBack.StoreId == 0)
  1547. {
  1548. return "您输入的坏机退回仓库编号不存在";
  1549. }
  1550. MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1551. MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1552. PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId && m.BindingState == 0 && m.BuyUserId == userForMakerCode.UserId) ?? new PosMachinesTwo();
  1553. if (oldpos.Id == 0)
  1554. {
  1555. return "您输入的坏机SN不存在或者不在" + MakerCode + "名下";
  1556. }
  1557. PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId && m.BindingState == 0) ?? new PosMachinesTwo();
  1558. if (newpos.Id == 0)
  1559. {
  1560. return "您输入的新机SN不存在";
  1561. }
  1562. StoreHouse storeFrom = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeFrom.StoreId);
  1563. if (Convert.ToInt32(storeFrom.BrandId) != Convert.ToInt32(newpos.BrandId))
  1564. {
  1565. return "您输入的新机发货仓库不符新机机具对应品牌";
  1566. }
  1567. if (newpos.StoreId != storeFrom.Id)
  1568. {
  1569. return "您输入的新机SN不在新机发货仓库";
  1570. }
  1571. StoreHouse storeBack = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeBack.StoreId);
  1572. if (Convert.ToInt32(storeBack.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1573. {
  1574. return "您输入的坏机退回仓库不符坏机机具对应品牌";
  1575. }
  1576. if (Convert.ToInt32(storeBack.BrandId) != Convert.ToInt32(storeFrom.BrandId))
  1577. {
  1578. return "您输入的新机发货仓库和坏机退回仓库品牌类型不同";
  1579. }
  1580. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1581. if (brandInfo.Name.Contains("大POS"))
  1582. {
  1583. amount = 300;
  1584. }
  1585. if (brandInfo.Name.Contains("电签"))
  1586. {
  1587. amount = 200;
  1588. }
  1589. // var storeUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == store.UserId) ?? new UserAccount();
  1590. var storeFromUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeFrom.UserId) ?? new UserAccount();
  1591. var storeBackUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeBack.UserId) ?? new UserAccount();
  1592. // storeUserAcount.ValidAmount += amount;
  1593. storeFromUserAcount.ValidAmount += amount;
  1594. storeBackUserAcount.ValidAmount -= amount;
  1595. newpos.BuyUserId = oldpos.BuyUserId;
  1596. newpos.UserId = oldpos.UserId;
  1597. newpos.RecycEndDate = oldpos.RecycEndDate;
  1598. newpos.ScanQrTrade = oldpos.ScanQrTrade;
  1599. newpos.DebitCardTrade = oldpos.DebitCardTrade;
  1600. newpos.CreditTrade = oldpos.CreditTrade;
  1601. newpos.PosSnType = oldpos.PosSnType;
  1602. newpos.TransferTime = oldpos.TransferTime;
  1603. newpos.IsPurchase = oldpos.IsPurchase;
  1604. newpos.BindingState = oldpos.BindingState;
  1605. newpos.ActivationState = oldpos.ActivationState;
  1606. newpos.BindingTime = oldpos.BindingTime;
  1607. newpos.ActivationTime = oldpos.ActivationTime;
  1608. newpos.IsFirst = oldpos.IsFirst;
  1609. newpos.SeoKeyword = oldpos.SeoKeyword;
  1610. newpos.PrizeParams = oldpos.PrizeParams;
  1611. newpos.LeaderUserId = oldpos.LeaderUserId;
  1612. newpos.BindMerchantId = oldpos.BindMerchantId;
  1613. newpos.StoreId = oldpos.StoreId;
  1614. newpos.UserId = oldpos.UserId;
  1615. newpos.PreUserId = oldpos.PreUserId;
  1616. oldpos.StoreId = storeBack.Id;
  1617. oldpos.BuyUserId = 0;
  1618. oldpos.UserId = 0;
  1619. oldpos.Status = -1;
  1620. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == newpos.BindMerchantId);
  1621. if (merchant != null)
  1622. {
  1623. merchant.KqSnNo = newpos.PosSn;
  1624. merchant.ActiveStatus = newpos.ActivationState;
  1625. }
  1626. db.SaveChanges();
  1627. BrokenMachineChange add = db.BrokenMachineChange.Add(new BrokenMachineChange()
  1628. {
  1629. CreateDate = DateTime.Now,
  1630. CreateMan = SysUserName + "-" + SysRealName,
  1631. ChangeNo = "BPC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3), //转换单号
  1632. OutStoreId = storeFrom.Id,
  1633. BackStoreId = storeBack.Id,
  1634. OutProductType = newpos.BrandId,
  1635. BackProductType = oldpos.BrandId,
  1636. UserId = userForMakerCode.UserId, //创客
  1637. }).Entity;
  1638. db.SaveChanges();
  1639. BrokenMachineChangeDetail adds = db.BrokenMachineChangeDetail.Add(new BrokenMachineChangeDetail()
  1640. {
  1641. CreateDate = DateTime.Now,
  1642. CreateMan = SysUserName + "-" + SysRealName,
  1643. OutSnNo = OldSnNum,
  1644. BackSnNo = NewSnNum,
  1645. ChangeNo = add.ChangeNo, //转换单号
  1646. ChangeId = add.Id, //转换记录Id
  1647. UserId = add.UserId, //创客
  1648. }).Entity;
  1649. db.SaveChanges();
  1650. var oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1651. if (IsSend == 1)
  1652. {
  1653. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1654. {
  1655. UserId = userForMakerCode.UserId, //接收创客
  1656. MsgType = 2,
  1657. Title = "坏机换新成功通知", //标题
  1658. Summary = "您的坏机 " + oldPosBrand.Name + " SN:" + OldSnNum + "已经成功为您换为" + oldPosBrand.Name + "SN:" + NewSnNum + "。",
  1659. CreateDate = DateTime.Now,
  1660. }));
  1661. }
  1662. }
  1663. }
  1664. //仓库坏机换新
  1665. else
  1666. {
  1667. if (string.IsNullOrEmpty(StoreCode))
  1668. {
  1669. return "请输入仓库编号";
  1670. }
  1671. if (string.IsNullOrEmpty(OldSn))
  1672. {
  1673. return "请输入坏机SN";
  1674. }
  1675. if (string.IsNullOrEmpty(NewSn))
  1676. {
  1677. return "请输入新机SN";
  1678. }
  1679. if (string.IsNullOrEmpty(FromStoreNo))
  1680. {
  1681. return "请输入新机发货仓库编号";
  1682. }
  1683. if (string.IsNullOrEmpty(BackStoreNo))
  1684. {
  1685. return "请输入坏机退回仓库编号";
  1686. }
  1687. OldSnList = OldSn.Split('\n');
  1688. NewSnList = NewSn.Split('\n');
  1689. if (OldSnList.Length != NewSnList.Length)
  1690. {
  1691. return "原机具SN数量和新机具SN数量不一致";
  1692. }
  1693. for (int i = 0; i < OldSnList.Length; i++)
  1694. {
  1695. string OldSnNum = OldSnList[i];
  1696. string NewSnNum = NewSnList[i];
  1697. StoreHouse store = new StoreHouse();
  1698. StoreForCode storeForBadCode = db.StoreForCode.FirstOrDefault(m => m.Code == StoreCode) ?? new StoreForCode();
  1699. if (storeForBadCode.StoreId > 0)
  1700. {
  1701. store = db.StoreHouse.FirstOrDefault(m => m.Id == storeForBadCode.StoreId);
  1702. }
  1703. StoreForCode storeForCodeFrom = db.StoreForCode.FirstOrDefault(m => m.Code == FromStoreNo) ?? new StoreForCode();
  1704. if (storeForCodeFrom.StoreId == 0)
  1705. {
  1706. return "您输入的新机发货仓库编号不存在";
  1707. }
  1708. StoreForCode storeForCodeBack = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1709. if (storeForCodeBack.StoreId == 0)
  1710. {
  1711. return "您输入的坏机退回仓库编号不存在";
  1712. }
  1713. MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1714. MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1715. PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId && m.BindingState == 0 && m.StoreId == storeForBadCode.StoreId) ?? new PosMachinesTwo();
  1716. if (oldpos.Id == 0)
  1717. {
  1718. return "您输入的坏机SN不存在或者不在仓库" + StoreCode + "中";
  1719. }
  1720. PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId && m.BindingState == 0) ?? new PosMachinesTwo();
  1721. if (newpos.Id == 0)
  1722. {
  1723. return "您输入的新机SN不存在";
  1724. }
  1725. StoreHouse storeFrom = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeFrom.StoreId);
  1726. if (Convert.ToInt32(storeFrom.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1727. {
  1728. return "您输入的新机发货仓库不符新机机具对应品牌";
  1729. }
  1730. if (newpos.StoreId != storeFrom.Id)
  1731. {
  1732. return "您输入的新机SN不在新机发货仓库";
  1733. }
  1734. StoreHouse storeBack = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCodeBack.StoreId);
  1735. if (Convert.ToInt32(storeBack.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1736. {
  1737. return "您输入的坏机退回仓库不符坏机机具对应品牌";
  1738. }
  1739. var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1740. if (brandInfo.Name.Contains("大POS"))
  1741. {
  1742. amount = 300;
  1743. }
  1744. if (brandInfo.Name.Contains("电签"))
  1745. {
  1746. amount = 200;
  1747. }
  1748. var storeUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == store.UserId) ?? new UserAccount();
  1749. var storeFromUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeFrom.UserId) ?? new UserAccount();
  1750. var storeBackUserAcount = db.UserAccount.FirstOrDefault(m => m.Id == storeBack.UserId) ?? new UserAccount();
  1751. // storeUserAcount.ValidAmount += amount;
  1752. storeFromUserAcount.ValidAmount += amount;
  1753. storeBackUserAcount.ValidAmount -= amount;
  1754. newpos.StoreId = oldpos.StoreId;
  1755. newpos.BuyUserId = oldpos.BuyUserId;
  1756. newpos.UserId = oldpos.UserId;
  1757. newpos.LeaderUserId = oldpos.LeaderUserId;
  1758. newpos.PreUserId = oldpos.PreUserId;
  1759. newpos.IsFirst = oldpos.IsFirst;
  1760. newpos.TransferTime = oldpos.TransferTime;
  1761. oldpos.StoreId = storeBack.Id;
  1762. oldpos.BuyUserId = 0;
  1763. oldpos.UserId = 0;
  1764. oldpos.Status = -1;
  1765. db.SaveChanges();
  1766. BrokenMachineChange add = db.BrokenMachineChange.Add(new BrokenMachineChange()
  1767. {
  1768. CreateDate = DateTime.Now,
  1769. CreateMan = SysUserName + "-" + SysRealName,
  1770. ChangeNo = "BPC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3), //转换单号
  1771. OutStoreId = storeFrom.Id,
  1772. BackStoreId = storeBack.Id,
  1773. OutProductType = newpos.BrandId,
  1774. BackProductType = oldpos.BrandId,
  1775. UserId = store.UserId, //创客
  1776. }).Entity;
  1777. db.SaveChanges();
  1778. BrokenMachineChangeDetail adds = db.BrokenMachineChangeDetail.Add(new BrokenMachineChangeDetail()
  1779. {
  1780. CreateDate = DateTime.Now,
  1781. CreateMan = SysUserName + "-" + SysRealName,
  1782. OutSnNo = OldSnNum,
  1783. BackSnNo = NewSnNum,
  1784. ChangeNo = add.ChangeNo, //转换单号
  1785. ChangeId = add.Id, //转换记录Id
  1786. UserId = add.UserId, //创客
  1787. }).Entity;
  1788. db.SaveChanges();
  1789. var oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1790. if (IsSend == 1)
  1791. {
  1792. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1793. {
  1794. UserId = store.UserId, //接收创客
  1795. MsgType = 2,
  1796. Title = "坏机换新成功通知", //标题
  1797. Summary = "您的坏机 " + oldPosBrand.Name + " SN:" + OldSnNum + "已经成功为您换为" + oldPosBrand.Name + "SN:" + NewSnNum + "。",
  1798. CreateDate = DateTime.Now,
  1799. }));
  1800. }
  1801. }
  1802. }
  1803. // for (int i = 0; i < OldSnList.Length; i++)
  1804. // {
  1805. // string OldSnNum = OldSnList[i];
  1806. // string NewSnNum = NewSnList[i];
  1807. // StoreForCode storeForCode = db.StoreForCode.FirstOrDefault(m => m.Code == BackStoreNo) ?? new StoreForCode();
  1808. // MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new MachineForSnNo();
  1809. // MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new MachineForSnNo();
  1810. // StoreHouse oldStore = new StoreHouse();
  1811. // PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId && m.BindingState == 0);
  1812. // if (storeForCode.StoreId > 0)
  1813. // {
  1814. // StoreHouse store = db.StoreHouse.FirstOrDefault(m => m.Id == storeForCode.StoreId);
  1815. // if (Convert.ToInt32(store.BrandId) != Convert.ToInt32(oldpos.BrandId))
  1816. // {
  1817. // return "您输入的仓库不符机具对应品牌";
  1818. // }
  1819. // oldStore = db.StoreHouse.FirstOrDefault(m => m.Id == oldpos.StoreId);
  1820. // oldpos.Status = -1;
  1821. // oldpos.StoreId = store.Id;
  1822. // }
  1823. // if (oldpos == null)
  1824. // {
  1825. // return "原机具SN不正确或者不符合换机条件";
  1826. // }
  1827. // PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId && m.BuyUserId == 0 && m.UserId == 0 && m.BindingState == 0);
  1828. // if (newpos == null)
  1829. // {
  1830. // return "新机具SN不正确或者不为仓库机";
  1831. // }
  1832. // if (oldpos.BrandId != newpos.BrandId)
  1833. // {
  1834. // return "新机具和旧机具品牌不相同";
  1835. // }
  1836. // var brandInfo = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
  1837. // if (brandInfo.Name.Contains("大POS"))
  1838. // {
  1839. // amount = 300;
  1840. // }
  1841. // if (brandInfo.Name.Contains("电签"))
  1842. // {
  1843. // amount = 200;
  1844. // }
  1845. // var storehouse = db.StoreHouse.FirstOrDefault(m => m.Id == newpos.StoreId) ?? new StoreHouse();
  1846. // storehouse.LaveNum -= 1;
  1847. // var userAcount = db.UserAccount.FirstOrDefault(m => m.Id == storehouse.UserId) ?? new UserAccount();
  1848. // if (oldpos.StoreId != newpos.StoreId)
  1849. // {
  1850. // userAcount.ValidAmount += amount;
  1851. // newpos.StoreId = oldpos.StoreId;
  1852. // newpos.BuyUserId = oldpos.BuyUserId;
  1853. // newpos.UserId = oldpos.UserId;
  1854. // newpos.LeaderUserId = oldpos.LeaderUserId;
  1855. // newpos.PreUserId = oldpos.PreUserId;
  1856. // newpos.IsFirst = oldpos.IsFirst;
  1857. // newpos.TransferTime = oldpos.TransferTime;
  1858. // }
  1859. // else
  1860. // {
  1861. // newpos.BuyUserId = oldpos.BuyUserId;
  1862. // newpos.UserId = oldpos.UserId;
  1863. // newpos.LeaderUserId = oldpos.LeaderUserId;
  1864. // newpos.PreUserId = oldpos.PreUserId;
  1865. // newpos.IsFirst = oldpos.IsFirst;
  1866. // newpos.TransferTime = oldpos.TransferTime;
  1867. // }
  1868. // db.SaveChanges();
  1869. // var oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new KqProducts();
  1870. // var newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new KqProducts();
  1871. // var UserId = 0;
  1872. // if (oldpos.BuyUserId == 0)
  1873. // {
  1874. // UserId = oldStore.UserId;
  1875. // }
  1876. // else
  1877. // {
  1878. // UserId = oldpos.BuyUserId;
  1879. // }
  1880. // BrokenMachineChange add = db.BrokenMachineChange.Add(new BrokenMachineChange()
  1881. // {
  1882. // CreateDate = DateTime.Now,
  1883. // CreateMan = SysUserName + "-" + SysRealName,
  1884. // ChangeNo = "BPC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3), //转换单号
  1885. // UserId = UserId, //创客
  1886. // }).Entity;
  1887. // db.SaveChanges();
  1888. // BrokenMachineChangeDetail adds = db.BrokenMachineChangeDetail.Add(new BrokenMachineChangeDetail()
  1889. // {
  1890. // CreateDate = DateTime.Now,
  1891. // CreateMan = SysUserName + "-" + SysRealName,
  1892. // OutSnNo = OldSnNum,
  1893. // BackSnNo = NewSnNum,
  1894. // ChangeNo = add.ChangeNo, //转换单号
  1895. // ChangeId = add.Id, //转换记录Id
  1896. // UserId = add.UserId, //创客
  1897. // }).Entity;
  1898. // db.SaveChanges();
  1899. // if (IsSend == 1)
  1900. // {
  1901. // RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  1902. // {
  1903. // UserId = UserId, //接收创客
  1904. // MsgType = 2,
  1905. // Title = "坏机换新成功通知", //标题
  1906. // Summary = "您的坏机 " + oldPosBrand.Name + " SN:" + OldSn + "已经成功为您换为" + newPosBrand.Name + "SN:" + NewSn + "。",
  1907. // CreateDate = DateTime.Now,
  1908. // }));
  1909. // }
  1910. // }
  1911. return "success";
  1912. }
  1913. #endregion
  1914. #region 机具解绑
  1915. public IActionResult Unbind(string right)
  1916. {
  1917. ViewBag.RightInfo = RightInfo;
  1918. ViewBag.right = right;
  1919. return View();
  1920. }
  1921. [HttpPost]
  1922. public string UnbindDo(string PosSn, string MakerCode, string MerNo, int IsSend = 0)
  1923. {
  1924. if (string.IsNullOrEmpty(PosSn))
  1925. {
  1926. return "请输入机具SN";
  1927. }
  1928. if (string.IsNullOrEmpty(MakerCode))
  1929. {
  1930. return "请输入创客编号";
  1931. }
  1932. MachineForSnNo forSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn);
  1933. if (forSnNo == null)
  1934. {
  1935. return "机具SN不存在";
  1936. }
  1937. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forSnNo.SnId);
  1938. if (pos == null)
  1939. {
  1940. return "机具SN不存在";
  1941. }
  1942. if (string.IsNullOrEmpty(MerNo) && pos.BrandId != 1 && pos.BrandId != 3)
  1943. {
  1944. return "请输入商户编号";
  1945. }
  1946. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  1947. if (forMakerCode == null)
  1948. {
  1949. return "创客不存在";
  1950. }
  1951. MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
  1952. if (forMerNo == null && pos.BrandId != 1 && pos.BrandId != 3)
  1953. {
  1954. return "商户编号不存在";
  1955. }
  1956. if (pos.BuyUserId != forMakerCode.UserId)
  1957. {
  1958. return "机具SN和创客不匹配";
  1959. }
  1960. if (pos.ActivationState == 1)
  1961. {
  1962. return "机具已激活,不能解绑";
  1963. }
  1964. if (pos.BuyUserId == 0)
  1965. {
  1966. return "机具是仓库机,不支持解绑";
  1967. }
  1968. if (pos.BindingState == 0)
  1969. {
  1970. return "机具未绑定,不支持解绑";
  1971. }
  1972. if (pos.BindingTime < DateTime.Now.AddDays(-30) && SysUserName != "admin")
  1973. {
  1974. return "机具绑定已超过30天,不支持解绑";
  1975. }
  1976. if (forMerNo != null)
  1977. {
  1978. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  1979. if (merchant != null)
  1980. {
  1981. //机具解绑删除客小爽企业版对应数据
  1982. var merInfo = db.BusinessPartnerMerchant.FirstOrDefault(m => m.MerchantId == merchant.Id);
  1983. if (merInfo != null)
  1984. {
  1985. db.BusinessPartnerMerchant.Remove(merInfo);
  1986. }
  1987. var posInfo = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == pos.Id);
  1988. if (posInfo != null)
  1989. {
  1990. db.BusinessPartnerPos.Remove(posInfo);
  1991. }
  1992. db.PosMerchantInfo.Remove(merchant);
  1993. }
  1994. db.MachineForMerNo.Remove(forMerNo);
  1995. db.SaveChanges();
  1996. }
  1997. db.MachineUnBind.Add(new MachineUnBind()
  1998. {
  1999. CreateDate = DateTime.Now,
  2000. SeoTitle = SysUserName + "-" + SysRealName,
  2001. MerchantId = pos.BindMerchantId,
  2002. AuditDate = DateTime.Now,
  2003. AuditDesc = "平台操作解绑",
  2004. AuditStatus = 1,
  2005. SnNo = pos.PosSn,
  2006. BrandId = pos.BrandId,
  2007. UserId = pos.BuyUserId,
  2008. ApplyNo = "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  2009. });
  2010. pos.BindMerchantId = 0;
  2011. pos.BindingState = 0;
  2012. pos.BindingTime = DateTime.Parse("1900-01-01");
  2013. pos.UserId = pos.BuyUserId;
  2014. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  2015. db.SaveChanges();
  2016. PublicFunction.SycnMachineCount(pos.BuyUserId, pos.BrandId);
  2017. if (IsSend == 1)
  2018. {
  2019. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  2020. {
  2021. UserId = pos.BuyUserId, //接收创客
  2022. MsgType = 2,
  2023. Title = "解绑成功通知", //标题
  2024. Summary = "" + PosSn + "机具已成功解绑,当前机具如需绑定同一客户请再2个工作日后再进行绑定,直接绑定会导致数控异常,相关损失由您本人承担。",
  2025. CreateDate = DateTime.Now,
  2026. }));
  2027. }
  2028. return "success";
  2029. }
  2030. #endregion
  2031. #region 通过券码修改盟主标记
  2032. public IActionResult ChangeSignQuan(string right)
  2033. {
  2034. ViewBag.RightInfo = RightInfo;
  2035. ViewBag.right = right;
  2036. return View();
  2037. }
  2038. [HttpPost]
  2039. public string ChangeSignQuanDo(string Coupons, string MakerCode)
  2040. {
  2041. if (string.IsNullOrEmpty(Coupons))
  2042. {
  2043. return "请输入机具券码,多个券码用回车分隔";
  2044. }
  2045. if (string.IsNullOrEmpty(MakerCode))
  2046. {
  2047. return "请输入创客编号";
  2048. }
  2049. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  2050. if (forMakerCode == null)
  2051. {
  2052. return "创客不存在";
  2053. }
  2054. string[] SnList = Coupons.Split('\n');
  2055. foreach (var item in SnList)
  2056. {
  2057. var coupos = db.PosCoupons.FirstOrDefault(m => m.ExchangeCode == item && m.LeaderUserId == 0);
  2058. if (coupos == null)
  2059. {
  2060. return "机具券不存在";
  2061. }
  2062. else
  2063. {
  2064. coupos.LeaderUserId = forMakerCode.UserId;
  2065. }
  2066. }
  2067. db.SaveChanges();
  2068. return "success";
  2069. }
  2070. #endregion
  2071. #region 通过机具SN修改盟主标记
  2072. public IActionResult ChangeSignSn(string right)
  2073. {
  2074. ViewBag.RightInfo = RightInfo;
  2075. ViewBag.right = right;
  2076. return View();
  2077. }
  2078. [HttpPost]
  2079. public string ChangeSignSnDo(string PosSn, string MakerCode)
  2080. {
  2081. if (string.IsNullOrEmpty(PosSn))
  2082. {
  2083. return "请输入机具SN,多个SN用回车分隔";
  2084. }
  2085. if (string.IsNullOrEmpty(MakerCode))
  2086. {
  2087. return "请输入创客编号";
  2088. }
  2089. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  2090. if (forMakerCode == null)
  2091. {
  2092. return "创客不存在";
  2093. }
  2094. string[] SnList = PosSn.Split('\n');
  2095. foreach (var item in SnList)
  2096. {
  2097. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == item && m.LeaderUserId == 0);
  2098. if (pos == null)
  2099. {
  2100. return "该机具不存在";
  2101. }
  2102. else
  2103. {
  2104. pos.LeaderUserId = forMakerCode.UserId;
  2105. }
  2106. }
  2107. db.SaveChanges();
  2108. return "success";
  2109. }
  2110. #endregion
  2111. #region 机具循环列表
  2112. /// <summary>
  2113. /// 根据条件查询机具循环列表
  2114. /// </summary>
  2115. /// <returns></returns>
  2116. public IActionResult Loop(string right, string SwapSnExpand, string SnNos)
  2117. {
  2118. ViewBag.RightInfo = RightInfo;
  2119. ViewBag.right = right;
  2120. ViewBag.SwapSnExpand = SwapSnExpand;
  2121. ViewBag.SnNos = SnNos;
  2122. return View();
  2123. }
  2124. #endregion
  2125. #region 根据条件查询机具循环列表
  2126. /// <summary>
  2127. /// 机具循环列表
  2128. /// </summary>
  2129. /// <returns></returns>
  2130. public IActionResult LoopData(string SwapSnExpand, string SnNos, int page = 1, int limit = 30)
  2131. {
  2132. Dictionary<string, string> Fields = new Dictionary<string, string>();
  2133. Fields.Add("SwapSnExpand", "1"); //申请机具SN
  2134. Fields.Add("SnNos", "1"); //发货机具SN
  2135. List<Dictionary<string, object>> dataList = new List<Dictionary<string, object>>();
  2136. if (!string.IsNullOrEmpty(SwapSnExpand))
  2137. {
  2138. var apply = db.MachineApply.FirstOrDefault(m => m.Status > -1 && m.SwapSnExpand.Contains(SwapSnExpand)) ?? new MachineApply();
  2139. var order = db.Orders.FirstOrDefault(m => m.Id == apply.QueryCount) ?? new Orders();
  2140. var user = db.Users.FirstOrDefault(m => m.Id == apply.UserId) ?? new Users();
  2141. string[] SnNoList = function.CheckNull(apply.SwapSnExpand).TrimEnd('\n').Split('\n');
  2142. string[] OrderSnNo = function.CheckNull(order.SnNos).Split(',');
  2143. for (int i = 0; i < SnNoList.Length; i++)
  2144. {
  2145. Dictionary<string, object> data = new Dictionary<string, object>();
  2146. data["ApplyNo"] = apply.ApplyNo;
  2147. data["OrderNo"] = order.OrderNo;
  2148. data["MakerCode"] = user.MakerCode;
  2149. data["RealName"] = user.RealName;
  2150. if (SnNoList.Length > i)
  2151. {
  2152. string SourceSn = SnNoList[i].Split(':')[0];
  2153. data["ComeSn"] = SnNoList[i];
  2154. data["ComeSnParam"] = SourceSn;
  2155. MachineForSnNo machineFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSn) ?? new MachineForSnNo();
  2156. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  2157. if (pos.LeaderUserId > 0)
  2158. {
  2159. Users leader = db.Users.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new Users();
  2160. data["ComeSnMakerCode"] = leader.MakerCode;
  2161. data["ComeSnRealName"] = leader.RealName;
  2162. }
  2163. }
  2164. if (OrderSnNo.Length > i)
  2165. {
  2166. string SendSn = OrderSnNo[i];
  2167. data["SendSn"] = OrderSnNo[i];
  2168. MachineForSnNo machineFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SendSn) ?? new MachineForSnNo();
  2169. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  2170. if (pos.LeaderUserId > 0)
  2171. {
  2172. Users leader = db.Users.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new Users();
  2173. data["SendSnMakerCode"] = leader.MakerCode;
  2174. data["SendSnRealName"] = leader.RealName;
  2175. }
  2176. }
  2177. data["CreateDate"] = apply.CreateDate == null ? "" : apply.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  2178. data["Status"] = order.Status == 2 ? "已发货" : "未发货";
  2179. dataList.Add(data);
  2180. }
  2181. }
  2182. else if (!string.IsNullOrEmpty(SnNos))
  2183. {
  2184. var order = db.Orders.FirstOrDefault(m => m.Status > -1 && m.SnNos.Contains(SnNos)) ?? new Orders();
  2185. var apply = db.MachineApply.FirstOrDefault(m => m.Id == order.Sort) ?? new MachineApply();
  2186. var user = db.Users.FirstOrDefault(m => m.Id == apply.UserId) ?? new Users();
  2187. string[] SnNoList = function.CheckNull(apply.SwapSnExpand).TrimEnd('\n').Split('\n');
  2188. string[] OrderSnNo = function.CheckNull(order.SnNos).Split(',');
  2189. for (int i = 0; i < OrderSnNo.Length; i++)
  2190. {
  2191. Dictionary<string, object> data = new Dictionary<string, object>();
  2192. data["ApplyNo"] = apply.ApplyNo;
  2193. data["OrderNo"] = order.OrderNo;
  2194. data["MakerCode"] = user.MakerCode;
  2195. data["RealName"] = user.RealName;
  2196. if (SnNoList.Length > i)
  2197. {
  2198. string SourceSn = SnNoList[i].Split(':')[0];
  2199. data["ComeSn"] = SnNoList[i];
  2200. data["ComeSnParam"] = SourceSn;
  2201. MachineForSnNo machineFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SourceSn) ?? new MachineForSnNo();
  2202. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  2203. if (pos.LeaderUserId > 0)
  2204. {
  2205. Users leader = db.Users.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new Users();
  2206. data["ComeSnMakerCode"] = leader.MakerCode;
  2207. data["ComeSnRealName"] = leader.RealName;
  2208. }
  2209. }
  2210. if (OrderSnNo.Length > i)
  2211. {
  2212. string SendSn = OrderSnNo[i];
  2213. data["SendSn"] = OrderSnNo[i];
  2214. MachineForSnNo machineFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == SendSn) ?? new MachineForSnNo();
  2215. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineFor.SnId) ?? new PosMachinesTwo();
  2216. if (pos.LeaderUserId > 0)
  2217. {
  2218. Users leader = db.Users.FirstOrDefault(m => m.Id == pos.LeaderUserId) ?? new Users();
  2219. data["SendSnMakerCode"] = leader.MakerCode;
  2220. data["SendSnRealName"] = leader.RealName;
  2221. }
  2222. }
  2223. data["CreateDate"] = apply.CreateDate == null ? "" : apply.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  2224. data["Status"] = order.Status == 2 ? "已发货" : "未发货";
  2225. dataList.Add(data);
  2226. }
  2227. }
  2228. Dictionary<string, object> obj = new Dictionary<string, object>();
  2229. obj.Add("code", 0);
  2230. obj.Add("msg", "");
  2231. obj.Add("count", dataList.Count);
  2232. obj.Add("data", dataList);
  2233. return Json(obj);
  2234. }
  2235. #endregion
  2236. #region 查询创客直推奖
  2237. public IActionResult Prize100(string right)
  2238. {
  2239. ViewBag.RightInfo = RightInfo;
  2240. ViewBag.right = right;
  2241. return View();
  2242. }
  2243. [HttpPost]
  2244. public Dictionary<string, string> Prize100Do(string MakerCode)
  2245. {
  2246. Dictionary<string, string> dic = new Dictionary<string, string>();
  2247. string result = "";
  2248. UserForMakerCode forMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode);
  2249. if (forMakerCode == null)
  2250. {
  2251. result += "创客编号不存在\n";
  2252. dic.Add("code", "1");
  2253. dic.Add("result", result);
  2254. return dic;
  2255. }
  2256. int OrderCount = db.Orders.Count(m => m.UserId == forMakerCode.UserId && m.PayStatus == 1 && m.TotalPrice == 600);
  2257. if (OrderCount == 0)
  2258. {
  2259. result += "该创客未下单\n";
  2260. dic.Add("code", "1");
  2261. dic.Add("result", result);
  2262. return dic;
  2263. }
  2264. if (OrderCount > 1)
  2265. {
  2266. result += "该创客已不是首笔下单\n";
  2267. dic.Add("code", "1");
  2268. dic.Add("result", result);
  2269. return dic;
  2270. }
  2271. Orders order = db.Orders.FirstOrDefault(m => m.UserId == forMakerCode.UserId && m.PayStatus == 1 && m.TotalPrice == 600) ?? new Orders();
  2272. if (order.PayStatus < 1)
  2273. {
  2274. result += "未付款\n";
  2275. dic.Add("code", "1");
  2276. dic.Add("result", result);
  2277. return dic;
  2278. }
  2279. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId) ?? new Users();
  2280. Users puser = db.Users.FirstOrDefault(m => m.Id == user.ParentUserId) ?? new Users();
  2281. result += "订单号:" + order.OrderNo + "\n";
  2282. result += "下单创客\n创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile + "\n";
  2283. result += "上级创客\n创客编号:" + puser.MakerCode + ",姓名:" + puser.RealName + ",手机号:" + puser.Mobile + "\n";
  2284. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  2285. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  2286. if (machineCount + ActiveCount >= 3)
  2287. {
  2288. string IsGet = "未发放";
  2289. int ToUserId = 0;
  2290. UserAccountRecord item = db.UserAccountRecord.FirstOrDefault(m => m.QueryCount == order.Id && m.ChangeType == 112);
  2291. if (item == null)
  2292. {
  2293. DateTime start = order.PayDate.Value.AddMinutes(-2);
  2294. DateTime end = order.PayDate.Value.AddMinutes(2);
  2295. item = db.UserAccountRecord.FirstOrDefault(m => m.UserId == puser.Id && m.ChangeType == 112 && m.ChangeAmount == 100 && m.CreateDate >= start && m.CreateDate <= end);
  2296. }
  2297. if (item != null)
  2298. {
  2299. IsGet = "已发放(发放时间:" + item.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + ")";
  2300. ToUserId = item.UserId;
  2301. dic.Add("code", "1");
  2302. }
  2303. else
  2304. {
  2305. dic.Add("id", order.Id.ToString());
  2306. dic.Add("code", "0");
  2307. }
  2308. Users touser = db.Users.FirstOrDefault(m => m.Id == ToUserId) ?? new Users();
  2309. result += "是否发放:" + IsGet + "\n";
  2310. result += "应该发放对象\n创客编号:" + puser.MakerCode + ",姓名:" + puser.RealName + ",手机号:" + puser.Mobile + "\n";
  2311. result += "实际发放对象\n创客编号:" + touser.MakerCode + ",姓名:" + touser.RealName + ",手机号:" + touser.Mobile + "\n";
  2312. }
  2313. else
  2314. {
  2315. result += "上级创客未满足条件\n购买机:" + machineCount + "台,激活机:" + ActiveCount + "台\n";
  2316. dic.Add("code", "1");
  2317. }
  2318. dic.Add("result", result);
  2319. return dic;
  2320. }
  2321. public string AddPrize100(int Id)
  2322. {
  2323. Orders order = db.Orders.FirstOrDefault(m => m.Id == Id);
  2324. if (order != null)
  2325. {
  2326. //机具券逻辑
  2327. OrderProduct pro = db.OrderProduct.FirstOrDefault(m => m.OrderId == Id);
  2328. if (pro != null)
  2329. {
  2330. //推荐下单奖励
  2331. if (pro.ProductId == 10 || pro.ProductId == 11)
  2332. {
  2333. int OrderId = order.Id;
  2334. bool checkPrize = db.UserAccountRecord.Any(m => m.QueryCount == OrderId && m.ChangeType == 112);
  2335. if (!checkPrize)
  2336. {
  2337. Users user = db.Users.FirstOrDefault(m => m.Id == order.UserId);
  2338. if (user != null)
  2339. {
  2340. bool directPrize = false; //直推奖标记
  2341. int ParentUserId = user.ParentUserId;
  2342. // List<int> proids = new List<int>();
  2343. // proids.Add(10);
  2344. // proids.Add(11);
  2345. while (ParentUserId > 0)
  2346. {
  2347. Users puser = db.Users.FirstOrDefault(m => m.Id == ParentUserId) ?? new Users();
  2348. int machineCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.PosSnType == 0); //判断是否拥有3台购买机
  2349. int ActiveCount = db.PosMachinesTwo.Count(m => m.UserId == ParentUserId && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  2350. int couponCount = db.PosCoupons.Count(m => m.UserId == ParentUserId && m.IsUse == 0); //判断是否拥有3张券
  2351. if ((machineCount + ActiveCount + couponCount >= 3 || puser.LeaderLevel > 0) && !directPrize)
  2352. {
  2353. DirectPrize(db, order.Id, ParentUserId, pro.ProductCount);
  2354. directPrize = true;
  2355. db.SaveChanges();
  2356. return "发放成功";
  2357. }
  2358. ParentUserId = puser.ParentUserId;
  2359. }
  2360. }
  2361. }
  2362. return "已发放,请勿重复发放";
  2363. }
  2364. }
  2365. }
  2366. return "操作失败";
  2367. }
  2368. public void DirectPrize(WebCMSEntities db, int OrderId, int UserId, int Count = 1)
  2369. {
  2370. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == UserId);
  2371. if (account == null)
  2372. {
  2373. account = db.UserAccount.Add(new UserAccount()
  2374. {
  2375. Id = UserId,
  2376. UserId = UserId,
  2377. }).Entity;
  2378. db.SaveChanges();
  2379. }
  2380. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  2381. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  2382. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  2383. account.BalanceAmount += 100 * Count;
  2384. account.TotalAmount += 100 * Count;
  2385. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  2386. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  2387. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  2388. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  2389. {
  2390. CreateDate = DateTime.Now,
  2391. UpdateDate = DateTime.Now,
  2392. UserId = UserId, //创客
  2393. ChangeType = 112, //变动类型
  2394. ChangeAmount = 100 * Count, //变更金额
  2395. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  2396. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  2397. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  2398. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  2399. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  2400. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  2401. QueryCount = OrderId,
  2402. }).Entity;
  2403. db.SaveChanges();
  2404. }
  2405. #endregion
  2406. #region 查询机具开机奖
  2407. public IActionResult OpenPrize(string right)
  2408. {
  2409. ViewBag.RightInfo = RightInfo;
  2410. ViewBag.right = right;
  2411. return View();
  2412. }
  2413. [HttpPost]
  2414. public Dictionary<string, string> OpenPrizeDo(string PosSn)
  2415. {
  2416. Dictionary<string, string> dic = new Dictionary<string, string>();
  2417. string result = "";
  2418. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  2419. if (pos == null)
  2420. {
  2421. result += "机具SN号不存在\n";
  2422. dic.Add("code", "1");
  2423. dic.Add("result", result);
  2424. return dic;
  2425. }
  2426. if (pos.BindingState == 0)
  2427. {
  2428. result += "机具未绑定\n";
  2429. dic.Add("code", "1");
  2430. dic.Add("result", result);
  2431. return dic;
  2432. }
  2433. if (pos.ActivationState == 0)
  2434. {
  2435. result += "机具未激活\n";
  2436. dic.Add("code", "1");
  2437. dic.Add("result", result);
  2438. return dic;
  2439. }
  2440. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  2441. if (merchant == null)
  2442. {
  2443. result += "商户不存在\n";
  2444. dic.Add("code", "1");
  2445. dic.Add("result", result);
  2446. return dic;
  2447. }
  2448. Users buyuser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  2449. result += "机具号:" + PosSn + "\n";
  2450. result += "机具所属人:\n创客编号:" + buyuser.MakerCode + ",姓名:" + buyuser.RealName + ",手机号:" + buyuser.Mobile + "\n";
  2451. result += "商户信息\n商户编号:" + merchant.KqMerNo + ",姓名:" + merchant.MerchantName + ",手机号:" + merchant.MerchantMobile + "\n";
  2452. string OpenUser10 = "未领取";
  2453. string OpenUser20 = "未领取";
  2454. var OpenRewardDetail = db.OpenRewardDetail.Where(m => m.SnNo == pos.PosSn).ToList();
  2455. OpenRewardDetail openreward = OpenRewardDetail.FirstOrDefault(m => m.CreditRewardAmount == 20) ?? new OpenRewardDetail();
  2456. if (openreward.Id > 0)
  2457. {
  2458. Users user = db.Users.FirstOrDefault(m => m.Id == openreward.UserId) ?? new Users();
  2459. OpenUser20 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  2460. }
  2461. openreward = OpenRewardDetail.FirstOrDefault(m => m.CreditRewardAmount == 10) ?? new OpenRewardDetail();
  2462. if (openreward.Id > 0)
  2463. {
  2464. Users user = db.Users.FirstOrDefault(m => m.Id == openreward.UserId) ?? new Users();
  2465. OpenUser10 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  2466. }
  2467. result += "开机奖20:" + OpenUser20 + "\n";
  2468. result += "开机奖10:" + OpenUser10 + "\n";
  2469. dic.Add("id", pos.Id.ToString());
  2470. dic.Add("code", "0");
  2471. dic.Add("result", result);
  2472. return dic;
  2473. }
  2474. public string AddOpenPrize(int Id)
  2475. {
  2476. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Id && m.ActivationState == 1);
  2477. if (pos == null)
  2478. {
  2479. return "机具未激活";
  2480. }
  2481. if (pos.BindingTime < DateTime.Now.AddDays(-30))
  2482. {
  2483. return "绑定已超过30天";
  2484. }
  2485. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  2486. if (ActPrize == 0)
  2487. {
  2488. return "机具无押金";
  2489. }
  2490. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  2491. if (merchant == null)
  2492. {
  2493. return "商户不存在";
  2494. }
  2495. bool check = db.OpenRewardDetail.Any(m => m.SnNo == pos.PosSn);
  2496. if (check)
  2497. {
  2498. return "开机奖励已发放,请勿重复操作";
  2499. }
  2500. // 从机具所属人上级开始分开机奖励
  2501. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId);
  2502. if (user != null)
  2503. {
  2504. if (!string.IsNullOrEmpty(user.ParentNav))
  2505. {
  2506. decimal Prize = 20;
  2507. List<string> PrizeFlag = new List<string>();
  2508. string[] ParentNavs = user.ParentNav.Trim(',').Replace(",,", ",").Split(',');
  2509. for (int i = ParentNavs.Length - 1; i >= 0; i--)
  2510. {
  2511. int UserId = int.Parse(ParentNavs[i]);
  2512. Users puser = db.Users.FirstOrDefault(m => m.Id == UserId && m.AuthFlag == 1);
  2513. if (puser != null && Prize > 0)
  2514. {
  2515. int machineCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.PosSnType == 0); //判断是否拥有3台购买机
  2516. int ActiveCount = db.PosMachinesTwo.Count(m => m.BuyUserId == puser.Id && m.ActivationState == 1); //判断是否拥有3台激活机(不限购买或赠送)
  2517. int couponCount = db.PosCoupons.Count(m => m.UserId == puser.Id && m.IsUse == 0); //判断是否拥有3张券
  2518. if (machineCount + ActiveCount + couponCount >= 3)
  2519. {
  2520. int pTopUserId = 0;
  2521. if (!string.IsNullOrEmpty(puser.ParentNav))
  2522. {
  2523. pTopUserId = int.Parse(puser.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  2524. }
  2525. Users machineUser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  2526. OpenRewardDetail detail = db.OpenRewardDetail.Add(new OpenRewardDetail()
  2527. {
  2528. CreateDate = DateTime.Now,
  2529. UpdateDate = DateTime.Now,
  2530. TradeMonth = DateTime.Now.ToString("yyyyMM"), //交易月
  2531. TradeDate = pos.ActivationTime, //达标日期
  2532. UserId = puser.Id, //创客
  2533. BrandId = pos.BrandId, //品牌
  2534. ProductName = RelationClass.GetKqProductBrandInfo(pos.BrandId), //产品名称
  2535. MerchantId = pos.BindMerchantId, //商户
  2536. DirectUserId = merchant.UserId, //商户直属人
  2537. SnNo = pos.PosSn, //SN号
  2538. MerNo = merchant.KqMerNo, //渠道商户号
  2539. SnType = pos.PosSnType, //机具类型
  2540. StandardDate = pos.ActivationTime, //商户的激活日期
  2541. SnStoreId = pos.StoreId, //SN仓库
  2542. MerBuddyType = puser.MerchantType, //商户创客类型
  2543. RewardType = 1, //奖励类型 1-开机直接奖励,2-开机间接奖励
  2544. RewardTips = "开机奖励", //奖励描述
  2545. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  2546. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  2547. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  2548. RewardDesc = "开机奖励", //奖励描述
  2549. TopUserId = pTopUserId, //顶级创客
  2550. SeoTitle = machineUser.RealName,
  2551. }).Entity;
  2552. db.OpenReward.Add(new OpenReward()
  2553. {
  2554. CreateDate = DateTime.Now,
  2555. UpdateDate = DateTime.Now,
  2556. TradeMonth = DateTime.Now.ToString("yyyyMM"), //交易月
  2557. TradeDate = DateTime.Now, //达标日期
  2558. UserId = puser.Id, //创客
  2559. BrandId = pos.BrandId, //品牌
  2560. RewardType = 2, //奖励类型
  2561. CreditTradeAmt = pos.CreditTrade, //贷记卡交易总金额
  2562. DebitTradeAmt = pos.DebitCardTrade, //借记卡交易总金额
  2563. CreditRewardAmount = Prize, //贷记卡交易奖励金额
  2564. RewardDesc = "开机奖励", //奖励描述
  2565. TopUserId = pTopUserId, //顶级创客
  2566. });
  2567. string IdBrand = puser.Id + "_" + pos.BrandId;
  2568. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  2569. if (userData == null)
  2570. {
  2571. userData = db.UserMachineData.Add(new UserMachineData()
  2572. {
  2573. IdBrand = IdBrand,
  2574. }).Entity;
  2575. db.SaveChanges();
  2576. }
  2577. userData.OpenProfit += Prize;
  2578. db.SaveChanges();
  2579. //账户入库
  2580. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == puser.Id);
  2581. if (account == null)
  2582. {
  2583. account = db.UserAccount.Add(new UserAccount()
  2584. {
  2585. Id = puser.Id,
  2586. UserId = puser.Id,
  2587. }).Entity;
  2588. db.SaveChanges();
  2589. }
  2590. //收支明细入库
  2591. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  2592. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  2593. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  2594. account.BalanceAmount += Prize;
  2595. account.TotalAmount += Prize;
  2596. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  2597. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  2598. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  2599. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  2600. {
  2601. CreateDate = DateTime.Now,
  2602. UpdateDate = DateTime.Now,
  2603. UserId = puser.Id, //创客
  2604. ChangeType = 50, //变动类型
  2605. ProductType = pos.BrandId, //产品类型
  2606. ChangeAmount = Prize, //变更金额
  2607. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  2608. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  2609. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  2610. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  2611. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  2612. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  2613. }).Entity;
  2614. db.SaveChanges();
  2615. RedisDbconn.Instance.Set("UserAccount:" + puser.Id, account);
  2616. string dateString = DateTime.Now.ToString("yyyyMMdd");
  2617. string monthString = DateTime.Now.ToString("yyyyMM");
  2618. // 开机奖励列表
  2619. List<string> dates = RedisDbconn.Instance.GetList<string>("OpenRewardDay:" + puser.Id + ":" + pos.BrandId);
  2620. if (!dates.Contains(dateString))
  2621. {
  2622. RedisDbconn.Instance.AddList("OpenRewardDay:" + puser.Id + ":" + pos.BrandId, dateString);
  2623. }
  2624. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + dateString, Prize);
  2625. List<string> months = RedisDbconn.Instance.GetList<string>("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId);
  2626. if (!months.Contains(monthString))
  2627. {
  2628. RedisDbconn.Instance.AddList("OpenRewardMonth:" + puser.Id + ":" + pos.BrandId, monthString);
  2629. }
  2630. RedisDbconn.Instance.AddNumber("OpenRewardAmt:" + puser.Id + ":" + pos.BrandId + ":" + monthString, Prize);
  2631. // 开机奖励详情
  2632. RedisDbconn.Instance.AddList("OpenRewardDetail:" + puser.Id + ":" + pos.BrandId + ":" + dateString, detail);
  2633. //收支明细
  2634. RedisDbconn.Instance.AddList("UserAccountRecord:" + puser.Id + ":1:" + monthString, userAccountRecord);
  2635. RedisDbconn.Instance.AddNumber("UserAccount:" + puser.Id + ":1:" + monthString, Prize);
  2636. Prize -= 10;
  2637. PrizeFlag.Add(Prize.ToString("f0"));
  2638. }
  2639. }
  2640. }
  2641. string result = "";
  2642. if (!PrizeFlag.Contains("20"))
  2643. {
  2644. result += "20元奖励未发,请检查上级是否满足条件\n";
  2645. }
  2646. if (!PrizeFlag.Contains("10"))
  2647. {
  2648. result += "10元奖励未发,请检查上级是否满足条件\n";
  2649. }
  2650. if (string.IsNullOrEmpty(result))
  2651. {
  2652. return "发放成功";
  2653. }
  2654. return result;
  2655. }
  2656. }
  2657. db.Dispose();
  2658. return "操作失败";
  2659. }
  2660. #endregion
  2661. #region 查询机具流量费分佣
  2662. public IActionResult FluxPrize(string right)
  2663. {
  2664. ViewBag.RightInfo = RightInfo;
  2665. ViewBag.right = right;
  2666. return View();
  2667. }
  2668. [HttpPost]
  2669. public Dictionary<string, string> FluxPrizeDo(string PosSn)
  2670. {
  2671. Dictionary<string, string> dic = new Dictionary<string, string>();
  2672. string result = "";
  2673. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn);
  2674. if (pos == null)
  2675. {
  2676. result += "机具SN号不存在\n";
  2677. dic.Add("code", "1");
  2678. dic.Add("result", result);
  2679. return dic;
  2680. }
  2681. if (pos.BindingState == 0)
  2682. {
  2683. result += "机具未绑定\n";
  2684. dic.Add("code", "1");
  2685. dic.Add("result", result);
  2686. return dic;
  2687. }
  2688. if (pos.ActivationState == 0)
  2689. {
  2690. result += "机具未激活\n";
  2691. dic.Add("code", "1");
  2692. dic.Add("result", result);
  2693. return dic;
  2694. }
  2695. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  2696. if (merchant == null)
  2697. {
  2698. result += "商户不存在\n";
  2699. dic.Add("code", "1");
  2700. dic.Add("result", result);
  2701. return dic;
  2702. }
  2703. Users buyuser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  2704. result += "机具号:" + PosSn + "\n";
  2705. result += "机具所属人:\n创客编号:" + buyuser.MakerCode + ",姓名:" + buyuser.RealName + ",手机号:" + buyuser.Mobile + "\n";
  2706. result += "商户信息\n商户编号:" + merchant.KqMerNo + ",姓名:" + merchant.MerchantName + ",手机号:" + merchant.MerchantMobile + "\n";
  2707. string FluxGet6 = "未领取";
  2708. string FluxUser6 = "";
  2709. FluxProfitDetail flux = db.FluxProfitDetail.FirstOrDefault(m => m.SnNo == pos.PosSn) ?? new FluxProfitDetail();
  2710. if (flux.Id > 0)
  2711. {
  2712. Users user = db.Users.FirstOrDefault(m => m.Id == flux.UserId) ?? new Users();
  2713. FluxGet6 = "已领取(领取时间:" + flux.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + ")";
  2714. FluxUser6 = "创客编号:" + user.MakerCode + ",姓名:" + user.RealName + ",手机号:" + user.Mobile;
  2715. }
  2716. result += "流量费:" + FluxGet6 + "\n";
  2717. result += "流量费发放对象:" + FluxUser6 + "\n";
  2718. dic.Add("id", pos.Id.ToString());
  2719. dic.Add("code", "0");
  2720. dic.Add("result", result);
  2721. return dic;
  2722. }
  2723. public string AddFluxPrize(int Id)
  2724. {
  2725. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == Id && m.ActivationState == 1);
  2726. if (pos == null)
  2727. {
  2728. return "机具未激活";
  2729. }
  2730. if (pos.BindingTime < DateTime.Now.AddDays(-30))
  2731. {
  2732. return "绑定已超过30天";
  2733. }
  2734. decimal ActPrize = decimal.Parse(function.CheckNum(pos.SeoKeyword));
  2735. if (ActPrize == 0)
  2736. {
  2737. return "机具无押金";
  2738. }
  2739. PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  2740. if (merchant == null)
  2741. {
  2742. return "商户不存在";
  2743. }
  2744. if (pos.BrandId == 6)
  2745. {
  2746. return "立刷不支持流量费分佣";
  2747. }
  2748. if (decimal.Parse(function.CheckNum(pos.SeoKeyword)) <= 0)
  2749. {
  2750. return "无押金机,不支持流量费分佣";
  2751. }
  2752. bool check = db.FluxProfitDetail.Any(m => m.MerNo == merchant.KqMerNo);
  2753. if (check)
  2754. {
  2755. return "开机奖励已发放,请勿重复操作";
  2756. }
  2757. SpModels.TradeFluxRecord trade = spdb.TradeFluxRecord.FirstOrDefault(m => m.TradeSnNo == pos.PosSn);
  2758. if (trade != null)
  2759. {
  2760. try
  2761. {
  2762. string OrderNo = trade.TradeSerialNo; //单号
  2763. DateTime TradeDate = trade.CreateDate.Value;
  2764. string TradeMonth = TradeDate.ToString("yyyyMM");
  2765. decimal FeeAmount = trade.FeeAmount; //流量费
  2766. if (trade.ProductType == "1" || trade.ProductType == "4" || trade.ProductType == "6" || trade.ProductType == "8" || trade.ProductType == "9")
  2767. {
  2768. FeeAmount = FeeAmount / 100;
  2769. }
  2770. string TradeSnNo = trade.TradeSnNo; //机具SN
  2771. decimal FluxProfit = 0;
  2772. if (trade.ProductType == "1" && FeeAmount == 60)
  2773. {
  2774. FluxProfit = 24;
  2775. }
  2776. else if (trade.ProductType == "7" && FeeAmount == 46)
  2777. {
  2778. FluxProfit = 10;
  2779. }
  2780. else if (trade.ProductType != "1" && FeeAmount == 48)
  2781. {
  2782. FluxProfit = 12;
  2783. }
  2784. if (FluxProfit > 0)
  2785. {
  2786. Users user = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new Users();
  2787. int GetUserId = user.Id;
  2788. int TopUserId = 0;
  2789. string ParentNav = user.ParentNav;
  2790. if (!string.IsNullOrEmpty(ParentNav))
  2791. {
  2792. string[] ParentNavList = ParentNav.Trim(',').Replace(",,", ",").Split(',');
  2793. if (ParentNavList.Length > 1)
  2794. {
  2795. TopUserId = int.Parse(ParentNavList[1]);
  2796. }
  2797. else if (ParentNavList.Length == 1)
  2798. {
  2799. TopUserId = int.Parse(ParentNavList[0]);
  2800. }
  2801. }
  2802. FluxProfitSummary fluxProfit = db.FluxProfitSummary.FirstOrDefault(m => m.UserId == GetUserId && m.BrandId == pos.BrandId && m.TradeMonth == TradeMonth);
  2803. if (fluxProfit == null)
  2804. {
  2805. fluxProfit = db.FluxProfitSummary.Add(new FluxProfitSummary()
  2806. {
  2807. CreateDate = DateTime.Now,
  2808. UpdateDate = DateTime.Now,
  2809. UserId = GetUserId, //创客
  2810. BrandId = pos.BrandId,
  2811. TopUserId = TopUserId, //顶级创客
  2812. TradeMonth = TradeMonth, //交易月
  2813. MerUserType = user.MerchantType, //商户创客类型
  2814. Remark = "流量卡分佣", //备注
  2815. }).Entity;
  2816. db.SaveChanges();
  2817. }
  2818. fluxProfit.FluxProfitAmt += FluxProfit; //流量分润总金额
  2819. db.FluxProfitDetail.Add(new FluxProfitDetail()
  2820. {
  2821. CreateDate = DateTime.Now,
  2822. UpdateDate = DateTime.Now,
  2823. RecordNo = OrderNo, //单号
  2824. TradeDate = TradeDate.ToString("yyyyMMdd"), //交易日期
  2825. TradeTime = TradeDate.ToString("HHmmss"), //交易时间
  2826. TradeMonth = TradeMonth, //交易月
  2827. UserId = GetUserId, //创客
  2828. MerchantId = pos.BindMerchantId, //商户
  2829. MerchantUserId = pos.UserId, //商户直属人
  2830. MerNo = merchant.KqMerNo, //渠道商户编号
  2831. SnNo = pos.PosSn, //渠道SN号
  2832. FluxOrderNo = OrderNo, //流量扣费单号
  2833. TradeOrderNo = OrderNo, //交易流水号
  2834. TradeAmt = trade.TradeAmount, //商户交易额
  2835. FluxFeeAmt = FeeAmount, //流量费
  2836. FluxProfitAmt = FluxProfit, //流量分润总金额
  2837. PosType = pos.PosSnType.ToString(), //POS类型
  2838. Remark = "流量卡分佣", //备注
  2839. BrandId = pos.BrandId, //品牌
  2840. TopUserId = TopUserId, //顶级创客
  2841. MerUserType = user.MerchantType, //商户创客类型
  2842. });
  2843. string IdBrand = user.Id + "_" + pos.BrandId;
  2844. UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  2845. if (userData == null)
  2846. {
  2847. userData = db.UserMachineData.Add(new UserMachineData()
  2848. {
  2849. IdBrand = IdBrand,
  2850. }).Entity;
  2851. db.SaveChanges();
  2852. }
  2853. userData.FluxProfit += FluxProfit;
  2854. db.SaveChanges();
  2855. UserAccount account = db.UserAccount.FirstOrDefault(m => m.Id == GetUserId);
  2856. if (account == null)
  2857. {
  2858. account = db.UserAccount.Add(new UserAccount()
  2859. {
  2860. Id = GetUserId,
  2861. UserId = GetUserId,
  2862. }).Entity;
  2863. db.SaveChanges();
  2864. }
  2865. decimal BeforeTotalAmount = account.TotalAmount; //变更前总金额
  2866. decimal BeforeFreezeAmount = account.FreezeAmount; //变更前冻结金额
  2867. decimal BeforeBalanceAmount = account.BalanceAmount; //变更前余额
  2868. account.BalanceAmount += FluxProfit;
  2869. account.TotalAmount += FluxProfit;
  2870. decimal AfterTotalAmount = account.TotalAmount; //变更后总金额
  2871. decimal AfterFreezeAmount = account.FreezeAmount; //变更后冻结金额
  2872. decimal AfterBalanceAmount = account.BalanceAmount; //变更后余额
  2873. UserAccountRecord userAccountRecord = db.UserAccountRecord.Add(new UserAccountRecord()
  2874. {
  2875. CreateDate = DateTime.Now,
  2876. UpdateDate = DateTime.Now,
  2877. UserId = GetUserId, //创客
  2878. ChangeType = 60, //变动类型
  2879. ProductType = pos.BrandId, //产品类型
  2880. ChangeAmount = FluxProfit, //变更金额
  2881. BeforeTotalAmount = BeforeTotalAmount, //变更前总金额
  2882. AfterTotalAmount = AfterTotalAmount, //变更后总金额
  2883. BeforeFreezeAmount = BeforeFreezeAmount, //变更前冻结金额
  2884. AfterFreezeAmount = AfterFreezeAmount, //变更后冻结金额
  2885. BeforeBalanceAmount = BeforeBalanceAmount, //变更前余额
  2886. AfterBalanceAmount = AfterBalanceAmount, //变更后余额
  2887. }).Entity;
  2888. db.SaveChanges();
  2889. spdb.Dispose();
  2890. db.Dispose();
  2891. return "发放成功";
  2892. }
  2893. }
  2894. catch (Exception ex)
  2895. {
  2896. function.WriteLog(DateTime.Now.ToString() + "\n$" + trade.Id + "$\n" + ex.ToString(), "流量卡分佣异常");
  2897. }
  2898. }
  2899. spdb.Dispose();
  2900. db.Dispose();
  2901. return "操作失败";
  2902. }
  2903. #endregion
  2904. #region 设置立刷费率
  2905. public IActionResult LisFee(string right)
  2906. {
  2907. ViewBag.RightInfo = RightInfo;
  2908. ViewBag.right = right;
  2909. return View();
  2910. }
  2911. [HttpPost]
  2912. public string LisFeeDo(string PosSns, decimal Fee, string Kind, string Fix)
  2913. {
  2914. if (string.IsNullOrEmpty(PosSns))
  2915. {
  2916. return "请输入机具号";
  2917. }
  2918. string result = SysUserName + "设置\n";
  2919. string[] PosSnList = PosSns.Split('\n');
  2920. int total = PosSnList.Length;
  2921. int index = 0;
  2922. foreach (string SubPosSn in PosSnList)
  2923. {
  2924. index += 1;
  2925. string PosSn = SubPosSn.Replace("\r", "");
  2926. result += PosSn + ":" + Fee + "\n";
  2927. if (Kind == "M5")
  2928. {
  2929. result += SetLiSFee(PosSn, Fee) + "\n";
  2930. }
  2931. else if (Kind == "N1")
  2932. {
  2933. result += SetLiSDeposit(PosSn, (int)Fee);
  2934. }
  2935. RedisDbconn.Instance.Set("LisFeeProcess:" + SysId, index + " / " + total);
  2936. }
  2937. result += "\n\n";
  2938. function.WriteLog(result, "设置立刷费率");
  2939. return result;
  2940. }
  2941. public string LisFeeProcess()
  2942. {
  2943. return RedisDbconn.Instance.Get<string>("LisFeeProcess:" + SysId);
  2944. }
  2945. #region 立刷
  2946. string LiSAgentId = "50814834";
  2947. 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=";
  2948. string LiSReqUrl = "https://openapi.jlpay.com/access/charge/"; //请求url
  2949. public string LiSSign(string data)
  2950. {
  2951. var rsa = RSA.Create();
  2952. var key = Convert.FromBase64String(LiSPrivateKey);
  2953. var content = Encoding.UTF8.GetBytes(data);
  2954. rsa.ImportPkcs8PrivateKey(key, out _);
  2955. var result = rsa.SignData(content, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  2956. return Convert.ToBase64String(result);
  2957. }
  2958. // 设置费率
  2959. public string SetLiSFee(string sn, decimal serviceFee)
  2960. {
  2961. Dictionary<string, object> dic = new Dictionary<string, object>();
  2962. dic.Add("agentId", LiSAgentId);
  2963. dic.Add("deviceSn", sn);
  2964. List<Dictionary<string, object>> feeList = new List<Dictionary<string, object>>();
  2965. Dictionary<string, object> item = new Dictionary<string, object>();
  2966. item.Add("feeCalcType", "M5");
  2967. item.Add("fixed", "0");
  2968. item.Add("rate", serviceFee);
  2969. feeList.Add(item);
  2970. dic.Add("feeList", feeList);
  2971. dic.Add("signMethod", "02");
  2972. string content = LiSAgentId + sn + "M50" + serviceFee + "02";
  2973. string signstr = LiSSign(content);
  2974. dic.Add("signData", signstr);
  2975. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  2976. string result = function.PostWebRequest(LiSReqUrl + "FEE003", req, new Dictionary<string, string>(), "application/json");
  2977. return result;
  2978. }
  2979. // 设置押金
  2980. public string SetLiSDeposit(string sn, int serviceFee)
  2981. {
  2982. serviceFee = serviceFee * 100;
  2983. Dictionary<string, object> dic = new Dictionary<string, object>();
  2984. dic.Add("agentId", LiSAgentId);
  2985. dic.Add("deviceSn", sn);
  2986. List<Dictionary<string, object>> feeList = new List<Dictionary<string, object>>();
  2987. Dictionary<string, object> item = new Dictionary<string, object>();
  2988. item.Add("feeCalcType", "N1");
  2989. item.Add("fixed", "1");
  2990. item.Add("rate", serviceFee);
  2991. feeList.Add(item);
  2992. dic.Add("feeList", feeList);
  2993. dic.Add("signMethod", "02");
  2994. string content = LiSAgentId + sn + "N11" + serviceFee + "02";
  2995. string signstr = LiSSign(content);
  2996. dic.Add("signData", signstr);
  2997. string req = Newtonsoft.Json.JsonConvert.SerializeObject(dic);
  2998. string result = function.PostWebRequest(LiSReqUrl + "FEE003", req, new Dictionary<string, string>(), "application/json");
  2999. return result;
  3000. }
  3001. #endregion
  3002. #endregion
  3003. #region 查询商户型创客设置信息
  3004. public IActionResult SeeMerchantType(string right)
  3005. {
  3006. ViewBag.RightInfo = RightInfo;
  3007. ViewBag.right = right;
  3008. return View();
  3009. }
  3010. [HttpPost]
  3011. public string SeeMerchantTypeDo(string PosSn)
  3012. {
  3013. if (string.IsNullOrEmpty(PosSn))
  3014. {
  3015. return "请输入机具SN或者创客编号";
  3016. }
  3017. SetMerchantTypeRecord pos = new SetMerchantTypeRecord();
  3018. if (PosSn.StartsWith("K"))
  3019. {
  3020. UserForMakerCode userfor = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == PosSn) ?? new UserForMakerCode();
  3021. bool check = db.SetMerchantTypeRecord.Any(m => m.ToUserId == userfor.UserId);
  3022. if (check)
  3023. {
  3024. pos = db.SetMerchantTypeRecord.Where(m => m.ToUserId == userfor.UserId).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SetMerchantTypeRecord();
  3025. }
  3026. }
  3027. else
  3028. {
  3029. bool check = db.SetMerchantTypeRecord.Any(m => m.PosSn == PosSn);
  3030. if (check)
  3031. {
  3032. pos = db.SetMerchantTypeRecord.Where(m => m.PosSn == PosSn).OrderByDescending(m => m.Id).FirstOrDefault() ?? new SetMerchantTypeRecord();
  3033. }
  3034. }
  3035. if (pos.Id == 0)
  3036. {
  3037. return "查无信息";
  3038. }
  3039. Users setUser = db.Users.FirstOrDefault(m => m.Id == pos.FromUserId) ?? new Users();
  3040. Users toUser = db.Users.FirstOrDefault(m => m.Id == pos.ToUserId) ?? new Users();
  3041. string CreateDate = pos.CreateDate == null ? "" : pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  3042. string bindTime = pos.BindDate == null ? "" : pos.BindDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  3043. string activeTime = pos.ActDate == null ? "" : pos.ActDate.Value.ToString("yyyy-MM-dd HH:mm:ss");
  3044. string bindResult = pos.BindStatus == 1 ? "已绑定" : "未绑定";
  3045. string activeResult = pos.ActStatus == 1 ? "已激活" : "未激活";
  3046. string RecycFlag = pos.IsRecyc == 1 ? "已循环" : "未循环";
  3047. string result = "";
  3048. result += "机具SN:" + pos.PosSn + "\n";
  3049. result += "操作人:创客编号:" + setUser.MakerCode + ",姓名:" + setUser.RealName + ",手机号:" + setUser.Mobile + "\n";
  3050. result += "操作对象:创客编号:" + toUser.MakerCode + ",姓名:" + toUser.RealName + ",手机号:" + toUser.Mobile + "\n";
  3051. result += "商户编号:" + pos.MerNo + "\n";
  3052. result += "操作时间:" + pos.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss") + "\n";
  3053. result += "绑定状态:" + bindResult + "(绑定时间:" + bindTime + ")\n";
  3054. result += "激活状态:" + activeResult + "(激活时间:" + activeTime + ")\n";
  3055. result += "贷记卡交易额:" + pos.CreditAmount + "\n";
  3056. result += "是否循环:" + RecycFlag + "\n";
  3057. return result;
  3058. }
  3059. #endregion
  3060. #region 三要素验证
  3061. public IActionResult CheckThreeElements(string right)
  3062. {
  3063. ViewBag.RightInfo = RightInfo;
  3064. ViewBag.right = right;
  3065. return View();
  3066. }
  3067. [HttpPost]
  3068. public string CheckThreeElementsDo(string bankcard, string idcard, string name)
  3069. {
  3070. var host = "https://bankcard3c.shumaidata.com";
  3071. var path = "/bankcard3c";
  3072. var method = "GET";
  3073. var appcode = "8e5704921ca3422f80f0deb935a7ddc6";
  3074. if (string.IsNullOrEmpty(bankcard))
  3075. {
  3076. return "结算银行卡号";
  3077. }
  3078. if (string.IsNullOrEmpty(idcard))
  3079. {
  3080. return "身份证号";
  3081. }
  3082. if (string.IsNullOrEmpty(name))
  3083. {
  3084. return "真实姓名";
  3085. }
  3086. String querys = "bankcard=" + bankcard + "&idcard=" + idcard + "&name=" + name;
  3087. function.WriteLog(DateTime.Now.ToString(), "验证三要素日志");
  3088. function.WriteLog(querys, "验证三要素日志");
  3089. String bodys = "";
  3090. String url = host + path;
  3091. HttpWebRequest httpRequest = null;
  3092. HttpWebResponse httpResponse = null;
  3093. if (0 < querys.Length)
  3094. {
  3095. url = url + "?" + querys;
  3096. }
  3097. if (host.Contains("https://"))
  3098. {
  3099. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  3100. httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
  3101. }
  3102. else
  3103. {
  3104. httpRequest = (HttpWebRequest)WebRequest.Create(url);
  3105. }
  3106. httpRequest.Method = method;
  3107. httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
  3108. if (0 < bodys.Length)
  3109. {
  3110. byte[] data = Encoding.UTF8.GetBytes(bodys);
  3111. using (Stream stream = httpRequest.GetRequestStream())
  3112. {
  3113. stream.Write(data, 0, data.Length);
  3114. }
  3115. }
  3116. try
  3117. {
  3118. httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  3119. }
  3120. catch (WebException ex)
  3121. {
  3122. httpResponse = (HttpWebResponse)ex.Response;
  3123. }
  3124. Stream st = httpResponse.GetResponseStream();
  3125. StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
  3126. string result = reader.ReadToEnd();
  3127. JsonData jsonObj = JsonMapper.ToObject(result);
  3128. result = jsonObj["data"]["desc"].ToString();
  3129. function.WriteLog(result, "验证三要素日志");
  3130. function.WriteLog("\n\n", "验证三要素日志");
  3131. return result;
  3132. }
  3133. #endregion
  3134. #region 四要素验证
  3135. public IActionResult CheckFourElements(string right)
  3136. {
  3137. ViewBag.RightInfo = RightInfo;
  3138. ViewBag.right = right;
  3139. return View();
  3140. }
  3141. [HttpPost]
  3142. public string CheckFourElementsDo(string bankcard, string idcard, string mobile, string name)
  3143. {
  3144. var host = "https://bankcard4c.shumaidata.com";
  3145. var path = "/bankcard4c";
  3146. var method = "GET";
  3147. var appcode = "8e5704921ca3422f80f0deb935a7ddc6";
  3148. if (string.IsNullOrEmpty(bankcard))
  3149. {
  3150. return "请输入结算银行卡号";
  3151. }
  3152. if (string.IsNullOrEmpty(idcard))
  3153. {
  3154. return "请输入身份证号";
  3155. }
  3156. if (string.IsNullOrEmpty(mobile))
  3157. {
  3158. return "请输入银行预留手机号";
  3159. }
  3160. if (string.IsNullOrEmpty(name))
  3161. {
  3162. return "请输入真实姓名";
  3163. }
  3164. String querys = "bankcard=" + bankcard + "&idcard=" + idcard + "&mobile=" + mobile + "&name=" + name;
  3165. function.WriteLog(DateTime.Now.ToString(), "验证四要素日志");
  3166. function.WriteLog(querys, "验证四要素日志");
  3167. String bodys = "";
  3168. String url = host + path;
  3169. HttpWebRequest httpRequest = null;
  3170. HttpWebResponse httpResponse = null;
  3171. if (0 < querys.Length)
  3172. {
  3173. url = url + "?" + querys;
  3174. }
  3175. if (host.Contains("https://"))
  3176. {
  3177. ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
  3178. httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
  3179. }
  3180. else
  3181. {
  3182. httpRequest = (HttpWebRequest)WebRequest.Create(url);
  3183. }
  3184. httpRequest.Method = method;
  3185. httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
  3186. if (0 < bodys.Length)
  3187. {
  3188. byte[] data = Encoding.UTF8.GetBytes(bodys);
  3189. using (Stream stream = httpRequest.GetRequestStream())
  3190. {
  3191. stream.Write(data, 0, data.Length);
  3192. }
  3193. }
  3194. try
  3195. {
  3196. httpResponse = (HttpWebResponse)httpRequest.GetResponse();
  3197. }
  3198. catch (WebException ex)
  3199. {
  3200. httpResponse = (HttpWebResponse)ex.Response;
  3201. }
  3202. Stream st = httpResponse.GetResponseStream();
  3203. StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
  3204. string result = reader.ReadToEnd();
  3205. JsonData jsonObj = JsonMapper.ToObject(result);
  3206. result = jsonObj["data"]["desc"].ToString();
  3207. function.WriteLog(result, "验证四要素日志");
  3208. function.WriteLog("\n\n", "验证四要素日志");
  3209. return result;
  3210. }
  3211. #endregion
  3212. public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  3213. {
  3214. return true;
  3215. }
  3216. #region 根据机具号同步商户激活状态
  3217. public IActionResult SyncMerchantStatus(string right)
  3218. {
  3219. ViewBag.RightInfo = RightInfo;
  3220. ViewBag.right = right;
  3221. return View();
  3222. }
  3223. [HttpPost]
  3224. public string SyncMerchantStatusDo(string PosSn)
  3225. {
  3226. if (string.IsNullOrEmpty(PosSn))
  3227. {
  3228. return "请输入机具号";
  3229. }
  3230. var pos = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn) ?? new MachineForSnNo();
  3231. var posInfo = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.SnId) ?? new PosMachinesTwo();
  3232. var posMerchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == posInfo.BindMerchantId) ?? new PosMerchantInfo();
  3233. if (posInfo.Id == 0)
  3234. {
  3235. return "机具" + PosSn + "不存在";
  3236. }
  3237. if (posMerchant.Id == 0)
  3238. {
  3239. return "机具绑定商户不存在";
  3240. }
  3241. else
  3242. {
  3243. posMerchant.ActiveStatus = 1;
  3244. posMerchant.UpdateDate = DateTime.Now;
  3245. posMerchant.MerStandardDate = posInfo.ActivationTime;
  3246. function.WriteLog(DateTime.Now.ToString() + ":请求参数," + PosSn + ":" + SysUserName + "-" + SysRealName, "根据机具号同步商户激活状态");
  3247. db.SaveChanges();
  3248. }
  3249. return "success";
  3250. }
  3251. #endregion
  3252. #region 根据机具号补发开机奖励
  3253. public IActionResult PosRewardReissued(string right)
  3254. {
  3255. ViewBag.RightInfo = RightInfo;
  3256. ViewBag.right = right;
  3257. return View();
  3258. }
  3259. [HttpPost]
  3260. public string PosRewardReissuedDo(string PosSn)
  3261. {
  3262. if (string.IsNullOrEmpty(PosSn))
  3263. {
  3264. return "请输入机具SN";
  3265. }
  3266. string[] PosSnList = PosSn.Split('\n');
  3267. var PosId = "";
  3268. var error = "";
  3269. foreach (var item in PosSnList)
  3270. {
  3271. var pos = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == item) ?? new MachineForSnNo();
  3272. var posInfo = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.SnId) ?? new PosMachinesTwo();
  3273. bool checkExist = db.OpenRewardDetail.Any(m => m.SnNo == item);
  3274. if (checkExist)
  3275. {
  3276. error += "以下操作失败" + item + ',' + "该机具开机奖励已发,请勿再次操作" + '\n';
  3277. }
  3278. else
  3279. {
  3280. if (posInfo.Id > 0)
  3281. {
  3282. PosId += posInfo.Id + ",";
  3283. }
  3284. }
  3285. }
  3286. if (!string.IsNullOrEmpty(error))
  3287. {
  3288. return "Warning|" + error;
  3289. }
  3290. PosId = PosId.TrimEnd(',');
  3291. function.WriteLog(DateTime.Now.ToString() + ":请求参数," + PosId + ":" + SysUserName + "-" + SysRealName, "根据机具号补发开机奖励");
  3292. RedisDbconn.Instance.AddList("OpenRewardQueue", PosId);
  3293. return "success";
  3294. }
  3295. #endregion
  3296. #region 根据机具号设置费率
  3297. public IActionResult SetPosFee(string right)
  3298. {
  3299. ViewBag.RightInfo = RightInfo;
  3300. ViewBag.right = right;
  3301. return View();
  3302. }
  3303. [HttpPost]
  3304. public string SetPosFeeDo(string PosSn, string FeeType)
  3305. {
  3306. if (string.IsNullOrEmpty(PosSn))
  3307. {
  3308. return "请输入机具SN";
  3309. }
  3310. if (string.IsNullOrEmpty(FeeType))
  3311. {
  3312. return "请输入选择费率";
  3313. }
  3314. if (FeeType == "0")
  3315. {
  3316. FeeType = "0.6";
  3317. }
  3318. if (FeeType == "1")
  3319. {
  3320. FeeType = "0.63";
  3321. }
  3322. string[] PosSnList = PosSn.Split('\n');
  3323. var PosId = "";
  3324. var RecordId = "";
  3325. var error = "";
  3326. foreach (var item in PosSnList)
  3327. {
  3328. var pos = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == item) ?? new MachineForSnNo();
  3329. var posInfo = db.PosMachinesTwo.FirstOrDefault(m => m.Id == pos.SnId) ?? new PosMachinesTwo();
  3330. if (posInfo.Id > 0)
  3331. {
  3332. var query = db.PosMachinesFeeChangeRecord.Add(new PosMachinesFeeChangeRecord()
  3333. {
  3334. Sort = posInfo.BrandId,
  3335. CreateDate = DateTime.Now,
  3336. PosId = posInfo.Id,
  3337. PosSn = posInfo.PosSn,
  3338. PosUserId = posInfo.UserId,
  3339. MerchantId = posInfo.BindMerchantId,
  3340. UserId = posInfo.UserId,
  3341. ChangeFee = decimal.Parse(FeeType),
  3342. }).Entity;
  3343. db.SaveChanges();
  3344. PosId += posInfo.Id + ",";
  3345. RecordId += query.Id + ",";
  3346. }
  3347. else
  3348. {
  3349. error += "机具" + PosSn + "不存在";
  3350. }
  3351. }
  3352. if (!string.IsNullOrEmpty(error))
  3353. {
  3354. return "Warning|" + error;
  3355. }
  3356. PosId = PosId.TrimEnd(',');
  3357. function.WriteLog(DateTime.Now.ToString() + ":请求参数," + PosId + ":" + SysUserName + "-" + SysRealName, "根据机具号设置费率");
  3358. string data = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + PosId + "\",\"Fee\": \"" + FeeType + "\",\"Kind\": \"2\",\"OpMan\": \"系统\"}";
  3359. RedisDbconn.Instance.AddList("SetDepositPostQueue", data);
  3360. return "success";
  3361. }
  3362. #endregion
  3363. #region 根据机具号、商户号、商户姓名关联商户和机具信息
  3364. public IActionResult RelationPosAndMerchant(string right)
  3365. {
  3366. ViewBag.RightInfo = RightInfo;
  3367. ViewBag.right = right;
  3368. return View();
  3369. }
  3370. [HttpPost]
  3371. public string RelationPosAndMerchantDo(string PosSn, string MerchantNo, string MerchantName)
  3372. {
  3373. if (string.IsNullOrEmpty(PosSn))
  3374. {
  3375. return "请输入机具SN";
  3376. }
  3377. if (string.IsNullOrEmpty(MerchantNo))
  3378. {
  3379. return "请输入商户编号";
  3380. }
  3381. if (string.IsNullOrEmpty(MerchantName))
  3382. {
  3383. return "请输入商户姓名";
  3384. }
  3385. var query = PosSn + "," + MerchantNo + "," + MerchantName;
  3386. var posMerchant = db.PosMerchantInfo.FirstOrDefault(m => m.MerchantNo == MerchantNo && m.MerchantName == MerchantName) ?? new PosMerchantInfo();
  3387. var machineForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == posMerchant.KqSnNo) ?? new MachineForSnNo();
  3388. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForSnNo.SnId) ?? new PosMachinesTwo();
  3389. if (posMerchant.Id == 0)
  3390. {
  3391. return "商户" + MerchantNo + "不存在";
  3392. }
  3393. if (pos.Id == 0)
  3394. {
  3395. return "机具" + PosSn + "不存在";
  3396. }
  3397. else
  3398. {
  3399. pos.BindMerchantId = posMerchant.Id;
  3400. db.SaveChanges();
  3401. function.WriteLog(DateTime.Now.ToString() + ":请求参数," + query + ":" + SysUserName + "-" + SysRealName, "根据机具号、商户号、商户姓名关联");
  3402. }
  3403. return "success";
  3404. }
  3405. #endregion
  3406. #region 认证创客
  3407. public IActionResult AuthMaker(string right)
  3408. {
  3409. ViewBag.RightInfo = RightInfo;
  3410. ViewBag.right = right;
  3411. return View();
  3412. }
  3413. [HttpPost]
  3414. public string AuthMakerDo(string MakerCode, string RealName, string CertId, string BackCard, string BackName)
  3415. {
  3416. if (string.IsNullOrEmpty(MakerCode))
  3417. {
  3418. return "请输入创客编号";
  3419. }
  3420. if (string.IsNullOrEmpty(RealName))
  3421. {
  3422. return "请输入真实姓名";
  3423. }
  3424. if (string.IsNullOrEmpty(CertId))
  3425. {
  3426. return "请输入身份证号";
  3427. }
  3428. if (string.IsNullOrEmpty(BackCard))
  3429. {
  3430. return "请输入银行卡号";
  3431. }
  3432. if (string.IsNullOrEmpty(BackName))
  3433. {
  3434. return "请输入银行名称";
  3435. }
  3436. var query = MakerCode + "," + RealName + "," + CertId + "," + BackCard + "," + BackName;
  3437. var userForMakerCode = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == MakerCode) ?? new UserForMakerCode();
  3438. var userInfo = db.Users.FirstOrDefault(m => m.Id == userForMakerCode.UserId) ?? new Users();
  3439. if (userInfo.AuthFlag == 1)
  3440. {
  3441. return "已认证";
  3442. }
  3443. else
  3444. {
  3445. userInfo.UpdateDate = DateTime.Now;
  3446. userInfo.RealName = RealName;
  3447. userInfo.CertId = CertId;
  3448. userInfo.SettleBankCardNo = BackCard;
  3449. userInfo.SettleBankCardName = BackName;
  3450. db.SaveChanges();
  3451. function.WriteLog(DateTime.Now.ToString() + ":请求参数," + query + ":" + SysUserName + "-" + SysRealName, "认证创客");
  3452. }
  3453. return "success";
  3454. }
  3455. #endregion
  3456. #region 划拨创客机具
  3457. public IActionResult TransferPos(string right)
  3458. {
  3459. ViewBag.RightInfo = RightInfo;
  3460. ViewBag.right = right;
  3461. return View();
  3462. }
  3463. [HttpPost]
  3464. public string TransferPosDo(string PosSn, string FromMakerCode, string ToMakerCode)
  3465. {
  3466. if (string.IsNullOrEmpty(PosSn))
  3467. {
  3468. return "请输入机具SN";
  3469. }
  3470. if (string.IsNullOrEmpty(FromMakerCode))
  3471. {
  3472. return "请输入机具来源创客编号";
  3473. }
  3474. if (string.IsNullOrEmpty(ToMakerCode))
  3475. {
  3476. return "请输入划拨对象创客编号";
  3477. }
  3478. var query = PosSn + "," + FromMakerCode + "," + ToMakerCode;
  3479. var machineForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == PosSn) ?? new MachineForSnNo();
  3480. var posInfo = db.PosMachinesTwo.FirstOrDefault(m => m.Id == machineForSnNo.SnId) ?? new PosMachinesTwo();
  3481. if (posInfo.Id == 0)
  3482. {
  3483. return "机具" + PosSn + "不存在";
  3484. }
  3485. var user = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == FromMakerCode) ?? new UserForMakerCode();
  3486. var tuser = db.UserForMakerCode.FirstOrDefault(m => m.MakerCode == ToMakerCode) ?? new UserForMakerCode();
  3487. var fUserId = db.Users.FirstOrDefault(m => m.Id == user.UserId) ?? new Users();
  3488. if (fUserId.Id == 0)
  3489. {
  3490. return "来源创客" + FromMakerCode + "不存在";
  3491. }
  3492. var tUserId = db.Users.FirstOrDefault(m => m.Id == tuser.UserId) ?? new Users();
  3493. if (tUserId.Id == 0)
  3494. {
  3495. return "划拨创客" + ToMakerCode + "不存在";
  3496. }
  3497. if (posInfo.BindingState == 0)
  3498. {
  3499. posInfo.BuyUserId = tUserId.Id;
  3500. posInfo.UserId = tUserId.Id;
  3501. db.SaveChanges();
  3502. function.WriteLog(DateTime.Now.ToString() + ":请求参数," + query + ":" + SysUserName + "-" + SysRealName, "划拨创客机具");
  3503. }
  3504. else
  3505. {
  3506. return "机具" + PosSn + "已使用";
  3507. }
  3508. return "success";
  3509. }
  3510. #endregion
  3511. #region 还原提现订单状态
  3512. public IActionResult RestoreStatus(string right)
  3513. {
  3514. ViewBag.RightInfo = RightInfo;
  3515. ViewBag.right = right;
  3516. return View();
  3517. }
  3518. [HttpPost]
  3519. public string RestoreStatusDo(string OrderNo)
  3520. {
  3521. if (string.IsNullOrEmpty(OrderNo))
  3522. {
  3523. return "请输入提现订单编号";
  3524. }
  3525. string[] OrderNoList = OrderNo.Replace("\r","").Split('\n');
  3526. var error = "";
  3527. foreach (var item in OrderNoList)
  3528. {
  3529. var cash = db.UserCashRecord.FirstOrDefault(m => m.CashOrderNo == item) ?? new UserCashRecord();
  3530. if (cash.Id > 0)
  3531. {
  3532. cash.QueryCount = 0;
  3533. cash.Status = 0;
  3534. db.SaveChanges();
  3535. }
  3536. else
  3537. {
  3538. error += "订单" + item + "不存在" + '\n';
  3539. }
  3540. }
  3541. if (!string.IsNullOrEmpty(error))
  3542. {
  3543. return "Warning|" + error;
  3544. }
  3545. return "success";
  3546. }
  3547. #endregion
  3548. }
  3549. }