SysToolsController.cs 155 KB

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