company_user.go 197 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261
  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. item, err := models.GetWxUserByUserId(userId)
  2929. if err != nil {
  2930. br.Msg = "获取失败"
  2931. br.Msg = "获取联系人信息失败,Err:" + err.Error()
  2932. return
  2933. }
  2934. var lastViewTimeT time.Time
  2935. if lastViewTime != `` {
  2936. lastViewTimeT, err = time.ParseInLocation(utils.FormatDateTime, lastViewTime, time.Local)
  2937. if err != nil {
  2938. br.Msg = "获取失败"
  2939. br.Msg = "最近一次阅读时间转换异常,Err:" + err.Error()
  2940. return
  2941. }
  2942. }
  2943. list := make([]*company.ViewReportList, 0)
  2944. recordList := make([]*models.UserReportViewRecord, 0)
  2945. obj := models.UserReportViewRecord{}
  2946. var baseCondition string // 基础条件
  2947. var basePars []interface{}
  2948. if item.Mobile != "" {
  2949. baseCondition = ` AND mobile = ? `
  2950. basePars = append(basePars, item.Mobile)
  2951. } else if item.Email == "" {
  2952. baseCondition = ` AND email = ? `
  2953. basePars = append(basePars, item.Email)
  2954. }
  2955. // 阅读记录汇总数
  2956. total := item.FiccViewTotal + item.RaiViewTotal
  2957. switch txtType {
  2958. case 1:
  2959. baseCondition += ` AND source = ? `
  2960. basePars = append(basePars, 4)
  2961. total = item.RaiViewTotal
  2962. if lastViewTimeT.IsZero() {
  2963. if item.RaiLastViewTime != `` && item.RaiLastViewTime != `0000-00-00 00:00:00` {
  2964. lastViewTimeT, _ = time.ParseInLocation(utils.FormatDateTime, item.RaiLastViewTime, time.Local)
  2965. } else {
  2966. lastViewTimeT = time.Now()
  2967. }
  2968. lastViewTime = lastViewTimeT.Format(utils.FormatDateTime)
  2969. }
  2970. // 权益
  2971. case 2: // ficc
  2972. baseCondition += ` AND source in (?,?,?) `
  2973. basePars = append(basePars, 1, 2, 3)
  2974. total = item.FiccViewTotal
  2975. if lastViewTimeT.IsZero() {
  2976. if item.FiccLastViewTime != `` && item.FiccLastViewTime != `0000-00-00 00:00:00` {
  2977. lastViewTimeT, _ = time.ParseInLocation(utils.FormatDateTime, item.FiccLastViewTime, time.Local)
  2978. } else {
  2979. lastViewTimeT = time.Now()
  2980. }
  2981. lastViewTime = lastViewTimeT.Format(utils.FormatDateTime)
  2982. }
  2983. default:
  2984. if lastViewTimeT.IsZero() {
  2985. var ficcViewTime, raiViewTime time.Time
  2986. if item.FiccLastViewTime != `` && item.FiccLastViewTime != `0000-00-00 00:00:00` {
  2987. ficcViewTime, _ = time.ParseInLocation(utils.FormatDateTime, item.FiccLastViewTime, time.Local)
  2988. }
  2989. if item.RaiLastViewTime != `` && item.RaiLastViewTime != `0000-00-00 00:00:00` {
  2990. raiViewTime, _ = time.ParseInLocation(utils.FormatDateTime, item.RaiLastViewTime, time.Local)
  2991. }
  2992. // 如果两个都没有值的话,那么就是当前时间
  2993. if ficcViewTime.IsZero() && raiViewTime.IsZero() {
  2994. lastViewTimeT = time.Now()
  2995. } else if !ficcViewTime.IsZero() && !raiViewTime.IsZero() {
  2996. if ficcViewTime.After(raiViewTime) {
  2997. lastViewTimeT = ficcViewTime
  2998. } else {
  2999. lastViewTimeT = raiViewTime
  3000. }
  3001. } else if !ficcViewTime.IsZero() {
  3002. lastViewTimeT = ficcViewTime
  3003. } else {
  3004. lastViewTimeT = raiViewTime
  3005. }
  3006. // 默认要把最新的记录列出来,所以往后加1s
  3007. lastViewTime = lastViewTimeT.Add(1 * time.Second).Format(utils.FormatDateTime)
  3008. }
  3009. }
  3010. // 开始实际的查询
  3011. {
  3012. condition := baseCondition
  3013. pars := basePars
  3014. condition += `AND create_time < ?`
  3015. pars = append(pars, lastViewTime)
  3016. total, items, err := obj.GetViewReportList(lastViewTimeT.Year(), condition, pars, pageSize)
  3017. if err != nil {
  3018. br.Msg = "获取失败"
  3019. br.Msg = "获取失败,Err:" + err.Error()
  3020. return
  3021. }
  3022. recordList = append(recordList, items...)
  3023. // 查询去年的表
  3024. if total < int64(pageSize) {
  3025. secondCondition := baseCondition
  3026. secondPars := basePars
  3027. secondCondition += `AND create_time < ?`
  3028. secondPars = append(secondPars, lastViewTime)
  3029. _, items, err = obj.GetViewReportList(lastViewTimeT.Year(), secondCondition, secondPars, int(total)-pageSize)
  3030. if err != nil {
  3031. br.Msg = "获取失败"
  3032. br.Msg = "获取失败,Err:" + err.Error()
  3033. return
  3034. }
  3035. recordList = append(recordList, items...)
  3036. }
  3037. }
  3038. viewReportMap := make(map[string]company.ViewReportList) // 阅读记录map
  3039. sourceReportIdListMap := make(map[int8][]int)
  3040. for _, v := range recordList {
  3041. tmpList, ok := sourceReportIdListMap[v.Source]
  3042. if !ok {
  3043. tmpList = make([]int, 0)
  3044. }
  3045. tmpList = append(tmpList, v.ReportId)
  3046. sourceReportIdListMap[v.Source] = tmpList
  3047. tmpTxtType := `ficc`
  3048. if v.Source == 4 {
  3049. tmpTxtType = `rights`
  3050. }
  3051. stopTime := `--`
  3052. if v.StopTime > 0 {
  3053. stopTime = fmt.Sprint(stopTime)
  3054. }
  3055. viewReportMap[fmt.Sprint(v.Source, "_", v.ReportId)] = company.ViewReportList{
  3056. ResearchReportName: "",
  3057. ReportType: "",
  3058. CreatedTime: v.CreateTime,
  3059. TxtType: tmpTxtType,
  3060. MatchTypeName: "--",
  3061. StopTime: stopTime,
  3062. }
  3063. }
  3064. // 获取报告详情
  3065. for source, reportIdList := range sourceReportIdListMap {
  3066. switch source {
  3067. case 1: // rddp报告
  3068. tmpReportList, err := models.GetReportByIdList(reportIdList)
  3069. if err != nil {
  3070. br.Msg = "获取失败"
  3071. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3072. return
  3073. }
  3074. for _, v := range tmpReportList {
  3075. tmpKey := fmt.Sprint(source, "_", v.Id)
  3076. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3077. tmpInfo.ResearchReportName = v.Title
  3078. tmpInfo.ReportType = `rddp`
  3079. tmpInfo.MatchTypeName = v.ClassifyNameFirst
  3080. viewReportMap[tmpKey] = tmpInfo
  3081. }
  3082. }
  3083. case 2: // weekly报告
  3084. tmpReportList, err := models.GetResearchReportListByIdList(reportIdList)
  3085. if err != nil {
  3086. br.Msg = "获取失败"
  3087. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3088. return
  3089. }
  3090. for _, v := range tmpReportList {
  3091. tmpKey := fmt.Sprint(source, "_", v.ResearchReportId)
  3092. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3093. tmpInfo.ResearchReportName = v.ResearchReportName
  3094. tmpInfo.ReportType = v.Type
  3095. viewReportMap[tmpKey] = tmpInfo
  3096. }
  3097. }
  3098. case 3: // weekly_report商品的报告(应该是作废了);
  3099. tmpReportList, err := models.GetChartPermissionByIdList(reportIdList)
  3100. if err != nil {
  3101. br.Msg = "获取失败"
  3102. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3103. return
  3104. }
  3105. for _, v := range tmpReportList {
  3106. tmpKey := fmt.Sprint(source, "_", v.ChartPermissionId)
  3107. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3108. tmpInfo.ResearchReportName = v.ChartPermissionName
  3109. tmpInfo.ReportType = `advisory`
  3110. tmpInfo.MatchTypeName = v.ClassifyName
  3111. viewReportMap[tmpKey] = tmpInfo
  3112. }
  3113. }
  3114. case 4: // 察研观向的报告
  3115. tmpReportList, err := cygx.GetArticleListByIdList(reportIdList)
  3116. if err != nil {
  3117. br.Msg = "获取失败"
  3118. br.Msg = "获取weekly报告详情失败,Err:" + err.Error()
  3119. return
  3120. }
  3121. for _, v := range tmpReportList {
  3122. tmpKey := fmt.Sprint(source, "_", v.ArticleId)
  3123. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3124. tmpInfo.ResearchReportName = v.Title
  3125. tmpInfo.ReportType = `cygx`
  3126. tmpInfo.MatchTypeName = v.MatchTypeName
  3127. viewReportMap[tmpKey] = tmpInfo
  3128. }
  3129. }
  3130. }
  3131. }
  3132. // 报告标题处理
  3133. for _, v := range recordList {
  3134. tmpKey := fmt.Sprint(v.Source, "_", v.ReportId)
  3135. if tmpInfo, ok := viewReportMap[tmpKey]; ok {
  3136. list = append(list, &tmpInfo)
  3137. }
  3138. }
  3139. // 类型处理
  3140. for k, v := range list {
  3141. if v.ReportType == "day" {
  3142. list[k].MatchTypeName = "晨报"
  3143. } else if v.ReportType == "week" {
  3144. list[k].MatchTypeName = "周报"
  3145. } else if v.ReportType == "two_week" {
  3146. list[k].MatchTypeName = "双周报"
  3147. } else if v.ReportType == "month" {
  3148. list[k].MatchTypeName = "月报"
  3149. } else if v.ReportType == "rddp" {
  3150. //list[k].MatchTypeName = "日评"
  3151. createdTime := utils.StrTimeToTime(v.CreatedTime)
  3152. monthStr := createdTime.Format("01")
  3153. dayStr := strconv.Itoa(createdTime.Day())
  3154. if len(dayStr) == 1 {
  3155. dayStr = "0" + dayStr
  3156. }
  3157. list[k].ResearchReportName += "(" + monthStr + dayStr + ")"
  3158. }
  3159. if v.StopTime != "--" {
  3160. list[k].StopTime += "s"
  3161. }
  3162. }
  3163. resp := new(company.ViewReportListResp)
  3164. resp.List = list
  3165. resp.Total = total
  3166. br.Ret = 200
  3167. br.Success = true
  3168. br.Msg = "获取成功"
  3169. br.Data = resp
  3170. }
  3171. // @Title 导出潜在用户
  3172. // @Description 导出潜在用户接口
  3173. // @Param ApplyMethod query int true "申请方式,0:全部,1:未申请,2:已付费客户申请试用,3:非客户申请试用"
  3174. // @Param KeyWord query string true "搜索关键词"
  3175. // @Success Ret=200 导出成功
  3176. // @router /potential/user/export [get]
  3177. func (this *CompanyUserController) PotentialUserExport() {
  3178. br := new(models.BaseResponse).Init()
  3179. defer func() {
  3180. this.Data["json"] = br
  3181. this.ServeJSON()
  3182. }()
  3183. keyWord := this.GetString("KeyWord")
  3184. applyMethod, _ := this.GetInt("ApplyMethod")
  3185. sysUser := this.SysUser
  3186. if sysUser == nil {
  3187. br.Msg = "请登录"
  3188. br.ErrMsg = "请登录,SysUser Is Empty"
  3189. br.Ret = 408
  3190. return
  3191. }
  3192. list := make([]*models.PotentialUserItem, 0)
  3193. //管理员才让导出数据
  3194. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN {
  3195. var condition string
  3196. var pars []interface{}
  3197. if applyMethod > 0 {
  3198. condition += ` AND apply_method=? `
  3199. pars = append(pars, applyMethod-1)
  3200. }
  3201. if keyWord != "" {
  3202. condition += ` AND (a.nick_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  3203. }
  3204. tmpList, err := models.GetPotentialUserListExport(condition, pars)
  3205. if err != nil {
  3206. br.Msg = "获取失败"
  3207. br.ErrMsg = "获取失败,Err:" + err.Error()
  3208. return
  3209. }
  3210. list = tmpList
  3211. }
  3212. dir, err := os.Executable()
  3213. exPath := filepath.Dir(dir)
  3214. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  3215. xlsxFile := xlsx.NewFile()
  3216. if err != nil {
  3217. br.Msg = "生成文件失败"
  3218. br.ErrMsg = "生成文件失败"
  3219. return
  3220. }
  3221. style := xlsx.NewStyle()
  3222. alignment := xlsx.Alignment{
  3223. Horizontal: "center",
  3224. Vertical: "center",
  3225. WrapText: true,
  3226. }
  3227. style.Alignment = alignment
  3228. style.ApplyAlignment = true
  3229. sheel, err := xlsxFile.AddSheet("客户数据")
  3230. if err != nil {
  3231. br.Msg = "新增Sheet失败"
  3232. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  3233. return
  3234. }
  3235. titleRow := sheel.AddRow()
  3236. titleRow.AddCell().SetValue("姓名")
  3237. titleRow.AddCell().SetValue("手机号")
  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. for _, v := range list {
  3245. dataRow := sheel.AddRow()
  3246. dataRow.SetHeight(20)
  3247. dataRow.AddCell().SetString(v.RealName)
  3248. dataRow.AddCell().SetString(v.Mobile)
  3249. dataRow.AddCell().SetString(v.Email)
  3250. dataRow.AddCell().SetString(v.CreatedTime)
  3251. dataRow.AddCell().SetString(v.CompanyName)
  3252. if v.ApplyMethod == 0 {
  3253. dataRow.AddCell().SetString("未申请")
  3254. } else if v.ApplyMethod == 1 {
  3255. dataRow.AddCell().SetString("已付费客户申请试用")
  3256. } else {
  3257. dataRow.AddCell().SetString("非客户申请试用")
  3258. }
  3259. reportView, err := models.GetReportViewMaxTime(int(v.UserId))
  3260. if err != nil {
  3261. br.Msg = "获取失败"
  3262. br.ErrMsg = "获取GetReportViewMaxTime失败,Err:" + err.Error()
  3263. return
  3264. }
  3265. if reportView.LastViewTime.After(v.LastViewTime) {
  3266. v.LastViewTimeStr = v.LastViewTime.Format(utils.FormatDateTime)
  3267. } else {
  3268. v.LastViewTimeStr = v.LastViewTime.Format(utils.FormatDateTime)
  3269. }
  3270. v.ViewTotal += reportView.ViewTotal
  3271. dataRow.AddCell().SetInt(v.ViewTotal)
  3272. if v.LastViewTimeStr == "0001-01-01 00:00:00" {
  3273. v.LastViewTimeStr = ""
  3274. }
  3275. dataRow.AddCell().SetString(v.LastViewTimeStr)
  3276. }
  3277. err = xlsxFile.Save(downLoadnFilePath)
  3278. if err != nil {
  3279. br.Msg = "保存文件失败"
  3280. br.ErrMsg = "保存文件失败"
  3281. return
  3282. }
  3283. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  3284. downloadFileName := "潜在用户列表_" + randStr + ".xlsx"
  3285. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  3286. defer func() {
  3287. os.Remove(downLoadnFilePath)
  3288. }()
  3289. br.Ret = 200
  3290. br.Success = true
  3291. br.Msg = "导出成功"
  3292. }
  3293. // @Title 获取批量导入联系人数据
  3294. // @Description 获取批量导入联系人数据
  3295. // @Param File query file true "文件"
  3296. // @Param CompanyId query file true "客户id"
  3297. // @Success 200 {object} models.ImportListResp
  3298. // @router /import/list [post]
  3299. func (this *CompanyUserController) ImportList() {
  3300. br := new(models.BaseResponse).Init()
  3301. defer func() {
  3302. this.Data["json"] = br
  3303. this.ServeJSON()
  3304. }()
  3305. sysUser := this.SysUser
  3306. if sysUser == nil {
  3307. br.Msg = "请重新登录"
  3308. return
  3309. }
  3310. file, h, err := this.GetFile("File")
  3311. if err != nil {
  3312. br.Msg = "获取文件失败"
  3313. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  3314. return
  3315. }
  3316. companyId, _ := this.GetInt("CompanyId")
  3317. if companyId <= 0 {
  3318. br.Msg = "参数错误"
  3319. br.ErrMsg = "客户id为0"
  3320. return
  3321. }
  3322. //产品id
  3323. productId := services.GetProductId(sysUser.RoleTypeCode)
  3324. //操作权限校验
  3325. errMsg, err := checkOpCompanyUserByCompanyId(companyId, productId, sysUser.AdminId, sysUser.RoleTypeCode)
  3326. if err != nil {
  3327. br.Msg = "导入失败!" + errMsg
  3328. br.ErrMsg = "导入失败,Err:" + err.Error()
  3329. return
  3330. }
  3331. //userBusinessCardCount := 0
  3332. //if productId > 0 {
  3333. // tmpUserBusinessCardCount, err := models.GetCompanyUserBusinessCardCountByProductId(companyId, productId)
  3334. // if err != nil {
  3335. // br.Msg = "导入失败"
  3336. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3337. // return
  3338. // }
  3339. // userBusinessCardCount = tmpUserBusinessCardCount
  3340. //} else {
  3341. // tmpUserBusinessCardCount, err := company.GetCompanyUserBusinessCardCount(companyId)
  3342. // if err != nil {
  3343. // br.Msg = "导入失败"
  3344. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3345. // return
  3346. // }
  3347. // userBusinessCardCount = tmpUserBusinessCardCount
  3348. //}
  3349. //
  3350. //if userBusinessCardCount <= 0 {
  3351. // br.Msg = "请先至少添加一位名片联系人"
  3352. // return
  3353. //}
  3354. //客户信息
  3355. companyInfo, err := company.GetCompanyById(companyId)
  3356. if err != nil {
  3357. br.Msg = "导入异常"
  3358. br.Msg = "导入异常:" + err.Error()
  3359. return
  3360. }
  3361. uploadDir := "static/xls"
  3362. err = os.MkdirAll(uploadDir, 766)
  3363. if err != nil {
  3364. br.Msg = "存储目录创建失败"
  3365. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  3366. return
  3367. }
  3368. path := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  3369. defer file.Close()
  3370. err = this.SaveToFile("File", path)
  3371. if err != nil {
  3372. br.Msg = "文件保存失败"
  3373. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  3374. return
  3375. }
  3376. xlFile, err := xlsx.OpenFile(path)
  3377. if err != nil {
  3378. br.Msg = "获取失败"
  3379. br.ErrMsg = "打开文件失败,Err:" + err.Error() + ";path:" + path
  3380. return
  3381. }
  3382. existUser := make([]*models.WxUser, 0)
  3383. //excel中已经存在的数据,用来判断excel中是否存在相同手机号/邮箱,避免重复提交
  3384. excelData := make(map[string]string)
  3385. //重复数据
  3386. repeatUser := make([]*models.WxUser, 0)
  3387. // 遍历sheet页读取
  3388. for _, sheet := range xlFile.Sheets {
  3389. //遍历行读取
  3390. maxRow := sheet.MaxRow
  3391. fmt.Println("maxRow:", maxRow)
  3392. fmt.Println("maxRow")
  3393. for i := 0; i < maxRow; i++ {
  3394. if i == 0 {
  3395. row := sheet.Row(i)
  3396. cells := row.Cells
  3397. for k, cell := range cells {
  3398. text := cell.String()
  3399. if k == 0 {
  3400. if text != "姓名" {
  3401. br.Msg = "模板格式错误"
  3402. br.ErrMsg = "模板格式错误,title:" + text
  3403. return
  3404. }
  3405. }
  3406. if k == 1 {
  3407. if !strings.Contains(text, "性别") {
  3408. br.Msg = "模板格式错误"
  3409. br.ErrMsg = "模板格式错误,title:" + text
  3410. return
  3411. }
  3412. }
  3413. if k == 2 {
  3414. if !strings.Contains(text, "国际区号(中国大陆填写:86)") {
  3415. br.Msg = "模板格式错误"
  3416. br.ErrMsg = "模板格式错误,title:" + text
  3417. return
  3418. }
  3419. }
  3420. if k == 3 {
  3421. if !strings.Contains(text, "手机号") {
  3422. br.Msg = "模板格式错误"
  3423. br.ErrMsg = "模板格式错误,title:" + text
  3424. return
  3425. }
  3426. }
  3427. if k == 4 {
  3428. if !strings.Contains(text, "邮箱") {
  3429. br.Msg = "模板格式错误"
  3430. br.ErrMsg = "模板格式错误,title:" + text
  3431. return
  3432. }
  3433. }
  3434. if k == 5 {
  3435. if !strings.Contains(text, "是否决策人") {
  3436. br.Msg = "模板格式错误"
  3437. br.ErrMsg = "模板格式错误,title:" + text
  3438. return
  3439. }
  3440. }
  3441. if k == 6 {
  3442. if !strings.Contains(text, "职位") {
  3443. br.Msg = "模板格式错误"
  3444. br.ErrMsg = "模板格式错误,title:" + text
  3445. return
  3446. }
  3447. }
  3448. if k == 7 {
  3449. if !strings.Contains(text, "部门") {
  3450. br.Msg = "模板格式错误"
  3451. br.ErrMsg = "模板格式错误,title:" + text
  3452. return
  3453. }
  3454. }
  3455. }
  3456. }
  3457. if i >= 1 {
  3458. row := sheet.Row(i)
  3459. cells := row.Cells
  3460. var userName, sex, countryCode, mobileOne, email, isMaker, position, departmentName string
  3461. for k, cell := range cells {
  3462. if k == 0 {
  3463. userName = cell.String()
  3464. }
  3465. if k == 1 {
  3466. sex = cell.String()
  3467. }
  3468. if k == 2 {
  3469. countryCode = cell.String()
  3470. }
  3471. if k == 3 {
  3472. mobileOne = cell.String()
  3473. }
  3474. if k == 4 {
  3475. email = cell.String()
  3476. }
  3477. if k == 5 {
  3478. isMaker = cell.String()
  3479. }
  3480. if k == 6 {
  3481. position = cell.String()
  3482. }
  3483. if k == 7 {
  3484. departmentName = cell.String()
  3485. }
  3486. }
  3487. //移除空格
  3488. mobileOne = utils.TrimStr(mobileOne)
  3489. email = utils.TrimStr(email)
  3490. //这些字段都没有的话,系统认为excel到底了
  3491. if userName == "" && sex == "" && mobileOne == "" {
  3492. break
  3493. }
  3494. if userName == "" {
  3495. continue
  3496. }
  3497. //如果手机号或者邮箱都没有填写的情况下
  3498. if mobileOne == "" && email == "" {
  3499. continue
  3500. }
  3501. if mobileOne != "" && countryCode == "" {
  3502. br.Msg = "导入数据中存在【国际区号】为空的联系人,请检查"
  3503. return
  3504. }
  3505. //如果是权益,且是海外用户的情况下,那么手机号、邮箱有一个没有填写的情况下,那么退出当前循环
  3506. if productId == 2 && companyInfo.RegionType == "海外" && (mobileOne == "" || email == "") {
  3507. continue
  3508. }
  3509. item := new(models.WxUser)
  3510. item.CountryCode = countryCode
  3511. item.RealName = userName
  3512. if sex == "男" {
  3513. item.Sex = 1
  3514. } else if sex == "女" {
  3515. item.Sex = 2
  3516. } else {
  3517. item.Sex = 0
  3518. }
  3519. item.Mobile = mobileOne
  3520. item.Email = email
  3521. if isMaker == "是" {
  3522. item.IsMaker = 1
  3523. } else {
  3524. item.IsMaker = 0
  3525. }
  3526. item.Position = position
  3527. item.DepartmentName = departmentName
  3528. if mobileOne != "" {
  3529. if _, ok := excelData[mobileOne]; ok {
  3530. //将用户插入其中,然后退出当前循环,进入下一循环
  3531. repeatUser = append(repeatUser, item)
  3532. continue
  3533. }
  3534. excelData[mobileOne] = ""
  3535. }
  3536. if email != "" {
  3537. if _, ok := excelData[email]; ok {
  3538. //将用户插入其中,然后退出当前循环,进入下一循环
  3539. repeatUser = append(repeatUser, item)
  3540. continue
  3541. }
  3542. excelData[mobileOne] = ""
  3543. }
  3544. //没问题数据
  3545. existUser = append(existUser, item)
  3546. }
  3547. }
  3548. }
  3549. //defer func() {
  3550. // os.Remove(path)
  3551. //}()
  3552. br.Msg = "获取成功"
  3553. br.Ret = 200
  3554. br.Success = true
  3555. br.Data = models.ImportListResp{
  3556. ValidUser: existUser,
  3557. RepeatUser: repeatUser,
  3558. }
  3559. }
  3560. // 需要更新的联系人数据实体
  3561. type UpdateWxUser struct {
  3562. OldWxUser models.WxUser `description:"旧的联系人数据orm实体(未变更前的)"`
  3563. WxUser *models.WxUser `description:"联系人数据orm实体"`
  3564. Cols []string `description:"需要修改的字段名切片"`
  3565. }
  3566. // @Title 批量导入联系人数据
  3567. // @Description 批量导入联系人数据
  3568. // @Param File query file true "文件"
  3569. // @Param CompanyId query file true "客户id"
  3570. // @Success 600 {object} []*company.CompanyUser
  3571. // @Success 200 Ret=200 导入成功
  3572. // @router /import [post]
  3573. func (this *CompanyUserController) Import() {
  3574. br := new(models.BaseResponse).Init()
  3575. defer func() {
  3576. this.Data["json"] = br
  3577. this.ServeJSON()
  3578. }()
  3579. sysUser := this.SysUser
  3580. if sysUser == nil {
  3581. br.Msg = "请重新登录"
  3582. return
  3583. }
  3584. file, h, err := this.GetFile("File")
  3585. if err != nil {
  3586. br.Msg = "获取文件失败"
  3587. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  3588. return
  3589. }
  3590. companyId, _ := this.GetInt("CompanyId")
  3591. if companyId <= 0 {
  3592. br.Msg = "参数错误"
  3593. br.ErrMsg = "客户id为0"
  3594. return
  3595. }
  3596. //产品id
  3597. productId := services.GetProductId(sysUser.RoleTypeCode)
  3598. //操作权限校验
  3599. errMsg, err := checkOpCompanyUserByCompanyId(companyId, productId, sysUser.AdminId, sysUser.RoleTypeCode)
  3600. if err != nil {
  3601. br.Msg = "导入失败!" + errMsg
  3602. br.ErrMsg = "导入失败,Err:" + err.Error()
  3603. return
  3604. }
  3605. //userBusinessCardCount := 0
  3606. //if productId > 0 {
  3607. // tmpUserBusinessCardCount, err := models.GetCompanyUserBusinessCardCountByProductId(companyId, productId)
  3608. // if err != nil {
  3609. // br.Msg = "导入失败"
  3610. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3611. // return
  3612. // }
  3613. // userBusinessCardCount = tmpUserBusinessCardCount
  3614. //} else {
  3615. // tmpUserBusinessCardCount, err := company.GetCompanyUserBusinessCardCount(companyId)
  3616. // if err != nil {
  3617. // br.Msg = "导入失败"
  3618. // br.ErrMsg = "判断是否已经存在上传名片联系人失败,err:" + err.Error()
  3619. // return
  3620. // }
  3621. // userBusinessCardCount = tmpUserBusinessCardCount
  3622. //}
  3623. //
  3624. //if userBusinessCardCount <= 0 {
  3625. // br.Msg = "请先至少添加一位名片联系人"
  3626. // return
  3627. //}
  3628. uploadDir := "static/xls"
  3629. err = os.MkdirAll(uploadDir, 766)
  3630. if err != nil {
  3631. br.Msg = "存储目录创建失败"
  3632. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  3633. return
  3634. }
  3635. path := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  3636. defer file.Close()
  3637. err = this.SaveToFile("File", path)
  3638. if err != nil {
  3639. br.Msg = "文件保存失败"
  3640. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  3641. return
  3642. }
  3643. xlFile, err := xlsx.OpenFile(path)
  3644. if err != nil {
  3645. br.Msg = "文件导入失败"
  3646. br.ErrMsg = "文件导入失败,Err:" + err.Error()
  3647. return
  3648. }
  3649. //获取当前操作员产品权限列表
  3650. companyProductList, productErr := getSysCompanyProductList(productId, companyId)
  3651. if productErr != nil {
  3652. br.Msg = "新增失败"
  3653. br.ErrMsg = "新增失败,获取当前操作员产品权限异常,Err:" + productErr.Error()
  3654. return
  3655. }
  3656. if companyProductList == nil {
  3657. br.Msg = "新增失败"
  3658. br.ErrMsg = "新增失败,获取当前操作员找不到产品权限"
  3659. return
  3660. }
  3661. //客户信息
  3662. companyInfo, err := company.GetCompanyById(companyId)
  3663. if err != nil {
  3664. br.Msg = "导入异常"
  3665. br.Msg = "导入异常:" + err.Error()
  3666. return
  3667. }
  3668. //已存在联系人的切片
  3669. existUser := make([]*company.CompanyUser, 0)
  3670. //待添加销售员与联系人关系的切片
  3671. notRelationUser := make([]map[int]*company.CompanyUser, 0)
  3672. //允许添加的联系人
  3673. needAddUser := make([]*models.WxUserDetail, 0)
  3674. //excel中已经存在的数据,用来判断excel中是否存在相同手机号/邮箱,避免重复提交
  3675. excelData := make(map[string]string)
  3676. //需要更新的数据实体切片
  3677. updateWxUserSlice := make([]UpdateWxUser, 0)
  3678. // 遍历sheet页读取
  3679. for _, sheet := range xlFile.Sheets {
  3680. //遍历行读取
  3681. maxRow := sheet.MaxRow
  3682. for i := 0; i < maxRow; i++ {
  3683. if i == 0 {
  3684. row := sheet.Row(i)
  3685. cells := row.Cells
  3686. for k, cell := range cells {
  3687. text := cell.String()
  3688. if k == 0 {
  3689. if text != "姓名" {
  3690. br.Msg = "模板格式错误"
  3691. br.ErrMsg = "模板格式错误,title:" + text
  3692. return
  3693. }
  3694. }
  3695. if k == 1 {
  3696. if !strings.Contains(text, "性别") {
  3697. br.Msg = "模板格式错误"
  3698. br.ErrMsg = "模板格式错误,title:" + text
  3699. return
  3700. }
  3701. }
  3702. if k == 2 {
  3703. if !strings.Contains(text, "国际区号(中国大陆填写:86)") {
  3704. br.Msg = "模板格式错误"
  3705. br.ErrMsg = "模板格式错误,title:" + text
  3706. return
  3707. }
  3708. }
  3709. if k == 3 {
  3710. if !strings.Contains(text, "手机号") {
  3711. br.Msg = "模板格式错误"
  3712. br.ErrMsg = "模板格式错误,title:" + text
  3713. return
  3714. }
  3715. }
  3716. if k == 4 {
  3717. if !strings.Contains(text, "邮箱") {
  3718. br.Msg = "模板格式错误"
  3719. br.ErrMsg = "模板格式错误,title:" + text
  3720. return
  3721. }
  3722. }
  3723. if k == 5 {
  3724. if !strings.Contains(text, "是否决策人") {
  3725. br.Msg = "模板格式错误"
  3726. br.ErrMsg = "模板格式错误,title:" + text
  3727. return
  3728. }
  3729. }
  3730. if k == 6 {
  3731. if !strings.Contains(text, "职位") {
  3732. br.Msg = "模板格式错误"
  3733. br.ErrMsg = "模板格式错误,title:" + text
  3734. return
  3735. }
  3736. }
  3737. if k == 7 {
  3738. if !strings.Contains(text, "部门") {
  3739. br.Msg = "模板格式错误"
  3740. br.ErrMsg = "模板格式错误,title:" + text
  3741. return
  3742. }
  3743. }
  3744. }
  3745. }
  3746. if i >= 1 {
  3747. row := sheet.Row(i)
  3748. cells := row.Cells
  3749. var userName, sex, countryCode, mobileOne, email, isMaker, position, departmentName string
  3750. for k, cell := range cells {
  3751. if k == 0 {
  3752. userName = cell.String()
  3753. }
  3754. if k == 1 {
  3755. sex = cell.String()
  3756. }
  3757. if k == 2 {
  3758. countryCode = strings.Replace(cell.String(), "+", "", -1)
  3759. countryCode = strings.Replace(countryCode, "-", "", -1)
  3760. countryCode = strings.Replace(countryCode, " ", "", -1)
  3761. }
  3762. if k == 3 {
  3763. mobileOne = cell.String()
  3764. }
  3765. if k == 4 {
  3766. email = cell.String()
  3767. }
  3768. if k == 5 {
  3769. isMaker = cell.String()
  3770. }
  3771. if k == 6 {
  3772. position = cell.String()
  3773. }
  3774. if k == 7 {
  3775. departmentName = cell.String()
  3776. }
  3777. }
  3778. //移除空格
  3779. mobileOne = utils.TrimStr(mobileOne)
  3780. email = utils.TrimStr(email)
  3781. //这些字段都没有的话,系统认为excel到底了
  3782. if userName == "" && sex == "" && mobileOne == "" {
  3783. break
  3784. }
  3785. if userName == "" {
  3786. br.Msg = "导入数据中存在【姓名】为空的联系人,请检查"
  3787. return
  3788. }
  3789. if sex == "" {
  3790. br.Msg = "导入数据中存在【性别】为空的联系人,请检查"
  3791. return
  3792. }
  3793. if mobileOne != "" && countryCode == "" {
  3794. br.Msg = "导入数据中存在【国际区号】为空的联系人,请检查"
  3795. return
  3796. }
  3797. //如果手机号或者邮箱都没有填写的情况下
  3798. if mobileOne == "" && email == "" {
  3799. continue
  3800. }
  3801. //如果是权益,且是海外用户的情况下,那么手机号、邮箱有一个没有填写的情况下,那么退出当前循环
  3802. if productId == 2 && companyInfo.RegionType == "海外" && (mobileOne == "" || email == "") {
  3803. continue
  3804. }
  3805. if isMaker == "" {
  3806. br.Msg = "导入数据中存在【决策人】为空的联系人,请检查"
  3807. return
  3808. }
  3809. item := new(models.WxUserDetail)
  3810. item.RealName = userName
  3811. if sex == "男" {
  3812. item.Sex = 1
  3813. } else if sex == "女" {
  3814. item.Sex = 2
  3815. } else {
  3816. item.Sex = 0
  3817. }
  3818. item.Mobile = mobileOne
  3819. item.Email = email
  3820. if isMaker == "是" {
  3821. item.IsMaker = 1
  3822. } else {
  3823. item.IsMaker = 0
  3824. }
  3825. item.Position = position
  3826. item.DepartmentName = departmentName
  3827. item.SellerRealName = companyProductList[0].SellerName
  3828. item.CompanyName = companyProductList[0].CompanyName
  3829. var mobileOneItem, emailItem *company.CompanyUser
  3830. var mobileNotRelationUserMap, emailNotRelationUserMap map[int]*company.CompanyUser
  3831. //手机号校验
  3832. if mobileOne != "" {
  3833. if _, ok := excelData[mobileOne]; ok {
  3834. br.Msg = "excel表数据异常,存在重复的手机号:" + mobileOne + ",请检查excel表格数据"
  3835. br.ErrMsg = "excel表数据异常,存在重复的手机号:" + mobileOne + ",请检查excel表格数据"
  3836. return
  3837. }
  3838. excelData[mobileOne] = ""
  3839. tmpMobileOneItem, err := company.GetUserCountByMobile(mobileOne)
  3840. if err != nil && err.Error() != utils.ErrNoRow() {
  3841. br.Msg = "新增失败"
  3842. br.ErrMsg = "判断手机号1是否存在,Err:" + err.Error()
  3843. return
  3844. }
  3845. mobileOneItem = tmpMobileOneItem
  3846. if mobileOneItem != nil {
  3847. if mobileOneItem.UserId > 0 {
  3848. hasMove := true //是否可移动
  3849. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  3850. if mobileOneItem.CompanyId > 1 {
  3851. //获取联系人分组信息
  3852. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(mobileOneItem.UserId))
  3853. //根据分组校验
  3854. if err == nil && len(userSellerGroupInfoList) > 0 {
  3855. sellerMap := make(map[int]string) //销售信息map
  3856. sellerNameSlice := make([]string, 0) //销售姓名切片
  3857. for _, v := range userSellerGroupInfoList {
  3858. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  3859. hasMove = false
  3860. }
  3861. if _, ok := sellerMap[v.SellerId]; ok {
  3862. continue
  3863. }
  3864. sellerMap[v.SellerId] = v.SellerRealName
  3865. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  3866. }
  3867. mobileOneItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  3868. }
  3869. }
  3870. item.SellerRealName = mobileOneItem.SellerRealName
  3871. item.CompanyName = mobileOneItem.CompanyName
  3872. //判断该联系人的 客户企业编号 是否与 当前客户编号 一致,如果不一致,那么将该联系人加入到已经存在的联系人列表中去,并终止当前循环,进入下一循环。
  3873. if mobileOneItem.CompanyId != companyId {
  3874. mobileOneItem.HasMove = hasMove //是否可移动
  3875. if hasMove {
  3876. if mobileOneItem.RealName != item.RealName {
  3877. mobileOneItem.RealName = item.RealName
  3878. }
  3879. if mobileOneItem.DepartmentName != item.DepartmentName {
  3880. mobileOneItem.DepartmentName = item.DepartmentName
  3881. }
  3882. if mobileOneItem.RealName != item.RealName {
  3883. mobileOneItem.RealName = item.RealName
  3884. }
  3885. }
  3886. mobileOneItem.CountryCode = countryCode
  3887. existUser = append(existUser, mobileOneItem)
  3888. continue
  3889. }
  3890. //判断该用户与当前账号是否存在关系,如果没有关系,那么直接返回
  3891. notRelationUserMap, checkErr := checkProductListUser(*item, mobileOneItem, companyProductList)
  3892. //sql异常
  3893. if checkErr != nil && checkErr.Error() != utils.ErrNoRow() {
  3894. br.Msg = "新增失败"
  3895. br.ErrMsg = "新增失败,Err:" + checkErr.Error()
  3896. return
  3897. }
  3898. mobileNotRelationUserMap = notRelationUserMap
  3899. }
  3900. }
  3901. }
  3902. //邮箱校验
  3903. if email != "" {
  3904. if _, ok := excelData[email]; ok {
  3905. br.Msg = "excel表数据异常,存在重复的邮箱:" + email + ",请检查excel表格数据"
  3906. br.ErrMsg = "excel表数据异常,存在重复的邮箱:" + email + ",请检查excel表格数据"
  3907. return
  3908. }
  3909. excelData[email] = ""
  3910. tmpEmailItem, err := company.CheckCompanyUserCountByEmail(email)
  3911. if err != nil && err.Error() != utils.ErrNoRow() {
  3912. br.Msg = "新增失败"
  3913. br.ErrMsg = "新增失败,Err:" + err.Error()
  3914. return
  3915. }
  3916. emailItem = tmpEmailItem
  3917. if emailItem != nil {
  3918. if emailItem.UserId > 0 {
  3919. hasMove := true //是否可移动
  3920. //如果是大于 1 的客户,那么就是非潜在客户,需要去查询对应的销售
  3921. if emailItem.CompanyId > 1 {
  3922. //获取联系人分组信息
  3923. userSellerGroupInfoList, err := models.GetUserGroupSellersByUserId(int(emailItem.UserId))
  3924. //根据分组校验
  3925. if err == nil && len(userSellerGroupInfoList) > 0 {
  3926. sellerMap := make(map[int]string) //销售信息map
  3927. sellerNameSlice := make([]string, 0) //销售姓名切片
  3928. for _, v := range userSellerGroupInfoList {
  3929. if v.Status != utils.COMPANY_STATUS_LOSE && v.Status != utils.COMPANY_STATUS_FOREVER && hasMove == true {
  3930. hasMove = false
  3931. }
  3932. if _, ok := sellerMap[v.SellerId]; ok {
  3933. continue
  3934. }
  3935. sellerMap[v.SellerId] = v.SellerRealName
  3936. sellerNameSlice = append(sellerNameSlice, v.SellerRealName)
  3937. }
  3938. emailItem.SellerRealName = strings.Join(sellerNameSlice, "/")
  3939. }
  3940. }
  3941. item.SellerRealName = emailItem.SellerRealName
  3942. item.CompanyName = emailItem.CompanyName
  3943. //判断该联系人的 客户企业编号 是否与 当前客户编号 一致,如果不一致,那么将该联系人加入到已经存在的联系人列表中去,并终止当前循环,进入下一循环。
  3944. if emailItem.CompanyId != companyId {
  3945. emailItem.HasMove = hasMove //是否可移动
  3946. if hasMove {
  3947. if emailItem.RealName != item.RealName {
  3948. emailItem.RealName = item.RealName
  3949. }
  3950. if emailItem.DepartmentName != item.DepartmentName {
  3951. emailItem.DepartmentName = item.DepartmentName
  3952. }
  3953. if emailItem.RealName != item.RealName {
  3954. emailItem.RealName = item.RealName
  3955. }
  3956. }
  3957. emailItem.CountryCode = countryCode
  3958. existUser = append(existUser, emailItem)
  3959. continue
  3960. }
  3961. //判断该用户与当前账号是否存在关系,如果没有关系,那么直接返回
  3962. notRelationUserMap, checkErr := checkProductListUser(*item, emailItem, companyProductList)
  3963. //sql异常
  3964. if checkErr != nil && checkErr.Error() != utils.ErrNoRow() {
  3965. br.Msg = "新增失败"
  3966. br.ErrMsg = "新增失败,Err:" + checkErr.Error()
  3967. return
  3968. }
  3969. //判断该用户与销售是否存在关系,如果不存在,那么加入到 待添加关系 的切片中
  3970. emailNotRelationUserMap = notRelationUserMap
  3971. }
  3972. }
  3973. }
  3974. //如果系统已存在该联系人,那么逻辑处理后退出当前循环,进入下一循环
  3975. if mobileOneItem != nil || emailItem != nil {
  3976. //如果既存在该手机号联系人,也存在该邮箱联系人;那么需要去判断 这 两个 联系人 是不是同一个,
  3977. //如果不是同一个联系人的话,那么只添加对应的关系,不去修改对应的数据
  3978. var wxUserItem *models.WxUser
  3979. var oldTmpWxUserItem models.WxUser
  3980. cols := make([]string, 0)
  3981. if mobileOneItem != nil && emailItem != nil {
  3982. //如果两个联系人 不是 同一个
  3983. if mobileOneItem.UserId != emailItem.UserId {
  3984. notRelationUser = append(notRelationUser, mobileNotRelationUserMap, emailNotRelationUserMap)
  3985. //wxUserItem = mobileOneItem
  3986. } else {
  3987. //如果两个联系人 是 同一个
  3988. notRelationUser = append(notRelationUser, mobileNotRelationUserMap)
  3989. tmpWxUserItem, err := models.GetWxUserByUserId(int(mobileOneItem.UserId))
  3990. if err != nil {
  3991. br.Msg = "导入失败"
  3992. br.ErrMsg = "查询联系人信息失败,Err:" + err.Error()
  3993. return
  3994. }
  3995. oldTmpWxUserItem := *tmpWxUserItem
  3996. if tmpWxUserItem.IsMaker != item.IsMaker {
  3997. tmpWxUserItem.IsMaker = item.IsMaker
  3998. cols = append(cols, "IsMaker")
  3999. }
  4000. if tmpWxUserItem.DepartmentName != item.DepartmentName {
  4001. tmpWxUserItem.DepartmentName = item.DepartmentName
  4002. cols = append(cols, "DepartmentName")
  4003. }
  4004. if tmpWxUserItem.RealName != item.RealName {
  4005. tmpWxUserItem.RealName = item.RealName
  4006. cols = append(cols, "RealName")
  4007. }
  4008. if len(cols) > 0 {
  4009. updateWxUser := UpdateWxUser{
  4010. OldWxUser: oldTmpWxUserItem,
  4011. WxUser: tmpWxUserItem,
  4012. Cols: cols,
  4013. }
  4014. updateWxUserSlice = append(updateWxUserSlice, updateWxUser)
  4015. }
  4016. wxUserItem = tmpWxUserItem
  4017. }
  4018. } else if mobileOneItem != nil {
  4019. //如果只存在 手机号联系人
  4020. notRelationUser = append(notRelationUser, mobileNotRelationUserMap)
  4021. //获取联系人orm实体类
  4022. tmpWxUserItem, err := models.GetWxUserByUserId(int(mobileOneItem.UserId))
  4023. if err != nil {
  4024. br.Msg = "导入失败"
  4025. br.ErrMsg = "查询联系人信息失败,Err:" + err.Error()
  4026. return
  4027. }
  4028. oldTmpWxUserItem = *tmpWxUserItem
  4029. if tmpWxUserItem.IsMaker != item.IsMaker {
  4030. tmpWxUserItem.IsMaker = item.IsMaker
  4031. cols = append(cols, "IsMaker")
  4032. }
  4033. if tmpWxUserItem.DepartmentName != item.DepartmentName && item.DepartmentName != "" {
  4034. tmpWxUserItem.DepartmentName = item.DepartmentName
  4035. cols = append(cols, "DepartmentName")
  4036. }
  4037. if tmpWxUserItem.RealName != item.RealName && item.RealName != "" {
  4038. tmpWxUserItem.RealName = item.RealName
  4039. cols = append(cols, "RealName")
  4040. }
  4041. if tmpWxUserItem.Email != item.Email && item.Email != "" {
  4042. tmpWxUserItem.Email = item.Email
  4043. cols = append(cols, "Email")
  4044. }
  4045. wxUserItem = tmpWxUserItem
  4046. } else if emailItem != nil {
  4047. //如果只存在 邮箱联系人
  4048. notRelationUser = append(notRelationUser, emailNotRelationUserMap)
  4049. //获取联系人orm实体类
  4050. tmpWxUserItem, err := models.GetWxUserByUserId(int(emailItem.UserId))
  4051. if err != nil {
  4052. br.Msg = "导入失败"
  4053. br.ErrMsg = "查询联系人信息失败,Err:" + err.Error()
  4054. return
  4055. }
  4056. oldTmpWxUserItem = *tmpWxUserItem
  4057. if tmpWxUserItem.IsMaker != item.IsMaker {
  4058. tmpWxUserItem.IsMaker = item.IsMaker
  4059. cols = append(cols, "IsMaker")
  4060. }
  4061. if tmpWxUserItem.DepartmentName != item.DepartmentName && item.DepartmentName != "" {
  4062. tmpWxUserItem.DepartmentName = item.DepartmentName
  4063. cols = append(cols, "DepartmentName")
  4064. }
  4065. if tmpWxUserItem.RealName != item.RealName && item.RealName != "" {
  4066. tmpWxUserItem.RealName = item.RealName
  4067. cols = append(cols, "RealName")
  4068. }
  4069. if tmpWxUserItem.Email != item.Email && item.Email != "" {
  4070. tmpWxUserItem.Email = item.Email
  4071. cols = append(cols, "Email")
  4072. }
  4073. wxUserItem = tmpWxUserItem
  4074. }
  4075. //如果变更字段数量大于0
  4076. if len(cols) > 0 {
  4077. updateWxUser := UpdateWxUser{
  4078. OldWxUser: oldTmpWxUserItem,
  4079. WxUser: wxUserItem,
  4080. Cols: cols,
  4081. }
  4082. updateWxUserSlice = append(updateWxUserSlice, updateWxUser)
  4083. }
  4084. continue
  4085. }
  4086. //确实系统中不存在该用户
  4087. item.CountryCode = countryCode
  4088. needAddUser = append(needAddUser, item)
  4089. }
  4090. }
  4091. }
  4092. fmt.Println("表格中用户与系统中存在用户没有冲突")
  4093. //如果表格中用户与系统中存在用户 没有冲突,那么继续执行
  4094. for _, user := range needAddUser {
  4095. if user.RealName != "" && (user.Mobile != "" || user.Email != "") {
  4096. userData := new(models.WxUser)
  4097. userData.RealName = user.RealName
  4098. userData.Sex = user.Sex
  4099. userData.Mobile = user.Mobile
  4100. userData.CountryCode = user.CountryCode
  4101. userData.OutboundMobile = user.Mobile
  4102. userData.OutboundCountryCode = user.CountryCode
  4103. userData.Email = user.Email
  4104. userData.Position = user.Position
  4105. userData.IsMaker = user.IsMaker
  4106. userData.CompanyId = companyId
  4107. userData.DepartmentName = user.DepartmentName
  4108. userData.CountryCode = user.CountryCode
  4109. //判断该手机号、邮箱是否已经添加,如果已经添加,那么就不再添加
  4110. var key string
  4111. if user.Mobile != "" {
  4112. key = "wx_user:mobile:" + userData.Mobile
  4113. } else {
  4114. key = "wx_user:email:" + userData.Email
  4115. }
  4116. isHas := utils.Rc.IsExist(key)
  4117. if isHas == false {
  4118. newId, err := models.AddWxUser(userData)
  4119. userData.UserId = newId
  4120. if err != nil {
  4121. br.Msg = "导入失败"
  4122. br.ErrMsg = "导入失败,Err:" + err.Error()
  4123. return
  4124. }
  4125. //添加联系人与销售员的关系
  4126. for _, companyProduct := range companyProductList {
  4127. models.AddUserSellerRelation(newId, companyProduct.CompanyId, companyProduct.SellerId, companyProduct.ProductId, companyProduct.SellerName, user.Mobile, user.Email)
  4128. }
  4129. //联系人信息
  4130. userInfo, _ := json.Marshal(userData)
  4131. go services.AddWxUserOpLog(company.WxUserOpLog{
  4132. LogType: "import",
  4133. UserId: int(userData.UserId),
  4134. CompanyId: userData.CompanyId,
  4135. Mobile: userData.Mobile,
  4136. Email: userData.Email,
  4137. OriginalUserInfo: "",
  4138. UserInfo: string(userInfo),
  4139. OriginalUserSellerInfo: "",
  4140. UserSellerInfo: "",
  4141. OpUserId: sysUser.AdminId,
  4142. OpUserName: sysUser.RealName,
  4143. CreateTime: time.Now(),
  4144. })
  4145. }
  4146. }
  4147. }
  4148. //添加已经存在的联系人与销售员关系
  4149. fmt.Println("开始添加已经存在的联系人与销售员关系")
  4150. for _, notRelationUserMap := range notRelationUser {
  4151. if item, ok := notRelationUserMap[1]; ok {
  4152. //添加联系人与销售员的关系
  4153. mobileOne := item.Mobile
  4154. mobileTwo := item.MobileTwo
  4155. email := item.Email
  4156. businessCardUrl := item.BusinessCardUrl
  4157. //对老数据做新增字段处理
  4158. _ = company.EditCompanyUserContact(item.UserId, mobileOne, mobileTwo, email, businessCardUrl)
  4159. models.AddUserSellerRelation(item.UserId, item.CompanyId, item.SellerId, 1, item.SellerName, item.Mobile, item.Email)
  4160. }
  4161. if item, ok := notRelationUserMap[2]; ok {
  4162. //添加联系人与销售员的关系
  4163. mobileOne := item.Mobile
  4164. mobileTwo := item.MobileTwo
  4165. email := item.Email
  4166. businessCardUrl := item.BusinessCardUrl
  4167. //对老数据做新增字段处理
  4168. _ = company.EditCompanyUserContact(item.UserId, mobileOne, mobileTwo, email, businessCardUrl)
  4169. models.AddUserSellerRelation(item.UserId, item.CompanyId, item.SellerId, 2, item.SellerName, item.Mobile, item.Email)
  4170. }
  4171. }
  4172. fmt.Println("开始更新联系人信息")
  4173. for _, updateWxUser := range updateWxUserSlice {
  4174. _ = updateWxUser.WxUser.Update(updateWxUser.Cols)
  4175. //联系人信息
  4176. originalUserInfo, _ := json.Marshal(updateWxUser.OldWxUser)
  4177. userInfo, _ := json.Marshal(updateWxUser.WxUser)
  4178. go services.AddWxUserOpLog(company.WxUserOpLog{
  4179. LogType: "import",
  4180. UserId: int(updateWxUser.WxUser.UserId),
  4181. CompanyId: updateWxUser.WxUser.CompanyId,
  4182. Mobile: updateWxUser.WxUser.Mobile,
  4183. Email: updateWxUser.WxUser.Email,
  4184. OriginalUserInfo: string(originalUserInfo),
  4185. UserInfo: string(userInfo),
  4186. OriginalUserSellerInfo: "",
  4187. UserSellerInfo: "",
  4188. OpUserId: sysUser.AdminId,
  4189. OpUserName: sysUser.RealName,
  4190. CreateTime: time.Now(),
  4191. })
  4192. }
  4193. defer func() {
  4194. os.Remove(path)
  4195. }()
  4196. if len(existUser) > 0 {
  4197. br.Ret = 600
  4198. br.Msg = "系统中已存在用户"
  4199. br.IsSendEmail = false
  4200. br.Data = existUser
  4201. return
  4202. }
  4203. //更新客户最近阅读次数以及最近阅读时间
  4204. go services.ModifyCompanyProductLastViewData([]int{companyId})
  4205. br.Msg = "导入成功"
  4206. br.Ret = 200
  4207. br.Success = true
  4208. }
  4209. // @Title 客户的联系人导出
  4210. // @Description 客户的联系人导出接口
  4211. // @Param CompanyId query int true "公司id,必填"
  4212. // @Param KeyWord query string true "搜索关键词"
  4213. // @Success 200 {object} company.CompanyUserListResp
  4214. // @router /user/exportByCompanyId [get]
  4215. func (this *CompanyUserController) Export() {
  4216. br := new(models.BaseResponse).Init()
  4217. defer func() {
  4218. this.Data["json"] = br
  4219. this.ServeJSON()
  4220. }()
  4221. sysUser := this.SysUser
  4222. if sysUser == nil {
  4223. br.Msg = "请登录"
  4224. br.ErrMsg = "请登录,SysUser Is Empty"
  4225. br.Ret = 408
  4226. return
  4227. }
  4228. companyId, _ := this.GetInt("CompanyId")
  4229. keyWord := utils.TrimStr(this.GetString("KeyWord"))
  4230. if companyId <= 0 {
  4231. br.Msg = "请选择客户"
  4232. br.ErrMsg = "客户参数错误"
  4233. return
  4234. }
  4235. var startSize int
  4236. var condition string
  4237. var pars []interface{}
  4238. if keyWord != "" {
  4239. condition += ` AND (a.real_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR a.email LIKE '%` + keyWord + `%') `
  4240. }
  4241. //非管理员,那么只能看到该产品下的联系人
  4242. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  4243. productId := services.GetProductId(sysUser.RoleTypeCode)
  4244. condition += ` AND b.product_id = ? `
  4245. pars = append(pars, productId)
  4246. }
  4247. list, err := company.GetCompanyUserListV2(condition, pars, companyId, startSize, 100000)
  4248. if err != nil {
  4249. br.Msg = "获取失败"
  4250. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  4251. return
  4252. }
  4253. //获取企业客户信息
  4254. companyInfo, err := company.GetCompanyById(companyId)
  4255. if err != nil {
  4256. br.Msg = "获取失败"
  4257. br.ErrMsg = "获取客户信息失败,Err:" + err.Error()
  4258. return
  4259. }
  4260. lenList := len(list)
  4261. if lenList > 0 {
  4262. userIdSlice := make([]string, 0)
  4263. mobilesSlice := make([]string, 0)
  4264. emailsSlice := make([]string, 0)
  4265. for _, v := range list {
  4266. userIdSlice = append(userIdSlice, strconv.Itoa(int(v.UserId)))
  4267. if v.Mobile != "" {
  4268. mobilesSlice = append(mobilesSlice, v.Mobile)
  4269. } else if v.Email != "" {
  4270. emailsSlice = append(emailsSlice, v.Email)
  4271. }
  4272. }
  4273. //用户浏览数据
  4274. userIdStr := strings.Join(userIdSlice, ",")
  4275. mobileStr := strings.Join(mobilesSlice, "','")
  4276. mobileStr = "'" + mobileStr + "'"
  4277. emailStr := strings.Join(emailsSlice, "','")
  4278. emailStr = "'" + emailStr + "'"
  4279. //userViewTotalList, _ := models.GetCountUserViewHistoryByUserIds(userIdStr)
  4280. //userViewTotalMap := make(map[int]*models.UserViewTotalSlice)
  4281. //for _, userView := range userViewTotalList {
  4282. // userViewTotalMap[userView.UserId] = userView
  4283. //}
  4284. //用户日评浏览数据
  4285. //userReportViewTotalList, _ := models.GetReportViewMaxTimeByUserIds(userIdStr)
  4286. //userReportViewTotalMap := make(map[int]*models.ReportViewRecord)
  4287. //for _, userReportView := range userReportViewTotalList {
  4288. // userReportViewTotalMap[userReportView.UserId] = userReportView
  4289. //}
  4290. //报告统计删除晨报部分统计加入每日资讯 2021-4-9
  4291. //手机号
  4292. userViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  4293. userReportViewMobileTotalMap := make(map[string]*models.ReportViewMobileRecord)
  4294. userAdvisoryViewMobileTotalMap := make(map[string]*advisory.UserViewMobileTotalSlice)
  4295. userCygxArticleHistoryRecordViewMobileTotalMap := make(map[string]*models.UserViewMobileTotalSlice)
  4296. if len(mobilesSlice) > 0 {
  4297. //用户浏览数据
  4298. userViewMobileTotalList, _ := models.GetCountUserViewHistoryByMobiles(mobileStr)
  4299. for _, userViewMobile := range userViewMobileTotalList {
  4300. userViewMobileTotalMap[userViewMobile.Mobile] = userViewMobile
  4301. }
  4302. //每日点评手机号数据
  4303. userReportViewMobileTotalList, _ := models.GetReportViewMaxTimeByMobiles(mobileStr)
  4304. for _, userReportViewMobile := range userReportViewMobileTotalList {
  4305. userReportViewMobileTotalMap[userReportViewMobile.Mobile] = userReportViewMobile
  4306. }
  4307. //每日资讯
  4308. userAdvisoryViewMobileTotalList, _ := advisory.GetCountUserViewHistoryByMobiles(mobileStr)
  4309. //fmt.Println(userAdvisoryViewMobileTotalList)
  4310. for _, userAdvisoryViewMobile := range userAdvisoryViewMobileTotalList {
  4311. userAdvisoryViewMobileTotalMap[userAdvisoryViewMobile.Mobile] = userAdvisoryViewMobile
  4312. }
  4313. //查研观向
  4314. userCygxArticleHistoryRecordViewMobileTotalList, _ := models.GetCountCygxArticleHistoryRecordByMobiles(mobileStr)
  4315. for _, userCygxArticleHistoryRecordViewMobile := range userCygxArticleHistoryRecordViewMobileTotalList {
  4316. userCygxArticleHistoryRecordViewMobileTotalMap[userCygxArticleHistoryRecordViewMobile.Mobile] = userCygxArticleHistoryRecordViewMobile
  4317. }
  4318. }
  4319. //邮箱
  4320. userViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  4321. userReportViewEmailTotalMap := make(map[string]*models.ReportViewEmailRecord)
  4322. userAdvisoryViewEmailTotalMap := make(map[string]*advisory.UserViewEmailTotalSlice)
  4323. userCygxViewEmailTotalMap := make(map[string]*models.UserViewEmailTotalSlice)
  4324. if len(emailsSlice) > 0 {
  4325. //用户浏览数据
  4326. userViewEmailTotalList, _ := models.GetCountUserViewHistoryByEmails(emailStr)
  4327. for _, userViewEmail := range userViewEmailTotalList {
  4328. userViewEmailTotalMap[userViewEmail.Email] = userViewEmail
  4329. }
  4330. //每日点评手机号数据
  4331. userReportViewEmailTotalList, _ := models.GetReportViewMaxTimeByEmails(emailStr)
  4332. for _, userReportViewEmail := range userReportViewEmailTotalList {
  4333. userReportViewEmailTotalMap[userReportViewEmail.Email] = userReportViewEmail
  4334. }
  4335. //每日资讯
  4336. userAdvisoryViewEmailTotalList, _ := advisory.GetCountUserViewHistoryByEmails(emailStr)
  4337. for _, userAdvisoryViewEmail := range userAdvisoryViewEmailTotalList {
  4338. userAdvisoryViewEmailTotalMap[userAdvisoryViewEmail.Email] = userAdvisoryViewEmail
  4339. }
  4340. //查研观向
  4341. userCygxViewEmailTotalList, _ := models.GetCountCygxArticleHistoryRecordByEmails(emailStr)
  4342. for _, userCygxReportViewEmail := range userCygxViewEmailTotalList {
  4343. userCygxViewEmailTotalMap[userCygxReportViewEmail.Email] = userCygxReportViewEmail
  4344. }
  4345. }
  4346. //获取联系人关联产品的条数
  4347. //userSellerRelationTotalSlice, err := models.GetUserSellerRelationCountByUserIds(userIdStr)
  4348. //if err != nil {
  4349. // br.Msg = "获取失败"
  4350. // br.ErrMsg = "获取联系人与销售员信息失败,Err:" + err.Error()
  4351. // return
  4352. //}
  4353. //userSellerRelationTotalMap := make(map[int]*models.UserSellerRelationSlice)
  4354. //for _, userSellerRelationTotal := range userSellerRelationTotalSlice {
  4355. // userSellerRelationTotalMap[userSellerRelationTotal.UserId] = userSellerRelationTotal
  4356. //}
  4357. userRecordRegisterMap := make(map[int]time.Time)
  4358. if len(userIdSlice) > 0 {
  4359. //用户绑定注册数据
  4360. userRecordRegisterList, _ := models.GetUserRecordRegisterByUserIds(userIdStr)
  4361. for _, userRecordRegister := range userRecordRegisterList {
  4362. userRecordRegisterMap[userRecordRegister.UserId] = userRecordRegister.CreateTime
  4363. }
  4364. }
  4365. for i := 0; i < lenList; i++ {
  4366. item := list[i]
  4367. //企业名称
  4368. list[i].CompanyName = companyInfo.CompanyName
  4369. //手机号用户累计浏览次数
  4370. if userView, ok := userViewMobileTotalMap[item.Mobile]; ok {
  4371. list[i].ViewTotal += userView.Total
  4372. if userView.CreatedTime.After(list[i].LastViewTime) {
  4373. list[i].LastViewTime = userView.CreatedTime
  4374. list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  4375. }
  4376. }
  4377. //邮箱用户累计浏览次数
  4378. if userView, ok := userViewEmailTotalMap[item.Email]; ok {
  4379. //总共浏览的报表数据需要调整为(手机号用户浏览晨报数+邮箱号用户浏览晨报数)
  4380. list[i].ViewTotal += userView.Total
  4381. if userView.CreatedTime.After(list[i].LastViewTime) {
  4382. list[i].LastViewTime = userView.CreatedTime
  4383. list[i].LastViewTimeStr = userView.CreatedTime.Format(utils.FormatDateTime)
  4384. }
  4385. }
  4386. if item.OpenId != "" {
  4387. list[i].IsRegister = true
  4388. } else {
  4389. if item.RegisterPlatform == 2 {
  4390. if item.Mobile != "" || item.Email != "" {
  4391. list[i].IsRegister = true
  4392. }
  4393. } else if item.RegisterTime != "" {
  4394. list[i].IsRegister = true
  4395. } else {
  4396. list[i].IsRegister = false
  4397. }
  4398. }
  4399. //如果存在手机号用户日评数据,那么需要做处理
  4400. if reportView, ok := userReportViewMobileTotalMap[item.Mobile]; ok {
  4401. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  4402. if reportView.LastViewTime.After(list[i].LastViewTime) {
  4403. list[i].LastViewTime = reportView.LastViewTime
  4404. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  4405. }
  4406. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  4407. list[i].ViewTotal += reportView.ViewTotal
  4408. }
  4409. //如果存在手机号用户日评数据,那么需要做处理
  4410. if reportView, ok := userReportViewEmailTotalMap[item.Email]; ok {
  4411. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  4412. if reportView.LastViewTime.After(list[i].LastViewTime) {
  4413. list[i].LastViewTime = reportView.LastViewTime
  4414. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  4415. }
  4416. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  4417. list[i].ViewTotal += reportView.ViewTotal
  4418. }
  4419. //如果存在手机号用户每日资讯,那么需要做处理
  4420. if advisoryView, ok := userAdvisoryViewMobileTotalMap[item.Mobile]; ok {
  4421. //fmt.Println("蝴蝶眨几次眼睛")
  4422. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4423. if advisoryView.CreatedTime.After(list[i].LastViewTime) {
  4424. list[i].LastViewTime = advisoryView.CreatedTime
  4425. list[i].LastViewTimeStr = advisoryView.CreatedTime.Format(utils.FormatDateTime)
  4426. }
  4427. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数)
  4428. list[i].ViewTotal += advisoryView.Total
  4429. }
  4430. //如果存在邮箱号用户每日资讯,那么需要做处理
  4431. if advisoryView, ok := userAdvisoryViewEmailTotalMap[item.Email]; ok {
  4432. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4433. if advisoryView.CreatedTime.After(list[i].LastViewTime) {
  4434. list[i].LastViewTime = advisoryView.CreatedTime
  4435. list[i].LastViewTimeStr = advisoryView.CreatedTime.Format(utils.FormatDateTime)
  4436. }
  4437. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数)
  4438. list[i].ViewTotal += advisoryView.Total
  4439. }
  4440. //如果存在手机号用户查研观向,那么需要做处理
  4441. if cygxView, ok := userCygxArticleHistoryRecordViewMobileTotalMap[item.Mobile]; ok {
  4442. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4443. if cygxView.CreatedTime.After(list[i].LastViewTime) {
  4444. list[i].LastViewTime = cygxView.CreatedTime
  4445. list[i].LastViewTimeStr = cygxView.CreatedTime.Format(utils.FormatDateTime)
  4446. }
  4447. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数+查研观向)
  4448. list[i].ViewTotal += cygxView.Total
  4449. }
  4450. //如果存在邮箱号用户查研观向,那么需要做处理
  4451. if cygxView, ok := userCygxViewEmailTotalMap[item.Email]; ok {
  4452. //对每日资讯数据做校验,每日资讯时间晚日评报时间,那么将最近一次浏览时间替换
  4453. if cygxView.CreatedTime.After(list[i].LastViewTime) {
  4454. list[i].LastViewTime = cygxView.CreatedTime
  4455. list[i].LastViewTimeStr = cygxView.CreatedTime.Format(utils.FormatDateTime)
  4456. }
  4457. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数+每日资讯数+查研观向)
  4458. list[i].ViewTotal += cygxView.Total
  4459. }
  4460. /*if reportView, ok := userReportViewTotalMap[int(item.UserId)]; ok {
  4461. //对日评数据做校验,日评时间晚于晨报时间,那么将最近一次浏览时间替换
  4462. if reportView.LastViewTime.After(list[i].LastViewTime) {
  4463. list[i].LastViewTime = reportView.LastViewTime
  4464. list[i].LastViewTimeStr = reportView.LastViewTime.Format(utils.FormatDateTime)
  4465. }
  4466. //总共浏览的报表数据需要调整为(浏览晨报数+浏览日评数)
  4467. list[i].ViewTotal += reportView.ViewTotal
  4468. }*/
  4469. //如果有绑定数据,那么将数据返回对应的注册绑定数据
  4470. if _, ok := userRecordRegisterMap[int(item.UserId)]; ok {
  4471. list[i].IsRegister = true
  4472. //list[i].RegisterTime = userRecordRegisterTime.Format(utils.FormatDateTime)
  4473. }
  4474. //是否共享联系人
  4475. //if userSellerRelationTotal, ok := userSellerRelationTotalMap[int(item.UserId)]; ok {
  4476. // //条数大于1,代表这是共享联系人
  4477. // if userSellerRelationTotal.Total > 1 {
  4478. // list[i].IsShared = true
  4479. // }
  4480. //}
  4481. }
  4482. }
  4483. dir, err := os.Executable()
  4484. exPath := filepath.Dir(dir)
  4485. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  4486. xlsxFile := xlsx.NewFile()
  4487. if err != nil {
  4488. br.Msg = "生成文件失败"
  4489. br.ErrMsg = "生成文件失败"
  4490. return
  4491. }
  4492. style := xlsx.NewStyle()
  4493. alignment := xlsx.Alignment{
  4494. Horizontal: "center",
  4495. Vertical: "center",
  4496. WrapText: true,
  4497. }
  4498. style.Alignment = alignment
  4499. style.ApplyAlignment = true
  4500. sheel, err := xlsxFile.AddSheet("联系人数据")
  4501. if err != nil {
  4502. br.Msg = "新增Sheet失败"
  4503. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  4504. return
  4505. }
  4506. sheel.SetColWidth(0, 0, 18)
  4507. sheel.SetColWidth(1, 2, 24)
  4508. sheel.SetColWidth(3, 3, 20)
  4509. sheel.SetColWidth(4, 4, 40)
  4510. titleRow := sheel.AddRow()
  4511. cellName := titleRow.AddCell()
  4512. cellName.SetStyle(style)
  4513. cellName.SetValue("姓名")
  4514. cellMobile := titleRow.AddCell()
  4515. cellMobile.SetStyle(style)
  4516. cellMobile.SetValue("手机号")
  4517. cellEmail := titleRow.AddCell()
  4518. cellEmail.SetStyle(style)
  4519. cellEmail.SetValue("邮箱")
  4520. cellMark := titleRow.AddCell()
  4521. cellMark.SetStyle(style)
  4522. cellMark.SetValue("是否决策人")
  4523. cellCompanyName := titleRow.AddCell()
  4524. cellCompanyName.SetStyle(style)
  4525. cellCompanyName.SetValue("客户名称")
  4526. for _, v := range list {
  4527. dataRow := sheel.AddRow()
  4528. dataRow.SetHeight(20)
  4529. isMark := "否"
  4530. if v.IsMaker == 1 {
  4531. isMark = "是"
  4532. }
  4533. cellDataName := dataRow.AddCell()
  4534. cellDataName.SetStyle(style)
  4535. cellDataName.SetValue(v.RealName)
  4536. cellDataMobile := dataRow.AddCell()
  4537. cellDataMobile.SetStyle(style)
  4538. cellDataMobile.SetValue(v.Mobile)
  4539. cellDataEmail := dataRow.AddCell()
  4540. cellDataEmail.SetStyle(style)
  4541. cellDataEmail.SetValue(v.Email)
  4542. cellDataMark := dataRow.AddCell()
  4543. cellDataMark.SetStyle(style)
  4544. cellDataMark.SetValue(isMark)
  4545. cellDataCompanyName := dataRow.AddCell()
  4546. cellDataCompanyName.SetStyle(style)
  4547. cellDataCompanyName.SetValue(companyInfo.CompanyName)
  4548. }
  4549. err = xlsxFile.Save(downLoadnFilePath)
  4550. if err != nil {
  4551. br.Msg = "保存文件失败"
  4552. br.ErrMsg = "保存文件失败"
  4553. return
  4554. }
  4555. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  4556. downloadFileName := "联系人列表_" + randStr + ".xlsx"
  4557. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  4558. defer func() {
  4559. os.Remove(downLoadnFilePath)
  4560. }()
  4561. br.Ret = 200
  4562. br.Success = true
  4563. br.Msg = "导出成功"
  4564. }
  4565. // 检测产品
  4566. func checkProductListUser(item models.WxUserDetail, companyUser *company.CompanyUser, companyProductList []*company.CompanyProduct) (notRelationUserMap map[int]*company.CompanyUser, err error) {
  4567. notRelationUserMap = make(map[int]*company.CompanyUser)
  4568. //判断该用户与当前账号是否存在关系,如果没有关系,那么直接返回
  4569. for _, companyProduct := range companyProductList {
  4570. _, relationErr := models.GetUserSellerRelation(companyUser.UserId, companyProduct.SellerId)
  4571. //找到数据了
  4572. if relationErr == nil {
  4573. continue
  4574. }
  4575. //sql异常
  4576. if relationErr.Error() != utils.ErrNoRow() {
  4577. err = relationErr
  4578. return
  4579. }
  4580. //没找到数据,不存在关系
  4581. //如果不存在关系,那么直接添加关系就好了,其他操作不做
  4582. if relationErr.Error() == utils.ErrNoRow() {
  4583. if item.Mobile != "" && companyUser.Mobile == "" {
  4584. companyUser.Mobile = strings.Replace(item.Mobile, " ", "", -1)
  4585. }
  4586. if item.Email != "" && companyUser.Email == "" {
  4587. companyUser.Email = strings.Replace(companyUser.Email, " ", "", -1)
  4588. }
  4589. companyUser.SellerId = companyProduct.SellerId
  4590. companyUser.SellerName = companyProduct.SellerName
  4591. companyUser.DepartmentId = companyProduct.DepartmentId
  4592. companyUser.GroupId = companyProduct.GroupId
  4593. notRelationUserMap[companyProduct.ProductId] = companyUser
  4594. }
  4595. }
  4596. return
  4597. }
  4598. // @Title 下载模板
  4599. // @Description 下载模板
  4600. // @Success 200
  4601. // @router /template [get]
  4602. func (this *CompanyUserController) TemplateDownload() {
  4603. br := new(models.BaseResponse).Init()
  4604. defer func() {
  4605. this.Data["json"] = br
  4606. this.ServeJSON()
  4607. }()
  4608. this.Ctx.Output.Download("./static/导入联系人模板.xlsx", "导入联系人模板.xlsx")
  4609. br.Ret = 200
  4610. br.Success = true
  4611. br.Msg = "下载成功"
  4612. }
  4613. //百度
  4614. //func init() {
  4615. // fmt.Println("start")
  4616. // businessCardUrl := `https://hongze.oss-accelerate.aliyuncs.com/static/images/202101/20210111/wB3ELOuH8AxTvrIDXHaFTn9Z0WXB.jpg`
  4617. // services.GetBdGeneralBasic(businessCardUrl)
  4618. // fmt.Println("end")
  4619. //}
  4620. //阿里云
  4621. //func init() {
  4622. // fmt.Println("start")
  4623. // businessCardUrl := `https://hongze.oss-accelerate.aliyuncs.com/static/images/202101/20210111/wB3ELOuH8AxTvrIDXHaFTn9Z0WXB.jpg`
  4624. // aliyunResult, err := services.AliyunBusinessCardOcr(businessCardUrl)
  4625. //
  4626. // if err != nil {
  4627. // fmt.Println("line 1026")
  4628. // return
  4629. // }
  4630. // if !aliyunResult.Success {
  4631. // fmt.Println("line 1026")
  4632. // return
  4633. // }
  4634. //
  4635. // mobileStr := strings.Join(aliyunResult.TelCell, ",")
  4636. // mobileOne:="18366208402"
  4637. // if strings.Contains(mobileStr, mobileOne) {
  4638. // fmt.Println("true")
  4639. // } else {
  4640. // fmt.Println("false")
  4641. // }
  4642. // fmt.Println("end")
  4643. //}
  4644. // 获取是否拥有操作权限
  4645. func checkOpCompanyUserByUserId(userId, productId, opId int, roleTypeCode string) (errMsg string, err error) {
  4646. userInfo, userErr := models.GetWxUserByUserId(userId)
  4647. if userErr != nil {
  4648. errMsg = "查询用户信息失败"
  4649. err = errors.New("查询用户信息失败,Err:" + userErr.Error())
  4650. return
  4651. }
  4652. //产品权限
  4653. //获取当前操作员产品权限
  4654. companyProductList, productErr := getSysCompanyProductList(productId, userInfo.CompanyId)
  4655. if productErr != nil {
  4656. errMsg = "获取当前操作员产品权限异常"
  4657. err = errors.New("删除失败,获取当前操作员产品权限异常,Err:" + productErr.Error())
  4658. return
  4659. }
  4660. if companyProductList == nil {
  4661. errMsg = "删除失败"
  4662. err = errors.New("删除失败,获取当前操作员找不到产品权限")
  4663. return
  4664. }
  4665. item, err := company.GetCompanyDetailById(userInfo.CompanyId)
  4666. if err != nil {
  4667. return
  4668. }
  4669. //操作权限校验
  4670. for _, v := range companyProductList {
  4671. companyUserStatus := services.CheckCompanyUserButton(roleTypeCode, v.SellerId, opId, v.ProductId, item.ShareSellerId, v.Status)
  4672. if companyUserStatus == false {
  4673. errMsg = "没有操作权限"
  4674. err = errors.New("没有操作权限")
  4675. return
  4676. }
  4677. }
  4678. return
  4679. }
  4680. // 获取是否拥有操作权限
  4681. func checkOpCompanyUserByCompanyId(companyId, productId, opId int, roleTypeCode string) (errMsg string, err error) {
  4682. //获取当前操作员产品权限
  4683. companyProductList, productErr := getSysCompanyProductList(productId, companyId)
  4684. if productErr != nil {
  4685. errMsg = "获取当前操作员产品权限异常"
  4686. err = errors.New("删除失败,获取当前操作员产品权限异常,Err:" + productErr.Error())
  4687. return
  4688. }
  4689. if companyProductList == nil {
  4690. errMsg = "删除失败"
  4691. err = errors.New("删除失败,获取当前操作员找不到产品权限")
  4692. return
  4693. }
  4694. item, err := company.GetCompanyDetailById(companyId)
  4695. if err != nil {
  4696. return
  4697. }
  4698. //操作权限校验
  4699. for _, v := range companyProductList {
  4700. companyUserStatus := services.CheckCompanyUserButton(roleTypeCode, v.SellerId, opId, v.ProductId, item.ShareSellerId, v.Status)
  4701. if companyUserStatus == false {
  4702. errMsg = "没有操作权限"
  4703. err = errors.New("没有操作权限")
  4704. return
  4705. }
  4706. }
  4707. return
  4708. }
  4709. // @Title FICC申请单-移动潜在用户
  4710. // @Description FICC申请单移动潜在用户接口
  4711. // @Param request body models.ApplyRecordPotentialUserMoveReq true "type json string"
  4712. // @Success Ret=200 移动成功
  4713. // @router /potential/user/moveByApplyRecord [post]
  4714. func (this *CompanyController) ApplyRecordPotentialUserMove() {
  4715. br := new(models.BaseResponse).Init()
  4716. defer func() {
  4717. this.Data["json"] = br
  4718. this.ServeJSON()
  4719. }()
  4720. sysUser := this.SysUser
  4721. if sysUser == nil {
  4722. br.Msg = "请登录"
  4723. br.ErrMsg = "请登录,SysUser Is Empty"
  4724. br.Ret = 408
  4725. return
  4726. }
  4727. var req models.ApplyRecordPotentialUserMoveReq
  4728. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  4729. if err != nil {
  4730. br.Msg = "参数解析异常!"
  4731. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  4732. return
  4733. }
  4734. if req.UserId <= 0 {
  4735. br.Msg = "参数错误"
  4736. br.ErrMsg = "客户id错误"
  4737. return
  4738. }
  4739. wxUser, err := models.GetWxUserByUserId(req.UserId)
  4740. //如果移动客户编号小于等于0,那么移动到潜在客户
  4741. if req.CompanyId <= 0 {
  4742. req.CompanyId = 1
  4743. }
  4744. if err != nil {
  4745. br.Msg = "移动失败,该潜在用户已被删除"
  4746. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  4747. return
  4748. }
  4749. if req.Mobile == "" {
  4750. req.Mobile = wxUser.Mobile
  4751. }
  4752. if req.Email == "" {
  4753. req.Email = wxUser.Email
  4754. }
  4755. if req.Mobile == "" && req.Email == "" {
  4756. br.Msg = "手机号和邮箱不能同时为空"
  4757. return
  4758. }
  4759. //校验手机号
  4760. if req.Mobile != "" && wxUser.Mobile != req.Mobile {
  4761. count, err := models.CheckUserMobileIsRegister(req.UserId, req.Mobile)
  4762. if err != nil {
  4763. br.Msg = "校验数据失败"
  4764. br.ErrMsg = "校验数据失败,Err:" + err.Error()
  4765. return
  4766. }
  4767. if count > 0 {
  4768. br.Msg = "该手机号已经注册过用户"
  4769. return
  4770. }
  4771. }
  4772. //校验邮箱
  4773. if req.Email != "" && wxUser.Email != req.Email {
  4774. if !utils.ValidateEmailFormatat(req.Email) {
  4775. br.Msg = "请输入正确的邮箱(包含@)"
  4776. return
  4777. }
  4778. count, err := models.CheckUserEmailIsRegister(req.UserId, req.Email)
  4779. if err != nil {
  4780. br.Msg = "校验数据失败"
  4781. br.ErrMsg = "校验数据失败,Err:" + err.Error()
  4782. return
  4783. }
  4784. if count > 0 {
  4785. br.Msg = "该邮箱已经注册过用户"
  4786. br.IsSendEmail = false
  4787. return
  4788. }
  4789. }
  4790. updateCol := make([]string, 0)
  4791. if wxUser.OutboundMobile == "" || wxUser.Mobile == wxUser.OutboundMobile {
  4792. wxUser.OutboundMobile = req.Mobile
  4793. wxUser.OutboundCountryCode = req.CountryCode
  4794. updateCol = append(updateCol, "OutboundMobile", "OutboundCountryCode")
  4795. }
  4796. if req.Email != "" {
  4797. wxUser.Email = req.Email
  4798. updateCol = append(updateCol, "Email")
  4799. }
  4800. if req.RealName != "" {
  4801. wxUser.RealName = req.RealName
  4802. updateCol = append(updateCol, "RealName")
  4803. }
  4804. if req.Mobile != "" {
  4805. wxUser.Mobile = req.Mobile
  4806. updateCol = append(updateCol, "Mobile")
  4807. }
  4808. if req.CountryCode != "" {
  4809. wxUser.CountryCode = req.CountryCode
  4810. updateCol = append(updateCol, "CountryCode")
  4811. }
  4812. if req.Position != "" {
  4813. wxUser.Position = req.Position
  4814. updateCol = append(updateCol, "Position")
  4815. }
  4816. if wxUser.DepartmentName == "" && req.DepartmentName != "" {
  4817. wxUser.DepartmentName = req.DepartmentName
  4818. updateCol = append(updateCol, "DepartmentName")
  4819. }
  4820. wxUser.Sex = req.Sex
  4821. wxUser.IsMaker = req.IsMaker
  4822. updateCol = append(updateCol, "Sex", "IsMaker")
  4823. //名片匹配
  4824. if req.BusinessCardUrl != "" {
  4825. //if req.BusinessCardUrl != "" && utils.RunMode == "release" {
  4826. // 获取图片识别手机号的配置
  4827. crmConfig2, err := company.GetConfigDetailByCode("card_recognition_tel")
  4828. if err != nil {
  4829. br.Msg = "获取配置失败"
  4830. br.ErrMsg = "获取配置失败"
  4831. br.IsSendEmail = false
  4832. return
  4833. }
  4834. if crmConfig2.ConfigValue == "true" {
  4835. card, err := services.GetBusinessCard(req.BusinessCardUrl)
  4836. if err != nil {
  4837. br.Msg = "名片识别失败"
  4838. br.ErrMsg = "名片识别失败,Err:" + err.Error()
  4839. return
  4840. }
  4841. isFlag := false
  4842. if len(card.WordsResult.MOBILE) > 0 {
  4843. mobileStr := strings.Join(card.WordsResult.MOBILE, ",")
  4844. //if req.Mobile != "" {
  4845. // if strings.Contains(mobileStr, req.Mobile) || mobileStr == "" {
  4846. // isFlag = true
  4847. // } else {
  4848. // isFlag = false
  4849. // }
  4850. //}
  4851. if req.Mobile != "" && strings.Contains(mobileStr, req.Mobile) {
  4852. isFlag = true
  4853. }
  4854. }
  4855. if !isFlag {
  4856. //阿里云识别
  4857. //if utils.RunMode == "release" {
  4858. aliyunResult, errMsg, err := services.AliyunOcrBusinessCard(req.BusinessCardUrl)
  4859. if err != nil {
  4860. br.Msg = "识别失败"
  4861. br.ErrMsg = "识别失败,Err:" + err.Error()
  4862. return
  4863. }
  4864. if !aliyunResult.Success {
  4865. br.Msg = "识别失败"
  4866. br.ErrMsg = "识别失败"
  4867. return
  4868. }
  4869. if errMsg != "" {
  4870. br.Msg = errMsg
  4871. return
  4872. }
  4873. if len(aliyunResult.TelCell) > 0 {
  4874. mobileStr := strings.Join(aliyunResult.TelCell, ",")
  4875. if req.Mobile != "" {
  4876. if strings.Contains(mobileStr, req.Mobile) {
  4877. isFlag = true
  4878. } else {
  4879. isFlag = false
  4880. }
  4881. }
  4882. } else {
  4883. isFlag = true
  4884. }
  4885. //}
  4886. }
  4887. if !isFlag && crmConfig2.ConfigValue == "true" {
  4888. br.Msg = "名片信息与联系人手机号不符"
  4889. return
  4890. }
  4891. }
  4892. wxUser.BusinessCardUrl = req.BusinessCardUrl
  4893. updateCol = append(updateCol, "BusinessCardUrl")
  4894. }
  4895. productId := services.GetProductId(sysUser.RoleTypeCode)
  4896. companyProductList, _ := getSysCompanyProductList(productId, req.CompanyId)
  4897. err = services.MovePotentialUserByApplyRecord(sysUser.AdminId, req.UserId, req.CompanyId, req.ApplyRecordId, req.RealName, req.Mobile, req.Email, req.Remark, companyProductList, wxUser, updateCol)
  4898. if err != nil {
  4899. br.Msg = "移动失败"
  4900. br.ErrMsg = "移动失败,Err:" + err.Error()
  4901. return
  4902. }
  4903. //更新客户最近阅读次数以及最近阅读时间
  4904. go services.ModifyCompanyProductLastViewData([]int{req.CompanyId})
  4905. br.Ret = 200
  4906. br.Success = true
  4907. br.Msg = "移动成功"
  4908. br.IsAddLog = true
  4909. }
  4910. // @Title 新增联系人图表分类权限
  4911. // @Description 新增联系人图表分类权限接口
  4912. // @Param request body request.SaveCompanyUserChartClassifyPermissionReq true "type json string"
  4913. // @Success 200 Ret=200 操作成功
  4914. // @router /user/addChartClassifyPermission [post]
  4915. func (this *CompanyUserController) AddCompanyUserChartClassifyPermission() {
  4916. br := new(models.BaseResponse).Init()
  4917. defer func() {
  4918. this.Data["json"] = br
  4919. this.ServeJSON()
  4920. }()
  4921. sysUser := this.SysUser
  4922. if sysUser == nil {
  4923. br.Msg = "请登录"
  4924. br.ErrMsg = "请登录,SysUser Is Empty"
  4925. br.Ret = 408
  4926. return
  4927. }
  4928. var req request.SaveCompanyUserChartClassifyPermissionReq
  4929. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  4930. if err != nil {
  4931. br.Msg = "参数解析异常!"
  4932. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  4933. return
  4934. }
  4935. if req.UserId <= 0 {
  4936. br.Msg = "参数丢失:联系人ID"
  4937. br.ErrMsg = "参数丢失,联系人ID"
  4938. return
  4939. }
  4940. if req.StartTime == "" || req.EndTime == "" {
  4941. br.Msg = "请选择开始结束时间"
  4942. br.ErrMsg = "请选择开始结束时间"
  4943. br.IsSendEmail = false
  4944. return
  4945. }
  4946. startTime, err := time.Parse(utils.FormatDate, req.StartTime)
  4947. if err != nil {
  4948. br.Msg = "开始时间格式有误"
  4949. br.ErrMsg = "开始时间转换失败, Err:" + err.Error()
  4950. return
  4951. }
  4952. endTime, err := time.Parse(utils.FormatDate, req.EndTime)
  4953. if err != nil {
  4954. br.Msg = "结束时间格式有误"
  4955. br.ErrMsg = "结束时间转换失败, Err:" + err.Error()
  4956. return
  4957. }
  4958. if startTime.After(endTime) {
  4959. br.Msg = "结束时间不能小于开始时间"
  4960. br.ErrMsg = "结束时间不能小于开始时间"
  4961. br.IsSendEmail = false
  4962. return
  4963. }
  4964. _, err = company_user.GetChartClassifyPermissionByUserId(req.UserId)
  4965. if err == nil {
  4966. br.Msg = "请勿重复设置图表权限"
  4967. br.ErrMsg = "请勿重复设置图表权限"
  4968. br.IsSendEmail = false
  4969. return
  4970. }
  4971. // 判断是否允许设置
  4972. userInfo, err := models.GetWxUserByUserId(req.UserId)
  4973. if err != nil {
  4974. br.Msg = "联系人信息有误"
  4975. br.ErrMsg = "联系人信息有误, Err: " + err.Error()
  4976. return
  4977. }
  4978. companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
  4979. if err != nil {
  4980. br.Msg = "获取客户信息失败"
  4981. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  4982. return
  4983. }
  4984. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(userInfo.CompanyId, 1)
  4985. if err != nil {
  4986. br.Msg = "获取客户产品信息失败"
  4987. br.ErrMsg = "获取客户产品信息失败, Err: " + err.Error()
  4988. return
  4989. }
  4990. companyStatus := companyProductInfo.Status
  4991. if companyStatus != utils.COMPANY_STATUS_FOREVER {
  4992. // 非永续客户
  4993. if companyStatus == utils.COMPANY_STATUS_TRY_OUT || companyStatus == utils.COMPANY_STATUS_FORMAL {
  4994. // 正式/试用
  4995. expireTimeStr := companyProductInfo.EndDate
  4996. expireTime, _ := time.ParseInLocation(utils.FormatDate, expireTimeStr, time.Local)
  4997. // 服务结束时间小于设置的结束时间则不允许设置
  4998. if endTime.After(expireTime.AddDate(0, 0, 1)) {
  4999. br.Msg = "终止日期不允许超过服务日期, 服务终止日期为: " + expireTimeStr
  5000. br.IsSendEmail = false
  5001. return
  5002. }
  5003. } else {
  5004. br.Msg = "当前客户不允许设置图表权限, 客户状态为: " + companyStatus
  5005. br.IsSendEmail = false
  5006. return
  5007. }
  5008. }
  5009. item := &company_user.ChartClassifyPermission{
  5010. UserId: req.UserId,
  5011. CompanyId: companyInfo.CompanyId,
  5012. StartTime: startTime,
  5013. EndTime: endTime,
  5014. Enabled: req.Enabled,
  5015. CreateTime: time.Now(),
  5016. }
  5017. _, err = company_user.AddChartClassifyPermission(item)
  5018. if err != nil {
  5019. br.Msg = "新增图表权限失败"
  5020. br.ErrMsg = "新增图表权限失败, Err:" + err.Error()
  5021. return
  5022. }
  5023. // 操作记录
  5024. {
  5025. record := &company_user.CompanyUserChartPermissionOperationRecord{
  5026. Type: 1,
  5027. CompanyId: companyInfo.CompanyId,
  5028. UserId: req.UserId,
  5029. AdminId: sysUser.AdminId,
  5030. StartTime: startTime,
  5031. EndTime: endTime,
  5032. Enabled: req.Enabled,
  5033. CreateTime: time.Now(),
  5034. }
  5035. go company_user.AddChartPermissionOperationRecord(record)
  5036. }
  5037. br.Ret = 200
  5038. br.Success = true
  5039. br.Msg = "操作成功"
  5040. }
  5041. // @Title 编辑联系人图表分类权限
  5042. // @Description 编辑联系人图表分类权限接口
  5043. // @Param request body request.SaveCompanyUserChartClassifyPermissionReq true "type json string"
  5044. // @Success 200 Ret=200 操作成功
  5045. // @router /user/editChartClassifyPermission [post]
  5046. func (this *CompanyUserController) EditCompanyUserChartClassifyPermission() {
  5047. br := new(models.BaseResponse).Init()
  5048. defer func() {
  5049. this.Data["json"] = br
  5050. this.ServeJSON()
  5051. }()
  5052. sysUser := this.SysUser
  5053. if sysUser == nil {
  5054. br.Msg = "请登录"
  5055. br.ErrMsg = "请登录,SysUser Is Empty"
  5056. br.Ret = 408
  5057. return
  5058. }
  5059. var req request.SaveCompanyUserChartClassifyPermissionReq
  5060. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  5061. if err != nil {
  5062. br.Msg = "参数解析异常!"
  5063. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  5064. return
  5065. }
  5066. if req.UserId <= 0 {
  5067. br.Msg = "参数丢失:联系人ID"
  5068. br.ErrMsg = "参数丢失,联系人ID"
  5069. return
  5070. }
  5071. if req.StartTime == "" || req.EndTime == "" {
  5072. br.Msg = "请选择开始结束时间"
  5073. br.ErrMsg = "请选择开始结束时间"
  5074. br.IsSendEmail = false
  5075. return
  5076. }
  5077. startTime, err := time.Parse(utils.FormatDate, req.StartTime)
  5078. if err != nil {
  5079. br.Msg = "开始时间格式有误"
  5080. br.ErrMsg = "开始时间转换失败, Err:" + err.Error()
  5081. return
  5082. }
  5083. endTime, err := time.Parse(utils.FormatDate, req.EndTime)
  5084. if err != nil {
  5085. br.Msg = "结束时间格式有误"
  5086. br.ErrMsg = "结束时间转换失败, Err:" + err.Error()
  5087. return
  5088. }
  5089. if startTime.After(endTime) {
  5090. br.Msg = "结束时间不能小于开始时间"
  5091. br.ErrMsg = "结束时间不能小于开始时间"
  5092. br.IsSendEmail = false
  5093. return
  5094. }
  5095. // 判断是否允许设置
  5096. userInfo, err := models.GetWxUserByUserId(req.UserId)
  5097. if err != nil {
  5098. br.Msg = "联系人信息有误"
  5099. br.ErrMsg = "联系人信息有误, Err: " + err.Error()
  5100. return
  5101. }
  5102. companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
  5103. if err != nil {
  5104. br.Msg = "获取客户信息失败"
  5105. br.ErrMsg = "获取客户信息失败, Err: " + err.Error()
  5106. return
  5107. }
  5108. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(userInfo.CompanyId, 1)
  5109. if err != nil {
  5110. br.Msg = "获取客户产品信息失败"
  5111. br.ErrMsg = "获取客户产品信息失败, Err: " + err.Error()
  5112. return
  5113. }
  5114. companyStatus := companyProductInfo.Status
  5115. if companyStatus != utils.COMPANY_STATUS_FOREVER {
  5116. // 非永续客户
  5117. if companyStatus == utils.COMPANY_STATUS_TRY_OUT || companyStatus == utils.COMPANY_STATUS_FORMAL {
  5118. // 正式/试用
  5119. expireTimeStr := companyProductInfo.EndDate
  5120. expireTime, _ := time.ParseInLocation(utils.FormatDate, expireTimeStr, time.Local)
  5121. // 服务结束时间小于设置的结束时间则不允许设置
  5122. if endTime.After(expireTime.AddDate(0, 0, 1)) {
  5123. br.Msg = "终止日期不允许超过服务日期, 服务终止日期为: " + expireTimeStr
  5124. br.IsSendEmail = false
  5125. return
  5126. }
  5127. } else {
  5128. br.Msg = "当前客户不允许设置图表权限, 客户状态为: " + companyStatus
  5129. br.IsSendEmail = false
  5130. return
  5131. }
  5132. }
  5133. permissionInfo, err := company_user.GetChartClassifyPermissionByUserId(req.UserId)
  5134. if err != nil {
  5135. br.Msg = "获取联系人图表分类权限失败"
  5136. br.ErrMsg = "获取联系人图表分类权限失败, Err:" + err.Error()
  5137. return
  5138. }
  5139. permissionInfo.CompanyId = companyInfo.CompanyId
  5140. permissionInfo.Enabled = req.Enabled
  5141. permissionInfo.StartTime = startTime
  5142. permissionInfo.EndTime = endTime
  5143. permissionInfo.UpdateTime = time.Now()
  5144. err = permissionInfo.Update([]string{"CompanyId", "Enabled", "StartTime", "EndTime", "UpdateTime"})
  5145. if err != nil {
  5146. br.Msg = "编辑联系人图表分类权限失败"
  5147. br.ErrMsg = "编辑联系人图表分类权限失败, Err:" + err.Error()
  5148. return
  5149. }
  5150. // 操作记录
  5151. {
  5152. record := &company_user.CompanyUserChartPermissionOperationRecord{
  5153. Type: 2,
  5154. CompanyId: companyInfo.CompanyId,
  5155. UserId: req.UserId,
  5156. AdminId: sysUser.AdminId,
  5157. StartTime: startTime,
  5158. EndTime: endTime,
  5159. Enabled: req.Enabled,
  5160. CreateTime: time.Now(),
  5161. }
  5162. go company_user.AddChartPermissionOperationRecord(record)
  5163. }
  5164. br.Ret = 200
  5165. br.Success = true
  5166. br.Msg = "操作成功"
  5167. }
  5168. // @Title 获取联系人图表分类权限详情
  5169. // @Description 获取联系人图表分类权限详情接口
  5170. // @Param UserId query int true "联系人ID,必填"
  5171. // @Success 200 {object} response.CompanyUserChartClassifyPermissionDetailResp
  5172. // @router /user/getChartClassifyPermissionDetail [get]
  5173. func (this *CompanyUserController) GetCompanyUserChartClassifyPermissionDetail() {
  5174. br := new(models.BaseResponse).Init()
  5175. defer func() {
  5176. this.Data["json"] = br
  5177. this.ServeJSON()
  5178. }()
  5179. sysUser := this.SysUser
  5180. if sysUser == nil {
  5181. br.Msg = "请登录"
  5182. br.ErrMsg = "请登录,SysUser Is Empty"
  5183. br.Ret = 408
  5184. return
  5185. }
  5186. reqUserId := this.GetString("UserId", "")
  5187. userId, _ := strconv.Atoi(reqUserId)
  5188. if userId <= 0 {
  5189. br.Msg = "参数丢失:联系人ID"
  5190. return
  5191. }
  5192. userInfo, err := models.GetWxUserByUserId(userId)
  5193. if err != nil {
  5194. br.Msg = "联系人信息有误"
  5195. br.ErrMsg = "联系人信息有误, Err: " + err.Error()
  5196. return
  5197. }
  5198. permissionInfo, err := company_user.GetChartClassifyPermissionByUserId(userId)
  5199. if err != nil && err.Error() != utils.ErrNoRow() {
  5200. br.Msg = "获取联系人图表分类权限失败"
  5201. br.ErrMsg = "获取联系人图表分类权限失败, Err:" + err.Error()
  5202. return
  5203. }
  5204. resp := new(response.CompanyUserChartClassifyPermissionDetailResp)
  5205. // 客户服务日期
  5206. companyProductInfo, err := company.GetCompanyProductByCompanyIdAndProductId(userInfo.CompanyId, 1)
  5207. if err != nil {
  5208. br.Msg = "获取客户产品信息失败"
  5209. br.ErrMsg = "获取客户产品信息失败, Err: " + err.Error()
  5210. return
  5211. }
  5212. companyStatus := companyProductInfo.Status
  5213. // 0-不允许设置 1-允许设置最大90天 2-90天
  5214. nowTime := time.Now()
  5215. free := 0
  5216. if companyStatus == utils.COMPANY_STATUS_FOREVER {
  5217. // 永续客户
  5218. free = 2
  5219. resp.ExpireDay = "2199-12-31"
  5220. } else if companyStatus == utils.COMPANY_STATUS_TRY_OUT || companyStatus == utils.COMPANY_STATUS_FORMAL {
  5221. free = 1
  5222. resp.ExpireDay = companyProductInfo.EndDate
  5223. } else {
  5224. resp.ExpireDay = nowTime.Format(utils.FormatDate)
  5225. }
  5226. var startTime, endTime time.Time
  5227. if permissionInfo != nil {
  5228. // 已设置
  5229. startTime = permissionInfo.StartTime
  5230. endTime = permissionInfo.EndTime
  5231. resp.ChartPermissionId = permissionInfo.Id
  5232. resp.UserId = permissionInfo.UserId
  5233. resp.StartTime = permissionInfo.StartTime.Format(utils.FormatDate)
  5234. resp.EndTime = permissionInfo.EndTime.Format(utils.FormatDate)
  5235. resp.Enabled = permissionInfo.Enabled
  5236. } else {
  5237. // 初次设置
  5238. startTime = nowTime
  5239. endTime = startTime.Add(89 * 24 * time.Hour) // 实际验证时以结束日期的23时59分为过期时间所以此处日期-1
  5240. if free == 0 {
  5241. endTime = nowTime
  5242. } else if free == 1 {
  5243. expireTime, _ := time.ParseInLocation(utils.FormatDate, companyProductInfo.EndDate, time.Local)
  5244. sub := expireTime.Sub(nowTime)
  5245. diff := int(sub.Hours() / 24)
  5246. if diff < 90 {
  5247. // 小于90天则终止时间为服务结束时间
  5248. endTime = expireTime
  5249. }
  5250. }
  5251. resp.StartTime = startTime.Format(utils.FormatDate)
  5252. resp.EndTime = endTime.Format(utils.FormatDate)
  5253. resp.UserId = userId
  5254. resp.Enabled = 0 // 默认关闭
  5255. }
  5256. // 计算阅读时长天数
  5257. subDuration := endTime.Sub(startTime)
  5258. subHours := subDuration.Hours()
  5259. subDays := int(subHours / 24)
  5260. resp.DayDuration = subDays + 1
  5261. br.Ret = 200
  5262. br.Success = true
  5263. br.Msg = "获取成功"
  5264. br.Data = resp
  5265. }
  5266. // GetOtherProduct
  5267. // @Title 获取其他分产品的筛选列表
  5268. // @Description 获取联系人图表分类权限详情接口
  5269. // @Param UserId query int true "联系人ID,必填"
  5270. // @Success 200 {object} response.CompanyUserChartClassifyPermissionDetailResp
  5271. // @router /user/otherProductList [get]
  5272. func (this *CompanyUserController) GetOtherProduct() {
  5273. br := new(models.BaseResponse).Init()
  5274. defer func() {
  5275. this.Data["json"] = br
  5276. this.ServeJSON()
  5277. }()
  5278. sysUser := this.SysUser
  5279. if sysUser == nil {
  5280. br.Msg = "请登录"
  5281. br.ErrMsg = "请登录,SysUser Is Empty"
  5282. br.Ret = 408
  5283. return
  5284. }
  5285. resp := make([]response.OtherProductTypeListResp, 0)
  5286. statisticFlag, _ := this.GetBool("StatisticFlag")
  5287. questionItem := response.OtherProductTypeListResp{
  5288. ProductId: 0,
  5289. ProductType: 3,
  5290. ProductName: "问答社区",
  5291. }
  5292. roadVideoItem := response.OtherProductTypeListResp{
  5293. ProductId: 2,
  5294. ProductType: 2,
  5295. ProductName: "线上路演",
  5296. }
  5297. videoItem := response.OtherProductTypeListResp{
  5298. ProductId: 1,
  5299. ProductType: 2,
  5300. ProductName: "5分钟小视频",
  5301. }
  5302. if !statisticFlag {
  5303. resp = append(resp, questionItem, roadVideoItem, videoItem)
  5304. }
  5305. voiceSectionList, err := yb.GetAllEnableVoiceSectionList()
  5306. if err != nil {
  5307. br.Msg = "获取数据失败"
  5308. br.ErrMsg = "获取语音播报版块, Err: " + err.Error()
  5309. return
  5310. }
  5311. for _, v := range voiceSectionList {
  5312. resp = append(resp, response.OtherProductTypeListResp{
  5313. ProductId: v.SectionId,
  5314. ProductType: 1,
  5315. ProductName: v.SectionName,
  5316. })
  5317. }
  5318. if statisticFlag {
  5319. resp = append(resp, questionItem, roadVideoItem, videoItem)
  5320. }
  5321. // 全部
  5322. resp = append(resp, response.OtherProductTypeListResp{
  5323. ProductId: 0,
  5324. ProductType: 99,
  5325. ProductName: "全部",
  5326. })
  5327. br.Ret = 200
  5328. br.Success = true
  5329. br.Msg = "获取成功"
  5330. br.Data = resp
  5331. }
  5332. //func init() {
  5333. // BusinessCardUrl := `https://hongze.oss-accelerate.aliyuncs.com/static/images/202107/20210715/R5xT6jw331gPwHZlsIg90cuSbuFW.jpeg`
  5334. // BusinessCardUrl = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202102/20210224/Bdr3Dv8oZDvsAnkzUpx8uA3S4odB.jpg`
  5335. // BusinessCardUrl = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202202/20220215/0glY7kgE07OLBHTnxxwS6u2cAxRx.png`
  5336. // BusinessCardUrl = `https://hongze.oss-accelerate.aliyuncs.com/static/images/202206/20220614/ZHEUpbNtUfE09ZVp6qfQxSPUrLfS.jpg`
  5337. // card, err := services.GetBusinessCard(BusinessCardUrl)
  5338. // if err != nil {
  5339. // fmt.Println("名片识别失败,Err:" + err.Error())
  5340. // return
  5341. // }
  5342. // fmt.Println(card)
  5343. //
  5344. // fmt.Println("company:", card.WordsResult.COMPANY)
  5345. // fmt.Println("===================")
  5346. //
  5347. // {
  5348. // aliyunResult, err := services.AliyunBusinessCardOcr(BusinessCardUrl)
  5349. // if err != nil {
  5350. // fmt.Println("名片识别失败2,Err:" + err.Error())
  5351. // return
  5352. // }
  5353. // fmt.Println(aliyunResult)
  5354. // fmt.Println("company:", aliyunResult.Company)
  5355. // }
  5356. //
  5357. //}
  5358. // @Title 联系人名单匹配导入
  5359. // @Description 联系人名单匹配导入
  5360. // @Param File query file true "文件"
  5361. // @Success 200 {object} company.UserImportMatchResp
  5362. // @router /list/match/import [post]
  5363. func (this *CompanyUserController) ImportListMatch() {
  5364. br := new(models.BaseResponse).Init()
  5365. defer func() {
  5366. this.Data["json"] = br
  5367. this.ServeJSON()
  5368. }()
  5369. sysUser := this.SysUser
  5370. if sysUser == nil {
  5371. br.Msg = "请重新登录"
  5372. return
  5373. }
  5374. file, h, err := this.GetFile("File")
  5375. if err != nil {
  5376. br.Msg = "获取文件失败"
  5377. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  5378. return
  5379. }
  5380. defer func() {
  5381. _ = file.Close()
  5382. }()
  5383. ext := path.Ext(h.Filename)
  5384. if ext != ".xlsx" && ext != ".xls" {
  5385. br.Msg = "请上传Excel文件"
  5386. return
  5387. }
  5388. uploadDir := "static/xls"
  5389. err = os.MkdirAll(uploadDir, 766)
  5390. if err != nil {
  5391. br.Msg = "存储目录创建失败"
  5392. br.ErrMsg = "存储目录创建失败,Err:" + err.Error()
  5393. return
  5394. }
  5395. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  5396. err = this.SaveToFile("File", uploadPath)
  5397. if err != nil {
  5398. br.Msg = "文件保存失败"
  5399. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  5400. return
  5401. }
  5402. defer func() {
  5403. _ = os.Remove(uploadPath)
  5404. }()
  5405. xlFile, err := xlsx.OpenFile(uploadPath)
  5406. if err != nil {
  5407. br.Msg = "文件导入失败"
  5408. br.ErrMsg = "文件导入失败,Err:" + err.Error()
  5409. return
  5410. }
  5411. dataList := make([]*company.UserImportMatch, 0)
  5412. mobileArr := make([]string, 0)
  5413. // 遍历sheet页读取
  5414. for _, sheet := range xlFile.Sheets {
  5415. // 遍历行读取
  5416. maxRow := sheet.MaxRow
  5417. for i := 0; i < maxRow; i++ {
  5418. // 首行
  5419. if i == 0 {
  5420. row := sheet.Row(i)
  5421. cells := row.Cells
  5422. for k, cell := range cells {
  5423. text := cell.String()
  5424. if k == 0 {
  5425. if text != "姓名" {
  5426. br.Msg = "匹配失败, 请按模板导入"
  5427. br.ErrMsg = "模板格式错误,title:" + text
  5428. return
  5429. }
  5430. }
  5431. if k == 1 {
  5432. if text != "国际(区号)" {
  5433. br.Msg = "匹配失败, 请按模板导入"
  5434. br.ErrMsg = "模板格式错误,title:" + text
  5435. return
  5436. }
  5437. }
  5438. if k == 2 {
  5439. if text != "手机号" {
  5440. br.Msg = "匹配失败, 请按模板导入"
  5441. br.ErrMsg = "模板格式错误,title:" + text
  5442. return
  5443. }
  5444. }
  5445. if k == 3 {
  5446. if text != "客户公司" {
  5447. br.Msg = "匹配失败, 请按模板导入"
  5448. br.ErrMsg = "模板格式错误,title:" + text
  5449. return
  5450. }
  5451. }
  5452. }
  5453. }
  5454. // 数据行
  5455. if i >= 1 {
  5456. row := sheet.Row(i)
  5457. cells := row.Cells
  5458. var userName, countryCode, mobile, companyName string
  5459. for k, cell := range cells {
  5460. if k == 0 {
  5461. userName = utils.TrimStr(cell.String())
  5462. }
  5463. if k == 1 {
  5464. countryCode = utils.TrimStr(cell.String())
  5465. }
  5466. if k == 2 {
  5467. mobile = utils.TrimStr(cell.String())
  5468. }
  5469. if k == 3 {
  5470. companyName = utils.TrimStr(cell.String())
  5471. }
  5472. }
  5473. // 手机号为空则读取下一行
  5474. if mobile == "" {
  5475. continue
  5476. }
  5477. // mobile列存在"86-11位手机号"这种格式
  5478. if strings.Contains(mobile, "-") {
  5479. mobileArr := strings.Split(mobile, "-")
  5480. if len(mobileArr) == 2 {
  5481. countryCode = mobileArr[0]
  5482. mobile = mobileArr[1]
  5483. }
  5484. }
  5485. mobileArr = append(mobileArr, mobile)
  5486. item := &company.UserImportMatch{
  5487. UserName: userName,
  5488. CountryCode: countryCode,
  5489. Mobile: mobile,
  5490. CompanyName: companyName,
  5491. }
  5492. dataList = append(dataList, item)
  5493. }
  5494. }
  5495. }
  5496. mobileLen := len(mobileArr)
  5497. if mobileLen == 0 {
  5498. br.Msg = "匹配失败, 无有效数据"
  5499. br.ErrMsg = "匹配失败, 无有效数据"
  5500. return
  5501. }
  5502. // 根据手机号查询联系人信息
  5503. var userCond string
  5504. var userPars []interface{}
  5505. userCond += ` AND mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  5506. userPars = append(userPars, mobileArr)
  5507. userList, e := company.GetCompanyUsersByCondition(userCond, userPars)
  5508. if e != nil {
  5509. br.Msg = "匹配失败, 请按模板导入"
  5510. br.ErrMsg = "匹配失败, 获取联系人信息失败, Err: " + e.Error()
  5511. return
  5512. }
  5513. companyIds := make([]int, 0)
  5514. mobileCompanyIdMap := make(map[string]int, 0)
  5515. for i := range userList {
  5516. companyIds = append(companyIds, userList[i].CompanyId)
  5517. mobileCompanyIdMap[userList[i].Mobile] = userList[i].CompanyId
  5518. }
  5519. companyLen := len(companyIds)
  5520. // 查询客户、客户产品
  5521. var productCond string
  5522. var productPars []interface{}
  5523. if companyLen > 0 {
  5524. productCond += ` AND a.company_id IN (` + utils.GetOrmInReplace(companyLen) + `)`
  5525. productPars = append(productPars, companyIds)
  5526. }
  5527. productList, e := company.GetCompanyProductsByCondition(productCond, productPars)
  5528. if e != nil {
  5529. br.Msg = "匹配失败, 请按模板导入"
  5530. br.ErrMsg = "匹配失败, 获取客户产品信息失败, Err: " + e.Error()
  5531. return
  5532. }
  5533. ficcProductMap := make(map[int]*company.CompanyProduct, 0)
  5534. raiProductMap := make(map[int]*company.CompanyProduct, 0)
  5535. for i := range productList {
  5536. if productList[i].ProductId == 1 {
  5537. ficcProductMap[productList[i].CompanyId] = productList[i]
  5538. } else {
  5539. raiProductMap[productList[i].CompanyId] = productList[i]
  5540. }
  5541. }
  5542. // 根据客户信息返回数据
  5543. for i := range dataList {
  5544. companyId := mobileCompanyIdMap[dataList[i].Mobile]
  5545. if companyId == 0 {
  5546. continue
  5547. }
  5548. ficc := ficcProductMap[companyId]
  5549. if ficc != nil {
  5550. dataList[i].SysCompanyName = ficc.CompanyName
  5551. dataList[i].FiccStatus = ficc.Status
  5552. dataList[i].FiccSeller = ficc.SellerName
  5553. }
  5554. rai := raiProductMap[companyId]
  5555. if rai != nil {
  5556. dataList[i].SysCompanyName = rai.CompanyName
  5557. dataList[i].RaiStatus = rai.Status
  5558. dataList[i].RaiSeller = rai.SellerName
  5559. }
  5560. }
  5561. // 缓存导入数据, 导出时用缓存数据生成Excel
  5562. var code, cacheKey string
  5563. dataJson, e := json.Marshal(dataList)
  5564. if e == nil {
  5565. code = fmt.Sprint(time.Now().UnixNano())
  5566. cacheKey = utils.CACHE_KEY_COMPANY_MATCH_PRE + code
  5567. _ = utils.Rc.Put(cacheKey, string(dataJson), time.Minute*30)
  5568. }
  5569. resp := new(company.UserImportMatchResp)
  5570. resp.List = dataList
  5571. resp.Code = code
  5572. br.Data = resp
  5573. br.Msg = "匹配成功"
  5574. br.Ret = 200
  5575. br.Success = true
  5576. }
  5577. // @Title 联系人名单匹配结果下载
  5578. // @Description 联系人名单匹配结果下载
  5579. // @Param Code query string true "导入时返回的Code"
  5580. // @Success 200 Ret=200 匹配成功
  5581. // @router /list/match/download [get]
  5582. func (this *CompanyUserController) ImportListMatchDownload() {
  5583. br := new(models.BaseResponse).Init()
  5584. defer func() {
  5585. this.Data["json"] = br
  5586. this.ServeJSON()
  5587. }()
  5588. sysUser := this.SysUser
  5589. if sysUser == nil {
  5590. br.Msg = "请重新登录"
  5591. return
  5592. }
  5593. code := this.GetString("Code")
  5594. if code == "" {
  5595. br.Msg = "参数有误"
  5596. return
  5597. }
  5598. cacheKey := utils.CACHE_KEY_COMPANY_MATCH_PRE + code
  5599. // 获取导入时的缓存数据
  5600. dataJson, e := utils.Rc.RedisString(cacheKey)
  5601. if e != nil {
  5602. br.Msg = "获取导出数据失败"
  5603. br.ErrMsg = "获取导出数据失败, Err: " + e.Error()
  5604. return
  5605. }
  5606. if dataJson == "" {
  5607. br.Msg = "匹配数据已失效, 请重新匹配"
  5608. return
  5609. }
  5610. dataList := make([]*company.UserImportMatch, 0)
  5611. if e = json.Unmarshal([]byte(dataJson), &dataList); e != nil {
  5612. br.Msg = "匹配数据已失效, 请重新匹配"
  5613. return
  5614. }
  5615. if len(dataList) == 0 {
  5616. br.Msg = "匹配数据已失效, 请重新匹配"
  5617. return
  5618. }
  5619. // 生成Excel表格
  5620. dir, err := os.Executable()
  5621. exPath := filepath.Dir(dir)
  5622. downloadPath := exPath + "/company_match_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  5623. excelFile := xlsx.NewFile()
  5624. if err != nil {
  5625. br.Msg = "生成文件失败"
  5626. br.ErrMsg = "生成文件失败"
  5627. return
  5628. }
  5629. style := xlsx.NewStyle()
  5630. alignment := xlsx.Alignment{
  5631. Horizontal: "center",
  5632. Vertical: "center",
  5633. WrapText: true,
  5634. }
  5635. style.Alignment = alignment
  5636. style.ApplyAlignment = true
  5637. // 新增Sheet和表头
  5638. sheet, err := excelFile.AddSheet("客户名单")
  5639. if err != nil {
  5640. br.Msg = "新增Sheet失败"
  5641. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  5642. return
  5643. }
  5644. titleRow := sheet.AddRow()
  5645. titleRow.AddCell().SetValue("姓名")
  5646. titleRow.AddCell().SetValue("国际(区号)")
  5647. titleRow.AddCell().SetValue("手机号")
  5648. titleRow.AddCell().SetValue("客户公司")
  5649. titleRow.AddCell().SetValue("系统客户")
  5650. titleRow.AddCell().SetValue("FICC状态")
  5651. titleRow.AddCell().SetValue("FICC销售")
  5652. titleRow.AddCell().SetValue("权益状态")
  5653. titleRow.AddCell().SetValue("权益销售")
  5654. // 表格赋值
  5655. for _, v := range dataList {
  5656. dataRow := sheet.AddRow()
  5657. dataRow.SetHeight(20)
  5658. dataRow.AddCell().SetString(v.UserName)
  5659. dataRow.AddCell().SetString(v.CountryCode)
  5660. dataRow.AddCell().SetString(v.Mobile)
  5661. dataRow.AddCell().SetString(v.CompanyName)
  5662. dataRow.AddCell().SetString(v.SysCompanyName)
  5663. dataRow.AddCell().SetString(v.FiccStatus)
  5664. dataRow.AddCell().SetString(v.FiccSeller)
  5665. dataRow.AddCell().SetString(v.RaiStatus)
  5666. dataRow.AddCell().SetString(v.RaiSeller)
  5667. }
  5668. // 输出Excel
  5669. if e = excelFile.Save(downloadPath); e != nil {
  5670. br.Msg = "保存文件失败"
  5671. br.ErrMsg = "保存文件失败"
  5672. return
  5673. }
  5674. defer func() {
  5675. _ = os.Remove(downloadPath)
  5676. }()
  5677. suffix := time.Now().Format(utils.FormatDateTimeUnSpace)
  5678. downloadFileName := "客户名单匹配_" + suffix + ".xlsx"
  5679. this.Ctx.Output.Download(downloadPath, downloadFileName)
  5680. br.Msg = "导出成功"
  5681. br.Ret = 200
  5682. br.Success = true
  5683. }
  5684. // Follow
  5685. // @Title 关注联系人
  5686. // @Description 关注联系人
  5687. // @Param request body request.ApplyMarkReq true "type json string"
  5688. // @Success 200 {object} 标记处理成功
  5689. // @router /follow [post]
  5690. func (this *CompanyUserController) Follow() {
  5691. br := new(models.BaseResponse).Init()
  5692. defer func() {
  5693. this.Data["json"] = br
  5694. this.ServeJSON()
  5695. }()
  5696. sysUser := this.SysUser
  5697. if sysUser == nil {
  5698. br.Msg = "请登录"
  5699. br.ErrMsg = "请登录,SysUser is Empty"
  5700. br.Ret = 408
  5701. return
  5702. }
  5703. var req request.FollowReq
  5704. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  5705. if err != nil {
  5706. br.Msg = "参数解析异常"
  5707. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  5708. return
  5709. }
  5710. if req.UserId <= 0 {
  5711. br.Msg = "用户ID异常"
  5712. br.ErrMsg = "用户ID异常"
  5713. return
  5714. }
  5715. // 查公司里有多少已关注,上限三个
  5716. count, err := models.GetCompanyFollowCount(req.CompanyId)
  5717. if err != nil {
  5718. br.Msg = "操作失败! "
  5719. br.ErrMsg = "操作失败,Err:" + err.Error()
  5720. return
  5721. }
  5722. if count == 3 && req.Type == 1 {
  5723. br.Msg = "关注失败,特别关注已达上限(上限三个)"
  5724. br.ErrMsg = "关注失败,特别关注已达上限(上限三个)"
  5725. return
  5726. }
  5727. // 关注或取关
  5728. err = models.SetUserFollow(req.UserId, req.Type)
  5729. if err != nil {
  5730. br.Msg = "操作失败! "
  5731. br.ErrMsg = "操作失败,Err:" + err.Error()
  5732. return
  5733. }
  5734. br.Ret = 200
  5735. br.Success = true
  5736. br.Msg = "操作成功"
  5737. }
  5738. // @Title 开通或者关闭用户研选权限
  5739. // @Description 开通或者关闭用户研选权限
  5740. // @Param request body request.ApplyMarkReq true "type json string"
  5741. // @Success 200 {object} 操作成功
  5742. // @router /update/user/yananxuan/permission [post]
  5743. func (this *CompanyUserController) UpadteUserYanxuanPermission() {
  5744. br := new(models.BaseResponse).Init()
  5745. defer func() {
  5746. this.Data["json"] = br
  5747. this.ServeJSON()
  5748. }()
  5749. sysUser := this.SysUser
  5750. if sysUser == nil {
  5751. br.Msg = "请登录"
  5752. br.ErrMsg = "请登录,SysUser is Empty"
  5753. br.Ret = 408
  5754. return
  5755. }
  5756. var req cygx.UpdateUserYanxuanPermissionReq
  5757. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  5758. if err != nil {
  5759. br.Msg = "参数解析异常"
  5760. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  5761. return
  5762. }
  5763. userId := req.UserId
  5764. if userId <= 0 {
  5765. br.Msg = "用户ID异常"
  5766. br.ErrMsg = "用户ID异常"
  5767. return
  5768. }
  5769. //获取联系人详情
  5770. userInfo, err := models.GetWxUserByUserId(req.UserId)
  5771. if err != nil {
  5772. br.Msg = "获取联系人异常!"
  5773. br.ErrMsg = "获取联系人异常,Err:" + err.Error()
  5774. return
  5775. }
  5776. if userInfo.BusinessCardUrl == "" {
  5777. br.Msg = "该联系人尚未添加名片,请点击编辑,上传名片后再开通试用"
  5778. return
  5779. }
  5780. companyInfo, err := company.GetCompanyById(userInfo.CompanyId)
  5781. if err != nil {
  5782. br.Msg = "查找客户失败"
  5783. br.ErrMsg = "查找客户失败,err:" + err.Error()
  5784. return
  5785. }
  5786. // 查询用户是否开通过
  5787. count, err := cygx.GetCygxUserYanxuanPermissionCountByUserId(userId)
  5788. if err != nil {
  5789. br.Msg = "操作失败! "
  5790. br.ErrMsg = "操作失败,Err:" + err.Error()
  5791. return
  5792. }
  5793. item := new(cygx.CygxUserYanxuanPermission)
  5794. if req.Type == 0 {
  5795. item.Status = "未开通"
  5796. } else {
  5797. item.Status = "试用"
  5798. }
  5799. item.UserId = userId
  5800. item.CompanyId = companyInfo.CompanyId
  5801. item.CompanyName = companyInfo.CompanyName
  5802. item.StartDate = time.Now().Format(utils.FormatDate)
  5803. item.EndDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate) // 试用期两个月
  5804. item.Mobile = userInfo.Mobile
  5805. item.Email = userInfo.Email
  5806. item.RealName = userInfo.RealName
  5807. item.AdminId = sysUser.AdminId
  5808. item.AdminRealName = sysUser.RealName
  5809. item.CreatedTime = time.Now()
  5810. item.LastUpdatedTime = time.Now()
  5811. item.ModifyTime = time.Now()
  5812. if count == 0 {
  5813. _, err = cygx.AddCygxUserYanxuanPermission(item) // 添加
  5814. } else {
  5815. if req.Type == 0 {
  5816. err = cygx.UpdateCygxUserYanxuanPermissionClose(userId) // 关闭
  5817. } else {
  5818. err = cygx.UpdateCygxUserYanxuanPermissionOpen(item) // 开通
  5819. }
  5820. }
  5821. if err != nil {
  5822. br.Msg = "操作失败! "
  5823. br.ErrMsg = "操作失败,Err:" + err.Error()
  5824. return
  5825. }
  5826. go cygxService.AddCygxUserYanxuanPermissionLog(item)
  5827. br.Ret = 200
  5828. br.Success = true
  5829. br.Msg = "操作成功"
  5830. }