company_user.go 188 KB

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