company_user.go 199 KB

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