company_user.go 209 KB

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