company_user.go 201 KB

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