company_user.go 192 KB

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