company_user.go 178 KB

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