company_user.go 213 KB

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