company_user.go 178 KB

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