company_user.go 193 KB

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