company_user.go 209 KB

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