company_user.go 179 KB

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