company_user.go 202 KB

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