company_user.go 198 KB

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