company_user.go 202 KB

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