company_user.go 192 KB

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