company_user.go 203 KB

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