company_user.go 191 KB

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