company_user.go 209 KB

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