company_user.go 193 KB

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