company_user.go 196 KB

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