SysToolsController.cs 155 KB

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