company_user.go 210 KB

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