company_user.go 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "github.com/tealeg/xlsx"
  8. "hongze/hz_crm_api/models"
  9. "hongze/hz_crm_api/models/advisory"
  10. "hongze/hz_crm_api/models/company"
  11. "hongze/hz_crm_api/models/company_user"
  12. "hongze/hz_crm_api/models/company_user/request"
  13. "hongze/hz_crm_api/models/company_user/response"
  14. "hongze/hz_crm_api/models/cygx"
  15. "hongze/hz_crm_api/models/system"
  16. "hongze/hz_crm_api/models/yb"
  17. "hongze/hz_crm_api/services"
  18. "hongze/hz_crm_api/services/alarm_msg"
  19. cygxService "hongze/hz_crm_api/services/cygx"
  20. "hongze/hz_crm_api/services/elastic"
  21. "hongze/hz_crm_api/utils"
  22. "os"
  23. "path"
  24. "path/filepath"
  25. "strconv"
  26. "strings"
  27. "time"
  28. "unicode/utf8"
  29. )
  30. // 客户联系人管理
  31. type CompanyUserController struct {
  32. BaseAuthController
  33. }
  34. // @Title 新增客户联系人
  35. // @Description 新增客户联系人接口
  36. // @Param request body company.AddUserReq true "type json string"
  37. // @router /user/add [post]
  38. func (this *CompanyUserController) AddUser() {
  39. br := new(models.BaseResponse).Init()
  40. defer func() {
  41. this.Data["json"] = br
  42. this.ServeJSON()
  43. }()
  44. sysUser := this.SysUser
  45. if sysUser == nil {
  46. br.Msg = "请登录"
  47. br.ErrMsg = "请登录,SysUser Is Empty"
  48. br.Ret = 408
  49. return
  50. }
  51. var req company.AddUserReq
  52. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  53. if err != nil {
  54. br.Msg = "参数解析异常!"
  55. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  56. return
  57. }
  58. if req.RealName == "" {
  59. br.Msg = "请填写姓名"
  60. br.IsSendEmail = false
  61. return
  62. }
  63. //空格移除
  64. req.MobileOne = utils.TrimStr(req.MobileOne)
  65. req.MobileTwo = utils.TrimStr(req.MobileTwo)
  66. req.Email = utils.TrimStr(req.Email)
  67. req.RealName = utils.TrimStr(req.RealName)
  68. //判断该手机号、邮箱是否已经添加,如果已经添加,那么就不再添加
  69. var key string
  70. if req.MobileOne != "" {
  71. key = "wx_user:mobile:" + req.MobileOne
  72. } else {
  73. key = "wx_user:email:" + req.Email
  74. }
  75. isHas := utils.Rc.IsExist(key)
  76. if isHas == true {
  77. br.Msg = "重复添加"
  78. return
  79. } else {
  80. //设置3分钟缓存,不允许重复添加
  81. utils.Rc.SetNX(key, 1, time.Second*300)
  82. //添加完成删除对应的缓存
  83. defer utils.Rc.Delete(key)
  84. }
  85. source := req.Source
  86. if source == "" {
  87. br.Msg = "没有来源"
  88. return
  89. }
  90. regionType := req.RegionType //区域来源
  91. //操作员的产品权限
  92. companyProductList := make([]*company.CompanyProduct, 0)
  93. shareSellerId := 0
  94. companyInfo := new(company.Company)
  95. if req.CompanyId > 0 {
  96. item, e := company.GetCompanyById(req.CompanyId)
  97. if e != nil {
  98. br.Msg = "客户信息有误"
  99. br.ErrMsg = "获取客户信息失败, Err: " + e.Error()
  100. return
  101. }
  102. companyInfo = item
  103. shareSellerId = item.ShareSellerId
  104. }
  105. //权益类客户,邮箱必填(2021-03-22 13:32:59修改)
  106. productId := services.GetProductId(sysUser.RoleTypeCode)
  107. if productId == 1 || regionType == "国内" { //ficc类客户
  108. if req.MobileOne == "" && req.Email == "" {
  109. br.Msg = "请输入手机号或邮箱"
  110. br.IsSendEmail = false
  111. return
  112. }
  113. } else if productId == 2 { //权益类客户
  114. if req.MobileOne == "" {
  115. br.Msg = "请输入手机号"
  116. br.ErrMsg = "请输入手机号"
  117. br.IsSendEmail = false
  118. return
  119. }
  120. if req.Email == "" {
  121. br.Msg = "请输入邮箱"
  122. br.ErrMsg = "请输入邮箱"
  123. br.IsSendEmail = false
  124. return
  125. }
  126. } else {
  127. //如果是管理员账户,那么需要从企业信息里面去获取所属类型客户了
  128. //获取企业客户信息
  129. if req.Source == "add_custom" {
  130. //添加客户时新增联系人
  131. } else if req.Source == "pick_custom" {
  132. //领取客户时添加联系人
  133. br.ErrMsg = "管理员账户不允许领取客户"
  134. br.Msg = "来源异常"
  135. return
  136. } else if req.Source == "add_user" {
  137. //普通类型添加客户
  138. //companyInfo, err := company.GetCompanyById(req.CompanyId)
  139. //if err != nil {
  140. // br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  141. // br.Msg = "获取客户信息失败"
  142. // return
  143. //}
  144. //手机号、邮箱校验(必填一个)
  145. if req.MobileOne == "" && req.Email == "" {
  146. br.Msg = "请输入手机号或邮箱"
  147. br.IsSendEmail = false
  148. return
  149. }
  150. //shareSellerId = companyInfo.ShareSellerId
  151. //获取当前操作员产品权限
  152. tmpCompanyProductList, productErr := getSysCompanyProductList(productId, req.CompanyId)
  153. if productErr != nil {
  154. br.Msg = "新增失败"
  155. br.ErrMsg = "新增失败,获取当前操作员产品权限异常,Err:" + productErr.Error()
  156. return
  157. }
  158. if tmpCompanyProductList == nil {
  159. br.Msg = "新增失败"
  160. br.ErrMsg = "新增失败,获取当前操作员找不到产品权限"
  161. return
  162. }
  163. for _, v := range tmpCompanyProductList {
  164. //权益(海外)客户,需要有提示
  165. if v.ProductId == 2 && companyInfo.RegionType == "海外" {
  166. if req.MobileOne == "" {
  167. br.Msg = "请输入手机号"
  168. }
  169. if req.Email == "" {
  170. br.Msg = "请输入邮箱"
  171. br.ErrMsg = "请输入邮箱"
  172. return
  173. }
  174. }
  175. }
  176. companyProductList = tmpCompanyProductList
  177. } else {
  178. br.ErrMsg = "来源异常"
  179. br.Msg = "来源异常"
  180. return
  181. }
  182. }
  183. if productId != 0 {
  184. if req.Source == "add_custom" {
  185. //添加客户时新增联系人
  186. } else if req.Source == "pick_custom" {
  187. //领取客户时添加联系人
  188. tmpCompanyProduct := &company.CompanyProduct{
  189. CompanyId: req.CompanyId,
  190. ProductId: productId,
  191. ProductName: sysUser.RoleName,
  192. SellerId: sysUser.AdminId,
  193. SellerName: sysUser.RealName,
  194. GroupId: sysUser.GroupId,
  195. DepartmentId: sysUser.DepartmentId,
  196. }
  197. companyProductList = append(companyProductList, tmpCompanyProduct)
  198. } else if req.Source == "add_user" {
  199. //获取当前操作员产品权限
  200. tmpCompanyProductList, productErr := getSysCompanyProductList(productId, req.CompanyId)
  201. if productErr != nil {
  202. br.Msg = "新增失败"
  203. br.ErrMsg = "新增失败,获取当前操作员产品权限异常,Err:" + productErr.Error()
  204. return
  205. }
  206. if tmpCompanyProductList == nil {
  207. br.Msg = "新增失败"
  208. br.ErrMsg = "新增失败,获取当前操作员找不到产品权限"
  209. return
  210. }
  211. companyProductList = tmpCompanyProductList
  212. //操作权限校验
  213. for _, v := range tmpCompanyProductList {
  214. companyUserStatus := services.CheckCompanyUserButton(sysUser.RoleTypeCode, v.SellerId, sysUser.AdminId, v.ProductId, shareSellerId, v.Status)
  215. if companyUserStatus == false {
  216. br.Msg = "没有操作权限"
  217. br.ErrMsg = "没有操作权限"
  218. br.IsSendEmail = false
  219. return
  220. }
  221. }
  222. }
  223. } else {
  224. //超管
  225. if req.Source != "add_custom" {
  226. //不是添加客户的时候添加的联系人
  227. tmpCompanyProductList, productErr := getSysCompanyProductList(productId, req.CompanyId)
  228. if productErr != nil {
  229. br.Msg = "新增失败"
  230. br.ErrMsg = "新增失败,获取当前操作员产品权限异常,Err:" + productErr.Error()
  231. return
  232. }
  233. if tmpCompanyProductList == nil {
  234. br.Msg = "新增失败"
  235. br.ErrMsg = "新增失败,获取当前操作员找不到产品权限"
  236. return
  237. }
  238. companyProductList = tmpCompanyProductList
  239. }
  240. }
  241. userCount, err := company.GetCompanyUserCount(req.CompanyId)
  242. if err != nil {
  243. br.Msg = "新增失败"
  244. br.ErrMsg = "判断联系人数量失败,Err:" + err.Error()
  245. return
  246. }
  247. if userCount == 0 {
  248. if req.BusinessCardUrl == "" {
  249. br.Msg = "请上传名片"
  250. br.IsSendEmail = false
  251. return
  252. }
  253. }
  254. //systemUser models.Company.CompanyUser
  255. //var sysemUser company.CompanyUser
  256. //校验主手机号
  257. if req.MobileOne != "" {
  258. mobileOneItem, err := company.GetUserCountByMobile(req.MobileOne)
  259. if err != nil && err.Error() == utils.ErrNoRow() {
  260. mobileOneItem, err = company.CheckCompanyUserCountByMobileTwo(req.MobileOne)
  261. if err != nil && err.Error() != utils.ErrNoRow() {
  262. br.Msg = "新增失败"
  263. br.ErrMsg = "判断手机号1是否存在,Err:" + err.Error()
  264. return
  265. }
  266. }
  267. if mobileOneItem != nil {
  268. if mobileOneItem.UserId > 0 {
  269. //如果有传入客户编号,且客户编号与该查出来的用户编号一致的情况下
  270. if req.CompanyId > 0 {
  271. //获取联系人分组信息
  272. userSellerGroupInfo, err := models.GetUserGroupSellerByUserId(int(mobileOneItem.UserId))
  273. if err != nil && err.Error() != utils.ErrNoRow() {
  274. br.Msg = "查询信息异常"
  275. br.Success = true
  276. br.Data = mobileOneItem
  277. br.IsSendEmail = false
  278. br.Ret = 600
  279. return
  280. }
  281. if req.CompanyId == mobileOneItem.CompanyId {
  282. //如果是该客户下的用户,那么判断该用户与当前账号是否存在关系,如果没有关系,那么直接添加关系后直接返回
  283. if len(companyProductList) > 0 {
  284. isHas := checkUserSellerRelation(companyProductList, mobileOneItem, &req, br)
  285. if isHas == false {
  286. return
  287. }
  288. }
  289. //修改返回给前端用户的信息
  290. if userSellerGroupInfo != nil {
  291. mobileOneItem.SellerRealName = userSellerGroupInfo.SellerRealName
  292. mobileOneItem.Status = userSellerGroupInfo.Status
  293. }
  294. br.Msg = "手机号1已存在"
  295. br.Success = true
  296. br.Data = mobileOneItem
  297. br.IsSendEmail = false
  298. br.Ret = 600
  299. return
  300. } else {
  301. hasMove := true //是否可移动
  302. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  303. if mobileOneItem.CompanyId > 1 {
  304. //获取联系人分组信息
  305. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(mobileOneItem.UserId))
  306. //根据分组校验
  307. if err == nil && len(userSellerGroupInfoList) > 0 {
  308. sellerMap := make(map[int]string) //销售信息map
  309. sellerNameSlice := make([]string, 0) //销售姓名切片
  310. statusNameSlice := make([]string, 0) //客户状态姓名切片
  311. for _, v := range userSellerGroupInfoList {
  312. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  313. hasMove = false
  314. }
  315. statusNameSlice = append(statusNameSlice, v.Status)
  316. if _, ok := sellerMap[v.SellerId]; ok {
  317. continue
  318. }
  319. sellerMap[v.SellerId] = v.SellerRealName
  320. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  321. }
  322. mobileOneItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  323. mobileOneItem.Status = strings.Join(statusNameSlice, "/")
  324. }
  325. }
  326. //是否可移动
  327. mobileOneItem.HasMove = hasMove
  328. br.Msg = "手机号1已存在"
  329. br.Success = true
  330. br.Data = mobileOneItem
  331. br.IsSendEmail = false
  332. br.Ret = 600
  333. return
  334. }
  335. } else {
  336. if strings.Contains(mobileOneItem.Status, "/") {
  337. if strings.Contains(mobileOneItem.Status, "正式") || strings.Contains(mobileOneItem.Status, "试用") {
  338. mobileOneItem.HasMove = false
  339. } else {
  340. mobileOneItem.HasMove = true
  341. }
  342. } else {
  343. if mobileOneItem.Status == utils.COMPANY_STATUS_FREEZE || //冻结
  344. mobileOneItem.Status == utils.COMPANY_STATUS_LOSE || //流失
  345. mobileOneItem.Status == utils.COMPANY_STATUS_POTENTIAL || //潜在
  346. mobileOneItem.Status == utils.COMPANY_STATUS_FOREVER { //永续
  347. mobileOneItem.HasMove = true
  348. }
  349. }
  350. br.Msg = "手机号1已存在"
  351. br.Success = true
  352. br.Data = mobileOneItem
  353. br.IsSendEmail = false
  354. br.Ret = 600
  355. return
  356. }
  357. }
  358. }
  359. }
  360. if req.Email != "" {
  361. emailItem, err := company.CheckCompanyUserCountByEmail(req.Email)
  362. if err != nil && err.Error() != utils.ErrNoRow() {
  363. br.Msg = "新增失败"
  364. br.ErrMsg = "新增失败,Err:" + err.Error()
  365. return
  366. }
  367. if emailItem != nil {
  368. if emailItem.UserId > 0 {
  369. if req.CompanyId > 0 {
  370. //获取联系人分组信息
  371. userSellerGroupInfo, err := models.GetUserGroupSellerByUserId(int(emailItem.UserId))
  372. if err != nil && err.Error() != utils.ErrNoRow() {
  373. br.Msg = "查询信息异常"
  374. br.Success = true
  375. br.Data = emailItem
  376. br.IsSendEmail = false
  377. br.Ret = 600
  378. return
  379. }
  380. if req.CompanyId == emailItem.CompanyId {
  381. //如果是该客户下的用户,那么判断该用户与当前账号是否存在关系,如果没有关系,那么直接添加关系后直接返回
  382. if len(companyProductList) > 0 {
  383. isHas := checkUserSellerRelation(companyProductList, emailItem, &req, br)
  384. if isHas == false {
  385. return
  386. }
  387. }
  388. //修改返回给前端用户的信息
  389. if userSellerGroupInfo != nil {
  390. emailItem.SellerRealName = userSellerGroupInfo.SellerRealName
  391. emailItem.Status = userSellerGroupInfo.Status
  392. }
  393. br.Msg = "邮箱已存在"
  394. br.Success = true
  395. br.Data = emailItem
  396. br.IsSendEmail = false
  397. br.Ret = 600
  398. return
  399. } else {
  400. hasMove := true //是否可移动
  401. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  402. if emailItem.CompanyId > 1 {
  403. //获取联系人分组信息
  404. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(emailItem.UserId))
  405. //根据分组校验
  406. if err == nil && len(userSellerGroupInfoList) > 0 {
  407. sellerMap := make(map[int]string) //销售信息map
  408. sellerNameSlice := make([]string, 0) //销售姓名切片
  409. statusNameSlice := make([]string, 0) //客户状态姓名切片
  410. for _, v := range userSellerGroupInfoList {
  411. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  412. hasMove = false
  413. }
  414. statusNameSlice = append(statusNameSlice, v.Status)
  415. if _, ok := sellerMap[v.SellerId]; ok {
  416. continue
  417. }
  418. sellerMap[v.SellerId] = v.SellerRealName
  419. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  420. }
  421. emailItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  422. emailItem.Status = strings.Join(statusNameSlice, "/")
  423. }
  424. }
  425. //是否可移动
  426. emailItem.HasMove = hasMove
  427. br.Msg = "邮箱已存在"
  428. br.Success = true
  429. br.Data = emailItem
  430. br.IsSendEmail = false
  431. br.Ret = 600
  432. return
  433. }
  434. } else {
  435. if strings.Contains(emailItem.Status, "/") {
  436. if strings.Contains(emailItem.Status, "正式") || strings.Contains(emailItem.Status, "试用") {
  437. emailItem.HasMove = false
  438. } else {
  439. emailItem.HasMove = true
  440. }
  441. } else {
  442. if emailItem.Status == utils.COMPANY_STATUS_FREEZE || //冻结
  443. emailItem.Status == utils.COMPANY_STATUS_LOSE || //流失
  444. emailItem.Status == utils.COMPANY_STATUS_POTENTIAL || //潜在
  445. emailItem.Status == utils.COMPANY_STATUS_FOREVER { //永续
  446. emailItem.HasMove = true
  447. }
  448. }
  449. br.Msg = "邮箱已存在"
  450. br.Success = true
  451. br.Data = emailItem
  452. br.IsSendEmail = false
  453. br.Ret = 600
  454. return
  455. }
  456. }
  457. }
  458. }
  459. //if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  460. if req.BusinessCardUrl != "" {
  461. // 获取图片识别客户名称的配置
  462. crmConfig, err := company.GetConfigDetailByCode("card_recognition_company_name")
  463. if err != nil {
  464. br.Msg = "获取配置失败"
  465. br.ErrMsg = "获取配置失败"
  466. br.IsSendEmail = false
  467. return
  468. }
  469. // 获取图片识别手机号的配置
  470. crmConfig2, err := company.GetConfigDetailByCode("card_recognition_tel")
  471. if err != nil {
  472. br.Msg = "获取配置失败"
  473. br.ErrMsg = "获取配置失败"
  474. br.IsSendEmail = false
  475. return
  476. }
  477. if crmConfig.ConfigValue == "true" || crmConfig2.ConfigValue == "true" {
  478. companyName := req.CompanyName
  479. if companyName == "" {
  480. companyInfo, err := company.GetCompanyById(req.CompanyId)
  481. if err != nil {
  482. br.Msg = "查找客户失败"
  483. br.ErrMsg = "查找客户失败,err:" + err.Error()
  484. return
  485. }
  486. companyName = companyInfo.CompanyName
  487. }
  488. companyName = utils.TrimStr(companyName)
  489. tmpCompanyNameList, err := elastic.Analyze(companyName)
  490. if err != nil {
  491. br.Msg = "添加失败"
  492. br.ErrMsg = "客户名称分词失败,Err:" + err.Error()
  493. return
  494. }
  495. //公司名称切片
  496. analyzeNameList := []string{companyName}
  497. for _, v := range tmpCompanyNameList {
  498. if v == "" {
  499. continue
  500. }
  501. ignoreStrs := []string{"", "有限", "投资", "资本", "管理", "贸易", "资产", "公司", "股份", "集团", "有限公司"}
  502. if !strings.Contains(strings.Join(ignoreStrs, ","), v) && utf8.RuneCount([]byte(v)) > 1 {
  503. //不在上面的过滤字里面,且切出来的字符大于1位
  504. analyzeNameList = append(analyzeNameList, v)
  505. }
  506. }
  507. //名片识别出来的客户名称切片
  508. companyNameSlice := make([]string, 0)
  509. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  510. if err != nil {
  511. br.Msg = "名片识别失败"
  512. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  513. return
  514. }
  515. //if card.Errno != 0 {
  516. // br.Msg = "您上传了非名片图片,请重新上传"
  517. // br.ErrMsg = "名片识别失败,ErrMsg:" + card.ErrorMsg + ";errCode:" + strconv.Itoa(card.Errno)
  518. // return
  519. //}
  520. //var mobileStr string
  521. //for _, v := range card.WordsResult {
  522. // mobileStr += v.Words + ","
  523. //}
  524. isFlag := false
  525. if len(card.WordsResult.MOBILE) > 0 {
  526. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  527. //if req.MobileOne != "" {
  528. // if strings.Contains(mobileStr, req.MobileOne) || mobileStr == "" {
  529. // isFlag = true
  530. // } else {
  531. // isFlag = false
  532. // }
  533. //}
  534. //
  535. //if isFlag == false && req.MobileTwo != "" {
  536. // if strings.Contains(mobileStr, req.MobileTwo) {
  537. // isFlag = true
  538. // } else {
  539. // isFlag = false
  540. // }
  541. //}
  542. if req.MobileOne != "" && strings.Contains(mobileStr, req.MobileOne) {
  543. isFlag = true
  544. }
  545. if req.MobileTwo != "" && strings.Contains(mobileStr, req.MobileTwo) {
  546. isFlag = true
  547. }
  548. //如果百度识别出来了客户名称,那么将识别出来的客户名称添加到待校验客户名称切片中
  549. if len(card.WordsResult.COMPANY) > 0 && card.WordsResult.COMPANY[0] != "" {
  550. companyNameSlice = card.WordsResult.COMPANY
  551. }
  552. }
  553. //如果百度未识别出来手机号或者公司名称,那么进行阿里识别
  554. if !isFlag || (len(companyNameSlice) == 0 && crmConfig.ConfigValue == "true") {
  555. //阿里云识别
  556. aliyunResult, errMsg, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
  557. if err != nil {
  558. br.Msg = "识别失败"
  559. br.ErrMsg = "识别失败,Err:" + err.Error()
  560. return
  561. }
  562. if !aliyunResult.Success {
  563. br.Msg = "识别失败"
  564. br.ErrMsg = "识别失败"
  565. return
  566. }
  567. if errMsg != "" {
  568. br.Msg = errMsg
  569. return
  570. }
  571. //如果百度没有识别出来手机号,那么还需要阿里识别出来的手机号校验下
  572. if !isFlag {
  573. if len(aliyunResult.TelCell) > 0 {
  574. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  575. if req.MobileOne != "" {
  576. if strings.Contains(mobileStr, req.MobileOne) {
  577. isFlag = true
  578. } else {
  579. isFlag = false
  580. }
  581. }
  582. } else {
  583. isFlag = true
  584. }
  585. }
  586. //如果阿里云识别出来了客户名称,那么将识别出来的客户名称添加到待校验客户名称切片中
  587. if len(aliyunResult.Company) > 0 {
  588. companyNameSlice = append(companyNameSlice, aliyunResult.Company...)
  589. }
  590. }
  591. if !isFlag && crmConfig2.ConfigValue == "true" {
  592. br.Msg = "名片信息与联系人手机号不符"
  593. br.IsSendEmail = false
  594. return
  595. }
  596. //校验识别出来的名片名称与传入的客户名称是否一致
  597. if len(companyNameSlice) > 0 && crmConfig.ConfigValue == "true" {
  598. isCompanyNameFlag := false //是否成功匹配客户名称
  599. //遍历识别出来的客户名称,然后与系统的客户名称做匹配(识别出来的名称包含系统名称的话,那么标记成功匹配)
  600. for _, tmpCompanyName := range companyNameSlice {
  601. //空串不处理
  602. if companyName == "" {
  603. continue
  604. }
  605. for _, analyzeName := range analyzeNameList {
  606. //如果分词出来的公司名称正好在识别出来的公司名称里面,那么就认为校验通过了
  607. if strings.Contains(tmpCompanyName, analyzeName) {
  608. isCompanyNameFlag = true
  609. break
  610. }
  611. }
  612. if isCompanyNameFlag {
  613. break
  614. }
  615. }
  616. //如果没有匹配到客户名称,那么就返回报错
  617. if !isCompanyNameFlag {
  618. br.Msg = "名片信息与公司名称不符"
  619. br.IsSendEmail = false
  620. return
  621. }
  622. }
  623. }
  624. }
  625. user := new(models.WxUser)
  626. user.RealName = utils.TrimStr(req.RealName)
  627. user.Sex = req.Sex
  628. user.CountryCode = utils.TrimStr(req.CountryCode)
  629. user.Mobile = utils.TrimStr(req.MobileOne)
  630. user.OutboundCountryCode = utils.TrimStr(req.CountryCode)
  631. user.OutboundMobile = utils.TrimStr(req.MobileOne)
  632. user.MobileTwo = utils.TrimStr(req.MobileTwo)
  633. user.Email = utils.TrimStr(req.Email)
  634. user.Position = req.Position
  635. user.IsMaker = req.IsMaker
  636. user.BusinessCardUrl = req.BusinessCardUrl
  637. user.CompanyId = req.CompanyId
  638. if req.CompanyId == 0 {
  639. user.CompanyId = 1
  640. }
  641. user.DepartmentName = req.DepartmentName
  642. user.CreatedTime = time.Now()
  643. //user.RegisterTime = time.Now()
  644. user.RegisterPlatform = 3
  645. newId, err := models.AddWxUser(user)
  646. user.UserId = newId
  647. if err != nil {
  648. br.Msg = "新增失败"
  649. br.ErrMsg = "新增失败,Err:" + err.Error()
  650. return
  651. }
  652. //如果有传入企业编号,那么需要将该联系人与销售员做绑定
  653. if len(companyProductList) > 0 {
  654. for _, companyProduct := range companyProductList {
  655. models.AddUserSellerRelation(newId, companyProduct.CompanyId, companyProduct.SellerId, companyProduct.ProductId, companyProduct.SellerName, req.MobileOne, req.Email)
  656. }
  657. }
  658. //更新客户最近阅读次数以及最近阅读时间
  659. go services.ModifyCompanyProductLastViewData([]int{req.CompanyId})
  660. //联系人信息
  661. userInfo, _ := json.Marshal(user)
  662. go services.AddWxUserOpLog(company.WxUserOpLog{
  663. LogType: "add",
  664. UserId: int(user.UserId),
  665. CompanyId: user.CompanyId,
  666. Mobile: user.Mobile,
  667. Email: user.Email,
  668. OriginalUserInfo: "",
  669. UserInfo: string(userInfo),
  670. OriginalUserSellerInfo: "",
  671. UserSellerInfo: "",
  672. OpUserId: sysUser.AdminId,
  673. OpUserName: sysUser.RealName,
  674. CreateTime: time.Now(),
  675. })
  676. br.Ret = 200
  677. br.Success = true
  678. br.Msg = "新增成功"
  679. br.Data = newId
  680. br.IsAddLog = true
  681. }
  682. // 获取当前操作员产品权限(所有产品数据)
  683. func getSysCompanyProductList(productId, companyId int) (companyProduct []*company.CompanyProduct, err error) {
  684. if productId == 0 {
  685. tmpCompanyProduct, productErr := company.GetCompanyProductByCompanyIdAndProductId(companyId, 1)
  686. if productErr != nil && productErr.Error() != utils.ErrNoRow() {
  687. //sql异常
  688. err = productErr
  689. return
  690. }
  691. if tmpCompanyProduct != nil {
  692. companyProduct = append(companyProduct, tmpCompanyProduct)
  693. }
  694. tmpCompanyProduct, productErr = company.GetCompanyProductByCompanyIdAndProductId(companyId, 2)
  695. if productErr != nil && productErr.Error() != utils.ErrNoRow() {
  696. //sql异常
  697. err = productErr
  698. return
  699. }
  700. if tmpCompanyProduct != nil {
  701. companyProduct = append(companyProduct, tmpCompanyProduct)
  702. }
  703. } else {
  704. tmpCompanyProduct, productErr := company.GetCompanyProductByCompanyIdAndProductId(companyId, productId)
  705. if productErr != nil && productErr.Error() != utils.ErrNoRow() {
  706. //sql异常
  707. err = productErr
  708. return
  709. }
  710. if tmpCompanyProduct != nil {
  711. companyProduct = append(companyProduct, tmpCompanyProduct)
  712. }
  713. }
  714. return
  715. }
  716. // 校验用户与销售信息
  717. func checkUserSellerRelation(companyProductList []*company.CompanyProduct, companyUser *company.CompanyUser, req *company.AddUserReq, br *models.BaseResponse) (isHas bool) {
  718. for _, companyProduct := range companyProductList {
  719. _, relationErr := models.GetUserSellerRelation(companyUser.UserId, companyProduct.SellerId)
  720. //找到数据了
  721. if relationErr == nil {
  722. isHas = true
  723. continue
  724. }
  725. //没找到数据,不存在关系
  726. //如果不存在关系,那么直接添加关系就好了,其他操作不做
  727. if relationErr.Error() == utils.ErrNoRow() {
  728. //添加用户与联系人的关系
  729. models.AddUserSellerRelation(companyUser.UserId, companyProduct.CompanyId, companyProduct.SellerId, companyProduct.ProductId, companyProduct.SellerName, companyUser.Mobile, companyUser.Email)
  730. } else {
  731. //sql异常
  732. br.Msg = "新增失败"
  733. br.ErrMsg = "判断手机号或邮箱是否存在关联关系,Err:" + relationErr.Error()
  734. return
  735. }
  736. //对老数据做字段数据新增处理,如果该字段已有数据则不做处理start
  737. {
  738. mobileOne := companyUser.Mobile
  739. mobileTwo := companyUser.MobileTwo
  740. email := companyUser.Email
  741. businessCardUrl := companyUser.BusinessCardUrl
  742. if req.MobileOne != "" && companyUser.Mobile == "" {
  743. mobileOne = utils.TrimStr(req.MobileOne)
  744. }
  745. if req.MobileTwo != "" && companyUser.MobileTwo == "" {
  746. mobileTwo = utils.TrimStr(req.MobileTwo)
  747. }
  748. if req.Email != "" && companyUser.Email == "" {
  749. email = utils.TrimStr(req.Email)
  750. }
  751. if req.BusinessCardUrl != "" && companyUser.BusinessCardUrl == "" {
  752. businessCardUrl = strings.Replace(req.BusinessCardUrl, " ", "", -1)
  753. }
  754. _ = company.EditCompanyUserContact(companyUser.UserId, mobileOne, mobileTwo, email, businessCardUrl)
  755. }
  756. //对老数据做字段数据新增处理,如果该字段已有数据则不做处理start
  757. br.Ret = 200
  758. br.Success = true
  759. br.Msg = "新增成功"
  760. br.Data = companyUser.UserId
  761. return
  762. }
  763. return
  764. }
  765. // List
  766. // @Title 联系人列表
  767. // @Description 联系人列表接口
  768. // @Param PageSize query int true "每页数据条数"
  769. // @Param CurrentIndex query int true "当前页页码,从1开始"
  770. // @Param CompanyId query int true "公司id,必填"
  771. // @Param KeyWord query string true "搜索关键词"
  772. // @Param ProductType query int true "产品类型"
  773. // @Param ProductId query int true "产品id"
  774. // @Param IsSubscribe query int true "是否关注了公众号: -1-默认全部; 0-未关注; 1-已关注"
  775. // @Param IsSubscribeHzyj query int true "是否关注了弘则研究公众号: -1-默认全部; 0-未关注; 1-已关注"
  776. // @Param IsSubscribeCygx query int true "是否关注了查研观向小助手公众号: -1-默认全部; 0-未关注; 1-已关注"
  777. // @Success 200 {object} company.CompanyUserListResp
  778. // @router /user/list [get]
  779. func (this *CompanyUserController) List() {
  780. br := new(models.BaseResponse).Init()
  781. defer func() {
  782. this.Data["json"] = br
  783. this.ServeJSON()
  784. }()
  785. sysUser := this.SysUser
  786. if sysUser == nil {
  787. br.Msg = "请登录"
  788. br.ErrMsg = "请登录,SysUser Is Empty"
  789. br.Ret = 408
  790. return
  791. }
  792. pageSize, _ := this.GetInt("PageSize")
  793. currentIndex, _ := this.GetInt("CurrentIndex")
  794. companyId, _ := this.GetInt("CompanyId")
  795. keyWord := utils.TrimStr(this.GetString("KeyWord"))
  796. productType, _ := this.GetInt("ProductType")
  797. ybProductId, _ := this.GetInt("ProductId")
  798. isSubscribe, _ := this.GetInt("IsSubscribe", -1)
  799. isSubscribeHzyj, _ := this.GetInt("IsSubscribeHzyj", -1)
  800. isSubscribeCygx, _ := this.GetInt("IsSubscribeCygx", -1)
  801. // 默认是问答社区
  802. if productType <= 0 {
  803. productType = 3
  804. }
  805. if companyId <= 0 {
  806. br.Msg = "请选择客户"
  807. br.ErrMsg = "客户参数错误"
  808. return
  809. }
  810. var startSize int
  811. if pageSize <= 0 {
  812. pageSize = utils.PageSize20
  813. }
  814. if currentIndex <= 0 {
  815. currentIndex = 1
  816. }
  817. startSize = paging.StartIndex(currentIndex, pageSize)
  818. var condition string
  819. var pars []interface{}
  820. if keyWord != "" {
  821. condition += ` AND (a.real_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  822. }
  823. // 是否关注公众号
  824. if isSubscribe == 0 {
  825. condition += ` AND (c.subscribe = 0 OR ISNULL(c.subscribe)) `
  826. }
  827. if isSubscribe == 1 {
  828. condition += ` AND c.subscribe = 1 `
  829. }
  830. // 是否关注弘则研究公众号
  831. if isSubscribeHzyj == 0 {
  832. condition += ` AND (c.subscribe = 0 OR ISNULL(c.subscribe)) `
  833. }
  834. if isSubscribeHzyj == 1 {
  835. condition += ` AND c.subscribe = 1 `
  836. }
  837. // 是否关注弘则研究公众号
  838. if isSubscribeCygx == 0 {
  839. condition += ` AND (a.cygx_subscribe = 0 OR ISNULL(a.cygx_subscribe)) `
  840. }
  841. if isSubscribeCygx == 1 {
  842. condition += ` AND a.cygx_subscribe = 1 `
  843. }
  844. //非管理员,那么只能看到该产品下的联系人
  845. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  846. productId := services.GetProductId(sysUser.RoleTypeCode)
  847. condition += ` AND b.product_id = ? `
  848. pars = append(pars, productId)
  849. //流失客户(除非是超级管理员或者是部门管理员,要不然不允许看对应的联系人)
  850. var companyProduct *company.CompanyProduct
  851. //当前是ficc权限,且非ficc管理员
  852. //if productId == 1 && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FICC_ADMIN {
  853. // tmpCompanyProduct, _ := company.GetCompanyProductByCompanyIdAndProductId(companyId, 1)
  854. // companyProduct = tmpCompanyProduct
  855. //}
  856. if productId == 2 && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN {
  857. // 当前是权益权限,且非 权益管理员
  858. tmpCompanyProduct, _ := company.GetCompanyProductByCompanyIdAndProductId(companyId, 2)
  859. companyProduct = tmpCompanyProduct
  860. if companyProduct != nil && companyProduct.Status == utils.COMPANY_STATUS_LOSE {
  861. condition += ` AND seller_id=? `
  862. pars = append(pars, sysUser.AdminId)
  863. }
  864. }
  865. }
  866. total, err := company.GetCompanyUserListCountV2(condition, pars, companyId)
  867. if err != nil {
  868. br.Msg = "获取失败"
  869. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  870. return
  871. }
  872. list, err := company.GetCompanyUserListV2(condition, pars, companyId, startSize, pageSize)
  873. if err != nil {
  874. br.Msg = "获取失败"
  875. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  876. return
  877. }
  878. //获取企业客户信息
  879. companyInfo, err := company.GetCompanyById(companyId)
  880. if err != nil {
  881. br.Msg = "获取失败"
  882. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  883. return
  884. }
  885. lenList := len(list)
  886. if lenList > 0 {
  887. userIdSlice := make([]string, 0)
  888. mobilesSlice := make([]string, 0)
  889. emailsSlice := make([]string, 0)
  890. userIds := make([]int, 0)
  891. for _, v := range list {
  892. userIdSlice = append(userIdSlice, strconv.Itoa(int(v.UserId)))
  893. userIds = append(userIds, int(v.UserId))
  894. if v.Mobile != "" {
  895. mobilesSlice = append(mobilesSlice, v.Mobile)
  896. } else if v.Email != "" {
  897. emailsSlice = append(emailsSlice, v.Email)
  898. }
  899. }
  900. //用户浏览数据
  901. userIdStr := strings.Join(userIdSlice, ",")
  902. mobileStr := strings.Join(mobilesSlice, "','")
  903. mobileStr = "'" + mobileStr + "'"
  904. emailStr := strings.Join(emailsSlice, "','")
  905. emailStr = "'" + emailStr + "'"
  906. //userViewTotalList, _ := models.GetCountUserViewHistoryByUserIds(userIdStr)
  907. //userViewTotalMap := make(map[int]*models.UserViewTotalSlice)
  908. //for _, userView := range userViewTotalList {
  909. // userViewTotalMap[userView.UserId] = userView
  910. //}
  911. //用户日评浏览数据
  912. //userReportViewTotalList, _ := models.GetReportViewMaxTimeByUserIds(userIdStr)
  913. //userReportViewTotalMap := make(map[int]*models.ReportViewRecord)
  914. //for _, userReportView := range userReportViewTotalList {
  915. // userReportViewTotalMap[userReportView.UserId] = userReportView
  916. //}
  917. //报告统计删除晨报部分统计加入每日资讯 2021-4-9
  918. ////手机号
  919. //userViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  920. //userReportViewMobileTotalMap := make(map[string]*models.ReportViewMobileRecord)
  921. //userAdvisoryViewMobileTotalMap := make(map[string]*advisory.UserViewMobileTotalSlice)
  922. //userCygxArticleHistoryRecordViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  923. //if len(mobilesSlice) > 0 {
  924. // //用户浏览数据
  925. // userViewMobileTotalList, _ := models.GetCountUserViewHistoryByMobiles(mobileStr)
  926. // for _, userViewMobile := range userViewMobileTotalList {
  927. // userViewMobileTotalMap[userViewMobile.Mobile] = userViewMobile
  928. // }
  929. //
  930. // //每日点评手机号数据
  931. // userReportViewMobileTotalList, _ := models.GetReportViewMaxTimeByMobiles(mobileStr)
  932. // for _, userReportViewMobile := range userReportViewMobileTotalList {
  933. // userReportViewMobileTotalMap[userReportViewMobile.Mobile] = userReportViewMobile
  934. // }
  935. //
  936. // //每日资讯
  937. // userAdvisoryViewMobileTotalList, _ := advisory.GetCountUserViewHistoryByMobiles(mobileStr)
  938. // //fmt.Println(userAdvisoryViewMobileTotalList)
  939. // for _, userAdvisoryViewMobile := range userAdvisoryViewMobileTotalList {
  940. // userAdvisoryViewMobileTotalMap[userAdvisoryViewMobile.Mobile] = userAdvisoryViewMobile
  941. // }
  942. // //查研观向
  943. // userCygxArticleHistoryRecordViewMobileTotalList, _ := models.GetCountCygxArticleHistoryRecordByMobiles(mobileStr)
  944. // for _, userCygxArticleHistoryRecordViewMobile := range userCygxArticleHistoryRecordViewMobileTotalList {
  945. // userCygxArticleHistoryRecordViewMobileTotalMap[userCygxArticleHistoryRecordViewMobile.Mobile] = userCygxArticleHistoryRecordViewMobile
  946. // }
  947. //}
  948. ////邮箱
  949. //userViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  950. //userReportViewEmailTotalMap := make(map[string]*models.ReportViewEmailRecord)
  951. //userAdvisoryViewEmailTotalMap := make(map[string]*advisory.UserViewEmailTotalSlice)
  952. //userCygxViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  953. //if len(emailsSlice) > 0 {
  954. // //用户浏览数据
  955. // userViewEmailTotalList, _ := models.GetCountUserViewHistoryByEmails(emailStr)
  956. // for _, userViewEmail := range userViewEmailTotalList {
  957. // userViewEmailTotalMap[userViewEmail.Email] = userViewEmail
  958. // }
  959. // //每日点评手机号数据
  960. // userReportViewEmailTotalList, _ := models.GetReportViewMaxTimeByEmails(emailStr)
  961. // for _, userReportViewEmail := range userReportViewEmailTotalList {
  962. // userReportViewEmailTotalMap[userReportViewEmail.Email] = userReportViewEmail
  963. // }
  964. // //每日资讯
  965. // userAdvisoryViewEmailTotalList, _ := advisory.GetCountUserViewHistoryByEmails(emailStr)
  966. // for _, userAdvisoryViewEmail := range userAdvisoryViewEmailTotalList {
  967. // userAdvisoryViewEmailTotalMap[userAdvisoryViewEmail.Email] = userAdvisoryViewEmail
  968. // }
  969. // //查研观向
  970. // userCygxViewEmailTotalList, _ := models.GetCountCygxArticleHistoryRecordByEmails(emailStr)
  971. // for _, userCygxReportViewEmail := range userCygxViewEmailTotalList {
  972. // userCygxViewEmailTotalMap[userCygxReportViewEmail.Email] = userCygxReportViewEmail
  973. // }
  974. //}
  975. //获取联系人关联产品的条数
  976. userSellerRelationTotalSlice, err := models.GetUserSellerRelationCountByUserIds(userIdStr)
  977. if err != nil {
  978. br.Msg = "获取失败"
  979. br.ErrMsg = "获取联系人与销售员信息失败,Err:" + err.Error()
  980. return
  981. }
  982. userSellerRelationTotalMap := make(map[int]*models.UserSellerRelationSlice)
  983. for _, userSellerRelationTotal := range userSellerRelationTotalSlice {
  984. userSellerRelationTotalMap[userSellerRelationTotal.UserId] = userSellerRelationTotal
  985. }
  986. userRecordRegisterMap := make(map[int]time.Time)
  987. if len(userIdSlice) > 0 {
  988. //用户绑定注册数据
  989. userRecordRegisterList, _ := models.GetUserRecordRegisterByUserIds(userIdStr)
  990. for _, userRecordRegister := range userRecordRegisterList {
  991. userRecordRegisterMap[userRecordRegister.UserId] = userRecordRegister.CreateTime
  992. }
  993. }
  994. // 用户图表权限设置信息
  995. userChartPermissionMap := make(map[int]*company_user.ChartClassifyPermission)
  996. userChartPermissionList, err := company_user.GetChartClassifyPermissionListByUserIds(userIdStr)
  997. if err != nil {
  998. br.Msg = "获取失败"
  999. br.ErrMsg = "获取联系人图表权限信息失败, Err:" + err.Error()
  1000. return
  1001. }
  1002. for i := 0; i < len(userChartPermissionList); i++ {
  1003. userChartPermissionMap[userChartPermissionList[i].UserId] = userChartPermissionList[i]
  1004. }
  1005. // 获取admin列表-匹配联系人是否为admin用户
  1006. sysUserList, err := system.GetAdminList()
  1007. if err != nil {
  1008. br.Msg = "获取失败"
  1009. br.ErrMsg = "获取admin列表失败, Err:" + err.Error()
  1010. return
  1011. }
  1012. sysUserMobileMap := make(map[string]int, 0)
  1013. for i := 0; i < len(sysUserList); i++ {
  1014. sysUserMobile := sysUserList[i].Mobile
  1015. if sysUserMobile != "" {
  1016. sysUserMobileMap[sysUserList[i].Mobile] = 1
  1017. }
  1018. }
  1019. // 获取研报分产品统计
  1020. //ybViewsList, e := yb.GetCompanyUserYbProductVisitCount(userIds)
  1021. ybViewsList, e := yb.GetCompanyUserYbProductVisitCountByProductType(userIds, productType, ybProductId)
  1022. if e != nil {
  1023. br.Msg = "获取失败"
  1024. br.ErrMsg = "获取分产品阅读统计失败, Err:" + e.Error()
  1025. return
  1026. }
  1027. userYbViewsMap := make(map[int]int)
  1028. for _, v := range ybViewsList {
  1029. userYbViewsMap[v.UserId] = v.VisitCount
  1030. }
  1031. for i := 0; i < lenList; i++ {
  1032. item := list[i]
  1033. //企业名称
  1034. list[i].CompanyName = companyInfo.CompanyName
  1035. if item.OpenId != "" {
  1036. list[i].IsRegister = true
  1037. } else {
  1038. if item.RegisterPlatform == 2 {
  1039. if item.Mobile != "" || item.Email != "" {
  1040. list[i].IsRegister = true
  1041. }
  1042. } else if item.RegisterTime != "" {
  1043. list[i].IsRegister = true
  1044. } else {
  1045. list[i].IsRegister = false
  1046. }
  1047. }
  1048. ////手机号用户累计浏览次数
  1049. //if userView, ok := userViewMobileTotalMap[item.Mobile]; ok {
  1050. // list[i].ViewTotal += userView.Total
  1051. // if userView.CreatedTime.After(list[i].LastViewTime) {
  1052. // list[i].LastViewTime = userView.CreatedTime
  1053. // list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  1054. // }
  1055. //}
  1056. ////邮箱用户累计浏览次数
  1057. //if userView, ok := userViewEmailTotalMap[item.Email]; ok {
  1058. // //总共浏览的报表数据需要调整为(手机号用户浏览晨报数+邮箱号用户浏览晨报数)
  1059. // list[i].ViewTotal += userView.Total
  1060. // if userView.CreatedTime.After(list[i].LastViewTime) {
  1061. // list[i].LastViewTime = userView.CreatedTime
  1062. // list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  1063. // }
  1064. //}
  1065. ////如果存在手机号用户日评数据,那么需要做处理
  1066. //if reportView, ok := userReportViewMobileTotalMap[item.Mobile]; ok {
  1067. // //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  1068. // if reportView.LastViewTime.After(list[i].LastViewTime) {
  1069. // list[i].LastViewTime = reportView.LastViewTime
  1070. // list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  1071. // }
  1072. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  1073. // list[i].ViewTotal += reportView.ViewTotal
  1074. //}
  1075. ////如果存在手机号用户日评数据,那么需要做处理
  1076. //if reportView, ok := userReportViewEmailTotalMap[item.Email]; ok {
  1077. // //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  1078. // if reportView.LastViewTime.After(list[i].LastViewTime) {
  1079. // list[i].LastViewTime = reportView.LastViewTime
  1080. // list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  1081. // }
  1082. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  1083. // list[i].ViewTotal += reportView.ViewTotal
  1084. //}
  1085. //
  1086. ////如果存在手机号用户每日资讯,那么需要做处理
  1087. //if advisoryView, ok := userAdvisoryViewMobileTotalMap[item.Mobile]; ok {
  1088. // //fmt.Println("蝴蝶眨几次眼睛")
  1089. // //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  1090. // if advisoryView.CreatedTime.After(list[i].LastViewTime) {
  1091. // list[i].LastViewTime = advisoryView.CreatedTime
  1092. // list[i].LastViewTimeStr = advisoryView.CreatedTime.Format(utils.FormatDateTime)
  1093. // }
  1094. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数)
  1095. // list[i].ViewTotal += advisoryView.Total
  1096. //}
  1097. ////如果存在邮箱号用户每日资讯,那么需要做处理
  1098. //if advisoryView, ok := userAdvisoryViewEmailTotalMap[item.Email]; ok {
  1099. // //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  1100. // if advisoryView.CreatedTime.After(list[i].LastViewTime) {
  1101. // list[i].LastViewTime = advisoryView.CreatedTime
  1102. // list[i].LastViewTimeStr = advisoryView.CreatedTime.Format(utils.FormatDateTime)
  1103. // }
  1104. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数)
  1105. // list[i].ViewTotal += advisoryView.Total
  1106. //}
  1107. //
  1108. ////如果存在手机号用户查研观向,那么需要做处理
  1109. //if cygxView, ok := userCygxArticleHistoryRecordViewMobileTotalMap[item.Mobile]; ok {
  1110. // //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  1111. // if cygxView.CreatedTime.After(list[i].LastViewTime) {
  1112. // list[i].LastViewTime = cygxView.CreatedTime
  1113. // list[i].LastViewTimeStr = cygxView.CreatedTime.Format(utils.FormatDateTime)
  1114. // }
  1115. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数+查研观向)
  1116. // list[i].ViewTotal += cygxView.Total
  1117. //}
  1118. ////如果存在邮箱号用户查研观向,那么需要做处理
  1119. //if cygxView, ok := userCygxViewEmailTotalMap[item.Email]; ok {
  1120. // //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  1121. // if cygxView.CreatedTime.After(list[i].LastViewTime) {
  1122. // list[i].LastViewTime = cygxView.CreatedTime
  1123. // list[i].LastViewTimeStr = cygxView.CreatedTime.Format(utils.FormatDateTime)
  1124. // }
  1125. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数+查研观向)
  1126. // list[i].ViewTotal += cygxView.Total
  1127. //}
  1128. ///*if reportView, ok := userReportViewTotalMap[int(item.UserId)]; ok {
  1129. // //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  1130. // if reportView.LastViewTime.After(list[i].LastViewTime) {
  1131. // list[i].LastViewTime = reportView.LastViewTime
  1132. // list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  1133. // }
  1134. // //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  1135. // list[i].ViewTotal += reportView.ViewTotal
  1136. //}*/
  1137. list[i].ViewTotal = list[i].FiccViewTotal + list[i].RaiViewTotal
  1138. var tmpLastViewTime time.Time
  1139. tmpLastViewTime = list[i].FiccLastViewTime
  1140. if !list[i].RaiLastViewTime.IsZero() {
  1141. if tmpLastViewTime.IsZero() || list[i].RaiLastViewTime.After(tmpLastViewTime) {
  1142. tmpLastViewTime = list[i].RaiLastViewTime
  1143. }
  1144. }
  1145. list[i].LastViewTime = tmpLastViewTime
  1146. if !tmpLastViewTime.IsZero() {
  1147. list[i].LastViewTimeStr = tmpLastViewTime.Format(utils.FormatDateTime)
  1148. }
  1149. if list[i].LastViewTime.Before(time.Now().AddDate(0, 0, -7)) && list[i].IsFollow == 1 {
  1150. list[i].NotRead = true
  1151. }
  1152. //如果有绑定数据,那么将数据返回对应的注册绑定数据
  1153. if _, ok := userRecordRegisterMap[int(item.UserId)]; ok {
  1154. list[i].IsRegister = true
  1155. //list[i].RegisterTime = userRecordRegisterTime.Format(utils.FormatDateTime)
  1156. }
  1157. //是否共享联系人
  1158. if userSellerRelationTotal, ok := userSellerRelationTotalMap[int(item.UserId)]; ok {
  1159. //条数大于1,代表这是共享联系人
  1160. //if userSellerRelationTotal.Total > 1 {
  1161. // list[i].IsShared = true
  1162. //}
  1163. list[i].ProductIds = userSellerRelationTotal.ProductIds
  1164. }
  1165. // 图表权限
  1166. if _, isAdmin := sysUserMobileMap[list[i].Mobile]; isAdmin {
  1167. list[i].IsChartPermissionSetting = 2
  1168. } else {
  1169. if _, ok := userChartPermissionMap[int(item.UserId)]; ok {
  1170. list[i].IsChartPermissionSetting = 1
  1171. }
  1172. }
  1173. }
  1174. isUserYanXuanButtonShow, err := services.CheckCompanyUserYanXuanButton(sysUser.RoleTypeCode, sysUser.Mobile)
  1175. if err != nil {
  1176. br.Msg = "获取失败"
  1177. br.ErrMsg = "获取专项调研信息失败,Err:" + err.Error()
  1178. return
  1179. }
  1180. if isUserYanXuanButtonShow {
  1181. userYanxuanPermissionMap := cygxService.GetCygxUserYanxuanPermissionMapItem(userIds) // 如果是权益部门下的人,则展示研选相关的信息
  1182. userInteractionNumMap := cygxService.GetUserInteractionNumMap(userIds) // 如果是权益部门下的人,展示互动量相关的信息
  1183. mfyxuserRecordRegisterMap := make(map[int]string)
  1184. if len(userIdSlice) > 0 {
  1185. //用户绑定注册数据
  1186. mfyxuserRecordRegisterList, _ := models.GetUserRecordRegisterByUserIdsMyfx(userIdStr)
  1187. for _, userRecordRegister := range mfyxuserRecordRegisterList {
  1188. mfyxuserRecordRegisterMap[userRecordRegister.UserId] = userRecordRegister.CreateTime.Format(utils.FormatDateTime)
  1189. }
  1190. }
  1191. for i := 0; i < lenList; i++ {
  1192. item := list[i]
  1193. // 分产品阅读统计
  1194. list[i].YbProductViewTotal = userYbViewsMap[int(item.UserId)]
  1195. list[i].MfyxInteractionNum = userInteractionNumMap[int(item.UserId)]
  1196. //权益销售查看用户研选的信息
  1197. if userYanxuanPermissionMap[int(item.UserId)] != nil {
  1198. mfyxItem := userYanxuanPermissionMap[int(item.UserId)]
  1199. list[i].MfyxStatus = mfyxItem.Status
  1200. list[i].MfyxStartDate = mfyxItem.StartDate
  1201. list[i].MfyxEndDate = mfyxItem.EndDate
  1202. } else {
  1203. list[i].MfyxStatus = "未开通"
  1204. }
  1205. if mfyxuserRecordRegisterMap[int(item.UserId)] != "" {
  1206. list[i].MfyxIsBinding = true
  1207. list[i].MfyxBindingTime = mfyxuserRecordRegisterMap[int(item.UserId)]
  1208. }
  1209. list[i].LastViewTimeStr = list[i].RaiLastViewTime.Format(utils.FormatDateTime)
  1210. }
  1211. }
  1212. }
  1213. page := paging.GetPaging(currentIndex, pageSize, total)
  1214. resp := new(company.CompanyUserListResp)
  1215. resp.List = list
  1216. resp.Paging = page
  1217. br.Ret = 200
  1218. br.Success = true
  1219. br.Msg = "获取成功"
  1220. br.Data = resp
  1221. }
  1222. func (this *CompanyUserController) List2() {
  1223. br := new(models.BaseResponse).Init()
  1224. defer func() {
  1225. this.Data["json"] = br
  1226. this.ServeJSON()
  1227. }()
  1228. sysUser := this.SysUser
  1229. if sysUser == nil {
  1230. br.Msg = "请登录"
  1231. br.ErrMsg = "请登录,SysUser Is Empty"
  1232. br.Ret = 408
  1233. return
  1234. }
  1235. pageSize, _ := this.GetInt("PageSize")
  1236. currentIndex, _ := this.GetInt("CurrentIndex")
  1237. companyId, _ := this.GetInt("CompanyId")
  1238. keyWord := this.GetString("KeyWord")
  1239. if companyId <= 0 {
  1240. br.Msg = "请选择客户"
  1241. br.ErrMsg = "客户参数错误"
  1242. return
  1243. }
  1244. var startSize int
  1245. if pageSize <= 0 {
  1246. pageSize = utils.PageSize20
  1247. }
  1248. if currentIndex <= 0 {
  1249. currentIndex = 1
  1250. }
  1251. startSize = paging.StartIndex(currentIndex, pageSize)
  1252. var condition string
  1253. var pars []interface{}
  1254. if keyWord != "" {
  1255. condition += ` AND (a.real_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  1256. }
  1257. total, err := company.GetCompanyUserListCount(condition, pars, companyId)
  1258. if err != nil {
  1259. br.Msg = "获取失败"
  1260. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  1261. return
  1262. }
  1263. list, err := company.GetCompanyUserList(condition, pars, companyId, startSize, pageSize)
  1264. if err != nil {
  1265. br.Msg = "获取失败"
  1266. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1267. return
  1268. }
  1269. lenList := len(list)
  1270. for i := 0; i < lenList; i++ {
  1271. item := list[i]
  1272. if item.OpenId != "" {
  1273. list[i].IsRegister = true
  1274. } else {
  1275. if item.RegisterPlatform == 2 {
  1276. if item.Mobile != "" || item.Email != "" {
  1277. list[i].IsRegister = true
  1278. }
  1279. } else {
  1280. list[i].IsRegister = false
  1281. }
  1282. }
  1283. reportView, err := models.GetReportViewMaxTime(int(item.UserId))
  1284. if err != nil {
  1285. br.Msg = "获取失败"
  1286. br.ErrMsg = "获取GetReportViewMaxTime失败,Err:" + err.Error()
  1287. return
  1288. }
  1289. if reportView.LastViewTime.After(list[i].LastViewTime) {
  1290. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  1291. } else {
  1292. list[i].LastViewTimeStr = list[i].LastViewTime.Format(utils.FormatDateTime)
  1293. }
  1294. list[i].ViewTotal += reportView.ViewTotal
  1295. }
  1296. page := paging.GetPaging(currentIndex, pageSize, total)
  1297. resp := new(company.CompanyUserListResp)
  1298. resp.List = list
  1299. resp.Paging = page
  1300. br.Ret = 200
  1301. br.Success = true
  1302. br.Msg = "获取成功"
  1303. br.Data = resp
  1304. }
  1305. // @Title 获取客户名下联系人数量(根据当前账号角色区分)
  1306. // @Description 获取客户名下联系人数量(根据当前账号角色区分)接口
  1307. // @Param CompanyId query int true "客户id"
  1308. // @Param ProductId query int true "产品权限id"
  1309. // @Success 200 {object} company.CompanyUserTotal
  1310. // @router /company_user/total [get]
  1311. func (this *CompanyUserController) GetUserCountByCompanyId() {
  1312. br := new(models.BaseResponse).Init()
  1313. defer func() {
  1314. this.Data["json"] = br
  1315. this.ServeJSON()
  1316. }()
  1317. sysUser := this.SysUser
  1318. if sysUser == nil {
  1319. br.Msg = "请登录"
  1320. br.ErrMsg = "请登录,SysUser Is Empty"
  1321. br.Ret = 408
  1322. return
  1323. }
  1324. companyId, _ := this.GetInt("CompanyId")
  1325. if companyId <= 0 {
  1326. br.Msg = "参数错误"
  1327. return
  1328. }
  1329. productId, _ := this.GetInt("ProductId")
  1330. //如果没有传入,那么去根据权限来获取id
  1331. if productId <= 0 {
  1332. productId = services.GetProductId(sysUser.RoleTypeCode)
  1333. }
  1334. total, err := services.GetUserCountByCompanyId(companyId, productId)
  1335. if err != nil {
  1336. br.Msg = "获取失败"
  1337. br.Msg = "获取联系人总数失败,Err:" + err.Error()
  1338. return
  1339. }
  1340. br.Ret = 200
  1341. br.Success = true
  1342. br.Msg = "获取成功"
  1343. br.Data = company.CompanyUserTotal{
  1344. Total: total,
  1345. }
  1346. }
  1347. // @Title 删除客户联系人
  1348. // @Description 删除客户联系人接口
  1349. // @Param request body company.DeleteUserReq true "type json string"
  1350. // @router /user/delete [post]
  1351. func (this *CompanyUserController) DeleteUser() {
  1352. br := new(models.BaseResponse).Init()
  1353. defer func() {
  1354. this.Data["json"] = br
  1355. this.ServeJSON()
  1356. }()
  1357. sysUser := this.SysUser
  1358. if sysUser == nil {
  1359. br.Msg = "请登录"
  1360. br.ErrMsg = "请登录,SysUser Is Empty"
  1361. br.Ret = 408
  1362. return
  1363. }
  1364. var req company.DeleteUserReq
  1365. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1366. if err != nil {
  1367. br.Msg = "参数解析异常!"
  1368. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1369. return
  1370. }
  1371. if req.UserId <= 0 {
  1372. br.Msg = "参数错误!"
  1373. return
  1374. }
  1375. //获取联系人详情
  1376. userInfo, err := models.GetWxUserByUserId(req.UserId)
  1377. if err != nil {
  1378. br.Msg = "获取联系人异常!"
  1379. br.ErrMsg = "获取联系人异常,Err:" + err.Error()
  1380. return
  1381. }
  1382. //产品权限
  1383. productId := services.GetProductId(sysUser.RoleTypeCode)
  1384. //操作权限校验
  1385. errMsg, err := checkOpCompanyUserByUserId(req.UserId, productId, sysUser.AdminId, sysUser.RoleTypeCode)
  1386. if err != nil {
  1387. br.Msg = "删除失败!" + errMsg
  1388. br.ErrMsg = "删除失败,Err:" + err.Error()
  1389. return
  1390. }
  1391. userRelationList := make([]*models.UserSellerRelation, 0)
  1392. //如果不是超管权限,那么删除的时候,需要获取下联系人归属销售信息
  1393. if productId != 0 {
  1394. tmpUserRelationList, err := models.GetUserSellerRelationList(req.UserId)
  1395. if err != nil {
  1396. br.Msg = "删除失败"
  1397. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  1398. return
  1399. }
  1400. if len(tmpUserRelationList) > 0 {
  1401. userRelationList = append(userRelationList, tmpUserRelationList...)
  1402. }
  1403. }
  1404. //联系人与销售的关系
  1405. userSellerRelationList, err := models.GetUserSellerRelationList(int(userInfo.UserId))
  1406. if err != nil {
  1407. return
  1408. }
  1409. userSellerRelationInfo, _ := json.Marshal(userSellerRelationList)
  1410. //删除所有的标识,并真正删除数据
  1411. _ = models.DeleteUserSellerRelationByProductId(req.UserId, 1)
  1412. _ = models.DeleteUserSellerRelationByProductId(req.UserId, 2)
  1413. //真正删除用户
  1414. err = services.DeleteWxUser(req.UserId)
  1415. if err != nil {
  1416. br.Msg = "删除失败!"
  1417. br.ErrMsg = "删除失败,Err:" + err.Error()
  1418. return
  1419. }
  1420. if len(userRelationList) > 0 {
  1421. companyInfo, err := company.GetCompanyById(userRelationList[0].CompanyId)
  1422. if err != nil {
  1423. return
  1424. }
  1425. content := fmt.Sprint("联系人:", userInfo.RealName, "被删除")
  1426. if companyInfo != nil {
  1427. content = fmt.Sprint(companyInfo.CompanyName, ":", content)
  1428. }
  1429. for _, userRelation := range userRelationList {
  1430. if userRelation.ProductId != productId {
  1431. go services.AddCompanyMessage(sysUser.AdminId, userRelation.SellerId, userRelation.CompanyId, 0, 1, companyInfo.CompanyName, content, content)
  1432. }
  1433. }
  1434. }
  1435. //更新客户最近阅读次数以及最近阅读时间
  1436. go services.ModifyCompanyProductLastViewData([]int{userInfo.CompanyId})
  1437. //联系人信息
  1438. originalUserInfo, _ := json.Marshal(userInfo)
  1439. go services.AddWxUserOpLog(company.WxUserOpLog{
  1440. LogType: "delete",
  1441. UserId: int(userInfo.UserId),
  1442. CompanyId: userInfo.CompanyId,
  1443. Mobile: userInfo.Mobile,
  1444. Email: userInfo.Email,
  1445. OriginalUserInfo: string(originalUserInfo),
  1446. UserInfo: "",
  1447. OriginalUserSellerInfo: string(userSellerRelationInfo),
  1448. UserSellerInfo: "",
  1449. OpUserId: sysUser.AdminId,
  1450. OpUserName: sysUser.RealName,
  1451. CreateTime: time.Now(),
  1452. })
  1453. // 若该联系人为弘则研究公司下的,则同步禁用手机号关联的系统管理员
  1454. if userInfo.CompanyId == 16 {
  1455. go services.ForbiddenSysUserByMobile(userInfo.Mobile)
  1456. }
  1457. br.Ret = 200
  1458. br.Success = true
  1459. br.Msg = "删除成功"
  1460. br.IsAddLog = true
  1461. }
  1462. // @Title 编辑客户联系人
  1463. // @Description 编辑客户联系人接口
  1464. // @Param request body company.EditUserReq true "type json string"
  1465. // @router /user/edit [post]
  1466. func (this *CompanyUserController) EditUser() {
  1467. br := new(models.BaseResponse).Init()
  1468. defer func() {
  1469. this.Data["json"] = br
  1470. this.ServeJSON()
  1471. }()
  1472. sysUser := this.SysUser
  1473. if sysUser == nil {
  1474. br.Msg = "请登录"
  1475. br.ErrMsg = "请登录,SysUser Is Empty"
  1476. br.Ret = 408
  1477. return
  1478. }
  1479. var req company.EditUserReq
  1480. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1481. if err != nil {
  1482. br.Msg = "参数解析异常!"
  1483. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1484. return
  1485. }
  1486. if req.UserId <= 0 {
  1487. br.Msg = "参数错误!"
  1488. return
  1489. }
  1490. //移除空格
  1491. req.MobileOne = utils.TrimStr(req.MobileOne)
  1492. req.MobileTwo = utils.TrimStr(req.MobileTwo)
  1493. req.Email = utils.TrimStr(req.Email)
  1494. req.RealName = utils.TrimStr(req.RealName)
  1495. productId := services.GetProductId(sysUser.RoleTypeCode)
  1496. //操作权限校验
  1497. wxUser, err := models.GetWxUserByUserId(req.UserId)
  1498. if err != nil {
  1499. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  1500. br.Msg = "获取信息失败"
  1501. return
  1502. }
  1503. //如果用户已经存在名片的情况下,但是二次编辑的时候前端没传名片信息,那么名片信息不做任何变更,并发送异常信息提醒
  1504. if wxUser.BusinessCardUrl != "" && req.BusinessCardUrl == "" {
  1505. req.BusinessCardUrl = wxUser.BusinessCardUrl
  1506. go alarm_msg.SendAlarmMsg(fmt.Sprint("用户已经存在名片的情况下,但是二次编辑的时候前端没传名片信息。操作销售:", sysUser.RealName, "被操作UserId:", req.UserId), 3)
  1507. }
  1508. //获取当前操作员产品权限
  1509. companyProductList, productErr := getSysCompanyProductList(productId, wxUser.CompanyId)
  1510. if productErr != nil {
  1511. br.Msg = "编辑失败!获取当前操作员产品权限异常"
  1512. br.ErrMsg = "获取当前操作员产品权限异常,Err:" + productErr.Error()
  1513. return
  1514. }
  1515. if companyProductList == nil {
  1516. br.Msg = "编辑失败!获取当前操作员找不到产品权限"
  1517. br.ErrMsg = "获取当前操作员找不到产品权限"
  1518. return
  1519. }
  1520. item, err := company.GetCompanyDetailById(wxUser.CompanyId)
  1521. if err != nil {
  1522. br.Msg = "获取信息失败"
  1523. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1524. return
  1525. }
  1526. //操作权限校验
  1527. for _, v := range companyProductList {
  1528. companyUserStatus := services.CheckCompanyUserButton(sysUser.RoleTypeCode, v.SellerId, sysUser.AdminId, v.ProductId, item.ShareSellerId, v.Status)
  1529. if item.ShareSellerId == sysUser.AdminId {
  1530. companyUserStatus = true
  1531. }
  1532. if companyUserStatus == false {
  1533. br.Msg = "没有操作权限"
  1534. br.ErrMsg = "没有操作权限"
  1535. return
  1536. }
  1537. }
  1538. if req.RealName == "" {
  1539. br.Msg = "请填写姓名"
  1540. return
  1541. }
  1542. oldWxUser := *wxUser //旧的联系人信息(赋值,而不是赋址)
  1543. if req.MobileOne == "" && req.Email == "" {
  1544. br.Msg = "请输入手机号或邮箱"
  1545. return
  1546. }
  1547. //不是管理员的情况下://手机号邮箱填写校验
  1548. if productId != 0 {
  1549. companyInfo, err := company.GetCompanyById(req.CompanyId)
  1550. if err != nil {
  1551. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  1552. br.Msg = "获取客户信息失败"
  1553. return
  1554. }
  1555. for _, v := range companyProductList {
  1556. //权益(海外)客户,需要有提示
  1557. if v.ProductId == 2 && companyInfo.RegionType == "海外" {
  1558. if req.MobileOne == "" {
  1559. br.Msg = "请输入手机号"
  1560. }
  1561. if req.Email == "" {
  1562. br.Msg = "请输入邮箱"
  1563. br.ErrMsg = "请输入邮箱"
  1564. return
  1565. }
  1566. }
  1567. }
  1568. }
  1569. if req.MobileOne != "" && req.MobileOne != wxUser.Mobile {
  1570. mobileOneItem, err := company.GetUserCountByMobile(req.MobileOne)
  1571. if err != nil && err.Error() == utils.ErrNoRow() {
  1572. mobileOneItem, err = company.CheckCompanyUserCountByMobileTwo(req.MobileOne)
  1573. if err != nil && err.Error() != utils.ErrNoRow() {
  1574. br.Msg = "编辑失败"
  1575. br.ErrMsg = "判断手机号是否存在,Err:" + err.Error()
  1576. return
  1577. }
  1578. }
  1579. if mobileOneItem != nil {
  1580. if mobileOneItem.UserId > 0 {
  1581. if req.CompanyId > 0 {
  1582. //获取联系人分组信息
  1583. userSellerGroupInfo, err := models.GetUserGroupSellerByUserId(int(mobileOneItem.UserId))
  1584. if err != nil && err.Error() != utils.ErrNoRow() {
  1585. br.Msg = "查询信息异常"
  1586. br.Success = true
  1587. br.Data = mobileOneItem
  1588. br.IsSendEmail = false
  1589. br.Ret = 600
  1590. return
  1591. }
  1592. if req.CompanyId == mobileOneItem.CompanyId {
  1593. //修改返回给前端用户的信息
  1594. if userSellerGroupInfo != nil {
  1595. mobileOneItem.SellerRealName = userSellerGroupInfo.SellerRealName
  1596. mobileOneItem.Status = userSellerGroupInfo.Status
  1597. }
  1598. br.Msg = "手机号1已存在"
  1599. br.Success = true
  1600. br.Data = mobileOneItem
  1601. br.IsSendEmail = false
  1602. br.Ret = 600
  1603. return
  1604. } else {
  1605. hasMove := true //是否可移动
  1606. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  1607. if mobileOneItem.CompanyId > 1 {
  1608. //获取联系人分组信息
  1609. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(mobileOneItem.UserId))
  1610. //根据分组校验
  1611. if err == nil && len(userSellerGroupInfoList) > 0 {
  1612. sellerMap := make(map[int]string) //销售信息map
  1613. sellerNameSlice := make([]string, 0) //销售姓名切片
  1614. statusNameSlice := make([]string, 0) //客户状态姓名切片
  1615. for _, v := range userSellerGroupInfoList {
  1616. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  1617. hasMove = false
  1618. }
  1619. statusNameSlice = append(statusNameSlice, v.Status)
  1620. if _, ok := sellerMap[v.SellerId]; ok {
  1621. continue
  1622. }
  1623. sellerMap[v.SellerId] = v.SellerRealName
  1624. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  1625. }
  1626. mobileOneItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  1627. mobileOneItem.Status = strings.Join(statusNameSlice, "/")
  1628. }
  1629. }
  1630. //是否可移动
  1631. mobileOneItem.HasMove = hasMove
  1632. br.Msg = "手机号1已存在"
  1633. br.Success = true
  1634. br.Data = mobileOneItem
  1635. br.IsSendEmail = false
  1636. br.Ret = 600
  1637. return
  1638. }
  1639. } else {
  1640. if strings.Contains(mobileOneItem.Status, "/") {
  1641. if strings.Contains(mobileOneItem.Status, "正式") || strings.Contains(mobileOneItem.Status, "试用") {
  1642. mobileOneItem.HasMove = false
  1643. } else {
  1644. mobileOneItem.HasMove = true
  1645. }
  1646. } else {
  1647. if mobileOneItem.Status == utils.COMPANY_STATUS_FREEZE || //冻结
  1648. mobileOneItem.Status == utils.COMPANY_STATUS_LOSE || //流失
  1649. mobileOneItem.Status == utils.COMPANY_STATUS_POTENTIAL || //潜在
  1650. mobileOneItem.Status == utils.COMPANY_STATUS_FOREVER { //永续
  1651. mobileOneItem.HasMove = true
  1652. }
  1653. }
  1654. br.Msg = "手机号1已存在"
  1655. br.Success = true
  1656. br.Data = mobileOneItem
  1657. br.IsSendEmail = false
  1658. br.Ret = 600
  1659. return
  1660. }
  1661. }
  1662. }
  1663. }
  1664. //手机号2校验(不需要修改,修改时间:2021年03月09日11:23:37)
  1665. /*if req.MobileTwo != "" && req.MobileTwo != wxUser.MobileTwo {
  1666. mobileTwoItem, err := company.GetUserCountByMobile(req.MobileTwo)
  1667. if err != nil && err.Error() == utils.ErrNoRow() {
  1668. mobileTwoItem, err = company.CheckCompanyUserCountByMobileTwo(req.MobileTwo)
  1669. if err != nil && err.Error() != utils.ErrNoRow() {
  1670. br.Msg = "编辑失败"
  1671. br.ErrMsg = "判断手机号2是否存在,Err:" + err.Error()
  1672. return
  1673. }
  1674. }
  1675. if mobileTwoItem != nil {
  1676. if mobileTwoItem.UserId > 0 {
  1677. if req.CompanyId > 0 {
  1678. if req.CompanyId == mobileTwoItem.CompanyId {
  1679. br.Msg = "手机号2已存在"
  1680. br.Success = true
  1681. br.Data = mobileTwoItem
  1682. br.IsSendEmail = false
  1683. br.Ret = 600
  1684. return
  1685. } else {
  1686. if strings.Contains(mobileTwoItem.Status, "/") {
  1687. if strings.Contains(mobileTwoItem.Status, "正式") || strings.Contains(mobileTwoItem.Status, "试用") {
  1688. mobileTwoItem.HasMove = false
  1689. } else {
  1690. mobileTwoItem.HasMove = true
  1691. }
  1692. } else {
  1693. if mobileTwoItem.Status == utils.COMPANY_STATUS_FREEZE || //冻结
  1694. mobileTwoItem.Status == utils.COMPANY_STATUS_LOSE || //流失
  1695. mobileTwoItem.Status == utils.COMPANY_STATUS_POTENTIAL || //潜在
  1696. mobileTwoItem.Status == utils.COMPANY_STATUS_FOREVER { //永续
  1697. mobileTwoItem.HasMove = true
  1698. }
  1699. }
  1700. br.Msg = "手机号2已存在"
  1701. br.Success = true
  1702. br.Data = mobileTwoItem
  1703. br.IsSendEmail = false
  1704. br.Ret = 600
  1705. return
  1706. }
  1707. } else {
  1708. if strings.Contains(mobileTwoItem.Status, "/") {
  1709. if strings.Contains(mobileTwoItem.Status, "正式") || strings.Contains(mobileTwoItem.Status, "试用") {
  1710. mobileTwoItem.HasMove = false
  1711. } else {
  1712. mobileTwoItem.HasMove = true
  1713. }
  1714. } else {
  1715. if mobileTwoItem.Status == utils.COMPANY_STATUS_FREEZE || //冻结
  1716. mobileTwoItem.Status == utils.COMPANY_STATUS_LOSE || //流失
  1717. mobileTwoItem.Status == utils.COMPANY_STATUS_POTENTIAL || //潜在
  1718. mobileTwoItem.Status == utils.COMPANY_STATUS_FOREVER { //永续
  1719. mobileTwoItem.HasMove = true
  1720. }
  1721. }
  1722. br.Msg = "手机号2已存在"
  1723. br.Success = true
  1724. br.Data = mobileTwoItem
  1725. br.IsSendEmail = false
  1726. br.Ret = 600
  1727. return
  1728. }
  1729. }
  1730. }
  1731. }*/
  1732. if req.Email != "" && req.Email != wxUser.Email {
  1733. emailItem, err := company.CheckCompanyUserCountByEmail(req.Email)
  1734. if err != nil && err.Error() != utils.ErrNoRow() {
  1735. br.Msg = "编辑失败"
  1736. br.ErrMsg = "编辑失败,Err:" + err.Error()
  1737. return
  1738. }
  1739. if emailItem != nil {
  1740. if emailItem.UserId > 0 {
  1741. //获取联系人分组信息
  1742. userSellerGroupInfo, err := models.GetUserGroupSellerByUserId(int(emailItem.UserId))
  1743. if err != nil && err.Error() != utils.ErrNoRow() {
  1744. br.Msg = "查询信息异常"
  1745. br.Success = true
  1746. br.Data = emailItem
  1747. br.IsSendEmail = false
  1748. br.Ret = 600
  1749. return
  1750. }
  1751. if req.CompanyId == emailItem.CompanyId {
  1752. //修改返回给前端用户的信息
  1753. if userSellerGroupInfo != nil {
  1754. emailItem.SellerRealName = userSellerGroupInfo.SellerRealName
  1755. emailItem.Status = userSellerGroupInfo.Status
  1756. }
  1757. br.Msg = "邮箱已存在"
  1758. br.Success = true
  1759. br.Data = emailItem
  1760. br.IsSendEmail = false
  1761. br.Ret = 600
  1762. return
  1763. } else {
  1764. hasMove := true //是否可移动
  1765. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  1766. if emailItem.CompanyId > 1 {
  1767. //获取联系人分组信息
  1768. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(emailItem.UserId))
  1769. //根据分组校验
  1770. if err == nil && len(userSellerGroupInfoList) > 0 {
  1771. sellerMap := make(map[int]string) //销售信息map
  1772. sellerNameSlice := make([]string, 0) //销售姓名切片
  1773. statusNameSlice := make([]string, 0) //客户状态姓名切片
  1774. for _, v := range userSellerGroupInfoList {
  1775. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  1776. hasMove = false
  1777. }
  1778. statusNameSlice = append(statusNameSlice, v.Status)
  1779. if _, ok := sellerMap[v.SellerId]; ok {
  1780. continue
  1781. }
  1782. sellerMap[v.SellerId] = v.SellerRealName
  1783. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  1784. }
  1785. emailItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  1786. emailItem.Status = strings.Join(statusNameSlice, "/")
  1787. }
  1788. }
  1789. //是否可移动
  1790. emailItem.HasMove = hasMove
  1791. br.Msg = "邮箱已存在"
  1792. br.Success = true
  1793. br.Data = emailItem
  1794. br.IsSendEmail = false
  1795. br.Ret = 600
  1796. return
  1797. }
  1798. } else {
  1799. if strings.Contains(emailItem.Status, "/") {
  1800. if strings.Contains(emailItem.Status, "正式") || strings.Contains(emailItem.Status, "试用") {
  1801. emailItem.HasMove = false
  1802. } else {
  1803. emailItem.HasMove = true
  1804. }
  1805. } else {
  1806. if emailItem.Status == utils.COMPANY_STATUS_FREEZE || //冻结
  1807. emailItem.Status == utils.COMPANY_STATUS_LOSE || //流失
  1808. emailItem.Status == utils.COMPANY_STATUS_POTENTIAL || //潜在
  1809. emailItem.Status == utils.COMPANY_STATUS_FOREVER { //永续
  1810. emailItem.HasMove = true
  1811. }
  1812. }
  1813. br.Msg = "邮箱已存在"
  1814. br.Success = true
  1815. br.Data = emailItem
  1816. br.IsSendEmail = false
  1817. br.Ret = 600
  1818. return
  1819. }
  1820. }
  1821. }
  1822. //if wxUser.BusinessCardUrl != req.BusinessCardUrl && utils.RunMode == "release" {
  1823. if wxUser.BusinessCardUrl != req.BusinessCardUrl {
  1824. // 获取图片识别客户名称的配置
  1825. crmConfig, err := company.GetConfigDetailByCode("card_recognition_company_name")
  1826. if err != nil {
  1827. br.Msg = "获取配置失败"
  1828. br.ErrMsg = "获取配置失败"
  1829. br.IsSendEmail = false
  1830. return
  1831. }
  1832. // 获取图片识别手机号的配置
  1833. crmConfig2, err := company.GetConfigDetailByCode("card_recognition_tel")
  1834. if err != nil {
  1835. br.Msg = "获取配置失败"
  1836. br.ErrMsg = "获取配置失败"
  1837. br.IsSendEmail = false
  1838. return
  1839. }
  1840. if crmConfig.ConfigValue == "true" || crmConfig2.ConfigValue == "true" {
  1841. //companyName := wxUser.CompanyName
  1842. //if companyName == "" {
  1843. companyInfo, err := company.GetCompanyById(req.CompanyId)
  1844. if err != nil {
  1845. br.Msg = "查找客户失败"
  1846. br.ErrMsg = "查找客户失败,err:" + err.Error()
  1847. return
  1848. }
  1849. companyName := companyInfo.CompanyName
  1850. //}
  1851. companyName = utils.TrimStr(companyName)
  1852. tmpCompanyNameList, err := elastic.Analyze(companyName)
  1853. if err != nil {
  1854. br.Msg = "添加失败"
  1855. br.ErrMsg = "客户名称分词失败,Err:" + err.Error()
  1856. return
  1857. }
  1858. //公司名称切片
  1859. analyzeNameList := []string{companyName}
  1860. for _, v := range tmpCompanyNameList {
  1861. if v == "" {
  1862. continue
  1863. }
  1864. ignoreStrs := []string{"", "有限", "投资", "资本", "管理", "贸易", "资产", "公司", "股份", "集团", "有限公司"}
  1865. if !strings.Contains(strings.Join(ignoreStrs, ","), v) && utf8.RuneCount([]byte(v)) > 1 {
  1866. //不在上面的过滤字里面,且切出来的字符大于1位
  1867. analyzeNameList = append(analyzeNameList, v)
  1868. }
  1869. }
  1870. //名片识别出来的客户名称切片
  1871. companyNameSlice := make([]string, 0)
  1872. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  1873. if err != nil {
  1874. br.Msg = "名片识别失败"
  1875. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  1876. return
  1877. }
  1878. //if card.Errno != 0 {
  1879. // br.Msg = "您上传了非名片图片,请重新上传"
  1880. // br.ErrMsg = "名片识别失败,ErrMsg:" + card.ErrorMsg + ";errCode:" + strconv.Itoa(card.Errno)
  1881. // return
  1882. //}
  1883. //var mobileStr string
  1884. //for _, v := range card.WordsResult {
  1885. // mobileStr += v.Words + ","
  1886. //}
  1887. isFlag := false
  1888. if len(card.WordsResult.MOBILE) > 0 {
  1889. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  1890. //if req.MobileOne != "" {
  1891. // if strings.Contains(mobileStr, req.MobileOne) || mobileStr == "" {
  1892. // isFlag = true
  1893. // } else {
  1894. // isFlag = false
  1895. // }
  1896. //}
  1897. //
  1898. //if isFlag == false && req.MobileTwo != "" {
  1899. // if strings.Contains(mobileStr, req.MobileTwo) {
  1900. // isFlag = true
  1901. // } else {
  1902. // isFlag = false
  1903. // }
  1904. //}
  1905. if req.MobileOne != "" && strings.Contains(mobileStr, req.MobileOne) {
  1906. isFlag = true
  1907. }
  1908. if req.MobileTwo != "" && strings.Contains(mobileStr, req.MobileTwo) {
  1909. isFlag = true
  1910. }
  1911. //如果百度识别出来了客户名称,那么将识别出来的客户名称添加到待校验客户名称切片中
  1912. if len(card.WordsResult.COMPANY) > 0 && card.WordsResult.COMPANY[0] != "" {
  1913. companyNameSlice = card.WordsResult.COMPANY
  1914. }
  1915. }
  1916. //如果百度未识别出来手机号或者公司名称,那么进行阿里识别
  1917. if !isFlag || (len(companyNameSlice) == 0 && crmConfig.ConfigValue == "true") {
  1918. //阿里云识别
  1919. aliyunResult, errMsg, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
  1920. if err != nil {
  1921. br.Msg = "识别失败"
  1922. br.ErrMsg = "识别失败,Err:" + err.Error()
  1923. return
  1924. }
  1925. if !aliyunResult.Success {
  1926. br.Msg = "识别失败"
  1927. br.ErrMsg = "识别失败"
  1928. return
  1929. }
  1930. if errMsg != "" {
  1931. br.Msg = errMsg
  1932. return
  1933. }
  1934. //如果百度没有识别出来手机号,那么还需要阿里识别出来的手机号校验下
  1935. if !isFlag {
  1936. if len(aliyunResult.TelCell) > 0 {
  1937. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  1938. if req.MobileOne != "" {
  1939. if strings.Contains(mobileStr, req.MobileOne) {
  1940. isFlag = true
  1941. } else {
  1942. isFlag = false
  1943. }
  1944. }
  1945. } else {
  1946. isFlag = true
  1947. }
  1948. }
  1949. //如果阿里云识别出来了客户名称,那么将识别出来的客户名称添加到待校验客户名称切片中
  1950. if len(aliyunResult.Company) > 0 {
  1951. companyNameSlice = append(companyNameSlice, aliyunResult.Company...)
  1952. }
  1953. }
  1954. if !isFlag && crmConfig2.ConfigValue == "true" {
  1955. br.Msg = "名片信息与联系人手机号不符"
  1956. br.IsSendEmail = false
  1957. return
  1958. }
  1959. //校验识别出来的名片名称与传入的客户名称是否一致
  1960. if len(companyNameSlice) > 0 && crmConfig.ConfigValue == "true" {
  1961. isCompanyNameFlag := false //是否成功匹配客户名称
  1962. //遍历识别出来的客户名称,然后与系统的客户名称做匹配(识别出来的名称包含系统名称的话,那么标记成功匹配)
  1963. for _, tmpCompanyName := range companyNameSlice {
  1964. //空串不处理
  1965. if companyName == "" {
  1966. continue
  1967. }
  1968. for _, analyzeName := range analyzeNameList {
  1969. //如果分词出来的公司名称正好在识别出来的公司名称里面,那么就认为校验通过了
  1970. if strings.Contains(tmpCompanyName, analyzeName) {
  1971. isCompanyNameFlag = true
  1972. break
  1973. }
  1974. }
  1975. if isCompanyNameFlag {
  1976. break
  1977. }
  1978. }
  1979. //如果没有匹配到客户名称,那么就返回报错
  1980. if !isCompanyNameFlag {
  1981. br.Msg = "名片信息与公司名称不符"
  1982. br.IsSendEmail = false
  1983. return
  1984. }
  1985. }
  1986. }
  1987. }
  1988. //UPDATE wx_user
  1989. // SET
  1990. // last_updated_time = NOW(),
  1991. // position = ?,
  1992. // is_maker = ?,
  1993. // business_card_url = ?,
  1994. // mobile_two= ?,
  1995. // department_name=?,
  1996. // company_id=?,country_code=?
  1997. //待更新字段
  1998. updateCol := []string{"RealName", "Mobile", "MobileTwo", "Email", "Sex", "LastUpdatedTime", "Position", "IsMaker", "BusinessCardUrl", "DepartmentName", "CompanyId", "CountryCode"}
  1999. //外呼手机号、外呼手机号区号
  2000. if wxUser.OutboundMobile == "" || wxUser.OutboundMobile == wxUser.Mobile {
  2001. wxUser.OutboundMobile = req.MobileOne
  2002. wxUser.OutboundCountryCode = req.CountryCode
  2003. //外呼字段数据修改
  2004. updateCol = append(updateCol, "OutboundMobile", "OutboundCountryCode")
  2005. }
  2006. wxUser.RealName = req.RealName
  2007. wxUser.Mobile = req.MobileOne
  2008. wxUser.MobileTwo = req.MobileTwo
  2009. wxUser.Email = req.Email
  2010. wxUser.Sex = req.Sex
  2011. wxUser.LastUpdatedTime = time.Now()
  2012. wxUser.Position = req.Position
  2013. wxUser.IsMaker = req.IsMaker
  2014. wxUser.BusinessCardUrl = req.BusinessCardUrl
  2015. wxUser.DepartmentName = req.DepartmentName
  2016. wxUser.CompanyId = req.CompanyId
  2017. wxUser.CountryCode = req.CountryCode
  2018. fmt.Println("wxUser 2", wxUser)
  2019. err = wxUser.Update(updateCol)
  2020. //err = company.EditCompanyUser(&req)
  2021. if err != nil {
  2022. br.Msg = "编辑失败!"
  2023. br.ErrMsg = "编辑失败,Err:" + err.Error()
  2024. return
  2025. }
  2026. //更新客户最近阅读次数以及最近阅读时间
  2027. go services.ModifyCompanyProductLastViewData([]int{wxUser.CompanyId})
  2028. //联系人信息
  2029. originalUserInfo, _ := json.Marshal(oldWxUser)
  2030. userInfo, _ := json.Marshal(wxUser)
  2031. go services.AddWxUserOpLog(company.WxUserOpLog{
  2032. LogType: "edit",
  2033. UserId: int(wxUser.UserId),
  2034. CompanyId: wxUser.CompanyId,
  2035. Mobile: wxUser.Mobile,
  2036. Email: wxUser.Email,
  2037. OriginalUserInfo: string(originalUserInfo),
  2038. UserInfo: string(userInfo),
  2039. OriginalUserSellerInfo: "",
  2040. UserSellerInfo: "",
  2041. OpUserId: sysUser.AdminId,
  2042. OpUserName: sysUser.RealName,
  2043. CreateTime: time.Now(),
  2044. })
  2045. br.Ret = 200
  2046. br.Success = true
  2047. br.Msg = "编辑成功"
  2048. br.IsAddLog = true
  2049. }
  2050. // @Title 潜在用户列表
  2051. // @Description 潜在用户列表接口
  2052. // @Param PageSize query int true "每页数据条数"
  2053. // @Param CurrentIndex query int true "当前页页码,从1开始"
  2054. // @Param ApplyMethod query int true "申请方式,0:全部,1:未申请,2:已付费客户申请试用,3:非客户申请试用"
  2055. // @Param KeyWord query string true "搜索关键词"
  2056. // @Success 200 {object} models.PotentialUserListResp
  2057. // @router /potential/user/list [get]
  2058. func (this *CompanyController) PotentialUserList() {
  2059. br := new(models.BaseResponse).Init()
  2060. defer func() {
  2061. this.Data["json"] = br
  2062. this.ServeJSON()
  2063. }()
  2064. sysUser := this.SysUser
  2065. if sysUser == nil {
  2066. br.Msg = "请登录"
  2067. br.ErrMsg = "请登录,SysUser Is Empty"
  2068. br.Ret = 408
  2069. return
  2070. }
  2071. pageSize, _ := this.GetInt("PageSize")
  2072. currentIndex, _ := this.GetInt("CurrentIndex")
  2073. keyWord := this.GetString("KeyWord")
  2074. applyMethod, _ := this.GetInt("ApplyMethod")
  2075. var startSize int
  2076. if pageSize <= 0 {
  2077. pageSize = utils.PageSize20
  2078. }
  2079. if currentIndex <= 0 {
  2080. currentIndex = 1
  2081. }
  2082. startSize = utils.StartIndex(currentIndex, pageSize)
  2083. var condition string
  2084. var pars []interface{}
  2085. if applyMethod > 0 {
  2086. condition += ` AND apply_method=? `
  2087. pars = append(pars, applyMethod-1)
  2088. }
  2089. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER {
  2090. condition += ` AND apply_method=? `
  2091. pars = append(pars, 1)
  2092. }
  2093. //非管理员不让看数据
  2094. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  2095. list := make([]*models.PotentialUserItem, 0)
  2096. page := paging.GetPaging(currentIndex, pageSize, 0)
  2097. resp := new(models.PotentialUserListResp)
  2098. resp.List = list
  2099. resp.Paging = page
  2100. br.Ret = 200
  2101. br.Success = true
  2102. br.Msg = "获取成功"
  2103. br.Data = resp
  2104. return
  2105. }
  2106. if keyWord != "" {
  2107. condition += ` AND (a.real_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  2108. }
  2109. total, err := models.GetPotentialUserListCount(condition, pars)
  2110. if err != nil {
  2111. br.Msg = "获取失败"
  2112. br.ErrMsg = "获取分页数据失败,Err:" + err.Error()
  2113. return
  2114. }
  2115. list, err := models.GetPotentialUserList(condition, pars, startSize, pageSize)
  2116. if err != nil {
  2117. br.Msg = "获取失败"
  2118. br.ErrMsg = "获取失败,Err:" + err.Error()
  2119. return
  2120. }
  2121. listLen := len(list)
  2122. mobilesSlice := make([]string, 0)
  2123. emailsSlice := make([]string, 0)
  2124. for _, v := range list {
  2125. if v.Mobile != "" {
  2126. mobilesSlice = append(mobilesSlice, v.Mobile)
  2127. } else if v.Email != "" {
  2128. emailsSlice = append(emailsSlice, v.Email)
  2129. }
  2130. }
  2131. //用户浏览数据
  2132. mobileStr := strings.Join(mobilesSlice, "','")
  2133. mobileStr = "'" + mobileStr + "'"
  2134. emailStr := strings.Join(emailsSlice, "','")
  2135. emailStr = "'" + emailStr + "'"
  2136. //userViewTotalList, _ := models.GetCountUserViewHistoryByUserIds(userIdStr)
  2137. //userViewTotalMap := make(map[int]*models.UserViewTotalSlice)
  2138. //for _, userView := range userViewTotalList {
  2139. // userViewTotalMap[userView.UserId] = userView
  2140. //}
  2141. //用户日评浏览数据
  2142. //userReportViewTotalList, _ := models.GetReportViewMaxTimeByUserIds(userIdStr)
  2143. //userReportViewTotalMap := make(map[int]*models.ReportViewRecord)
  2144. //for _, userReportView := range userReportViewTotalList {
  2145. // userReportViewTotalMap[userReportView.UserId] = userReportView
  2146. //}
  2147. //手机号
  2148. userViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  2149. userReportViewMobileTotalMap := make(map[string]*models.ReportViewMobileRecord)
  2150. if len(mobilesSlice) > 0 {
  2151. //用户浏览数据
  2152. userViewMobileTotalList, _ := models.GetCountUserViewHistoryByMobiles(mobileStr)
  2153. for _, userViewMobile := range userViewMobileTotalList {
  2154. userViewMobileTotalMap[userViewMobile.Mobile] = userViewMobile
  2155. }
  2156. //每日点评手机号数据
  2157. userReportViewMobileTotalList, _ := models.GetReportViewMaxTimeByMobiles(mobileStr)
  2158. for _, userReportViewMobile := range userReportViewMobileTotalList {
  2159. userReportViewMobileTotalMap[userReportViewMobile.Mobile] = userReportViewMobile
  2160. }
  2161. }
  2162. //邮箱
  2163. userViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  2164. userReportViewEmailTotalMap := make(map[string]*models.ReportViewEmailRecord)
  2165. if len(emailsSlice) > 0 {
  2166. //用户浏览数据
  2167. userViewEmailTotalList, _ := models.GetCountUserViewHistoryByEmails(emailStr)
  2168. for _, userViewEmail := range userViewEmailTotalList {
  2169. userViewEmailTotalMap[userViewEmail.Email] = userViewEmail
  2170. }
  2171. //每日点评手机号数据
  2172. userReportViewEmailTotalList, _ := models.GetReportViewMaxTimeByEmails(emailStr)
  2173. for _, userReportViewEmail := range userReportViewEmailTotalList {
  2174. userReportViewEmailTotalMap[userReportViewEmail.Email] = userReportViewEmail
  2175. }
  2176. }
  2177. for i := 0; i < listLen; i++ {
  2178. item := list[i]
  2179. //item, err := models.GetReportViewMaxTime(list[i].UserId)
  2180. //if err != nil {
  2181. // br.Msg = "获取失败"
  2182. // br.ErrMsg = "获取GetReportViewMaxTime失败,Err:" + err.Error()
  2183. // return
  2184. //}
  2185. //list[i].ViewTotal += item.ViewTotal
  2186. //手机号用户累计浏览次数
  2187. if userView, ok := userViewMobileTotalMap[item.Mobile]; ok {
  2188. list[i].ViewTotal += userView.Total
  2189. if userView.CreatedTime.After(list[i].LastViewTime) {
  2190. list[i].LastViewTime = userView.CreatedTime
  2191. list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  2192. }
  2193. }
  2194. //邮箱用户累计浏览次数
  2195. if userView, ok := userViewEmailTotalMap[item.Email]; ok {
  2196. //总共浏览的报表数据需要调整为(手机号用户浏览晨报数+邮箱号用户浏览晨报数)
  2197. list[i].ViewTotal += userView.Total
  2198. if userView.CreatedTime.After(list[i].LastViewTime) {
  2199. list[i].LastViewTime = userView.CreatedTime
  2200. list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  2201. }
  2202. }
  2203. //如果存在手机号用户日评数据,那么需要做处理
  2204. if reportView, ok := userReportViewMobileTotalMap[item.Mobile]; ok {
  2205. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  2206. if reportView.LastViewTime.After(list[i].LastViewTime) {
  2207. list[i].LastViewTime = reportView.LastViewTime
  2208. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  2209. }
  2210. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  2211. list[i].ViewTotal += reportView.ViewTotal
  2212. }
  2213. //如果存在手机号用户日评数据,那么需要做处理
  2214. if reportView, ok := userReportViewEmailTotalMap[item.Email]; ok {
  2215. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  2216. if reportView.LastViewTime.After(list[i].LastViewTime) {
  2217. list[i].LastViewTime = reportView.LastViewTime
  2218. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  2219. }
  2220. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  2221. list[i].ViewTotal += reportView.ViewTotal
  2222. }
  2223. }
  2224. page := paging.GetPaging(currentIndex, pageSize, total)
  2225. resp := new(models.PotentialUserListResp)
  2226. resp.List = list
  2227. resp.Paging = page
  2228. br.Ret = 200
  2229. br.Success = true
  2230. br.Msg = "获取成功"
  2231. br.Data = resp
  2232. }
  2233. // @Title 标记潜在用户
  2234. // @Description 标记潜在用户接口
  2235. // @Param request body models.PotentialUserDealReq true "type json string"
  2236. // @Success Ret=200 标记成功
  2237. // @router /potential/user/deal [post]
  2238. func (this *CompanyController) PotentialUserDeal() {
  2239. br := new(models.BaseResponse).Init()
  2240. defer func() {
  2241. this.Data["json"] = br
  2242. this.ServeJSON()
  2243. }()
  2244. var req models.PotentialUserDealReq
  2245. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2246. if err != nil {
  2247. br.Msg = "参数解析异常!"
  2248. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2249. return
  2250. }
  2251. sysUser := this.SysUser
  2252. if sysUser == nil {
  2253. br.Msg = "请登录"
  2254. br.ErrMsg = "请登录,SysUser Is Empty"
  2255. br.Ret = 408
  2256. return
  2257. }
  2258. // 标记处理
  2259. err = services.DealPotentialUser(req.UserId, sysUser.AdminId)
  2260. if err != nil {
  2261. br.Msg = "标记失败"
  2262. br.ErrMsg = "标记失败,Err:" + err.Error()
  2263. return
  2264. }
  2265. br.Ret = 200
  2266. br.Success = true
  2267. br.Msg = "标记成功"
  2268. }
  2269. // @Title 删除潜在用户
  2270. // @Description 删除潜在用户接口
  2271. // @Param request body models.PotentialUserDeleteReq true "type json string"
  2272. // @Success Ret=200 删除成功
  2273. // @router /potential/user/delete [post]
  2274. func (this *CompanyController) PotentialUserDelete() {
  2275. br := new(models.BaseResponse).Init()
  2276. defer func() {
  2277. this.Data["json"] = br
  2278. this.ServeJSON()
  2279. }()
  2280. var req models.PotentialUserDeleteReq
  2281. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2282. if err != nil {
  2283. br.Msg = "参数解析异常!"
  2284. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2285. return
  2286. }
  2287. sysUser := this.SysUser
  2288. if sysUser == nil {
  2289. br.Msg = "请登录"
  2290. br.ErrMsg = "请登录,SysUser Is Empty"
  2291. br.Ret = 408
  2292. return
  2293. }
  2294. //删除用户
  2295. err = services.DeleteWxUser(req.UserId)
  2296. if err != nil {
  2297. br.Msg = "删除失败"
  2298. br.ErrMsg = "删除失败,Err:" + err.Error()
  2299. return
  2300. }
  2301. br.Ret = 200
  2302. br.Success = true
  2303. br.Msg = "删除成功"
  2304. }
  2305. // @Title 移动潜在用户
  2306. // @Description 移动潜在用户接口
  2307. // @Param request body models.PotentialUserMoveReq true "type json string"
  2308. // @Success Ret=200 移动成功
  2309. // @router /potential/user/move [post]
  2310. func (this *CompanyController) PotentialUserMove() {
  2311. br := new(models.BaseResponse).Init()
  2312. defer func() {
  2313. this.Data["json"] = br
  2314. this.ServeJSON()
  2315. }()
  2316. sysUser := this.SysUser
  2317. if sysUser == nil {
  2318. br.Msg = "请登录"
  2319. br.ErrMsg = "请登录,SysUser Is Empty"
  2320. br.Ret = 408
  2321. return
  2322. }
  2323. var req models.PotentialUserMoveReq
  2324. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2325. if err != nil {
  2326. br.Msg = "参数解析异常!"
  2327. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2328. return
  2329. }
  2330. if req.UserId <= 0 {
  2331. br.Msg = "参数错误"
  2332. br.ErrMsg = "客户id错误"
  2333. return
  2334. }
  2335. //如果移动客户编号小于等于0,那么移动到潜在客户
  2336. if req.CompanyId <= 0 {
  2337. req.CompanyId = 1
  2338. }
  2339. wxUser, err := models.GetWxUserByUserId(req.UserId)
  2340. if err != nil {
  2341. br.Msg = "移动失败"
  2342. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  2343. return
  2344. }
  2345. oldWxUser := *wxUser
  2346. if req.Mobile == "" {
  2347. req.Mobile = wxUser.Mobile
  2348. }
  2349. if req.Email == "" {
  2350. req.Email = wxUser.Email
  2351. }
  2352. if req.Mobile == "" && req.Email == "" {
  2353. br.Msg = "手机号和邮箱不能同时为空"
  2354. return
  2355. }
  2356. //校验手机号
  2357. if req.Mobile != "" && wxUser.Mobile != req.Mobile {
  2358. count, err := models.CheckUserMobileIsRegister(req.UserId, req.Mobile)
  2359. if err != nil {
  2360. br.Msg = "校验数据失败"
  2361. br.ErrMsg = "校验数据失败,Err:" + err.Error()
  2362. return
  2363. }
  2364. if count > 0 {
  2365. br.Msg = "该手机号已经注册过用户"
  2366. return
  2367. }
  2368. }
  2369. //校验邮箱
  2370. if req.Email != "" && wxUser.Email != req.Email {
  2371. if !utils.ValidateEmailFormatat(req.Email) {
  2372. br.Msg = "请输入正确的邮箱(包含@)"
  2373. return
  2374. }
  2375. count, err := models.CheckUserEmailIsRegister(req.UserId, req.Email)
  2376. if err != nil {
  2377. br.Msg = "校验数据失败"
  2378. br.ErrMsg = "校验数据失败,Err:" + err.Error()
  2379. return
  2380. }
  2381. if count > 0 {
  2382. br.Msg = "该邮箱已经注册过用户"
  2383. br.IsSendEmail = false
  2384. return
  2385. }
  2386. }
  2387. updateCol := make([]string, 0)
  2388. if wxUser.OutboundMobile == "" || wxUser.Mobile == wxUser.OutboundMobile {
  2389. wxUser.OutboundMobile = req.Mobile
  2390. wxUser.OutboundCountryCode = req.CountryCode
  2391. updateCol = append(updateCol, "OutboundMobile", "OutboundCountryCode")
  2392. }
  2393. if req.Email != "" {
  2394. wxUser.Email = req.Email
  2395. updateCol = append(updateCol, "Email")
  2396. }
  2397. if req.RealName != "" {
  2398. wxUser.RealName = req.RealName
  2399. updateCol = append(updateCol, "RealName")
  2400. }
  2401. if req.Mobile != "" {
  2402. wxUser.Mobile = req.Mobile
  2403. updateCol = append(updateCol, "Mobile")
  2404. }
  2405. if req.CountryCode != "" {
  2406. wxUser.CountryCode = req.CountryCode
  2407. updateCol = append(updateCol, "CountryCode")
  2408. }
  2409. if req.Position != "" {
  2410. wxUser.Position = req.Position
  2411. updateCol = append(updateCol, "Position")
  2412. }
  2413. if wxUser.DepartmentName == "" && req.DepartmentName != "" {
  2414. wxUser.DepartmentName = req.DepartmentName
  2415. updateCol = append(updateCol, "DepartmentName")
  2416. }
  2417. wxUser.Sex = req.Sex
  2418. wxUser.IsMaker = req.IsMaker
  2419. updateCol = append(updateCol, "Sex", "IsMaker")
  2420. //名片匹配
  2421. if req.BusinessCardUrl != "" {
  2422. //if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  2423. // 获取图片识别手机号的配置
  2424. crmConfig2, err := company.GetConfigDetailByCode("card_recognition_tel")
  2425. if err != nil {
  2426. br.Msg = "获取配置失败"
  2427. br.ErrMsg = "获取配置失败"
  2428. br.IsSendEmail = false
  2429. return
  2430. }
  2431. if crmConfig2.ConfigValue == "true" {
  2432. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  2433. if err != nil {
  2434. br.Msg = "名片识别失败"
  2435. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  2436. return
  2437. }
  2438. isFlag := false
  2439. if len(card.WordsResult.MOBILE) > 0 {
  2440. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  2441. //if req.Mobile != "" {
  2442. // if strings.Contains(mobileStr, req.Mobile) || mobileStr == "" {
  2443. // isFlag = true
  2444. // } else {
  2445. // isFlag = false
  2446. // }
  2447. //}
  2448. if req.Mobile != "" && strings.Contains(mobileStr, req.Mobile) {
  2449. isFlag = true
  2450. }
  2451. }
  2452. if !isFlag {
  2453. //阿里云识别
  2454. //if utils.RunMode == "release" {
  2455. aliyunResult, errMsg, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
  2456. if err != nil {
  2457. br.Msg = "识别失败"
  2458. br.ErrMsg = "识别失败,Err:" + err.Error()
  2459. return
  2460. }
  2461. if !aliyunResult.Success {
  2462. br.Msg = "识别失败"
  2463. br.ErrMsg = "识别失败"
  2464. return
  2465. }
  2466. if errMsg != "" {
  2467. br.Msg = errMsg
  2468. return
  2469. }
  2470. if len(aliyunResult.TelCell) > 0 {
  2471. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  2472. if req.Mobile != "" {
  2473. if strings.Contains(mobileStr, req.Mobile) {
  2474. isFlag = true
  2475. } else {
  2476. isFlag = false
  2477. }
  2478. }
  2479. } else {
  2480. isFlag = true
  2481. }
  2482. //}
  2483. }
  2484. if !isFlag && crmConfig2.ConfigValue == "true" {
  2485. br.Msg = "名片信息与联系人手机号不符"
  2486. return
  2487. }
  2488. }
  2489. wxUser.BusinessCardUrl = req.BusinessCardUrl
  2490. updateCol = append(updateCol, "BusinessCardUrl")
  2491. }
  2492. productId := services.GetProductId(sysUser.RoleTypeCode)
  2493. companyProductList, _ := getSysCompanyProductList(productId, req.CompanyId)
  2494. //联系人与销售的关系
  2495. userSellerRelationList, err := models.GetUserSellerRelationList(int(wxUser.UserId))
  2496. if err != nil {
  2497. return
  2498. }
  2499. userSellerRelationInfo, _ := json.Marshal(userSellerRelationList)
  2500. err = services.MovePotentialUser(req.UserId, req.CompanyId, req.RealName, req.Mobile, req.Email, req.Remark, companyProductList, wxUser, updateCol)
  2501. //err = models.MovePotentialUser(req.UserId, req.CompanyId, req.RealName, req.Mobile, req.Email, req.Remark)
  2502. if err != nil {
  2503. br.Msg = "移动失败"
  2504. br.ErrMsg = "移动失败,Err:" + err.Error()
  2505. return
  2506. }
  2507. //更新客户最近阅读次数以及最近阅读时间
  2508. go services.ModifyCompanyProductLastViewData([]int{req.CompanyId})
  2509. //联系人信息
  2510. originalUserInfo, _ := json.Marshal(oldWxUser)
  2511. userInfo, _ := json.Marshal(wxUser)
  2512. go services.AddWxUserOpLog(company.WxUserOpLog{
  2513. LogType: "move",
  2514. UserId: int(wxUser.UserId),
  2515. CompanyId: wxUser.CompanyId,
  2516. Mobile: wxUser.Mobile,
  2517. Email: wxUser.Email,
  2518. OriginalUserInfo: string(originalUserInfo),
  2519. UserInfo: string(userInfo),
  2520. OriginalUserSellerInfo: string(userSellerRelationInfo),
  2521. UserSellerInfo: "",
  2522. OpUserId: sysUser.AdminId,
  2523. OpUserName: sysUser.RealName,
  2524. CreateTime: time.Now(),
  2525. })
  2526. br.Ret = 200
  2527. br.Success = true
  2528. br.Msg = "移动成功"
  2529. }
  2530. // @Title 移动联系人
  2531. // @Description 移动联系接口,将联系人从自己名下移动到另一个客户销售名下
  2532. // @Param request body models.UserMoveReq true "type json string"
  2533. // @Success Ret=200 移动成功
  2534. // @router /user/move [post]
  2535. func (this *CompanyController) UserMove() {
  2536. br := new(models.BaseResponse).Init()
  2537. defer func() {
  2538. this.Data["json"] = br
  2539. this.ServeJSON()
  2540. }()
  2541. sysUser := this.SysUser
  2542. if sysUser == nil {
  2543. br.Msg = "请登录"
  2544. br.ErrMsg = "请登录,SysUser Is Empty"
  2545. br.Ret = 408
  2546. return
  2547. }
  2548. var req models.UserMoveReq
  2549. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2550. if err != nil {
  2551. br.Msg = "参数解析异常!"
  2552. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2553. return
  2554. }
  2555. if req.UserId <= 0 {
  2556. br.Msg = "参数错误"
  2557. br.ErrMsg = "联系人id错误"
  2558. return
  2559. }
  2560. if req.CompanyId <= 0 {
  2561. br.Msg = "参数错误"
  2562. br.ErrMsg = "客户id错误"
  2563. return
  2564. }
  2565. if req.SellerId < 0 {
  2566. br.Msg = "参数错误"
  2567. br.ErrMsg = "销售id错误"
  2568. return
  2569. }
  2570. wxUser, err := models.GetWxUserByUserId(req.UserId)
  2571. if err != nil {
  2572. br.Msg = "移动失败"
  2573. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  2574. return
  2575. }
  2576. oldWxUser := *wxUser
  2577. if wxUser.CompanyId == req.CompanyId {
  2578. br.Msg = "移动失败"
  2579. br.ErrMsg = "该联系人属于该客户"
  2580. return
  2581. }
  2582. //产品id
  2583. productId := services.GetProductId(sysUser.RoleTypeCode)
  2584. //操作权限校验
  2585. errMsg, err := checkOpCompanyUserByCompanyId(wxUser.CompanyId, productId, sysUser.AdminId, sysUser.RoleTypeCode)
  2586. if err != nil {
  2587. br.Msg = "移动失败!" + errMsg
  2588. br.ErrMsg = "移动失败,Err:" + err.Error()
  2589. return
  2590. }
  2591. //联系人与销售的关系
  2592. userSellerRelationList, err := models.GetUserSellerRelationList(int(wxUser.UserId))
  2593. if err != nil {
  2594. return
  2595. }
  2596. userSellerRelationInfo, _ := json.Marshal(userSellerRelationList)
  2597. err = services.MoveUser(req.UserId, req.CompanyId, req.SellerId, wxUser, sysUser.AdminId)
  2598. //err = models.MovePotentialUser(req.UserId, req.CompanyId, req.RealName, req.Mobile, req.Email, req.Remark)
  2599. if err != nil {
  2600. br.Msg = "移动失败"
  2601. br.ErrMsg = "移动失败,Err:" + err.Error()
  2602. return
  2603. }
  2604. //更新客户最近阅读次数以及最近阅读时间
  2605. go services.ModifyCompanyProductLastViewData([]int{wxUser.CompanyId, req.CompanyId})
  2606. //联系人信息
  2607. originalUserInfo, _ := json.Marshal(oldWxUser)
  2608. userInfo, _ := json.Marshal(wxUser)
  2609. go services.AddWxUserOpLog(company.WxUserOpLog{
  2610. LogType: "move",
  2611. UserId: int(wxUser.UserId),
  2612. CompanyId: wxUser.CompanyId,
  2613. Mobile: wxUser.Mobile,
  2614. Email: wxUser.Email,
  2615. OriginalUserInfo: string(originalUserInfo),
  2616. UserInfo: string(userInfo),
  2617. OriginalUserSellerInfo: string(userSellerRelationInfo),
  2618. UserSellerInfo: "",
  2619. OpUserId: sysUser.AdminId,
  2620. OpUserName: sysUser.RealName,
  2621. CreateTime: time.Now(),
  2622. })
  2623. br.Ret = 200
  2624. br.Success = true
  2625. br.Msg = "移动成功"
  2626. br.IsAddLog = true
  2627. }
  2628. // @Title 搜索客户
  2629. // @Description 搜索客户接口
  2630. // @Param KeyWord query string true "搜索关键词"
  2631. // @Success 200 {object} company.CompanySearchResp
  2632. // @router /potential/company/search [get]
  2633. func (this *CompanyUserController) CompanySearch() {
  2634. br := new(models.BaseResponse).Init()
  2635. defer func() {
  2636. this.Data["json"] = br
  2637. this.ServeJSON()
  2638. }()
  2639. sysUser := this.SysUser
  2640. if sysUser == nil {
  2641. br.Msg = "请登录"
  2642. br.ErrMsg = "请登录,SysUser Is Empty"
  2643. br.Ret = 408
  2644. return
  2645. }
  2646. var err error
  2647. keyWord := this.GetString("KeyWord")
  2648. item := make([]*company.CompanyItem, 0)
  2649. if keyWord != "" {
  2650. item, err = company.GetCompanyBySearch(keyWord)
  2651. if err != nil && err.Error() != utils.ErrNoRow() {
  2652. br.Msg = "获取失败"
  2653. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  2654. return
  2655. }
  2656. }
  2657. resp := new(company.CompanySearchResp)
  2658. resp.List = item
  2659. br.Ret = 200
  2660. br.Success = true
  2661. br.Msg = "获取成功"
  2662. br.Data = resp
  2663. }
  2664. // @Title 企查查模糊查询客户接口
  2665. // @Description 企查查模糊查询客户
  2666. // @Param KeyWord query string true "搜索关键词"
  2667. // @Success 200 {object} services.Result
  2668. // @router /potential/company/qccSearch [get]
  2669. func (this *CompanyUserController) CompanyQCCSearch() {
  2670. br := new(models.BaseResponse).Init()
  2671. defer func() {
  2672. this.Data["json"] = br
  2673. this.ServeJSON()
  2674. }()
  2675. sysUser := this.SysUser
  2676. if sysUser == nil {
  2677. br.Msg = "请登录"
  2678. br.ErrMsg = "请登录,SysUser Is Empty"
  2679. br.Ret = 408
  2680. return
  2681. }
  2682. var err error
  2683. var results []services.Result
  2684. keyWord := this.GetString("KeyWord")
  2685. if keyWord != "" {
  2686. results, err = services.QCCFuzzySearch(keyWord)
  2687. if err != nil {
  2688. br.Data = results
  2689. br.Msg = "获取数据失败," + err.Error()
  2690. br.Ret = 200
  2691. br.Success = true
  2692. return
  2693. }
  2694. }
  2695. br.Data = results
  2696. br.Ret = 200
  2697. br.Success = true
  2698. br.Msg = "获取成功"
  2699. }
  2700. // @Title 检测客户名称或社会信用码重复
  2701. // @Description 检测客户名称或社会信用码重复接口(新增/编辑客户页面使用)
  2702. // @Param CompanyName query string true "客户名称"
  2703. // @Param CreditCode query string true "社会统一信用码"
  2704. // @Success 200 {object} company.CheckCompanyInfoRepeatResp
  2705. // @router /check/RepeatCompanyInfo [get]
  2706. func (this *CompanyController) CheckCompanyInfoRepeat() {
  2707. br := new(models.BaseResponse).Init()
  2708. defer func() {
  2709. this.Data["json"] = br
  2710. this.ServeJSON()
  2711. }()
  2712. sysUser := this.SysUser
  2713. if sysUser == nil {
  2714. br.Msg = "请登录"
  2715. br.ErrMsg = "请登录,SysUser Is Empty"
  2716. br.Ret = 408
  2717. return
  2718. }
  2719. roleTypeCode := sysUser.RoleTypeCode
  2720. productId := services.GetProductId(roleTypeCode)
  2721. companyName := this.GetString("CompanyName")
  2722. creditCode := this.GetString("CreditCode")
  2723. var status, comanyId, repeatStatus int
  2724. var productName string
  2725. if productId == 0 {
  2726. productId = 1
  2727. checkProductId := 2
  2728. if creditCode != "" {
  2729. codeItem, err := company.CheckCompanyProductByCreditCode(creditCode, productId)
  2730. if err != nil && err.Error() != utils.ErrNoRow() {
  2731. br.Msg = "校验社会信用码是否存在失败"
  2732. br.ErrMsg = "校验社会信用码是否存在失败,Err:" + err.Error()
  2733. return
  2734. }
  2735. //存在ficc权限
  2736. if codeItem != nil && codeItem.CompanyId > 0 {
  2737. repeatStatus = 1
  2738. goto Loop
  2739. }
  2740. codeCheckItem, err := company.CheckCompanyProductByCreditCode(creditCode, checkProductId)
  2741. if err != nil && err.Error() != utils.ErrNoRow() {
  2742. br.Msg = "校验社会信用码是否存在失败"
  2743. br.ErrMsg = "校验社会信用码是否存在失败,Err:" + err.Error() + " productId:" + strconv.Itoa(productId) + ";checkProductId:" + strconv.Itoa(checkProductId)
  2744. return
  2745. }
  2746. if codeCheckItem != nil && codeCheckItem.CompanyId > 0 {
  2747. status = 1
  2748. repeatStatus = 1
  2749. productName = codeCheckItem.ProductName
  2750. comanyId = codeCheckItem.CompanyId
  2751. goto Loop
  2752. }
  2753. }
  2754. if companyName != "" {
  2755. nameItem, err := company.CheckCompanyProductByName(companyName, productId)
  2756. if err != nil && err.Error() != utils.ErrNoRow() {
  2757. br.Msg = "校验客户名称是否存在失败"
  2758. br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error()
  2759. return
  2760. }
  2761. if nameItem != nil && nameItem.CompanyId > 0 {
  2762. repeatStatus = 2
  2763. goto Loop
  2764. }
  2765. nameCheckItem, err := company.CheckCompanyProductByName(companyName, checkProductId)
  2766. if err != nil && err.Error() != utils.ErrNoRow() {
  2767. br.Msg = "校验客户名称是否存在失败"
  2768. br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error() + " productId:" + strconv.Itoa(productId) + ";checkProductId:" + strconv.Itoa(checkProductId)
  2769. return
  2770. }
  2771. if nameCheckItem != nil && nameCheckItem.CompanyId > 0 {
  2772. status = 1
  2773. repeatStatus = 2
  2774. productName = nameCheckItem.ProductName
  2775. comanyId = nameCheckItem.CompanyId
  2776. goto Loop
  2777. }
  2778. }
  2779. productId = 2
  2780. checkProductId = 1
  2781. if creditCode != "" {
  2782. codeItem, err := company.CheckCompanyProductByCreditCode(creditCode, productId)
  2783. if err != nil && err.Error() != utils.ErrNoRow() {
  2784. br.Msg = "校验社会信用码是否存在失败"
  2785. br.ErrMsg = "校验社会信用码是否存在失败,Err:" + err.Error()
  2786. return
  2787. }
  2788. if codeItem != nil && codeItem.CompanyId > 0 {
  2789. repeatStatus = 1
  2790. goto Loop
  2791. }
  2792. codeCheckItem, err := company.CheckCompanyProductByCreditCode(creditCode, checkProductId)
  2793. if err != nil && err.Error() != utils.ErrNoRow() {
  2794. br.Msg = "校验社会信用码是否存在失败"
  2795. br.ErrMsg = "校验社会信用码是否存在失败,Err:" + err.Error() + " productId:" + strconv.Itoa(productId) + ";checkProductId:" + strconv.Itoa(checkProductId)
  2796. return
  2797. }
  2798. if codeCheckItem != nil && codeCheckItem.CompanyId > 0 {
  2799. status = 1
  2800. repeatStatus = 1
  2801. productName = codeCheckItem.ProductName
  2802. comanyId = codeCheckItem.CompanyId
  2803. goto Loop
  2804. }
  2805. }
  2806. if companyName != "" {
  2807. nameItem, err := company.CheckCompanyProductByName(companyName, productId)
  2808. if err != nil && err.Error() != utils.ErrNoRow() {
  2809. br.Msg = "校验客户名称是否存在失败"
  2810. br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error()
  2811. return
  2812. }
  2813. if nameItem != nil && nameItem.CompanyId > 0 {
  2814. repeatStatus = 2
  2815. goto Loop
  2816. }
  2817. nameCheckItem, err := company.CheckCompanyProductByName(companyName, checkProductId)
  2818. if err != nil && err.Error() != utils.ErrNoRow() {
  2819. br.Msg = "校验客户名称是否存在失败"
  2820. br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error() + " productId:" + strconv.Itoa(productId) + ";checkProductId:" + strconv.Itoa(checkProductId)
  2821. return
  2822. }
  2823. if nameCheckItem != nil && nameCheckItem.CompanyId > 0 {
  2824. status = 1
  2825. repeatStatus = 2
  2826. productName = nameCheckItem.ProductName
  2827. comanyId = nameCheckItem.CompanyId
  2828. goto Loop
  2829. }
  2830. }
  2831. } else {
  2832. checkProductId := 0
  2833. if productId == 1 {
  2834. checkProductId = 2
  2835. } else if productId == 2 {
  2836. checkProductId = 1
  2837. }
  2838. if creditCode != "" {
  2839. codeItem, err := company.CheckCompanyProductByCreditCode(creditCode, productId)
  2840. if err != nil && err.Error() != utils.ErrNoRow() {
  2841. br.Msg = "校验社会信用码是否存在失败"
  2842. br.ErrMsg = "校验社会信用码是否存在失败,Err:" + err.Error()
  2843. return
  2844. }
  2845. if codeItem != nil && codeItem.CompanyId > 0 {
  2846. repeatStatus = 1
  2847. goto Loop
  2848. }
  2849. codeCheckItem, err := company.CheckCompanyProductByCreditCode(creditCode, checkProductId)
  2850. if err != nil && err.Error() != utils.ErrNoRow() {
  2851. br.Msg = "校验社会信用码是否存在失败"
  2852. br.ErrMsg = "校验社会信用码是否存在失败,Err:" + err.Error() + " productId:" + strconv.Itoa(productId) + ";checkProductId:" + strconv.Itoa(checkProductId)
  2853. return
  2854. }
  2855. if codeCheckItem != nil && codeCheckItem.CompanyId > 0 {
  2856. status = 1
  2857. repeatStatus = 1
  2858. productName = codeCheckItem.ProductName
  2859. comanyId = codeCheckItem.CompanyId
  2860. goto Loop
  2861. }
  2862. }
  2863. if companyName != "" {
  2864. nameItem, err := company.CheckCompanyProductByName(companyName, productId)
  2865. if err != nil && err.Error() != utils.ErrNoRow() {
  2866. br.Msg = "校验客户名称是否存在失败"
  2867. br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error()
  2868. return
  2869. }
  2870. if nameItem != nil && nameItem.ProductName != "" {
  2871. repeatStatus = 2
  2872. goto Loop
  2873. }
  2874. nameCheckoutItem, err := company.CheckCompanyProductByName(companyName, checkProductId)
  2875. if err != nil && err.Error() != utils.ErrNoRow() {
  2876. br.Msg = "校验客户名称是否存在失败"
  2877. br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error() + " productId:" + strconv.Itoa(productId) + ";checkProductId:" + strconv.Itoa(checkProductId)
  2878. return
  2879. }
  2880. if nameCheckoutItem != nil && nameCheckoutItem.CompanyId > 0 {
  2881. status = 1
  2882. repeatStatus = 2
  2883. productName = nameCheckoutItem.ProductName
  2884. comanyId = nameCheckoutItem.CompanyId
  2885. goto Loop
  2886. }
  2887. }
  2888. }
  2889. Loop:
  2890. resp := new(company.CheckCompanyInfoRepeatResp)
  2891. resp.Status = status
  2892. resp.RepeatStatus = repeatStatus
  2893. resp.ProductName = productName
  2894. resp.CompanyId = comanyId
  2895. br.Ret = 200
  2896. br.Success = true
  2897. br.Msg = "获取成功"
  2898. br.Data = resp
  2899. }
  2900. // ViewReportList
  2901. // @Title 联系人阅读报告记录
  2902. // @Description 联系人阅读报告记录
  2903. // @Param UserId query int true "用户id"
  2904. // @Param TxtType query int true "类型0全部,1权益,2ficc"
  2905. // @Param PageSize query int true "每页数据条数"
  2906. // @Param LastViewTime query string true "最近一次的阅读时间,没有的话,那就是获取最新数据"
  2907. // @Success 200 {object} company.ViewReportListResp
  2908. // @router /view/report/list [get]
  2909. func (this *CompanyUserController) ViewReportList() {
  2910. br := new(models.BaseResponse).Init()
  2911. defer func() {
  2912. this.Data["json"] = br
  2913. this.ServeJSON()
  2914. }()
  2915. sysUser := this.SysUser
  2916. if sysUser == nil {
  2917. br.Msg = "请登录"
  2918. br.ErrMsg = "请登录,SysUser Is Empty"
  2919. br.Ret = 408
  2920. return
  2921. }
  2922. userId, _ := this.GetInt("UserId")
  2923. txtType, _ := this.GetInt("TxtType")
  2924. pageSize, _ := this.GetInt("PageSize")
  2925. lastViewTime := this.GetString("LastViewTime")
  2926. if userId <= 0 {
  2927. br.Msg = "参数错误"
  2928. return
  2929. }
  2930. // 默认20条数据
  2931. if pageSize <= 0 {
  2932. pageSize = utils.PageSize20
  2933. }
  2934. if userId > 0 {
  2935. }
  2936. item, err := models.GetWxUserByUserId(userId)
  2937. if err != nil {
  2938. br.Msg = "获取失败"
  2939. br.Msg = "获取联系人信息失败,Err:" + err.Error()
  2940. return
  2941. }
  2942. var lastViewTimeT time.Time
  2943. if lastViewTime != `` {
  2944. lastViewTimeT, err = time.ParseInLocation(utils.FormatDateTime, lastViewTime, time.Local)
  2945. if err != nil {
  2946. br.Msg = "获取失败"
  2947. br.Msg = "最近一次阅读时间转换异常,Err:" + err.Error()
  2948. return
  2949. }
  2950. }
  2951. list := make([]*company.ViewReportList, 0)
  2952. recordList := make([]*models.UserReportViewRecord, 0)
  2953. obj := models.UserReportViewRecord{}
  2954. var baseCondition string // 基础条件
  2955. var basePars []interface{}
  2956. if item.Mobile != "" {
  2957. baseCondition = ` AND mobile = ? `
  2958. basePars = append(basePars, item.Mobile)
  2959. } else if item.Email == "" {
  2960. baseCondition = ` AND email = ? `
  2961. basePars = append(basePars, item.Email)
  2962. }
  2963. // 阅读记录汇总数
  2964. total := item.FiccViewTotal + item.RaiViewTotal
  2965. switch txtType {
  2966. case 1:
  2967. baseCondition += ` AND source = ? `
  2968. basePars = append(basePars, 4)
  2969. total = item.RaiViewTotal
  2970. if lastViewTimeT.IsZero() {
  2971. if item.RaiLastViewTime != `` && item.RaiLastViewTime != `0000-00-00 00:00:00` {
  2972. lastViewTimeT, _ = time.ParseInLocation(utils.FormatDateTime, item.RaiLastViewTime, time.Local)
  2973. } else {
  2974. lastViewTimeT = time.Now()
  2975. }
  2976. lastViewTime = lastViewTimeT.Format(utils.FormatDateTime)
  2977. }
  2978. // 权益
  2979. case 2: // ficc
  2980. baseCondition += ` AND source in (?,?,?) `
  2981. basePars = append(basePars, 1, 2, 3)
  2982. total = item.FiccViewTotal
  2983. if lastViewTimeT.IsZero() {
  2984. if item.FiccLastViewTime != `` && item.FiccLastViewTime != `0000-00-00 00:00:00` {
  2985. lastViewTimeT, _ = time.ParseInLocation(utils.FormatDateTime, item.FiccLastViewTime, time.Local)
  2986. } else {
  2987. lastViewTimeT = time.Now()
  2988. }
  2989. lastViewTime = lastViewTimeT.Format(utils.FormatDateTime)
  2990. }
  2991. default:
  2992. if lastViewTimeT.IsZero() {
  2993. var ficcViewTime, raiViewTime time.Time
  2994. if item.FiccLastViewTime != `` && item.FiccLastViewTime != `0000-00-00 00:00:00` {
  2995. ficcViewTime, _ = time.ParseInLocation(utils.FormatDateTime, item.FiccLastViewTime, time.Local)
  2996. }
  2997. if item.RaiLastViewTime != `` && item.RaiLastViewTime != `0000-00-00 00:00:00` {
  2998. raiViewTime, _ = time.ParseInLocation(utils.FormatDateTime, item.RaiLastViewTime, time.Local)
  2999. }
  3000. // 如果两个都没有值的话,那么就是当前时间
  3001. if ficcViewTime.IsZero() && raiViewTime.IsZero() {
  3002. lastViewTimeT = time.Now()
  3003. } else if !ficcViewTime.IsZero() && !raiViewTime.IsZero() {
  3004. if ficcViewTime.After(raiViewTime) {
  3005. lastViewTimeT = ficcViewTime
  3006. } else {
  3007. lastViewTimeT = raiViewTime
  3008. }
  3009. } else if !ficcViewTime.IsZero() {
  3010. lastViewTimeT = ficcViewTime
  3011. } else {
  3012. lastViewTimeT = raiViewTime
  3013. }
  3014. // 默认要把最新的记录列出来,所以往后加1s
  3015. lastViewTime = lastViewTimeT.Add(1 * time.Second).Format(utils.FormatDateTime)
  3016. }
  3017. }
  3018. // 开始实际的查询
  3019. {
  3020. condition := baseCondition
  3021. pars := basePars
  3022. condition += `AND create_time < ?`
  3023. pars = append(pars, lastViewTime)
  3024. total, items, err := obj.GetViewReportList(lastViewTimeT.Year(), condition, pars, pageSize)
  3025. if err != nil {
  3026. br.Msg = "获取失败"
  3027. br.Msg = "获取失败,Err:" + err.Error()
  3028. return
  3029. }
  3030. recordList = append(recordList, items...)
  3031. // 查询去年的表
  3032. if total < int64(pageSize) {
  3033. secondCondition := baseCondition
  3034. secondPars := basePars
  3035. secondCondition += `AND create_time < ?`
  3036. secondPars = append(secondPars, lastViewTime)
  3037. _, items, err = obj.GetViewReportList(lastViewTimeT.Year(), secondCondition, secondPars, int(total)-pageSize)
  3038. if err != nil {
  3039. br.Msg = "获取失败"
  3040. br.Msg = "获取失败,Err:" + err.Error()
  3041. return
  3042. }
  3043. recordList = append(recordList, items...)
  3044. }
  3045. }
  3046. viewReportMap := make(map[string]company.ViewReportList) // 阅读记录map
  3047. sourceReportIdListMap := make(map[int8][]int)
  3048. for _, v := range recordList {
  3049. tmpList, ok := sourceReportIdListMap[v.Source]
  3050. if !ok {
  3051. tmpList = make([]int, 0)
  3052. }
  3053. tmpList = append(tmpList, v.ReportId)
  3054. sourceReportIdListMap[v.Source] = tmpList
  3055. tmpTxtType := `ficc`
  3056. if v.Source == 4 {
  3057. tmpTxtType = `rights`
  3058. }
  3059. stopTime := `--`
  3060. if v.StopTime > 0 {
  3061. stopTime = fmt.Sprint(stopTime)
  3062. }
  3063. viewReportMap[fmt.Sprint(v.Source, "_", v.ReportId)] = company.ViewReportList{
  3064. ResearchReportName: "",
  3065. ReportType: "",
  3066. CreatedTime: v.CreateTime,
  3067. TxtType: tmpTxtType,
  3068. MatchTypeName: "--",
  3069. StopTime: stopTime,
  3070. }
  3071. }
  3072. // 获取报告详情
  3073. for source, reportIdList := range sourceReportIdListMap {
  3074. switch source {
  3075. case 1: // rddp报告
  3076. tmpReportList, err := models.GetReportByIdList(reportIdList)
  3077. if err != nil {
  3078. br.Msg = "获取失败"
  3079. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3080. return
  3081. }
  3082. for _, v := range tmpReportList {
  3083. tmpKey := fmt.Sprint(source, "_", v.Id)
  3084. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3085. tmpInfo.ResearchReportName = v.Title
  3086. tmpInfo.ReportType = `rddp`
  3087. tmpInfo.MatchTypeName = v.ClassifyNameFirst
  3088. viewReportMap[tmpKey] = tmpInfo
  3089. }
  3090. }
  3091. case 2: // weekly报告
  3092. tmpReportList, err := models.GetResearchReportListByIdList(reportIdList)
  3093. if err != nil {
  3094. br.Msg = "获取失败"
  3095. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3096. return
  3097. }
  3098. for _, v := range tmpReportList {
  3099. tmpKey := fmt.Sprint(source, "_", v.ResearchReportId)
  3100. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3101. tmpInfo.ResearchReportName = v.ResearchReportName
  3102. tmpInfo.ReportType = v.Type
  3103. viewReportMap[tmpKey] = tmpInfo
  3104. }
  3105. }
  3106. case 3: // weekly_report商品的报告(应该是作废了);
  3107. tmpReportList, err := models.GetChartPermissionByIdList(reportIdList)
  3108. if err != nil {
  3109. br.Msg = "获取失败"
  3110. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3111. return
  3112. }
  3113. for _, v := range tmpReportList {
  3114. tmpKey := fmt.Sprint(source, "_", v.ChartPermissionId)
  3115. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3116. tmpInfo.ResearchReportName = v.ChartPermissionName
  3117. tmpInfo.ReportType = `advisory`
  3118. tmpInfo.MatchTypeName = v.ClassifyName
  3119. viewReportMap[tmpKey] = tmpInfo
  3120. }
  3121. }
  3122. case 4: // 察研观向的报告
  3123. tmpReportList, err := cygx.GetArticleListByIdList(reportIdList)
  3124. if err != nil {
  3125. br.Msg = "获取失败"
  3126. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3127. return
  3128. }
  3129. for _, v := range tmpReportList {
  3130. tmpKey := fmt.Sprint(source, "_", v.ArticleId)
  3131. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3132. tmpInfo.ResearchReportName = v.Title
  3133. tmpInfo.ReportType = `cygx`
  3134. tmpInfo.MatchTypeName = v.MatchTypeName
  3135. viewReportMap[tmpKey] = tmpInfo
  3136. }
  3137. }
  3138. }
  3139. }
  3140. // 报告标题处理
  3141. for _, v := range recordList {
  3142. tmpKey := fmt.Sprint(v.Source, "_", v.ReportId)
  3143. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3144. list = append(list, &tmpInfo)
  3145. }
  3146. }
  3147. // 类型处理
  3148. for k, v := range list {
  3149. if v.ReportType == "day" {
  3150. list[k].MatchTypeName = "晨报"
  3151. } else if v.ReportType == "week" {
  3152. list[k].MatchTypeName = "周报"
  3153. } else if v.ReportType == "two_week" {
  3154. list[k].MatchTypeName = "双周报"
  3155. } else if v.ReportType == "month" {
  3156. list[k].MatchTypeName = "月报"
  3157. } else if v.ReportType == "rddp" {
  3158. //list[k].MatchTypeName = "日评"
  3159. createdTime := utils.StrTimeToTime(v.CreatedTime)
  3160. monthStr := createdTime.Format("01")
  3161. dayStr := strconv.Itoa(createdTime.Day())
  3162. if len(dayStr) == 1 {
  3163. dayStr = "0" + dayStr
  3164. }
  3165. list[k].ResearchReportName += "(" + monthStr + dayStr + ")"
  3166. }
  3167. if v.StopTime != "--" {
  3168. list[k].StopTime += "s"
  3169. }
  3170. }
  3171. resp := new(company.ViewReportListResp)
  3172. resp.List = list
  3173. resp.Total = total
  3174. br.Ret = 200
  3175. br.Success = true
  3176. br.Msg = "获取成功"
  3177. br.Data = resp
  3178. }
  3179. // @Title 导出潜在用户
  3180. // @Description 导出潜在用户接口
  3181. // @Param ApplyMethod query int true "申请方式,0:全部,1:未申请,2:已付费客户申请试用,3:非客户申请试用"
  3182. // @Param KeyWord query string true "搜索关键词"
  3183. // @Success Ret=200 导出成功
  3184. // @router /potential/user/export [get]
  3185. func (this *CompanyUserController) PotentialUserExport() {
  3186. br := new(models.BaseResponse).Init()
  3187. defer func() {
  3188. this.Data["json"] = br
  3189. this.ServeJSON()
  3190. }()
  3191. keyWord := this.GetString("KeyWord")
  3192. applyMethod, _ := this.GetInt("ApplyMethod")
  3193. sysUser := this.SysUser
  3194. if sysUser == nil {
  3195. br.Msg = "请登录"
  3196. br.ErrMsg = "请登录,SysUser Is Empty"
  3197. br.Ret = 408
  3198. return
  3199. }
  3200. list := make([]*models.PotentialUserItem, 0)
  3201. //管理员才让导出数据
  3202. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  3203. var condition string
  3204. var pars []interface{}
  3205. if applyMethod > 0 {
  3206. condition += ` AND apply_method=? `
  3207. pars = append(pars, applyMethod-1)
  3208. }
  3209. if keyWord != "" {
  3210. condition += ` AND (a.nick_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  3211. }
  3212. tmpList, err := models.GetPotentialUserListExport(condition, pars)
  3213. if err != nil {
  3214. br.Msg = "获取失败"
  3215. br.ErrMsg = "获取失败,Err:" + err.Error()
  3216. return
  3217. }
  3218. list = tmpList
  3219. }
  3220. dir, err := os.Executable()
  3221. exPath := filepath.Dir(dir)
  3222. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  3223. xlsxFile := xlsx.NewFile()
  3224. if err != nil {
  3225. br.Msg = "生成文件失败"
  3226. br.ErrMsg = "生成文件失败"
  3227. return
  3228. }
  3229. style := xlsx.NewStyle()
  3230. alignment := xlsx.Alignment{
  3231. Horizontal: "center",
  3232. Vertical: "center",
  3233. WrapText: true,
  3234. }
  3235. style.Alignment = alignment
  3236. style.ApplyAlignment = true
  3237. sheel, err := xlsxFile.AddSheet("客户数据")
  3238. if err != nil {
  3239. br.Msg = "新增Sheet失败"
  3240. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  3241. return
  3242. }
  3243. titleRow := sheel.AddRow()
  3244. titleRow.AddCell().SetValue("姓名")
  3245. titleRow.AddCell().SetValue("手机号")
  3246. titleRow.AddCell().SetValue("邮箱")
  3247. titleRow.AddCell().SetValue("注册时间")
  3248. titleRow.AddCell().SetValue("公司")
  3249. titleRow.AddCell().SetValue("申请类型")
  3250. titleRow.AddCell().SetValue("累计阅读次数")
  3251. titleRow.AddCell().SetValue("最后一次阅读时间")
  3252. for _, v := range list {
  3253. dataRow := sheel.AddRow()
  3254. dataRow.SetHeight(20)
  3255. dataRow.AddCell().SetString(v.RealName)
  3256. dataRow.AddCell().SetString(v.Mobile)
  3257. dataRow.AddCell().SetString(v.Email)
  3258. dataRow.AddCell().SetString(v.CreatedTime)
  3259. dataRow.AddCell().SetString(v.CompanyName)
  3260. if v.ApplyMethod == 0 {
  3261. dataRow.AddCell().SetString("未申请")
  3262. } else if v.ApplyMethod == 1 {
  3263. dataRow.AddCell().SetString("已付费客户申请试用")
  3264. } else {
  3265. dataRow.AddCell().SetString("非客户申请试用")
  3266. }
  3267. reportView, err := models.GetReportViewMaxTime(int(v.UserId))
  3268. if err != nil {
  3269. br.Msg = "获取失败"
  3270. br.ErrMsg = "获取GetReportViewMaxTime失败,Err:" + err.Error()
  3271. return
  3272. }
  3273. if reportView.LastViewTime.After(v.LastViewTime) {
  3274. v.LastViewTimeStr = v.LastViewTime.Format(utils.FormatDateTime)
  3275. } else {
  3276. v.LastViewTimeStr = v.LastViewTime.Format(utils.FormatDateTime)
  3277. }
  3278. v.ViewTotal += reportView.ViewTotal
  3279. dataRow.AddCell().SetInt(v.ViewTotal)
  3280. if v.LastViewTimeStr == "0001-01-01 00:00:00" {
  3281. v.LastViewTimeStr = ""
  3282. }
  3283. dataRow.AddCell().SetString(v.LastViewTimeStr)
  3284. }
  3285. err = xlsxFile.Save(downLoadnFilePath)
  3286. if err != nil {
  3287. br.Msg = "保存文件失败"
  3288. br.ErrMsg = "保存文件失败"
  3289. return
  3290. }
  3291. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  3292. downloadFileName := "潜在用户列表_" + randStr + ".xlsx"
  3293. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  3294. defer func() {
  3295. os.Remove(downLoadnFilePath)
  3296. }()
  3297. br.Ret = 200
  3298. br.Success = true
  3299. br.Msg = "导出成功"
  3300. }
  3301. // @Title 获取批量导入联系人数据
  3302. // @Description 获取批量导入联系人数据
  3303. // @Param File query file true "文件"
  3304. // @Param CompanyId query file true "客户id"
  3305. // @Success 200 {object} models.ImportListResp
  3306. // @router /import/list [post]
  3307. func (this *CompanyUserController) ImportList() {
  3308. br := new(models.BaseResponse).Init()
  3309. defer func() {
  3310. this.Data["json"] = br
  3311. this.ServeJSON()
  3312. }()
  3313. sysUser := this.SysUser
  3314. if sysUser == nil {
  3315. br.Msg = "请重新登录"
  3316. return
  3317. }
  3318. file, h, err := this.GetFile("File")
  3319. if err != nil {
  3320. br.Msg = "获取文件失败"
  3321. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  3322. return
  3323. }
  3324. companyId, _ := this.GetInt("CompanyId")
  3325. if companyId <= 0 {
  3326. br.Msg = "参数错误"
  3327. br.ErrMsg = "客户id为0"
  3328. return
  3329. }
  3330. //产品id
  3331. productId := services.GetProductId(sysUser.RoleTypeCode)
  3332. //操作权限校验
  3333. errMsg, err := checkOpCompanyUserByCompanyId(companyId, productId, sysUser.AdminId, sysUser.RoleTypeCode)
  3334. if err != nil {
  3335. br.Msg = "导入失败!" + errMsg
  3336. br.ErrMsg = "导入失败,Err:" + err.Error()
  3337. return
  3338. }
  3339. //userBusinessCardCount := 0
  3340. //if productId > 0 {
  3341. // tmpUserBusinessCardCount, err := models.GetCompanyUserBusinessCardCountByProductId(companyId, productId)
  3342. // if err != nil {
  3343. // br.Msg = "导入失败"
  3344. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3345. // return
  3346. // }
  3347. // userBusinessCardCount = tmpUserBusinessCardCount
  3348. //} else {
  3349. // tmpUserBusinessCardCount, err := company.GetCompanyUserBusinessCardCount(companyId)
  3350. // if err != nil {
  3351. // br.Msg = "导入失败"
  3352. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3353. // return
  3354. // }
  3355. // userBusinessCardCount = tmpUserBusinessCardCount
  3356. //}
  3357. //
  3358. //if userBusinessCardCount <= 0 {
  3359. // br.Msg = "请先至少添加一位名片联系人"
  3360. // return
  3361. //}
  3362. //客户信息
  3363. companyInfo, err := company.GetCompanyById(companyId)
  3364. if err != nil {
  3365. br.Msg = "导入异常"
  3366. br.Msg = "导入异常:" + err.Error()
  3367. return
  3368. }
  3369. uploadDir := "static/xls"
  3370. err = os.MkdirAll(uploadDir, 766)
  3371. if err != nil {
  3372. br.Msg = "存储目录创建失败"
  3373. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  3374. return
  3375. }
  3376. path := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  3377. defer file.Close()
  3378. err = this.SaveToFile("File", path)
  3379. if err != nil {
  3380. br.Msg = "文件保存失败"
  3381. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  3382. return
  3383. }
  3384. xlFile, err := xlsx.OpenFile(path)
  3385. if err != nil {
  3386. br.Msg = "获取失败"
  3387. br.ErrMsg = "打开文件失败,Err:" + err.Error() + ";path:" + path
  3388. return
  3389. }
  3390. existUser := make([]*models.WxUser, 0)
  3391. //excel中已经存在的数据,用来判断excel中是否存在相同手机号/邮箱,避免重复提交
  3392. excelData := make(map[string]string)
  3393. //重复数据
  3394. repeatUser := make([]*models.WxUser, 0)
  3395. // 遍历sheet页读取
  3396. for _, sheet := range xlFile.Sheets {
  3397. //遍历行读取
  3398. maxRow := sheet.MaxRow
  3399. fmt.Println("maxRow:", maxRow)
  3400. fmt.Println("maxRow")
  3401. for i := 0; i < maxRow; i++ {
  3402. if i == 0 {
  3403. row := sheet.Row(i)
  3404. cells := row.Cells
  3405. for k, cell := range cells {
  3406. text := cell.String()
  3407. if k == 0 {
  3408. if text != "姓名" {
  3409. br.Msg = "模板格式错误"
  3410. br.ErrMsg = "模板格式错误,title:" + text
  3411. return
  3412. }
  3413. }
  3414. if k == 1 {
  3415. if !strings.Contains(text, "性别") {
  3416. br.Msg = "模板格式错误"
  3417. br.ErrMsg = "模板格式错误,title:" + text
  3418. return
  3419. }
  3420. }
  3421. if k == 2 {
  3422. if !strings.Contains(text, "国际区号(中国大陆填写:86)") {
  3423. br.Msg = "模板格式错误"
  3424. br.ErrMsg = "模板格式错误,title:" + text
  3425. return
  3426. }
  3427. }
  3428. if k == 3 {
  3429. if !strings.Contains(text, "手机号") {
  3430. br.Msg = "模板格式错误"
  3431. br.ErrMsg = "模板格式错误,title:" + text
  3432. return
  3433. }
  3434. }
  3435. if k == 4 {
  3436. if !strings.Contains(text, "邮箱") {
  3437. br.Msg = "模板格式错误"
  3438. br.ErrMsg = "模板格式错误,title:" + text
  3439. return
  3440. }
  3441. }
  3442. if k == 5 {
  3443. if !strings.Contains(text, "是否决策人") {
  3444. br.Msg = "模板格式错误"
  3445. br.ErrMsg = "模板格式错误,title:" + text
  3446. return
  3447. }
  3448. }
  3449. if k == 6 {
  3450. if !strings.Contains(text, "职位") {
  3451. br.Msg = "模板格式错误"
  3452. br.ErrMsg = "模板格式错误,title:" + text
  3453. return
  3454. }
  3455. }
  3456. if k == 7 {
  3457. if !strings.Contains(text, "部门") {
  3458. br.Msg = "模板格式错误"
  3459. br.ErrMsg = "模板格式错误,title:" + text
  3460. return
  3461. }
  3462. }
  3463. }
  3464. }
  3465. if i >= 1 {
  3466. row := sheet.Row(i)
  3467. cells := row.Cells
  3468. var userName, sex, countryCode, mobileOne, email, isMaker, position, departmentName string
  3469. for k, cell := range cells {
  3470. if k == 0 {
  3471. userName = cell.String()
  3472. }
  3473. if k == 1 {
  3474. sex = cell.String()
  3475. }
  3476. if k == 2 {
  3477. countryCode = cell.String()
  3478. }
  3479. if k == 3 {
  3480. mobileOne = cell.String()
  3481. }
  3482. if k == 4 {
  3483. email = cell.String()
  3484. }
  3485. if k == 5 {
  3486. isMaker = cell.String()
  3487. }
  3488. if k == 6 {
  3489. position = cell.String()
  3490. }
  3491. if k == 7 {
  3492. departmentName = cell.String()
  3493. }
  3494. }
  3495. //移除空格
  3496. mobileOne = utils.TrimStr(mobileOne)
  3497. email = utils.TrimStr(email)
  3498. //这些字段都没有的话,系统认为excel到底了
  3499. if userName == "" && sex == "" && mobileOne == "" {
  3500. break
  3501. }
  3502. if userName == "" {
  3503. continue
  3504. }
  3505. //如果手机号或者邮箱都没有填写的情况下
  3506. if mobileOne == "" && email == "" {
  3507. continue
  3508. }
  3509. if mobileOne != "" && countryCode == "" {
  3510. br.Msg = "导入数据中存在【国际区号】为空的联系人,请检查"
  3511. return
  3512. }
  3513. //如果是权益,且是海外用户的情况下,那么手机号、邮箱有一个没有填写的情况下,那么退出当前循环
  3514. if productId == 2 && companyInfo.RegionType == "海外" && (mobileOne == "" || email == "") {
  3515. continue
  3516. }
  3517. item := new(models.WxUser)
  3518. item.CountryCode = countryCode
  3519. item.RealName = userName
  3520. if sex == "男" {
  3521. item.Sex = 1
  3522. } else if sex == "女" {
  3523. item.Sex = 2
  3524. } else {
  3525. item.Sex = 0
  3526. }
  3527. item.Mobile = mobileOne
  3528. item.Email = email
  3529. if isMaker == "是" {
  3530. item.IsMaker = 1
  3531. } else {
  3532. item.IsMaker = 0
  3533. }
  3534. item.Position = position
  3535. item.DepartmentName = departmentName
  3536. if mobileOne != "" {
  3537. if _, ok := excelData[mobileOne]; ok {
  3538. //将用户插入其中,然后退出当前循环,进入下一循环
  3539. repeatUser = append(repeatUser, item)
  3540. continue
  3541. }
  3542. excelData[mobileOne] = ""
  3543. }
  3544. if email != "" {
  3545. if _, ok := excelData[email]; ok {
  3546. //将用户插入其中,然后退出当前循环,进入下一循环
  3547. repeatUser = append(repeatUser, item)
  3548. continue
  3549. }
  3550. excelData[mobileOne] = ""
  3551. }
  3552. //没问题数据
  3553. existUser = append(existUser, item)
  3554. }
  3555. }
  3556. }
  3557. //defer func() {
  3558. // os.Remove(path)
  3559. //}()
  3560. br.Msg = "获取成功"
  3561. br.Ret = 200
  3562. br.Success = true
  3563. br.Data = models.ImportListResp{
  3564. ValidUser: existUser,
  3565. RepeatUser: repeatUser,
  3566. }
  3567. }
  3568. // 需要更新的联系人数据实体
  3569. type UpdateWxUser struct {
  3570. OldWxUser models.WxUser `description:"旧的联系人数据orm实体(未变更前的)"`
  3571. WxUser *models.WxUser `description:"联系人数据orm实体"`
  3572. Cols []string `description:"需要修改的字段名切片"`
  3573. }
  3574. // @Title 批量导入联系人数据
  3575. // @Description 批量导入联系人数据
  3576. // @Param File query file true "文件"
  3577. // @Param CompanyId query file true "客户id"
  3578. // @Success 600 {object} []*company.CompanyUser
  3579. // @Success 200 Ret=200 导入成功
  3580. // @router /import [post]
  3581. func (this *CompanyUserController) Import() {
  3582. br := new(models.BaseResponse).Init()
  3583. defer func() {
  3584. this.Data["json"] = br
  3585. this.ServeJSON()
  3586. }()
  3587. sysUser := this.SysUser
  3588. if sysUser == nil {
  3589. br.Msg = "请重新登录"
  3590. return
  3591. }
  3592. file, h, err := this.GetFile("File")
  3593. if err != nil {
  3594. br.Msg = "获取文件失败"
  3595. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  3596. return
  3597. }
  3598. companyId, _ := this.GetInt("CompanyId")
  3599. if companyId <= 0 {
  3600. br.Msg = "参数错误"
  3601. br.ErrMsg = "客户id为0"
  3602. return
  3603. }
  3604. //产品id
  3605. productId := services.GetProductId(sysUser.RoleTypeCode)
  3606. //操作权限校验
  3607. errMsg, err := checkOpCompanyUserByCompanyId(companyId, productId, sysUser.AdminId, sysUser.RoleTypeCode)
  3608. if err != nil {
  3609. br.Msg = "导入失败!" + errMsg
  3610. br.ErrMsg = "导入失败,Err:" + err.Error()
  3611. return
  3612. }
  3613. //userBusinessCardCount := 0
  3614. //if productId > 0 {
  3615. // tmpUserBusinessCardCount, err := models.GetCompanyUserBusinessCardCountByProductId(companyId, productId)
  3616. // if err != nil {
  3617. // br.Msg = "导入失败"
  3618. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3619. // return
  3620. // }
  3621. // userBusinessCardCount = tmpUserBusinessCardCount
  3622. //} else {
  3623. // tmpUserBusinessCardCount, err := company.GetCompanyUserBusinessCardCount(companyId)
  3624. // if err != nil {
  3625. // br.Msg = "导入失败"
  3626. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3627. // return
  3628. // }
  3629. // userBusinessCardCount = tmpUserBusinessCardCount
  3630. //}
  3631. //
  3632. //if userBusinessCardCount <= 0 {
  3633. // br.Msg = "请先至少添加一位名片联系人"
  3634. // return
  3635. //}
  3636. uploadDir := "static/xls"
  3637. err = os.MkdirAll(uploadDir, 766)
  3638. if err != nil {
  3639. br.Msg = "存储目录创建失败"
  3640. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  3641. return
  3642. }
  3643. path := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  3644. defer file.Close()
  3645. err = this.SaveToFile("File", path)
  3646. if err != nil {
  3647. br.Msg = "文件保存失败"
  3648. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  3649. return
  3650. }
  3651. xlFile, err := xlsx.OpenFile(path)
  3652. if err != nil {
  3653. br.Msg = "文件导入失败"
  3654. br.ErrMsg = "文件导入失败,Err:" + err.Error()
  3655. return
  3656. }
  3657. //获取当前操作员产品权限列表
  3658. companyProductList, productErr := getSysCompanyProductList(productId, companyId)
  3659. if productErr != nil {
  3660. br.Msg = "新增失败"
  3661. br.ErrMsg = "新增失败,获取当前操作员产品权限异常,Err:" + productErr.Error()
  3662. return
  3663. }
  3664. if companyProductList == nil {
  3665. br.Msg = "新增失败"
  3666. br.ErrMsg = "新增失败,获取当前操作员找不到产品权限"
  3667. return
  3668. }
  3669. //客户信息
  3670. companyInfo, err := company.GetCompanyById(companyId)
  3671. if err != nil {
  3672. br.Msg = "导入异常"
  3673. br.Msg = "导入异常:" + err.Error()
  3674. return
  3675. }
  3676. //已存在联系人的切片
  3677. existUser := make([]*company.CompanyUser, 0)
  3678. //待添加销售员与联系人关系的切片
  3679. notRelationUser := make([]map[int]*company.CompanyUser, 0)
  3680. //允许添加的联系人
  3681. needAddUser := make([]*models.WxUserDetail, 0)
  3682. //excel中已经存在的数据,用来判断excel中是否存在相同手机号/邮箱,避免重复提交
  3683. excelData := make(map[string]string)
  3684. //需要更新的数据实体切片
  3685. updateWxUserSlice := make([]UpdateWxUser, 0)
  3686. // 遍历sheet页读取
  3687. for _, sheet := range xlFile.Sheets {
  3688. //遍历行读取
  3689. maxRow := sheet.MaxRow
  3690. for i := 0; i < maxRow; i++ {
  3691. if i == 0 {
  3692. row := sheet.Row(i)
  3693. cells := row.Cells
  3694. for k, cell := range cells {
  3695. text := cell.String()
  3696. if k == 0 {
  3697. if text != "姓名" {
  3698. br.Msg = "模板格式错误"
  3699. br.ErrMsg = "模板格式错误,title:" + text
  3700. return
  3701. }
  3702. }
  3703. if k == 1 {
  3704. if !strings.Contains(text, "性别") {
  3705. br.Msg = "模板格式错误"
  3706. br.ErrMsg = "模板格式错误,title:" + text
  3707. return
  3708. }
  3709. }
  3710. if k == 2 {
  3711. if !strings.Contains(text, "国际区号(中国大陆填写:86)") {
  3712. br.Msg = "模板格式错误"
  3713. br.ErrMsg = "模板格式错误,title:" + text
  3714. return
  3715. }
  3716. }
  3717. if k == 3 {
  3718. if !strings.Contains(text, "手机号") {
  3719. br.Msg = "模板格式错误"
  3720. br.ErrMsg = "模板格式错误,title:" + text
  3721. return
  3722. }
  3723. }
  3724. if k == 4 {
  3725. if !strings.Contains(text, "邮箱") {
  3726. br.Msg = "模板格式错误"
  3727. br.ErrMsg = "模板格式错误,title:" + text
  3728. return
  3729. }
  3730. }
  3731. if k == 5 {
  3732. if !strings.Contains(text, "是否决策人") {
  3733. br.Msg = "模板格式错误"
  3734. br.ErrMsg = "模板格式错误,title:" + text
  3735. return
  3736. }
  3737. }
  3738. if k == 6 {
  3739. if !strings.Contains(text, "职位") {
  3740. br.Msg = "模板格式错误"
  3741. br.ErrMsg = "模板格式错误,title:" + text
  3742. return
  3743. }
  3744. }
  3745. if k == 7 {
  3746. if !strings.Contains(text, "部门") {
  3747. br.Msg = "模板格式错误"
  3748. br.ErrMsg = "模板格式错误,title:" + text
  3749. return
  3750. }
  3751. }
  3752. }
  3753. }
  3754. if i >= 1 {
  3755. row := sheet.Row(i)
  3756. cells := row.Cells
  3757. var userName, sex, countryCode, mobileOne, email, isMaker, position, departmentName string
  3758. for k, cell := range cells {
  3759. if k == 0 {
  3760. userName = cell.String()
  3761. }
  3762. if k == 1 {
  3763. sex = cell.String()
  3764. }
  3765. if k == 2 {
  3766. countryCode = strings.Replace(cell.String(), "+", "", -1)
  3767. countryCode = strings.Replace(countryCode, "-", "", -1)
  3768. countryCode = strings.Replace(countryCode, " ", "", -1)
  3769. }
  3770. if k == 3 {
  3771. mobileOne = cell.String()
  3772. }
  3773. if k == 4 {
  3774. email = cell.String()
  3775. }
  3776. if k == 5 {
  3777. isMaker = cell.String()
  3778. }
  3779. if k == 6 {
  3780. position = cell.String()
  3781. }
  3782. if k == 7 {
  3783. departmentName = cell.String()
  3784. }
  3785. }
  3786. //移除空格
  3787. mobileOne = utils.TrimStr(mobileOne)
  3788. email = utils.TrimStr(email)
  3789. //这些字段都没有的话,系统认为excel到底了
  3790. if userName == "" && sex == "" && mobileOne == "" {
  3791. break
  3792. }
  3793. if userName == "" {
  3794. br.Msg = "导入数据中存在【姓名】为空的联系人,请检查"
  3795. return
  3796. }
  3797. if sex == "" {
  3798. br.Msg = "导入数据中存在【性别】为空的联系人,请检查"
  3799. return
  3800. }
  3801. if mobileOne != "" && countryCode == "" {
  3802. br.Msg = "导入数据中存在【国际区号】为空的联系人,请检查"
  3803. return
  3804. }
  3805. //如果手机号或者邮箱都没有填写的情况下
  3806. if mobileOne == "" && email == "" {
  3807. continue
  3808. }
  3809. //如果是权益,且是海外用户的情况下,那么手机号、邮箱有一个没有填写的情况下,那么退出当前循环
  3810. if productId == 2 && companyInfo.RegionType == "海外" && (mobileOne == "" || email == "") {
  3811. continue
  3812. }
  3813. if isMaker == "" {
  3814. br.Msg = "导入数据中存在【决策人】为空的联系人,请检查"
  3815. return
  3816. }
  3817. item := new(models.WxUserDetail)
  3818. item.RealName = userName
  3819. if sex == "男" {
  3820. item.Sex = 1
  3821. } else if sex == "女" {
  3822. item.Sex = 2
  3823. } else {
  3824. item.Sex = 0
  3825. }
  3826. item.Mobile = mobileOne
  3827. item.Email = email
  3828. if isMaker == "是" {
  3829. item.IsMaker = 1
  3830. } else {
  3831. item.IsMaker = 0
  3832. }
  3833. item.Position = position
  3834. item.DepartmentName = departmentName
  3835. item.SellerRealName = companyProductList[0].SellerName
  3836. item.CompanyName = companyProductList[0].CompanyName
  3837. var mobileOneItem, emailItem *company.CompanyUser
  3838. var mobileNotRelationUserMap, emailNotRelationUserMap map[int]*company.CompanyUser
  3839. //手机号校验
  3840. if mobileOne != "" {
  3841. if _, ok := excelData[mobileOne]; ok {
  3842. br.Msg = "excel表数据异常,存在重复的手机号:" + mobileOne + ",请检查excel表格数据"
  3843. br.ErrMsg = "excel表数据异常,存在重复的手机号:" + mobileOne + ",请检查excel表格数据"
  3844. return
  3845. }
  3846. excelData[mobileOne] = ""
  3847. tmpMobileOneItem, err := company.GetUserCountByMobile(mobileOne)
  3848. if err != nil && err.Error() != utils.ErrNoRow() {
  3849. br.Msg = "新增失败"
  3850. br.ErrMsg = "判断手机号1是否存在,Err:" + err.Error()
  3851. return
  3852. }
  3853. mobileOneItem = tmpMobileOneItem
  3854. if mobileOneItem != nil {
  3855. if mobileOneItem.UserId > 0 {
  3856. hasMove := true //是否可移动
  3857. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  3858. if mobileOneItem.CompanyId > 1 {
  3859. //获取联系人分组信息
  3860. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(mobileOneItem.UserId))
  3861. //根据分组校验
  3862. if err == nil && len(userSellerGroupInfoList) > 0 {
  3863. sellerMap := make(map[int]string) //销售信息map
  3864. sellerNameSlice := make([]string, 0) //销售姓名切片
  3865. for _, v := range userSellerGroupInfoList {
  3866. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  3867. hasMove = false
  3868. }
  3869. if _, ok := sellerMap[v.SellerId]; ok {
  3870. continue
  3871. }
  3872. sellerMap[v.SellerId] = v.SellerRealName
  3873. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  3874. }
  3875. mobileOneItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  3876. }
  3877. }
  3878. item.SellerRealName = mobileOneItem.SellerRealName
  3879. item.CompanyName = mobileOneItem.CompanyName
  3880. //判断该联系人的 客户企业编号 是否与 当前客户编号 一致,如果不一致,那么将该联系人加入到已经存在的联系人列表中去,并终止当前循环,进入下一循环。
  3881. if mobileOneItem.CompanyId != companyId {
  3882. mobileOneItem.HasMove = hasMove //是否可移动
  3883. if hasMove {
  3884. if mobileOneItem.RealName != item.RealName {
  3885. mobileOneItem.RealName = item.RealName
  3886. }
  3887. if mobileOneItem.DepartmentName != item.DepartmentName {
  3888. mobileOneItem.DepartmentName = item.DepartmentName
  3889. }
  3890. if mobileOneItem.RealName != item.RealName {
  3891. mobileOneItem.RealName = item.RealName
  3892. }
  3893. }
  3894. mobileOneItem.CountryCode = countryCode
  3895. existUser = append(existUser, mobileOneItem)
  3896. continue
  3897. }
  3898. //判断该用户与当前账号是否存在关系,如果没有关系,那么直接返回
  3899. notRelationUserMap, checkErr := checkProductListUser(*item, mobileOneItem, companyProductList)
  3900. //sql异常
  3901. if checkErr != nil && checkErr.Error() != utils.ErrNoRow() {
  3902. br.Msg = "新增失败"
  3903. br.ErrMsg = "新增失败,Err:" + checkErr.Error()
  3904. return
  3905. }
  3906. mobileNotRelationUserMap = notRelationUserMap
  3907. }
  3908. }
  3909. }
  3910. //邮箱校验
  3911. if email != "" {
  3912. if _, ok := excelData[email]; ok {
  3913. br.Msg = "excel表数据异常,存在重复的邮箱:" + email + ",请检查excel表格数据"
  3914. br.ErrMsg = "excel表数据异常,存在重复的邮箱:" + email + ",请检查excel表格数据"
  3915. return
  3916. }
  3917. excelData[email] = ""
  3918. tmpEmailItem, err := company.CheckCompanyUserCountByEmail(email)
  3919. if err != nil && err.Error() != utils.ErrNoRow() {
  3920. br.Msg = "新增失败"
  3921. br.ErrMsg = "新增失败,Err:" + err.Error()
  3922. return
  3923. }
  3924. emailItem = tmpEmailItem
  3925. if emailItem != nil {
  3926. if emailItem.UserId > 0 {
  3927. hasMove := true //是否可移动
  3928. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  3929. if emailItem.CompanyId > 1 {
  3930. //获取联系人分组信息
  3931. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(emailItem.UserId))
  3932. //根据分组校验
  3933. if err == nil && len(userSellerGroupInfoList) > 0 {
  3934. sellerMap := make(map[int]string) //销售信息map
  3935. sellerNameSlice := make([]string, 0) //销售姓名切片
  3936. for _, v := range userSellerGroupInfoList {
  3937. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  3938. hasMove = false
  3939. }
  3940. if _, ok := sellerMap[v.SellerId]; ok {
  3941. continue
  3942. }
  3943. sellerMap[v.SellerId] = v.SellerRealName
  3944. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  3945. }
  3946. emailItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  3947. }
  3948. }
  3949. item.SellerRealName = emailItem.SellerRealName
  3950. item.CompanyName = emailItem.CompanyName
  3951. //判断该联系人的 客户企业编号 是否与 当前客户编号 一致,如果不一致,那么将该联系人加入到已经存在的联系人列表中去,并终止当前循环,进入下一循环。
  3952. if emailItem.CompanyId != companyId {
  3953. emailItem.HasMove = hasMove //是否可移动
  3954. if hasMove {
  3955. if emailItem.RealName != item.RealName {
  3956. emailItem.RealName = item.RealName
  3957. }
  3958. if emailItem.DepartmentName != item.DepartmentName {
  3959. emailItem.DepartmentName = item.DepartmentName
  3960. }
  3961. if emailItem.RealName != item.RealName {
  3962. emailItem.RealName = item.RealName
  3963. }
  3964. }
  3965. emailItem.CountryCode = countryCode
  3966. existUser = append(existUser, emailItem)
  3967. continue
  3968. }
  3969. //判断该用户与当前账号是否存在关系,如果没有关系,那么直接返回
  3970. notRelationUserMap, checkErr := checkProductListUser(*item, emailItem, companyProductList)
  3971. //sql异常
  3972. if checkErr != nil && checkErr.Error() != utils.ErrNoRow() {
  3973. br.Msg = "新增失败"
  3974. br.ErrMsg = "新增失败,Err:" + checkErr.Error()
  3975. return
  3976. }
  3977. //判断该用户与销售是否存在关系,如果不存在,那么加入到 待添加关系 的切片中
  3978. emailNotRelationUserMap = notRelationUserMap
  3979. }
  3980. }
  3981. }
  3982. //如果系统已存在该联系人,那么逻辑处理后退出当前循环,进入下一循环
  3983. if mobileOneItem != nil || emailItem != nil {
  3984. //如果既存在该手机号联系人,也存在该邮箱联系人;那么需要去判断 这 两个 联系人 是不是同一个,
  3985. //如果不是同一个联系人的话,那么只添加对应的关系,不去修改对应的数据
  3986. var wxUserItem *models.WxUser
  3987. var oldTmpWxUserItem models.WxUser
  3988. cols := make([]string, 0)
  3989. if mobileOneItem != nil && emailItem != nil {
  3990. //如果两个联系人 不是 同一个
  3991. if mobileOneItem.UserId != emailItem.UserId {
  3992. notRelationUser = append(notRelationUser, mobileNotRelationUserMap, emailNotRelationUserMap)
  3993. //wxUserItem = mobileOneItem
  3994. } else {
  3995. //如果两个联系人 是 同一个
  3996. notRelationUser = append(notRelationUser, mobileNotRelationUserMap)
  3997. tmpWxUserItem, err := models.GetWxUserByUserId(int(mobileOneItem.UserId))
  3998. if err != nil {
  3999. br.Msg = "导入失败"
  4000. br.ErrMsg = "查询联系人信息失败,Err:" + err.Error()
  4001. return
  4002. }
  4003. oldTmpWxUserItem := *tmpWxUserItem
  4004. if tmpWxUserItem.IsMaker != item.IsMaker {
  4005. tmpWxUserItem.IsMaker = item.IsMaker
  4006. cols = append(cols, "IsMaker")
  4007. }
  4008. if tmpWxUserItem.DepartmentName != item.DepartmentName {
  4009. tmpWxUserItem.DepartmentName = item.DepartmentName
  4010. cols = append(cols, "DepartmentName")
  4011. }
  4012. if tmpWxUserItem.RealName != item.RealName {
  4013. tmpWxUserItem.RealName = item.RealName
  4014. cols = append(cols, "RealName")
  4015. }
  4016. if len(cols) > 0 {
  4017. updateWxUser := UpdateWxUser{
  4018. OldWxUser: oldTmpWxUserItem,
  4019. WxUser: tmpWxUserItem,
  4020. Cols: cols,
  4021. }
  4022. updateWxUserSlice = append(updateWxUserSlice, updateWxUser)
  4023. }
  4024. wxUserItem = tmpWxUserItem
  4025. }
  4026. } else if mobileOneItem != nil {
  4027. //如果只存在 手机号联系人
  4028. notRelationUser = append(notRelationUser, mobileNotRelationUserMap)
  4029. //获取联系人orm实体类
  4030. tmpWxUserItem, err := models.GetWxUserByUserId(int(mobileOneItem.UserId))
  4031. if err != nil {
  4032. br.Msg = "导入失败"
  4033. br.ErrMsg = "查询联系人信息失败,Err:" + err.Error()
  4034. return
  4035. }
  4036. oldTmpWxUserItem = *tmpWxUserItem
  4037. if tmpWxUserItem.IsMaker != item.IsMaker {
  4038. tmpWxUserItem.IsMaker = item.IsMaker
  4039. cols = append(cols, "IsMaker")
  4040. }
  4041. if tmpWxUserItem.DepartmentName != item.DepartmentName && item.DepartmentName != "" {
  4042. tmpWxUserItem.DepartmentName = item.DepartmentName
  4043. cols = append(cols, "DepartmentName")
  4044. }
  4045. if tmpWxUserItem.RealName != item.RealName && item.RealName != "" {
  4046. tmpWxUserItem.RealName = item.RealName
  4047. cols = append(cols, "RealName")
  4048. }
  4049. if tmpWxUserItem.Email != item.Email && item.Email != "" {
  4050. tmpWxUserItem.Email = item.Email
  4051. cols = append(cols, "Email")
  4052. }
  4053. wxUserItem = tmpWxUserItem
  4054. } else if emailItem != nil {
  4055. //如果只存在 邮箱联系人
  4056. notRelationUser = append(notRelationUser, emailNotRelationUserMap)
  4057. //获取联系人orm实体类
  4058. tmpWxUserItem, err := models.GetWxUserByUserId(int(emailItem.UserId))
  4059. if err != nil {
  4060. br.Msg = "导入失败"
  4061. br.ErrMsg = "查询联系人信息失败,Err:" + err.Error()
  4062. return
  4063. }
  4064. oldTmpWxUserItem = *tmpWxUserItem
  4065. if tmpWxUserItem.IsMaker != item.IsMaker {
  4066. tmpWxUserItem.IsMaker = item.IsMaker
  4067. cols = append(cols, "IsMaker")
  4068. }
  4069. if tmpWxUserItem.DepartmentName != item.DepartmentName && item.DepartmentName != "" {
  4070. tmpWxUserItem.DepartmentName = item.DepartmentName
  4071. cols = append(cols, "DepartmentName")
  4072. }
  4073. if tmpWxUserItem.RealName != item.RealName && item.RealName != "" {
  4074. tmpWxUserItem.RealName = item.RealName
  4075. cols = append(cols, "RealName")
  4076. }
  4077. if tmpWxUserItem.Email != item.Email && item.Email != "" {
  4078. tmpWxUserItem.Email = item.Email
  4079. cols = append(cols, "Email")
  4080. }
  4081. wxUserItem = tmpWxUserItem
  4082. }
  4083. //如果变更字段数量大于0
  4084. if len(cols) > 0 {
  4085. updateWxUser := UpdateWxUser{
  4086. OldWxUser: oldTmpWxUserItem,
  4087. WxUser: wxUserItem,
  4088. Cols: cols,
  4089. }
  4090. updateWxUserSlice = append(updateWxUserSlice, updateWxUser)
  4091. }
  4092. continue
  4093. }
  4094. //确实系统中不存在该用户
  4095. item.CountryCode = countryCode
  4096. needAddUser = append(needAddUser, item)
  4097. }
  4098. }
  4099. }
  4100. fmt.Println("表格中用户与系统中存在用户没有冲突")
  4101. //如果表格中用户与系统中存在用户 没有冲突,那么继续执行
  4102. for _, user := range needAddUser {
  4103. if user.RealName != "" && (user.Mobile != "" || user.Email != "") {
  4104. userData := new(models.WxUser)
  4105. userData.RealName = user.RealName
  4106. userData.Sex = user.Sex
  4107. userData.Mobile = user.Mobile
  4108. userData.CountryCode = user.CountryCode
  4109. userData.OutboundMobile = user.Mobile
  4110. userData.OutboundCountryCode = user.CountryCode
  4111. userData.Email = user.Email
  4112. userData.Position = user.Position
  4113. userData.IsMaker = user.IsMaker
  4114. userData.CompanyId = companyId
  4115. userData.DepartmentName = user.DepartmentName
  4116. userData.CountryCode = user.CountryCode
  4117. //判断该手机号、邮箱是否已经添加,如果已经添加,那么就不再添加
  4118. var key string
  4119. if user.Mobile != "" {
  4120. key = "wx_user:mobile:" + userData.Mobile
  4121. } else {
  4122. key = "wx_user:email:" + userData.Email
  4123. }
  4124. isHas := utils.Rc.IsExist(key)
  4125. if isHas == false {
  4126. newId, err := models.AddWxUser(userData)
  4127. userData.UserId = newId
  4128. if err != nil {
  4129. br.Msg = "导入失败"
  4130. br.ErrMsg = "导入失败,Err:" + err.Error()
  4131. return
  4132. }
  4133. //添加联系人与销售员的关系
  4134. for _, companyProduct := range companyProductList {
  4135. models.AddUserSellerRelation(newId, companyProduct.CompanyId, companyProduct.SellerId, companyProduct.ProductId, companyProduct.SellerName, user.Mobile, user.Email)
  4136. }
  4137. //联系人信息
  4138. userInfo, _ := json.Marshal(userData)
  4139. go services.AddWxUserOpLog(company.WxUserOpLog{
  4140. LogType: "import",
  4141. UserId: int(userData.UserId),
  4142. CompanyId: userData.CompanyId,
  4143. Mobile: userData.Mobile,
  4144. Email: userData.Email,
  4145. OriginalUserInfo: "",
  4146. UserInfo: string(userInfo),
  4147. OriginalUserSellerInfo: "",
  4148. UserSellerInfo: "",
  4149. OpUserId: sysUser.AdminId,
  4150. OpUserName: sysUser.RealName,
  4151. CreateTime: time.Now(),
  4152. })
  4153. }
  4154. }
  4155. }
  4156. //添加已经存在的联系人与销售员关系
  4157. fmt.Println("开始添加已经存在的联系人与销售员关系")
  4158. for _, notRelationUserMap := range notRelationUser {
  4159. if item, ok := notRelationUserMap[1]; ok {
  4160. //添加联系人与销售员的关系
  4161. mobileOne := item.Mobile
  4162. mobileTwo := item.MobileTwo
  4163. email := item.Email
  4164. businessCardUrl := item.BusinessCardUrl
  4165. //对老数据做新增字段处理
  4166. _ = company.EditCompanyUserContact(item.UserId, mobileOne, mobileTwo, email, businessCardUrl)
  4167. models.AddUserSellerRelation(item.UserId, item.CompanyId, item.SellerId, 1, item.SellerName, item.Mobile, item.Email)
  4168. }
  4169. if item, ok := notRelationUserMap[2]; ok {
  4170. //添加联系人与销售员的关系
  4171. mobileOne := item.Mobile
  4172. mobileTwo := item.MobileTwo
  4173. email := item.Email
  4174. businessCardUrl := item.BusinessCardUrl
  4175. //对老数据做新增字段处理
  4176. _ = company.EditCompanyUserContact(item.UserId, mobileOne, mobileTwo, email, businessCardUrl)
  4177. models.AddUserSellerRelation(item.UserId, item.CompanyId, item.SellerId, 2, item.SellerName, item.Mobile, item.Email)
  4178. }
  4179. }
  4180. fmt.Println("开始更新联系人信息")
  4181. for _, updateWxUser := range updateWxUserSlice {
  4182. _ = updateWxUser.WxUser.Update(updateWxUser.Cols)
  4183. //联系人信息
  4184. originalUserInfo, _ := json.Marshal(updateWxUser.OldWxUser)
  4185. userInfo, _ := json.Marshal(updateWxUser.WxUser)
  4186. go services.AddWxUserOpLog(company.WxUserOpLog{
  4187. LogType: "import",
  4188. UserId: int(updateWxUser.WxUser.UserId),
  4189. CompanyId: updateWxUser.WxUser.CompanyId,
  4190. Mobile: updateWxUser.WxUser.Mobile,
  4191. Email: updateWxUser.WxUser.Email,
  4192. OriginalUserInfo: string(originalUserInfo),
  4193. UserInfo: string(userInfo),
  4194. OriginalUserSellerInfo: "",
  4195. UserSellerInfo: "",
  4196. OpUserId: sysUser.AdminId,
  4197. OpUserName: sysUser.RealName,
  4198. CreateTime: time.Now(),
  4199. })
  4200. }
  4201. defer func() {
  4202. os.Remove(path)
  4203. }()
  4204. if len(existUser) > 0 {
  4205. br.Ret = 600
  4206. br.Msg = "系统中已存在用户"
  4207. br.IsSendEmail = false
  4208. br.Data = existUser
  4209. return
  4210. }
  4211. //更新客户最近阅读次数以及最近阅读时间
  4212. go services.ModifyCompanyProductLastViewData([]int{companyId})
  4213. br.Msg = "导入成功"
  4214. br.Ret = 200
  4215. br.Success = true
  4216. }
  4217. // @Title 客户的联系人导出
  4218. // @Description 客户的联系人导出接口
  4219. // @Param CompanyId query int true "公司id,必填"
  4220. // @Param KeyWord query string true "搜索关键词"
  4221. // @Success 200 {object} company.CompanyUserListResp
  4222. // @router /user/exportByCompanyId [get]
  4223. func (this *CompanyUserController) Export() {
  4224. br := new(models.BaseResponse).Init()
  4225. defer func() {
  4226. this.Data["json"] = br
  4227. this.ServeJSON()
  4228. }()
  4229. sysUser := this.SysUser
  4230. if sysUser == nil {
  4231. br.Msg = "请登录"
  4232. br.ErrMsg = "请登录,SysUser Is Empty"
  4233. br.Ret = 408
  4234. return
  4235. }
  4236. companyId, _ := this.GetInt("CompanyId")
  4237. keyWord := utils.TrimStr(this.GetString("KeyWord"))
  4238. if companyId <= 0 {
  4239. br.Msg = "请选择客户"
  4240. br.ErrMsg = "客户参数错误"
  4241. return
  4242. }
  4243. var startSize int
  4244. var condition string
  4245. var pars []interface{}
  4246. if keyWord != "" {
  4247. condition += ` AND (a.real_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  4248. }
  4249. //非管理员,那么只能看到该产品下的联系人
  4250. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  4251. productId := services.GetProductId(sysUser.RoleTypeCode)
  4252. condition += ` AND b.product_id = ? `
  4253. pars = append(pars, productId)
  4254. }
  4255. list, err := company.GetCompanyUserListV2(condition, pars, companyId, startSize, 100000)
  4256. if err != nil {
  4257. br.Msg = "获取失败"
  4258. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  4259. return
  4260. }
  4261. //获取企业客户信息
  4262. companyInfo, err := company.GetCompanyById(companyId)
  4263. if err != nil {
  4264. br.Msg = "获取失败"
  4265. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  4266. return
  4267. }
  4268. lenList := len(list)
  4269. if lenList > 0 {
  4270. userIdSlice := make([]string, 0)
  4271. mobilesSlice := make([]string, 0)
  4272. emailsSlice := make([]string, 0)
  4273. for _, v := range list {
  4274. userIdSlice = append(userIdSlice, strconv.Itoa(int(v.UserId)))
  4275. if v.Mobile != "" {
  4276. mobilesSlice = append(mobilesSlice, v.Mobile)
  4277. } else if v.Email != "" {
  4278. emailsSlice = append(emailsSlice, v.Email)
  4279. }
  4280. }
  4281. //用户浏览数据
  4282. userIdStr := strings.Join(userIdSlice, ",")
  4283. mobileStr := strings.Join(mobilesSlice, "','")
  4284. mobileStr = "'" + mobileStr + "'"
  4285. emailStr := strings.Join(emailsSlice, "','")
  4286. emailStr = "'" + emailStr + "'"
  4287. //userViewTotalList, _ := models.GetCountUserViewHistoryByUserIds(userIdStr)
  4288. //userViewTotalMap := make(map[int]*models.UserViewTotalSlice)
  4289. //for _, userView := range userViewTotalList {
  4290. // userViewTotalMap[userView.UserId] = userView
  4291. //}
  4292. //用户日评浏览数据
  4293. //userReportViewTotalList, _ := models.GetReportViewMaxTimeByUserIds(userIdStr)
  4294. //userReportViewTotalMap := make(map[int]*models.ReportViewRecord)
  4295. //for _, userReportView := range userReportViewTotalList {
  4296. // userReportViewTotalMap[userReportView.UserId] = userReportView
  4297. //}
  4298. //报告统计删除晨报部分统计加入每日资讯 2021-4-9
  4299. //手机号
  4300. userViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  4301. userReportViewMobileTotalMap := make(map[string]*models.ReportViewMobileRecord)
  4302. userAdvisoryViewMobileTotalMap := make(map[string]*advisory.UserViewMobileTotalSlice)
  4303. userCygxArticleHistoryRecordViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  4304. if len(mobilesSlice) > 0 {
  4305. //用户浏览数据
  4306. userViewMobileTotalList, _ := models.GetCountUserViewHistoryByMobiles(mobileStr)
  4307. for _, userViewMobile := range userViewMobileTotalList {
  4308. userViewMobileTotalMap[userViewMobile.Mobile] = userViewMobile
  4309. }
  4310. //每日点评手机号数据
  4311. userReportViewMobileTotalList, _ := models.GetReportViewMaxTimeByMobiles(mobileStr)
  4312. for _, userReportViewMobile := range userReportViewMobileTotalList {
  4313. userReportViewMobileTotalMap[userReportViewMobile.Mobile] = userReportViewMobile
  4314. }
  4315. //每日资讯
  4316. userAdvisoryViewMobileTotalList, _ := advisory.GetCountUserViewHistoryByMobiles(mobileStr)
  4317. //fmt.Println(userAdvisoryViewMobileTotalList)
  4318. for _, userAdvisoryViewMobile := range userAdvisoryViewMobileTotalList {
  4319. userAdvisoryViewMobileTotalMap[userAdvisoryViewMobile.Mobile] = userAdvisoryViewMobile
  4320. }
  4321. //查研观向
  4322. userCygxArticleHistoryRecordViewMobileTotalList, _ := models.GetCountCygxArticleHistoryRecordByMobiles(mobileStr)
  4323. for _, userCygxArticleHistoryRecordViewMobile := range userCygxArticleHistoryRecordViewMobileTotalList {
  4324. userCygxArticleHistoryRecordViewMobileTotalMap[userCygxArticleHistoryRecordViewMobile.Mobile] = userCygxArticleHistoryRecordViewMobile
  4325. }
  4326. }
  4327. //邮箱
  4328. userViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  4329. userReportViewEmailTotalMap := make(map[string]*models.ReportViewEmailRecord)
  4330. userAdvisoryViewEmailTotalMap := make(map[string]*advisory.UserViewEmailTotalSlice)
  4331. userCygxViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  4332. if len(emailsSlice) > 0 {
  4333. //用户浏览数据
  4334. userViewEmailTotalList, _ := models.GetCountUserViewHistoryByEmails(emailStr)
  4335. for _, userViewEmail := range userViewEmailTotalList {
  4336. userViewEmailTotalMap[userViewEmail.Email] = userViewEmail
  4337. }
  4338. //每日点评手机号数据
  4339. userReportViewEmailTotalList, _ := models.GetReportViewMaxTimeByEmails(emailStr)
  4340. for _, userReportViewEmail := range userReportViewEmailTotalList {
  4341. userReportViewEmailTotalMap[userReportViewEmail.Email] = userReportViewEmail
  4342. }
  4343. //每日资讯
  4344. userAdvisoryViewEmailTotalList, _ := advisory.GetCountUserViewHistoryByEmails(emailStr)
  4345. for _, userAdvisoryViewEmail := range userAdvisoryViewEmailTotalList {
  4346. userAdvisoryViewEmailTotalMap[userAdvisoryViewEmail.Email] = userAdvisoryViewEmail
  4347. }
  4348. //查研观向
  4349. userCygxViewEmailTotalList, _ := models.GetCountCygxArticleHistoryRecordByEmails(emailStr)
  4350. for _, userCygxReportViewEmail := range userCygxViewEmailTotalList {
  4351. userCygxViewEmailTotalMap[userCygxReportViewEmail.Email] = userCygxReportViewEmail
  4352. }
  4353. }
  4354. //获取联系人关联产品的条数
  4355. //userSellerRelationTotalSlice, err := models.GetUserSellerRelationCountByUserIds(userIdStr)
  4356. //if err != nil {
  4357. // br.Msg = "获取失败"
  4358. // br.ErrMsg = "获取联系人与销售员信息失败,Err:" + err.Error()
  4359. // return
  4360. //}
  4361. //userSellerRelationTotalMap := make(map[int]*models.UserSellerRelationSlice)
  4362. //for _, userSellerRelationTotal := range userSellerRelationTotalSlice {
  4363. // userSellerRelationTotalMap[userSellerRelationTotal.UserId] = userSellerRelationTotal
  4364. //}
  4365. userRecordRegisterMap := make(map[int]time.Time)
  4366. if len(userIdSlice) > 0 {
  4367. //用户绑定注册数据
  4368. userRecordRegisterList, _ := models.GetUserRecordRegisterByUserIds(userIdStr)
  4369. for _, userRecordRegister := range userRecordRegisterList {
  4370. userRecordRegisterMap[userRecordRegister.UserId] = userRecordRegister.CreateTime
  4371. }
  4372. }
  4373. for i := 0; i < lenList; i++ {
  4374. item := list[i]
  4375. //企业名称
  4376. list[i].CompanyName = companyInfo.CompanyName
  4377. //手机号用户累计浏览次数
  4378. if userView, ok := userViewMobileTotalMap[item.Mobile]; ok {
  4379. list[i].ViewTotal += userView.Total
  4380. if userView.CreatedTime.After(list[i].LastViewTime) {
  4381. list[i].LastViewTime = userView.CreatedTime
  4382. list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  4383. }
  4384. }
  4385. //邮箱用户累计浏览次数
  4386. if userView, ok := userViewEmailTotalMap[item.Email]; ok {
  4387. //总共浏览的报表数据需要调整为(手机号用户浏览晨报数+邮箱号用户浏览晨报数)
  4388. list[i].ViewTotal += userView.Total
  4389. if userView.CreatedTime.After(list[i].LastViewTime) {
  4390. list[i].LastViewTime = userView.CreatedTime
  4391. list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  4392. }
  4393. }
  4394. if item.OpenId != "" {
  4395. list[i].IsRegister = true
  4396. } else {
  4397. if item.RegisterPlatform == 2 {
  4398. if item.Mobile != "" || item.Email != "" {
  4399. list[i].IsRegister = true
  4400. }
  4401. } else if item.RegisterTime != "" {
  4402. list[i].IsRegister = true
  4403. } else {
  4404. list[i].IsRegister = false
  4405. }
  4406. }
  4407. //如果存在手机号用户日评数据,那么需要做处理
  4408. if reportView, ok := userReportViewMobileTotalMap[item.Mobile]; ok {
  4409. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  4410. if reportView.LastViewTime.After(list[i].LastViewTime) {
  4411. list[i].LastViewTime = reportView.LastViewTime
  4412. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  4413. }
  4414. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  4415. list[i].ViewTotal += reportView.ViewTotal
  4416. }
  4417. //如果存在手机号用户日评数据,那么需要做处理
  4418. if reportView, ok := userReportViewEmailTotalMap[item.Email]; ok {
  4419. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  4420. if reportView.LastViewTime.After(list[i].LastViewTime) {
  4421. list[i].LastViewTime = reportView.LastViewTime
  4422. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  4423. }
  4424. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  4425. list[i].ViewTotal += reportView.ViewTotal
  4426. }
  4427. //如果存在手机号用户每日资讯,那么需要做处理
  4428. if advisoryView, ok := userAdvisoryViewMobileTotalMap[item.Mobile]; ok {
  4429. //fmt.Println("蝴蝶眨几次眼睛")
  4430. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4431. if advisoryView.CreatedTime.After(list[i].LastViewTime) {
  4432. list[i].LastViewTime = advisoryView.CreatedTime
  4433. list[i].LastViewTimeStr = advisoryView.CreatedTime.Format(utils.FormatDateTime)
  4434. }
  4435. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数)
  4436. list[i].ViewTotal += advisoryView.Total
  4437. }
  4438. //如果存在邮箱号用户每日资讯,那么需要做处理
  4439. if advisoryView, ok := userAdvisoryViewEmailTotalMap[item.Email]; ok {
  4440. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4441. if advisoryView.CreatedTime.After(list[i].LastViewTime) {
  4442. list[i].LastViewTime = advisoryView.CreatedTime
  4443. list[i].LastViewTimeStr = advisoryView.CreatedTime.Format(utils.FormatDateTime)
  4444. }
  4445. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数)
  4446. list[i].ViewTotal += advisoryView.Total
  4447. }
  4448. //如果存在手机号用户查研观向,那么需要做处理
  4449. if cygxView, ok := userCygxArticleHistoryRecordViewMobileTotalMap[item.Mobile]; ok {
  4450. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4451. if cygxView.CreatedTime.After(list[i].LastViewTime) {
  4452. list[i].LastViewTime = cygxView.CreatedTime
  4453. list[i].LastViewTimeStr = cygxView.CreatedTime.Format(utils.FormatDateTime)
  4454. }
  4455. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数+查研观向)
  4456. list[i].ViewTotal += cygxView.Total
  4457. }
  4458. //如果存在邮箱号用户查研观向,那么需要做处理
  4459. if cygxView, ok := userCygxViewEmailTotalMap[item.Email]; ok {
  4460. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4461. if cygxView.CreatedTime.After(list[i].LastViewTime) {
  4462. list[i].LastViewTime = cygxView.CreatedTime
  4463. list[i].LastViewTimeStr = cygxView.CreatedTime.Format(utils.FormatDateTime)
  4464. }
  4465. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数+查研观向)
  4466. list[i].ViewTotal += cygxView.Total
  4467. }
  4468. /*if reportView, ok := userReportViewTotalMap[int(item.UserId)]; ok {
  4469. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  4470. if reportView.LastViewTime.After(list[i].LastViewTime) {
  4471. list[i].LastViewTime = reportView.LastViewTime
  4472. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  4473. }
  4474. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  4475. list[i].ViewTotal += reportView.ViewTotal
  4476. }*/
  4477. //如果有绑定数据,那么将数据返回对应的注册绑定数据
  4478. if _, ok := userRecordRegisterMap[int(item.UserId)]; ok {
  4479. list[i].IsRegister = true
  4480. //list[i].RegisterTime = userRecordRegisterTime.Format(utils.FormatDateTime)
  4481. }
  4482. //是否共享联系人
  4483. //if userSellerRelationTotal, ok := userSellerRelationTotalMap[int(item.UserId)]; ok {
  4484. // //条数大于1,代表这是共享联系人
  4485. // if userSellerRelationTotal.Total > 1 {
  4486. // list[i].IsShared = true
  4487. // }
  4488. //}
  4489. }
  4490. }
  4491. dir, err := os.Executable()
  4492. exPath := filepath.Dir(dir)
  4493. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  4494. xlsxFile := xlsx.NewFile()
  4495. if err != nil {
  4496. br.Msg = "生成文件失败"
  4497. br.ErrMsg = "生成文件失败"
  4498. return
  4499. }
  4500. style := xlsx.NewStyle()
  4501. alignment := xlsx.Alignment{
  4502. Horizontal: "center",
  4503. Vertical: "center",
  4504. WrapText: true,
  4505. }
  4506. style.Alignment = alignment
  4507. style.ApplyAlignment = true
  4508. sheel, err := xlsxFile.AddSheet("联系人数据")
  4509. if err != nil {
  4510. br.Msg = "新增Sheet失败"
  4511. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  4512. return
  4513. }
  4514. sheel.SetColWidth(0, 0, 18)
  4515. sheel.SetColWidth(1, 2, 24)
  4516. sheel.SetColWidth(3, 3, 20)
  4517. sheel.SetColWidth(4, 4, 40)
  4518. titleRow := sheel.AddRow()
  4519. cellName := titleRow.AddCell()
  4520. cellName.SetStyle(style)
  4521. cellName.SetValue("姓名")
  4522. cellMobile := titleRow.AddCell()
  4523. cellMobile.SetStyle(style)
  4524. cellMobile.SetValue("手机号")
  4525. cellEmail := titleRow.AddCell()
  4526. cellEmail.SetStyle(style)
  4527. cellEmail.SetValue("邮箱")
  4528. cellMark := titleRow.AddCell()
  4529. cellMark.SetStyle(style)
  4530. cellMark.SetValue("是否决策人")
  4531. cellCompanyName := titleRow.AddCell()
  4532. cellCompanyName.SetStyle(style)
  4533. cellCompanyName.SetValue("客户名称")
  4534. for _, v := range list {
  4535. dataRow := sheel.AddRow()
  4536. dataRow.SetHeight(20)
  4537. isMark := "否"
  4538. if v.IsMaker == 1 {
  4539. isMark = "是"
  4540. }
  4541. cellDataName := dataRow.AddCell()
  4542. cellDataName.SetStyle(style)
  4543. cellDataName.SetValue(v.RealName)
  4544. cellDataMobile := dataRow.AddCell()
  4545. cellDataMobile.SetStyle(style)
  4546. cellDataMobile.SetValue(v.Mobile)
  4547. cellDataEmail := dataRow.AddCell()
  4548. cellDataEmail.SetStyle(style)
  4549. cellDataEmail.SetValue(v.Email)
  4550. cellDataMark := dataRow.AddCell()
  4551. cellDataMark.SetStyle(style)
  4552. cellDataMark.SetValue(isMark)
  4553. cellDataCompanyName := dataRow.AddCell()
  4554. cellDataCompanyName.SetStyle(style)
  4555. cellDataCompanyName.SetValue(companyInfo.CompanyName)
  4556. }
  4557. err = xlsxFile.Save(downLoadnFilePath)
  4558. if err != nil {
  4559. br.Msg = "保存文件失败"
  4560. br.ErrMsg = "保存文件失败"
  4561. return
  4562. }
  4563. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  4564. downloadFileName := "联系人列表_" + randStr + ".xlsx"
  4565. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  4566. defer func() {
  4567. os.Remove(downLoadnFilePath)
  4568. }()
  4569. br.Ret = 200
  4570. br.Success = true
  4571. br.Msg = "导出成功"
  4572. }
  4573. // 检测产品
  4574. func checkProductListUser(item models.WxUserDetail, companyUser *company.CompanyUser, companyProductList []*company.CompanyProduct) (notRelationUserMap map[int]*company.CompanyUser, err error) {
  4575. notRelationUserMap = make(map[int]*company.CompanyUser)
  4576. //判断该用户与当前账号是否存在关系,如果没有关系,那么直接返回
  4577. for _, companyProduct := range companyProductList {
  4578. _, relationErr := models.GetUserSellerRelation(companyUser.UserId, companyProduct.SellerId)
  4579. //找到数据了
  4580. if relationErr == nil {
  4581. continue
  4582. }
  4583. //sql异常
  4584. if relationErr.Error() != utils.ErrNoRow() {
  4585. err = relationErr
  4586. return
  4587. }
  4588. //没找到数据,不存在关系
  4589. //如果不存在关系,那么直接添加关系就好了,其他操作不做
  4590. if relationErr.Error() == utils.ErrNoRow() {
  4591. if item.Mobile != "" && companyUser.Mobile == "" {
  4592. companyUser.Mobile = strings.Replace(item.Mobile, " ", "", -1)
  4593. }
  4594. if item.Email != "" && companyUser.Email == "" {
  4595. companyUser.Email = strings.Replace(companyUser.Email, " ", "", -1)
  4596. }
  4597. companyUser.SellerId = companyProduct.SellerId
  4598. companyUser.SellerName = companyProduct.SellerName
  4599. companyUser.DepartmentId = companyProduct.DepartmentId
  4600. companyUser.GroupId = companyProduct.GroupId
  4601. notRelationUserMap[companyProduct.ProductId] = companyUser
  4602. }
  4603. }
  4604. return
  4605. }
  4606. // @Title 下载模板
  4607. // @Description 下载模板
  4608. // @Success 200
  4609. // @router /template [get]
  4610. func (this *CompanyUserController) TemplateDownload() {
  4611. br := new(models.BaseResponse).Init()
  4612. defer func() {
  4613. this.Data["json"] = br
  4614. this.ServeJSON()
  4615. }()
  4616. this.Ctx.Output.Download("./static/导入联系人模板.xlsx", "导入联系人模板.xlsx")
  4617. br.Ret = 200
  4618. br.Success = true
  4619. br.Msg = "下载成功"
  4620. }
  4621. //百度
  4622. //func init() {
  4623. // fmt.Println("start")
  4624. // businessCardUrl := `https://hongze.oss-accelerate.aliyuncs.com/static/images/202101/20210111/wB3ELOuH8AxTvrIDXHaFTn9Z0WXB.jpg`
  4625. // services.GetBdGeneralBasic(businessCardUrl)
  4626. // fmt.Println("end")
  4627. //}
  4628. //阿里云
  4629. //func init() {
  4630. // fmt.Println("start")
  4631. // businessCardUrl := `https://hongze.oss-accelerate.aliyuncs.com/static/images/202101/20210111/wB3ELOuH8AxTvrIDXHaFTn9Z0WXB.jpg`
  4632. // aliyunResult, err := services.AliyunBusinessCardOcr(businessCardUrl)
  4633. //
  4634. // if err != nil {
  4635. // fmt.Println("line 1026")
  4636. // return
  4637. // }
  4638. // if !aliyunResult.Success {
  4639. // fmt.Println("line 1026")
  4640. // return
  4641. // }
  4642. //
  4643. // mobileStr := strings.Join(aliyunResult.TelCell, ",")
  4644. // mobileOne:="18366208402"
  4645. // if strings.Contains(mobileStr, mobileOne) {
  4646. // fmt.Println("true")
  4647. // } else {
  4648. // fmt.Println("false")
  4649. // }
  4650. // fmt.Println("end")
  4651. //}
  4652. // 获取是否拥有操作权限
  4653. func checkOpCompanyUserByUserId(userId, productId, opId int, roleTypeCode string) (errMsg string, err error) {
  4654. userInfo, userErr := models.GetWxUserByUserId(userId)
  4655. if userErr != nil {
  4656. errMsg = "查询用户信息失败"
  4657. err = errors.New("查询用户信息失败,Err:" + userErr.Error())
  4658. return
  4659. }
  4660. //产品权限
  4661. //获取当前操作员产品权限
  4662. companyProductList, productErr := getSysCompanyProductList(productId, userInfo.CompanyId)
  4663. if productErr != nil {
  4664. errMsg = "获取当前操作员产品权限异常"
  4665. err = errors.New("删除失败,获取当前操作员产品权限异常,Err:" + productErr.Error())
  4666. return
  4667. }
  4668. if companyProductList == nil {
  4669. errMsg = "删除失败"
  4670. err = errors.New("删除失败,获取当前操作员找不到产品权限")
  4671. return
  4672. }
  4673. item, err := company.GetCompanyDetailById(userInfo.CompanyId)
  4674. if err != nil {
  4675. return
  4676. }
  4677. //操作权限校验
  4678. for _, v := range companyProductList {
  4679. companyUserStatus := services.CheckCompanyUserButton(roleTypeCode, v.SellerId, opId, v.ProductId, item.ShareSellerId, v.Status)
  4680. if companyUserStatus == false {
  4681. errMsg = "没有操作权限"
  4682. err = errors.New("没有操作权限")
  4683. return
  4684. }
  4685. }
  4686. return
  4687. }
  4688. // 获取是否拥有操作权限
  4689. func checkOpCompanyUserByCompanyId(companyId, productId, opId int, roleTypeCode string) (errMsg string, err error) {
  4690. //获取当前操作员产品权限
  4691. companyProductList, productErr := getSysCompanyProductList(productId, companyId)
  4692. if productErr != nil {
  4693. errMsg = "获取当前操作员产品权限异常"
  4694. err = errors.New("删除失败,获取当前操作员产品权限异常,Err:" + productErr.Error())
  4695. return
  4696. }
  4697. if companyProductList == nil {
  4698. errMsg = "删除失败"
  4699. err = errors.New("删除失败,获取当前操作员找不到产品权限")
  4700. return
  4701. }
  4702. item, err := company.GetCompanyDetailById(companyId)
  4703. if err != nil {
  4704. return
  4705. }
  4706. //操作权限校验
  4707. for _, v := range companyProductList {
  4708. companyUserStatus := services.CheckCompanyUserButton(roleTypeCode, v.SellerId, opId, v.ProductId, item.ShareSellerId, v.Status)
  4709. if companyUserStatus == false {
  4710. errMsg = "没有操作权限"
  4711. err = errors.New("没有操作权限")
  4712. return
  4713. }
  4714. }
  4715. return
  4716. }
  4717. // @Title FICC申请单-移动潜在用户
  4718. // @Description FICC申请单移动潜在用户接口
  4719. // @Param request body models.ApplyRecordPotentialUserMoveReq true "type json string"
  4720. // @Success Ret=200 移动成功
  4721. // @router /potential/user/moveByApplyRecord [post]
  4722. func (this *CompanyController) ApplyRecordPotentialUserMove() {
  4723. br := new(models.BaseResponse).Init()
  4724. defer func() {
  4725. this.Data["json"] = br
  4726. this.ServeJSON()
  4727. }()
  4728. sysUser := this.SysUser
  4729. if sysUser == nil {
  4730. br.Msg = "请登录"
  4731. br.ErrMsg = "请登录,SysUser Is Empty"
  4732. br.Ret = 408
  4733. return
  4734. }
  4735. var req models.ApplyRecordPotentialUserMoveReq
  4736. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  4737. if err != nil {
  4738. br.Msg = "参数解析异常!"
  4739. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  4740. return
  4741. }
  4742. if req.UserId <= 0 {
  4743. br.Msg = "参数错误"
  4744. br.ErrMsg = "客户id错误"
  4745. return
  4746. }
  4747. wxUser, err := models.GetWxUserByUserId(req.UserId)
  4748. //如果移动客户编号小于等于0,那么移动到潜在客户
  4749. if req.CompanyId <= 0 {
  4750. req.CompanyId = 1
  4751. }
  4752. if err != nil {
  4753. br.Msg = "移动失败,该潜在用户已被删除"
  4754. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  4755. return
  4756. }
  4757. if req.Mobile == "" {
  4758. req.Mobile = wxUser.Mobile
  4759. }
  4760. if req.Email == "" {
  4761. req.Email = wxUser.Email
  4762. }
  4763. if req.Mobile == "" && req.Email == "" {
  4764. br.Msg = "手机号和邮箱不能同时为空"
  4765. return
  4766. }
  4767. //校验手机号
  4768. if req.Mobile != "" && wxUser.Mobile != req.Mobile {
  4769. count, err := models.CheckUserMobileIsRegister(req.UserId, req.Mobile)
  4770. if err != nil {
  4771. br.Msg = "校验数据失败"
  4772. br.ErrMsg = "校验数据失败,Err:" + err.Error()
  4773. return
  4774. }
  4775. if count > 0 {
  4776. br.Msg = "该手机号已经注册过用户"
  4777. return
  4778. }
  4779. }
  4780. //校验邮箱
  4781. if req.Email != "" && wxUser.Email != req.Email {
  4782. if !utils.ValidateEmailFormatat(req.Email) {
  4783. br.Msg = "请输入正确的邮箱(包含@)"
  4784. return
  4785. }
  4786. count, err := models.CheckUserEmailIsRegister(req.UserId, req.Email)
  4787. if err != nil {
  4788. br.Msg = "校验数据失败"
  4789. br.ErrMsg = "校验数据失败,Err:" + err.Error()
  4790. return
  4791. }
  4792. if count > 0 {
  4793. br.Msg = "该邮箱已经注册过用户"
  4794. br.IsSendEmail = false
  4795. return
  4796. }
  4797. }
  4798. updateCol := make([]string, 0)
  4799. if wxUser.OutboundMobile == "" || wxUser.Mobile == wxUser.OutboundMobile {
  4800. wxUser.OutboundMobile = req.Mobile
  4801. wxUser.OutboundCountryCode = req.CountryCode
  4802. updateCol = append(updateCol, "OutboundMobile", "OutboundCountryCode")
  4803. }
  4804. if req.Email != "" {
  4805. wxUser.Email = req.Email
  4806. updateCol = append(updateCol, "Email")
  4807. }
  4808. if req.RealName != "" {
  4809. wxUser.RealName = req.RealName
  4810. updateCol = append(updateCol, "RealName")
  4811. }
  4812. if req.Mobile != "" {
  4813. wxUser.Mobile = req.Mobile
  4814. updateCol = append(updateCol, "Mobile")
  4815. }
  4816. if req.CountryCode != "" {
  4817. wxUser.CountryCode = req.CountryCode
  4818. updateCol = append(updateCol, "CountryCode")
  4819. }
  4820. if req.Position != "" {
  4821. wxUser.Position = req.Position
  4822. updateCol = append(updateCol, "Position")
  4823. }
  4824. if wxUser.DepartmentName == "" && req.DepartmentName != "" {
  4825. wxUser.DepartmentName = req.DepartmentName
  4826. updateCol = append(updateCol, "DepartmentName")
  4827. }
  4828. wxUser.Sex = req.Sex
  4829. wxUser.IsMaker = req.IsMaker
  4830. updateCol = append(updateCol, "Sex", "IsMaker")
  4831. //名片匹配
  4832. if req.BusinessCardUrl != "" {
  4833. //if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  4834. // 获取图片识别手机号的配置
  4835. crmConfig2, err := company.GetConfigDetailByCode("card_recognition_tel")
  4836. if err != nil {
  4837. br.Msg = "获取配置失败"
  4838. br.ErrMsg = "获取配置失败"
  4839. br.IsSendEmail = false
  4840. return
  4841. }
  4842. if crmConfig2.ConfigValue == "true" {
  4843. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  4844. if err != nil {
  4845. br.Msg = "名片识别失败"
  4846. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  4847. return
  4848. }
  4849. isFlag := false
  4850. if len(card.WordsResult.MOBILE) > 0 {
  4851. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  4852. //if req.Mobile != "" {
  4853. // if strings.Contains(mobileStr, req.Mobile) || mobileStr == "" {
  4854. // isFlag = true
  4855. // } else {
  4856. // isFlag = false
  4857. // }
  4858. //}
  4859. if req.Mobile != "" && strings.Contains(mobileStr, req.Mobile) {
  4860. isFlag = true
  4861. }
  4862. }
  4863. if !isFlag {
  4864. //阿里云识别
  4865. //if utils.RunMode == "release" {
  4866. aliyunResult, errMsg, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
  4867. if err != nil {
  4868. br.Msg = "识别失败"
  4869. br.ErrMsg = "识别失败,Err:" + err.Error()
  4870. return
  4871. }
  4872. if !aliyunResult.Success {
  4873. br.Msg = "识别失败"
  4874. br.ErrMsg = "识别失败"
  4875. return
  4876. }
  4877. if errMsg != "" {
  4878. br.Msg = errMsg
  4879. return
  4880. }
  4881. if len(aliyunResult.TelCell) > 0 {
  4882. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  4883. if req.Mobile != "" {
  4884. if strings.Contains(mobileStr, req.Mobile) {
  4885. isFlag = true
  4886. } else {
  4887. isFlag = false
  4888. }
  4889. }
  4890. } else {
  4891. isFlag = true
  4892. }
  4893. //}
  4894. }
  4895. if !isFlag && crmConfig2.ConfigValue == "true" {
  4896. br.Msg = "名片信息与联系人手机号不符"
  4897. return
  4898. }
  4899. }
  4900. wxUser.BusinessCardUrl = req.BusinessCardUrl
  4901. updateCol = append(updateCol, "BusinessCardUrl")
  4902. }
  4903. productId := services.GetProductId(sysUser.RoleTypeCode)
  4904. companyProductList, _ := getSysCompanyProductList(productId, req.CompanyId)
  4905. err = services.MovePotentialUserByApplyRecord(sysUser.AdminId, req.UserId, req.CompanyId, req.ApplyRecordId, req.RealName, req.Mobile, req.Email, req.Remark, companyProductList, wxUser, updateCol)
  4906. if err != nil {
  4907. br.Msg = "移动失败"
  4908. br.ErrMsg = "移动失败,Err:" + err.Error()
  4909. return
  4910. }
  4911. //更新客户最近阅读次数以及最近阅读时间
  4912. go services.ModifyCompanyProductLastViewData([]int{req.CompanyId})
  4913. br.Ret = 200
  4914. br.Success = true
  4915. br.Msg = "移动成功"
  4916. br.IsAddLog = true
  4917. }
  4918. // @Title 新增联系人图表分类权限
  4919. // @Description 新增联系人图表分类权限接口
  4920. // @Param request body request.SaveCompanyUserChartClassifyPermissionReq true "type json string"
  4921. // @Success 200 Ret=200 操作成功
  4922. // @router /user/addChartClassifyPermission [post]
  4923. func (this *CompanyUserController) AddCompanyUserChartClassifyPermission() {
  4924. br := new(models.BaseResponse).Init()
  4925. defer func() {
  4926. this.Data["json"] = br
  4927. this.ServeJSON()
  4928. }()
  4929. sysUser := this.SysUser
  4930. if sysUser == nil {
  4931. br.Msg = "请登录"
  4932. br.ErrMsg = "请登录,SysUser Is Empty"
  4933. br.Ret = 408
  4934. return
  4935. }
  4936. var req request.SaveCompanyUserChartClassifyPermissionReq
  4937. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  4938. if err != nil {
  4939. br.Msg = "参数解析异常!"
  4940. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  4941. return
  4942. }
  4943. if req.UserId <= 0 {
  4944. br.Msg = "参数丢失:联系人ID"
  4945. br.ErrMsg = "参数丢失,联系人ID"
  4946. return
  4947. }
  4948. if req.StartTime == "" || req.EndTime == "" {
  4949. br.Msg = "请选择开始结束时间"
  4950. br.ErrMsg = "请选择开始结束时间"
  4951. br.IsSendEmail = false
  4952. return
  4953. }
  4954. startTime, err := time.Parse(utils.FormatDate, req.StartTime)
  4955. if err != nil {
  4956. br.Msg = "开始时间格式有误"
  4957. br.ErrMsg = "开始时间转换失败, Err:" + err.Error()
  4958. return
  4959. }
  4960. endTime, err := time.Parse(utils.FormatDate, req.EndTime)
  4961. if err != nil {
  4962. br.Msg = "结束时间格式有误"
  4963. br.ErrMsg = "结束时间转换失败, Err:" + err.Error()
  4964. return
  4965. }
  4966. if startTime.After(endTime) {
  4967. br.Msg = "结束时间不能小于开始时间"
  4968. br.ErrMsg = "结束时间不能小于开始时间"
  4969. br.IsSendEmail = false
  4970. return
  4971. }
  4972. _, err = company_user.GetChartClassifyPermissionByUserId(req.UserId)
  4973. if err == nil {
  4974. br.Msg = "请勿重复设置图表权限"
  4975. br.ErrMsg = "请勿重复设置图表权限"
  4976. br.IsSendEmail = false
  4977. return
  4978. }
  4979. // 判断是否允许设置
  4980. userInfo, err := models.GetWxUserByUserId(req.UserId)
  4981. if err != nil {
  4982. br.Msg = "联系人信息有误"
  4983. br.ErrMsg = "联系人信息有误, Err: " + err.Error()
  4984. return
  4985. }
  4986. companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
  4987. if err != nil {
  4988. br.Msg = "获取客户信息失败"
  4989. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  4990. return
  4991. }
  4992. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(userInfo.CompanyId, 1)
  4993. if err != nil {
  4994. br.Msg = "获取客户产品信息失败"
  4995. br.ErrMsg = "获取客户产品信息失败, Err: " + err.Error()
  4996. return
  4997. }
  4998. companyStatus := companyProductInfo.Status
  4999. if companyStatus != utils.COMPANY_STATUS_FOREVER {
  5000. // 非永续客户
  5001. if companyStatus == utils.COMPANY_STATUS_TRY_OUT || companyStatus == utils.COMPANY_STATUS_FORMAL {
  5002. // 正式/试用
  5003. expireTimeStr := companyProductInfo.EndDate
  5004. expireTime, _ := time.ParseInLocation(utils.FormatDate, expireTimeStr, time.Local)
  5005. // 服务结束时间小于设置的结束时间则不允许设置
  5006. if endTime.After(expireTime.AddDate(0, 0, 1)) {
  5007. br.Msg = "终止日期不允许超过服务日期, 服务终止日期为: " + expireTimeStr
  5008. br.IsSendEmail = false
  5009. return
  5010. }
  5011. } else {
  5012. br.Msg = "当前客户不允许设置图表权限, 客户状态为: " + companyStatus
  5013. br.IsSendEmail = false
  5014. return
  5015. }
  5016. }
  5017. item := &company_user.ChartClassifyPermission{
  5018. UserId: req.UserId,
  5019. CompanyId: companyInfo.CompanyId,
  5020. StartTime: startTime,
  5021. EndTime: endTime,
  5022. Enabled: req.Enabled,
  5023. CreateTime: time.Now(),
  5024. }
  5025. _, err = company_user.AddChartClassifyPermission(item)
  5026. if err != nil {
  5027. br.Msg = "新增图表权限失败"
  5028. br.ErrMsg = "新增图表权限失败, Err:" + err.Error()
  5029. return
  5030. }
  5031. // 操作记录
  5032. {
  5033. record := &company_user.CompanyUserChartPermissionOperationRecord{
  5034. Type: 1,
  5035. CompanyId: companyInfo.CompanyId,
  5036. UserId: req.UserId,
  5037. AdminId: sysUser.AdminId,
  5038. StartTime: startTime,
  5039. EndTime: endTime,
  5040. Enabled: req.Enabled,
  5041. CreateTime: time.Now(),
  5042. }
  5043. go company_user.AddChartPermissionOperationRecord(record)
  5044. }
  5045. br.Ret = 200
  5046. br.Success = true
  5047. br.Msg = "操作成功"
  5048. }
  5049. // @Title 编辑联系人图表分类权限
  5050. // @Description 编辑联系人图表分类权限接口
  5051. // @Param request body request.SaveCompanyUserChartClassifyPermissionReq true "type json string"
  5052. // @Success 200 Ret=200 操作成功
  5053. // @router /user/editChartClassifyPermission [post]
  5054. func (this *CompanyUserController) EditCompanyUserChartClassifyPermission() {
  5055. br := new(models.BaseResponse).Init()
  5056. defer func() {
  5057. this.Data["json"] = br
  5058. this.ServeJSON()
  5059. }()
  5060. sysUser := this.SysUser
  5061. if sysUser == nil {
  5062. br.Msg = "请登录"
  5063. br.ErrMsg = "请登录,SysUser Is Empty"
  5064. br.Ret = 408
  5065. return
  5066. }
  5067. var req request.SaveCompanyUserChartClassifyPermissionReq
  5068. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  5069. if err != nil {
  5070. br.Msg = "参数解析异常!"
  5071. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  5072. return
  5073. }
  5074. if req.UserId <= 0 {
  5075. br.Msg = "参数丢失:联系人ID"
  5076. br.ErrMsg = "参数丢失,联系人ID"
  5077. return
  5078. }
  5079. if req.StartTime == "" || req.EndTime == "" {
  5080. br.Msg = "请选择开始结束时间"
  5081. br.ErrMsg = "请选择开始结束时间"
  5082. br.IsSendEmail = false
  5083. return
  5084. }
  5085. startTime, err := time.Parse(utils.FormatDate, req.StartTime)
  5086. if err != nil {
  5087. br.Msg = "开始时间格式有误"
  5088. br.ErrMsg = "开始时间转换失败, Err:" + err.Error()
  5089. return
  5090. }
  5091. endTime, err := time.Parse(utils.FormatDate, req.EndTime)
  5092. if err != nil {
  5093. br.Msg = "结束时间格式有误"
  5094. br.ErrMsg = "结束时间转换失败, Err:" + err.Error()
  5095. return
  5096. }
  5097. if startTime.After(endTime) {
  5098. br.Msg = "结束时间不能小于开始时间"
  5099. br.ErrMsg = "结束时间不能小于开始时间"
  5100. br.IsSendEmail = false
  5101. return
  5102. }
  5103. // 判断是否允许设置
  5104. userInfo, err := models.GetWxUserByUserId(req.UserId)
  5105. if err != nil {
  5106. br.Msg = "联系人信息有误"
  5107. br.ErrMsg = "联系人信息有误, Err: " + err.Error()
  5108. return
  5109. }
  5110. companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
  5111. if err != nil {
  5112. br.Msg = "获取客户信息失败"
  5113. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  5114. return
  5115. }
  5116. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(userInfo.CompanyId, 1)
  5117. if err != nil {
  5118. br.Msg = "获取客户产品信息失败"
  5119. br.ErrMsg = "获取客户产品信息失败, Err: " + err.Error()
  5120. return
  5121. }
  5122. companyStatus := companyProductInfo.Status
  5123. if companyStatus != utils.COMPANY_STATUS_FOREVER {
  5124. // 非永续客户
  5125. if companyStatus == utils.COMPANY_STATUS_TRY_OUT || companyStatus == utils.COMPANY_STATUS_FORMAL {
  5126. // 正式/试用
  5127. expireTimeStr := companyProductInfo.EndDate
  5128. expireTime, _ := time.ParseInLocation(utils.FormatDate, expireTimeStr, time.Local)
  5129. // 服务结束时间小于设置的结束时间则不允许设置
  5130. if endTime.After(expireTime.AddDate(0, 0, 1)) {
  5131. br.Msg = "终止日期不允许超过服务日期, 服务终止日期为: " + expireTimeStr
  5132. br.IsSendEmail = false
  5133. return
  5134. }
  5135. } else {
  5136. br.Msg = "当前客户不允许设置图表权限, 客户状态为: " + companyStatus
  5137. br.IsSendEmail = false
  5138. return
  5139. }
  5140. }
  5141. permissionInfo, err := company_user.GetChartClassifyPermissionByUserId(req.UserId)
  5142. if err != nil {
  5143. br.Msg = "获取联系人图表分类权限失败"
  5144. br.ErrMsg = "获取联系人图表分类权限失败, Err:" + err.Error()
  5145. return
  5146. }
  5147. permissionInfo.CompanyId = companyInfo.CompanyId
  5148. permissionInfo.Enabled = req.Enabled
  5149. permissionInfo.StartTime = startTime
  5150. permissionInfo.EndTime = endTime
  5151. permissionInfo.UpdateTime = time.Now()
  5152. err = permissionInfo.Update([]string{"CompanyId", "Enabled", "StartTime", "EndTime", "UpdateTime"})
  5153. if err != nil {
  5154. br.Msg = "编辑联系人图表分类权限失败"
  5155. br.ErrMsg = "编辑联系人图表分类权限失败, Err:" + err.Error()
  5156. return
  5157. }
  5158. // 操作记录
  5159. {
  5160. record := &company_user.CompanyUserChartPermissionOperationRecord{
  5161. Type: 2,
  5162. CompanyId: companyInfo.CompanyId,
  5163. UserId: req.UserId,
  5164. AdminId: sysUser.AdminId,
  5165. StartTime: startTime,
  5166. EndTime: endTime,
  5167. Enabled: req.Enabled,
  5168. CreateTime: time.Now(),
  5169. }
  5170. go company_user.AddChartPermissionOperationRecord(record)
  5171. }
  5172. br.Ret = 200
  5173. br.Success = true
  5174. br.Msg = "操作成功"
  5175. }
  5176. // @Title 获取联系人图表分类权限详情
  5177. // @Description 获取联系人图表分类权限详情接口
  5178. // @Param UserId query int true "联系人ID,必填"
  5179. // @Success 200 {object} response.CompanyUserChartClassifyPermissionDetailResp
  5180. // @router /user/getChartClassifyPermissionDetail [get]
  5181. func (this *CompanyUserController) GetCompanyUserChartClassifyPermissionDetail() {
  5182. br := new(models.BaseResponse).Init()
  5183. defer func() {
  5184. this.Data["json"] = br
  5185. this.ServeJSON()
  5186. }()
  5187. sysUser := this.SysUser
  5188. if sysUser == nil {
  5189. br.Msg = "请登录"
  5190. br.ErrMsg = "请登录,SysUser Is Empty"
  5191. br.Ret = 408
  5192. return
  5193. }
  5194. reqUserId := this.GetString("UserId", "")
  5195. userId, _ := strconv.Atoi(reqUserId)
  5196. if userId <= 0 {
  5197. br.Msg = "参数丢失:联系人ID"
  5198. return
  5199. }
  5200. userInfo, err := models.GetWxUserByUserId(userId)
  5201. if err != nil {
  5202. br.Msg = "联系人信息有误"
  5203. br.ErrMsg = "联系人信息有误, Err: " + err.Error()
  5204. return
  5205. }
  5206. permissionInfo, err := company_user.GetChartClassifyPermissionByUserId(userId)
  5207. if err != nil && err.Error() != utils.ErrNoRow() {
  5208. br.Msg = "获取联系人图表分类权限失败"
  5209. br.ErrMsg = "获取联系人图表分类权限失败, Err:" + err.Error()
  5210. return
  5211. }
  5212. resp := new(response.CompanyUserChartClassifyPermissionDetailResp)
  5213. // 客户服务日期
  5214. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(userInfo.CompanyId, 1)
  5215. if err != nil {
  5216. br.Msg = "获取客户产品信息失败"
  5217. br.ErrMsg = "获取客户产品信息失败, Err: " + err.Error()
  5218. return
  5219. }
  5220. companyStatus := companyProductInfo.Status
  5221. // 0-不允许设置 1-允许设置最大90天 2-90天
  5222. nowTime := time.Now()
  5223. free := 0
  5224. if companyStatus == utils.COMPANY_STATUS_FOREVER {
  5225. // 永续客户
  5226. free = 2
  5227. resp.ExpireDay = "2199-12-31"
  5228. } else if companyStatus == utils.COMPANY_STATUS_TRY_OUT || companyStatus == utils.COMPANY_STATUS_FORMAL {
  5229. free = 1
  5230. resp.ExpireDay = companyProductInfo.EndDate
  5231. } else {
  5232. resp.ExpireDay = nowTime.Format(utils.FormatDate)
  5233. }
  5234. var startTime, endTime time.Time
  5235. if permissionInfo != nil {
  5236. // 已设置
  5237. startTime = permissionInfo.StartTime
  5238. endTime = permissionInfo.EndTime
  5239. resp.ChartPermissionId = permissionInfo.Id
  5240. resp.UserId = permissionInfo.UserId
  5241. resp.StartTime = permissionInfo.StartTime.Format(utils.FormatDate)
  5242. resp.EndTime = permissionInfo.EndTime.Format(utils.FormatDate)
  5243. resp.Enabled = permissionInfo.Enabled
  5244. } else {
  5245. // 初次设置
  5246. startTime = nowTime
  5247. endTime = startTime.Add(89 * 24 * time.Hour) // 实际验证时以结束日期的23时59分为过期时间所以此处日期-1
  5248. if free == 0 {
  5249. endTime = nowTime
  5250. } else if free == 1 {
  5251. expireTime, _ := time.ParseInLocation(utils.FormatDate, companyProductInfo.EndDate, time.Local)
  5252. sub := expireTime.Sub(nowTime)
  5253. diff := int(sub.Hours() / 24)
  5254. if diff < 90 {
  5255. // 小于90天则终止时间为服务结束时间
  5256. endTime = expireTime
  5257. }
  5258. }
  5259. resp.StartTime = startTime.Format(utils.FormatDate)
  5260. resp.EndTime = endTime.Format(utils.FormatDate)
  5261. resp.UserId = userId
  5262. resp.Enabled = 0 // 默认关闭
  5263. }
  5264. // 计算阅读时长天数
  5265. subDuration := endTime.Sub(startTime)
  5266. subHours := subDuration.Hours()
  5267. subDays := int(subHours / 24)
  5268. resp.DayDuration = subDays + 1
  5269. br.Ret = 200
  5270. br.Success = true
  5271. br.Msg = "获取成功"
  5272. br.Data = resp
  5273. }
  5274. // GetOtherProduct
  5275. // @Title 获取其他分产品的筛选列表
  5276. // @Description 获取联系人图表分类权限详情接口
  5277. // @Param UserId query int true "联系人ID,必填"
  5278. // @Success 200 {object} response.CompanyUserChartClassifyPermissionDetailResp
  5279. // @router /user/otherProductList [get]
  5280. func (this *CompanyUserController) GetOtherProduct() {
  5281. br := new(models.BaseResponse).Init()
  5282. defer func() {
  5283. this.Data["json"] = br
  5284. this.ServeJSON()
  5285. }()
  5286. sysUser := this.SysUser
  5287. if sysUser == nil {
  5288. br.Msg = "请登录"
  5289. br.ErrMsg = "请登录,SysUser Is Empty"
  5290. br.Ret = 408
  5291. return
  5292. }
  5293. resp := make([]response.OtherProductTypeListResp, 0)
  5294. statisticFlag, _ := this.GetBool("StatisticFlag")
  5295. questionItem := response.OtherProductTypeListResp{
  5296. ProductId: 0,
  5297. ProductType: 3,
  5298. ProductName: "问答社区",
  5299. }
  5300. roadVideoItem := response.OtherProductTypeListResp{
  5301. ProductId: 2,
  5302. ProductType: 2,
  5303. ProductName: "线上路演",
  5304. }
  5305. videoItem := response.OtherProductTypeListResp{
  5306. ProductId: 1,
  5307. ProductType: 2,
  5308. ProductName: "5分钟小视频",
  5309. }
  5310. if !statisticFlag {
  5311. resp = append(resp, questionItem, roadVideoItem, videoItem)
  5312. }
  5313. voiceSectionList, err := yb.GetAllEnableVoiceSectionList()
  5314. if err != nil {
  5315. br.Msg = "获取数据失败"
  5316. br.ErrMsg = "获取语音播报版块, Err: " + err.Error()
  5317. return
  5318. }
  5319. for _, v := range voiceSectionList {
  5320. resp = append(resp, response.OtherProductTypeListResp{
  5321. ProductId: v.SectionId,
  5322. ProductType: 1,
  5323. ProductName: v.SectionName,
  5324. })
  5325. }
  5326. if statisticFlag {
  5327. resp = append(resp, questionItem, roadVideoItem, videoItem)
  5328. }
  5329. // 全部
  5330. resp = append(resp, response.OtherProductTypeListResp{
  5331. ProductId: 0,
  5332. ProductType: 99,
  5333. ProductName: "全部",
  5334. })
  5335. br.Ret = 200
  5336. br.Success = true
  5337. br.Msg = "获取成功"
  5338. br.Data = resp
  5339. }
  5340. //func init() {
  5341. // BusinessCardUrl := `https://hongze.oss-accelerate.aliyuncs.com/static/images/202107/20210715/R5xT6jw331gPwHZlsIg90cuSbuFW.jpeg`
  5342. // BusinessCardUrl = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202102/20210224/Bdr3Dv8oZDvsAnkzUpx8uA3S4odB.jpg`
  5343. // BusinessCardUrl = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202202/20220215/0glY7kgE07OLBHTnxxwS6u2cAxRx.png`
  5344. // BusinessCardUrl = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202206/20220614/ZHEUpbNtUfE09ZVp6qfQxSPUrLfS.jpg`
  5345. // card, err := services.GetBusinessCard(BusinessCardUrl)
  5346. // if err != nil {
  5347. // fmt.Println("名片识别失败,Err:" + err.Error())
  5348. // return
  5349. // }
  5350. // fmt.Println(card)
  5351. //
  5352. // fmt.Println("company:", card.WordsResult.COMPANY)
  5353. // fmt.Println("===================")
  5354. //
  5355. // {
  5356. // aliyunResult, err := services.AliyunBusinessCardOcr(BusinessCardUrl)
  5357. // if err != nil {
  5358. // fmt.Println("名片识别失败2,Err:" + err.Error())
  5359. // return
  5360. // }
  5361. // fmt.Println(aliyunResult)
  5362. // fmt.Println("company:", aliyunResult.Company)
  5363. // }
  5364. //
  5365. //}
  5366. // @Title 联系人名单匹配导入
  5367. // @Description 联系人名单匹配导入
  5368. // @Param File query file true "文件"
  5369. // @Success 200 {object} company.UserImportMatchResp
  5370. // @router /list/match/import [post]
  5371. func (this *CompanyUserController) ImportListMatch() {
  5372. br := new(models.BaseResponse).Init()
  5373. defer func() {
  5374. this.Data["json"] = br
  5375. this.ServeJSON()
  5376. }()
  5377. sysUser := this.SysUser
  5378. if sysUser == nil {
  5379. br.Msg = "请重新登录"
  5380. return
  5381. }
  5382. file, h, err := this.GetFile("File")
  5383. if err != nil {
  5384. br.Msg = "获取文件失败"
  5385. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  5386. return
  5387. }
  5388. defer func() {
  5389. _ = file.Close()
  5390. }()
  5391. ext := path.Ext(h.Filename)
  5392. if ext != ".xlsx" && ext != ".xls" {
  5393. br.Msg = "请上传Excel文件"
  5394. return
  5395. }
  5396. uploadDir := "static/xls"
  5397. err = os.MkdirAll(uploadDir, 766)
  5398. if err != nil {
  5399. br.Msg = "存储目录创建失败"
  5400. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  5401. return
  5402. }
  5403. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  5404. err = this.SaveToFile("File", uploadPath)
  5405. if err != nil {
  5406. br.Msg = "文件保存失败"
  5407. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  5408. return
  5409. }
  5410. defer func() {
  5411. _ = os.Remove(uploadPath)
  5412. }()
  5413. xlFile, err := xlsx.OpenFile(uploadPath)
  5414. if err != nil {
  5415. br.Msg = "文件导入失败"
  5416. br.ErrMsg = "文件导入失败,Err:" + err.Error()
  5417. return
  5418. }
  5419. dataList := make([]*company.UserImportMatch, 0)
  5420. mobileArr := make([]string, 0)
  5421. // 遍历sheet页读取
  5422. for _, sheet := range xlFile.Sheets {
  5423. // 遍历行读取
  5424. maxRow := sheet.MaxRow
  5425. for i := 0; i < maxRow; i++ {
  5426. // 首行
  5427. if i == 0 {
  5428. row := sheet.Row(i)
  5429. cells := row.Cells
  5430. for k, cell := range cells {
  5431. text := cell.String()
  5432. if k == 0 {
  5433. if text != "姓名" {
  5434. br.Msg = "匹配失败, 请按模板导入"
  5435. br.ErrMsg = "模板格式错误,title:" + text
  5436. return
  5437. }
  5438. }
  5439. if k == 1 {
  5440. if text != "国际(区号)" {
  5441. br.Msg = "匹配失败, 请按模板导入"
  5442. br.ErrMsg = "模板格式错误,title:" + text
  5443. return
  5444. }
  5445. }
  5446. if k == 2 {
  5447. if text != "手机号" {
  5448. br.Msg = "匹配失败, 请按模板导入"
  5449. br.ErrMsg = "模板格式错误,title:" + text
  5450. return
  5451. }
  5452. }
  5453. if k == 3 {
  5454. if text != "客户公司" {
  5455. br.Msg = "匹配失败, 请按模板导入"
  5456. br.ErrMsg = "模板格式错误,title:" + text
  5457. return
  5458. }
  5459. }
  5460. }
  5461. }
  5462. // 数据行
  5463. if i >= 1 {
  5464. row := sheet.Row(i)
  5465. cells := row.Cells
  5466. var userName, countryCode, mobile, companyName string
  5467. for k, cell := range cells {
  5468. if k == 0 {
  5469. userName = utils.TrimStr(cell.String())
  5470. }
  5471. if k == 1 {
  5472. countryCode = utils.TrimStr(cell.String())
  5473. }
  5474. if k == 2 {
  5475. mobile = utils.TrimStr(cell.String())
  5476. }
  5477. if k == 3 {
  5478. companyName = utils.TrimStr(cell.String())
  5479. }
  5480. }
  5481. // 手机号为空则读取下一行
  5482. if mobile == "" {
  5483. continue
  5484. }
  5485. // mobile列存在"86-11位手机号"这种格式
  5486. if strings.Contains(mobile, "-") {
  5487. mobileArr := strings.Split(mobile, "-")
  5488. if len(mobileArr) == 2 {
  5489. countryCode = mobileArr[0]
  5490. mobile = mobileArr[1]
  5491. }
  5492. }
  5493. mobileArr = append(mobileArr, mobile)
  5494. item := &company.UserImportMatch{
  5495. UserName: userName,
  5496. CountryCode: countryCode,
  5497. Mobile: mobile,
  5498. CompanyName: companyName,
  5499. }
  5500. dataList = append(dataList, item)
  5501. }
  5502. }
  5503. }
  5504. mobileLen := len(mobileArr)
  5505. if mobileLen == 0 {
  5506. br.Msg = "匹配失败, 无有效数据"
  5507. br.ErrMsg = "匹配失败, 无有效数据"
  5508. return
  5509. }
  5510. // 根据手机号查询联系人信息
  5511. var userCond string
  5512. var userPars []interface{}
  5513. userCond += ` AND mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  5514. userPars = append(userPars, mobileArr)
  5515. userList, e := company.GetCompanyUsersByCondition(userCond, userPars)
  5516. if e != nil {
  5517. br.Msg = "匹配失败, 请按模板导入"
  5518. br.ErrMsg = "匹配失败, 获取联系人信息失败, Err: " + e.Error()
  5519. return
  5520. }
  5521. companyIds := make([]int, 0)
  5522. mobileCompanyIdMap := make(map[string]int, 0)
  5523. for i := range userList {
  5524. companyIds = append(companyIds, userList[i].CompanyId)
  5525. mobileCompanyIdMap[userList[i].Mobile] = userList[i].CompanyId
  5526. }
  5527. companyLen := len(companyIds)
  5528. // 查询客户、客户产品
  5529. var productCond string
  5530. var productPars []interface{}
  5531. if companyLen > 0 {
  5532. productCond += ` AND a.company_id IN (` + utils.GetOrmInReplace(companyLen) + `)`
  5533. productPars = append(productPars, companyIds)
  5534. }
  5535. productList, e := company.GetCompanyProductsByCondition(productCond, productPars)
  5536. if e != nil {
  5537. br.Msg = "匹配失败, 请按模板导入"
  5538. br.ErrMsg = "匹配失败, 获取客户产品信息失败, Err: " + e.Error()
  5539. return
  5540. }
  5541. ficcProductMap := make(map[int]*company.CompanyProduct, 0)
  5542. raiProductMap := make(map[int]*company.CompanyProduct, 0)
  5543. for i := range productList {
  5544. if productList[i].ProductId == 1 {
  5545. ficcProductMap[productList[i].CompanyId] = productList[i]
  5546. } else {
  5547. raiProductMap[productList[i].CompanyId] = productList[i]
  5548. }
  5549. }
  5550. // 根据客户信息返回数据
  5551. for i := range dataList {
  5552. companyId := mobileCompanyIdMap[dataList[i].Mobile]
  5553. if companyId == 0 {
  5554. continue
  5555. }
  5556. ficc := ficcProductMap[companyId]
  5557. if ficc != nil {
  5558. dataList[i].SysCompanyName = ficc.CompanyName
  5559. dataList[i].FiccStatus = ficc.Status
  5560. dataList[i].FiccSeller = ficc.SellerName
  5561. }
  5562. rai := raiProductMap[companyId]
  5563. if rai != nil {
  5564. dataList[i].SysCompanyName = rai.CompanyName
  5565. dataList[i].RaiStatus = rai.Status
  5566. dataList[i].RaiSeller = rai.SellerName
  5567. }
  5568. }
  5569. // 缓存导入数据, 导出时用缓存数据生成Excel
  5570. var code, cacheKey string
  5571. dataJson, e := json.Marshal(dataList)
  5572. if e == nil {
  5573. code = fmt.Sprint(time.Now().UnixNano())
  5574. cacheKey = utils.CACHE_KEY_COMPANY_MATCH_PRE + code
  5575. _ = utils.Rc.Put(cacheKey, string(dataJson), time.Minute*30)
  5576. }
  5577. resp := new(company.UserImportMatchResp)
  5578. resp.List = dataList
  5579. resp.Code = code
  5580. br.Data = resp
  5581. br.Msg = "匹配成功"
  5582. br.Ret = 200
  5583. br.Success = true
  5584. }
  5585. // @Title 联系人名单匹配结果下载
  5586. // @Description 联系人名单匹配结果下载
  5587. // @Param Code query string true "导入时返回的Code"
  5588. // @Success 200 Ret=200 匹配成功
  5589. // @router /list/match/download [get]
  5590. func (this *CompanyUserController) ImportListMatchDownload() {
  5591. br := new(models.BaseResponse).Init()
  5592. defer func() {
  5593. this.Data["json"] = br
  5594. this.ServeJSON()
  5595. }()
  5596. sysUser := this.SysUser
  5597. if sysUser == nil {
  5598. br.Msg = "请重新登录"
  5599. return
  5600. }
  5601. code := this.GetString("Code")
  5602. if code == "" {
  5603. br.Msg = "参数有误"
  5604. return
  5605. }
  5606. cacheKey := utils.CACHE_KEY_COMPANY_MATCH_PRE + code
  5607. // 获取导入时的缓存数据
  5608. dataJson, e := utils.Rc.RedisString(cacheKey)
  5609. if e != nil {
  5610. br.Msg = "获取导出数据失败"
  5611. br.ErrMsg = "获取导出数据失败, Err: " + e.Error()
  5612. return
  5613. }
  5614. if dataJson == "" {
  5615. br.Msg = "匹配数据已失效, 请重新匹配"
  5616. return
  5617. }
  5618. dataList := make([]*company.UserImportMatch, 0)
  5619. if e = json.Unmarshal([]byte(dataJson), &dataList); e != nil {
  5620. br.Msg = "匹配数据已失效, 请重新匹配"
  5621. return
  5622. }
  5623. if len(dataList) == 0 {
  5624. br.Msg = "匹配数据已失效, 请重新匹配"
  5625. return
  5626. }
  5627. // 生成Excel表格
  5628. dir, err := os.Executable()
  5629. exPath := filepath.Dir(dir)
  5630. downloadPath := exPath + "/company_match_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  5631. excelFile := xlsx.NewFile()
  5632. if err != nil {
  5633. br.Msg = "生成文件失败"
  5634. br.ErrMsg = "生成文件失败"
  5635. return
  5636. }
  5637. style := xlsx.NewStyle()
  5638. alignment := xlsx.Alignment{
  5639. Horizontal: "center",
  5640. Vertical: "center",
  5641. WrapText: true,
  5642. }
  5643. style.Alignment = alignment
  5644. style.ApplyAlignment = true
  5645. // 新增Sheet和表头
  5646. sheet, err := excelFile.AddSheet("客户名单")
  5647. if err != nil {
  5648. br.Msg = "新增Sheet失败"
  5649. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  5650. return
  5651. }
  5652. titleRow := sheet.AddRow()
  5653. titleRow.AddCell().SetValue("姓名")
  5654. titleRow.AddCell().SetValue("国际(区号)")
  5655. titleRow.AddCell().SetValue("手机号")
  5656. titleRow.AddCell().SetValue("客户公司")
  5657. titleRow.AddCell().SetValue("系统客户")
  5658. titleRow.AddCell().SetValue("FICC状态")
  5659. titleRow.AddCell().SetValue("FICC销售")
  5660. titleRow.AddCell().SetValue("权益状态")
  5661. titleRow.AddCell().SetValue("权益销售")
  5662. // 表格赋值
  5663. for _, v := range dataList {
  5664. dataRow := sheet.AddRow()
  5665. dataRow.SetHeight(20)
  5666. dataRow.AddCell().SetString(v.UserName)
  5667. dataRow.AddCell().SetString(v.CountryCode)
  5668. dataRow.AddCell().SetString(v.Mobile)
  5669. dataRow.AddCell().SetString(v.CompanyName)
  5670. dataRow.AddCell().SetString(v.SysCompanyName)
  5671. dataRow.AddCell().SetString(v.FiccStatus)
  5672. dataRow.AddCell().SetString(v.FiccSeller)
  5673. dataRow.AddCell().SetString(v.RaiStatus)
  5674. dataRow.AddCell().SetString(v.RaiSeller)
  5675. }
  5676. // 输出Excel
  5677. if e = excelFile.Save(downloadPath); e != nil {
  5678. br.Msg = "保存文件失败"
  5679. br.ErrMsg = "保存文件失败"
  5680. return
  5681. }
  5682. defer func() {
  5683. _ = os.Remove(downloadPath)
  5684. }()
  5685. suffix := time.Now().Format(utils.FormatDateTimeUnSpace)
  5686. downloadFileName := "客户名单匹配_" + suffix + ".xlsx"
  5687. this.Ctx.Output.Download(downloadPath, downloadFileName)
  5688. br.Msg = "导出成功"
  5689. br.Ret = 200
  5690. br.Success = true
  5691. }
  5692. // Follow
  5693. // @Title 关注联系人
  5694. // @Description 关注联系人
  5695. // @Param request body request.ApplyMarkReq true "type json string"
  5696. // @Success 200 {object} 标记处理成功
  5697. // @router /follow [post]
  5698. func (this *CompanyUserController) Follow() {
  5699. br := new(models.BaseResponse).Init()
  5700. defer func() {
  5701. this.Data["json"] = br
  5702. this.ServeJSON()
  5703. }()
  5704. sysUser := this.SysUser
  5705. if sysUser == nil {
  5706. br.Msg = "请登录"
  5707. br.ErrMsg = "请登录,SysUser is Empty"
  5708. br.Ret = 408
  5709. return
  5710. }
  5711. var req request.FollowReq
  5712. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  5713. if err != nil {
  5714. br.Msg = "参数解析异常"
  5715. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  5716. return
  5717. }
  5718. if req.UserId <= 0 {
  5719. br.Msg = "用户ID异常"
  5720. br.ErrMsg = "用户ID异常"
  5721. return
  5722. }
  5723. // 查公司里有多少已关注,上限三个
  5724. count, err := models.GetCompanyFollowCount(req.CompanyId)
  5725. if err != nil {
  5726. br.Msg = "操作失败! "
  5727. br.ErrMsg = "操作失败,Err:" + err.Error()
  5728. return
  5729. }
  5730. if count == 3 && req.Type == 1 {
  5731. br.Msg = "关注失败,特别关注已达上限(上限三个)"
  5732. br.ErrMsg = "关注失败,特别关注已达上限(上限三个)"
  5733. return
  5734. }
  5735. // 关注或取关
  5736. err = models.SetUserFollow(req.UserId, req.Type)
  5737. if err != nil {
  5738. br.Msg = "操作失败! "
  5739. br.ErrMsg = "操作失败,Err:" + err.Error()
  5740. return
  5741. }
  5742. br.Ret = 200
  5743. br.Success = true
  5744. br.Msg = "操作成功"
  5745. }
  5746. // @Title 开通或者关闭用户研选权限
  5747. // @Description 开通或者关闭用户研选权限
  5748. // @Param request body request.ApplyMarkReq true "type json string"
  5749. // @Success 200 {object} 操作成功
  5750. // @router /update/user/yananxuan/permission [post]
  5751. func (this *CompanyUserController) UpadteUserYanxuanPermission() {
  5752. br := new(models.BaseResponse).Init()
  5753. defer func() {
  5754. this.Data["json"] = br
  5755. this.ServeJSON()
  5756. }()
  5757. sysUser := this.SysUser
  5758. if sysUser == nil {
  5759. br.Msg = "请登录"
  5760. br.ErrMsg = "请登录,SysUser is Empty"
  5761. br.Ret = 408
  5762. return
  5763. }
  5764. var req cygx.UpdateUserYanxuanPermissionReq
  5765. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  5766. if err != nil {
  5767. br.Msg = "参数解析异常"
  5768. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  5769. return
  5770. }
  5771. userId := req.UserId
  5772. if userId <= 0 {
  5773. br.Msg = "用户ID异常"
  5774. br.ErrMsg = "用户ID异常"
  5775. return
  5776. }
  5777. //获取联系人详情
  5778. userInfo, err := models.GetWxUserByUserId(req.UserId)
  5779. if err != nil {
  5780. br.Msg = "获取联系人异常!"
  5781. br.ErrMsg = "获取联系人异常,Err:" + err.Error()
  5782. return
  5783. }
  5784. if userInfo.BusinessCardUrl == "" {
  5785. br.Msg = "该联系人尚未添加名片,请点击编辑,上传名片后再开通试用"
  5786. return
  5787. }
  5788. companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
  5789. if err != nil {
  5790. br.Msg = "查找客户失败"
  5791. br.ErrMsg = "查找客户失败,err:" + err.Error()
  5792. return
  5793. }
  5794. // 查询用户是否开通过
  5795. count, err := cygx.GetCygxUserYanxuanPermissionCountByUserId(userId)
  5796. if err != nil {
  5797. br.Msg = "操作失败! "
  5798. br.ErrMsg = "操作失败,Err:" + err.Error()
  5799. return
  5800. }
  5801. item := new(cygx.CygxUserYanxuanPermission)
  5802. if req.Type == 0 {
  5803. item.Status = "未开通"
  5804. } else {
  5805. item.Status = "试用"
  5806. }
  5807. item.UserId = userId
  5808. item.CompanyId = companyInfo.CompanyId
  5809. item.CompanyName = companyInfo.CompanyName
  5810. item.StartDate = time.Now().Format(utils.FormatDate)
  5811. item.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate) // 试用期两个月
  5812. item.Mobile = userInfo.Mobile
  5813. item.Email = userInfo.Email
  5814. item.RealName = userInfo.RealName
  5815. item.AdminId = sysUser.AdminId
  5816. item.AdminRealName = sysUser.RealName
  5817. item.CreatedTime = time.Now()
  5818. item.LastUpdatedTime = time.Now()
  5819. item.ModifyTime = time.Now()
  5820. if count == 0 {
  5821. _, err = cygx.AddCygxUserYanxuanPermission(item) // 添加
  5822. } else {
  5823. if req.Type == 0 {
  5824. err = cygx.UpdateCygxUserYanxuanPermissionClose(userId) // 关闭
  5825. } else {
  5826. err = cygx.UpdateCygxUserYanxuanPermissionOpen(item) // 开通
  5827. }
  5828. }
  5829. if err != nil {
  5830. br.Msg = "操作失败! "
  5831. br.ErrMsg = "操作失败,Err:" + err.Error()
  5832. return
  5833. }
  5834. go cygxService.AddCygxUserYanxuanPermissionLog(item)
  5835. br.Ret = 200
  5836. br.Success = true
  5837. br.Msg = "操作成功"
  5838. }
  5839. // @Title 客户联系人详情
  5840. // @Description 客户联系人详情接口
  5841. // @Param UserId query int true "用户ID"
  5842. // @router /user/detail [get]
  5843. func (this *CompanyUserController) DetailUser() {
  5844. br := new(models.BaseResponse).Init()
  5845. defer func() {
  5846. this.Data["json"] = br
  5847. this.ServeJSON()
  5848. }()
  5849. sysUser := this.SysUser
  5850. if sysUser == nil {
  5851. br.Msg = "请登录"
  5852. br.ErrMsg = "请登录,SysUser Is Empty"
  5853. br.Ret = 408
  5854. return
  5855. }
  5856. userId, _ := this.GetInt("UserId")
  5857. if userId <= 0 {
  5858. br.Msg = "参数错误!"
  5859. return
  5860. }
  5861. wxUser, err := models.GetWxUserByUserId(userId)
  5862. if err != nil {
  5863. br.ErrMsg = "获取信息失败,Err:" + err.Error()
  5864. br.Msg = "获取信息失败"
  5865. return
  5866. }
  5867. resp := new(models.WxUserResp)
  5868. resp.Detail = wxUser
  5869. br.Ret = 200
  5870. br.Success = true
  5871. br.Msg = "获取成功"
  5872. br.Data = resp
  5873. }
  5874. // ListByReport
  5875. // @Title 联系人列表
  5876. // @Description 联系人列表接口
  5877. // @Param PageSize query int true "每页数据条数"
  5878. // @Param CurrentIndex query int true "当前页页码,从1开始"
  5879. // @Param CompanyId query int true "公司id,必填"
  5880. // @Param SortParam query int false "排序字段:1-点击量;2-点击时间"
  5881. // @Param SortType query int false "排序方式:1-倒序;2-正序"
  5882. // @Success 200 {object} company.CompanyUserListResp
  5883. // @router /user/listByReport [get]
  5884. func (this *CompanyUserController) ListByReport() {
  5885. br := new(models.BaseResponse).Init()
  5886. defer func() {
  5887. this.Data["json"] = br
  5888. this.ServeJSON()
  5889. }()
  5890. sysUser := this.SysUser
  5891. if sysUser == nil {
  5892. br.Msg = "请登录"
  5893. br.ErrMsg = "请登录,SysUser Is Empty"
  5894. br.Ret = 408
  5895. return
  5896. }
  5897. pageSize, _ := this.GetInt("PageSize")
  5898. currentIndex, _ := this.GetInt("CurrentIndex")
  5899. companyId, _ := this.GetInt("CompanyId")
  5900. sortParam, _ := this.GetInt("SortParam", 2)
  5901. sortType, _ := this.GetInt("SortType", 1)
  5902. if companyId <= 0 {
  5903. br.Msg = "请选择客户"
  5904. br.ErrMsg = "客户参数错误"
  5905. return
  5906. }
  5907. var startSize int
  5908. if pageSize <= 0 {
  5909. pageSize = utils.PageSize20
  5910. }
  5911. if currentIndex <= 0 {
  5912. currentIndex = 1
  5913. }
  5914. startSize = paging.StartIndex(currentIndex, pageSize)
  5915. var order string
  5916. sortArr := []int{1, 2}
  5917. if utils.InArrayByInt(sortArr, sortParam) && utils.InArrayByInt(sortArr, sortType) {
  5918. pMap := map[int]string{1: "ficc_view_total", 2: "ficc_last_view_time"}
  5919. tMap := map[int]string{1: "DESC", 2: "ASC"}
  5920. order = fmt.Sprintf(` ORDER BY %s %s`, pMap[sortParam], tMap[sortType])
  5921. }
  5922. total, err := company.GetCompanyUserCount(companyId)
  5923. if err != nil {
  5924. br.Msg = "获取失败"
  5925. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  5926. return
  5927. }
  5928. list, err := company.GetCompanyUserListByReport(companyId, startSize, pageSize, order)
  5929. if err != nil {
  5930. br.Msg = "获取失败"
  5931. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  5932. return
  5933. }
  5934. respList := make([]*company.CompanyViewResp, 0)
  5935. for i := range list {
  5936. v := &company.CompanyViewResp{
  5937. UserId: int(list[i].UserId),
  5938. UserName: list[i].RealName,
  5939. Mobile: list[i].Mobile,
  5940. Email: list[i].Email,
  5941. ViewTotal: list[i].FiccViewTotal,
  5942. LastViewTime: list[i].FiccLastViewTime.Format(utils.FormatDateTime),
  5943. }
  5944. if v.LastViewTime == "0001-01-01 00:00:00" {
  5945. v.LastViewTime = ""
  5946. }
  5947. respList = append(respList, v)
  5948. }
  5949. page := paging.GetPaging(currentIndex, pageSize, total)
  5950. resp := &company.CompanyViewPageListResp{
  5951. Paging: page,
  5952. List: respList,
  5953. }
  5954. br.Ret = 200
  5955. br.Success = true
  5956. br.Msg = "获取成功"
  5957. br.Data = resp
  5958. }