company_user.go 201 KB

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