company_user.go 201 KB

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