Statistics.php 430 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611
  1. <?php
  2. namespace app\sys\controller;
  3. use app\logics\CustomerLogic;
  4. use app\model\Customer;
  5. use app\model\CustomerClue;
  6. use app\model\CustomerVisitLog;
  7. use app\model\Employee;
  8. use app\model\Inspectors as InspectorsModel;
  9. use app\model\Org;
  10. use app\model\Setting;
  11. use app\model\StatisticsSetting;
  12. use think\facade\View;
  13. use think\helper\Str;
  14. use app\model\Company;
  15. use app\model\Activity as ModelActivity;
  16. use app\model\CustomerPackage;
  17. use app\model\CustomerPortraitField;
  18. use app\model\CustomerRecycle;
  19. use app\model\CustomerSource;
  20. use app\model\CustomerDropPool;
  21. use think\facade\Request;
  22. use app\model\ShareLog;
  23. use app\model\TrainCourseView;
  24. use app\model\TrainDoneLog;
  25. use app\model\CustomerPortraitFieldSelect;
  26. use app\model\CustomersSubscribe;
  27. use app\model\ExamEmpResult;
  28. use app\model\TalkskillViewLog;
  29. use app\model\OperateLog;
  30. use app\model\Shop;
  31. use app\model\AsyncTask;
  32. use app\model\OutCallLog;
  33. use app\model\CustomerInvalidLog;
  34. use app\model\OutCallMbLog;
  35. use think\facade\Log;
  36. use think\facade\Db;
  37. use toolkits\Aec;
  38. use app\model\CustomerExtension;
  39. use app\model\ExportLog;
  40. use openssl\Aes;
  41. /**
  42. * 数据统计
  43. */
  44. class Statistics
  45. {
  46. private $request;
  47. private $root_id;
  48. /**
  49. * 构造方法
  50. */
  51. public function __construct()
  52. {
  53. $this->request = request();
  54. $this->root_id = $this->request->employee->root_id;
  55. $xinjushang = 0;
  56. View::assign('xinjushang', $xinjushang);
  57. }
  58. /**
  59. * 数据获取接口
  60. *
  61. * @param string $metod
  62. * @return void
  63. */
  64. public function __call($method, $arguments)
  65. {
  66. if (!$this->request->isAjax()) {
  67. if (Str::snake($method) == 'customer' || Str::snake($method) == 'clue') {
  68. //返回组织列表
  69. $org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
  70. View::assign('org', $org);
  71. }
  72. $aes = new Aes('zqxg@screen');
  73. $rootAes = $aes->encrypt($this->root_id);
  74. view::assign('rootId', $rootAes);
  75. return View::fetch(Str::snake($method) . '_data');
  76. }
  77. $method .= 'Data';
  78. if (method_exists($this, $method)) {
  79. if ($method == 'followUpData') {
  80. $rs = $this->followUpData($arguments);
  81. } elseif ($method == 'customerData') {
  82. $rs = $this->customerData($arguments);
  83. } elseif ($method == 'clueData') {
  84. $rs = $this->clueData($arguments);
  85. } elseif ($method == 'editInspectorData') {
  86. $rs = $this->editInspectorData($arguments);
  87. } elseif ($method == 'inspectorsData') {
  88. $rs = $this->inspectorsData($arguments);
  89. } elseif ($method == 'addInspectorData') {
  90. $rs = $this->addInspectorData($arguments);
  91. } elseif ($method == 'houseData') {
  92. $rs = $this->houseData($arguments);
  93. } elseif ($method == 'sourceData') {
  94. $rs = $this->sourceData($arguments);
  95. }
  96. // $rs = $this->$method($arguments);
  97. return json($rs);
  98. }
  99. return json(['code' => 0, 'data' => [], 'count' => 0]);
  100. }
  101. /**
  102. * 客户数据统计
  103. *
  104. * @return string
  105. */
  106. private function customerData($param)
  107. {
  108. $startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
  109. $endCondition = ['addtime', '<', date('Y-m-d H:i:s')];
  110. $beforeCondition = '';
  111. //2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
  112. if (!empty($param['start_date'])) {
  113. $count_dates = explode(' - ', $param['start_date']);
  114. $param['start_date'] = date('Y-m-d', strtotime($count_dates[0]));
  115. $param['end_date'] = date('Y-m-d', strtotime($count_dates[1]));
  116. $startCondition = ['addtime', '>', date('Y-m-d H:i:s', strtotime($param['start_date']))];
  117. $beforeCondition = date('Y-m-d H:i:s', strtotime($param['start_date']));
  118. }
  119. if (!empty($param['end_date'])) {
  120. $endCondition = ['addtime', '<', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
  121. }
  122. $param['page'] = !empty($param['page']) ? $param['page'] : 1;
  123. $param['limit'] = !empty($param['limit']) ? $param['limit'] : 10;
  124. // 占位数据
  125. $initdata = ', 0 as customer_num, 0 as unconfirm_num, 0 as deposit_num, 0 as signed_num, 0 as deposit_total_money, 0 as signed_total_money, 0 as shop_num,
  126. 0 as measure_num, 0 as clue_num, 0 as effective_num, 0 as meet_num, "0%" as meet_lv, "0%" as deposit_lv, "0%" as signed_lv, 0 as average_val, "0%" as effective_lv, 0 as maika_num';
  127. /** 不同数据内容整理 */
  128. if (empty($param['type']) || $param['type'] == 1) {
  129. $group = 'org_id';
  130. // 部门名称列表获取(只获取没有子部门的部门)
  131. if (!empty($param['select_org_id'])) {
  132. $select_org_path = Org::where('id', $param['select_org_id'])->value('path');
  133. $condition = [['path', 'like', $select_org_path . '%']];
  134. } else {
  135. $condition = [['path', 'like', $this->root_id . '-%']];
  136. }
  137. $org_where = [];
  138. $org_data = Org::where($condition)->select()->toArray();
  139. foreach ($org_data as $item) {
  140. $search = 'false';
  141. foreach ($org_data as $val) {
  142. if ($search == 'false') {
  143. if ($val['pid'] == $item['id']) {
  144. $search = 'true';
  145. }
  146. }
  147. }
  148. if ($search == 'false') $org_where[] = $item['id'];
  149. }
  150. $data = Org::where([['path', 'like', $this->root_id . '-%'], ['id', 'in', $org_where]])->page($param['page'], $param['limit'])->column('id,name as org_name, 0 as employee_num' . $initdata);
  151. // 部门id
  152. $orgIdList = array_column($data, 'id');
  153. // 查询条件
  154. $typeCondition = ['org_id', 'in', $orgIdList];
  155. // 量房,到店,见面数(量房+到店)
  156. $customerVisitData = $this->getCustomerVisitNum('org', $orgIdList, $startCondition, $endCondition, $beforeCondition, $typeCondition);
  157. $count = Org::where([['path', 'like', $this->root_id . '-%'], ['id', 'in', $org_where]])->count();
  158. } else {
  159. $group = 'employee_id';
  160. $orgCondition = [];
  161. if (!empty($param['org_id'])) $orgCondition = ['org_id', '=', $param['org_id']];
  162. // 部门人员获取
  163. $data = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->page($param['page'], $param['limit'])->column('id, org_id, name' . $initdata);
  164. // 员工id
  165. $employeeIdList = array_column($data, 'id');
  166. // 查询条件
  167. $typeCondition = ['employee_id', 'in', $employeeIdList];
  168. // 量房,到店,见面数(量房+到店)
  169. $customerVisitData = $this->getCustomerVisitNum('employee', $employeeIdList, $startCondition, $endCondition, $beforeCondition, $typeCondition);
  170. // 员工部门获取
  171. $orgs = Org::where([['path', 'like', $this->root_id . '-%']])->column('name', 'id');
  172. $count = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->count();
  173. $typeCondition = ['org_id', '=', $param['org_id']];
  174. }
  175. // 线索获取-转移客户没有转移线索导致数据对不上
  176. $sql = CustomerClue::where([$startCondition, $endCondition, $typeCondition, ['employee_id', 'in', $customerVisitData['eid']]])->group($group)->field("count(id) as clue_num,count(if(state=1, '1', null)) as effective_num, org_id, $group")->buildSql();
  177. $clueData = Db::query($sql);
  178. $clueData = array_combine(array_column($clueData, $group), $clueData);
  179. foreach ($data as &$item) {
  180. // 员工数据,添加部门信息
  181. if (!empty($param['type']) && $param['type'] != 1) {
  182. if (isset($orgs[$item['org_id']])) {
  183. $item['org_name'] = $orgs[$item['org_id']];
  184. } else {
  185. $item['org_name'] = '';
  186. }
  187. }
  188. if (isset($clueData[$item['id']])) {
  189. $item['clue_num'] = $clueData[$item['id']]['clue_num'];
  190. $item['effective_num'] = $clueData[$item['id']]['effective_num'];
  191. if (isset($customerVisitData['data'][$item['id']])) {
  192. $item['effective_num'] += $customerVisitData['data'][$item['id']]['effective_total_number'];
  193. }
  194. }
  195. if (isset($customerVisitData['data'][$item['id']])) {
  196. $item['deposit_total_money'] = $customerVisitData['data'][$item['id']]['deposit_total_money'];
  197. $item['signed_total_money'] = $customerVisitData['data'][$item['id']]['signed_total_money'];
  198. $item['signed_num'] = $customerVisitData['data'][$item['id']]['signed_num'];
  199. $item['deposit_num'] = $customerVisitData['data'][$item['id']]['deposit_num'];
  200. $item['unconfirm_num'] = $customerVisitData['data'][$item['id']]['unconfirm_num'];
  201. $item['shop_num'] = $customerVisitData['data'][$item['id']]['shop_num'];
  202. $item['measure_num'] = $customerVisitData['data'][$item['id']]['measure_num'];
  203. $item['meet_num'] = $customerVisitData['data'][$item['id']]['meet_num'];
  204. $item['maika_num'] = $customerVisitData['data'][$item['id']]['maika_num'];
  205. if (empty($param['type']) || $param['type'] == 1) {
  206. $item['employee_num'] = $customerVisitData['data'][$item['id']]['employee_num'];
  207. }
  208. $item['customer_num'] = $customerVisitData['data'][$item['id']]['customer_num'];
  209. }
  210. // 计算数据
  211. if ($item['signed_num'] != 0) $item['average_val'] = floor($item['signed_total_money'] / $item['signed_num']);
  212. if ($item['customer_num'] !== 0) $item['meet_lv'] = $item['customer_num'] ? round($item['meet_num'] / $item['customer_num'] * 100, 2) . '%' : '0%';
  213. if ($item['meet_num'] !== 0) $item['deposit_lv'] = round($item['deposit_num'] / $item['meet_num'] * 100, 2) . '%';
  214. if ($item['meet_num'] !== 0) $item['signed_lv'] = round($item['signed_num'] / $item['meet_num'] * 100, 2) . '%';
  215. if ($item['customer_num'] != 0) $item['effective_lv'] = round($item['effective_num'] / $item['customer_num'] * 100, 2) . '%';
  216. }
  217. return ['code' => 0, 'data' => $data, 'count' => $count];
  218. }
  219. /**
  220. * 量房到店数获取(用户客户数据统计获取优化)
  221. */
  222. public function getCustomerVisitNum($type, $ids, $startCondition, $endCondition, $beforeCondition, $typeCondition)
  223. {
  224. $orgId = $ids;
  225. if ($type == 'org') {
  226. // 部门人员获取
  227. $employees = Employee::where([['org_id', 'in', $ids], ['state', '=', '在职'], ['uid', '<>', 0]])->column('org_id', 'id');
  228. // 员工id
  229. $ids = array_keys($employees);
  230. }
  231. $state2 = CustomerVisitLog::changeState('待确认', 'chaos');
  232. $state3 = CustomerVisitLog::changeState('已到访', 'chaos');
  233. $state4 = CustomerVisitLog::changeState('已交定', 'chaos');
  234. $state5 = CustomerVisitLog::changeState('已量房', 'chaos');
  235. $state8 = CustomerVisitLog::changeState('已到场', 'chaos');
  236. $state6 = CustomerVisitLog::changeState('已签单', 'chaos');
  237. $state15 = CustomerVisitLog::changeState('已卖卡', 'chaos');
  238. //查询时间之前的见面数量
  239. $beforeCustomersId = [];
  240. if ($beforeCondition) {
  241. $vis_where = [['state', 'in', array_merge($state3, $state5, $state8)], ['addtime', '<', $beforeCondition]];
  242. $beforeCustomers = Customer::with([
  243. 'visitLog' => function ($query) use ($vis_where) {
  244. $query->where($vis_where)->field('customer_id,state');
  245. }
  246. ])->where(function ($query) {
  247. $not_sure = Customer::changeState('待确认', 'chaos');
  248. $or1[] = ['crm_res_id', 'null', null];
  249. $or2[] = ['crm_res_id', 'not null', null];
  250. $or2[] = ['state', 'not in', $not_sure];
  251. $query->whereOr([$or1, $or2]);
  252. })->where([$typeCondition])->field('id,employee_id,state')->select()->toArray();
  253. foreach ($beforeCustomers as $v) {
  254. foreach ($v['visitLog'] as $va) {
  255. if (in_array($va['state'], array_merge($state3, $state5, $state8))) {
  256. $beforeCustomersId[] = $v['id'];
  257. }
  258. }
  259. }
  260. }
  261. $vis_where = [$startCondition, $endCondition, ['state', 'in', array_merge($state2, $state3, $state4, $state5, $state6, $state8, $state15)]];
  262. $customers = Customer::with(['visitLog' => function ($query) use ($vis_where) {
  263. $query->where($vis_where)->field('customer_id,addtime,state,money')->order('addtime desc');
  264. }])->where(function ($query) {
  265. $not_sure = Customer::changeState('待确认', 'chaos');
  266. $or1[] = ['crm_res_id', 'null', null];
  267. $or2[] = ['crm_res_id', 'not null', null];
  268. $or2[] = ['state', 'not in', $not_sure];
  269. $query->whereOr([$or1, $or2]);
  270. })->where([$typeCondition])->field('id,employee_id,state')->select();
  271. // 无效客户数
  272. $no_where = [
  273. ['employee_id', 'in', $ids],
  274. $startCondition,
  275. $endCondition
  276. ];
  277. $no_valid_customer = CustomerInvalidLog::where($no_where)->column('customer_id,org_id,employee_id');
  278. $customerVisitData = [];
  279. $checkDaraIds = [];
  280. $measureMumIds = []; //量房数
  281. $shopNumIds = []; //到店数
  282. $meetNumIds = []; //见面数
  283. $depositNumIds = []; //订金数-签单数
  284. $signedNumIds = []; //签单数-转单数
  285. $unconfirmNumIds = []; //待确认
  286. $maikaNumIds = []; //卖卡
  287. foreach ($customers as $c) {
  288. if (!isset($customerVisitData[$c['employee_id']])) {
  289. $customerVisitData[$c['employee_id']] = ['maika_num' => 0, 'effective_total_number' => 0, 'signed_total_money' => 0, 'deposit_total_money' => 0, 'signed_num' => 0, 'deposit_num' => 0, 'unconfirm_num' => 0, 'shop_num' => 0, 'measure_num' => 0, 'meet_num' => 0, 'customer_num' => 0, 'customer' => []];
  290. }
  291. $state = [];
  292. $signed_total_money = [];
  293. $deposit_total_money = [];
  294. foreach ($c['visitLog'] as $v) {
  295. $state[$v['state']][] = $v;
  296. }
  297. foreach ($state as $k => $v) {
  298. if (in_array($k, $state15) && !in_array($c['id'], $maikaNumIds)) {
  299. $customerVisitData[$c['employee_id']]['maika_num']++;
  300. $measureMumIds[] = $c['id'];
  301. }
  302. if (in_array($k, $state6) && !in_array($c['id'], $signedNumIds)) {
  303. $customerVisitData[$c['employee_id']]['signed_num']++;
  304. $signedNumIds[] = $c['id'];
  305. $signed_total_money[] = $v[0]['money'];
  306. }
  307. if (in_array($k, $state4) && !in_array($c['id'], $depositNumIds)) {
  308. $customerVisitData[$c['employee_id']]['deposit_num']++;
  309. $depositNumIds[] = $c['id'];
  310. $deposit_total_money[] = $v[0]['money'];
  311. }
  312. if (in_array($k, $state2) && !in_array($c['id'], $unconfirmNumIds)) {
  313. $customerVisitData[$c['employee_id']]['unconfirm_num']++;
  314. $unconfirmNumIds[] = $c['id'];
  315. }
  316. if (in_array($k, $state3) && !in_array($c['id'], $shopNumIds)) {
  317. $customerVisitData[$c['employee_id']]['shop_num']++;
  318. $shopNumIds[] = $c['id'];
  319. }
  320. if (in_array($k, $state5) && !in_array($c['id'], $measureMumIds)) {
  321. $customerVisitData[$c['employee_id']]['measure_num']++;
  322. $measureMumIds[] = $c['id'];
  323. }
  324. if (!in_array($c['id'], $beforeCustomersId) && in_array($k, array_merge($state3, $state4, $state5, $state6, $state8)) && !in_array($c['id'], $meetNumIds)) {
  325. $customerVisitData[$c['employee_id']]['meet_num']++;
  326. $meetNumIds[] = $c['id'];
  327. }
  328. if (!in_array($c['id'], $checkDaraIds) && in_array($k, array_merge($state3, $state4, $state5, $state6, $state8, $state15))) {
  329. $customerVisitData[$c['employee_id']]['effective_total_number']++;
  330. $checkDaraIds[] = $c['id'];
  331. }
  332. }
  333. $customerVisitData[$c['employee_id']]['signed_total_money'] += array_sum($signed_total_money);
  334. $customerVisitData[$c['employee_id']]['deposit_total_money'] += array_sum($deposit_total_money);
  335. $customerVisitData[$c['employee_id']]['customer'][] = $c['id'];
  336. }
  337. $no_employee = [];
  338. foreach ($no_valid_customer as $k => $v) {
  339. $no_employee[$v['employee_id']][] = $v['customer_id'];
  340. }
  341. // 赋值无效客户
  342. foreach ($customerVisitData as $k => $v) {
  343. if (isset($no_employee[$k])) {
  344. $customer_ids = array_unique(array_merge($v['customer'], $no_employee[$k]));
  345. $customerVisitData[$k]['customer_num'] = count($customer_ids);
  346. } else {
  347. $customerVisitData[$k]['customer_num'] = count($customerVisitData[$k]['customer']);
  348. }
  349. }
  350. if ($type == 'org') {
  351. $visitData = [];
  352. foreach ($employees as $empId => $org_id) {
  353. if (!isset($visitData[$org_id])) $visitData[$org_id] = ['maika_num' => 0, 'effective_total_number' => 0, 'signed_total_money' => 0, 'deposit_total_money' => 0, 'signed_num' => 0, 'deposit_num' => 0, 'unconfirm_num' => 0, 'shop_num' => 0, 'measure_num' => 0, 'employee_num' => 0, 'meet_num' => 0, 'customer_num' => 0];
  354. if (isset($customerVisitData[$empId])) {
  355. $visitData[$org_id]['maika_num'] += $customerVisitData[$empId]['maika_num'];
  356. $visitData[$org_id]['effective_total_number'] += $customerVisitData[$empId]['effective_total_number'];
  357. $visitData[$org_id]['signed_total_money'] += $customerVisitData[$empId]['signed_total_money'];
  358. $visitData[$org_id]['deposit_total_money'] += $customerVisitData[$empId]['deposit_total_money'];
  359. $visitData[$org_id]['signed_num'] += $customerVisitData[$empId]['signed_num'];
  360. $visitData[$org_id]['deposit_num'] += $customerVisitData[$empId]['deposit_num'];
  361. $visitData[$org_id]['unconfirm_num'] += $customerVisitData[$empId]['unconfirm_num'];
  362. $visitData[$org_id]['shop_num'] += $customerVisitData[$empId]['shop_num'];
  363. $visitData[$org_id]['measure_num'] += $customerVisitData[$empId]['measure_num'];
  364. $visitData[$org_id]['meet_num'] += $customerVisitData[$empId]['meet_num'];
  365. $visitData[$org_id]['customer_num'] += $customerVisitData[$empId]['customer_num'];
  366. }
  367. $visitData[$org_id]['employee_num']++;
  368. }
  369. return ['data' => $visitData, 'eid' => $ids];
  370. }
  371. return ['data' => $customerVisitData, 'eid' => $ids];
  372. }
  373. /**
  374. * 跟进情况统计
  375. *
  376. * @return string
  377. */
  378. private function followUpData($param)
  379. {
  380. $startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
  381. $endCondition = ['addtime', '<', date('Y-m-d H:i:s')];
  382. if (!empty($param['start_date'])) {
  383. $startCondition = ['addtime', '>', date('Y-m-d H:i:s', strtotime($param['start_date']))];
  384. }
  385. if (!empty($param['end_date'])) {
  386. $endCondition = ['addtime', '<', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
  387. }
  388. // 客户等级获取(该项在系统中被写死,如需改变需调查后调整)
  389. if (!empty($param['level'])) {
  390. $level = [$param['level']];
  391. $levelCondition = ['level', '=', $param['level']];
  392. } else {
  393. $level = ['A', 'B', 'C'];
  394. $levelCondition = ['level', 'in', $level];
  395. }
  396. $param['page'] = !empty($param['page']) ? $param['page'] : 1;
  397. $param['limit'] = !empty($param['limit']) ? $param['limit'] : 10;
  398. /** 不同数据内容整理 */
  399. if (empty($param['type']) || $param['type'] == 1) {
  400. $group = 'org_id';
  401. // 部门名称列表获取
  402. $data = Org::where([['path', 'like', $this->root_id . '-%']])->page($param['page'], $param['limit'])->column('id,name as org_name');
  403. // 部门id
  404. $orgIdList = array_column($data, 'id');
  405. // 员工查找
  406. $employees = Employee::where([['org_id', 'in', $orgIdList], ['state', '=', '在职'], ['uid', '<>', 0]])->column('org_id', 'id');
  407. // 员工id
  408. $employeeIdList = array_keys($employees);
  409. // 查询条件
  410. $typeCondition = ['org_id', 'in', $orgIdList];
  411. $count = Org::where([['path', 'like', $this->root_id . '-%']])->count();
  412. } else {
  413. $group = 'employee_id';
  414. $orgCondition = [];
  415. if (!empty($param['org_id'])) $orgCondition = ['org_id', '=', $param['org_id']];
  416. // 部门人员获取
  417. $data = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->page($param['page'], $param['limit'])->column('id, org_id, name');
  418. // 员工id
  419. $employeeIdList = array_column($data, 'id');
  420. // 查询条件
  421. $typeCondition = ['employee_id', 'in', $employeeIdList];
  422. // 员工部门获取
  423. $orgs = Org::where([['path', 'like', $this->root_id . '-%']])->column('name', 'id');
  424. foreach ($data as &$c) {
  425. $c['org_name'] = $orgs[$c['org_id']];
  426. }
  427. $count = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], $orgCondition])->count();
  428. }
  429. // 客户数据获取
  430. $customerData = Customer::where([$typeCondition, $startCondition, $endCondition, $levelCondition, ['state', '<>', '无效']])
  431. ->column("
  432. id,
  433. $group,
  434. level,
  435. state,
  436. deposit_money,
  437. signed_money
  438. ");
  439. // 查询访问记录
  440. $customerVisitData = CustomerVisitLog::where([['employee_id', 'in', $employeeIdList], $startCondition, $endCondition])
  441. ->group('customer_id')->column("
  442. count(if(state in ('" . implode('\',\'', CustomerVisitLog::changeState('已到店', 'chaos')) . "'), '1', null)) as shop_num,
  443. count(if(state in ('" . implode('\',\'', CustomerVisitLog::changeState('已量房', 'chaos')) . "'), '1', null)) as measure_num
  444. ", 'customer_id');
  445. // 数据整理 结构【分组名ID】【客户类型】{num,confirm_num,shop_num,measure_num,deposit_num,signed_num}
  446. $clearData = [];
  447. foreach ($customerData as $customer) {
  448. if (!isset($clearData[$customer[$group]][$customer['level']]))
  449. $clearData[$customer[$group]][$customer['level']] = [
  450. 'level' => $customer['level'],
  451. 'num' => 0,
  452. 'confirm_num' => 0,
  453. 'shop_num' => 0,
  454. 'measure_num' => 0,
  455. 'deposit_num' => 0,
  456. 'signed_num' => 0
  457. ];
  458. $clearData[$customer[$group]][$customer['level']]['num'] += 1;
  459. if (in_array($customer['state'], Customer::changeState('待确认', 'chaos')))
  460. $clearData[$customer[$group]][$customer['level']]['confirm_num'] += 1;
  461. if (isset($customerVisitData[$customer['id']])) {
  462. ($customerVisitData[$customer['id']]['shop_num'] > 0 || in_array($customer['state'], Customer::changeState('确认到店', 'chaos'))) ?? $clearData[$customer[$group]][$customer['level']]['shop_num'] += 1;
  463. ($customerVisitData[$customer['id']]['measure_num'] > 0 || in_array($customer['state'], Customer::changeState('确认量房', 'chaos'))) ?? $clearData[$customer[$group]][$customer['level']]['measure_num'] += 1;
  464. }
  465. if ($customer['deposit_money'] > 0 || in_array($customer['state'], Customer::changeState('交定', 'chaos')))
  466. $clearData[$customer[$group]][$customer['level']]['deposit_num'] += 1;
  467. if ($customer['signed_money'] > 0 || in_array($customer['state'], Customer::changeState('签单', 'chaos')))
  468. $clearData[$customer[$group]][$customer['level']]['signed_num'] += 1;
  469. }
  470. // 数据二次处理
  471. $dealData = [];
  472. foreach ($data as &$i) {
  473. if (isset($i['org_name'])) $i['org_name'] = '<a href="follow_up.html?type=2&org_id=' . $i['id'] . '">' . $i['org_name'] . '</a>';
  474. foreach ($level as $n => $l) {
  475. if (isset($clearData[$i['id']]) && isset($clearData[$i['id']][$l])) {
  476. $item = $clearData[$i['id']][$l];
  477. $item['unshop_num'] = $item['num'] - $item['shop_num'];
  478. } else {
  479. $item = [
  480. 'level' => $l,
  481. 'num' => 0,
  482. 'confirm_num' => 0,
  483. 'shop_num' => 0,
  484. 'measure_num' => 0,
  485. 'deposit_num' => 0,
  486. 'signed_num' => 0,
  487. 'unshop_num' => 0
  488. ];
  489. }
  490. $dealData[] = array_merge($i, $item);
  491. }
  492. }
  493. return ['code' => 0, 'data' => $dealData, 'count' => $count];
  494. }
  495. /**
  496. * 房屋信息统计
  497. *
  498. * @return string
  499. */
  500. private function houseData()
  501. {
  502. return ['code' => 0, 'data' => [], 'count' => 0];
  503. }
  504. /**
  505. * 客户来源渠道统计
  506. *
  507. * @return string
  508. */
  509. private function sourceData()
  510. {
  511. return ['code' => 0, 'data' => [], 'count' => 0];
  512. }
  513. /**
  514. * 线索统计
  515. *
  516. * @return string
  517. */
  518. public function clueData($param)
  519. {
  520. $startCondition = ['addtime', '>', date('Y-m-d H:i:s', 0)];
  521. $endCondition = ['addtime', '<', date('Y-m-d H:i:s')];
  522. //2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
  523. if (!empty($param['start_date'])) {
  524. $count_dates = explode(' - ', $param['start_date']);
  525. $param['start_date'] = date('Y-m-d', strtotime($count_dates[0]));
  526. $param['end_date'] = date('Y-m-d', strtotime($count_dates[1]));
  527. $startCondition = ['addtime', '>', date('Y-m-d H:i:s', strtotime($param['start_date']))];
  528. }
  529. if (!empty($param['end_date'])) {
  530. $endCondition = ['addtime', '<', date('Y-m-d', strtotime($param['end_date'])) . ' 23:59:59'];
  531. }
  532. $param['page'] = !empty($param['page']) ? $param['page'] : 1;
  533. $param['limit'] = !empty($param['limit']) ? $param['limit'] : 10;
  534. /** 不同数据内容整理 */
  535. if (empty($param['type']) || $param['type'] == 1) {
  536. // 部门名称列表获取
  537. $org_where = [['path', 'like', $this->root_id . '-%']];
  538. if (!empty($param['org_id'])) {
  539. $org = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
  540. unset($org_where);
  541. $org_where[] = ['path', 'like', $org['path'] . '%'];
  542. }
  543. $data = Org::where($org_where)->page($param['page'], $param['limit'])->column('id,name as org_name');
  544. $count = Org::where($org_where)->count();
  545. foreach ($data as &$i) {
  546. $employee_id = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], ['org_id', '=', $i['id']]])->column('id');
  547. $clueData = [];
  548. if (isset($employee_id) && $employee_id) {
  549. $typeConditionOrg = ['org_id', '=', $i['id']];
  550. $typeConditionEmployee = ['employee_id', 'in', $employee_id];
  551. $clueData = CustomerClue::where([$startCondition, $endCondition, $typeConditionOrg, $typeConditionEmployee])->field("count(*) as num,count(if(state=1,1,null)) as valid_num,count(if(state=2,1,null)) as invalid_num")->find();
  552. }
  553. $i['org_name'] = '<a href="clue.html?type=2&org_id=' . $i['id'] . '">' . $i['org_name'] . '</a>';
  554. if (isset($clueData) && $clueData) {
  555. $i['num'] = $clueData['num'];
  556. $i['valid_num'] = $clueData['valid_num'];
  557. $i['invalid_num'] = $clueData['invalid_num'];
  558. } else {
  559. $i['num'] = 0;
  560. $i['valid_num'] = 0;
  561. $i['invalid_num'] = 0;
  562. }
  563. $i['valid_lv'] = $i['num'] == 0 ? '0%' : round($i['valid_num'] / $i['num'], 2) * 100 . '%';
  564. }
  565. } else {
  566. // 部门人员获取
  567. $data = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], ['org_id', '=', $param['org_id']]])->page($param['page'], $param['limit'])->column('id, org_id, name');
  568. // 员工id
  569. $employeeIdList = array_column($data, 'id');
  570. $orgs = Org::where([['path', 'like', $this->root_id . '-%']])->column('name', 'id');
  571. foreach ($data as &$c) {
  572. $c['org_name'] = $orgs[$c['org_id']];
  573. }
  574. $count = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['uid', '<>', 0], ['org_id', '=', $param['org_id']]])->count();
  575. //查询部门下的员工保证数据里外一致
  576. $typeConditionOrg = ['org_id', 'in', $param['org_id']];
  577. $typeConditionEmployee = ['employee_id', 'in', $employeeIdList];
  578. $clueData = CustomerClue::where([$startCondition, $endCondition, $typeConditionOrg, $typeConditionEmployee])->field("employee_id,count(id) as num,count(if(state=1,1,null)) as valid_num,count(if(state=2,1,null)) as invalid_num")->group('employee_id')->select()->toArray();
  579. $clueData = array_combine(array_column($clueData, 'employee_id'), $clueData);
  580. // 数据处理
  581. foreach ($data as &$i) {
  582. if (isset($clueData[$i['id']])) {
  583. $i['num'] = $clueData[$i['id']]['num'];
  584. $i['valid_num'] = $clueData[$i['id']]['valid_num'];
  585. $i['invalid_num'] = $clueData[$i['id']]['invalid_num'];
  586. } else {
  587. $i['num'] = 0;
  588. $i['valid_num'] = 0;
  589. $i['invalid_num'] = 0;
  590. }
  591. $i['valid_lv'] = $i['num'] == 0 ? '0%' : round($i['valid_num'] / $i['num'], 2) * 100 . '%';
  592. }
  593. }
  594. return ['code' => 0, 'data' => $data, 'count' => $count];
  595. }
  596. //客户扩展字段转换
  597. public function emp_extfield_change($customerList)
  598. {
  599. $chmod = new CustomerLogic;
  600. $list = CustomerPortraitField::with(['select'])->where([['root_id', '=', $this->root_id], ['pid', '<>', 0]])->select()->toArray();
  601. foreach ($customerList as $key => $val) {
  602. $new = '';
  603. if (!empty($val['ext']) && $val['ext'] != 'null') {
  604. //$extdata=get_object_vars($val['ext']);
  605. $extdata = json_decode($val['ext'], true);
  606. if (isset($extdata['ext1'])) {
  607. $new = $chmod->old_data_saves($val, $list);
  608. } else {
  609. $new = $chmod->new_data_saves($val, $extdata, $list);
  610. }
  611. } else {
  612. $new = $chmod->no_ext_saves($val, $list);
  613. }
  614. $new = array_combine(array_column($new, 'keyname'), array_column($new, 'valname'));
  615. //$customerList[$key]['newext']=$new;
  616. $customerList[$key]['ext'] = $new;
  617. }
  618. return $customerList;
  619. }
  620. /**
  621. * 客户跟进数据统计
  622. */
  623. public function customer_statistics($page, $limit, $getTotal = false)
  624. {
  625. $param = $this->request->only(['sdate' => '', 'edate' => '', 'org_id' => 0, 'add_wechat_time']);
  626. // 获取部门id
  627. $root_id = request()->employee->root_id;
  628. $orgs = Org::where([['path', 'like', $root_id . '-%']])->column('name,id,pid,level,path', 'id');
  629. // 部门领导获取
  630. $leaders = Employee::with('org')->where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['uid', '<>', 0]])->select();
  631. // 领导结构修改
  632. $leadersData = [];
  633. foreach ($leaders as $l) {
  634. if (isset($leadersData[$l['org_id']])) {
  635. $leadersData[$l['org_id']]['leaders'][] = $l['name'];
  636. $leadersData[$l['org_id']]['leaders_ids'][] = $l['id'];
  637. } else {
  638. $leadersData[$l['org_id']] = [
  639. 'org_name' => $l['org']['name'],
  640. 'level' => $l['org']['level'],
  641. 'path' => $l['org']['path'],
  642. 'leaders_ids' => [
  643. $l['id']
  644. ],
  645. 'leaders' => [
  646. $l['name']
  647. ]
  648. ];
  649. }
  650. }
  651. // 补充没领导的部门
  652. foreach ($orgs as $k => $item) {
  653. if (!isset($leadersData[$k])) {
  654. $leadersData[$k] = [
  655. 'org_name' => $item['name'],
  656. 'level' => $item['level'],
  657. 'path' => $item['path'],
  658. 'leaders' => [],
  659. 'leaders_ids' => []
  660. ];
  661. }
  662. }
  663. // 查询最近访问的客户
  664. $where = [];
  665. if ($param['org_id']) {
  666. //查询本部门下面是否有部门
  667. $childorg = orgSubIds($param['org_id']);
  668. $where[] = ['org_id|customer_org_id', 'in', $childorg];
  669. // $where[] = ['org_id', '=', $param['org_id']];
  670. } else {
  671. $where[] = ['org_id', 'in', array_keys($orgs)];
  672. }
  673. if ($param['sdate']) {
  674. $param['sdate'] = date('Y-m-d', strtotime($param['sdate']));
  675. $where[] = ['addtime', '>=', $param['sdate'] . ' 00:00:00'];
  676. }
  677. if ($param['edate']) {
  678. $param['edate'] = date('Y-m-d', strtotime($param['edate']));
  679. $where[] = ['addtime', '<=', $param['edate'] . ' 23:59:59'];
  680. }
  681. // 查询排除的部门
  682. $sheji = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '设计部']])->value('id');
  683. $xiaoshou = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '市场部']])->value('id');
  684. $fanweinei = Org::whereOr([
  685. [['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $sheji . '-%']],
  686. [['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $xiaoshou . '-%']],
  687. ])->column('id');
  688. $searchEmployeeIdList = Employee::where([['org_id', 'in', $fanweinei]])->column('id');
  689. //如果有部门筛选把员工控制在本部门
  690. if ($param['org_id']) $searchEmployeeIdList = Employee::where([['org_id', 'in', $childorg]])->column('id');
  691. //$where[] = ['employee_id', 'in', $searchEmployeeIdList];
  692. $where[] = ['customer_employee_id', 'in', $searchEmployeeIdList];
  693. //2022-10-31辛居尚重11月1号开始正式使用
  694. $where[] = ['addtime', '>', date('2022-10-31 23:59:59')];
  695. if (!empty($param['add_wechat_time'])) {
  696. $newtime = explode(' - ', $param['add_wechat_time']);
  697. $sdate = strtotime($newtime[0]);
  698. $edate = strtotime($newtime[1]);
  699. $wechat_count = [];
  700. $wechat_count = Customer::where([['ext', 'not null', ''], ['ext', '<>', ''], ['add_wechat_time', '>=', date('Y-m-d', $sdate)], ['add_wechat_time', '<=', date('Y-m-d', $edate)]])->column('id');
  701. $cids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
  702. $wechat_count = array_intersect($cids, $wechat_count);
  703. $where[] = ['customer_id', 'in', $wechat_count];
  704. }
  705. $total = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date")->where($where)->group('customer_id,date')->count();
  706. $searchObject = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date, max(addtime) as addtime, SUM(number_of_visitors) as number_of_visitors, SUM(measure_room_img_type='量房图片') as measure_type1, SUM(measure_room_img_type='量房报告图片') as measure_type2, SUM(measure_room_img_type='客户需求表') as measure_type3,GROUP_CONCAT(aid) as aid_list")->where($where)->order('date desc,addtime desc')->group('customer_id,date');
  707. if ($getTotal == false) $searchObject->page($page, $limit);
  708. $lastVisitCustomerList = $searchObject->select();
  709. // 获取客户信息
  710. $customerIdList = array_unique(array_column(empty($lastVisitCustomerList) ? [] : $lastVisitCustomerList->toArray(), 'customer_id'));
  711. $customerList = Customer::with(['employee', 'designer'])->where([['id', 'in', $customerIdList]])->field('id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  712. $customerList1 = CustomerRecycle::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  713. $customerList2 = CustomerDropPool::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  714. $customerList = array_merge($customerList->toArray(), $customerList1->toArray(), $customerList2->toArray());
  715. $customerList = array_combine(array_column($customerList, 'id'), $customerList);
  716. $customerList = $this->emp_extfield_change($customerList);
  717. // 查询爆品产品
  718. $packageId = implode(',', array_column($customerList, 'package_id'));
  719. $package = CustomerPackage::where([['id', 'in', $packageId]])->column('total_price', 'id');
  720. // 获取公司名称
  721. $companyName = Company::where([['root_id', '=', $this->root_id]])->value('company_name');
  722. // 公司负责人获取
  723. // $companyLeader = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['org_id', '=', $this->root_id], ['uid', '<>', 0]])->column('name');
  724. // 来源
  725. $sourseIdList = array_column($customerList, 'source_id');
  726. $sourseList = CustomerSource::where([['id', 'in', $sourseIdList]])->column('source', 'id');
  727. $num = ($page - 1) * $limit + 1;
  728. // 处理数据
  729. $data = [];
  730. foreach ($lastVisitCustomerList as $c) {
  731. $item = [
  732. 'key' => $num,
  733. 'customer_id' => $c['customer_id'],
  734. ];
  735. if (!isset($customerList[$c['customer_id']])) {
  736. $data[] = $item;
  737. $num++;
  738. continue;
  739. }
  740. list($Y, $m, $d) = explode('-', $c['date']);
  741. $leader_arr = ['941' => '罗镐', '1847' => '刘有凤']; //馨居尚,菡萏怡景,部门负责人
  742. $item = [
  743. 'key' => $num,
  744. 'customer_name' => $customerList[$c['customer_id']]['name'],
  745. 'date' => $Y . '年' . $m . '月' . $d . '日',
  746. 'addtime' => $c['addtime'],
  747. 'company_name' => $companyName,
  748. 'leader' => !empty($leader_arr[$this->root_id]) ? $leader_arr[$this->root_id] : '罗镐',
  749. 'community_name' => $customerList[$c['customer_id']]['community_name'],
  750. // 'measure_type1' => $c['measure_type1'] ? '√' : '',
  751. // 'measure_type2' => $c['measure_type2'] ? '√' : '',
  752. // 'measure_type3' => $c['measure_type3'] ? '√' : '',
  753. 'measure_room_store' => $this->measureRoomStore($c['date'], $c['customer_id']),
  754. 'number_of_visitors' => $c['number_of_visitors'] == 0 ? '' : $c['number_of_visitors'],
  755. 'area' => $c['area'],
  756. //'ext39' => $customerList[$c['customer_id']]['ext']['房屋位置'],
  757. 'ext39' => $customerList[$c['customer_id']]['ext']['house_location'],
  758. //'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['谈单时长']) ? $customerList[$c['customer_id']]['ext']['谈单时长'] : 0,
  759. 'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['talking_about_single_time']) ? $customerList[$c['customer_id']]['ext']['talking_about_single_time'] : 0,
  760. 'source' => isset($sourseList[$customerList[$c['customer_id']]['source_id']]) ? $sourseList[$customerList[$c['customer_id']]['source_id']] : '',
  761. 'square' => $customerList[$c['customer_id']]['square'],
  762. //'ext42' => $customerList[$c['customer_id']]['ext']['门牌单元号']
  763. 'ext42' => $customerList[$c['customer_id']]['ext']['unit_number']
  764. ];
  765. //量房图片类型
  766. $where_img_type = [];
  767. $where_img_type[] = ['addtime', 'like', '%' . $c['date'] . '%'];
  768. $where_img_type[] = ['customer_id', '=', $c['customer_id']];
  769. $where_img_type[] = CustomerVisitLog::changeState(['state', '=', '确认量房']);
  770. $str = CustomerVisitLog::where($where_img_type)->column('measure_room_img_type');
  771. $str = $str ? implode(',', $str) : '';
  772. $item['measure_type2'] = ($str && (strpos($str, '量房图片') !== false || strpos($str, '量房报告图片') !== false || strpos($str, '量房验房报告') !== false)) ? '√' : '';
  773. $item['measure_type3'] = ($str && strpos($str, '客户需求图片') !== false) ? '√' : '';
  774. // 计算交房相差月份
  775. $houseDeliveryTime = strtotime($customerList[$c['customer_id']]['house_delivery_time']);
  776. $month = (date('Y', $houseDeliveryTime) - date('Y')) * 12 + date('m', $houseDeliveryTime) - date('m');
  777. $type = '0';
  778. // if ($customerList[$c['customer_id']]['ext']['加微类型'] == '常规加微') {
  779. // $type = '1';
  780. // } elseif ($customerList[$c['customer_id']]['ext']['加微类型'] == '社群加微') {
  781. // $type = '2';
  782. // }
  783. $item['add_wechat_time'] = $customerList[$c['customer_id']]['ext']['add_wechat_time'];
  784. if ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '常规加微') {
  785. $type = '1';
  786. } elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '社群加微') {
  787. $type = '2';
  788. } elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '历史加微') {
  789. $type = '3';
  790. }
  791. if ($type !== '0') {
  792. $item['house_type' . $type] = in_array($customerList[$c['customer_id']]['house_type'], ['现房', '二手房']) ? '√' : '';
  793. if ($item['house_type' . $type] != '√' && !empty($customerList[$c['customer_id']]['house_delivery_time'])) {
  794. $item['delivery_month' . $type . '_1'] = $month <= 3 ? '√' : '';
  795. $item['delivery_month' . $type . '_2'] = $month > 3 && $month <= 6 ? '√' : '';
  796. $item['delivery_month' . $type . '_3'] = $month > 6 && $month <= 12 ? '√' : '';
  797. $item['delivery_month' . $type . '_4'] = $month > 12 ? '√' : '';
  798. } else {
  799. $item['delivery_month' . $type . '_1'] = '';
  800. $item['delivery_month' . $type . '_2'] = '';
  801. $item['delivery_month' . $type . '_3'] = '';
  802. $item['delivery_month' . $type . '_4'] = '';
  803. }
  804. }
  805. // 设计师
  806. if (!empty($customerList[$c['customer_id']]['designer'])) {
  807. $designerOrgId = $customerList[$c['customer_id']]['designer']['org_id'];
  808. $item['designer_name'] = $customerList[$c['customer_id']]['designer']['name'];
  809. $item['designer_org'] = $orgs[$designerOrgId]['name'];
  810. $item['designer_leader'] = isset($leadersData[$designerOrgId]) ? implode(',', $leadersData[$designerOrgId]['leaders']) : '';
  811. $path = $leadersData[$designerOrgId]['path'];
  812. $pathList = array_filter(explode('-', $path));
  813. foreach ($pathList as $od) {
  814. if (strstr($leadersData[$od]['org_name'], '部') !== false && $leadersData[$od]['org_name'] != '设计部') {
  815. $level = 3;
  816. } else {
  817. $level = $leadersData[$od]['level'];
  818. }
  819. if (isset($item['employee_org_level' . $level])) $level++;
  820. $item['designer_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
  821. $item['designer_org_level' . $level] = $leadersData[$od]['org_name'];
  822. }
  823. } else {
  824. $item['designer_name'] = $item['designer_org'] = $item['designer_leader'] = '';
  825. }
  826. // 员工
  827. if (!empty($customerList[$c['customer_id']]['employee'])) {
  828. $employeeOrgId = $customerList[$c['customer_id']]['employee']['org_id'];
  829. $item['employee_name'] = $customerList[$c['customer_id']]['employee']['name'];
  830. $item['employee_org'] = $orgs[$employeeOrgId]['name'];
  831. $item['employee_leader'] = implode(',', $leadersData[$employeeOrgId]['leaders']);
  832. $path = $leadersData[$employeeOrgId]['path'];
  833. $pathList = array_filter(explode('-', $path));
  834. foreach ($pathList as $od) {
  835. if (strstr($leadersData[$od]['org_name'], '区中区') !== false) {
  836. $level = 3;
  837. } elseif (strstr($leadersData[$od]['org_name'], '中心') !== false) {
  838. $level = 4;
  839. } elseif (strstr($leadersData[$od]['org_name'], '区') !== false) {
  840. $level = 2;
  841. } else {
  842. $level = $leadersData[$od]['level'];
  843. }
  844. if (isset($item['employee_org_level' . $level])) $level++;
  845. // $level = $leadersData[$od]['level'];
  846. $item['employee_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
  847. $item['employee_org_level' . $level] = $leadersData[$od]['org_name'];
  848. }
  849. // 查询直属领导是否跟踪过
  850. // 获取直属领导id
  851. $leaderIdList = $leadersData[$employeeOrgId]['leaders_ids'];
  852. $query = [];
  853. $query[] = ['employee_id', 'in', $leaderIdList];
  854. $query[] = ['addtime', 'like', $c['date'] . '%'];
  855. $query[] = ['customer_id', '=', $c['customer_id']];
  856. $item['leader_visit'] = !CustomerVisitLog::where($query)->findOrEmpty()->isEmpty() && ($item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? "√" : "";
  857. } else {
  858. $item['employee_name'] = $item['employee_org'] = $item['employee_leader'] = '';
  859. $item['leader_visit'] = '';
  860. }
  861. // 量房验房
  862. $item['measure_type1'] = ($item['leader_visit'] == '√' || $item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? '√' : '';
  863. // 手机号
  864. $phoneArr = array_filter([$customerList[$c['customer_id']]['phone'], $customerList[$c['customer_id']]['phone1'], $customerList[$c['customer_id']]['phone2']]);
  865. $item['phone'] = substr_replace(array_shift($phoneArr), '******', 3, 6);
  866. // 建群情况
  867. //$jianqun = explode(',', $customerList[$c['customer_id']]['ext']['建群情况']);
  868. $jianqun = explode(',', $customerList[$c['customer_id']]['ext']['group_building']);
  869. $item['ext35_1'] = in_array('1对1小群', $jianqun) || in_array('一对一小群', $jianqun) ? '√' : '';
  870. $item['ext35_2'] = in_array('临时大群', $jianqun) ? '√' : '';
  871. $item['ext35_3'] = in_array('社群', $jianqun) ? '√' : '';
  872. // 直播
  873. //$zhibo = explode(',', $customerList[$c['customer_id']]['ext']['直播情况']);
  874. $zhibo = explode(',', $customerList[$c['customer_id']]['ext']['live_broadcast']);
  875. $item['ext36_1'] = in_array('一对一直播', $zhibo) || in_array('1对1业务直播', $zhibo) ? '√' : '';
  876. $item['ext36_2'] = in_array('1对1设计直播', $zhibo) ? '√' : '';
  877. $item['ext36_3'] = in_array('一对多直播', $zhibo) ? '√' : '';
  878. // $item['ext37_1'] = $item['ext36_1'] == '√' ? $customerList[$c['customer_id']]['ext']['1对1业务人员'] : '';
  879. // $item['ext37_2'] = $item['ext36_2'] == '√' ? $customerList[$c['customer_id']]['ext']['1对1设计人员'] : '';
  880. // $item['ext37_3'] = $item['ext36_3'] == '√' ? $customerList[$c['customer_id']]['ext']['直播人员'] : '';
  881. $item['ext37_1'] = $item['ext36_1'] == '√' ? $customerList[$c['customer_id']]['ext']['live_broadcast_business'] : '';
  882. $item['ext37_2'] = $item['ext36_2'] == '√' ? $customerList[$c['customer_id']]['ext']['live_broadcast_design'] : '';
  883. $item['ext37_3'] = $item['ext36_3'] == '√' ? $customerList[$c['customer_id']]['ext']['live_broadcast_personnel'] : '';
  884. // 签单金额
  885. $condition = [];
  886. $condition[] = ['addtime', 'like', $c['date'] . '%'];
  887. $condition[] = ['customer_id', '=', $c['customer_id']];
  888. $condition1 = $condition;
  889. $condition2 = $condition;
  890. $condition1[] = CustomerVisitLog::changeState(['state', '=', '已交定']);
  891. $condition2[] = CustomerVisitLog::changeState(['state', '=', '已签单']);
  892. $item['signature_money'] = CustomerVisitLog::where($condition2)->order('addtime desc')->value('money');
  893. $deposit = CustomerVisitLog::where($condition1)->order('addtime desc')->field('money,deposit_mode')->find();
  894. if (!empty($deposit)) {
  895. $deposit['deposit_mode'] !== 0 ? $item['deposit_money1'] = $deposit['money'] : $item['deposit_money2'] = $deposit['money'];
  896. // 爆品定金
  897. if ($customerList[$c['customer_id']]['package_id']) {
  898. $packageIdList = explode(',', $customerList[$c['customer_id']]['package_id']);
  899. $totalMoney = 0;
  900. foreach ($packageIdList as $pid) {
  901. $totalMoney += $package[$pid];
  902. }
  903. if ($totalMoney !== 0) {
  904. $item['package'] = $deposit['money'] / $totalMoney * 100 >= 10 ? '√' : '';
  905. }
  906. }
  907. }
  908. // 预计装修时间 -> 高意向到店客户
  909. if (!empty($customerList[$c['customer_id']]['plan_deco_time'])) {
  910. $planDecoTime = strtotime($customerList[$c['customer_id']]['plan_deco_time']);
  911. $item['plan_month_day'] = ($planDecoTime - time()) / 86400 < 30 ? '√' : '';
  912. }
  913. //活动见面类型
  914. $query_actmet = [];
  915. $query_actmet[] = ['addtime', 'like', $c['date'] . '%'];
  916. $query_actmet[] = ['customer_id', '=', $c['customer_id']];
  917. $query_actmet[] = ['actmeet_type', '<>', 0];
  918. $query_actmet[] = CustomerVisitLog::changeState(['state', '=', '已到场']);
  919. $actmet = CustomerVisitLog::where($query_actmet)->order('addtime desc')->value('actmeet_type');
  920. if (!empty($actmet)) {
  921. $item['actmeet_type1'] = ($actmet == 1) ? '√' : '';
  922. $item['actmeet_type2'] = ($actmet == 2) ? '√' : '';
  923. $item['actmeet_type3'] = ($actmet == 3) ? '√' : '';
  924. } else {
  925. $item['actmeet_type1'] = '';
  926. $item['actmeet_type2'] = '';
  927. $item['actmeet_type3'] = '';
  928. }
  929. // if (!empty($customerList[$c['customer_id']]['plan_deco_time'])) {
  930. // $planDecoTime = strtotime($customerList[$c['customer_id']]['plan_deco_time']);
  931. // $month = (date('Y') - date('Y', $planDecoTime)) * 12 + date('m') - date('m', $planDecoTime);
  932. // $item['plan_month1'] = $month <= 3 ? '√' : 0;
  933. // $item['plan_month2'] = $month > 3 && $month <= 6 ? '√' : 0;
  934. // $item['plan_month3'] = $month > 6 ? '√' : 0;
  935. // } else {
  936. // $item['plan_month1'] = '';
  937. // $item['plan_month2'] = '';
  938. // $item['plan_month3'] = '';
  939. // }
  940. // 到店次数
  941. $where = [];
  942. $where[] = ['addtime', '<', $c['date'] . ' 23:59:59'];
  943. $where[] = ['customer_id', '=', $c['customer_id']];
  944. $state1 = CustomerVisitLog::changeState('确定到店', 'chaos');
  945. $where[] = ['state', 'in', $state1];
  946. $count = CustomerVisitLog::where($where)->count();
  947. $item['visit_number1'] = $count == 1 ? '√' : '';
  948. $item['visit_number2'] = $count == 2 ? '√' : '';
  949. $item['visit_number3'] = $count >= 3 ? '√' : '';
  950. // 外场活动名
  951. $aidList = array_unique(explode(',', $c['aid_list']));
  952. $huodong = ModelActivity::where([['id', 'in', $aidList], ['cate', '=', 1]])->column('title,address');
  953. $huodongList = [];
  954. foreach ($huodong as $h) {
  955. if ($h['address']) {
  956. $huodongList[] = $h['title'] . '(' . $h['address'] . ')';
  957. } else {
  958. $huodongList[] = $h['title'];
  959. }
  960. }
  961. $daochang = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]])->count();
  962. $item['outfield_activities'] = $daochang ? implode(',', $huodongList) : '';
  963. // 外场活动转内场新客户 活动前未到店以及后续状态,参加活动后,第一次到店显示
  964. $item['outfield_infield'] = $this->getIsNewCusomerVisitAfterActivity($c['date'], $c['customer_id']);
  965. $data[] = $item;
  966. $num++;
  967. }
  968. // if (!empty($param['add_wechat_time'])) {
  969. // foreach ($data as $key => $val) {
  970. // if (!empty($val['add_wechat_time'])) {
  971. // $xin[] = $val;
  972. // }
  973. // }
  974. // $newtime = explode(' - ', $param['add_wechat_time']);
  975. // $startwetime = str_replace('-', '/', $newtime[0]);
  976. // $endwetime = str_replace('-', '/', $newtime[1]);
  977. // foreach ($xin as $key => $val) {
  978. // if ($val['add_wechat_time'] >= $startwetime && $val['add_wechat_time'] <= $endwetime) {
  979. // $tian[] = $val;
  980. // }
  981. // }
  982. // $total = count($tian);
  983. // if ($getTotal == true) {
  984. // $data = $tian;
  985. // } else {
  986. // $data = array_slice($tian, ($param['page'] - 1) * $param['limit'], $param['limit']);
  987. // }
  988. // }
  989. return json(['code' => 0, 'data' => $data, 'count' => $total, 'page' => ceil($total / $limit)]);
  990. }
  991. /**
  992. * 统计部报表数据统计
  993. */
  994. public function new_customer_statistics($page, $limit, $getTotal = false)
  995. {
  996. $param = $this->request->only(['sdate' => '', 'edate' => '', 'org_id' => 0, 'add_wechat_time']);
  997. // 获取部门id
  998. $root_id = request()->employee->root_id;
  999. $orgs = Org::where([['path', 'like', $root_id . '-%']])->column('name,id,pid,level,path', 'id');
  1000. // 部门领导获取
  1001. $leaders = Employee::with('org')->where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['uid', '<>', 0]])->select();
  1002. // 领导结构修改
  1003. $leadersData = [];
  1004. foreach ($leaders as $l) {
  1005. if (isset($leadersData[$l['org_id']])) {
  1006. $leadersData[$l['org_id']]['leaders'][] = $l['name'];
  1007. $leadersData[$l['org_id']]['leaders_ids'][] = $l['id'];
  1008. } else {
  1009. $leadersData[$l['org_id']] = [
  1010. 'org_name' => $l['org']['name'],
  1011. 'level' => $l['org']['level'],
  1012. 'path' => $l['org']['path'],
  1013. 'leaders_ids' => [
  1014. $l['id']
  1015. ],
  1016. 'leaders' => [
  1017. $l['name']
  1018. ]
  1019. ];
  1020. }
  1021. }
  1022. // 补充没领导的部门
  1023. foreach ($orgs as $k => $item) {
  1024. if (!isset($leadersData[$k])) {
  1025. $leadersData[$k] = [
  1026. 'org_name' => $item['name'],
  1027. 'level' => $item['level'],
  1028. 'path' => $item['path'],
  1029. 'leaders' => [],
  1030. 'leaders_ids' => []
  1031. ];
  1032. }
  1033. }
  1034. // 查询最近访问的客户
  1035. $where = [];
  1036. if ($param['org_id']) {
  1037. //查询本部门下面是否有部门
  1038. $childorg = orgSubIds($param['org_id']);
  1039. $where[] = ['org_id|customer_org_id', 'in', $childorg];
  1040. // $where[] = ['org_id', '=', $param['org_id']];
  1041. } else {
  1042. $where[] = ['org_id', 'in', array_keys($orgs)];
  1043. }
  1044. if ($param['sdate']) {
  1045. $param['sdate'] = date('Y-m-d', strtotime($param['sdate']));
  1046. $where[] = ['addtime', '>=', $param['sdate'] . ' 00:00:00'];
  1047. }
  1048. if ($param['edate']) {
  1049. $param['edate'] = date('Y-m-d', strtotime($param['edate']));
  1050. $where[] = ['addtime', '<=', $param['edate'] . ' 23:59:59'];
  1051. }
  1052. // 查询排除的部门
  1053. $sheji = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '设计部']])->value('id');
  1054. $xiaoshou = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '市场部']])->value('id');
  1055. $fanweinei = Org::whereOr([
  1056. [['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $sheji . '-%']],
  1057. [['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $xiaoshou . '-%']],
  1058. ])->column('id');
  1059. $searchEmployeeIdList = Employee::where([['org_id', 'in', $fanweinei]])->column('id');
  1060. //如果有部门筛选把员工控制在本部门
  1061. if ($param['org_id']) $searchEmployeeIdList = Employee::where([['org_id', 'in', $childorg]])->column('id');
  1062. //$where[] = ['employee_id', 'in', $searchEmployeeIdList];
  1063. $where[] = ['customer_employee_id', 'in', $searchEmployeeIdList];
  1064. //2022-10-31辛居尚重11月1号开始正式使用
  1065. $where[] = ['addtime', '>', date('2022-10-31 23:59:59')];
  1066. //新增加查询记录到前一天的24点
  1067. $sel_endtime = date('Y-m-d');
  1068. if (empty($param['edate']) && empty($param['sdate'])) $where[] = ['addtime', '<', date('Y-m-d', strtotime("$sel_endtime -1 days")).' 23:59:59'];
  1069. if (!empty($param['add_wechat_time'])) {
  1070. $newtime = explode(' - ', $param['add_wechat_time']);
  1071. $sdate = strtotime($newtime[0]);
  1072. $edate = strtotime($newtime[1]);
  1073. $wechat_count = [];
  1074. $wechat_count = Customer::where([['ext', 'not null', ''], ['ext', '<>', ''], ['add_wechat_time', '>=', date('Y-m-d', $sdate)], ['add_wechat_time', '<=', date('Y-m-d', $edate)]])->column('id');
  1075. $cids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
  1076. $wechat_count = array_intersect($cids, $wechat_count);
  1077. $where[] = ['customer_id', 'in', $wechat_count];
  1078. }
  1079. //去掉不需要的跟进记录
  1080. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  1081. $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
  1082. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  1083. $state4 = CustomerVisitLog::changeState('已交定', 'chaos');
  1084. $state5 = CustomerVisitLog::changeState('已签单', 'chaos');
  1085. $where[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5)];
  1086. $total = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date")->where($where)->group('customer_id,date')->count();
  1087. $searchObject = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date, max(addtime) as addtime, SUM(number_of_visitors) as number_of_visitors, SUM(measure_room_img_type='量房图片') as measure_type1, SUM(measure_room_img_type='量房报告图片') as measure_type2, SUM(measure_room_img_type='客户需求表') as measure_type3,GROUP_CONCAT(aid) as aid_list")->where($where)->order('date desc,addtime desc')->group('customer_id,date');
  1088. if ($getTotal == false) $searchObject->page($page, $limit);
  1089. $lastVisitCustomerList = $searchObject->select();
  1090. // 获取客户信息
  1091. $customerIdList = array_unique(array_column(empty($lastVisitCustomerList) ? [] : $lastVisitCustomerList->toArray(), 'customer_id'));
  1092. $customerList = Customer::with(['employee', 'designer'])->where([['id', 'in', $customerIdList]])->field('id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  1093. $customerList1 = CustomerRecycle::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  1094. $customerList2 = CustomerDropPool::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  1095. $customerList = array_merge($customerList->toArray(), $customerList1->toArray(), $customerList2->toArray());
  1096. $customerList = array_combine(array_column($customerList, 'id'), $customerList);
  1097. $customerList = $this->emp_extfield_change($customerList);
  1098. // 查询爆品产品
  1099. $packageId = implode(',', array_column($customerList, 'package_id'));
  1100. $package = CustomerPackage::where([['id', 'in', $packageId]])->column('total_price', 'id');
  1101. // 获取公司名称
  1102. $companyName = Company::where([['root_id', '=', $this->root_id]])->value('company_name');
  1103. // 公司负责人获取
  1104. // $companyLeader = Employee::where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['org_id', '=', $this->root_id], ['uid', '<>', 0]])->column('name');
  1105. // 来源
  1106. $sourseIdList = array_column($customerList, 'source_id');
  1107. $sourseList = CustomerSource::where([['id', 'in', $sourseIdList]])->column('source', 'id');
  1108. $num = ($page - 1) * $limit + 1;
  1109. // 处理数据
  1110. $data = [];
  1111. foreach ($lastVisitCustomerList as $c) {
  1112. $item = [
  1113. 'key' => $num,
  1114. 'customer_id' => $c['customer_id'],
  1115. ];
  1116. if (!isset($customerList[$c['customer_id']])) {
  1117. $data[] = $item;
  1118. $num++;
  1119. continue;
  1120. }
  1121. list($Y, $m, $d) = explode('-', $c['date']);
  1122. $leader_arr = ['941' => '罗镐', '1847' => '刘有凤']; //馨居尚,菡萏怡景,部门负责人
  1123. $item = [
  1124. 'key' => $num,
  1125. 'customer_name' => $customerList[$c['customer_id']]['name'],
  1126. 'date' => $Y . '年' . $m . '月' . $d . '日',
  1127. 'addtime' => $c['addtime'],
  1128. 'company_name' => $companyName,
  1129. 'leader' => !empty($leader_arr[$this->root_id]) ? $leader_arr[$this->root_id] : '罗镐',
  1130. 'community_name' => $customerList[$c['customer_id']]['community_name'],
  1131. // 'measure_type1' => $c['measure_type1'] ? '√' : '',
  1132. // 'measure_type2' => $c['measure_type2'] ? '√' : '',
  1133. // 'measure_type3' => $c['measure_type3'] ? '√' : '',
  1134. 'measure_room_store' => $this->measureRoomStore($c['date'], $c['customer_id']),
  1135. 'number_of_visitors' => $c['number_of_visitors'] == 0 ? '' : $c['number_of_visitors'],
  1136. 'area' => $c['area'],
  1137. //'ext39' => $customerList[$c['customer_id']]['ext']['房屋位置'],
  1138. 'ext39' => $customerList[$c['customer_id']]['ext']['house_location'],
  1139. //'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['谈单时长']) ? $customerList[$c['customer_id']]['ext']['谈单时长'] : 0,
  1140. 'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['talking_about_single_time']) ? $customerList[$c['customer_id']]['ext']['talking_about_single_time'] : 0,
  1141. 'source' => isset($sourseList[$customerList[$c['customer_id']]['source_id']]) ? $sourseList[$customerList[$c['customer_id']]['source_id']] : '',
  1142. 'square' => $customerList[$c['customer_id']]['square'],
  1143. //'ext42' => $customerList[$c['customer_id']]['ext']['门牌单元号']
  1144. 'ext42' => $customerList[$c['customer_id']]['ext']['unit_number']
  1145. ];
  1146. //量房图片类型
  1147. $where_img_type = [];
  1148. $where_img_type[] = ['addtime', 'like', '%' . $c['date'] . '%'];
  1149. $where_img_type[] = ['customer_id', '=', $c['customer_id']];
  1150. $where_img_type[] = CustomerVisitLog::changeState(['state', '=', '确认量房']);
  1151. $str = CustomerVisitLog::where($where_img_type)->order('addtime desc')->column('measure_room_img_type', 'addtime');
  1152. $measure_type2 = $measure_type3 = '';
  1153. foreach ($str as $k => $v) {
  1154. if ($v && (strpos($v, '量房图片') !== false || strpos($v, '量房报告图片') !== false || strpos($v, '量房验房报告') !== false)) {
  1155. $measure_type2 = $k;
  1156. break;
  1157. }
  1158. if ($v && (strpos($v, '客户需求图片') !== false)) {
  1159. $measure_type3 = $k;
  1160. break;
  1161. }
  1162. }
  1163. $item['measure_type2'] = $measure_type2;
  1164. $item['measure_type3'] = $measure_type3;
  1165. $lastmeasure_time = CustomerVisitlog::where($where_img_type)->order('addtime desc')->value('addtime');
  1166. // $str = $str ? implode(',', $str) : '';
  1167. // $item['measure_type2'] = ($str && (strpos($str, '量房图片') !== false || strpos($str, '量房报告图片') !== false || strpos($str, '量房验房报告') !== false)) ? '√' : '';
  1168. // $item['measure_type3'] = ($str && strpos($str, '客户需求图片') !== false) ? '√' : '';
  1169. // 计算交房相差月份
  1170. $houseDeliveryTime = strtotime($customerList[$c['customer_id']]['house_delivery_time']);
  1171. $month = (date('Y', $houseDeliveryTime) - date('Y')) * 12 + date('m', $houseDeliveryTime) - date('m');
  1172. $type = '0';
  1173. // if ($customerList[$c['customer_id']]['ext']['加微类型'] == '常规加微') {
  1174. // $type = '1';
  1175. // } elseif ($customerList[$c['customer_id']]['ext']['加微类型'] == '社群加微') {
  1176. // $type = '2';
  1177. // }
  1178. $item['add_wechat_time'] = $customerList[$c['customer_id']]['ext']['add_wechat_time'];
  1179. if ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '常规加微') {
  1180. $type = '1';
  1181. } elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '社群加微') {
  1182. $type = '2';
  1183. } elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '历史加微') {
  1184. $type = '3';
  1185. }
  1186. if ($type !== '0') {
  1187. $item['house_type' . $type] = in_array($customerList[$c['customer_id']]['house_type'], ['现房', '二手房']) ? '√' : '';
  1188. if ($item['house_type' . $type] != '√' && !empty($customerList[$c['customer_id']]['house_delivery_time'])) {
  1189. $item['delivery_month' . $type . '_1'] = $month <= 3 ? '√' : '';
  1190. $item['delivery_month' . $type . '_2'] = $month > 3 && $month <= 6 ? '√' : '';
  1191. $item['delivery_month' . $type . '_3'] = $month > 6 && $month <= 12 ? '√' : '';
  1192. $item['delivery_month' . $type . '_4'] = $month > 12 ? '√' : '';
  1193. } else {
  1194. $item['delivery_month' . $type . '_1'] = '';
  1195. $item['delivery_month' . $type . '_2'] = '';
  1196. $item['delivery_month' . $type . '_3'] = '';
  1197. $item['delivery_month' . $type . '_4'] = '';
  1198. }
  1199. }
  1200. // 设计师
  1201. if (!empty($customerList[$c['customer_id']]['designer'])) {
  1202. $designerOrgId = $customerList[$c['customer_id']]['designer']['org_id'];
  1203. $item['designer_name'] = $customerList[$c['customer_id']]['designer']['name'];
  1204. $item['designer_org'] = $orgs[$designerOrgId]['name'];
  1205. $item['designer_leader'] = isset($leadersData[$designerOrgId]) ? implode(',', $leadersData[$designerOrgId]['leaders']) : '';
  1206. $path = $leadersData[$designerOrgId]['path'];
  1207. $pathList = array_filter(explode('-', $path));
  1208. foreach ($pathList as $od) {
  1209. if (strstr($leadersData[$od]['org_name'], '部') !== false && $leadersData[$od]['org_name'] != '设计部') {
  1210. $level = 3;
  1211. } else {
  1212. $level = $leadersData[$od]['level'];
  1213. }
  1214. if (isset($item['employee_org_level' . $level])) $level++;
  1215. $item['designer_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
  1216. $item['designer_org_level' . $level] = $leadersData[$od]['org_name'];
  1217. }
  1218. } else {
  1219. $item['designer_name'] = $item['designer_org'] = $item['designer_leader'] = '';
  1220. }
  1221. // 员工
  1222. if (!empty($customerList[$c['customer_id']]['employee'])) {
  1223. $employeeOrgId = $customerList[$c['customer_id']]['employee']['org_id'];
  1224. $item['employee_name'] = $customerList[$c['customer_id']]['employee']['name'];
  1225. $item['employee_org'] = $orgs[$employeeOrgId]['name'];
  1226. $item['employee_leader'] = implode(',', $leadersData[$employeeOrgId]['leaders']);
  1227. $path = $leadersData[$employeeOrgId]['path'];
  1228. $pathList = array_filter(explode('-', $path));
  1229. foreach ($pathList as $od) {
  1230. if (strstr($leadersData[$od]['org_name'], '区中区') !== false) {
  1231. $level = 3;
  1232. } elseif (strstr($leadersData[$od]['org_name'], '中心') !== false) {
  1233. $level = 4;
  1234. } elseif (strstr($leadersData[$od]['org_name'], '区') !== false) {
  1235. $level = 2;
  1236. } else {
  1237. $level = $leadersData[$od]['level'];
  1238. }
  1239. if (isset($item['employee_org_level' . $level])) $level++;
  1240. // $level = $leadersData[$od]['level'];
  1241. $item['employee_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
  1242. $item['employee_org_level' . $level] = $leadersData[$od]['org_name'];
  1243. }
  1244. // 查询直属领导是否跟踪过
  1245. // 获取直属领导id
  1246. $leaderIdList = $leadersData[$employeeOrgId]['leaders_ids'];
  1247. $query = [];
  1248. $query[] = ['employee_id', 'in', $leaderIdList];
  1249. $query[] = ['addtime', 'like', $c['date'] . '%'];
  1250. $query[] = ['customer_id', '=', $c['customer_id']];
  1251. $leader_visit_time = CustomerVisitLog::where($query)->order('addtime desc')->value('addtime');
  1252. $item['leader_visit'] = !empty($leader_visit_time) && ($item['measure_type2'] || $item['measure_type3']) ? $leader_visit_time : "";
  1253. //$item['leader_visit'] = !CustomerVisitLog::where($query)->findOrEmpty()->isEmpty() && ($item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? "√" : "";
  1254. } else {
  1255. $item['employee_name'] = $item['employee_org'] = $item['employee_leader'] = '';
  1256. $item['leader_visit'] = '';
  1257. }
  1258. // 量房验房
  1259. //$item['measure_type1'] = ($item['leader_visit'] == '√' || $item['measure_type2'] == '√' || $item['measure_type3'] == '√') ? $lastmeasure_time : '';
  1260. $item['measure_type1'] = ($item['leader_visit'] || $item['measure_type2'] || $item['measure_type3']) ? $lastmeasure_time : '';
  1261. // 手机号
  1262. $phoneArr = array_filter([$customerList[$c['customer_id']]['phone'], $customerList[$c['customer_id']]['phone1'], $customerList[$c['customer_id']]['phone2']]);
  1263. $item['phone'] = substr_replace(array_shift($phoneArr), '******', 3, 6);
  1264. // 建群情况
  1265. //$jianqun = explode(',', $customerList[$c['customer_id']]['ext']['建群情况']);
  1266. $jianqun = explode(',', $customerList[$c['customer_id']]['ext']['group_building']);
  1267. //建群时间
  1268. $jianqun_time = !empty($customerList[$c['customer_id']]['ext']['group_building_date']) ? $customerList[$c['customer_id']]['ext']['group_building_date'] : '';
  1269. if (empty($jianqun_time)) {
  1270. $jianqun_time = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['save_portrait_field', 'like', '%group_building%']])->value('addtime');
  1271. }
  1272. $item['ext35_1'] = (in_array('1对1小群', $jianqun) || in_array('一对一小群', $jianqun)) && $jianqun_time ? $jianqun_time : '';
  1273. $item['ext35_2'] = in_array('临时大群', $jianqun) && $jianqun_time ? $jianqun_time : '';
  1274. $item['ext35_3'] = in_array('社群', $jianqun) && $jianqun_time ? $jianqun_time : '';
  1275. // 直播
  1276. //$zhibo = explode(',', $customerList[$c['customer_id']]['ext']['直播情况']);
  1277. $zhibo = explode(',', $customerList[$c['customer_id']]['ext']['live_broadcast']);
  1278. $zhibo_time = !empty($customerList[$c['customer_id']]['ext']['live_broadcast_date']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_date'] : '';
  1279. if (empty($zhibo_time)) {
  1280. $zhibo_time = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['save_portrait_field', 'like', '%live_broadcast%']])->value('addtime');
  1281. }
  1282. $item['ext36_1'] = (in_array('一对一直播', $zhibo) || in_array('1对1业务直播', $zhibo)) && $zhibo_time ? $zhibo_time : '';
  1283. $item['ext36_2'] = in_array('1对1设计直播', $zhibo) && $zhibo_time ? $zhibo_time : '';
  1284. $item['ext36_3'] = in_array('一对多直播', $zhibo) && $zhibo_time ? $zhibo_time : '';
  1285. $item['ext37_1'] = !empty($item['ext36_1']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_business'] : '';
  1286. $item['ext37_2'] = !empty($item['ext36_2']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_design'] : '';
  1287. $item['ext37_3'] = !empty($item['ext36_3']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_personnel'] : '';
  1288. // 签单金额
  1289. $condition = [];
  1290. // $condition[] = ['addtime', 'like', $c['date'] . '%'];
  1291. $condition[] = ['customer_id', '=', $c['customer_id']];
  1292. $condition1 = $condition;
  1293. $condition2 = $condition;
  1294. $condition1[] = CustomerVisitLog::changeState(['state', '=', '已交定']);
  1295. $condition2[] = CustomerVisitLog::changeState(['state', '=', '已签单']);
  1296. $item['signature_money'] = CustomerVisitLog::where($condition2)->order('addtime desc')->value('money');
  1297. $deposit = CustomerVisitLog::where($condition1)->order('addtime desc')->field('money,deposit_mode')->find();
  1298. if (!empty($deposit)) {
  1299. $deposit['deposit_mode'] !== 0 ? $item['deposit_money1'] = $deposit['money'] : $item['deposit_money2'] = $deposit['money'];
  1300. // 爆品定金
  1301. if ($customerList[$c['customer_id']]['package_id']) {
  1302. $packageIdList = explode(',', $customerList[$c['customer_id']]['package_id']);
  1303. $totalMoney = 0;
  1304. foreach ($packageIdList as $pid) {
  1305. $totalMoney += $package[$pid];
  1306. }
  1307. if ($totalMoney !== 0) {
  1308. $item['package'] = $deposit['money'] / $totalMoney * 100 >= 10 ? '√' : '';
  1309. }
  1310. }
  1311. }
  1312. // 预计装修时间 -> 高意向到店客户
  1313. if (!empty($customerList[$c['customer_id']]['plan_deco_time'])) {
  1314. $planDecoTime = strtotime($customerList[$c['customer_id']]['plan_deco_time']);
  1315. $item['plan_month_day'] = ($planDecoTime - time()) / 86400 < 30 ? $customerList[$c['customer_id']]['plan_deco_time'] : '';
  1316. }
  1317. //活动见面类型
  1318. $query_actmet = [];
  1319. $query_actmet[] = ['addtime', 'like', $c['date'] . '%'];
  1320. $query_actmet[] = ['customer_id', '=', $c['customer_id']];
  1321. $query_actmet[] = ['actmeet_type', '<>', 0];
  1322. $query_actmet[] = CustomerVisitLog::changeState(['state', '=', '已到场']);
  1323. $actmet = CustomerVisitLog::where($query_actmet)->order('addtime desc')->value('addtime,actmeet_type');
  1324. if (!empty($actmet)) {
  1325. $item['actmeet_type1'] = !empty($actmet['actmeet_type']) && $actmet['actmeet_type'] == 1 ? $actmet['addtime'] : '';
  1326. $item['actmeet_type2'] = !empty($actmet['actmeet_type']) && $actmet['actmeet_type'] == 2 ? $actmet['addtime'] : '';
  1327. $item['actmeet_type3'] = !empty($actmet['actmeet_type']) && $actmet['actmeet_type'] == 3 ? $actmet['addtime'] : '';
  1328. } else {
  1329. $item['actmeet_type1'] = '';
  1330. $item['actmeet_type2'] = '';
  1331. $item['actmeet_type3'] = '';
  1332. }
  1333. //查询谈单时长
  1334. $query_order_time = [];
  1335. $query_order_time[] = ['addtime', 'like', $c['date'] . '%'];
  1336. $query_order_time[] = ['customer_id', '=', $c['customer_id']];
  1337. $query_order_time[] = CustomerVisitLog::changeState(['state', '=', '已到店']);
  1338. $order_time = CustomerVisitLog::where($query_order_time)->sum('talking_order_time');
  1339. $item['order_time'] = $order_time;
  1340. //新的判断网谈登记,茶楼见面
  1341. // $query_actmet1 = [];
  1342. // $query_actmet1[] = ['addtime', 'like', $c['date'] . '%'];
  1343. // $query_actmet1[] = ['customer_id', '=', $c['customer_id']];
  1344. // //$query_actmet1[] = ['save_portrait_field','not null',''];
  1345. // //$query_actmet1[] = ['save_portrait_field','like','online_meet_checked%'];
  1346. // $query_actmet1[] = ['save_portrait_field', 'like', ['%online_meet_checked%', '%teahouse_meet%'], 'OR'];
  1347. // $query_actmet1[] = CustomerVisitLog::changeState(['state', '=', '未到访']);
  1348. // $actmet_data = CustomerVisitLog::where($query_actmet1)->column('save_portrait_field', 'id');
  1349. // $online_meet_checked = $teahouse_meet = '';
  1350. // if ($actmet_data) {
  1351. // foreach ($actmet_data as $m => $p) {
  1352. // $save_field_list = json_decode($p, true);
  1353. // foreach ($save_field_list as $k => $v) {
  1354. // if ($v['keyname'] == 'online_meet_checked') $online_meet_checked = $v['value'];
  1355. // if ($v['keyname'] == 'teahouse_meet') $teahouse_meet = $v['value'];
  1356. // }
  1357. // }
  1358. // }
  1359. // $item['actmeet_type2'] = $teahouse_meet ? $teahouse_meet : '';
  1360. // $item['actmeet_type3'] = $online_meet_checked ? $online_meet_checked : '';
  1361. //查询定金客户转到店
  1362. $item['djzdd'] = $this->isStateToShop($c['date'], $c['customer_id'], '已交定', $customerList[$c['customer_id']]['ext']);
  1363. //查询量房客户转到店
  1364. $item['lfzdd'] = $this->isStateToShop($c['date'], $c['customer_id'], '已量房', $customerList[$c['customer_id']]['ext']);
  1365. //查询外场活动转到店
  1366. $item['wczdd'] = $this->isStateToShop($c['date'], $c['customer_id'], '已到场', $customerList[$c['customer_id']]['ext']);
  1367. //新到店次数
  1368. $where = [];
  1369. $starttime = date('Y-m', strtotime($c['date'])) . '-01 00:00:00';
  1370. $where[] = ['addtime', '<', $c['date'] . ' 23:59:59'];
  1371. $where[] = ['addtime', '>', $starttime];
  1372. $where[] = ['customer_id', '=', $c['customer_id']];
  1373. $state1 = CustomerVisitLog::changeState('确定到店', 'chaos');
  1374. $where[] = ['state', 'in', $state1];
  1375. $list = CustomerVisitLog::where($where)->order('addtime desc')->column('addtime,id');
  1376. $newlist = [];
  1377. if (!empty($list)) {
  1378. foreach ($list as $k => $v) {
  1379. $list[$k]['newtime'] = date('Y-m-d', strtotime($v['addtime']));
  1380. }
  1381. foreach ($list as $k => $v) {
  1382. $newlist[$v['newtime']][] = $v;
  1383. }
  1384. }
  1385. $count = count($newlist);
  1386. $daodian = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]])->order('addtime desc')->value('addtime');
  1387. $item['visit_number1'] = $count == 1 && $daodian ? end($newlist)[0]['addtime'] : '';
  1388. //$item['visit_number2'] = $count == 2 ? array_pop($newlist)[0]['addtime'] : '';
  1389. $item['visit_number2'] = $count == 2 && $daodian ? current($newlist)[0]['addtime'] : '';
  1390. $item['visit_number3'] = $count >= 3 && $daodian ? '√' : '';
  1391. // 外场活动名
  1392. $aidList = array_unique(explode(',', $c['aid_list']));
  1393. $huodong = ModelActivity::where([['id', 'in', $aidList], ['cate', '=', 1]])->column('title,address');
  1394. $huodongList = [];
  1395. foreach ($huodong as $h) {
  1396. if ($h['address']) {
  1397. $huodongList[] = $h['title'] . '(' . $h['address'] . ')';
  1398. } else {
  1399. $huodongList[] = $h['title'];
  1400. }
  1401. }
  1402. $daochang = CustomerVisitLog::where([['customer_id', '=', $c['customer_id']], ['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]])->count();
  1403. $item['outfield_activities'] = $daochang ? implode(',', $huodongList) : '';
  1404. // 外场活动转内场新客户 活动前未到店以及后续状态,参加活动后,第一次到店显示
  1405. //$item['outfield_infield'] = $this->getIsNewCusomerVisitAfterActivity($c['date'], $c['customer_id']);
  1406. $item['outfield_infield'] = $daodian && empty($item['djzdd']) && empty($item['lfzdd']) && empty($item['wczdd']) ? $daodian : '';
  1407. $data[] = $item;
  1408. $num++;
  1409. }
  1410. return json(['code' => 0, 'data' => $data, 'count' => $total, 'page' => ceil($total / $limit)]);
  1411. }
  1412. /**
  1413. * 查询是否是定金,量房,外场客户转到店
  1414. */
  1415. public function isStateToShop($date, $customerId, $state, $cusext)
  1416. {
  1417. $where = [];
  1418. $where[] = ['addtime', '<', $date . ' 23:59:59'];
  1419. $where[] = ['customer_id', '=', $customerId];
  1420. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  1421. $where[] = ['state', 'in', $state1];
  1422. // 查询日期必须到店
  1423. $had = CustomerVisitLog::where($where)->order('addtime asc')->column('addtime');
  1424. // 是否多次到店或未到店,当天到店多次算一次
  1425. if (empty($had)) return '';
  1426. foreach ($had as $k => $v) {
  1427. if ($v < $date . ' 00:00:00') return '';
  1428. }
  1429. //$lastTime = strtotime($had[0]);
  1430. $lastTime = strtotime(end($had));
  1431. // 判断到店日期是否是所选日期
  1432. if (date('Y-m-d', $lastTime) != $date) return '';
  1433. // 判断是否参加过活动
  1434. $state_time = CustomerVisitLog::where([
  1435. ['addtime', '<', $date . ' 23:59:59'],
  1436. ['state', 'in', CustomerVisitLog::changeState($state, 'chaos')],
  1437. ['customer_id', '=', $customerId]
  1438. ])->order('addtime asc')->value('addtime');
  1439. if (empty($state_time)) return '';
  1440. //if (in_array($state, ['已量房', '已交定']) && empty($state_time)) return '';
  1441. //如果已到场时间为空再查询网谈登记时间跟茶楼见面时间,如果两个都有值取最早时间的值
  1442. // if ($state == '已到场' && empty($state_time)) {
  1443. // if (isset($cusext['online_meet_checked']) && isset($cusext['teahouse_meet'])) $state_time = $cusext['online_meet_checked'] > isset($cusext['teahouse_meet']) ? isset($cusext['teahouse_meet']) : isset($cusext['online_meet_checked']);
  1444. // if (isset($cusext['online_meet_checked'])) $state_time = $cusext['online_meet_checked'];
  1445. // if (isset($cusext['teahouse_meet'])) $state_time = $cusext['teahouse_meet'];
  1446. // if (empty($state_time)) return '';
  1447. // }
  1448. if (strtotime($state_time) < $lastTime) return date('Y-m-d H:i:s', $lastTime);
  1449. return '';
  1450. }
  1451. /**
  1452. * 获取客户跟进数据表表头
  1453. */
  1454. public function new_table_data()
  1455. {
  1456. // 领导获取
  1457. $orgLeader = Employee::where([['root_id', '=', $this->root_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id, org_id, name');
  1458. $orgDealLeader = [];
  1459. foreach ($orgLeader as $leader) {
  1460. $orgDealLeader[$leader['org_id']][] = $leader['name'];
  1461. }
  1462. // 查询设计师部门
  1463. $designerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 2]])->order('level desc')->column('level, id, name');
  1464. // 部门处理
  1465. $dealDesignerOrg = [];
  1466. foreach ($designerOrg as $org) {
  1467. $dealDesignerOrg[$org['level']]['name'][] = $org['name'];
  1468. isset($dealDesignerOrg[$org['level']]['leader']) ?: $dealDesignerOrg[$org['level']]['leader'] = [];
  1469. if (isset($orgDealLeader[$org['id']]))
  1470. $dealDesignerOrg[$org['level']]['leader'] = array_merge($dealDesignerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
  1471. }
  1472. // 剔除末枝部门
  1473. $del = false;
  1474. foreach ($dealDesignerOrg as $k => $item) {
  1475. if (!$del) {
  1476. unset($dealDesignerOrg[$k]);
  1477. $del = true;
  1478. }
  1479. }
  1480. // 查询销售部门
  1481. $sellerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 1]])->order('level desc')->column('level, id, name');
  1482. // 部门处理
  1483. $dealSellerOrg = [];
  1484. foreach ($sellerOrg as $org) {
  1485. $dealSellerOrg[$org['level']]['name'][] = $org['name'];
  1486. isset($dealSellerOrg[$org['level']]['leader']) ?: $dealSellerOrg[$org['level']]['leader'] = [];
  1487. if (isset($orgDealLeader[$org['id']]))
  1488. $dealSellerOrg[$org['level']]['leader'] = array_merge($dealSellerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
  1489. }
  1490. // 剔除末枝部门
  1491. $del = false;
  1492. foreach ($dealSellerOrg as $k => $item) {
  1493. if (!$del) {
  1494. unset($dealSellerOrg[$k]);
  1495. $del = true;
  1496. }
  1497. }
  1498. $header = $this->getNewVisitLogHeaders();
  1499. view::assign('header1', $header['header1']);
  1500. view::assign('header2', $header['header2']);
  1501. $org = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
  1502. View::assign('org', $org);
  1503. $row = [];
  1504. foreach ($header['header1'] as $item) {
  1505. if (isset($item['child'])) {
  1506. foreach ($item['child'] as $c) {
  1507. $row[] = $c;
  1508. }
  1509. } else {
  1510. $row[] = $item['field'];
  1511. }
  1512. }
  1513. view::assign('row', json_encode($row));
  1514. return View::fetch();
  1515. }
  1516. /**
  1517. * 获取线上跟踪客户记录表头
  1518. */
  1519. private function getNewVisitLogHeaders()
  1520. {
  1521. // 第二行头部
  1522. $header2 = [];
  1523. // 设计师部门插入头部
  1524. $header2[] = ["field" => 'customer_name', "title" => '客户名字', "align" => 'center', "class" => "bgEEECE1"];
  1525. $header2[] = ["field" => 'phone', "title" => '客户电话', "align" => 'center', "class" => "bgEEECE1"];
  1526. $header2[] = ["field" => 'ext39', "title" => '省市区', "align" => 'center', "class" => "bgEEECE1"];
  1527. $header2[] = ["field" => 'community_name', "title" => '楼盘', "align" => 'center', "class" => "bgEEECE1"];
  1528. $header2[] = ["field" => 'ext42', "title" => '门牌单元号', "align" => 'center', "class" => "bgEEECE1"];
  1529. $header2[] = ["field" => 'square', "title" => '面积', "align" => 'center', "class" => "bgEEECE1"];
  1530. $header2[] = ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "class" => "bgEEECE1"];
  1531. $header2[] = ["field" => 'employee_org', "title" => '业务部门', "align" => 'center', "class" => "bgEEECE1"];
  1532. $header2[] = ["field" => 'employee_leader', "title" => '业务经理', "align" => 'center', "class" => "bgEEECE1"];
  1533. $header2[] = ["field" => 'employee_org_level4', "title" => '业务中心', "align" => 'center', "class" => "bgEEECE1"];
  1534. $header2[] = ["field" => 'employee_leader_level4', "title" => '业务总监', "align" => 'center', "class" => "bgEEECE1"];
  1535. $header2[] = ["field" => 'employee_org_level3', "title" => '区中区', "align" => 'center', "class" => "bgEEECE1"];
  1536. $header2[] = ["field" => 'employee_leader_level3', "title" => '区中区副总', "align" => 'center', "class" => "bgEEECE1"];
  1537. $header2[] = ["field" => 'employee_org_level2', "title" => '大区', "align" => 'center', "class" => "bgEEECE1"];
  1538. $header2[] = ["field" => 'employee_leader_level2', "title" => '大区副总', "align" => 'center', "class" => "bgEEECE1"];
  1539. $header2[] = ["field" => 'designer_name', "title" => '设计师', "align" => 'center', "class" => "bgEEECE1"];
  1540. $header2[] = ["field" => 'designer_org', "title" => '设计部门', "align" => 'center', "class" => "bgEEECE1"];
  1541. $header2[] = ["field" => 'designer_leader', "title" => '设计经理', "align" => 'center', "class" => "bgEEECE1"];
  1542. $header2[] = ["field" => 'designer_org_level2', "title" => '设计中心', "align" => 'center', "class" => "bgEEECE1"];
  1543. $header2[] = ["field" => 'designer_leader_level2', "title" => '设计总监', "align" => 'center', "class" => "bgEEECE1"];
  1544. $header2[] = ["field" => 'designer_leader_level1', "title" => '设计副总', "align" => 'center', "class" => "bgEEECE1"];
  1545. // 加微
  1546. $header2[] = ["field" => 'house_type1', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  1547. $header2[] = ["field" => 'delivery_month1_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  1548. $header2[] = ["field" => 'delivery_month1_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  1549. $header2[] = ["field" => 'delivery_month1_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  1550. $header2[] = ["field" => 'delivery_month1_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  1551. $header2[] = ["field" => 'house_type2', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  1552. $header2[] = ["field" => 'delivery_month2_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  1553. $header2[] = ["field" => 'delivery_month2_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  1554. $header2[] = ["field" => 'delivery_month2_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  1555. $header2[] = ["field" => 'delivery_month2_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  1556. $header2[] = ["field" => 'house_type3', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  1557. $header2[] = ["field" => 'delivery_month3_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  1558. $header2[] = ["field" => 'delivery_month3_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  1559. $header2[] = ["field" => 'delivery_month3_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  1560. $header2[] = ["field" => 'delivery_month3_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  1561. // 建群
  1562. $header2[] = ["field" => 'ext35_1', "title" => '1对1小群', "align" => 'center', "class" => "bgEEECE1"];
  1563. $header2[] = ["field" => 'ext35_2', "title" => '临时大群', "align" => 'center', "class" => "bgEEECE1"];
  1564. $header2[] = ["field" => 'ext35_3', "title" => '社群', "align" => 'center', "class" => "bgEEECE1"];
  1565. // 直播
  1566. $header2[] = ["field" => 'ext36_1', "title" => '1对1业务直播', "align" => 'center', "class" => "bgF2DCDB"];
  1567. $header2[] = ["field" => 'ext37_1', "title" => '业务员', "align" => 'center', "class" => "bgF2DCDB"];
  1568. $header2[] = ["field" => 'ext36_2', "title" => '1对1设计直播', "align" => 'center', "class" => "bgF2DCDB"];
  1569. $header2[] = ["field" => 'ext37_2', "title" => '设计师', "align" => 'center', "class" => "bgF2DCDB"];
  1570. $header2[] = ["field" => 'ext36_3', "title" => '一对多直播', "align" => 'center', "class" => "bgF2DCDB"];
  1571. $header2[] = ["field" => 'ext37_3', "title" => '直播人员', "align" => 'center', "class" => "bgF2DCDB"];
  1572. // 量房验房
  1573. $header2[] = ["field" => 'measure_type2', "title" => '量房验房报告', "align" => 'center', "class" => "bgEEECE1"];
  1574. $header2[] = ["field" => 'measure_type3', "title" => '客户需求表', "align" => 'center', "class" => "bgEEECE1"];
  1575. $header2[] = ["field" => 'leader_visit', "title" => '经理是否回访', "align" => 'center', "class" => "bgEEECE1"];
  1576. // 到店
  1577. $header2[] = ["field" => 'outfield_activities', "title" => '外场活动到店', "align" => 'center', "class" => "bgEEECE1"];
  1578. $header2[] = ["field" => 'outfield_infield', "title" => '内场直接到店', "align" => 'center', "class" => "bgEEECE1"];
  1579. //见面方式
  1580. $header2[] = ["field" => 'actmeet_type1', "title" => '外场活动', "align" => 'center', "class" => "bgEEECE1"];
  1581. $header2[] = ["field" => 'actmeet_type2', "title" => '茶楼见面', "align" => 'center', "class" => "bgEEECE1"];
  1582. $header2[] = ["field" => 'actmeet_type3', "title" => '网谈登记', "align" => 'center', "class" => "bgEEECE1"];
  1583. // 管理到店数据
  1584. $header2[] = ["field" => 'deposit_money2', "title" => '线上定金', "align" => 'center', "class" => "bgEEECE1"];
  1585. $header2[] = ["field" => 'deposit_money1', "title" => '线下定金', "align" => 'center', "class" => "bgEEECE1"];
  1586. $header2[] = ["field" => 'djzdd', "title" => '定金客户转到店', "align" => 'center', "class" => "bgEEECE1"];
  1587. $header2[] = ["field" => 'package', "title" => '爆品定金(10%以上)', "align" => 'center', "class" => "bgEEECE1"];
  1588. $header2[] = ["field" => 'measure_type1', "title" => '量房验房', "align" => 'center', "class" => "bgEEECE1"];
  1589. $header2[] = ["field" => 'lfzdd', "title" => '量房客户转到店', "align" => 'center', "class" => "bgEEECE1"];
  1590. $header2[] = ["field" => 'visit_number1', "title" => '一次到店<br>(内场到店)', "align" => 'center', "class" => "bgEEECE1"];
  1591. $header2[] = ["field" => 'visit_number2', "title" => '二次到店', "align" => 'center', "class" => "bgEEECE1"];
  1592. $header2[] = ["field" => 'visit_number3', "title" => '三次及以上到店', "align" => 'center', "class" => "bgEEECE1"];
  1593. // 插入销售部门
  1594. $employeeChild = ['employee_name', 'employee_org', 'employee_leader', 'employee_org_level4', 'employee_leader_level4', 'employee_org_level3', 'employee_leader_level3', 'employee_org_level2', 'employee_leader_level2'];
  1595. // 设计部门
  1596. $designerChild = ['designer_name', 'designer_org', 'designer_leader', 'designer_org_level2', 'designer_leader_level2', 'designer_leader_level1'];
  1597. // 第一行头部
  1598. $header1 = [];
  1599. $header1[] = ["type" => 'key', "field" => 'key', "title" => '序号', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1600. $header1[] = ["field" => 'date', "title" => '日期', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1601. $header1[] = ["field" => "", "child" => ['customer_name', 'phone', 'ext39', 'community_name', 'ext42', 'square'], "title" => '客户信息 ', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
  1602. $header1[] = ["field" => 'company_name', "title" => '公司', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
  1603. $header1[] = ["field" => 'leader', "title" => '负责人', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
  1604. $header1[] = ["field" => "", "child" => $employeeChild, "title" => '营销', "align" => 'center', "colspan" => 9, "class" => "bgEEECE1"];
  1605. $header1[] = ["field" => "", "child" => $designerChild, "title" => '设计', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
  1606. $header1[] = ["field" => "", "child" => ['house_type1', 'delivery_month1_1', 'delivery_month1_2', 'delivery_month1_3', 'delivery_month1_4'], "title" => '常规加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  1607. $header1[] = ["field" => "", "child" => ['house_type2', 'delivery_month2_1', 'delivery_month2_2', 'delivery_month2_3', 'delivery_month2_4'], "title" => '社群加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  1608. $header1[] = ["field" => "", "child" => ['house_type3', 'delivery_month3_1', 'delivery_month3_2', 'delivery_month3_3', 'delivery_month3_4'], "title" => '历史加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  1609. $header1[] = ["field" => "add_wechat_time", "title" => '加微时间', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1610. $header1[] = ["field" => "plan_month_day", "title" => '高意向到店客户<br>(1个月内装修)', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1611. $header1[] = ["field" => "", "child" => ['ext35_1', 'ext35_2', 'ext35_3'], "title" => '建群', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  1612. $header1[] = ["field" => "", "child" => ['ext36_1', 'ext37_1', 'ext36_2', 'ext37_2', 'ext36_3', 'ext37_3'], "title" => '直播', "align" => 'center', "colspan" => 6, "class" => "bgF2DCDB"];
  1613. $header1[] = ["field" => "", "child" => ['measure_type2', 'measure_type3', 'leader_visit'], "title" => '量房验房', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  1614. // $header1[] = ["field" => "yylf", "title" => '预约量房', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1615. // $header1[] = ["field" => "yydd", "title" => '预约到店', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1616. // $header1[] = ["field" => "yydc", "title" => '预约到场', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  1617. $header1[] = ["field" => '', "child" => ['outfield_activities', 'outfield_infield'], "title" => '到店', "align" => 'center', "colspan" => 2, "class" => "bgFDE9D9"];
  1618. $header1[] = ["field" => '', "child" => ['actmeet_type1', 'actmeet_type2', 'actmeet_type3'], "title" => '见面方式', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  1619. $header1[] = ["field" => '', "child" => ['deposit_money2', 'deposit_money1', 'djzdd', 'package', 'measure_type1', 'lfzdd', 'visit_number1', 'visit_number2', 'visit_number3'], "title" => '管理到店数据', "align" => 'center', "colspan" => 9, "class" => "bgFDE9D9"];
  1620. $header1[] = ["field" => 'number_of_visitors', "title" => '到店人数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  1621. $header1[] = ["field" => 'signature_money', "title" => '合同价', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  1622. $header1[] = ["field" => 'wczdd', "title" => '外场转内场到店', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  1623. $header1[] = ["field" => 'order_time', "title" => '谈单时长', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  1624. return ['header1' => $header1, 'header2' => $header2];
  1625. }
  1626. /**
  1627. * 总经办报表
  1628. */
  1629. public function officeStatistics($page, $limit)
  1630. {
  1631. $param = $this->request->only(['sdate' => '', 'edate' => '', 'org_id' => 0, 'add_wechat_time']);
  1632. // 获取部门id
  1633. $root_id = request()->employee->root_id;
  1634. $orgs = Org::where([['path', 'like', $root_id . '-%']])->column('name,id,pid,level,path', 'id');
  1635. // 部门领导获取
  1636. $leaders = Employee::with('org')->where([['root_id', '=', $this->root_id], ['state', '=', '在职'], ['is_manager', '=', '1'], ['uid', '<>', 0]])->select();
  1637. // 领导结构修改
  1638. $leadersData = [];
  1639. foreach ($leaders as $l) {
  1640. if (isset($leadersData[$l['org_id']])) {
  1641. $leadersData[$l['org_id']]['leaders'][] = $l['name'];
  1642. $leadersData[$l['org_id']]['leaders_ids'][] = $l['id'];
  1643. } else {
  1644. $leadersData[$l['org_id']] = [
  1645. 'org_name' => $l['org']['name'],
  1646. 'level' => $l['org']['level'],
  1647. 'path' => $l['org']['path'],
  1648. 'leaders_ids' => [
  1649. $l['id']
  1650. ],
  1651. 'leaders' => [
  1652. $l['name']
  1653. ]
  1654. ];
  1655. }
  1656. }
  1657. // 补充没领导的部门
  1658. foreach ($orgs as $k => $item) {
  1659. if (!isset($leadersData[$k])) {
  1660. $leadersData[$k] = [
  1661. 'org_name' => $item['name'],
  1662. 'level' => $item['level'],
  1663. 'path' => $item['path'],
  1664. 'leaders' => [],
  1665. 'leaders_ids' => []
  1666. ];
  1667. }
  1668. }
  1669. // 查询最近访问的客户
  1670. $where = [];
  1671. if ($param['org_id']) {
  1672. //查询本部门下面是否有部门
  1673. $childorg = orgSubIds($param['org_id']);
  1674. $where[] = ['org_id|customer_org_id', 'in', $childorg];
  1675. // $where[] = ['org_id', '=', $param['org_id']];
  1676. } else {
  1677. $where[] = ['org_id', 'in', array_keys($orgs)];
  1678. }
  1679. if ($param['sdate']) {
  1680. $param['sdate'] = date('Y-m-d', strtotime($param['sdate']));
  1681. $where[] = ['addtime', '>=', $param['sdate'] . ' 00:00:00'];
  1682. }
  1683. if ($param['edate']) {
  1684. $param['edate'] = date('Y-m-d', strtotime($param['edate']));
  1685. $where[] = ['addtime', '<=', $param['edate'] . ' 23:59:59'];
  1686. }
  1687. // 查询排除的部门
  1688. $sheji = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '设计部']])->value('id');
  1689. $xiaoshou = Org::where([['path', 'like', $this->root_id . '-%'], ['name', '=', '市场部']])->value('id');
  1690. $fanweinei = Org::whereOr([
  1691. [['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $sheji . '-%']],
  1692. [['path', 'like', $this->root_id . '-%'], ['path', 'like', $this->root_id . '-' . $xiaoshou . '-%']],
  1693. ])->column('id');
  1694. $searchEmployeeIdList = Employee::where([['org_id', 'in', $fanweinei]])->column('id');
  1695. //如果有部门筛选把员工控制在本部门
  1696. if ($param['org_id']) $searchEmployeeIdList = Employee::where([['org_id', 'in', $childorg]])->column('id');
  1697. //$where[] = ['employee_id', 'in', $searchEmployeeIdList];
  1698. $where[] = ['customer_employee_id', 'in', $searchEmployeeIdList];
  1699. //2022-10-31辛居尚重11月1号开始正式使用
  1700. $where[] = ['addtime', '>', date('2022-10-31 23:59:59')];
  1701. //新增加查询记录到前一天的24点
  1702. $sel_endtime = date('Y-m-d');
  1703. //if (empty($param['edate']) && empty($param['sdate'])) $where[] = ['addtime', '<', date('Y-m-d', strtotime("$sel_endtime -1 days")).' 23:59:59'];
  1704. if (!empty($param['add_wechat_time'])) {
  1705. $newtime = explode(' - ', $param['add_wechat_time']);
  1706. $sdate = strtotime($newtime[0]);
  1707. $edate = strtotime($newtime[1]);
  1708. $wechat_count = [];
  1709. $wechat_count = Customer::where([['ext', 'not null', ''], ['ext', '<>', ''], ['add_wechat_time', '>=', date('Y-m-d', $sdate)], ['add_wechat_time', '<=', date('Y-m-d', $edate)]])->column('id');
  1710. $cids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
  1711. $wechat_count = array_intersect($cids, $wechat_count);
  1712. $where[] = ['customer_id', 'in', $wechat_count];
  1713. }
  1714. //只查询客户的扩展字段更新跟量房的记录
  1715. $where1[] = ['save_portrait_field', 'like', ['%add_wechat_time%', '%add_wechat_type%', '%group_building"%', '%live_broadcast%'], 'OR'];
  1716. $where1[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
  1717. $total = CustomerVisitLog::field("customer_id, DATE_FORMAT(addtime,'%Y-%m-%d') as date")->where($where)
  1718. ->where(function ($query) use ($where1) {
  1719. $query->whereOr($where1);
  1720. })->group('customer_id,date')->count();
  1721. $lastVisitCustomerList = CustomerVisitLog::field("customer_id,state,save_portrait_field,DATE_FORMAT(addtime,'%Y-%m-%d') as date, max(addtime) as addtime")->where($where)
  1722. ->where(function ($query) use ($where1) {
  1723. $query->whereOr($where1);
  1724. })->order('date desc,addtime desc')->group('customer_id,date')->page($page, $limit)->select();
  1725. // 获取客户信息
  1726. $customerIdList = array_unique(array_column(empty($lastVisitCustomerList) ? [] : $lastVisitCustomerList->toArray(), 'customer_id'));
  1727. $customerList = Customer::with(['employee', 'designer'])->where([['id', 'in', $customerIdList]])->field('id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  1728. $customerList1 = CustomerRecycle::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  1729. $customerList2 = CustomerDropPool::with(['employee', 'designer'])->where([['customer_id', 'in', $customerIdList]])->field('customer_id as id,name,designer_id,employee_id,phone,phone1,phone2,square,community_name,ext,house_delivery_time,plan_deco_time,source_id,house_type,package_id,level,age_range,house_type,deco_style,source_id')->select();
  1730. $customerList = array_merge($customerList->toArray(), $customerList1->toArray(), $customerList2->toArray());
  1731. $customerList = array_combine(array_column($customerList, 'id'), $customerList);
  1732. $customerList = $this->emp_extfield_change($customerList);
  1733. // 获取公司名称
  1734. $companyName = Company::where([['root_id', '=', $this->root_id]])->value('company_name');
  1735. $num = ($page - 1) * $limit + 1;
  1736. // 处理数据
  1737. $data = [];
  1738. foreach ($lastVisitCustomerList as $c) {
  1739. $item = [
  1740. 'key' => $num,
  1741. 'customer_id' => $c['customer_id'],
  1742. ];
  1743. if (!isset($customerList[$c['customer_id']])) {
  1744. $data[] = $item;
  1745. $num++;
  1746. continue;
  1747. }
  1748. list($Y, $m, $d) = explode('-', $c['date']);
  1749. $leader_arr = ['941' => '罗镐', '1847' => '刘有凤']; //馨居尚,菡萏怡景,部门负责人
  1750. $item = [
  1751. 'key' => $num,
  1752. 'customer_name' => $customerList[$c['customer_id']]['name'],
  1753. 'date' => $Y . '年' . $m . '月' . $d . '日',
  1754. 'addtime' => $c['addtime'],
  1755. 'company_name' => $companyName,
  1756. 'leader' => !empty($leader_arr[$this->root_id]) ? $leader_arr[$this->root_id] : '罗镐',
  1757. 'community_name' => $customerList[$c['customer_id']]['community_name'],
  1758. 'measure_room_store' => $this->measureRoomStore($c['date'], $c['customer_id']),
  1759. 'number_of_visitors' => $c['number_of_visitors'] == 0 ? '' : $c['number_of_visitors'],
  1760. 'area' => $c['area'],
  1761. 'ext39' => $customerList[$c['customer_id']]['ext']['house_location'],
  1762. 'talking_about_single_time' => isset($customerList[$c['customer_id']]['ext']['talking_about_single_time']) ? $customerList[$c['customer_id']]['ext']['talking_about_single_time'] : 0,
  1763. 'source' => isset($sourseList[$customerList[$c['customer_id']]['source_id']]) ? $sourseList[$customerList[$c['customer_id']]['source_id']] : '',
  1764. 'square' => $customerList[$c['customer_id']]['square'],
  1765. 'ext42' => $customerList[$c['customer_id']]['ext']['unit_number']
  1766. ];
  1767. //查询当天所有的修改扩展字段的记录
  1768. $savelist = CustomerVisitLog::where([['addtime', 'between', [$c['date'] . ' 00:00:00', $c['date'] . ' 23:59:59']], ['save_portrait_field', 'like', ['%add_wechat_time%', '%add_wechat_type%', '%group_building"%', '%live_broadcast%'], 'OR'], ['customer_id', '=', $c['customer_id']]])->column('save_portrait_field');
  1769. $day_savefild = [];
  1770. if (!empty($savelist)) {
  1771. foreach ($savelist as $k => $v) {
  1772. $day_savefild = array_merge(json_decode($v, true), $day_savefild);
  1773. }
  1774. $day_savefild = array_unique(array_column($day_savefild, 'keyname'));
  1775. }
  1776. $item['day_savefild'] = $day_savefild;
  1777. // 计算交房相差月份
  1778. $houseDeliveryTime = strtotime($customerList[$c['customer_id']]['house_delivery_time']);
  1779. $month = (date('Y', $houseDeliveryTime) - date('Y')) * 12 + date('m', $houseDeliveryTime) - date('m');
  1780. $type = '0';
  1781. $item['add_wechat_time'] = in_array('add_wechat_time', $day_savefild) ? $customerList[$c['customer_id']]['ext']['add_wechat_time'] : '';
  1782. if ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '常规加微') {
  1783. $type = '1';
  1784. } elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '社群加微') {
  1785. $type = '2';
  1786. } elseif ($customerList[$c['customer_id']]['ext']['add_wechat_type'] == '历史加微') {
  1787. $type = '3';
  1788. }
  1789. if ($type !== '0' && in_array('add_wechat_type', $day_savefild)) {
  1790. $item['house_type' . $type] = in_array($customerList[$c['customer_id']]['house_type'], ['现房', '二手房']) ? '√' : '';
  1791. if ($item['house_type' . $type] != '√' && !empty($customerList[$c['customer_id']]['house_delivery_time'])) {
  1792. $item['delivery_month' . $type . '_1'] = $month <= 3 ? '√' : '';
  1793. $item['delivery_month' . $type . '_2'] = $month > 3 && $month <= 6 ? '√' : '';
  1794. $item['delivery_month' . $type . '_3'] = $month > 6 && $month <= 12 ? '√' : '';
  1795. $item['delivery_month' . $type . '_4'] = $month > 12 ? '√' : '';
  1796. } else {
  1797. $item['delivery_month' . $type . '_1'] = '';
  1798. $item['delivery_month' . $type . '_2'] = '';
  1799. $item['delivery_month' . $type . '_3'] = '';
  1800. $item['delivery_month' . $type . '_4'] = '';
  1801. }
  1802. }
  1803. // 设计师
  1804. if (!empty($customerList[$c['customer_id']]['designer'])) {
  1805. $designerOrgId = $customerList[$c['customer_id']]['designer']['org_id'];
  1806. $item['designer_name'] = $customerList[$c['customer_id']]['designer']['name'];
  1807. $item['designer_org'] = $orgs[$designerOrgId]['name'];
  1808. $item['designer_leader'] = isset($leadersData[$designerOrgId]) ? implode(',', $leadersData[$designerOrgId]['leaders']) : '';
  1809. $path = $leadersData[$designerOrgId]['path'];
  1810. $pathList = array_filter(explode('-', $path));
  1811. foreach ($pathList as $od) {
  1812. if (strstr($leadersData[$od]['org_name'], '部') !== false && $leadersData[$od]['org_name'] != '设计部') {
  1813. $level = 3;
  1814. } else {
  1815. $level = $leadersData[$od]['level'];
  1816. }
  1817. if (isset($item['employee_org_level' . $level])) $level++;
  1818. $item['designer_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
  1819. $item['designer_org_level' . $level] = $leadersData[$od]['org_name'];
  1820. }
  1821. } else {
  1822. $item['designer_name'] = $item['designer_org'] = $item['designer_leader'] = '';
  1823. }
  1824. // 员工
  1825. if (!empty($customerList[$c['customer_id']]['employee'])) {
  1826. $employeeOrgId = $customerList[$c['customer_id']]['employee']['org_id'];
  1827. $item['employee_name'] = $customerList[$c['customer_id']]['employee']['name'];
  1828. $item['employee_org'] = $orgs[$employeeOrgId]['name'];
  1829. $item['employee_leader'] = implode(',', $leadersData[$employeeOrgId]['leaders']);
  1830. $path = $leadersData[$employeeOrgId]['path'];
  1831. $pathList = array_filter(explode('-', $path));
  1832. foreach ($pathList as $od) {
  1833. if (strstr($leadersData[$od]['org_name'], '区中区') !== false) {
  1834. $level = 3;
  1835. } elseif (strstr($leadersData[$od]['org_name'], '中心') !== false) {
  1836. $level = 4;
  1837. } elseif (strstr($leadersData[$od]['org_name'], '区') !== false) {
  1838. $level = 2;
  1839. } else {
  1840. $level = $leadersData[$od]['level'];
  1841. }
  1842. if (isset($item['employee_org_level' . $level])) $level++;
  1843. $item['employee_leader_level' . $level] = implode(',', $leadersData[$od]['leaders']);
  1844. $item['employee_org_level' . $level] = $leadersData[$od]['org_name'];
  1845. }
  1846. // 查询直属领导是否跟踪过
  1847. // 获取直属领导id
  1848. $leaderIdList = $leadersData[$employeeOrgId]['leaders_ids'];
  1849. } else {
  1850. $item['employee_name'] = $item['employee_org'] = $item['employee_leader'] = '';
  1851. $item['leader_visit'] = '';
  1852. }
  1853. // 量房验房
  1854. $item['measure_type1'] = in_array($c['state'], [8, '已量房', '确定量房', '确认量房', '8']) ? '√' : '';
  1855. // 手机号
  1856. $phoneArr = array_filter([$customerList[$c['customer_id']]['phone'], $customerList[$c['customer_id']]['phone1'], $customerList[$c['customer_id']]['phone2']]);
  1857. $item['phone'] = substr_replace(array_shift($phoneArr), '******', 3, 6);
  1858. // 建群情况
  1859. $jianqun = in_array('group_building', $day_savefild) ? explode(',', $customerList[$c['customer_id']]['ext']['group_building']) : [];
  1860. $item['ext35_1'] = (in_array('1对1小群', $jianqun) || in_array('一对一小群', $jianqun)) ? '√' : '';
  1861. $item['ext35_2'] = in_array('临时大群', $jianqun) ? '√' : '';
  1862. $item['ext35_3'] = in_array('社群', $jianqun) ? '√' : '';
  1863. // 直播
  1864. $zhibo = in_array('live_broadcast', $day_savefild) ? explode(',', $customerList[$c['customer_id']]['ext']['live_broadcast']) : [];
  1865. $item['ext36_1'] = (in_array('一对一直播', $zhibo) || in_array('1对1业务直播', $zhibo)) ? '√' : '';
  1866. $item['ext36_2'] = in_array('1对1设计直播', $zhibo) ? '√' : '';
  1867. $item['ext36_3'] = in_array('一对多直播', $zhibo) ? '√' : '';
  1868. $item['ext37_1'] = !empty($item['ext36_1']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_business'] : '';
  1869. $item['ext37_2'] = !empty($item['ext36_2']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_design'] : '';
  1870. $item['ext37_3'] = !empty($item['ext36_3']) ? $customerList[$c['customer_id']]['ext']['live_broadcast_personnel'] : '';
  1871. $data[] = $item;
  1872. $num++;
  1873. }
  1874. return json(['code' => 0, 'data' => $data, 'count' => $total, 'page' => ceil($total / $limit)]);
  1875. }
  1876. /**
  1877. * 总经办报表表格
  1878. */
  1879. public function office_table_data()
  1880. {
  1881. // 领导获取
  1882. $orgLeader = Employee::where([['root_id', '=', $this->root_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id, org_id, name');
  1883. $orgDealLeader = [];
  1884. foreach ($orgLeader as $leader) {
  1885. $orgDealLeader[$leader['org_id']][] = $leader['name'];
  1886. }
  1887. // 查询设计师部门
  1888. $designerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 2]])->order('level desc')->column('level, id, name');
  1889. // 部门处理
  1890. $dealDesignerOrg = [];
  1891. foreach ($designerOrg as $org) {
  1892. $dealDesignerOrg[$org['level']]['name'][] = $org['name'];
  1893. isset($dealDesignerOrg[$org['level']]['leader']) ?: $dealDesignerOrg[$org['level']]['leader'] = [];
  1894. if (isset($orgDealLeader[$org['id']]))
  1895. $dealDesignerOrg[$org['level']]['leader'] = array_merge($dealDesignerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
  1896. }
  1897. // 剔除末枝部门
  1898. $del = false;
  1899. foreach ($dealDesignerOrg as $k => $item) {
  1900. if (!$del) {
  1901. unset($dealDesignerOrg[$k]);
  1902. $del = true;
  1903. }
  1904. }
  1905. // 查询销售部门
  1906. $sellerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 1]])->order('level desc')->column('level, id, name');
  1907. // 部门处理
  1908. $dealSellerOrg = [];
  1909. foreach ($sellerOrg as $org) {
  1910. $dealSellerOrg[$org['level']]['name'][] = $org['name'];
  1911. isset($dealSellerOrg[$org['level']]['leader']) ?: $dealSellerOrg[$org['level']]['leader'] = [];
  1912. if (isset($orgDealLeader[$org['id']]))
  1913. $dealSellerOrg[$org['level']]['leader'] = array_merge($dealSellerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
  1914. }
  1915. // 剔除末枝部门
  1916. $del = false;
  1917. foreach ($dealSellerOrg as $k => $item) {
  1918. if (!$del) {
  1919. unset($dealSellerOrg[$k]);
  1920. $del = true;
  1921. }
  1922. }
  1923. $header = $this->getOfficeHeaders();
  1924. view::assign('header1', $header['header1']);
  1925. view::assign('header2', $header['header2']);
  1926. $org = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
  1927. View::assign('org', $org);
  1928. $row = [];
  1929. foreach ($header['header1'] as $item) {
  1930. if (isset($item['child'])) {
  1931. foreach ($item['child'] as $c) {
  1932. $row[] = $c;
  1933. }
  1934. } else {
  1935. $row[] = $item['field'];
  1936. }
  1937. }
  1938. view::assign('row', json_encode($row));
  1939. return View::fetch();
  1940. }
  1941. /**
  1942. * 获取线上跟踪客户记录表头
  1943. */
  1944. private function getOfficeHeaders()
  1945. {
  1946. // 第二行头部
  1947. $header2 = [];
  1948. // 设计师部门插入头部
  1949. $header2[] = ["field" => 'customer_name', "title" => '客户名字', "align" => 'center', "class" => "bgEEECE1"];
  1950. $header2[] = ["field" => 'phone', "title" => '客户电话', "align" => 'center', "class" => "bgEEECE1"];
  1951. $header2[] = ["field" => 'ext39', "title" => '省市区', "align" => 'center', "class" => "bgEEECE1"];
  1952. $header2[] = ["field" => 'community_name', "title" => '楼盘', "align" => 'center', "class" => "bgEEECE1"];
  1953. $header2[] = ["field" => 'ext42', "title" => '门牌单元号', "align" => 'center', "class" => "bgEEECE1"];
  1954. $header2[] = ["field" => 'square', "title" => '面积', "align" => 'center', "class" => "bgEEECE1"];
  1955. $header2[] = ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "class" => "bgEEECE1"];
  1956. $header2[] = ["field" => 'employee_org', "title" => '业务部门', "align" => 'center', "class" => "bgEEECE1"];
  1957. $header2[] = ["field" => 'employee_leader', "title" => '业务经理', "align" => 'center', "class" => "bgEEECE1"];
  1958. $header2[] = ["field" => 'employee_org_level4', "title" => '业务中心', "align" => 'center', "class" => "bgEEECE1"];
  1959. $header2[] = ["field" => 'employee_leader_level4', "title" => '业务总监', "align" => 'center', "class" => "bgEEECE1"];
  1960. $header2[] = ["field" => 'employee_org_level3', "title" => '区中区', "align" => 'center', "class" => "bgEEECE1"];
  1961. $header2[] = ["field" => 'employee_leader_level3', "title" => '区中区副总', "align" => 'center', "class" => "bgEEECE1"];
  1962. $header2[] = ["field" => 'employee_org_level2', "title" => '大区', "align" => 'center', "class" => "bgEEECE1"];
  1963. $header2[] = ["field" => 'employee_leader_level2', "title" => '大区副总', "align" => 'center', "class" => "bgEEECE1"];
  1964. $header2[] = ["field" => 'designer_name', "title" => '设计师', "align" => 'center', "class" => "bgEEECE1"];
  1965. $header2[] = ["field" => 'designer_org', "title" => '设计部门', "align" => 'center', "class" => "bgEEECE1"];
  1966. $header2[] = ["field" => 'designer_leader', "title" => '设计经理', "align" => 'center', "class" => "bgEEECE1"];
  1967. $header2[] = ["field" => 'designer_org_level2', "title" => '设计中心', "align" => 'center', "class" => "bgEEECE1"];
  1968. $header2[] = ["field" => 'designer_leader_level2', "title" => '设计总监', "align" => 'center', "class" => "bgEEECE1"];
  1969. $header2[] = ["field" => 'designer_leader_level1', "title" => '设计副总', "align" => 'center', "class" => "bgEEECE1"];
  1970. // 加微
  1971. $header2[] = ["field" => 'house_type1', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  1972. $header2[] = ["field" => 'delivery_month1_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  1973. $header2[] = ["field" => 'delivery_month1_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  1974. $header2[] = ["field" => 'delivery_month1_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  1975. $header2[] = ["field" => 'delivery_month1_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  1976. $header2[] = ["field" => 'house_type2', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  1977. $header2[] = ["field" => 'delivery_month2_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  1978. $header2[] = ["field" => 'delivery_month2_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  1979. $header2[] = ["field" => 'delivery_month2_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  1980. $header2[] = ["field" => 'delivery_month2_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  1981. $header2[] = ["field" => 'house_type3', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  1982. $header2[] = ["field" => 'delivery_month3_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  1983. $header2[] = ["field" => 'delivery_month3_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  1984. $header2[] = ["field" => 'delivery_month3_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  1985. $header2[] = ["field" => 'delivery_month3_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  1986. // 建群
  1987. $header2[] = ["field" => 'ext35_1', "title" => '1对1小群', "align" => 'center', "class" => "bgEEECE1"];
  1988. $header2[] = ["field" => 'ext35_2', "title" => '临时大群', "align" => 'center', "class" => "bgEEECE1"];
  1989. $header2[] = ["field" => 'ext35_3', "title" => '社群', "align" => 'center', "class" => "bgEEECE1"];
  1990. // 直播
  1991. $header2[] = ["field" => 'ext36_1', "title" => '1对1业务直播', "align" => 'center', "class" => "bgF2DCDB"];
  1992. //$header2[] = ["field" => 'ext37_1', "title" => '业务员', "align" => 'center', "class" => "bgF2DCDB"];
  1993. $header2[] = ["field" => 'ext36_2', "title" => '1对1设计直播', "align" => 'center', "class" => "bgF2DCDB"];
  1994. //$header2[] = ["field" => 'ext37_2', "title" => '设计师', "align" => 'center', "class" => "bgF2DCDB"];
  1995. $header2[] = ["field" => 'ext36_3', "title" => '一对多直播', "align" => 'center', "class" => "bgF2DCDB"];
  1996. //$header2[] = ["field" => 'ext37_3', "title" => '直播人员', "align" => 'center', "class" => "bgF2DCDB"];
  1997. // 插入销售部门
  1998. $employeeChild = ['employee_name', 'employee_org', 'employee_leader', 'employee_org_level4', 'employee_leader_level4', 'employee_org_level3', 'employee_leader_level3', 'employee_org_level2', 'employee_leader_level2'];
  1999. // 设计部门
  2000. $designerChild = ['designer_name', 'designer_org', 'designer_leader', 'designer_org_level2', 'designer_leader_level2', 'designer_leader_level1'];
  2001. // 第一行头部
  2002. $header1 = [];
  2003. $header1[] = ["type" => 'key', "field" => 'key', "title" => '序号', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2004. $header1[] = ["field" => 'date', "title" => '日期', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2005. $header1[] = ["field" => "", "child" => ['customer_name', 'phone', 'ext39', 'community_name', 'ext42', 'square'], "title" => '客户信息 ', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
  2006. $header1[] = ["field" => 'company_name', "title" => '公司', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
  2007. $header1[] = ["field" => 'leader', "title" => '负责人', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
  2008. $header1[] = ["field" => "", "child" => $employeeChild, "title" => '营销', "align" => 'center', "colspan" => 9, "class" => "bgEEECE1"];
  2009. $header1[] = ["field" => "", "child" => $designerChild, "title" => '设计', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
  2010. $header1[] = ["field" => "", "child" => ['house_type1', 'delivery_month1_1', 'delivery_month1_2', 'delivery_month1_3', 'delivery_month1_4'], "title" => '常规加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  2011. $header1[] = ["field" => "", "child" => ['house_type2', 'delivery_month2_1', 'delivery_month2_2', 'delivery_month2_3', 'delivery_month2_4'], "title" => '社群加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  2012. $header1[] = ["field" => "", "child" => ['house_type3', 'delivery_month3_1', 'delivery_month3_2', 'delivery_month3_3', 'delivery_month3_4'], "title" => '历史加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  2013. $header1[] = ["field" => "add_wechat_time", "title" => '加微时间', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2014. $header1[] = ["field" => "", "child" => ['ext35_1', 'ext35_2', 'ext35_3'], "title" => '建群', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  2015. $header1[] = ["field" => "", "child" => ['ext36_1', 'ext36_2', 'ext36_3'], "title" => '直播', "align" => 'center', "colspan" => 3, "class" => "bgF2DCDB"];
  2016. $header1[] = ["field" => "measure_type1", "title" => '量房验房', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2017. return ['header1' => $header1, 'header2' => $header2];
  2018. }
  2019. /**
  2020. * 检测客户是否是外场转内场新客户
  2021. */
  2022. public function getIsNewCusomerVisitAfterActivity($date, $customerId)
  2023. {
  2024. $where = [];
  2025. $where[] = ['addtime', '<', $date . ' 23:59:59'];
  2026. $where[] = ['customer_id', '=', $customerId];
  2027. $state1 = CustomerVisitLog::changeState('已到店', 'chaos');
  2028. $where[] = ['state', 'in', $state1];
  2029. // 查询日期必须到店
  2030. $had = CustomerVisitLog::where($where)->order('addtime desc')->column('addtime, aid');
  2031. // 是否多次到店或未到店
  2032. if (count($had) != 1) return '';
  2033. $lastTime = strtotime($had[0]['addtime']);
  2034. // 判断到店日期是否是所选日期
  2035. if (date('Y-m-d', $lastTime) != $date) return '';
  2036. // 判断是否参加过活动
  2037. $aidExit = CustomerVisitLog::where([
  2038. ['addtime', '<', $date . ' 23:59:59'],
  2039. ['customer_id', '=', $customerId]
  2040. ])->order('addtime desc')->column('aid');
  2041. if (empty($aidExit)) return '';
  2042. $nei = ModelActivity::where([['id', 'in', $aidExit], ['cate', '=', 1]])->count();
  2043. if ($nei) return '√';
  2044. return '';
  2045. }
  2046. /**
  2047. * 量房转到店
  2048. */
  2049. public function measureRoomStore($date, $customer_id)
  2050. {
  2051. $where = [];
  2052. //单日内先量房,在到店
  2053. $where[] = ['addtime', 'like', '%' . $date . '%'];
  2054. $where[] = ['customer_id', '=', $customer_id];
  2055. $state1 = CustomerVisitLog::changeState('确认量房', 'chaos');
  2056. $state2 = CustomerVisitLog::changeState('确认到店', 'chaos');
  2057. $where[] = ['state', 'in', array_merge($state1, $state2)];
  2058. $arr = CustomerVisitLog::where($where)->order('id asc')->column('id,state');
  2059. $measure = $store = 0;
  2060. foreach ($arr as $k => $v) {
  2061. if ($measure == 0 && in_array($v['state'], $state1)) {
  2062. $measure = $v['id'];
  2063. }
  2064. if ($store == 0 && in_array($v['state'], $state2)) {
  2065. $store = $v['id'];
  2066. }
  2067. }
  2068. return ($measure > 0 && $store > 0 && $measure < $store) ? '√' : '';
  2069. }
  2070. /**
  2071. * 获取客户跟进数据表表头
  2072. */
  2073. public function table()
  2074. {
  2075. // 领导获取
  2076. $orgLeader = Employee::where([['root_id', '=', $this->root_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id, org_id, name');
  2077. $orgDealLeader = [];
  2078. foreach ($orgLeader as $leader) {
  2079. $orgDealLeader[$leader['org_id']][] = $leader['name'];
  2080. }
  2081. // 查询设计师部门
  2082. $designerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 2]])->order('level desc')->column('level, id, name');
  2083. // 部门处理
  2084. $dealDesignerOrg = [];
  2085. foreach ($designerOrg as $org) {
  2086. $dealDesignerOrg[$org['level']]['name'][] = $org['name'];
  2087. isset($dealDesignerOrg[$org['level']]['leader']) ?: $dealDesignerOrg[$org['level']]['leader'] = [];
  2088. if (isset($orgDealLeader[$org['id']]))
  2089. $dealDesignerOrg[$org['level']]['leader'] = array_merge($dealDesignerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
  2090. }
  2091. // 剔除末枝部门
  2092. $del = false;
  2093. foreach ($dealDesignerOrg as $k => $item) {
  2094. if (!$del) {
  2095. unset($dealDesignerOrg[$k]);
  2096. $del = true;
  2097. }
  2098. }
  2099. // 查询销售部门
  2100. $sellerOrg = Org::where([['path', 'like', $this->root_id . '-%'], ['id', "<>", $this->root_id], ['org_type', '=', 1]])->order('level desc')->column('level, id, name');
  2101. // 部门处理
  2102. $dealSellerOrg = [];
  2103. foreach ($sellerOrg as $org) {
  2104. $dealSellerOrg[$org['level']]['name'][] = $org['name'];
  2105. isset($dealSellerOrg[$org['level']]['leader']) ?: $dealSellerOrg[$org['level']]['leader'] = [];
  2106. if (isset($orgDealLeader[$org['id']]))
  2107. $dealSellerOrg[$org['level']]['leader'] = array_merge($dealSellerOrg[$org['level']]['leader'], $orgDealLeader[$org['id']]);
  2108. }
  2109. // 剔除末枝部门
  2110. $del = false;
  2111. foreach ($dealSellerOrg as $k => $item) {
  2112. if (!$del) {
  2113. unset($dealSellerOrg[$k]);
  2114. $del = true;
  2115. }
  2116. }
  2117. // // 设计师部门数量
  2118. // $designerCount = count($dealDesignerOrg) * 2 + 3;
  2119. // $sellerCount = count($dealSellerOrg) * 2 + 3;
  2120. // View::assign('dealDesignerOrg', $dealDesignerOrg);
  2121. // View::assign('designerCount', $designerCount);
  2122. // View::assign('dealSellerOrg', $dealSellerOrg);
  2123. // View::assign('sellerCount', $sellerCount);
  2124. $header = $this->getVisitLogHeaders();
  2125. view::assign('header1', $header['header1']);
  2126. view::assign('header2', $header['header2']);
  2127. $org = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
  2128. View::assign('org', $org);
  2129. $row = [];
  2130. foreach ($header['header1'] as $item) {
  2131. if (isset($item['child'])) {
  2132. foreach ($item['child'] as $c) {
  2133. $row[] = $c;
  2134. }
  2135. } else {
  2136. $row[] = $item['field'];
  2137. }
  2138. }
  2139. view::assign('row', json_encode($row));
  2140. return View::fetch('table_data');
  2141. }
  2142. private function tree($data, $pid = 0)
  2143. {
  2144. $new_arr = [];
  2145. foreach ($data as $k => $v) {
  2146. if ($v['pid'] == $pid) {
  2147. $children = $this->tree($data, $v['id']);
  2148. $v['children'] = $children;
  2149. $new_arr[] = $v;
  2150. }
  2151. }
  2152. return $new_arr;
  2153. }
  2154. /*
  2155. * 人员树
  2156. */
  2157. public function get_person()
  2158. {
  2159. $ids = input('id', '');
  2160. $root_id = request()->employee->root_id;
  2161. //树形
  2162. $where = [
  2163. ['path', 'like', $root_id . '-%'],
  2164. ['status', '=', 1]
  2165. ];
  2166. $allnodes = Org::where($where)->field('id value,id,pid,name')->order('level asc, id asc')->select()->toArray();
  2167. $tree = $this->tree($allnodes, 0);
  2168. return json($tree);
  2169. }
  2170. /**
  2171. * 获取线上跟踪客户记录表头
  2172. */
  2173. private function getVisitLogHeaders()
  2174. {
  2175. // 第二行头部
  2176. $header2 = [];
  2177. // 设计师部门插入头部
  2178. $header2[] = ["field" => 'customer_name', "title" => '客户名字', "align" => 'center', "class" => "bgEEECE1"];
  2179. $header2[] = ["field" => 'phone', "title" => '客户电话', "align" => 'center', "class" => "bgEEECE1"];
  2180. $header2[] = ["field" => 'ext39', "title" => '省市区', "align" => 'center', "class" => "bgEEECE1"];
  2181. $header2[] = ["field" => 'community_name', "title" => '楼盘', "align" => 'center', "class" => "bgEEECE1"];
  2182. $header2[] = ["field" => 'ext42', "title" => '门牌单元号', "align" => 'center', "class" => "bgEEECE1"];
  2183. $header2[] = ["field" => 'square', "title" => '面积', "align" => 'center', "class" => "bgEEECE1"];
  2184. $header2[] = ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "class" => "bgEEECE1"];
  2185. $header2[] = ["field" => 'employee_org', "title" => '业务部门', "align" => 'center', "class" => "bgEEECE1"];
  2186. $header2[] = ["field" => 'employee_leader', "title" => '业务经理', "align" => 'center', "class" => "bgEEECE1"];
  2187. $header2[] = ["field" => 'employee_org_level4', "title" => '业务中心', "align" => 'center', "class" => "bgEEECE1"];
  2188. $header2[] = ["field" => 'employee_leader_level4', "title" => '业务总监', "align" => 'center', "class" => "bgEEECE1"];
  2189. $header2[] = ["field" => 'employee_org_level3', "title" => '区中区', "align" => 'center', "class" => "bgEEECE1"];
  2190. $header2[] = ["field" => 'employee_leader_level3', "title" => '区中区副总', "align" => 'center', "class" => "bgEEECE1"];
  2191. $header2[] = ["field" => 'employee_org_level2', "title" => '大区', "align" => 'center', "class" => "bgEEECE1"];
  2192. $header2[] = ["field" => 'employee_leader_level2', "title" => '大区副总', "align" => 'center', "class" => "bgEEECE1"];
  2193. $header2[] = ["field" => 'designer_name', "title" => '设计师', "align" => 'center', "class" => "bgEEECE1"];
  2194. $header2[] = ["field" => 'designer_org', "title" => '设计部门', "align" => 'center', "class" => "bgEEECE1"];
  2195. $header2[] = ["field" => 'designer_leader', "title" => '设计经理', "align" => 'center', "class" => "bgEEECE1"];
  2196. $header2[] = ["field" => 'designer_org_level2', "title" => '设计中心', "align" => 'center', "class" => "bgEEECE1"];
  2197. $header2[] = ["field" => 'designer_leader_level2', "title" => '设计总监', "align" => 'center', "class" => "bgEEECE1"];
  2198. $header2[] = ["field" => 'designer_leader_level1', "title" => '设计副总', "align" => 'center', "class" => "bgEEECE1"];
  2199. // 加微
  2200. $header2[] = ["field" => 'house_type1', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  2201. $header2[] = ["field" => 'delivery_month1_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  2202. $header2[] = ["field" => 'delivery_month1_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  2203. $header2[] = ["field" => 'delivery_month1_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  2204. $header2[] = ["field" => 'delivery_month1_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  2205. $header2[] = ["field" => 'house_type2', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  2206. $header2[] = ["field" => 'delivery_month2_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  2207. $header2[] = ["field" => 'delivery_month2_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  2208. $header2[] = ["field" => 'delivery_month2_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  2209. $header2[] = ["field" => 'delivery_month2_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  2210. $header2[] = ["field" => 'house_type3', "title" => '现房', "align" => 'center', "class" => "bgDCE6F1"];
  2211. $header2[] = ["field" => 'delivery_month3_1', "title" => '0-3个月', "align" => 'center', "class" => "bgDCE6F1"];
  2212. $header2[] = ["field" => 'delivery_month3_2', "title" => '3-6个月', "align" => 'center', "class" => "bgDCE6F1"];
  2213. $header2[] = ["field" => 'delivery_month3_3', "title" => '6-12个月', "align" => 'center', "class" => "bgDCE6F1"];
  2214. $header2[] = ["field" => 'delivery_month3_4', "title" => '12个月以上', "align" => 'center', "class" => "bgDCE6F1"];
  2215. // 建群
  2216. $header2[] = ["field" => 'ext35_1', "title" => '1对1小群', "align" => 'center', "class" => "bgEEECE1"];
  2217. $header2[] = ["field" => 'ext35_2', "title" => '临时大群', "align" => 'center', "class" => "bgEEECE1"];
  2218. $header2[] = ["field" => 'ext35_3', "title" => '社群', "align" => 'center', "class" => "bgEEECE1"];
  2219. // 直播
  2220. $header2[] = ["field" => 'ext36_1', "title" => '1对1业务直播', "align" => 'center', "class" => "bgF2DCDB"];
  2221. $header2[] = ["field" => 'ext37_1', "title" => '业务员', "align" => 'center', "class" => "bgF2DCDB"];
  2222. $header2[] = ["field" => 'ext36_2', "title" => '1对1设计直播', "align" => 'center', "class" => "bgF2DCDB"];
  2223. $header2[] = ["field" => 'ext37_2', "title" => '设计师', "align" => 'center', "class" => "bgF2DCDB"];
  2224. $header2[] = ["field" => 'ext36_3', "title" => '一对多直播', "align" => 'center', "class" => "bgF2DCDB"];
  2225. $header2[] = ["field" => 'ext37_3', "title" => '直播人员', "align" => 'center', "class" => "bgF2DCDB"];
  2226. // 量房验房
  2227. $header2[] = ["field" => 'measure_type2', "title" => '量房验房报告', "align" => 'center', "class" => "bgEEECE1"];
  2228. $header2[] = ["field" => 'measure_type3', "title" => '客户需求表', "align" => 'center', "class" => "bgEEECE1"];
  2229. $header2[] = ["field" => 'leader_visit', "title" => '经理是否回访', "align" => 'center', "class" => "bgEEECE1"];
  2230. // 到店
  2231. $header2[] = ["field" => 'outfield_activities', "title" => '外场活动到店', "align" => 'center', "class" => "bgEEECE1"];
  2232. $header2[] = ["field" => 'outfield_infield', "title" => '内场直接到店', "align" => 'center', "class" => "bgEEECE1"];
  2233. //见面方式
  2234. $header2[] = ["field" => 'actmeet_type1', "title" => '外场活动', "align" => 'center', "class" => "bgEEECE1"];
  2235. $header2[] = ["field" => 'actmeet_type2', "title" => '茶楼见面', "align" => 'center', "class" => "bgEEECE1"];
  2236. $header2[] = ["field" => 'actmeet_type3', "title" => '网谈登记', "align" => 'center', "class" => "bgEEECE1"];
  2237. // 管理到店数据
  2238. $header2[] = ["field" => 'deposit_money2', "title" => '线上定金', "align" => 'center', "class" => "bgEEECE1"];
  2239. $header2[] = ["field" => 'deposit_money1', "title" => '线下定金', "align" => 'center', "class" => "bgEEECE1"];
  2240. $header2[] = ["field" => 'package', "title" => '爆品定金(10%以上)', "align" => 'center', "class" => "bgEEECE1"];
  2241. $header2[] = ["field" => 'measure_type1', "title" => '量房验房', "align" => 'center', "class" => "bgEEECE1"];
  2242. $header2[] = ["field" => 'visit_number1', "title" => '一次到店<br>(内场到店)', "align" => 'center', "class" => "bgEEECE1"];
  2243. $header2[] = ["field" => 'visit_number2', "title" => '二次到店', "align" => 'center', "class" => "bgEEECE1"];
  2244. $header2[] = ["field" => 'visit_number3', "title" => '三次及以上到店', "align" => 'center', "class" => "bgEEECE1"];
  2245. // 插入销售部门
  2246. $employeeChild = ['employee_name', 'employee_org', 'employee_leader', 'employee_org_level4', 'employee_leader_level4', 'employee_org_level3', 'employee_leader_level3', 'employee_org_level2', 'employee_leader_level2'];
  2247. // 设计部门
  2248. $designerChild = ['designer_name', 'designer_org', 'designer_leader', 'designer_org_level2', 'designer_leader_level2', 'designer_leader_level1'];
  2249. // 第一行头部
  2250. $header1 = [];
  2251. $header1[] = ["type" => 'key', "field" => 'key', "title" => '序号', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2252. $header1[] = ["field" => 'date', "title" => '日期', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2253. $header1[] = ["field" => "", "child" => ['customer_name', 'phone', 'ext39', 'community_name', 'ext42', 'square'], "title" => '客户信息 ', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
  2254. $header1[] = ["field" => 'company_name', "title" => '公司', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
  2255. $header1[] = ["field" => 'leader', "title" => '负责人', "align" => 'center', "rowspan" => 2, "class" => "bgA6A6A6"];
  2256. $header1[] = ["field" => "", "child" => $employeeChild, "title" => '营销', "align" => 'center', "colspan" => 9, "class" => "bgEEECE1"];
  2257. $header1[] = ["field" => "", "child" => $designerChild, "title" => '设计', "align" => 'center', "colspan" => 6, "class" => "bgEEECE1"];
  2258. $header1[] = ["field" => "", "child" => ['house_type1', 'delivery_month1_1', 'delivery_month1_2', 'delivery_month1_3', 'delivery_month1_4'], "title" => '常规加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  2259. $header1[] = ["field" => "", "child" => ['house_type2', 'delivery_month2_1', 'delivery_month2_2', 'delivery_month2_3', 'delivery_month2_4'], "title" => '社群加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  2260. $header1[] = ["field" => "", "child" => ['house_type3', 'delivery_month3_1', 'delivery_month3_2', 'delivery_month3_3', 'delivery_month3_4'], "title" => '历史加微<br>(交房时间)', "align" => 'center', "colspan" => 5, "class" => "bgDCE6F1"];
  2261. $header1[] = ["field" => "add_wechat_time", "title" => '加微时间', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2262. $header1[] = ["field" => "plan_month_day", "title" => '高意向到店客户<br>(1个月内装修)', "align" => 'center', "rowspan" => 2, "class" => "bgEEECE1"];
  2263. $header1[] = ["field" => "", "child" => ['ext35_1', 'ext35_2', 'ext35_3'], "title" => '建群', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  2264. $header1[] = ["field" => "", "child" => ['ext36_1', 'ext37_1', 'ext36_2', 'ext37_2', 'ext36_3', 'ext37_3'], "title" => '直播', "align" => 'center', "colspan" => 6, "class" => "bgF2DCDB"];
  2265. $header1[] = ["field" => "", "child" => ['measure_type2', 'measure_type3', 'leader_visit'], "title" => '量房验房', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  2266. $header1[] = ["field" => '', "child" => ['outfield_activities', 'visit_number1'], "title" => '到店', "align" => 'center', "colspan" => 2, "class" => "bgFDE9D9"];
  2267. $header1[] = ["field" => '', "child" => ['actmeet_type1', 'actmeet_type2', 'actmeet_type3'], "title" => '见面方式', "align" => 'center', "colspan" => 3, "class" => "bgEEECE1"];
  2268. $header1[] = ["field" => '', "child" => ['deposit_money2', 'deposit_money1', 'package', 'measure_type1', 'visit_number1', 'visit_number2', 'visit_number3'], "title" => '管理到店数据', "align" => 'center', "colspan" => 7, "class" => "bgFDE9D9"];
  2269. $header1[] = ["field" => 'number_of_visitors', "title" => '到店人数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  2270. $header1[] = ["field" => 'signature_money', "title" => '合同价', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  2271. return ['header1' => $header1, 'header2' => $header2];
  2272. }
  2273. /**
  2274. * 渠道统计
  2275. * 昨日,近一周,近一月
  2276. */
  2277. public function channelStatistics()
  2278. {
  2279. return View::fetch();
  2280. }
  2281. /**
  2282. * 渠道统计
  2283. * 昨日,近一周,近一月
  2284. */
  2285. public function channelStatisticsList()
  2286. {
  2287. $request = request();
  2288. $root_id = $request->employee->root_id;
  2289. $team_orgs = orgSubIds($request->employee->org_id);
  2290. $param = Request()->only(['page' => 1, 'limit' => 10, 'start_date', 'keyword']);
  2291. $startDateTime = date('Y-m-d H:i:s', 0);
  2292. $endDateTime = date('Y-m-d H:i:s', time());
  2293. if (!empty($param['start_date'])) {
  2294. $count_dates = explode(' - ', $param['start_date']);
  2295. $startDateTime = date('Y-m-d H:i:s', strtotime($count_dates[0]));
  2296. $endDateTime = date('Y-m-d H:i:s', strtotime($count_dates[1]) + 86400);
  2297. }
  2298. $soud_where[] = ['root_id', '=', $root_id];
  2299. setCondition($param, ['keyword', 'source'], ['like', '%VALUE%'], $soud_where);
  2300. $org_employee = Employee::where([['org_id', 'in', $team_orgs], ['state', '=', '在职'], ['uid', '>', 0]])->column('id');
  2301. if (!empty($param['source_id'])) $soud_where[] = ['id', 'in', explode(',', $param['source_id'])];
  2302. $soud_list = CustomerSource::field('id,source as name')->where($soud_where)->select()->toArray();
  2303. $where[] = ['addtime', 'between', [$startDateTime, $endDateTime]];
  2304. if (!empty($param['employee_id'])) $org_employee = [$param['employee_id']];
  2305. $where[] = ['employee_id', 'in', $org_employee];
  2306. $exten_where[] = ['root_id', '=', $root_id];
  2307. $exten_where[] = ['addtime', 'between', [$startDateTime, $endDateTime]];
  2308. if (!empty($param['employee_id'])) $exten_where[] = ['id', '=', 0];
  2309. $exten_list = CustomerExtension::with(['source' => function ($query) {
  2310. $query->field('id,source');
  2311. }])->where($exten_where)->select()->toArray();
  2312. $t = ['number' => 0, 'clue_count' => 0, 'valid_count' => 0, 'meet_count' => 0, 'delivery_count' => 0, 'order_count' => 0, 'maika_count' => 0, 'order_money' => 0, 'meet_grawth' => '0%', 'valid_grawth' => '0%', 'delivery_grawth' => '0%', 'order_grawth' => '0%'];
  2313. $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
  2314. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  2315. $state3 = CustomerVisitLog::changeState('已签单', 'chaos');
  2316. $state5 = Customer::changeState('无效', 'chaos');
  2317. $state6 = Customer::changeState('待确认', 'chaos');
  2318. $state7 = CustomerVisitLog::changeState('已量房', 'chaos');
  2319. $state8 = CustomerVisitLog::changeState('已到场', 'chaos');
  2320. $state9 = CustomerVisitLog::changeState('已卖卡', 'chaos');
  2321. $ismeet = [];
  2322. foreach ($soud_list as $key => $val) {
  2323. $soud_list[$key]['employee_id'] = !empty($param['employee_id']) ? $param['employee_id'] : '';
  2324. $exten_money = $exten_show = $exten_click = $clue_cont = $valid = $meet = $deposit = $sign = $basic_money = $maika_count = 0;
  2325. $validids = $cusids = [];
  2326. $customer_list = Customer::where($where)->where([['source_id', '=', $val['id']], ['state', 'not in', $state6]])->column('state', 'id');
  2327. $clue_ids = Customer::where($where)->where([['source_id', '=', $val['id']]])->column('id');
  2328. $clue_cont = count($clue_ids);
  2329. foreach ($customer_list as $k => $v) {
  2330. if (!in_array($v, $state5)) {
  2331. $cusids[] = $k;
  2332. $valid++;
  2333. }
  2334. }
  2335. unset($customer_list);
  2336. $validids = $cusids;
  2337. //查询无效的客户
  2338. $invalidcus = CustomerInvalidLog::where([['employee_id', 'in', $org_employee], ['root_id', '=', $root_id], ['source_id', '=', $val['id']], ['cus_addtime', 'between', [$startDateTime, $endDateTime]]])->column('customer_id,employee_id,status');
  2339. foreach ($invalidcus as $k => $v) {
  2340. if (!in_array($v['customer_id'], $validids)) {
  2341. if ($v['status'] == 1) {
  2342. $valid++;
  2343. $validids[] = $v['customer_id'];
  2344. }
  2345. }
  2346. if (!in_array($v['customer_id'], $clue_ids)) {
  2347. $clue_cont++;
  2348. $clue_ids[] = $v['customer_id'];
  2349. }
  2350. }
  2351. unset($clue_ids, $validids, $cusids);
  2352. foreach ($exten_list as $k => $v) {
  2353. if ($v['source_id'] == $val['id']) {
  2354. $exten_money += $v['money'];
  2355. $exten_show += $v['show'];
  2356. $exten_click += $v['click'];
  2357. }
  2358. }
  2359. $visitlog_list = CustomerVisitLog::hasWhere('customer', ['source_id' => $val['id']])
  2360. ->where([['CustomerVisitLog.customer_employee_id', 'in', $org_employee], ['CustomerVisitLog.state', '<>', 'null'], ['CustomerVisitLog.confirm_date', 'between', [$startDateTime, $endDateTime]]])
  2361. ->order('CustomerVisitLog.addtime desc')->column('CustomerVisitLog.id,CustomerVisitLog.customer_id,CustomerVisitLog.state,CustomerVisitLog.money');
  2362. $metids = [];
  2363. foreach ($visitlog_list as $k => $v) {
  2364. if (in_array($v['state'], array_merge($state1, $state7, $state8))) $metids[] = $v['customer_id'];
  2365. }
  2366. $yesids = !empty(array_unique($metids)) ? $this->selBeforelog(array_unique($metids), $startDateTime) : [];
  2367. $depositIds = []; //交定
  2368. $signIds = []; //签单
  2369. $maikaIds = []; //卖卡
  2370. foreach ($visitlog_list as $k => $v) {
  2371. if (in_array($v['state'], array_merge($state1, $state7, $state8)) && !in_array($v['customer_id'], $ismeet) && in_array($v['customer_id'], $yesids)) {
  2372. $meet++;
  2373. $ismeet[] = $v['customer_id'];
  2374. }
  2375. if (in_array($v['state'], $state2) && !in_array($v['customer_id'], $depositIds)) {
  2376. $deposit++;
  2377. $depositIds[] = $v['customer_id'];
  2378. }
  2379. if (in_array($v['state'], $state3) && !in_array($v['customer_id'], $signIds)) {
  2380. $sign++;
  2381. $basic_money += $v['money'];
  2382. $signIds[] = $v['customer_id'];
  2383. }
  2384. if (in_array($v['state'], $state9) && !in_array($v['customer_id'], $maikaIds)) {
  2385. $maika_count++;
  2386. $maikaIds[] = $v['customer_id'];
  2387. }
  2388. }
  2389. unset($visitlog_list);
  2390. $soud_list[$key]['source'] = $val['name'];
  2391. $soud_list[$key]['clue_count'] = $clue_cont;
  2392. $soud_list[$key]['valid_count'] = $valid;
  2393. $soud_list[$key]['meet_count'] = $meet;
  2394. $soud_list[$key]['delivery_count'] = $deposit;
  2395. $soud_list[$key]['order_count'] = $sign;
  2396. $soud_list[$key]['maika_count'] = $maika_count;
  2397. $soud_list[$key]['order_money'] = $basic_money;
  2398. $soud_list[$key]['meet_grawth'] = !empty($meet) && !empty($clue_cont) ? round($meet / $clue_cont * 100, 2) . '%' : '0%';
  2399. $soud_list[$key]['valid_grawth'] = $valid && $clue_cont ? round($valid / $clue_cont * 100, 2) . '%' : '0%';
  2400. $soud_list[$key]['delivery_grawth'] = !empty($deposit) && !empty($meet) ? round($deposit / $meet * 100, 2) . '%' : '0%';
  2401. $soud_list[$key]['order_grawth'] = !empty($sign) && !empty($meet) ? round($sign / $meet * 100, 2) . '%' : '0%';
  2402. //总数
  2403. $t['number']++;
  2404. $t['clue_count'] += $clue_cont;
  2405. $t['valid_count'] += $valid;
  2406. $t['meet_count'] += $meet;
  2407. $t['delivery_count'] += $deposit;
  2408. $t['order_count'] += $sign;
  2409. $t['maika_count'] += $maika_count;
  2410. $t['order_money'] += $basic_money;
  2411. }
  2412. $t['meet_grawth'] = ($t['clue_count'] == 0 || $t['meet_count'] == 0) ? '0%' : round($t['meet_count'] / $t['clue_count'] * 100, 2) . '%';
  2413. $t['valid_grawth'] = ($t['valid_count'] == 0 || $t['clue_count'] == 0) ? '0%' : round($t['valid_count'] / $t['clue_count'] * 100, 2) . '%';
  2414. $t['delivery_grawth'] = ($t['delivery_count'] == 0 || $t['meet_count'] == 0) ? '0%' : round($t['delivery_count'] / $t['meet_count'] * 100, 2) . '%';
  2415. $t['order_grawth'] = ($t['order_count'] == 0 || $t['meet_count'] == 0) ? '0%' : round($t['order_count'] / $t['meet_count'] * 100, 2) . '%';
  2416. return json(['code' => 0, 'data' => $soud_list, 'header' => $t, 'count' => 0, 'msg' => '获取成功']);
  2417. }
  2418. public function selBeforelog($metids, $firstday)
  2419. {
  2420. $statday = '2021-12-01 00:00:00';
  2421. $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
  2422. $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
  2423. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  2424. $q_where[] = ['customer_id', 'in', $metids];
  2425. $q_where[] = ['state', 'in', array_merge($state1, $state2, $state3)];
  2426. $q_where[] = ['addtime', 'between', [$statday, $firstday]];
  2427. $qianvslog = CustomerVisitLog::where($q_where)->group('customer_id')->column('customer_id');
  2428. $yesids = array_diff($metids, $qianvslog);
  2429. return $yesids;
  2430. }
  2431. /**
  2432. * 渠道统计合计
  2433. */
  2434. public function channelStatisticsTotal()
  2435. {
  2436. //合计
  2437. $param = Request()->only(['start_date' => '', 'end_date' => '', 'keyword' => '']);
  2438. $root_id = request()->employee->root_id;
  2439. $startDateTime = date('Y-m-d H:i:s', 0);
  2440. $endDateTime = date('Y-m-d H:i:s', time());
  2441. if (!empty($param['start_date'])) {
  2442. $count_dates = explode(' - ', $param['start_date']);
  2443. $startDateTime = date('Y-m-d H:i:s', strtotime($count_dates[0]));
  2444. $endDateTime = date('Y-m-d H:i:s', strtotime($count_dates[1]) + 86400);
  2445. }
  2446. //渠道列表
  2447. $where[] = ['root_id', '=', $root_id];
  2448. if ($param['keyword']) {
  2449. $where[] = ['source', 'like', '%' . $param['keyword'] . '%'];
  2450. }
  2451. //有效量,确认到场,确认量房,确认到店,交定签单
  2452. $state1 = CustomerVisitLog::changeState('确认到店', 'chaos');
  2453. $state2 = CustomerVisitLog::changeState('确认量房', 'chaos');
  2454. $state3 = CustomerVisitLog::changeState('确认到场', 'chaos');
  2455. $state4 = CustomerVisitLog::changeState('交定', 'chaos');
  2456. $state5 = CustomerVisitLog::changeState('签单', 'chaos');
  2457. $state6 = CustomerVisitLog::changeState('待确认', 'chaos');
  2458. $state7 = CustomerVisitLog::changeState('无效', 'chaos');
  2459. $state8 = CustomerVisitLog::changeState('已卖卡', 'chaos');
  2460. // 见面状态
  2461. $make_state0 = array_merge($state1, $state2, $state3);
  2462. $make_state1 = array_merge($state1, $state2, $state3, $state4, $state5);
  2463. $make_state2 = array_merge($state1, $state2, $state3, $state4, $state5, $state8);
  2464. //员工id
  2465. $eids = Employee::where([['root_id', '=', $root_id], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
  2466. //渠道ID
  2467. $sourceId = CustomerSource::where($where)->column('id');
  2468. //查询搜索日期之前的见面量
  2469. $beforeId = [];
  2470. if (!empty($param['start_date'])) {
  2471. $vis_where = [['state', 'in', $make_state2], ['addtime', '<', $startDateTime]];
  2472. $beforeList = Customer::with(['visitLog' => function ($query) use ($vis_where) {
  2473. $query->where($vis_where)->field('customer_id,state,money');
  2474. }])
  2475. ->where([['source_id', 'in', $sourceId], ['employee_id', 'in', $eids]])
  2476. ->where(function ($query) {
  2477. $not_sure = Customer::changeState('待确认', 'chaos');
  2478. $or1[] = ['crm_res_id', 'null', null];
  2479. $or2[] = ['crm_res_id', 'not null', null];
  2480. $or2[] = ['state', 'not in', $not_sure];
  2481. $query->whereOr([$or1, $or2]);
  2482. })->field('id,source_id')->select()->toArray();
  2483. foreach ($beforeList as $v) {
  2484. if ($v['visitLog']) $beforeId[] = $v['id'];
  2485. }
  2486. }
  2487. $vis_where = [['state', 'in', $make_state2], ['addtime', '>=', $startDateTime], ['addtime', '<', $endDateTime]];
  2488. $customerList = Customer::with(['visitLog' => function ($query) use ($vis_where) {
  2489. $query->where($vis_where)->field('customer_id,state,money');
  2490. }])
  2491. ->where([['source_id', 'in', $sourceId], ['employee_id', 'in', $eids]])
  2492. ->where(function ($query) {
  2493. $not_sure = Customer::changeState('待确认', 'chaos');
  2494. $or1[] = ['crm_res_id', 'null', null];
  2495. $or2[] = ['crm_res_id', 'not null', null];
  2496. $or2[] = ['state', 'not in', $not_sure];
  2497. $query->whereOr([$or1, $or2]);
  2498. })->field('id,source_id')->select()->toArray();
  2499. $data['source_count'] = count($sourceId); //渠道总数
  2500. $data['clue_count'] = 0; //线索总数
  2501. $data['valid_count'] = 0; //有效线索数量
  2502. $data['meet_count'] = 0; //见面数量
  2503. $data['delivery_count'] = 0; //交定数量
  2504. $data['order_count'] = 0; //签单数量
  2505. $data['maika_count'] = 0; //卖卡数量
  2506. $tmp_valid_count = $tmp_meet_count = $tmp_delivery_count = $tmp_order_count = $tmp_maika_count = [];
  2507. foreach ($customerList as $v) {
  2508. $data['clue_count'] += 1; //线索量
  2509. $tmp_valid_count[$v['source_id']][] = 0; //有效量
  2510. $tmp_meet_count[$v['source_id']][] = 0; //见面
  2511. $tmp_delivery_count[$v['source_id']][] = 0; //交定
  2512. $tmp_order_count[$v['source_id']][] = 0; //签单量
  2513. $tmp_maika_count[$v['source_id']][] = 0; //卖卡
  2514. foreach ($v['visitLog'] as $tmp) {
  2515. if (!in_array($v['id'], $tmp_valid_count[$v['source_id']]) && in_array($tmp['state'], $make_state2)) {
  2516. $tmp_valid_count[$v['source_id']][] = $v['id']; //有效量
  2517. $data['valid_count'] += 1;
  2518. }
  2519. if (!in_array($v['id'], $tmp_meet_count[$v['source_id']]) && in_array($tmp['state'], $make_state0) && !in_array($v['id'], $beforeId)) {
  2520. $tmp_meet_count[$v['source_id']][] = $v['id']; //见面
  2521. $data['meet_count'] += 1;
  2522. }
  2523. if (!in_array($v['id'], $tmp_delivery_count[$v['source_id']]) && in_array($tmp['state'], $state4)) {
  2524. $tmp_delivery_count[$v['source_id']][] = $v['id']; //交定
  2525. $data['delivery_count'] += 1;
  2526. }
  2527. if (!in_array($v['id'], $tmp_order_count[$v['source_id']]) && in_array($tmp['state'], $state5)) {
  2528. $tmp_order_count[$v['source_id']][] = $v['id']; //签单量
  2529. $data['order_count'] += 1;
  2530. }
  2531. if (!in_array($v['id'], $tmp_maika_count) && in_array($tmp['state'], $state8)) {
  2532. $tmp_maika_count[$v['source_id']][] = $v['id']; //卖卡
  2533. $data['maika_count'] += 1;
  2534. }
  2535. }
  2536. }
  2537. $data['valid_grawth'] = $data['clue_count'] == 0 ? '0%' : round($data['valid_count'] / $data['clue_count'] * 100, 2) . '%'; //有效率
  2538. if ($data['valid_count'] > 0) {
  2539. $data['meet_grawth'] = round($data['meet_count'] / $data['valid_count'] * 100, 2) . '%';
  2540. } else {
  2541. $data['meet_grawth'] = '0%';
  2542. }
  2543. if ($data['meet_count'] > 0) {
  2544. $data['delivery_grawth'] = round($data['delivery_count'] / $data['meet_count'] * 100, 2) . '%';
  2545. $data['order_grawth'] = round($data['order_count'] / $data['meet_count'] * 100, 2) . '%';
  2546. } else {
  2547. $data['delivery_grawth'] = '0%';
  2548. $data['order_grawth'] = '0%';
  2549. }
  2550. return json(['code' => 0, 'data' => $data, 'msg' => '操作成功']);
  2551. }
  2552. /**
  2553. * 线上巡店查询结果导出
  2554. */
  2555. public function export($m)
  2556. {
  2557. $typeList = [
  2558. 'newStatisticsExport' => ['headerMethod' => 'getNewVisitLogHeaders', 'dataMethod' => 'new_customer_statistics'],
  2559. 'officeStatisticsExport' => ['headerMethod' => 'getOfficeHeaders', 'dataMethod' => 'officeStatistics'],
  2560. 'statisticsExport' => ['headerMethod' => 'getVisitLogHeaders', 'dataMethod' => 'customer_statistics'],
  2561. ];
  2562. if (!isset($typeList[$m])) {
  2563. json(['code' => 1, 'msg' => '类型不存在'])->send();
  2564. return;
  2565. }
  2566. $param = Request::post();
  2567. $filename = uniqid();
  2568. $log = [
  2569. 'root_id' => request()->employee->root_id,
  2570. 'file' => $filename . '.csv',
  2571. 'type' => $m,
  2572. 'employee_id' => request()->employee->id,
  2573. 'search' => json_encode($param)
  2574. ];
  2575. $exportLog = ExportLog::create($log);
  2576. json(['code' => 0, 'msg' => '导出中,请稍后在记录中查看'])->send();
  2577. $headerMethod = $typeList[$m]['headerMethod'];
  2578. $dataMethod = $typeList[$m]['dataMethod'];
  2579. $header = $this->$headerMethod();
  2580. $row = [];
  2581. $title = [];
  2582. foreach ($header['header1'] as $item) {
  2583. if (isset($item['colspan'])) {
  2584. for ($s = 0; $s < $item['colspan']; $s++) {
  2585. $h2 = array_shift($header['header2']);
  2586. $title[] = strip_tags($h2['title']);
  2587. }
  2588. } else {
  2589. $title[] = strip_tags($item['title']);
  2590. }
  2591. if (isset($item['child'])) {
  2592. foreach ($item['child'] as $k) {
  2593. $row[] = $k;
  2594. }
  2595. } else {
  2596. $row[] = $item['field'];
  2597. }
  2598. }
  2599. touch('../download/' . $filename . '.csv');
  2600. $fp = fopen('../download/' . $filename . '.csv', 'a');
  2601. fputcsv($fp, $title);
  2602. $n = 2000;
  2603. $p = 1;
  2604. do {
  2605. $data = $this->$dataMethod($p, $n);
  2606. $data = $data->getData();
  2607. foreach ($data['data'] as $rowData) {
  2608. $r = [];
  2609. foreach ($row as $field) {
  2610. $r[] = isset($rowData[$field]) ? $rowData[$field] : '';
  2611. }
  2612. fputcsv($fp, $r);
  2613. }
  2614. $p++;
  2615. } while ($n * ($p - 1) < $data['count']);
  2616. fclose($fp);
  2617. // 文件上传
  2618. $rs = ossUpload($m . '/' . $filename . '.csv', './download/' . $filename . '.csv');
  2619. if ($rs) {
  2620. unlink('../download/' . $filename . '.csv');
  2621. }
  2622. $exportLog->save(['state' => 1]);
  2623. }
  2624. /**
  2625. * 线上巡店查询结果导出
  2626. */
  2627. public function exportList()
  2628. {
  2629. $param = request()->param();
  2630. $methodtype = 'statisticsExport';
  2631. if (!empty($param['methodtype'])) $methodtype = $param['methodtype'];
  2632. if (!in_array($methodtype, ['statisticsExport', 'newStatisticsExport', 'officeStatisticsExport'])) return json(['code' => 1, 'msg' => '方法参数错误']);
  2633. if (!request()->isAjax()) {
  2634. View::assign('methodtype', $methodtype);
  2635. return View::fetch();
  2636. }
  2637. $count = AsyncTask::where([['root_id', '=', request()->employee->root_id], ['method', '=', $methodtype]])->count();
  2638. $data = AsyncTask::field('params, addtime,state,complete_time,result')
  2639. ->where([['root_id', '=', request()->employee->root_id], ['method', '=', $methodtype]])
  2640. ->order('id desc')
  2641. ->page($param['page'], $param['limit'])
  2642. ->select();
  2643. if (!empty($data)) $data = $data->toArray();
  2644. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('name', 'id');
  2645. foreach ($data as &$item) {
  2646. if (isset($item['params']['sdate']) && $item['params']['sdate'] != '') $item['params']['sdate'] = date('Y-m-d', strtotime($item['params']['sdate']));
  2647. if (isset($item['params']['edate']) && $item['params']['edate'] != '') $item['params']['edate'] = date('Y-m-d', strtotime($item['params']['edate']));
  2648. if (isset($item['params']['org_id']) && isset($org[$item['params']['org_id']])) $item['params']['org_id'] = $org[$item['params']['org_id']];
  2649. }
  2650. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  2651. }
  2652. /**
  2653. * daochu文件下载
  2654. */
  2655. public function downloadXls($path, $methodtype)
  2656. {
  2657. if (in_array($methodtype, ['statisticsExport', 'newStatisticsExport', 'officeStatisticsExport', 'behaviorStatisticsData', 'highBehaviorStatisticsData', 'manageStatisticsData', 'customer_list_data', 'generalStatisticsData'])) {
  2658. $had = ExportLog::where([['root_id', '=', request()->employee->root_id], ['type', '=', $methodtype], ['file', '=', $path], ['state', '=', 1]])->find();
  2659. $floder = 'statisticsExport/';
  2660. if (in_array($methodtype, ['newStatisticsExport'])) $floder = $methodtype . '/';
  2661. } else {
  2662. return json(['code' => 1, 'msg' => '方法参数错误']);
  2663. }
  2664. $ossExit = ossFileExist($floder . $path);
  2665. if (empty($had) || (!file_exists('../download/' . $path) && !$ossExit)) return View::display('<script>alert("{$msg}");window.close();</script>', ['msg' => '文件不存在或数据处理中,请稍后再试']);
  2666. // 检测文件
  2667. if ($ossExit) {
  2668. $path = 'https://' . config('app.ali_oss_bindurl') . '/' . $floder . $path;
  2669. return redirect($path);
  2670. }
  2671. return download('../download/' . $path, $path);
  2672. }
  2673. /**
  2674. * 客户管理数据统计
  2675. */
  2676. public function customer_list()
  2677. {
  2678. $root_id = request()->employee->root_id;
  2679. $org = Org::where([['path', 'like', $root_id . '-%'], ['org_type', '=', 1]])->field('id,name')->select()->toArray();
  2680. View::assign('org', $org);
  2681. //渠道来源下拉
  2682. $source = CustomerSource::where('root_id', $root_id)->column('id,source');
  2683. View::assign('source', $source);
  2684. return View::fetch();
  2685. }
  2686. /**
  2687. * 客户统计列表数据
  2688. */
  2689. public function customer_list_data($page = 1, $limit = 20)
  2690. {
  2691. $root_id = request()->employee->root_id;
  2692. $source_id = input('source_id', 0);
  2693. $employee_name = input('employee_name', '', 'trim');
  2694. $customer_type = input('customer_type', '', 'intval');
  2695. switch ($customer_type) {
  2696. case 1:
  2697. $where[] = ['crm_res_id', '>', 0];
  2698. $where[] = ['remark', '<>', '公海获取'];
  2699. break;
  2700. case 2:
  2701. $where[] = ['remark', '=', '活动报名建档'];
  2702. break;
  2703. case 3:
  2704. $where[] = ['agents_id', '>', 0];
  2705. break;
  2706. case 4:
  2707. $where[] = ['crm_res_id', 'NULL', null];
  2708. $where[] = ['remark', '<>', '活动报名建档'];
  2709. $where[] = ['remark', '<>', '公海获取'];
  2710. $where[] = ['agents_id', 'NULL', null];
  2711. break;
  2712. case 5:
  2713. $where[] = ['remark', '=', '公海获取'];
  2714. break;
  2715. default:
  2716. break;
  2717. }
  2718. if ($employee_name) {
  2719. $employee_ids = Employee::where([['name', 'like', '%' . $employee_name . '%'], ['root_id', '=', $root_id]])->column('id');
  2720. $where[] = ['employee_id', 'in', $employee_ids];
  2721. }
  2722. $start_date = input('start_date', '', 'trim');
  2723. $end_date = input('end_date', '', 'trim');
  2724. if ($start_date) {
  2725. $count_dates = explode(' - ', $start_date);
  2726. $start_date = $count_dates[0];
  2727. $end_date = $count_dates[1];
  2728. } else {
  2729. $start_date = $end_date = '';
  2730. }
  2731. if ($start_date && $end_date) {
  2732. $where[] = ['addtime', 'between', [$start_date . ' 00:00:00', $end_date . ' 23:59:59']];
  2733. } elseif ($start_date) {
  2734. $where[] = ['addtime', '>', $start_date . ' 00:00:00'];
  2735. } elseif ($end_date) {
  2736. $where[] = ['addtime', '<', $end_date . ' 23:59:59'];
  2737. }
  2738. $org_id = input('select_org_id', '', 'trim');
  2739. $org_ids = orgSubIds($root_id);
  2740. if ($org_id) {
  2741. $org_id = explode(',', $org_id);
  2742. $sel_org_ids = [];
  2743. foreach ($org_id as $k => $v) {
  2744. if (in_array($v, $org_ids)) {
  2745. $sel_org_ids[] = $v;
  2746. }
  2747. }
  2748. $where[] = ['org_id', 'in', $sel_org_ids];
  2749. } else {
  2750. $where[] = ['org_id', 'in', $org_ids];
  2751. }
  2752. if ($source_id) $where[] = ['source_id', '=', $source_id];
  2753. $where[] = ['died', '<>', 2];
  2754. $where[] = ['employee_id', 'not null', ''];
  2755. $where[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  2756. //2023-02-26 只统计有效客户 和CRM客户管理-店面客户管理保持一致
  2757. $state11 = Customer::changeState('待确认', 'chaos');
  2758. $state22 = Customer::changeState('无效', 'chaos');
  2759. $model = Customer::where($where)->where(function ($query) {
  2760. $state11 = Customer::changeState('待确认', 'chaos');
  2761. $state22 = Customer::changeState('无效', 'chaos');
  2762. $or1[] = ['crm_res_id', 'null', null];
  2763. $or1[] = ['state', 'not in', array_merge($state11, $state22)];
  2764. $or2[] = ['crm_res_id', '>', 0];
  2765. $or2[] = ['state', 'not in', $state22];
  2766. $query->whereOr([$or1, $or2]);
  2767. });
  2768. $count = $model->count();
  2769. $list = $model->with(['employee', 'org', 'designer', 'source'])->page($page, $limit)->order('addtime desc')->select()->each(function ($item) {
  2770. if ($item['remark'] == '公海获取') {
  2771. $customer_type = '公海获取';
  2772. } elseif ($item['agents_id']) {
  2773. $customer_type = '装修推荐管';
  2774. } elseif ($item['remark'] == '活动报名建档') {
  2775. $customer_type = '活动报名';
  2776. } elseif ($item['crm_res_id']) {
  2777. $customer_type = '资源库';
  2778. } else {
  2779. $customer_type = '自建';
  2780. }
  2781. $item['customer_type'] = $customer_type;
  2782. if (empty($item['name'])) {
  2783. $item['name'] = '未知';
  2784. }
  2785. if (empty($item['community_name'])) {
  2786. $item['community_name'] = '未知';
  2787. }
  2788. if (empty($item['house_type'])) {
  2789. $item['house_type'] = '未知';
  2790. }
  2791. if (empty($item['square'])) {
  2792. $item['square'] = '未知';
  2793. }
  2794. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  2795. $item['signed_money_data'] = $item->getData('signed_money');
  2796. // 指派客户经理及设计师
  2797. $assigned_personal_manager_ids = [];
  2798. if ($item['assigned_personnel']) {
  2799. $assigned_personal = explode(',', $item['assigned_personnel']);
  2800. $assigned_personal_list = Employee::with('org')->where('id', 'in', $assigned_personal)->select()->toArray();
  2801. $assigned_personal_designer = [];
  2802. $assigned_personal_manager = [];
  2803. $assigned_personal_designer_org = [];
  2804. foreach ($assigned_personal_list as $k => $v) {
  2805. if ($v['org']['org_type'] == 2) {
  2806. $assigned_personal_designer[] = $v['name'];
  2807. $assigned_personal_designer_org[] = $v['org']['name'];
  2808. } else {
  2809. $assigned_personal_manager[] = $v['name'];
  2810. $assigned_personal_manager_ids[] = $v['id'];
  2811. }
  2812. }
  2813. $item['assigned_personal_manager'] = $assigned_personal_manager ? implode('、', $assigned_personal_manager) : '无';
  2814. $item['assigned_personal_designer'] = $assigned_personal_designer ? implode('、', $assigned_personal_designer) : '无';
  2815. $item['assigned_personal_designer_org'] = $assigned_personal_designer_org ? implode('、', $assigned_personal_designer_org) : '无';
  2816. } else {
  2817. $item['assigned_personal_manager'] = '无';
  2818. $item['assigned_personal_designer'] = '无';
  2819. $item['assigned_personal_designer_org'] = '无';
  2820. }
  2821. // 未跟踪天数
  2822. $last_visit_log = CustomerVisitLog::where('customer_id', '=', $item['id'])->findOrEmpty();
  2823. if (!$last_visit_log->isEmpty()) {
  2824. $time_long = time() - strtotime($last_visit_log['addtime']);
  2825. } else {
  2826. $time_long = time() - strtotime($item['addtime']);
  2827. }
  2828. $no_visit_day = ceil($time_long / (24 * 3600));
  2829. $item['no_visit_day'] = $no_visit_day;
  2830. // 是否加微
  2831. $ext = json_decode($item['ext'], true);
  2832. $item['wechat'] = '否'; // 是否加微
  2833. $item['add_wechat_type'] = '无'; // 加微类型
  2834. $item['add_wechat_time'] = '无'; // 加微时间
  2835. $item['plan_issuing_date'] = '无'; //出方案时间
  2836. $item['drawing_date'] = '无'; // 出图时间
  2837. $item['group_building_date'] = '无'; // 建群时间
  2838. if (!empty($ext)) {
  2839. foreach ($ext as $v) {
  2840. if (isset($v['keyname']) && $v['keyname'] == 'wechat' && !empty($v['value'])) {
  2841. $item['wechat'] = CustomerPortraitFieldSelect::where('id', $v['value'])->value('name');
  2842. } elseif (isset($v['keyname']) && $v['keyname'] == 'add_wechat_time' && !empty($v['value'])) {
  2843. $item['wechat'] = '是';
  2844. } elseif (isset($v['keyname']) && $v['keyname'] == 'add_wechat_type' && !empty($v['value'])) {
  2845. $item['wechat'] = '是';
  2846. }
  2847. if (isset($v['keyname']) && $v['keyname'] == 'add_wechat_time' && !empty($v['value'])) {
  2848. $item['add_wechat_time'] = $v['value'];
  2849. }
  2850. if (isset($v['keyname']) && $v['keyname'] == 'add_wechat_type' && !empty($v['value'])) {
  2851. $item['add_wechat_type'] = CustomerPortraitFieldSelect::where('id', $v['value'])->value('name');
  2852. }
  2853. if (isset($v['keyname']) && $v['keyname'] == 'plan_issuing_date' && !empty($v['value'])) {
  2854. $item['plan_issuing_date'] = $v['value'];
  2855. }
  2856. if (isset($v['keyname']) && $v['keyname'] == 'drawing_date' && !empty($v['value'])) {
  2857. $item['drawing_date'] = $v['value'];
  2858. }
  2859. if (isset($v['keyname']) && $v['keyname'] == 'group_building_date' && !empty($v['value'])) {
  2860. $item['group_building_date'] = $v['value'];
  2861. }
  2862. }
  2863. }
  2864. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  2865. $lf_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $lf_state]])->order('addtime asc')->select()->toArray();
  2866. if (!empty($lf_list)) {
  2867. $item['is_liangfang'] = '是';
  2868. $item['liangfang_date'] = $lf_list[0]['addtime'];
  2869. $item['once_liangfang_days'] = ceil((strtotime($lf_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
  2870. } else {
  2871. $item['is_liangfang'] = '否';
  2872. $item['liangfang_date'] = '无';
  2873. $item['once_liangfang_days'] = '无';
  2874. }
  2875. // 到店
  2876. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  2877. $dd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $dd_state]])->order('addtime asc')->select()->toArray();
  2878. $item['once_daodian'] = '否';
  2879. $item['daodian_date'] = '无';
  2880. $item['once_daodian_days'] = '无';
  2881. $item['once_daodian_shop'] = '无';
  2882. $item['twice_daodian_date'] = '无'; //二次到店时间
  2883. $item['twice_daodian_days'] = '无'; // 二次到店周期 二次到店时间 - 一次到店时间
  2884. $item['twice_daodian_shop'] = '无';
  2885. if (!empty($dd_list)) {
  2886. $item['once_daodian'] = '是';
  2887. $item['daodian_date'] = $dd_list[0]['addtime'];
  2888. $item['once_daodian_days'] = ceil((strtotime($dd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
  2889. if (!empty($dd_list[0]['shop_id'])) {
  2890. $shop_name = Shop::where('id', $dd_list[0]['shop_id'])->findOrEmpty();
  2891. if ($shop_name->isEmpty()) {
  2892. $item['once_daodian_shop'] = '未知';
  2893. } else {
  2894. $item['once_daodian_shop'] = $shop_name['name'];
  2895. }
  2896. }
  2897. // 二次到店
  2898. if (count($dd_list) > 1) {
  2899. $item['twice_daodian_date'] = $dd_list[1]['addtime'];
  2900. $item['twice_daodian_days'] = ceil((strtotime($dd_list[1]['addtime']) - strtotime($dd_list[0]['addtime'])) / (24 * 3600));
  2901. $shop_name = Shop::where('id', $dd_list[1]['shop_id'])->findOrEmpty();
  2902. if ($shop_name->isEmpty()) {
  2903. $item['twice_daodian_shop'] = '未知';
  2904. } else {
  2905. $item['twice_daodian_shop'] = $shop_name['name'];
  2906. }
  2907. }
  2908. }
  2909. // 量房 - 到店 周期
  2910. if (!empty($lf_list) && !empty($dd_list)) {
  2911. if ($lf_list[0]['addtime'] > $dd_list[0]['addtime']) {
  2912. $item['liangfang_daodian_days'] = ceil((strtotime($lf_list[0]['addtime']) - strtotime($dd_list[0]['addtime'])) / (24 * 3600));
  2913. } else {
  2914. $item['liangfang_daodian_days'] = ceil((strtotime($dd_list[0]['addtime']) - strtotime($lf_list[0]['addtime'])) / (24 * 3600));
  2915. }
  2916. } else {
  2917. $item['liangfang_daodian_days'] = '无';
  2918. }
  2919. // 交定(签单)
  2920. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  2921. $jd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $jd_state]])->order('addtime asc')->select()->toArray();
  2922. if (!empty($jd_list)) {
  2923. $item['is_deposit'] = '是';
  2924. $item['deposit_date'] = $jd_list[0]['addtime'];
  2925. $item['deposit_days'] = ceil((strtotime($jd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
  2926. } else {
  2927. $item['is_deposit'] = '否';
  2928. $item['deposit_date'] = '无';
  2929. $item['deposit_days'] = '无';
  2930. }
  2931. // 签单(转单)
  2932. $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  2933. $qd_list = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $qd_state]])->order('addtime asc')->select()->toArray();
  2934. if (!empty($qd_list)) {
  2935. $item['sign_date'] = $qd_list[0]['addtime'];
  2936. $item['sign_days'] = ceil((strtotime($qd_list[0]['addtime']) - strtotime($item['addtime'])) / (24 * 3600));
  2937. } else {
  2938. $item['sign_date'] = '无';
  2939. $item['sign_days'] = '无';
  2940. }
  2941. // 交定转单周期
  2942. if (!empty($jd_list) && !empty($qd_list)) {
  2943. if ($jd_list[0]['addtime'] > $qd_list[0]['addtime']) {
  2944. $item['deposit_sign_days'] = ceil((strtotime($jd_list[0]['addtime']) - strtotime($qd_list[0]['addtime'])) / (24 * 3600));
  2945. } else {
  2946. $item['deposit_sign_days'] = ceil((strtotime($qd_list[0]['addtime']) - strtotime($jd_list[0]['addtime'])) / (24 * 3600));
  2947. }
  2948. } else {
  2949. $item['deposit_sign_days'] = '无';
  2950. }
  2951. // 一次到店未签单时间
  2952. $first_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $dd_state]])->order('addtime asc')->findOrEmpty();
  2953. $item['first_visit_no_sign'] = '无';
  2954. if (!$first_visit->isEmpty()) {
  2955. $first_visit_no_sign = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['addtime', '>', $first_visit['addtime']]])->order('addtime asc')->findOrEmpty();
  2956. if (!$first_visit_no_sign->isEmpty()) {
  2957. if (!in_array($first_visit_no_sign['state'], $qd_state)) {
  2958. $item['first_visit_no_sign'] = $first_visit['addtime'];
  2959. }
  2960. }
  2961. }
  2962. // 客户经理回访
  2963. if (!empty($assigned_personal_manager_ids)) {
  2964. $manager_next_visit = CustomerVisitLog::where([['employee_id', 'in', $assigned_personal_manager_ids], ['next_contact_date', 'not null', ''], ['customer_id', '=', $item['id']]])->field('customer_id,next_contact_date,addtime,employee_id')->select()->toArray();
  2965. $manager_visit_times = 0;
  2966. $manager_visit_days = 0;
  2967. foreach ($manager_next_visit as $k => $v) {
  2968. unset($visit_where);
  2969. $visit_where[] = ['addtime', 'between', [$v['next_contact_date'] . ' 00:00:00', $v['next_contact_date'] . ' 23:59:59']];
  2970. $visit_where[] = ['customer_id', '=', $v['customer_id']];
  2971. $visit_where[] = ['employee_id', '=', $v['employee_id']];
  2972. $visit_next_find = CustomerVisitLog::where($visit_where)->findOrEmpty();
  2973. if (!$visit_next_find->isEmpty()) {
  2974. $manager_visit_times++;
  2975. $next_days = ceil((strtotime($visit_next_find['addtime']) - strtotime($v['next_contact_date'])) / (24 * 3600));
  2976. $manager_visit_days += $next_days;
  2977. }
  2978. }
  2979. $item['manager_visit_times'] = $manager_visit_times;
  2980. $item['manager_visit_cycle'] = $manager_visit_times ? round($manager_visit_days / $manager_visit_times, 1) : '无';
  2981. } else {
  2982. $item['manager_visit_times'] = 0;
  2983. $item['manager_visit_cycle'] = '无';
  2984. }
  2985. // 经理回访
  2986. $employee_next_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['next_contact_date', 'not null', ''], ['employee_id', '=', $item['employee_id']]])->field('customer_id,next_contact_date,employee_id')->select()->toArray();
  2987. $employee_visit_times = 0;
  2988. $employee_visit_days = 0;
  2989. foreach ($employee_next_visit as $k => $v) {
  2990. unset($visit_where);
  2991. $visit_where[] = ['addtime', 'between', [$v['next_contact_date'] . ' 00:00:00', $v['next_contact_date'] . ' 23:59:59']];
  2992. $visit_where[] = ['customer_id', '=', $v['customer_id']];
  2993. $visit_where[] = ['employee_id', '=', $v['employee_id']];
  2994. $visit_next_find = CustomerVisitLog::where($visit_where)->findOrEmpty();
  2995. if (!$visit_next_find->isEmpty()) {
  2996. $employee_visit_times++;
  2997. $next_days = ceil((strtotime($visit_next_find['addtime']) - strtotime($v['next_contact_date'])) / (24 * 3600));
  2998. $employee_visit_days += $next_days;
  2999. }
  3000. }
  3001. $item['employee_visit_times'] = $employee_visit_times;
  3002. $item['employee_visit_cycle'] = $employee_visit_times ? round($employee_visit_days / $employee_visit_times, 1) : '无';
  3003. $wx_state = Customer::changeState('无效', 'chaos');
  3004. if (in_array($item['state'], $wx_state)) {
  3005. $item['invalid'] = '是';
  3006. } else {
  3007. $item['invalid'] = '否';
  3008. }
  3009. if ($item['died'] == 2) {
  3010. $item['died'] = '是';
  3011. } else {
  3012. $item['died'] = '否';
  3013. }
  3014. // 卖卡时间
  3015. $maika_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3016. $maika_visit = CustomerVisitLog::where([['customer_id', '=', $item['id']], ['state', 'in', $maika_state]])->order('addtime desc')->findOrEmpty();
  3017. $item['maika_time'] = '无';
  3018. if (!$maika_visit->isEmpty()) {
  3019. $item['maika_time'] = $maika_visit['addtime'];
  3020. }
  3021. })->toArray();
  3022. $clue_where[] = ['name', '=', 'clueTag'];
  3023. $clue_where[] = ['root_id', '=', request()->employee->root_id];
  3024. $find = Setting::where($clue_where)->findOrEmpty();
  3025. if ($find->isEmpty()) {
  3026. $clue_tag = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  3027. } else {
  3028. $clue_tag = array_filter(explode(',', $find['content']));
  3029. }
  3030. foreach ($list as $k => $v) {
  3031. $list[$k]['house_delivery_time'] = $v['house_delivery_time'] ? $v['house_delivery_time'] : '未知';
  3032. $list[$k]['employee_name'] = $v['employee']['name'] ?? '无';
  3033. $list[$k]['source_name'] = $v['source']['source'] ?? '未知';
  3034. if ($v['invalid'] == '否') {
  3035. $list[$k]['invalid_remark'] = '';
  3036. } else {
  3037. $wx_state = CustomerVisitLog::changeState('无效', 'chaos');
  3038. $invalid_log = CustomerVisitLog::where([['customer_id', '=', $v['id']], ['state', 'in', $wx_state]])->order('addtime desc')->findOrEmpty();
  3039. if (!$invalid_log->isEmpty() && !empty($invalid_log['remark']) && in_array($invalid_log['remark'], $clue_tag)) {
  3040. $list[$k]['invalid_remark'] = $invalid_log['remark'];
  3041. } else {
  3042. $list[$k]['invalid_remark'] = '其他';
  3043. }
  3044. }
  3045. }
  3046. return json(['code' => 0, 'data' => $list, 'count' => $count, 'page' => ceil($count / $limit)]);
  3047. }
  3048. /**
  3049. * 客户统计列表导出
  3050. */
  3051. public function customer_list_export()
  3052. {
  3053. if (!request()->isAjax()) {
  3054. $header = [
  3055. ["field" => 'name', "title" => '客户姓名', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3056. ["field" => 'phone', "title" => '手机号', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3057. ["field" => 'community_name', "title" => '小 区', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3058. ["field" => 'house_type', "title" => '房屋类型', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3059. ["field" => 'house_delivery_time', "title" => '交房时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3060. ["field" => 'square', "title" => '面积', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3061. ["field" => 'org_name', "title" => '部门', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3062. ["field" => 'employee_name', "title" => '业务员', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3063. ["field" => 'addtime', "title" => '添加时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3064. ["field" => 'source_name', "title" => '来源', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3065. ["field" => 'assigned_personal_manager', "title" => '指派客户经理', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3066. ["field" => 'assigned_personal_designer_org', "title" => '指派设计部', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3067. ["field" => 'assigned_personal_designer', "title" => '指派设计师', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3068. ["field" => 'no_visit_day', "title" => '未跟踪天数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3069. ["field" => 'wechat', "title" => '是否加微信', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3070. ["field" => 'add_wechat_type', "title" => '加微类型', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3071. ["field" => 'add_wechat_time', "title" => '加微时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3072. ["field" => 'group_building_date', "title" => '建群时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3073. ["field" => 'is_liangfang', "title" => '是否量房', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3074. ["field" => 'liangfang_date', "title" => '量房时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3075. ["field" => 'once_liangfang_days', "title" => '量房周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3076. ["field" => 'plan_issuing_date', "title" => '计划出初步方案时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3077. ["field" => 'once_daodian', "title" => '是否一次到店', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3078. ["field" => 'daodian_date', "title" => '一次到店时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3079. ["field" => 'once_daodian_shop', "title" => '一次所到店面', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3080. ["field" => 'once_daodian_days', "title" => '一次到店周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3081. ["field" => 'liangfang_daodian_days', "title" => '量房到店周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3082. ["field" => 'twice_daodian_date', "title" => '二次到店时间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3083. ["field" => 'twice_daodian_shop', "title" => '二次所到店面', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3084. ["field" => 'twice_daodian_days', "title" => '二次到店周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  3085. ["field" => 'is_deposit', "title" => '是否交定/签单', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3086. ["field" => 'deposit_date', "title" => '交定/签单时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3087. ["field" => 'drawing_date', "title" => '出图时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3088. ["field" => 'sign_date', "title" => '转单时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3089. ["field" => 'sign_days', "title" => '交定转单周期', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3090. ["field" => 'signed_money_data', "title" => '合同金额(元)', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3091. ["field" => 'first_visit_no_sign', "title" => '一次到店未签单时间', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3092. ["field" => 'manager_visit_times', "title" => '客户经理回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3093. ["field" => 'manager_visit_cycle', "title" => '客户经理回访周期', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3094. ["field" => 'employee_visit_times', "title" => '经理回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3095. ["field" => 'employee_visit_cycle', "title" => '经理回访平均周期', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3096. ["field" => 'invalid', "title" => '无效客户', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3097. ["field" => 'invalid_remark', "title" => '无效原因', "align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"],
  3098. ["field" => 'died', "title" => '是否死单', " align" => 'center', "rowspan" => 1, "class" => "bgFED9D9"]
  3099. ];
  3100. view::assign('row', json_encode($header));
  3101. view::assign('header', $header);
  3102. return View::fetch();
  3103. } else {
  3104. return $this->customer_list_data();
  3105. }
  3106. }
  3107. /**
  3108. * 总统计页面
  3109. */
  3110. public function generalStatistics()
  3111. {
  3112. $root_id = request()->employee->root_id;
  3113. //房屋状态是否开启
  3114. $house_status_where = [
  3115. ['root_id', '=', $root_id],
  3116. ['keyname', '=', 'house_status'],
  3117. ['status', '=', 0]
  3118. ];
  3119. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  3120. $field = [];
  3121. if (!$house_status_per->isEmpty()) {
  3122. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  3123. $fields = [];
  3124. foreach ($field as $v) {
  3125. $fields[] = [
  3126. 'field' => 'house_status_' . $v['id'],
  3127. 'title' => $v['name'],
  3128. 'align' => 'center',
  3129. 'width' => 100,
  3130. 'rowspan' => 2
  3131. ];
  3132. }
  3133. $field = $fields;
  3134. }
  3135. View::assign('field', json_encode($field));
  3136. //无效客资
  3137. $invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  3138. $arr3 = [];
  3139. if ($invalid_customer) {
  3140. $arr3 = explode(',', $invalid_customer);
  3141. } else {
  3142. $arr3 = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  3143. }
  3144. $field4 = [];
  3145. foreach ($arr3 as $k3 => $v3) {
  3146. $field4[] = [
  3147. 'field' => 'invalid_customer_' . $k3,
  3148. 'title' => $v3,
  3149. 'align' => 'center',
  3150. 'width' => 100,
  3151. 'rowspan' => 1
  3152. ];
  3153. }
  3154. View::assign('field4', json_encode($field4));
  3155. //返回组织列表
  3156. $org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
  3157. View::assign('org', $org);
  3158. $type = input('type', '', 'intval');
  3159. View::assign('type', 2);
  3160. $org_id = input('org_id', '', 'intval');
  3161. View::assign('org_id', $org_id);
  3162. return View::fetch();
  3163. }
  3164. /**
  3165. * 平均周期
  3166. */
  3167. public function avgCycle($cus, $vis)
  3168. {
  3169. $state1 = CustomerVisitLog::changeState('已交定', 'chaos');
  3170. $state2 = CustomerVisitLog::changeState('已量房', 'chaos');
  3171. $state3 = CustomerVisitLog::changeState('已到店', 'chaos');
  3172. $dep_time = [];
  3173. $room_time = [];
  3174. $store_time = [];
  3175. $next_date = [];
  3176. foreach ($vis as $v) {
  3177. //交定日期
  3178. if (in_array($v['state'], $state1)) {
  3179. $dep_time[$v['customer_id']] = $v['addtime'];
  3180. } elseif (in_array($v['state'], $state2)) {
  3181. $room_time[$v['customer_id']] = $v['addtime'];
  3182. } elseif (in_array($v['state'], $state3)) {
  3183. $store_time[$v['customer_id']] = $v['addtime'];
  3184. }
  3185. //平均回访次数
  3186. if (!isset($next_date[$v['customer_id']]['addtime'])) {
  3187. $next_date[$v['customer_id']]['addtime'] = [];
  3188. }
  3189. if (!isset($next_date[$v['customer_id']]['next_date'])) {
  3190. $next_date[$v['customer_id']]['next_date'] = [];
  3191. }
  3192. $next_date[$v['customer_id']]['addtime'][] = date('Y-m-d', strtotime($v['addtime']));
  3193. $next_date[$v['customer_id']]['next_date'][] = date('Y-m-d', strtotime($v['next_contact_date']));
  3194. }
  3195. $dep_cycle = $room_cycle = $room_store_cycle = $next = [];
  3196. foreach ($cus as $v2) {
  3197. if (isset($dep_time[$v2['id']])) {
  3198. $dep_cycle[] = strtotime($dep_time[$v2['id']]) - strtotime($v2['addtime']);
  3199. }
  3200. if (isset($room_time[$v2['id']])) {
  3201. $room_cycle[] = strtotime($room_time[$v2['id']]) - strtotime($v2['addtime']);
  3202. }
  3203. if (isset($room_time[$v2['id']]) && isset($store_time[$v2['id']])) {
  3204. $room_store_cycle[] = abs(strtotime($room_time[$v2['id']]) - strtotime($store_time[$v2['id']]));
  3205. }
  3206. if (isset($next_date[$v2['id']]['addtime']) && $next_date[$v2['id']]['next_date']) {
  3207. $next[] = count(array_filter(array_intersect($next_date[$v2['id']]['addtime'], $next_date[$v2['id']]['next_date'])));
  3208. }
  3209. }
  3210. //平均交定周期
  3211. $data['dep_cycle'] = empty($dep_cycle) ? 0 : ceil(count($dep_cycle) / array_sum($dep_cycle));
  3212. //平均量房周期
  3213. $data['room_cycle'] = empty($room_cycle) ? 0 : ceil(count($room_cycle) / array_sum($room_cycle));
  3214. //平均量房到店周期
  3215. $data['room_store_cycle'] = empty($room_store_cycle) ? 0 : ceil(count($room_store_cycle) / array_sum($room_store_cycle));
  3216. //平均回访次数
  3217. $data['next'] = !array_sum($next) ? 0 : ceil(count($next) / array_sum($next));
  3218. return $data;
  3219. }
  3220. /**
  3221. * 获取见面客户
  3222. * @param $search_list 要判断有多少见面的log记录
  3223. * @param $before_list 之前见面的客户id集合
  3224. */
  3225. public function visitDeal($search_list, $before_list)
  3226. {
  3227. // 见面状态
  3228. $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
  3229. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  3230. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  3231. $state5 = CustomerVisitLog::changeState('已交定', 'chaos');
  3232. $state6 = CustomerVisitLog::changeState('已签单', 'chaos');
  3233. $visit_state = array_merge($state2, $state3, $state4, $state5, $state6);
  3234. $search_ids = [];
  3235. foreach ($search_list as $k => $v) {
  3236. if (in_array($v['state'], $visit_state)) {
  3237. $search_ids[] = $v['customer_id'];
  3238. }
  3239. }
  3240. $visit_ids = array_values(array_unique(array_diff($search_ids, $before_list)));
  3241. return $visit_ids;
  3242. }
  3243. /**
  3244. * 总统计数据
  3245. */
  3246. public function generalStatisticsData($page = 1, $limit = 10)
  3247. {
  3248. $param = request()->only(['date' => '', 'keyword' => '', 'org_id' => '', 'start_date' => '', 'end_date' => '', 'type' => '', 'source_id' => '']);
  3249. $root_id = request()->employee->root_id;
  3250. //日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
  3251. if ($param['start_date']) {
  3252. $count_dates = explode(' - ', $param['start_date']);
  3253. $param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0]));
  3254. $param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1]));
  3255. } else {
  3256. $param['start_date'] = '2010-01-01 00:00:00';
  3257. $param['end_date'] = date('Y-m-d H:i:s', time());
  3258. }
  3259. $date_query = [['addtime', 'between', [$param['start_date'], $param['end_date']]]];
  3260. $date_query1 = [['share_time', 'between', [$param['start_date'], $param['end_date']]]];
  3261. //加微时间筛选
  3262. $sdate = strtotime($param['start_date']);
  3263. $edate = strtotime($param['end_date']);
  3264. $where = [
  3265. ['root_id', '=', $root_id],
  3266. ['uid', '>', 0],
  3267. ['state', '=', '在职']
  3268. ];
  3269. if ($param['keyword']) {
  3270. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  3271. }
  3272. $org_id = $param['org_id'] ? $param['org_id'] : $root_id;
  3273. $sub_orgs = orgSubIds($org_id);
  3274. if ($param['org_id']) {
  3275. $o = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
  3276. $orgIds = Org::where([['path', 'like', $o['path'] . '%']])->column('id');
  3277. $where[] = ['org_id', 'in', $orgIds];
  3278. } else {
  3279. $where[] = ['org_id', 'in', $sub_orgs];
  3280. }
  3281. $list = Employee::with(['org' => function ($query) {
  3282. $query->field('id, name');
  3283. }])->where($where)->field('id,org_id,name')->page($page, $limit)->select()->toArray();
  3284. $count = Employee::where($where)->count();
  3285. $page_employee = array_column($list, 'id');
  3286. $state2 = CustomerVisitLog::changeState('已到场', 'chaos');
  3287. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  3288. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  3289. $state5 = CustomerVisitLog::changeState('已交定', 'chaos');
  3290. $state6 = CustomerVisitLog::changeState('已签单', 'chaos');
  3291. $state7 = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3292. $wuxiao_log_state = CustomerVisitLog::changeState('无效', 'chaos');
  3293. $c_where[] = ['Customer.employee_id', 'in', $page_employee];
  3294. $c_where[] = ['CustomerVisitLog.confirm_date', 'between', [$param['start_date'], $param['end_date']]];
  3295. $c_where[] = ['Customer.state', 'NOT NULL', null];
  3296. $add_where[] = ['employee_id', 'in', $page_employee];
  3297. $add_where[] = ['addtime|employee_time', 'between', [$param['start_date'], $param['end_date']]];
  3298. $add_where[] = ['state', 'NOT NULL', null];
  3299. $db_where[] = ['Customer.employee_id', 'in', $page_employee];
  3300. $db_where[] = ['Customer.state', 'NOT NULL', null];
  3301. if ($param['source_id']) {
  3302. $c_where[] = ['Customer.source_id', '=', $param['source_id']];
  3303. $db_where[] = ['Customer.source_id', '=', $param['source_id']];
  3304. $jiav_where[] = ['source_id', '=', $param['source_id']];
  3305. $j_w_where[] = ['source_id', '=', $param['source_id']];
  3306. $add_where[] = ['source_id', '=', $param['source_id']];
  3307. }
  3308. // 加微客户查询
  3309. // 先查搜索时间段内部门客户的加微数据,再判断哪些客户是page_employee中员工的客户,再判断其中哪些是已经置为无效的客户,再把这些置为无效的客户拼接到page_empployee员工身上。
  3310. $page_employee_org = array_unique(array_column($list, 'org_id'));
  3311. $jiav_where[] = ['org_id', 'in', $page_employee_org];
  3312. $jiav_where[] = ['add_wechat_time', 'between', [$param['start_date'], $param['end_date']]];
  3313. $jiav_list = Customer::where($jiav_where)->column('id,employee_id,state');
  3314. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  3315. if ($date_query) {
  3316. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$param['start_date'], $param['end_date']]];
  3317. }
  3318. // $customer_list = Customer::where($c_where)->column('id,employee_id,state,ext,is_resource,source_id,addtime,signed_money,house_type,square,house_status,remark,crm_res_id,agents_id,died,assign_type,employee_time');
  3319. // 搜索时间段内的跟踪客户
  3320. $customer_list = Db::view('Customer', 'id,employee_id,state,ext,is_resource,source_id,addtime,signed_money,house_type,square,house_status,remark,crm_res_id,agents_id,died,assign_type,employee_time')
  3321. ->view('CustomerVisitLog', [], 'Customer.id=CustomerVisitLog.customer_id')
  3322. ->where($c_where)
  3323. ->group('Customer.id')
  3324. ->select()
  3325. ->toArray();
  3326. // 搜索时间段内添加的客户
  3327. $add_customer = Customer::where($add_where)->column('id,employee_id,crm_res_id,addtime,employee_time');
  3328. $add_cus_employee = [];
  3329. $fenpei_cus_employee = [];
  3330. foreach ($add_customer as $k => $v) {
  3331. if (empty($v['crm_res_id'])) {
  3332. if ($v['addtime'] > $param['start_date'] && $v['addtime'] < $param['end_date']) {
  3333. $add_cus_employee[$v['employee_id']][] = $v;
  3334. }
  3335. } else {
  3336. if ((!empty($v['employee_time']) && $v['employee_time'] > $param['start_date'] && $v['employee_time'] < $param['end_date'])) {
  3337. $fenpei_cus_employee[$v['employee_id']][] = $v;
  3338. }
  3339. }
  3340. }
  3341. // 搜索时间段内的跟踪记录
  3342. $vislog_list = Db::view('CustomerVisitLog', 'id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date')
  3343. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  3344. ->where($db_where)
  3345. ->select()
  3346. ->toArray();
  3347. $visit_state = array_merge($state2, $state3, $state4, $state5, $state6);
  3348. $be_where[] = ['Customer.employee_id', 'in', $page_employee];
  3349. $be_where[] = ['Customer.state', 'NOT NULL', null];
  3350. $be_where[] = ['CustomerVisitLog.confirm_date', '<', $param['start_date']];
  3351. $be_where[] = ['CustomerVisitLog.state', 'in', $visit_state];
  3352. // 搜索时间段外的到访记录
  3353. $before_log = Db::view('CustomerVisitLog', 'id,customer_id')
  3354. ->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id')
  3355. ->where($be_where)
  3356. ->select()
  3357. ->toArray();
  3358. // 员工见过面的客户
  3359. $employee_visit = [];
  3360. foreach ($before_log as $k => $v) {
  3361. $employee_visit[$v['employee_id']][] = $v['customer_id'];
  3362. }
  3363. // 处理加微数据
  3364. $jiav_employee = []; // 正常的客户
  3365. $jiav_employee_wuxiao = []; // 无效的客户
  3366. foreach ($jiav_list as $k => $v) {
  3367. if (in_array($v['employee_id'], $page_employee) && !in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
  3368. $jiav_employee[$v['employee_id']][] = $v['id'];
  3369. } elseif (in_array($v['state'], Customer::changeState('无效', 'chaos', true))) {
  3370. $jiav_employee_wuxiao[] = $v['id'];
  3371. }
  3372. }
  3373. // 去查询这些置为无效的加微客户原先都是谁的
  3374. $j_w_where[] = ['customer_id', 'in', $jiav_employee_wuxiao];
  3375. $j_w_where[] = ['employee_id', 'in', $page_employee];
  3376. $jiav_wuxiao_log = CustomerInvalidLog::where($j_w_where)->column('customer_id,employee_id');
  3377. $jiav_wuxiao_log_deal = [];
  3378. foreach ($jiav_wuxiao_log as $v) {
  3379. $jiav_wuxiao_log_deal[$v['employee_id']][] = $v['customer_id'];
  3380. }
  3381. foreach ($jiav_employee as $k => $v) {
  3382. if (isset($jiav_wuxiao_log_deal[$k])) {
  3383. $jiav_employee[$k] = array_unique(array_merge($v, $jiav_wuxiao_log_deal[$k]));
  3384. }
  3385. }
  3386. $customer_employee = []; // 员工下的客户
  3387. $customer_ids_employee = []; // 员工下的客户id
  3388. $customer_ids_key = []; // 客户所属员工
  3389. foreach ($customer_list as $k => $v) {
  3390. $customer_employee[$v['employee_id']][] = $v;
  3391. $customer_ids_employee[$v['employee_id']][] = $v['id'];
  3392. $customer_ids_key[$v['id']] = $v['employee_id'];
  3393. }
  3394. //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
  3395. $no_where = [
  3396. ['employee_id', 'in', array_column($list, 'id')],
  3397. ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')],
  3398. ['addtime', 'between', [$param['start_date'], $param['end_date']]]
  3399. ];
  3400. $no_valid_customer = CustomerVisitLog::where($no_where)->column('id,customer_id,state,addtime,next_contact_date,remark,employee_id,confirm_date');
  3401. $vislog_list = array_merge($vislog_list, $no_valid_customer);
  3402. $visit_log_employee = []; //员工跟进记录
  3403. foreach ($vislog_list as $k => $v) {
  3404. $have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
  3405. if ($have_employee) {
  3406. $v_customer_ids = $customer_ids_employee[$have_employee] ?? [];
  3407. // 只保留还是员工客户的跟踪记录,或者是员工置为无效的客户记录
  3408. if (!in_array($v['customer_id'], $v_customer_ids) && !in_array($v['state'], $wuxiao_log_state, true)) {
  3409. unset($vislog_list[$k]);
  3410. continue;
  3411. }
  3412. $visit_log_employee[$have_employee][] = $v;
  3413. }
  3414. }
  3415. $vislog_list = array_values($vislog_list);
  3416. foreach ($list as $k => $v) {
  3417. $list[$k]['visit_log'] = $visit_log_employee[$v['id']] ?? [];
  3418. $list[$k]['customer'] = $customer_employee[$v['id']] ?? [];
  3419. }
  3420. $customer['sign_customer'] = []; //签单客户
  3421. $customer['measuring_room_customer'] = []; //量房客户
  3422. $customer['maika_customer'] = []; //卖卡客户
  3423. $customer['measuring_room_time'] = []; //量房时间
  3424. $customer['to_the_store'] = []; //到店客户id
  3425. $customer['to_the_store_time'] = []; //到店时间
  3426. $customer['deposit_customer'] = []; //交定客户
  3427. $customer['deposit_date'] = []; // 交定时间
  3428. foreach ($vislog_list as $v) {
  3429. if (in_array($v['state'], $state6, true)) { //签单客户id
  3430. $customer['sign_customer'][] = $v['customer_id'];
  3431. } elseif (in_array($v['state'], $state3, true)) { //量房客户id
  3432. $customer['measuring_room_customer'][] = $v['customer_id'];
  3433. //量房时间 按照时间升序排列
  3434. if (empty($customer['measuring_room_time'][$v['customer_id']])) {
  3435. $customer['measuring_room_time'][$v['customer_id']] = [];
  3436. $customer['measuring_room_time'][$v['customer_id']][] = $v['confirm_date'];
  3437. } else {
  3438. $customer['measuring_room_time'][$v['customer_id']][] = $v['confirm_date'];
  3439. }
  3440. } elseif (in_array($v['state'], $state4, true)) { //到店客户id
  3441. $customer['to_the_store'][] = $v['customer_id'];
  3442. //到店时间 按照时间升序排列
  3443. if (empty($customer['to_the_store_time'][$v['customer_id']])) {
  3444. $customer['to_the_store_time'][$v['customer_id']] = [];
  3445. $customer['to_the_store_time'][$v['customer_id']][] = $v['confirm_date'];
  3446. } else {
  3447. $customer['to_the_store_time'][$v['customer_id']][] = $v['confirm_date'];
  3448. }
  3449. } elseif (in_array($v['state'], $state5, true)) { //交定客户
  3450. $customer['deposit_customer'][] = $v['customer_id'];
  3451. //交定时间 按照时间升序排列
  3452. if (empty($customer['deposit_date'][$v['customer_id']])) {
  3453. $customer['deposit_date'][$v['customer_id']] = [];
  3454. $customer['deposit_date'][$v['customer_id']][] = $v['confirm_date'];
  3455. } else {
  3456. $customer['deposit_date'][$v['customer_id']][] = $v['confirm_date'];
  3457. }
  3458. } elseif (in_array($v['state'], $state7, true)) { //卖卡客户
  3459. $customer['maika_customer'][] = $v['customer_id'];
  3460. }
  3461. }
  3462. // 有跟进客户
  3463. $log_customer_id = []; // 某个员工下的跟踪客户id数组
  3464. //智慧屏讲解次数
  3465. $zhihui = [];
  3466. // 无效
  3467. $wuxiao_visit_log = [];
  3468. foreach ($vislog_list as $k => $v) {
  3469. if (in_array($v['state'], $wuxiao_log_state, true)) {
  3470. $wuxiao_visit_log[] = $v;
  3471. }
  3472. $have_employee = $customer_ids_key[$v['customer_id']] ?? 0;
  3473. if ($have_employee) {
  3474. if (strpos($v['remark'], '讲解智慧屏##') !== false) {
  3475. if (empty($zhihui[$have_employee])) {
  3476. $zhihui[$have_employee] = 1;
  3477. } else {
  3478. $zhihui[$have_employee] += 1;
  3479. }
  3480. }
  3481. }
  3482. if (in_array($v['state'], $wuxiao_log_state, true)) {
  3483. continue;
  3484. }
  3485. if (in_array($v['remark'], ['客户再分配', '资源库分配', '客户转移'], true) || (strpos($v['remark'], '业务员') !== false && strpos($v['remark'], '对客户') !== false && strpos($v['remark'], '进行报备') !== false)) {
  3486. continue;
  3487. }
  3488. if ($have_employee) {
  3489. if (isset($log_customer_id[$have_employee])) {
  3490. $log_customer_id[$have_employee][] = $v['customer_id'];
  3491. } else {
  3492. $log_customer_id[$have_employee] = [];
  3493. $log_customer_id[$have_employee][] = $v['customer_id'];
  3494. }
  3495. }
  3496. }
  3497. foreach ($log_customer_id as $k => $v) {
  3498. $log_customer_id[$k] = array_values(array_unique($v));
  3499. }
  3500. // 外呼数据
  3501. $outCallLog = OutCallLog::where([['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]])->where($date_query)->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,employee_id")->group('employee_id')->select()->toArray();
  3502. $outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog);
  3503. // 手机打电话次数获取
  3504. $outCallMbLog = OutCallMbLog::where([
  3505. ['root_id', '=', $root_id], ['employee_id', 'in', $page_employee]
  3506. ])->where($date_query)->field("count(id) as phone_count,employee_id")->group('employee_id')->select()->toArray();
  3507. $outCallMbLog = array_combine(array_column($outCallMbLog, 'employee_id'), $outCallMbLog);
  3508. // 房屋状态设置项
  3509. //房屋状态是否开启
  3510. $house_status_where = [
  3511. ['root_id', '=', $root_id],
  3512. ['keyname', '=', 'house_status'],
  3513. ['status', '=', 0]
  3514. ];
  3515. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  3516. $house_status_field = [];
  3517. if (!$house_status_per->isEmpty()) {
  3518. $house_status_field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  3519. }
  3520. // 户型是否开启
  3521. $housetype_where = [
  3522. ['root_id', '=', $root_id],
  3523. ['keyname', '=', 'housetype_arrow'],
  3524. ['status', '=', 0]
  3525. ];
  3526. $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
  3527. $housetype_arrow = [];
  3528. if (!$housetype_per->isEmpty()) {
  3529. $housetype_arrow = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
  3530. }
  3531. //分享内容次数
  3532. $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
  3533. $share = ShareLog::where([['employee_id', 'in', $page_employee], ['type', 'in', $share_where]])->where($date_query1)->field('concat(employee_id,type,data_id) as str,employee_id')->select()->toArray();
  3534. $shares = [];
  3535. foreach ($share as $v) {
  3536. if (empty($shares[$v['employee_id']])) {
  3537. $shares[$v['employee_id']] = 1;
  3538. } else {
  3539. $shares[$v['employee_id']] += 1;
  3540. }
  3541. }
  3542. // 培训资料是否看了 课程是否学习过
  3543. $look = TrainCourseView::where([['employee_id', 'in', $page_employee], ['time', '>', 0]])->where($date_query)->group('employee_id')->column('employee_id');
  3544. //考核次数 通过次数
  3545. $don = TrainDoneLog::where([['employee_id', 'in', $page_employee]])->where($date_query)->column('employee_id,done_percent');
  3546. $don1 = $don2 = [];
  3547. foreach ($don as $don_k => $don_v) {
  3548. if (empty($don1[$don_v['employee_id']])) {
  3549. $don1[$don_v['employee_id']] = 1;
  3550. } else {
  3551. $don1[$don_v['employee_id']] += 1;
  3552. }
  3553. if ($don_v['done_percent'] == 100) {
  3554. if (empty($don2[$don_v['employee_id']])) {
  3555. $don2[$don_v['employee_id']] = 1;
  3556. } else {
  3557. $don2[$don_v['employee_id']] += 1;
  3558. }
  3559. }
  3560. }
  3561. //无效客资
  3562. $invalid = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  3563. $invalid = $invalid ? explode(',', $invalid) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  3564. $invalid_customer = [];
  3565. // 按客户来源搜索处理
  3566. $wuxiao_customer_id = array_values(array_unique(array_column($wuxiao_visit_log, 'customer_id')));
  3567. $no_valid_customers = []; //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H 康
  3568. if ($param['source_id']) {
  3569. $wuxiao_source_customer_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['source_id', '=', $param['source_id']]])->column('id');
  3570. foreach ($wuxiao_visit_log as $k => $v) {
  3571. if (in_array($v['customer_id'], $wuxiao_source_customer_id)) {
  3572. $remark = '';
  3573. if (in_array($v['remark'], $invalid)) {
  3574. $remark = $v['remark'];
  3575. } else {
  3576. foreach ($invalid as $i_k => $i_v) {
  3577. if (strpos($v['remark'], $i_v) !== false) {
  3578. $remark = $i_v;
  3579. break;
  3580. }
  3581. }
  3582. }
  3583. if (isset($invalid_customer[$v['employee_id']])) {
  3584. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  3585. } else {
  3586. $invalid_customer[$v['employee_id']] = [];
  3587. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  3588. }
  3589. if (isset($no_valid_customers[$v['employee_id']])) {
  3590. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  3591. } else {
  3592. $no_valid_customers[$v['employee_id']] = [];
  3593. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  3594. }
  3595. }
  3596. }
  3597. } else {
  3598. foreach ($wuxiao_visit_log as $k => $v) {
  3599. $remark = '';
  3600. if (in_array($v['remark'], $invalid)) {
  3601. $remark = $v['remark'];
  3602. } else {
  3603. foreach ($invalid as $i_k => $i_v) {
  3604. if (strpos($v['remark'], $i_v) !== false) {
  3605. $remark = $i_v;
  3606. break;
  3607. }
  3608. }
  3609. }
  3610. if (isset($invalid_customer[$v['employee_id']])) {
  3611. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  3612. } else {
  3613. $invalid_customer[$v['employee_id']] = [];
  3614. $invalid_customer[$v['employee_id']][] = ['remark' => $remark, 'customer_id' => $v['customer_id']];
  3615. }
  3616. if (isset($no_valid_customers[$v['employee_id']])) {
  3617. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  3618. } else {
  3619. $no_valid_customers[$v['employee_id']] = [];
  3620. $no_valid_customers[$v['employee_id']][] = $v['customer_id'];
  3621. }
  3622. }
  3623. }
  3624. // 无效客资来自资源分配
  3625. $wuxiao_customer_res_id = Customer::where([['id', 'in', $wuxiao_customer_id], ['crm_res_id', '>', 0]])->column('id');
  3626. $wuxiao_customer_res_employee = [];
  3627. // 无效客资来自自建
  3628. $wuxiao_customer_build_employee = [];
  3629. foreach ($invalid_customer as $k => $v) {
  3630. $customer_id_v = array_column($v, 'customer_id');
  3631. foreach ($customer_id_v as $vv) {
  3632. if (in_array($vv, $wuxiao_customer_res_id)) {
  3633. $wuxiao_customer_res_employee[$k][] = $vv;
  3634. } else {
  3635. $wuxiao_customer_build_employee[$k][] = $vv;
  3636. }
  3637. }
  3638. }
  3639. $f_invalid = array_flip($invalid);
  3640. //预约记录
  3641. $sub_where = [
  3642. ['CustomersSubscribe.state', '=', 0],
  3643. ['CustomersSubscribe.type', 'in', [1, 3]],
  3644. ['CustomersSubscribe.employee_id', 'in', $page_employee]
  3645. ];
  3646. if ($date_query) $sub_where[] = ['CustomersSubscribe.addtime', 'between', [$param['start_date'], $param['end_date']]]; //日期条件
  3647. $sub1 = $sub2 = []; //1到店 2量房
  3648. $subscribe = Db::view('CustomersSubscribe')
  3649. ->view('Customer', 'id', 'CustomersSubscribe.customer_id=Customer.id')
  3650. ->where($sub_where)
  3651. ->column('CustomersSubscribe.type,CustomersSubscribe.employee_id');
  3652. foreach ($subscribe as $v) {
  3653. $v['type'] == 1 ? @$sub1[$v['employee_id']] += 1 : @$sub2[$v['employee_id']] += 1;
  3654. }
  3655. foreach ($list as $k => $v) {
  3656. //部门名称
  3657. $list[$k]['org_name'] = $v['org']['name'];
  3658. //预计到店数
  3659. $list[$k]['yuji_store'] = isset($sub1[$v['id']]) ? $sub1[$v['id']] : 0;
  3660. //客户id 所有的客户ID 包括资源,自创,无效等
  3661. $all_customer_id = array_column($v['customer'], 'id');
  3662. //数据处理
  3663. $customer_data = $this->dealCustomerData($v['customer'], $sdate, $edate, $customer['measuring_room_time'], $customer['to_the_store_time'], $customer['deposit_date']);
  3664. $cycle_data = $this->avgCycle($v['customer'], $v['visit_log']);
  3665. $v_employee_visit = $employee_visit[$v['id']] ?? [];
  3666. $visit_data = $this->visitDeal($v['visit_log'], $v_employee_visit);
  3667. // 分配资源数量 包含已置入公海的
  3668. $resource_wuxiao_ids = [];
  3669. if (isset($wuxiao_customer_res_employee[$v['id']])) {
  3670. $resource_wuxiao_ids = array_values(array_unique($wuxiao_customer_res_employee[$v['id']]));
  3671. }
  3672. // 由于置为无效之后没有了获取客户时间(employee_time) 所以资源不统计无效
  3673. // $list[$k]['resource_count'] = count(array_unique(array_merge($customer_data['is_resource'], $resource_wuxiao_ids)));
  3674. $resource_count = $fenpei_cus_employee[$v['id']] ?? [];
  3675. $list[$k]['resource_count'] = count($resource_count);
  3676. if (isset($outCallLog[$v['id']])) {
  3677. // 拨打电话数量
  3678. $list[$k]['connecting_count'] = $outCallLog[$v['id']]['phone_count'];
  3679. // 接通数量
  3680. $list[$k]['connecting_capacity'] = $outCallLog[$v['id']]['on_phone_count'];
  3681. // 接通率
  3682. $list[$k]['connecting_capacity_grawth'] = $outCallLog[$v['id']]['phone_count'] == 0 ? '0%' : round($outCallLog[$v['id']]['on_phone_count'] / $outCallLog[$v['id']]['phone_count'] * 100, 1) . '%';
  3683. // 未接通数量
  3684. $list[$k]['connecting_no'] = $outCallLog[$v['id']]['off_phone_count'];
  3685. // 总通话时长(分)
  3686. $list[$k]['phone_time'] = round($outCallLog[$v['id']]['phone_time'] / 60, 1);
  3687. // 平均通话时长(秒)
  3688. $list[$k]['average_duration'] = $outCallLog[$v['id']]['on_phone_count'] == 0 ? 0 : round($outCallLog[$v['id']]['phone_time'] / $outCallLog[$v['id']]['on_phone_count'], 0);
  3689. } else {
  3690. $list[$k]['connecting_count'] = $list[$k]['connecting_capacity'] = $list[$k]['connecting_no'] = $list[$k]['phone_time'] = $list[$k]['average_duration'] = 0;
  3691. $list[$k]['connecting_capacity_grawth'] = '0%';
  3692. }
  3693. // 手机通话
  3694. if (isset($outCallMbLog[$v['id']])) $list[$k]['connecting_count'] += $outCallMbLog[$v['id']]['phone_count'];
  3695. // 客户总量
  3696. $customer_count = $customer_data['customer_count'];
  3697. $list[$k]['following_up_count'] = count($customer_count);
  3698. // 加微客户数量
  3699. $v_wechat_count = $jiav_employee[$v['id']] ?? [];
  3700. $list[$k]['add_wechat_count'] = count($v_wechat_count);
  3701. // 待确认客户数
  3702. $list[$k]['not_sure_count'] = count($customer_data['not_sure_count']);
  3703. // 有效客户数
  3704. if (isset($no_valid_customers[$v['id']])) {
  3705. $list[$k]['valid_count'] = count(array_unique(array_merge($customer_data['valid_count'], $no_valid_customers[$v['id']])));
  3706. } else {
  3707. $list[$k]['valid_count'] = count($customer_data['valid_count']);
  3708. }
  3709. //有效未加微
  3710. $list[$k]['valid_no_wechat'] = count(array_diff($customer_data['valid_count'], $customer_data['wechat_count']));
  3711. // 报备客户数
  3712. //$reported_diff = array_diff($all_customer_id, $customer_data['is_resource']); //现有客户除去资源库来的
  3713. // $reported_quantity = isset($wuxiao_customer_build_employee[$v['id']]) ? array_unique(array_merge($reported_diff, $wuxiao_customer_build_employee[$v['id']])) : $reported_diff;
  3714. $reported_diff = $add_cus_employee[$v['id']] ?? [];
  3715. $list[$k]['reported_quantity'] = count($reported_diff);
  3716. // 加微率
  3717. $list[$k]['add_wechat_count_grawth'] = count($customer_count) == 0 ? '0%' : round(count($v_wechat_count) / count($customer_count) * 100, 2) . '%';
  3718. // 无效客户数
  3719. foreach ($invalid as $invalid_k => $invalid_v) {
  3720. $list[$k]['invalid_customer_' . $invalid_k] = [];
  3721. }
  3722. if (isset($invalid_customer[$v['id']])) {
  3723. $invalid_customer_all = array_unique(array_column($invalid_customer[$v['id']], 'customer_id'));
  3724. $list[$k]['invalid_customer_count'] = count($invalid_customer_all);
  3725. if ($invalid) {
  3726. foreach ($invalid_customer[$v['id']] as $key => $value) {
  3727. if ($value['remark']) {
  3728. $key = $f_invalid[$value['remark']];
  3729. $list[$k]['invalid_customer_' . $key][] = $value['customer_id'];
  3730. }
  3731. }
  3732. }
  3733. } else {
  3734. $invalid_customer_all = [];
  3735. $list[$k]['invalid_customer_count'] = 0;
  3736. }
  3737. foreach ($invalid as $invalid_k => $invalid_v) {
  3738. $list[$k]['invalid_customer_' . $invalid_k] = count(array_unique($list[$k]['invalid_customer_' . $invalid_k]));
  3739. }
  3740. // 线索总量
  3741. $v_customer = !empty($v['customer']) ? array_column($v['customer'], 'id') : [];
  3742. $all_customer = array_unique(array_merge($v_customer, $invalid_customer_all));
  3743. $list[$k]['all_customer_count'] = count($all_customer);
  3744. // 有效率(%)
  3745. $list[$k]['valid_grawth'] = count($all_customer) == 0 ? '0%' : round(count($customer_data['valid_count']) / count($all_customer) * 100, 2) . '%';
  3746. // 未跟进客户数
  3747. if (isset($log_customer_id[$v['id']])) {
  3748. $following_up_count = $log_customer_id[$v['id']];
  3749. } else {
  3750. $following_up_count = [];
  3751. }
  3752. $list[$k]['not_following_up_count'] = count($customer_count) - count(array_intersect($customer_count, $following_up_count)); // 总的减去有跟进的
  3753. // 意向分派客户数
  3754. $list[$k]['yixiang_assigned_count'] = count($customer_data['yixiang_assigned_count']);
  3755. // 到店分派客户数
  3756. $list[$k]['daodian_assigned_count'] = count($customer_data['daodian_assigned_count']);
  3757. // 量房分配客户数
  3758. $list[$k]['measuring_assigned_count'] = count($customer_data['liangfang_assigned_count']);
  3759. // 智慧屏讲解次数
  3760. $list[$k]['number_of_explanations'] = isset($zhihui[$v['id']]) ? $zhihui[$v['id']] : 0;
  3761. // 量房客户数
  3762. $measuring_room_customer = array_intersect($all_customer_id, $customer['measuring_room_customer']); //量房客户ID
  3763. $to_the_store_ids = array_intersect($all_customer_id, $customer['to_the_store']); // 到店客户ID
  3764. $list[$k]['measuring_room_customer'] = count($measuring_room_customer);
  3765. // 量房未到店
  3766. $measuring_room_customer_to_store = array_intersect($measuring_room_customer, $to_the_store_ids); // 量房 和 到店
  3767. $measuring_room_customer_no_to_store = array_diff($measuring_room_customer, $measuring_room_customer_to_store);
  3768. $list[$k]['measuring_room_customer_no_to_store'] = count($measuring_room_customer_no_to_store);
  3769. // 量房已到店
  3770. $list[$k]['measuring_room_customer_to_store'] = count($measuring_room_customer_to_store);
  3771. //平均量房到店周期
  3772. $list[$k]['avg_room_store_zhouqi'] = $cycle_data['room_store_cycle'];
  3773. // 平均量房周期(天)
  3774. $list[$k]['measuring_room_zhouqi'] = $cycle_data['room_cycle'];
  3775. // 量房率
  3776. $list[$k]['measuring_room_grawth'] = count($customer_count) == 0 ? '0%' : round(count($measuring_room_customer) / count($customer_count) * 100, 2) . '%';
  3777. // 到店数 次数
  3778. $to_the_store = 0;
  3779. foreach ($customer['to_the_store'] as $kk => $vv) {
  3780. if (in_array($vv, $all_customer_id)) {
  3781. $to_the_store++;
  3782. }
  3783. }
  3784. //有户型图的客户
  3785. $list[$k]['drawing_date_count'] = count($customer_data['drawing_date']);
  3786. $list[$k]['to_the_store'] = $to_the_store;
  3787. // 一次到店数
  3788. $list[$k]['to_the_store_1'] = $customer_data['strtore_count_0'];
  3789. // 二次到店
  3790. $list[$k]['to_the_store_2'] = $customer_data['strtore_count_1'];
  3791. // 二次到店比率
  3792. $list[$k]['strtore_2v1'] = $customer_data['strtore_count_0'] == 0 ? '0%' : round($customer_data['strtore_count_1'] / $customer_data['strtore_count_0'] * 100, 2) . '%';
  3793. // 三次及以上到店
  3794. $list[$k]['to_the_store_3'] = $customer_data['strtore_count_2'];
  3795. // 平均到店周期(天)
  3796. $list[$k]['strtore_avg_days_1'] = ceil($customer_data['strtore_0'] / 86400); // 一次
  3797. $list[$k]['strtore_avg_days_2'] = ceil($customer_data['strtore_1'] / 86400); // 二次
  3798. $list[$k]['strtore_avg_days_3'] = ceil($customer_data['strtore_2'] / 86400); // 三次
  3799. //一次 二次 三次 到店率
  3800. $list[$k]['strtore_count_1_grawth'] = count($customer_count) == 0 ? '0%' : round($customer_data['strtore_count_0'] / count($customer_count) * 100, 2) . '%';
  3801. $list[$k]['strtore_count_2_grawth'] = count($customer_count) == 0 ? '0%' : round($customer_data['strtore_count_1'] / count($customer_count) * 100, 2) . '%';
  3802. $list[$k]['strtore_count_3_grawth'] = count($customer_count) == 0 ? '0%' : round($customer_data['strtore_count_2'] / count($customer_count) * 100, 2) . '%';
  3803. // 到店客户数
  3804. $list[$k]['strtore_customer_count'] = count($to_the_store_ids);
  3805. // 到店率
  3806. $list[$k]['strtore_customer_grawth'] = count($customer_count) == 0 ? '0%' : round(count($to_the_store_ids) / count($customer_count) * 100, 2) . '%';
  3807. // 签单客户数量
  3808. $deposit_count_ids = array_intersect($all_customer_id, $customer['deposit_customer']);
  3809. $list[$k]['deposit_count'] = count($deposit_count_ids);
  3810. // 签单率
  3811. $list[$k]['deposit_avg'] = count($visit_data) == 0 ? '0%' : round(count($deposit_count_ids) / count($visit_data) * 100, 2) . '%';
  3812. // 平均签单周期
  3813. $list[$k]['deposit_avg_days'] = $cycle_data['dep_cycle'];
  3814. // 转单客户数量
  3815. $sign_customer_ids = array_intersect($all_customer_id, $customer['sign_customer']);
  3816. $list[$k]['sign_count'] = count($sign_customer_ids);
  3817. //一次 二次 三次 到店交定个数
  3818. $list[$k]['strtore_deposit_count_0'] = count(array_unique(array_intersect($customer['deposit_customer'], $customer_data['strtore_customer_0'])));
  3819. $list[$k]['strtore_deposit_count_1'] = count(array_unique(array_intersect($customer['deposit_customer'], $customer_data['strtore_customer_1'])));
  3820. $list[$k]['strtore_deposit_count_2'] = count(array_unique(array_intersect($customer['deposit_customer'], $customer_data['strtore_customer_2'])));
  3821. //一次 二次 三次 到店交定率 数量/有效
  3822. $list[$k]['strtore_deposit_grawth_0'] = count($customer_count) == 0 ? '0%' : round($list[$k]['strtore_deposit_count_0'] / count($customer_count) * 100, 2) . '%';
  3823. $list[$k]['strtore_deposit_grawth_1'] = count($customer_count) == 0 ? '0%' : round($list[$k]['strtore_deposit_count_1'] / count($customer_count) * 100, 2) . '%';
  3824. $list[$k]['strtore_deposit_grawth_2'] = count($customer_count) == 0 ? '0%' : round($list[$k]['strtore_deposit_count_2'] / count($customer_count) * 100, 2) . '%';
  3825. // 卖卡客户数量
  3826. $maika_customer_ids = array_intersect($all_customer_id, $customer['maika_customer']);
  3827. $list[$k]['maika_count'] = count($maika_customer_ids);
  3828. // 转单率
  3829. $list[$k]['sign_count_grawth'] = count($visit_data) == 0 ? '0%' : round(count($sign_customer_ids) / count($visit_data) * 100, 2) . '%';
  3830. // 平均跟进次数
  3831. $list[$k]['avg_visit_count'] = $cycle_data['next'];
  3832. // 平均合同金额(元)
  3833. $list[$k]['signed_money'] = $customer_data['signed_money'];
  3834. // 合同总金额
  3835. $list[$k]['signed_money_all'] = $customer_data['signed_money_all'];
  3836. // 面积
  3837. $list[$k]['square'] = $customer_data['square'];
  3838. $list[$k]['square_0_80'] = $customer_data['square_0_80'];
  3839. $list[$k]['square_80_100'] = $customer_data['square_80_100'];
  3840. $list[$k]['square_100_120'] = $customer_data['square_100_120'];
  3841. $list[$k]['square_120_200'] = $customer_data['square_120_200'];
  3842. $list[$k]['square_200_500'] = $customer_data['square_200_500'];
  3843. $list[$k]['square_500'] = $customer_data['square_500'];
  3844. // 房屋状态
  3845. $list[$k]['house_status'] = 0;
  3846. if ($house_status_field) {
  3847. $house_status = $this->getHouseStatus($customer_data['house_status'], $house_status_field);
  3848. $list[$k] = array_merge($list[$k], $house_status);
  3849. $list[$k]['house_status'] = array_sum($house_status);
  3850. }
  3851. //房屋户型 及 对应的转单率
  3852. if ($housetype_arrow) {
  3853. $housetype_arrow_data = $this->getHousetypeArrow($customer_data['housetype_arrow'], $housetype_arrow, $sign_customer_ids);
  3854. $list[$k] = array_merge($list[$k], $housetype_arrow_data);
  3855. }
  3856. // 房屋类型
  3857. $list[$k]['house_type'] = $customer_data['existing_homes_count'] + $customer_data['forward_housing_count'];
  3858. $list[$k]['existing_homes_count'] = $customer_data['existing_homes_count'];
  3859. $list[$k]['forward_housing_count'] = $customer_data['forward_housing_count'];
  3860. // 调用内容数量
  3861. // 分享内容次数
  3862. $list[$k]['shares_count'] = isset($shares[$v['id']]) ? $shares[$v['id']] : 0;
  3863. // 考核次数
  3864. $list[$k]['assessment_times'] = isset($don1[$v['id']]) ? $don1[$v['id']] : 0;
  3865. // 通过次数
  3866. $list[$k]['number_of_passes'] = isset($don2[$v['id']]) ? $don2[$v['id']] : 0;
  3867. //培训资料是否看了 课程是否学习过
  3868. $list[$k]['look_data'] = in_array($v['id'], $look) ? '是' : '否';
  3869. unset($list[$k]['customer']);
  3870. unset($list[$k]['visit_log']);
  3871. }
  3872. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  3873. }
  3874. /**
  3875. * 处理客户数据
  3876. * $list 业务员的客户数组
  3877. * $sdate 开始时间筛选
  3878. * $edate 结束时间筛选
  3879. * $measuring_room_time 客户量房时间 计算平均量房时间
  3880. * $to_the_store_time 客户到店时间 计算平均到店时间
  3881. * $deposit_customer 客户交定时间 计算平均交定周期
  3882. */
  3883. static function dealCustomerData($list, $sdate, $edate, $measuring_room_time, $to_the_store_time, $deposit_customer)
  3884. {
  3885. $time = time();
  3886. $data['not_sure_count'] = []; // 待确认客户id
  3887. $data['customer_count'] = []; // 客户量 除去资源、无效、死单
  3888. $data['valid_count'] = []; // 有效客户 除去资源、无效、死单、待确认 (客户量的基础上减去待确认的)
  3889. $data['yixiang_assigned_count'] = []; // 意向分配客户数
  3890. $data['daodian_assigned_count'] = []; // 到店分配客户数
  3891. $data['liangfang_assigned_count'] = []; // 量房分配客户数
  3892. $data['wechat_count'] = []; //加微客户id
  3893. $data['is_resource'] = []; //资源库分配客户id
  3894. $data['drawing_date'] = []; //有户型图的客户
  3895. $row['room_cycle_0'] = $row['room_cycle_1'] = $row['room_cycle_2'] = []; //一次 二次 三次量房周期
  3896. $row['strtore_0'] = $row['strtore_1'] = $row['strtore_2'] = []; //一次 二次 三次到店周期
  3897. $row['deposit_0'] = $row['deposit_1'] = $row['deposit_2'] = []; //一次 二次 三次交定周期
  3898. $data['room_cycle_customer_0'] = $data['room_cycle_customer_1'] = $data['room_cycle_customer_2'] = []; //一次 两次 三次量房客户
  3899. $data['strtore_customer_0'] = $data['strtore_customer_1'] = $data['strtore_customer_2'] = []; //一次 两次 三次到店客户
  3900. $data['deposit_customer_0'] = $data['deposit_customer_1'] = $data['deposit_customer_2'] = []; //一次 两次 三次交定客户
  3901. $row['signed_money'] = []; //合同金额
  3902. $house_type = []; //房屋类型
  3903. $housetype_arrow = []; // 房屋户型
  3904. $square = []; //房屋面积
  3905. $data['house_status'] = []; //房屋状态
  3906. //报备客户
  3907. $data['reported_quantity'] = 0;
  3908. //死单客户
  3909. $data['died'] = 0;
  3910. $not_sure_state = Customer::changeState('待确认', 'chaos');
  3911. $invalid_state = Customer::changeState('无效', 'chaos');
  3912. $invalid_not_sure_state = array_values(array_merge($invalid_state, $not_sure_state));
  3913. foreach ($list as $v) {
  3914. // 待确认客户
  3915. if (empty($v['crm_res_id']) && in_array($v['state'], $not_sure_state, true)) {
  3916. $data['not_sure_count'][] = $v['id'];
  3917. }
  3918. //客户添加时间
  3919. $addtime = strtotime($v['addtime']);
  3920. // 客户数量
  3921. if (intval($v['died']) != 2) {
  3922. if ((empty($v['crm_res_id']) && !in_array($v['state'], $invalid_state, true)) || (!empty($v['crm_res_id']) && !in_array($v['state'], $invalid_not_sure_state, true))) {
  3923. $data['customer_count'][] = $v['id'];
  3924. $square[] = $v['square']; //面积
  3925. }
  3926. }
  3927. // 有效客户 一部分数据只是有效时候才做统计
  3928. if (intval($v['died']) != 2 && !in_array($v['state'], $invalid_not_sure_state, true)) {
  3929. $data['valid_count'][] = $v['id'];
  3930. // 房屋类型
  3931. $house_type[] = $v['house_type'];
  3932. $row['signed_money'][] = $v['signed_money']; //合同金额
  3933. $data['house_status'][] = $v['house_status'];
  3934. // 房屋户型
  3935. $ext = json_decode($v['ext'], true);
  3936. if (!empty($ext) && is_array($ext)) {
  3937. $ext = array_filter($ext);
  3938. foreach ($ext as $e) {
  3939. if (isset($e['keyname']) && $e['keyname'] == 'housetype_arrow' && !empty($e['value'])) {
  3940. $housetype_arrow[$v['id']] = $e['value'];
  3941. }
  3942. }
  3943. }
  3944. }
  3945. // 意向分派客户数
  3946. if (!empty($v['assign_type']) && $v['assign_type'] == 'yixiang') {
  3947. $data['yixiang_assigned_count'][] = $v['id'];
  3948. }
  3949. // 到店分派客户数
  3950. if (!empty($v['assign_type']) && $v['assign_type'] == 'daodian') {
  3951. $data['daodian_assigned_count'][] = $v['id'];
  3952. }
  3953. // 量房分派客户数
  3954. if (!empty($v['assign_type']) && $v['assign_type'] == 'liangfang') {
  3955. $data['liangfang_assigned_count'][] = $v['id'];
  3956. }
  3957. //资源库分配数量
  3958. if ($v['crm_res_id'] && strtotime($v['employee_time']) >= $sdate && strtotime($v['employee_time']) <= $edate) {
  3959. $data['is_resource'][] = $v['id'];
  3960. }
  3961. //加微客户,有户型图客户
  3962. $v2 = $v['ext'];
  3963. $ext = json_decode($v2, true);
  3964. if (!empty($ext) && is_array($ext)) {
  3965. $ext = array_filter($ext);
  3966. foreach ($ext as $e) {
  3967. if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value'])) {
  3968. $data['wechat_count'][] = $v['id'];
  3969. } elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) <= $time) {
  3970. $data['drawing_date'][] = $v['id'];
  3971. }
  3972. }
  3973. }
  3974. //一次 二次 三次量房周期
  3975. if (isset($measuring_room_time[$v['id']])) {
  3976. $count = count($measuring_room_time[$v['id']]);
  3977. $start = $count >= 3 ? 2 : $count - 1;
  3978. $measuring_room_cycle = [];
  3979. $measuring_room_cycle[$start] = $measuring_room_time[$v['id']][$start];
  3980. // $measuring_room_cycle = array_slice($measuring_room_time[$v['id']],0, 3);
  3981. foreach ($measuring_room_cycle as $cycle_key => $cycle_item) {
  3982. $cycle_addtime = strtotime($cycle_item);
  3983. if ($addtime < $cycle_addtime) $row['room_cycle_' . $cycle_key][] = $cycle_addtime - $addtime;
  3984. $data['room_cycle_customer_' . $cycle_key][] = $v['id'];
  3985. }
  3986. }
  3987. //一次 二次 三次到店周期
  3988. if (isset($to_the_store_time[$v['id']])) {
  3989. $count = count($to_the_store_time[$v['id']]);
  3990. $start = $count >= 3 ? 2 : $count - 1;
  3991. $to_the_store_times = [];
  3992. $to_the_store_times[$start] = $to_the_store_time[$v['id']][$start];
  3993. // $to_the_store_times = array_slice($to_the_store_time[$v['id']],0, 3);
  3994. foreach ($to_the_store_times as $store_key => $store_item) {
  3995. $store_addtime = strtotime($store_item);
  3996. if ($addtime < $store_addtime) $row['strtore_' . $store_key][] = $store_addtime - $addtime;
  3997. $data['strtore_customer_' . $store_key][] = $v['id'];
  3998. }
  3999. }
  4000. //一次 二次 三次交定周期
  4001. if (isset($deposit_customer[$v['id']])) {
  4002. $count = count($deposit_customer[$v['id']]);
  4003. $start = $count >= 3 ? 2 : $count - 1;
  4004. $deposit_customer_time = [];
  4005. $deposit_customer_time[$start] = $deposit_customer[$v['id']][$start];
  4006. // $deposit_customer_time = array_slice($deposit_customer[$v['id']],0, 3);
  4007. foreach ($deposit_customer_time as $deposit_key => $deposit_item) {
  4008. $deposit_addtime = strtotime($deposit_item);
  4009. if ($addtime < $deposit_item) $row['deposit_' . $deposit_key][] = $deposit_addtime - $addtime;
  4010. $data['deposit_customer_' . $deposit_key][] = $v['id'];
  4011. }
  4012. }
  4013. //死单
  4014. if (intval($v['died']) == 2) $data['died'] += 1;
  4015. }
  4016. $data['housetype_arrow'] = $housetype_arrow;
  4017. foreach ([0, 1, 2] as $range) {
  4018. //平均一次,二次,三次量房周期
  4019. $data['room_cycle_' . $range] = $row['room_cycle_' . $range] ? ceil(array_sum($row['room_cycle_' . $range]) / count($row['room_cycle_' . $range])) : 0;
  4020. //一次,二次,三次 量房人数
  4021. $data['room_cycle_count_' . $range] = count($data['room_cycle_customer_' . $range]);
  4022. //平均到店周期
  4023. $data['strtore_' . $range] = $row['strtore_' . $range] ? ceil(array_sum($row['strtore_' . $range]) / count($row['strtore_' . $range])) : 0;
  4024. //一次 二次 三次 到店人数
  4025. $data['strtore_count_' . $range] = count($data['strtore_customer_' . $range]);
  4026. //平均交定周期
  4027. $data['deposit_' . $range] = $row['deposit_' . $range] ? ceil(array_sum($row['deposit_' . $range]) / count($row['deposit_' . $range])) : 0;
  4028. //一次 二次 三次 交定人数
  4029. $data['deposit_count_' . $range] = count($data['deposit_customer_' . $range]);
  4030. }
  4031. //平均合同金额
  4032. $signed_money = array_filter($row['signed_money']);
  4033. $data['signed_money'] = empty($signed_money) ? 0 : round(array_sum($signed_money) / count($signed_money), 2);
  4034. $data['signed_money_all'] = array_sum($signed_money);
  4035. //房屋类型
  4036. $data['existing_homes_count'] = 0; //现房数量
  4037. $data['forward_housing_count'] = 0; //期房数量
  4038. foreach ($house_type as $house_type_item) {
  4039. if (strpos($house_type_item, '现房') !== false) {
  4040. $data['existing_homes_count'] += 1;
  4041. } elseif (strpos($house_type_item, '期房') !== false) {
  4042. $data['forward_housing_count'] += 1;
  4043. }
  4044. }
  4045. //房屋面积
  4046. $data['square'] = $data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
  4047. foreach ($square as $square_item) {
  4048. if (!$square_item) continue;
  4049. $data['square'] += 1;
  4050. if ($square_item <= 80) {
  4051. $data['square_0_80'] += 1;
  4052. } elseif ($square_item <= 100) {
  4053. $data['square_80_100'] += 1;
  4054. } elseif ($square_item <= 120) {
  4055. $data['square_100_120'] += 1;
  4056. } elseif ($square_item <= 200) {
  4057. $data['square_120_200'] += 1;
  4058. } elseif ($square_item <= 500) {
  4059. $data['square_200_500'] += 1;
  4060. } else {
  4061. $data['square_500'] += 1;
  4062. }
  4063. }
  4064. return $data;
  4065. }
  4066. /**
  4067. * 弹框页面
  4068. */
  4069. public function generalOpen()
  4070. {
  4071. $root_id = request()->employee->root_id;
  4072. $eid = input('eid', '', 'intval');
  4073. View::assign('eid', $eid);
  4074. $type = input('type', '');
  4075. $start_date = input('start_date', '');
  4076. $end_date = input('end_date', '');
  4077. if ($start_date) {
  4078. $ls_date = explode(' - ', $start_date);
  4079. if (count($ls_date) == 2) {
  4080. $start_date = $ls_date[0];
  4081. $end_date = $ls_date[1];
  4082. }
  4083. }
  4084. if ($type == 'assign') {
  4085. //待指派
  4086. $field = [
  4087. ['field' => 'name', 'title' => '姓名'],
  4088. ['field' => 'phone', 'title' => '电话']
  4089. ];
  4090. $url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  4091. } elseif ($type == 'share') {
  4092. //调用内容
  4093. $field = [
  4094. ['field' => 'share_time', 'title' => '分享时间'],
  4095. ['field' => 'title', 'title' => '分享内容']
  4096. ];
  4097. $url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  4098. } elseif ($type == 'yuji_store') {
  4099. //预计到店
  4100. $field = [
  4101. ['field' => 'name', 'title' => '姓名'],
  4102. ['field' => 'date', 'title' => '预约时间']
  4103. ];
  4104. $url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  4105. } elseif ($type == 'number_of_explanations') {
  4106. //智慧屏讲解客户列表
  4107. $field = [
  4108. ['field' => 'name', 'title' => '姓名'],
  4109. ['field' => 'phone', 'title' => '电话'],
  4110. ['field' => 'addtime', 'title' => '讲解时间']
  4111. ];
  4112. $url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  4113. } elseif ($type == 'invalid') {
  4114. //无效客资
  4115. $field = [
  4116. ['field' => 'name', 'title' => '姓名'],
  4117. ['field' => 'phone', 'title' => '电话']
  4118. ];
  4119. $url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date, 'key' => input('key', '')]);
  4120. } else {
  4121. //加微客户
  4122. $field = [
  4123. ['field' => 'name', 'title' => '姓名'],
  4124. ['field' => 'phone', 'title' => '电话']
  4125. ];
  4126. $url = url("statistics/generalOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  4127. }
  4128. View::assign('fields', json_encode($field));
  4129. View::assign('url', $url);
  4130. return View::fetch('add_wechat');
  4131. }
  4132. /**
  4133. * 弹框列表
  4134. */
  4135. public function generalOpenData()
  4136. {
  4137. $param = Request::only(['page' => 1, 'limit' => 10, 'eid' => 0, 'type' => '', 'start_date' => '', 'end_date' => '']);
  4138. $page = $param['page'];
  4139. $limit = $param['limit'];
  4140. $root_id = request()->employee->root_id;
  4141. $where = [
  4142. ['employee_id', '=', $param['eid']]
  4143. ];
  4144. $sdate = strtotime('2015-01-01');
  4145. $edate = time();
  4146. if ($param['start_date'] && $param['end_date']) {
  4147. $sdate = strtotime($param['start_date']);
  4148. $edate = strtotime($param['end_date']);
  4149. }
  4150. $start_date = date('Y-m-d 00:00:00', $sdate);
  4151. $end_date = date('Y-m-d 23:59:59', $edate);
  4152. if ($param['type'] == 'wechat') {
  4153. $where[] = ['add_wechat_time', 'between', [$start_date, $end_date]];
  4154. $wuxiao_state = Customer::changeState('无效', 'chaos');
  4155. $w_where[] = ['add_wechat_time', 'between', [$start_date, $end_date]];
  4156. $w_where[] = ['employee_id', 'null', null];
  4157. $w_where[] = ['state', 'in', $wuxiao_state];
  4158. $ids_wuxiao = Customer::where($w_where)->column('id'); //这是加微的,但是置为无效的客户
  4159. $j_w_where[] = ['customer_id', 'in', $ids_wuxiao];
  4160. $j_w_where[] = ['employee_id', '=', $param['eid']];
  4161. // 无效的加微客户id
  4162. $jiav_wuxiao_log = CustomerInvalidLog::where($j_w_where)->column('customer_id');
  4163. // 所属员工的加微客户id
  4164. $customer_ids = Customer::where($where)->column('id');
  4165. $ids = array_unique(array_merge($customer_ids, $jiav_wuxiao_log));
  4166. $where1[] = ['id', 'in', $ids];
  4167. $where2[] = ['customer_id', 'in', $ids];
  4168. $data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
  4169. $list = $data['list'];
  4170. $count = $data['count'];
  4171. } elseif ($param['type'] == 'drawing') {
  4172. $field = 'drawing_date';
  4173. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4174. $vis_where[] = ['Customer.ext', 'like', '%' . $field . '%'];
  4175. $vis_where[] = ['Customer.died', '<>', 2];
  4176. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4177. $ext = Db::view('CustomerVisitLog', 'customer_id')
  4178. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4179. ->where($vis_where)
  4180. ->group('Customer.id')
  4181. ->column('Customer.id,Customer.ext');
  4182. $cid = [];
  4183. foreach ($ext as $v) {
  4184. $json = json_decode($v['ext'], true);
  4185. foreach ($json as $v2) {
  4186. if (!isset($v2['keyname'])) continue;
  4187. if ($v2['keyname'] == $field && !empty($v2['value']) && strtotime($v2['value']) >= $sdate && strtotime($v2['value']) <= $edate) {
  4188. $cid[] = $v['id'];
  4189. }
  4190. }
  4191. }
  4192. $where[] = ['id', 'in', $cid];
  4193. $count = Customer::where($where)->count();
  4194. $list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
  4195. } elseif ($param['type'] == 'share') {
  4196. //调用内容
  4197. $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
  4198. $wheres = [
  4199. ['employee_id', '=', $param['eid']],
  4200. ['type', 'in', $share_where],
  4201. ['share_time', 'between', [$start_date, $end_date]]
  4202. ];
  4203. $model = ShareLog::with('content')->where($wheres)->order('id desc');
  4204. $count = $model->count();
  4205. $list = $model->page($page, $limit)->select()->toArray();
  4206. $arr = [
  4207. 'Activity' => '#活动#',
  4208. 'Article' => '#图文素材#',
  4209. 'Building' => '#楼盘进度#',
  4210. 'Card' => '#名片#',
  4211. 'CompanyStrength' => '#公司实力#',
  4212. 'Construction' => '#在施工地#',
  4213. 'CustomerJsAll' => '',
  4214. 'MaterialCase' => '#装修案例#',
  4215. 'MaterialEvidence' => '#口碑见证#',
  4216. 'ToolAll' => '#谈单工具#',
  4217. 'Video' => '#视频#',
  4218. 'Weiwang' => '#个人微网#'
  4219. ];
  4220. foreach ($list as $k => $v) {
  4221. $type = isset($arr[$v['type']]) ? $arr[$v['type']] : '';
  4222. $field = $v['type'] == 'Building' ? 'name' : 'title';
  4223. if ($v['content'] && isset($v['content'][$field])) {
  4224. $list[$k]['title'] = $type . '《' . $v['content'][$field] . '》';
  4225. } else {
  4226. $list[$k]['title'] = '暂无';
  4227. }
  4228. }
  4229. } elseif ($param['type'] == 'resource') { //分配
  4230. $where[] = ['crm_res_id', 'not null', null];
  4231. $where[] = ['died', '<>', 2];
  4232. $where[] = ['employee_time', 'between', [$start_date, $end_date]];
  4233. $where[] = ['employee_id', '=', $param['eid']];
  4234. $ids = Customer::where($where)->column('id');
  4235. // 无效客资来自资源分配
  4236. $i_where[] = ['employee_id', '=', $param['eid']];
  4237. $i_where[] = ['cus_employee_time', 'between', [$start_date, $end_date]];
  4238. $wuxiao_ids = CustomerInvalidLog::where($i_where)->column('customer_id');
  4239. $ids = array_merge($ids, $wuxiao_ids);
  4240. $where1[] = ['id', 'in', $ids];
  4241. $where1[] = ['crm_res_id', '>', 0];
  4242. $where2[] = ['customer_id', 'in', $ids];
  4243. $where2[] = ['crm_res_id', '>', 0];
  4244. $data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
  4245. $list = $data['list'];
  4246. $count = $data['count'];
  4247. } elseif ($param['type'] == 'reported_quantity') {
  4248. // 报备客户数
  4249. $where[] = ['crm_res_id', 'null', null];
  4250. $where[] = ['addtime', 'between', [$start_date, $end_date]];
  4251. $where[] = ['employee_id', '=', $param['eid']];
  4252. $where[] = ['state', 'not null', null];
  4253. $count = Customer::where($where)->count();
  4254. $list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
  4255. /* $ids = Customer::where($where)->column('id');
  4256. // 无效客资
  4257. $i_where[] = ['employee_id', '=', $param['eid']];
  4258. $i_where[] = ['cus_addtime', 'between', [$start_date, $end_date]];
  4259. $i_where[] = ['is_resource', '=', 0];
  4260. $wuxiao_ids = CustomerInvalidLog::where($i_where)->column('customer_id');
  4261. $ids = array_merge($ids, $wuxiao_ids);
  4262. $where1[] = ['id', 'in', $ids];
  4263. $where1[] = ['crm_res_id', 'null', null];
  4264. $where2[] = ['customer_id', 'in', $ids];
  4265. $where2[] = ['crm_res_id', 'null', null];
  4266. $data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
  4267. $list = $data['list'];
  4268. $count = $data['count']; */
  4269. } elseif ($param['type'] == 'foolow') { //在跟进
  4270. //客户总数
  4271. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4272. $vis_where[] = ['Customer.died', '<>', 2];
  4273. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4274. $ids = Db::view('CustomerVisitLog', 'customer_id')
  4275. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4276. ->where($vis_where)
  4277. ->where(function ($query) {
  4278. $or1[] = ['Customer.crm_res_id', 'NULL', null];
  4279. $or1[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
  4280. $or2[] = ['Customer.crm_res_id', 'NOT NULL', null];
  4281. $or2[] = ['Customer.state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
  4282. $query->whereOr([$or1, $or2]);
  4283. })
  4284. ->group('CustomerVisitLog.customer_id')
  4285. ->column('CustomerVisitLog.customer_id');
  4286. $where[] = ['id', 'in', $ids];
  4287. $count = Customer::where($where)->count();
  4288. $list = Customer::where($where)->field('id,name,phone')->page($page, $limit)->select()->toArray();
  4289. } elseif (in_array($param['type'], ['dep', 'sign_count', 'room'])) {
  4290. //量房,到店,签单,转单客户数
  4291. $all_state_type = ['room' => '已量房', 'dep' => '已交定', 'sign_count' => '已签单', 'invalid_customer_count' => '无效'];
  4292. if (empty($all_state_type[$param['type']])) return json(['code' => 1, 'data' => '', 'count' => 0, 'msg' => '参数错误']);
  4293. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4294. switch ($param['type']) {
  4295. case 'room':
  4296. $state = CustomerVisitLog::changeState('已量房', 'chaos');
  4297. break;
  4298. case 'dep':
  4299. $state = CustomerVisitLog::changeState('已交定', 'chaos');
  4300. break;
  4301. case 'sign_count':
  4302. $state = CustomerVisitLog::changeState('已签单', 'chaos');
  4303. break;
  4304. break;
  4305. default:
  4306. $state = '';
  4307. break;
  4308. }
  4309. if ($state) {
  4310. $db_where[] = ['CustomerVisitLog.state', 'in', $state];
  4311. }
  4312. $db_where[] = ['Customer.employee_id', '=', $param['eid']];
  4313. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  4314. $cids = Db::view('Customer', 'id')
  4315. ->view('CustomerVisitLog', [], 'Customer.id=CustomerVisitLog.customer_id')
  4316. ->where($db_where)
  4317. ->group('Customer.id')
  4318. ->column('Customer.id');
  4319. $count = Customer::where([['id', 'in', $cids]])->count();
  4320. $list = Customer::where([['id', 'in', $cids]])->field('name,phone')->page($page, $limit)->select()->toArray();
  4321. } elseif ($param['type'] == 'valid') {
  4322. //有效客户数量
  4323. $state1 = Customer::changeState('待确认', 'chaos');
  4324. $state2 = Customer::changeState('无效', 'chaos');
  4325. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4326. $vis_where[] = ['Customer.state', 'not in', array_merge($state1, $state2)];
  4327. $vis_where[] = ['Customer.died', '<>', 2];
  4328. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4329. $ids = Db::view('CustomerVisitLog', 'customer_id')
  4330. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4331. ->where($vis_where)
  4332. ->group('CustomerVisitLog.customer_id')
  4333. ->column('CustomerVisitLog.customer_id');
  4334. //2023-03-18 3.3客户标记无效后不能影响数据统计内的有效数据、线索数据 https://kdocs.cn/l/cfx92KlU838H
  4335. $no_where[] = ['employee_id', '=', $param['eid']];
  4336. $no_where[] = ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')];
  4337. $no_where[] = ['addtime', 'between', [$start_date, $end_date]];
  4338. $no_valid_customer = CustomerVisitLog::where($no_where)->group('customer_id')->column('customer_id');
  4339. $ids = array_unique(array_merge($ids, $no_valid_customer));
  4340. $where1[] = ['id', 'in', $ids];
  4341. $where2[] = ['customer_id', 'in', $ids];
  4342. $data = $this->selCustomerValid($where1, $where2, $page, $limit, []);
  4343. $list = $data['list'];
  4344. $count = $data['count'];
  4345. } elseif ($param['type'] == 'valid_no_wechat') {
  4346. //有效客户数量
  4347. $state1 = Customer::changeState('待确认', 'chaos');
  4348. $state2 = Customer::changeState('无效', 'chaos');
  4349. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4350. $vis_where[] = ['Customer.state', 'not in', array_merge($state1, $state2)];
  4351. $vis_where[] = ['Customer.died', '<>', 2];
  4352. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4353. $list = Db::view('CustomerVisitLog', 'customer_id')
  4354. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4355. ->where($vis_where)
  4356. ->group('Customer.id')
  4357. ->column('Customer.id,Customer.ext');
  4358. $cid = [];
  4359. foreach ($list as $k => $v) {
  4360. if ($v['ext']) {
  4361. $json = json_decode($v['ext'], true);
  4362. $i = 0;
  4363. foreach ($json as $v2) {
  4364. if (!isset($v2['keyname'])) continue;
  4365. if ($v2['keyname'] == 'add_wechat_time' && !empty($v2['value'])) {
  4366. $i++;
  4367. continue;
  4368. }
  4369. }
  4370. if ($i == 0) $cid[] = $v['id'];
  4371. } else {
  4372. $cid[] = $v['id'];
  4373. }
  4374. }
  4375. $count = Customer::where([['id', 'in', $cid]])->count();
  4376. $list = Customer::where([['id', 'in', $cid]])->field('name,phone')->page($page, $limit)->select()->toArray();
  4377. } elseif ($param['type'] == 'room_no_store') {
  4378. //量房未到店
  4379. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  4380. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  4381. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4382. $db_where[] = ['Customer.employee_id', '=', $param['eid']];
  4383. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  4384. $db_where1 = $db_where;
  4385. $db_where1[] = ['CustomerVisitLog.state', 'in', $state3];
  4386. $cids1 = Db::view('Customer')
  4387. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  4388. ->where($db_where1)
  4389. ->group('Customer.id')
  4390. ->column('Customer.id');
  4391. $db_where2 = $db_where;
  4392. $db_where2[] = ['CustomerVisitLog.state', 'in', $state4];
  4393. $cids2 = Db::view('Customer')
  4394. ->view('CustomerVisitLog', '', 'Customer.id=CustomerVisitLog.customer_id')
  4395. ->where($db_where2)
  4396. ->group('Customer.id')
  4397. ->column('Customer.id');
  4398. $cids = array_diff($cids1, $cids2);
  4399. $count = Customer::where([['id', 'in', $cids]])->count();
  4400. $list = Customer::where([['id', 'in', $cids]])->field('name,phone')->page($page, $limit)->select()->toArray();
  4401. } elseif ($param['type'] == 'yuji_store') {
  4402. //预计到店
  4403. $sub_where[] = ['CustomersSubscribe.employee_id', '=', $param['eid']];
  4404. $sub_where[] = ['CustomersSubscribe.type', '=', 1];
  4405. $sub_where[] = ['CustomersSubscribe.state', '=', 0];
  4406. $sub_where[] = ['CustomersSubscribe.addtime', 'between', [$start_date, $end_date]];
  4407. $count = Db::view('CustomersSubscribe')
  4408. ->view('Customer', 'id', 'CustomersSubscribe.customer_id=Customer.id')
  4409. ->where($sub_where)->count();
  4410. $list = Db::view('CustomersSubscribe')
  4411. ->view('Customer', 'id', 'CustomersSubscribe.customer_id=Customer.id')
  4412. ->where($sub_where)
  4413. ->page($page, $limit)
  4414. ->column('CustomersSubscribe.customer_id,CustomersSubscribe.subscribe_date date,customer.name');
  4415. } elseif (in_array($param['type'], ['strtore_count_0', 'strtore_count_1', 'strtore_count_2'])) {
  4416. $arr = ['strtore_count_0' => 'count(CustomerVisitLog.id)=1', 'strtore_count_1' => 'count(CustomerVisitLog.id)=2', 'strtore_count_2' => 'count(CustomerVisitLog.id)>=3'];
  4417. $state3 = CustomerVisitLog::changeState('已到店', 'chaos');
  4418. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4419. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4420. $vis_where[] = ['CustomerVisitLog.state', 'in', $state3];
  4421. $vislog_list = Db::view('CustomerVisitLog', 'customer_id')
  4422. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4423. ->where($vis_where)
  4424. ->where([['CustomerVisitLog.is_merge', '=', 0]])
  4425. ->group('CustomerVisitLog.customer_id')
  4426. ->having($arr[$param['type']])
  4427. ->column('CustomerVisitLog.customer_id');
  4428. $where[] = ['id', 'in', $vislog_list];
  4429. $count = Customer::where($where)->count();
  4430. $list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
  4431. } elseif (in_array($param['type'], ['strtore_deposit_count_0', 'strtore_deposit_count_1', 'strtore_deposit_count_2'])) {
  4432. //到店签单
  4433. $arr = ['strtore_deposit_count_0' => 'count(CustomerVisitLog.id)=1', 'strtore_deposit_count_1' => 'count(CustomerVisitLog.id)=2', 'strtore_deposit_count_2' => 'count(CustomerVisitLog.id)>=3'];
  4434. $state3 = CustomerVisitLog::changeState('已到店', 'chaos');
  4435. $vis_where1[] = ['Customer.employee_id', '=', $param['eid']];
  4436. $vis_where1[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4437. $vis_where1[] = ['CustomerVisitLog.state', 'in', $state3];
  4438. $customer_ids = Db::view('CustomerVisitLog', 'customer_id')
  4439. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4440. ->where($vis_where1)
  4441. ->where([['CustomerVisitLog.is_merge', '=', 0]])
  4442. ->group('CustomerVisitLog.customer_id')
  4443. ->having($arr[$param['type']])
  4444. ->column('CustomerVisitLog.customer_id');
  4445. $state2 = CustomerVisitLog::changeState('已交定', 'chaos');
  4446. $vis_where2[] = ['Customer.employee_id', '=', $param['eid']];
  4447. $vis_where2[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4448. $vis_where2[] = ['CustomerVisitLog.state', 'in', $state2];
  4449. $customer_ids_ding = Db::view('CustomerVisitLog', 'customer_id')
  4450. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4451. ->where($vis_where2)
  4452. ->where([['CustomerVisitLog.is_merge', '=', 0]])
  4453. ->group('CustomerVisitLog.customer_id')
  4454. ->column('CustomerVisitLog.customer_id');
  4455. $where[] = ['id', 'in', array_intersect($customer_ids, $customer_ids_ding)];
  4456. $count = Customer::where($where)->count();
  4457. $list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
  4458. } elseif (in_array($param['type'], ['square_0_80', 'square_80_100', 'square_100_120', 'square_120_200', 'square_200_500', 'square_500'])) {
  4459. $arr = [
  4460. 'square_0_80' => [0, 80],
  4461. 'square_80_100' => [80, 100],
  4462. 'square_100_120' => [101, 120],
  4463. 'square_120_200' => [121, 200],
  4464. 'square_200_500' => [201, 500],
  4465. 'square_500' => [501, 999999],
  4466. ];
  4467. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4468. $vis_where[] = ['Customer.died', '<>', 2];
  4469. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4470. $square = Db::view('CustomerVisitLog', 'customer_id')
  4471. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4472. ->where($vis_where)
  4473. ->where(function ($query) {
  4474. $or1[] = ['Customer.crm_res_id', 'NULL', null];
  4475. $or1[] = ['Customer.state', 'not in', Customer::changeState('无效', 'chaos')];
  4476. $or2[] = ['Customer.crm_res_id', 'NOT NULL', null];
  4477. $or2[] = ['Customer.state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
  4478. $query->whereOr([$or1, $or2]);
  4479. })
  4480. ->group('Customer.id')
  4481. ->column('Customer.id,Customer.square');
  4482. $cid = [];
  4483. foreach ($square as $v) {
  4484. if ($v['square'] && $v['square'] > $arr[$param['type']][0] && $v['square'] <= $arr[$param['type']][1]) $cid[] = $v['id'];
  4485. }
  4486. $where[] = ['id', 'in', $cid];
  4487. $count = Customer::where($where)->count();
  4488. $list = Customer::where($where)->field('name,phone')->page($page, $limit)->select()->toArray();
  4489. } elseif ($param['type'] == 'existing_homes_count' || $param['type'] == 'forward_housing_count') {
  4490. //现房 期房客户
  4491. $field = $param['type'] == 'existing_homes_count' ? '现房' : '期房';
  4492. $vis_where[] = ['Customer.employee_id', '=', $param['eid']];
  4493. $vis_where[] = ['Customer.died', '<>', 2];
  4494. $vis_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_date, $end_date]];
  4495. $vis_where[] = ['Customer.house_type', 'like', '%' . $field . '%'];
  4496. $vis_where[] = ['state', 'not in', array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', 'chaos'))];
  4497. $ids = Db::view('CustomerVisitLog', 'customer_id')
  4498. ->view('Customer', [], 'CustomerVisitLog.customer_id=Customer.id')
  4499. ->where($vis_where)
  4500. ->group('Customer.id')
  4501. ->column('Customer.id');
  4502. $where[] = ['id', 'in', $ids];
  4503. $model = Customer::where($where)->field('name,phone');
  4504. $count = $model->count();
  4505. $list = $model->page($page, $limit)->select()->toArray();
  4506. } elseif ($param['type'] == 'number_of_explanations') {
  4507. $db_where[] = ['CustomerVisitLog.remark', 'like', '%讲解智慧屏##%'];
  4508. $db_where[] = ['CustomerVisitLog.addtime', 'between', [$start_date, $end_date]];
  4509. $db_where[] = ['Customer.employee_id', '=', $param['eid']];
  4510. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  4511. $model = Db::view('Customer', ['name', 'phone'])
  4512. ->view('CustomerVisitLog', 'customer_id,addtime', 'Customer.id=CustomerVisitLog.customer_id')
  4513. ->where($db_where);
  4514. $count = $model->count();
  4515. $list = $model->page($page, $limit)->select()->toArray();
  4516. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  4517. foreach ($list as $k => $v) {
  4518. $list[$k]['phone'] = $aec->decrypt($v['phone']);
  4519. }
  4520. } elseif ($param['type'] == 'invalid') {
  4521. $key = input('key', '');
  4522. if ($key === '') {
  4523. return json(['code' => 0, 'data' => [], 'count' => [], 'msg' => '获取成功']);
  4524. }
  4525. $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'clueTag']])->value('content');
  4526. if (empty($setting)) return json(['code' => 0, 'data' => [], 'count' => [], 'msg' => '获取成功']);
  4527. $setting = explode(',', $setting)[$key];
  4528. $where[] = ['employee_id', '=', $param['eid']];
  4529. $where[] = ['confirm_date', 'between', [$start_date, $end_date]];
  4530. $where[] = ['state', 'in', CustomerVisitLog::changeState('无效', 'chaos')];
  4531. $where[] = ['remark', 'like', '%' . $setting . '%'];
  4532. $customer_ids = CustomerVisitLog::where($where)->group('customer_id')->column('customer_id');
  4533. $count = Customer::where([['id', 'in', $customer_ids]])->count();
  4534. $list = Customer::where([['id', 'in', $customer_ids]])->field('name,phone')->page($page, $limit)->select()->toArray();
  4535. }
  4536. if ($param['type'] == 'share') {
  4537. foreach ($list as $k => $v) {
  4538. $type = isset($arr[$v['type']]) ? $arr[$v['type']] : '';
  4539. $field = $v['type'] == 'Building' ? 'name' : 'title';
  4540. if ($v['content'] && isset($v['content'][$field])) {
  4541. $list[$k]['title'] = $type . '《' . $v['content'][$field] . '》';
  4542. } else {
  4543. $list[$k]['title'] = '暂无';
  4544. }
  4545. }
  4546. } elseif ($param['type'] == 'yuji_store') {
  4547. /* foreach ($list as $k => $v) {
  4548. $list[$k]['name'] = isset($customer_ids[$v['customer_id']]) ? $customer_ids[$v['customer_id']] : '未知';
  4549. } */
  4550. } else {
  4551. foreach ($list as $k => $v) {
  4552. $list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
  4553. $list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
  4554. }
  4555. }
  4556. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  4557. }
  4558. /**
  4559. * 有效客户联查(包含回收站)
  4560. */
  4561. public function selCustomerValid($condition, $con_union, $page, $limit, $order)
  4562. {
  4563. $prefix = env('database.prefix');
  4564. $model = Db::table($prefix . 'customer')
  4565. ->field('id,designer_id,employee_id,name,community_name,phone,state,addtime,phone1,phone2,remark,ext,deposit_money,signed_money,org_id')
  4566. ->where($condition)
  4567. ->union(function ($query) use ($con_union) {
  4568. $prefix = env('database.prefix');
  4569. $query->field('customer_id id,designer_id,employee_id,name,community_name,phone,state,addtime,phone1,phone2,remark,ext,deposit_money,signed_money,org_id')->where($con_union)->table($prefix . 'customer_recycle');
  4570. });
  4571. $count = count($model->select()->toArray());
  4572. $list = $model->page($page, $limit)->order($order)->select()->toArray();
  4573. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  4574. foreach ($list as $k => $v) {
  4575. $list[$k]['state'] = Customer::changeState($v['state'], 'state');
  4576. $phone = $aec->decrypt($v['phone']);
  4577. $phone1 = !empty($v['phone1']) ? $aec->decrypt($v['phone1']) : '';
  4578. $phone2 = !empty($v['phone2']) ? $aec->decrypt($v['phone2']) : '';
  4579. $list[$k]['phone'] = substr_replace($phone, '******', 3, 6);
  4580. $list[$k]['phone1'] = $phone1 ? substr_replace($phone1, '******', 3, 6) : '';
  4581. $list[$k]['phone2'] = $phone2 ? substr_replace($phone2, '******', 3, 6) : '';
  4582. $employee = Employee::find($v['employee_id']);
  4583. $list[$k]['employee_name'] = '';
  4584. if (!empty($employee)) {
  4585. $list[$k]['employee_name'] = $employee['name'];
  4586. }
  4587. $designer = Employee::find($v['designer_id']);
  4588. $list[$k]['designer_name'] = '';
  4589. if (!empty($designer)) {
  4590. $list[$k]['designer_name'] = $designer['name'];
  4591. }
  4592. $list[$k]['org_name'] = '';
  4593. if ($v['org_id']) {
  4594. $org = Org::where('id', '=', $v['org_id'])->find();
  4595. if (!empty($org)) {
  4596. $list[$k]['org_name'] = $org['name'];
  4597. }
  4598. }
  4599. }
  4600. $data = ['count' => $count, 'list' => $list];
  4601. return $data;
  4602. }
  4603. /**
  4604. * 房屋状态
  4605. */
  4606. private function getHouseStatus($house_status, $field)
  4607. {
  4608. $data = [];
  4609. $name = array_column($field, 'name');
  4610. $house = [];
  4611. foreach ($house_status as $v) {
  4612. @$house[$v] += 1;
  4613. }
  4614. foreach ($field as $k2 => $v2) {
  4615. $data['house_status_' . $v2['id']] = isset($house[$v2['name']]) ? $house[$v2['name']] : 0;
  4616. }
  4617. return $data;
  4618. }
  4619. /**
  4620. * 房屋户型
  4621. */
  4622. private function getHousetypeArrow($housetype, $field, $sign_ids)
  4623. {
  4624. $data = [];
  4625. $house = [];
  4626. foreach ($housetype as $k => $v) {
  4627. @$house[$v][] = $k; // $v 是户型id,$k是客户id
  4628. }
  4629. // 每个户型的成交率
  4630. foreach ($field as $k => $v) {
  4631. $v_customer = $house[$v['id']] ?? [];
  4632. $v_sign = array_intersect($v_customer, $sign_ids);
  4633. $data['housetype_arrow_lv_' . $v['id']] = count($v_customer) > 0 ? round(count($v_sign) / count($v_customer) * 100, 2) . '%' : '0%';
  4634. }
  4635. // 每个户型的客户数
  4636. foreach ($field as $k => $v) {
  4637. $data['housetype_arrow_' . $v['id']] = isset($house[$v['id']]) ? count($house[$v['id']]) : 0;
  4638. }
  4639. return $data;
  4640. }
  4641. /**
  4642. * 加微数量
  4643. * $wechat 业务员的客户数组
  4644. * $sdate 开始时间筛选
  4645. * $edate 结束时间筛选
  4646. * $measuring_room_time 客户量房时间 计算平均量房时间
  4647. * $to_the_store_time 客户到店时间 计算平均到店时间
  4648. * $deposit_customer 客户交定时间 计算平均交定周期
  4649. */
  4650. private function getWechatCount($wechat, $sdate, $edate, $measuring_room_time, $to_the_store_time, $deposit_customer)
  4651. {
  4652. $time = time();
  4653. $data['wechat_count'] = []; //加微客户id
  4654. $data['is_resource'] = []; //资源库分配客户id
  4655. $data['drawing_date'] = []; //有户型图的客户
  4656. $row['room_cycle_0'] = $row['room_cycle_1'] = $row['room_cycle_2'] = []; //一次 二次 三次量房周期
  4657. $row['strtore_0'] = $row['strtore_1'] = $row['strtore_2'] = []; //一次 二次 三次到店周期
  4658. $row['deposit_0'] = $row['deposit_1'] = $row['deposit_2'] = []; //一次 二次 三次交定周期
  4659. $data['room_cycle_customer_0'] = $data['room_cycle_customer_1'] = $data['room_cycle_customer_2'] = []; //一次 两次 三次量房客户
  4660. $data['strtore_customer_0'] = $data['strtore_customer_1'] = $data['strtore_customer_2'] = []; //一次 两次 三次到店客户
  4661. $data['deposit_customer_0'] = $data['deposit_customer_1'] = $data['deposit_customer_2'] = []; //一次 两次 三次交定客户
  4662. $row['signed_money'] = []; //合同金额
  4663. $house_type = []; //房屋类型
  4664. $square = []; //房屋面积
  4665. $data['house_status'] = []; //房屋状态
  4666. //报备客户
  4667. $data['reported_quantity'] = 0;
  4668. //死单客户
  4669. $data['dead_order'] = 0;
  4670. foreach ($wechat as $v) {
  4671. //客户添加时间
  4672. $addtime = strtotime($v['addtime']);
  4673. //资源库分配数量
  4674. if ($v['crm_res_id']) $data['is_resource'][] = $v['id'];
  4675. $row['signed_money'][] = $v['signed_money']; //合同金额
  4676. $house_type[] = $v['house_type'];
  4677. $square[] = $v['square'];
  4678. $data['house_status'][] = $v['house_status'];
  4679. //加微客户,有户型图客户
  4680. $v2 = $v['ext'];
  4681. $ext = json_decode($v2, true);
  4682. if (!empty($ext) && is_array($ext)) {
  4683. $ext = array_filter($ext);
  4684. foreach ($ext as $e) {
  4685. if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
  4686. $data['wechat_count'][] = $v['id'];
  4687. } elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) <= $time) {
  4688. $data['drawing_date'][] = $v['id'];
  4689. }
  4690. }
  4691. }
  4692. //一次 二次 三次量房周期
  4693. if (isset($measuring_room_time[$v['id']])) {
  4694. $count = count($measuring_room_time[$v['id']]);
  4695. $start = $count >= 3 ? 2 : $count - 1;
  4696. $measuring_room_cycle = [];
  4697. $measuring_room_cycle[$start] = $measuring_room_time[$v['id']][$start];
  4698. // $measuring_room_cycle = array_slice($measuring_room_time[$v['id']],0, 3);
  4699. foreach ($measuring_room_cycle as $cycle_key => $cycle_item) {
  4700. $cycle_addtime = strtotime($cycle_item);
  4701. if ($addtime < $cycle_addtime) $row['room_cycle_' . $cycle_key][] = $cycle_addtime - $addtime;
  4702. $data['room_cycle_customer_' . $cycle_key][] = $v['id'];
  4703. }
  4704. }
  4705. //一次 二次 三次到店周期
  4706. if (isset($to_the_store_time[$v['id']])) {
  4707. $count = count($to_the_store_time[$v['id']]);
  4708. $start = $count >= 3 ? 2 : $count - 1;
  4709. $to_the_store_times = [];
  4710. $to_the_store_times[$start] = $to_the_store_time[$v['id']][$start];
  4711. // $to_the_store_times = array_slice($to_the_store_time[$v['id']],0, 3);
  4712. foreach ($to_the_store_times as $store_key => $store_item) {
  4713. $store_addtime = strtotime($store_item);
  4714. if ($addtime < $store_addtime) $row['strtore_' . $store_key][] = $store_addtime - $addtime;
  4715. $data['strtore_customer_' . $store_key][] = $v['id'];
  4716. }
  4717. }
  4718. //一次 二次 三次交定周期
  4719. if (isset($deposit_customer[$v['id']])) {
  4720. $count = count($deposit_customer[$v['id']]);
  4721. $start = $count >= 3 ? 2 : $count - 1;
  4722. $deposit_customer_time = [];
  4723. $deposit_customer_time[$start] = $deposit_customer[$v['id']][$start];
  4724. // $deposit_customer_time = array_slice($deposit_customer[$v['id']],0, 3);
  4725. foreach ($deposit_customer_time as $deposit_key => $deposit_item) {
  4726. $deposit_addtime = strtotime($deposit_item);
  4727. if ($addtime < $deposit_item) $row['deposit_' . $deposit_key][] = $deposit_addtime - $addtime;
  4728. $data['deposit_customer_' . $deposit_key][] = $v['id'];
  4729. }
  4730. }
  4731. //报备客户
  4732. // if () {
  4733. // # code...
  4734. // }
  4735. //死单
  4736. if ($v['dead_order'] == 2) $data['dead_order'] += 1;
  4737. }
  4738. foreach ([0, 1, 2] as $range) {
  4739. //平均一次,二次,三次量房周期
  4740. $data['room_cycle_' . $range] = $row['room_cycle_' . $range] ? ceil(array_sum($row['room_cycle_' . $range]) / count($row['room_cycle_' . $range])) : 0;
  4741. //一次,二次,三次 量房人数
  4742. $data['room_cycle_count_' . $range] = count($row['room_cycle_' . $range]);
  4743. //平均到店周期
  4744. $data['strtore_' . $range] = $row['strtore_' . $range] ? ceil(array_sum($row['strtore_' . $range]) / count($row['strtore_' . $range])) : 0;
  4745. //一次 二次 三次 到店人数
  4746. $data['strtore_count_' . $range] = count($row['strtore_' . $range]);
  4747. //平均交定周期
  4748. $data['deposit_' . $range] = $row['deposit_' . $range] ? ceil(array_sum($row['deposit_' . $range]) / count($row['deposit_' . $range])) : 0;
  4749. //一次 二次 三次 交定人数
  4750. $data['deposit_count_' . $range] = count($row['deposit_' . $range]);
  4751. }
  4752. //平均合同金额
  4753. $signed_money = array_filter($row['signed_money']);
  4754. $data['signed_money'] = empty($signed_money) ? 0 : round(array_sum($signed_money) / count($signed_money), 2);
  4755. //房屋类型
  4756. $data['existing_homes_count'] = 0; //现房数量
  4757. $data['forward_housing_count'] = 0; //期房数量
  4758. foreach ($house_type as $house_type_item) {
  4759. if (strpos($house_type_item, '现房') !== false) {
  4760. $data['existing_homes_count'] += 1;
  4761. } elseif (strpos($house_type_item, '期房') !== false) {
  4762. $data['forward_housing_count'] += 1;
  4763. }
  4764. }
  4765. //房屋面积
  4766. $data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
  4767. foreach ($square as $square_item) {
  4768. if (!$square_item) continue;
  4769. if ($square_item <= 80) {
  4770. $data['square_0_80'] += 1;
  4771. } elseif ($square_item <= 100) {
  4772. $data['square_80_100'] += 1;
  4773. } elseif ($square_item <= 120) {
  4774. $data['square_100_120'] += 1;
  4775. } elseif ($square_item <= 200) {
  4776. $data['square_120_200'] += 1;
  4777. } elseif ($square_item <= 500) {
  4778. $data['square_200_500'] += 1;
  4779. } else {
  4780. $data['square_500'] += 1;
  4781. }
  4782. }
  4783. return $data;
  4784. }
  4785. /**
  4786. * 行为统计页面
  4787. */
  4788. public function behaviorStatistics()
  4789. {
  4790. //返回组织列表
  4791. $org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
  4792. View::assign('org', $org);
  4793. return View::fetch();
  4794. }
  4795. /**
  4796. * 行为统计接口
  4797. * 日期截止到 2022011-20
  4798. */
  4799. public function behaviorStatisticsData($page = 1, $limit = 10)
  4800. {
  4801. $param = Request::only(['page' => 1, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']);
  4802. $root_id = request()->employee->root_id;
  4803. if ($param['start_date']) {
  4804. $count_dates = explode(' - ', $param['start_date']);
  4805. $start_date = strtotime($count_dates[0]);
  4806. $end_date = strtotime($count_dates[1]);
  4807. } else {
  4808. $start_date = strtotime('2022-11-20');
  4809. $end_date = strtotime(date('Y-m-d'));
  4810. }
  4811. //登录记录
  4812. $login_where = [
  4813. ['root_id', '=', $root_id],
  4814. ['content', '=', '小程序登录'],
  4815. ['addtime', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]]
  4816. ];
  4817. $login = OperateLog::where($login_where)->field("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
  4818. $login = array_column($login, 'indexx');
  4819. if ($param['keyword']) {
  4820. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  4821. }
  4822. $where[] = ['root_id', '=', $root_id];
  4823. $where[] = ['uid', '>', 0];
  4824. $where[] = ['state', 'in', ['在职', '离职']];
  4825. if (!empty($param['org_id'])) {
  4826. //部门筛选
  4827. $org = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
  4828. $orgIds = Org::where([['path', 'like', $org['path'] . '%']])->column('id');
  4829. $where[] = ['org_id', 'in', $orgIds];
  4830. }
  4831. $employees = Employee::where($where)->column('id,name,org_id,addtime,state,termination_time');
  4832. $day = ($end_date - $start_date) / 86400;
  4833. $list = [];
  4834. for ($i = 0; $i <= $day; $i++) {
  4835. $date = date('Y-m-d', $end_date - $i * 86400);
  4836. $time = strtotime($date) + 86399;
  4837. foreach ($employees as $k => $v) {
  4838. $addtime = strtotime($v['addtime']);
  4839. $now_date = date('Y-m-d', strtotime($date));
  4840. $indexx = $v['id'] . '@' . $now_date;
  4841. $termination_time = strtotime(date('Y-m-d 23:59:59', strtotime($v['termination_time'])));
  4842. // if ($time >= $addtime && in_array($indexx,$login)) {
  4843. $status_state = $v['state'] == '离职' ? ($termination_time >= $time ? true : false) : true;
  4844. if ($time >= $addtime && $status_state) {
  4845. $j = $v;
  4846. $j['x_date'] = date('Y年m月d日', strtotime($date));
  4847. $j['date'] = $now_date;
  4848. $j['index'] = $indexx;
  4849. $list[] = $j;
  4850. }
  4851. }
  4852. }
  4853. $count = count($list);
  4854. $list = array_slice($list, $limit * ($page - 1), $limit);
  4855. // $dates = array_unique(array_column($list,'date'));
  4856. $eids = array_unique(array_column($list, 'id'));
  4857. $query1 = ['employee_id', 'in', $eids];
  4858. $query2 = ['share_time', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
  4859. $query3 = ['addtime', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
  4860. $custoemr_where = [
  4861. ['employee_id', 'in', $eids]
  4862. ];
  4863. $customer = Customer::where($custoemr_where)->column('id,ext,employee_id,house_delivery_time');
  4864. //加微客户 预计转施工时间
  4865. $add_wechat_date = [];
  4866. $house_delivery_time = [];
  4867. foreach ($customer as $v1) {
  4868. $v2 = $v1['ext'];
  4869. $ext = json_decode($v2, true);
  4870. if (!empty($ext) && is_array($ext)) {
  4871. $ext = array_filter($ext);
  4872. foreach ($ext as $e) {
  4873. if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value'])) {
  4874. @$add_wechat_date[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
  4875. } elseif (isset($e['keyname']) && $e['keyname'] == 'will_transfer_to_construction_date' && !empty($e['value'])) {
  4876. @$house_delivery_time[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
  4877. }
  4878. }
  4879. }
  4880. }
  4881. //调用内容数量,分享内容次数
  4882. $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
  4883. $share = ShareLog::where([$query1, $query2, ['type', 'in', $share_where]])->field("id,concat(employee_id,'@',DATE_FORMAT(share_time,'%Y-%m-%d')) as indexx,concat(employee_id,type,data_id,DATE_FORMAT(share_time,'%Y-%m-%d')) as str,employee_id")->select()->toArray();
  4884. $shares = [];
  4885. foreach ($share as $share_v) {
  4886. @$shares[$share_v['indexx']] += 1;
  4887. }
  4888. ///培训资料是否看了 课程是否学习过
  4889. $look = TrainCourseView::where([$query1, $query3, ['time', '>', 0]])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
  4890. $look = array_unique(array_column($look, 'indexx'));
  4891. //考核次数 通过次数
  4892. $don = ExamEmpResult::with(['paperInfo' => function ($query) {
  4893. $query->field('id,base_score')->bind(['base_score' => 'base_score']);
  4894. }])->where([$query1, $query3, ['state', '=', 1]])->field("id,paper_id,final_score,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
  4895. $don1 = $don2 = []; //是否有考核,考核是否通过
  4896. foreach ($don as $vv) {
  4897. @$don1[$vv['indexx']] += 1; //考核次数
  4898. if ($vv['final_score'] >= $vv['base_score']) @$don2[$vv['indexx']] += 1; //考核通过次数
  4899. }
  4900. //客户跟进数量
  4901. $vislog = CustomerVisitLog::where([
  4902. ['employee_id|customer_employee_id', 'in', $eids], $query3
  4903. ])->column("state,IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
  4904. $log1 = $log2 = []; //跟进次数 , 跟进客户数
  4905. $state1 = CustomerVisitLog::changeState('已量房', 'chaos');
  4906. $state2 = CustomerVisitLog::changeState('已到店', 'chaos');
  4907. $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
  4908. $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
  4909. $state_count1 = $state_count2 = $state_count3 = $state_count4 = [];
  4910. foreach ($vislog as $ke => $va) {
  4911. @$log1[$va['indexx']] += 1;
  4912. @$log2[$va['indexx']][] = $va['customer_id'];
  4913. if (in_array($va['state'], $state1)) {
  4914. @$state_count1[$va['indexx']][] = $va['customer_id'];
  4915. } elseif (in_array($va['state'], $state2)) {
  4916. @$state_count2[$va['indexx']][] = $va['customer_id'];
  4917. } elseif (in_array($va['state'], $state3)) {
  4918. @$state_count3[$va['indexx']][] = $va['customer_id'];
  4919. } elseif (in_array($va['state'], $state4)) {
  4920. @$state_count4[$va['indexx']][] = $va['customer_id'];
  4921. }
  4922. }
  4923. //预约记录
  4924. $query4 = [
  4925. ['state', '=', 0],
  4926. ['type', 'in', [1, 3]],
  4927. ['employee_id', 'in', $eids],
  4928. ['subscribe_date', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]]
  4929. ];
  4930. $sub1 = $sub2 = []; //1到店 2量房
  4931. $subscribe = CustomersSubscribe::where($query4)->column("employee_id,customer_id,subscribe_date,type,concat(employee_id,'@',DATE_FORMAT(subscribe_date,'%Y-%m-%d')) as indexx");
  4932. foreach ($subscribe as $v4) {
  4933. $v4['type'] == 1 ? @$sub1[$v4['indexx']] += 1 : @$sub2[$v4['indexx']] += 1;
  4934. }
  4935. //话术浏览次数
  4936. $talk_view = TalkskillViewLog::where([$query1, $query3])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
  4937. $talk_count = [];
  4938. foreach ($talk_view as $t_item) {
  4939. @$talk_count[$t_item['indexx']] += 1;
  4940. }
  4941. //智慧屏讲解次数
  4942. $zhihuis = CustomerVisitLog::where([['employee_id|customer_employee_id', 'in', $eids], $query3, ['remark', 'like', '%讲解智慧屏##%']])
  4943. ->field("IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
  4944. $zhihui = [];
  4945. foreach ($zhihuis as $v) {
  4946. @$zhihui[$v['indexx']] += 1;
  4947. }
  4948. // 外呼系统统计
  4949. $outCallLog = OutCallLog::where([['employee_id', 'in', $eids]])->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
  4950. $outCallLog = array_combine(array_column($outCallLog, 'indexx'), $outCallLog);
  4951. // 手机打电话次数获取
  4952. $outCallMbLog = OutCallMbLog::where([
  4953. ['employee_id', 'in', $eids]
  4954. ])->field("count(id) as phone_count,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
  4955. $outCallMbLog = array_combine(array_column($outCallMbLog, 'indexx'), $outCallMbLog);
  4956. $org = Org::where([['id', 'in', array_column($list, 'org_id')]])->column('name', 'id');
  4957. foreach ($list as $key => $val) {
  4958. //部门名称
  4959. $list[$key]['org_name'] = isset($org[$val['org_id']]) ? $org[$val['org_id']] : '无';
  4960. //电话量
  4961. $list[$key]['phone_count'] = 0;
  4962. //接通量
  4963. $list[$key]['on_phone_count'] = 0;
  4964. //未接通
  4965. $list[$key]['off_phone_count'] = 0;
  4966. //平均通话时长
  4967. $list[$key]['avg_phone_time'] = 0;
  4968. if (isset($outCallLog[$val['index']])) {
  4969. //电话量
  4970. $list[$key]['phone_count'] = $outCallLog[$val['index']]['phone_count'];
  4971. //接通量
  4972. $list[$key]['on_phone_count'] = $outCallLog[$val['index']]['on_phone_count'];
  4973. //未接通
  4974. $list[$key]['off_phone_count'] = $outCallLog[$val['index']]['off_phone_count'];
  4975. //平均通话时长
  4976. $list[$key]['avg_phone_time'] = $list[$key]['on_phone_count'] == 0 ? 0 : round($outCallLog[$val['index']]['phone_time'] / $list[$key]['on_phone_count'], 2);
  4977. }
  4978. if (isset($outCallMbLog[$val['index']])) {
  4979. $list[$key]['phone_count'] += $outCallMbLog[$val['index']]['phone_count'];
  4980. }
  4981. //加微数量
  4982. $list[$key]['add_wechat_count'] = isset($add_wechat_date[$val['index']]) ? $add_wechat_date[$val['index']] : 0;
  4983. //分享内容数量
  4984. $list[$key]['share_count'] = isset($shares[$val['index']]) ? $shares[$val['index']] : 0;
  4985. //智慧屏讲解次数
  4986. $list[$key]['explain_count'] = isset($zhihui[$val['index']]) ? $zhihui[$val['index']] : 0;
  4987. //话术浏览次数
  4988. $list[$key]['talkskill_view_count'] = isset($talk_count[$val['index']]) ? $talk_count[$val['index']] : 0;
  4989. //培训是否看
  4990. $list[$key]['is_look'] = in_array($val['index'], $look) ? '是' : '否';
  4991. //跟进次数
  4992. $list[$key]['log_count'] = isset($log1[$val['index']]) ? $log1[$val['index']] : 0;
  4993. //跟进客户数量
  4994. $list[$key]['log_customer_count'] = isset($log2[$val['index']]) ? count(array_unique($log2[$val['index']])) : 0;
  4995. //量房客户数
  4996. $list[$key]['measuring_room_customer_count'] = isset($state_count1[$val['index']]) ? count(array_unique($state_count1[$val['index']])) : 0;
  4997. //到店客户数
  4998. $list[$key]['sto_customer_count'] = isset($state_count2[$val['index']]) ? count(array_unique($state_count2[$val['index']])) : 0;
  4999. //交定
  5000. $list[$key]['deposit_customer_count'] = isset($state_count3[$val['index']]) ? count(array_unique($state_count3[$val['index']])) : 0;
  5001. //签单
  5002. $list[$key]['sign_customer_count'] = isset($state_count4[$val['index']]) ? count(array_unique($state_count4[$val['index']])) : 0;
  5003. //预计转施工客户数量
  5004. $list[$key]['house_delivery_count'] = isset($house_delivery_time[$val['index']]) ? $house_delivery_time[$val['index']] : 0;
  5005. //预计到店数
  5006. $list[$key]['subscribe_store_count'] = isset($sub1[$val['index']]) ? $sub1[$val['index']] : 0;
  5007. //预计量房数量
  5008. $list[$key]['subscribe_room_count'] = isset($sub2[$val['index']]) ? $sub2[$val['index']] : 0;
  5009. //是否有考核
  5010. $list[$key]['is_exam'] = isset($don1[$val['index']]) ? '是' : '否';
  5011. //考核是否通过
  5012. $list[$key]['is_exam_ok'] = isset($don2[$val['index']]) ? '是' : '否';
  5013. }
  5014. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $limit)]);
  5015. }
  5016. /**
  5017. * 行为统计数据导出
  5018. */
  5019. public function behaviorStatisticsDataExport()
  5020. {
  5021. $root_id = request()->employee->root_id;
  5022. $type = input('type', '');
  5023. if (!request()->isAjax()) {
  5024. $header2 = $header3 = [];
  5025. if ($type == 'high') {
  5026. //高层统计
  5027. $header1 = [
  5028. ["field" => 'x_date', "title" => '时 间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5029. ["field" => 'name', "title" => '员工姓名', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5030. ["field" => 'org_name', "title" => '所属部门', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5031. ["field" => 'position', "title" => '职位', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5032. ["field" => 'phone_count', "title" => '电话数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5033. ["field" => 'on_phone_count', "title" => '接通数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5034. ["field" => 'off_phone_count', "title" => '未接通', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5035. ["field" => 'avg_phone_time', "title" => '平均通话时长', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5036. ["field" => 'add_wechat_count', "title" => '加微数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5037. ["field" => 'share_count', "title" => '调用内容数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5038. ["field" => 'talkskill_view_count', "title" => '浏览话术次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5039. ["field" => 'is_look', "title" => '看培训次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5040. ["field" => 'look_time', "title" => '看培训时长(S)', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5041. ["field" => 'is_exam', "title" => '考核次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5042. ["field" => 'is_exam_ok', "title" => '考核通过次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5043. ["field" => 'log_customer_count', "title" => '客户跟进数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5044. ["field" => 'log_count', "title" => '总回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5045. ["field" => 'log_time', "title" => '平均回访时长', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5046. ["field" => 'log_time1', "title" => '平均回访周期', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5047. ["field" => 'explain_count', "title" => '智慧屏讲解次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5048. ["field" => 'explain_count1', "title" => '每个客户讲解次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5049. ["field" => 'measuring_room_customer_count', "title" => '量房客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5050. ["field" => 'store_1', "title" => '一次到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5051. ["field" => 'store_1_grawth', "title" => '一次到店率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5052. ["field" => 'store_2', "title" => '二次到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5053. ["field" => 'store_2_grawth', "title" => '二次到店率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5054. ["field" => 'store_3', "title" => '三次及以上到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5055. ["field" => 'store_3_grawth', "title" => '三次及以上到店率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5056. ["field" => 'store_1_dep', "title" => '一次到店签单客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5057. ["field" => 'store_1_dep_grawth', "title" => '一次到店签单率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5058. ["field" => 'deposit_customer_count', "title" => '总签单数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5059. ["field" => 'deposit_customer_grawth', "title" => '总签单率', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"]
  5060. ];
  5061. view::assign('row', json_encode($header1));
  5062. } elseif ($type == 'manager') {
  5063. $header1 = [
  5064. ["field" => 'name', "title" => '员工名称', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5065. ["field" => 'customer_count', "title" => '客户数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5066. ["field" => 'return_visit_count', "title" => '待回访客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5067. ["field" => 'no_assigned_personnel', "title" => '待指派客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5068. ["field" => 'no_visit_3', "title" => '3天内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5069. ["field" => 'no_visit_7', "title" => '7天内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5070. ["field" => 'no_visit_15', "title" => '15天内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5071. ["field" => 'no_visit_30', "title" => '一个月内未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5072. ["field" => 'no_visit_31', "title" => '一个月以上未跟进客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5073. ["field" => 'valid_count', "title" => '有效客户', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5074. ["field" => 'valid_grawth', "title" => '有效率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5075. ["field" => 'phone_count', "title" => '电话数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5076. ["field" => 'on_phone_count', "title" => '接通数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5077. ["field" => 'on_phone_grawth', "title" => '接通率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5078. ["field" => 'off_phone_count', "title" => '未接通', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5079. ["field" => 'avg_phone_time', "title" => '平均通话时长', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5080. ["field" => 'add_wechat_count', "title" => '加微数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5081. ["field" => 'add_wechat_grawth', "title" => '加微率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5082. ["field" => 'room', "title" => '量房客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5083. ["field" => 'room_grawth', "title" => '量房率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5084. ["field" => 'store1', "title" => '一次到店客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5085. ["field" => 'store1_grawth', "title" => '一次到店率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5086. ["field" => 'store2', "title" => '二次到店客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5087. ["field" => 'store2_grawth', "title" => '二次到店率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5088. ["field" => 'store3', "title" => '三次及以上到店客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5089. ["field" => 'store3_grawth', "title" => '三次及以上到店率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5090. ["field" => 'store1_dep', "title" => '一次到店签单客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5091. ["field" => 'store1_dep_grawth', "title" => '一次到店签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5092. ["field" => 'store2_dep', "title" => '二次到店签单数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5093. ["field" => 'store2_dep_grawth', "title" => '二次到店签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5094. ["field" => 'store3_dep', "title" => '三次及以上到店签单数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5095. ["field" => 'store3_dep_grawth', "title" => '三次及以上到店签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5096. ["field" => 'dep', "title" => '总签单数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5097. ["field" => 'dep_grawth', "title" => '总签单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5098. ["field" => 'drawing_date', "title" => '出方案客户数', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5099. ["field" => 'sign_count', "title" => '转单客户', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5100. ["field" => 'sign_grawth', "title" => '转单率', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5101. ["field" => '', "child" => ['square_0_80', 'square_80_100', 'square_100_120', 'square_120_200', 'square_200_500', 'square_500'], "title" => '面积分布', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9", 'colspan' => 6],
  5102. ["field" => 'existing_homes_count', "title" => '现房数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5103. ["field" => 'forward_housing_count', "title" => '期房数量', "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"],
  5104. ];
  5105. //房屋状态是否开启
  5106. $house_status_where = [
  5107. ['root_id', '=', $root_id],
  5108. ['keyname', '=', 'house_status'],
  5109. ['status', '=', 0]
  5110. ];
  5111. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  5112. $field = [];
  5113. if (!$house_status_per->isEmpty()) {
  5114. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('id,name');
  5115. foreach ($field as $val) {
  5116. $header1[] = ["field" => 'house_status_' . $val['id'], "title" => $val['name'], "align" => 'center', "rowspan" => 2, "class" => "bgFDE9D9"];
  5117. }
  5118. }
  5119. $header2 = [
  5120. ["field" => 'square_0_80', "title" => '80以下', "align" => 'center', "class" => "bgFDE9D9"],
  5121. ["field" => 'square_80_100', "title" => '81-100', "align" => 'center', "class" => "bgFDE9D9"],
  5122. ["field" => 'square_100_120', "title" => '101-120', "align" => 'center', "class" => "bgFDE9D9"],
  5123. ["field" => 'square_120_200', "title" => '121-200', "align" => 'center', "class" => "bgFDE9D9"],
  5124. ["field" => 'square_200_500', "title" => '201-500', "align" => 'center', "class" => "bgFDE9D9"],
  5125. ["field" => 'square_500', "title" => '500以上', "align" => 'center', "class" => "bgFDE9D9"]
  5126. ];
  5127. $header = $header1;
  5128. foreach ($header as $k => $v) {
  5129. if (isset($v['child'])) {
  5130. array_splice($header, $k, 1, $header2);
  5131. }
  5132. }
  5133. view::assign('row', json_encode($header));
  5134. } elseif ($type == 'general') {
  5135. $header1 = [
  5136. ['field' => 'name', 'title' => '业务员名称', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5137. ['field' => 'org_name', 'title' => '业务员所在部门', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5138. ['field' => 'connecting_capacity', 'title' => '接通多少', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5139. ['field' => 'connecting_capacity_grawth', 'title' => '接通率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5140. ['field' => 'average_duration', 'title' => '平均通话时长', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5141. ['field' => 'add_wechat_count', 'title' => '加微数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5142. ['field' => 'add_wechat_count_grawth', 'title' => '微信通过率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5143. ['field' => 'shares_count', 'title' => '调用内容数量', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5144. ['field' => 'number_of_explanations', 'title' => '智慧屏讲解次数', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5145. ['field' => 'look_data', 'title' => '培训资料是否看了', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5146. ['field' => 'assessment_times', 'title' => '考核次数', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5147. ['field' => 'number_of_passes', 'title' => '通过次数', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5148. ['field' => 'resource_count', 'title' => '分配数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5149. ['field' => 'following_up_count', 'title' => '在跟进数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5150. ['field' => 'deposit_count', 'title' => '签单数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5151. ['field' => 'reported_quantity', 'title' => '报备数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5152. ['field' => 'valid_count', 'title' => '有效数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5153. ['field' => 'valid_grawth', 'title' => '有效率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5154. ['field' => 'valid_no_wechat', 'title' => '有效未加微', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5155. ['field' => 'measuring_room_customer_no_to_store', 'title' => '量房未到店', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5156. ['field' => 'drawing_date_count', 'title' => '户型图', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5157. ['field' => 'measuring_room_customer', 'title' => '量房数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5158. ['field' => 'measuring_room_zhouqi', 'title' => '平均量房周期', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5159. ['field' => 'measuring_room_grawth', 'title' => '量房率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5160. ['field' => 'yuji_store', 'title' => '预计到店数', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5161. ['field' => 'to_the_store_1', 'title' => '一次到店', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5162. ['field' => 'to_the_store_2', 'title' => '二次到店', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5163. ['field' => 'to_the_store_3', 'title' => '三次及以上到店', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5164. ['field' => 'strtore_avg_days_1', 'title' => '平均一次到店周期', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5165. ['field' => 'strtore_avg_days_2', 'title' => '平均二次到店周期', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5166. ['field' => 'strtore_avg_days_3', 'title' => '平均三次及以上到店周期', 'align' => 'center', 'width' => 200, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5167. ['field' => 'strtore_count_1_grawth', 'title' => '一次到店率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5168. ['field' => 'strtore_count_2_grawth', 'title' => '二次到店率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5169. ['field' => 'strtore_count_3_grawth', 'title' => '三次及以上到店率', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5170. ['field' => 'avg_room_store_zhouqi', 'title' => '平均量房到店周期', 'align' => 'center', 'width' => 170, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5171. ['field' => 'strtore_deposit_count_0', 'title' => '一次到店签单数量', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5172. ['field' => 'strtore_deposit_count_1', 'title' => '二次到店签单数量', 'align' => 'center', 'width' => 150, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5173. ['field' => 'strtore_deposit_count_2', 'title' => '三次到及以上店签单数量', 'align' => 'center', 'width' => 200, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 38],
  5174. ['field' => 'strtore_deposit_grawth_0', 'title' => '一次到店签单率', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 39],
  5175. ['field' => 'strtore_deposit_grawth_1', 'title' => '二次到店签单率', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 40],
  5176. ['field' => 'strtore_deposit_grawth_2', 'title' => '三次到及以上店签单率', 'align' => 'center', 'width' => 180, 'rowspan' => 1, 'class' => 'bgFDE9D9', 'sort' => 41],
  5177. ['field' => 'deposit_avg_days', 'title' => '平均签单周期', 'align' => 'center', 'width' => 135, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5178. ['field' => 'sign_count', 'title' => '转单数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5179. ['field' => 'sign_count_grawth', 'title' => '转单率', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5180. ['field' => 'avg_visit_count', 'title' => '平均回访次数', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5181. ['field' => 'signed_money', 'title' => '平均合同金额', 'align' => 'center', 'width' => 130, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5182. ['field' => 'square_0_80', 'title' => '80以下', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5183. ['field' => 'square_80_100', 'title' => '81-100', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5184. ['field' => 'square_100_120', 'title' => '101-120', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5185. ['field' => 'square_120_200', 'title' => '121-200', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5186. ['field' => 'square_200_500', 'title' => '201-500', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5187. ['field' => 'square_500', 'title' => '500以上', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5188. ['field' => 'existing_homes_count', 'title' => '现房数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5189. ['field' => 'forward_housing_count', 'title' => '期房数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5190. ['field' => 'maika_count', 'title' => '卖卡数量', 'align' => 'center', 'width' => 100, 'rowspan' => 1, 'class' => 'bgFDE9D9'],
  5191. ];
  5192. //网销端已增加type值到10,如增加type请往10后面加
  5193. $root_id = request()->employee->root_id;
  5194. //房屋状态是否开启
  5195. $house_status_where = [
  5196. ['root_id', '=', $root_id],
  5197. ['keyname', '=', 'house_status'],
  5198. ['status', '=', 0]
  5199. ];
  5200. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  5201. $house_field = [];
  5202. if (!$house_status_per->isEmpty()) {
  5203. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  5204. $fields = [];
  5205. foreach ($field as $v) {
  5206. $fields[] = [
  5207. 'field' => 'house_status_' . $v['id'],
  5208. 'title' => $v['name'],
  5209. 'align' => 'center',
  5210. 'width' => 100,
  5211. 'rowspan' => 1,
  5212. 'class' => 'bgFDE9D9'
  5213. ];
  5214. }
  5215. $house_field = $fields;
  5216. }
  5217. $header1 = array_merge($header1, $house_field);
  5218. // 户型
  5219. $housetype_where = [
  5220. ['root_id', '=', $root_id],
  5221. ['keyname', '=', 'housetype_arrow'],
  5222. ['status', '=', 0]
  5223. ];
  5224. $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
  5225. $housetype_field = [];
  5226. $housetype_done_field = [];
  5227. if (!$housetype_per->isEmpty()) {
  5228. $field = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
  5229. $fields = [];
  5230. $fields_done = [];
  5231. foreach ($field as $v) {
  5232. $fields[] = [
  5233. 'field' => 'housetype_arrow_' . $v['id'],
  5234. 'title' => $v['name'],
  5235. 'align' => 'center',
  5236. 'width' => 100,
  5237. 'rowspan' => 1,
  5238. 'class' => 'bgFDE9D9'
  5239. ];
  5240. $fields_done[] = [
  5241. 'field' => 'housetype_arrow_lv_' . $v['id'],
  5242. 'title' => $v['name'] . '成交率',
  5243. 'align' => 'center',
  5244. 'width' => 100,
  5245. 'rowspan' => 1,
  5246. 'class' => 'bgFDE9D9'
  5247. ];
  5248. }
  5249. $housetype_field = $fields;
  5250. $housetype_done_field = $fields_done;
  5251. }
  5252. $header1 = array_merge($header1, $housetype_field);
  5253. $header1 = array_merge($header1, $housetype_done_field);
  5254. //无效客资
  5255. $invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  5256. if ($invalid_customer) {
  5257. $invalid_arr = explode(',', $invalid_customer);
  5258. } else {
  5259. $invalid_arr = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  5260. }
  5261. $invalid_field = [];
  5262. foreach ($invalid_arr as $k => $v) {
  5263. $invalid_field[] = [
  5264. 'field' => 'invalid_customer_' . $k,
  5265. 'title' => $v,
  5266. 'align' => 'center',
  5267. 'width' => 100,
  5268. 'rowspan' => 1,
  5269. 'class' => 'bgFDE9D9'
  5270. ];
  5271. }
  5272. $new_type_5_3 = [];
  5273. foreach ($header1 as $k => $v) {
  5274. $new_type_5_3[$k] = $v;
  5275. if ($v['field'] == 'signed_money') {
  5276. $new_type_5_3 = array_merge($new_type_5_3, $invalid_field);
  5277. }
  5278. }
  5279. $header1 = $new_type_5_3;
  5280. view::assign('row', json_encode($header1));
  5281. } else {
  5282. // 行为统计
  5283. $header1 = [
  5284. ["field" => 'x_date', "title" => '时 间', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5285. ["field" => 'name', "title" => '员工姓名', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5286. ["field" => 'org_name', "title" => '所属部门', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5287. ["field" => 'phone_count', "title" => '电话量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5288. ["field" => 'on_phone_count', "title" => '接通量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5289. ["field" => 'off_phone_count', "title" => '未接通', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5290. ["field" => 'avg_phone_time', "title" => '平均通话时长', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5291. ["field" => 'add_wechat_count', "title" => '加微数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5292. ["field" => 'share_count', "title" => '调用内容数量', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5293. ["field" => 'explain_count', "title" => '智慧屏讲解次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5294. ["field" => 'talkskill_view_count', "title" => '浏览话术次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5295. ["field" => 'is_look', "title" => '培训是否看了', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5296. ["field" => 'is_exam', "title" => '是否有考核', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5297. ["field" => 'is_exam_ok', "title" => '考核是否通过', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5298. ["field" => 'log_customer_count', "title" => '客户跟进数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5299. ["field" => 'log_count', "title" => '总回访次数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5300. ["field" => 'subscribe_room_count', "title" => '预计量房客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5301. ["field" => 'subscribe_store_count', "title" => '预计到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5302. ["field" => 'measuring_room_customer_count', "title" => '量房客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5303. ["field" => 'sto_customer_count', "title" => '到店客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5304. ["field" => 'deposit_customer_count', "title" => '交定/签单客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5305. ["field" => 'house_delivery_count', "title" => '预计转施工客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"],
  5306. ["field" => 'sign_customer_count', "title" => '转单客户数', "align" => 'center', "rowspan" => 1, "class" => "bgFDE9D9"]
  5307. ];
  5308. view::assign('row', json_encode($header1));
  5309. }
  5310. View::assign('header1', $header1);
  5311. View::assign('header2', $header2);
  5312. View::assign('header3', $header3);
  5313. return View::fetch();
  5314. }
  5315. if ($type == 'high') {
  5316. return $this->highBehaviorStatisticsData();
  5317. } elseif ($type == 'manager') {
  5318. return $this->manageStatisticsData();
  5319. } elseif ($type == 'general') {
  5320. return $this->generalStatisticsData();
  5321. } else {
  5322. return $this->behaviorStatisticsData();
  5323. }
  5324. }
  5325. /**
  5326. * 分享内容页面
  5327. */
  5328. public function behaviorStatisticsShareLog()
  5329. {
  5330. $root_id = request()->employee->root_id;
  5331. $index = input('index', '');
  5332. View::assign('index', $index);
  5333. return View::fetch();
  5334. }
  5335. /**
  5336. * 分享内容列表接口
  5337. */
  5338. public function behaviorStatisticsShareLogData($page = 1, $limit = 10)
  5339. {
  5340. $param = Request::only(['page' => 1, 'index' => '']);
  5341. $root_id = request()->employee->root_id;
  5342. $arr = explode('@', $param['index']);
  5343. $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
  5344. $where = [
  5345. ['employee_id', '=', $arr[0]],
  5346. ['share_time', 'like', '%' . $arr[1] . '%'],
  5347. ['type', 'in', $share_where]
  5348. ];
  5349. $list = ShareLog::with('content')->where($where)->page($page, $limit)->order('id desc')->select()->toArray();
  5350. $count = ShareLog::where($where)->count();
  5351. $arr = [
  5352. 'Activity' => '#活动#',
  5353. 'Article' => '#图文素材#',
  5354. 'Building' => '#楼盘进度#',
  5355. 'Card' => '#名片#',
  5356. 'CompanyStrength' => '#公司实力#',
  5357. 'Construction' => '#在施工地#',
  5358. 'CustomerJsAll' => '',
  5359. 'MaterialCase' => '#装修案例#',
  5360. 'MaterialEvidence' => '#口碑见证#',
  5361. 'ToolAll' => '#谈单工具#',
  5362. 'Video' => '#视频#',
  5363. 'Weiwang' => '#个人微网#'
  5364. ];
  5365. foreach ($list as $k => $v) {
  5366. $type = isset($arr[$v['type']]) ? $arr[$v['type']] : '';
  5367. if ($v['content']) {
  5368. $field = $v['type'] == 'Building' ? 'name' : 'title';
  5369. $list[$k]['title'] = $type . '《' . $v['content'][$field] . '》';
  5370. } else {
  5371. $list[$k]['title'] = '';
  5372. }
  5373. }
  5374. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  5375. }
  5376. /**
  5377. * 高层行为统计页面
  5378. */
  5379. public function highBehaviorStatistics()
  5380. {
  5381. //返回组织列表
  5382. $org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
  5383. View::assign('org', $org);
  5384. return View::fetch();
  5385. }
  5386. /**
  5387. * 高层行为统计接口
  5388. * 日期截止到 2022011-20
  5389. */
  5390. public function highBehaviorStatisticsData($page = 1, $limit = 10)
  5391. {
  5392. $param = Request::only(['page' => 1, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']);
  5393. $root_id = request()->employee->root_id;
  5394. if ($param['start_date']) {
  5395. $count_dates = explode(' - ', $param['start_date']);
  5396. $start_date = strtotime($count_dates[0]);
  5397. $end_date = strtotime($count_dates[1]);
  5398. } else {
  5399. $start_date = strtotime('2022-11-20');
  5400. $end_date = strtotime(date('Y-m-d'));
  5401. }
  5402. if ($param['keyword']) $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  5403. if ($param['org_id']) {
  5404. $o = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
  5405. $orgIds = Org::where([['path', 'like', $o['path'] . '%']])->column('id');
  5406. $where[] = ['org_id', 'in', $orgIds];
  5407. }
  5408. $where[] = ['root_id', '=', $root_id];
  5409. $where[] = ['uid', '>', 0];
  5410. $where[] = ['state', 'in', ['在职', '离职']];
  5411. $eids = Employee::where($where)->column('id');
  5412. $e_count = count($eids);
  5413. //每页开始的位置
  5414. $start = ($page - 1) * $limit;
  5415. $end = $start + $limit - 1;
  5416. //统计日期总天数
  5417. $day = ($end_date - $start_date) / 86400;
  5418. //数据总量
  5419. $count = ($day + 1) * $e_count;
  5420. $ids = [];
  5421. for ($i = 0; $i <= $day; $i++) {
  5422. $j = $i * $e_count;
  5423. if (($j + $e_count) < $start) continue;
  5424. $e = false;
  5425. foreach ($eids as $k => $v) {
  5426. $g = $j + $k;
  5427. if ($g >= $start && $g <= $end) {
  5428. $date = date('Y-m-d', $end_date - ($i * 86400));
  5429. $ids[] = ['id' => $v, 'index' => $v . '@' . $date, 'g' => $g];
  5430. } elseif ($j > $end) {
  5431. $e = true;
  5432. break;
  5433. }
  5434. }
  5435. if ($e) break;
  5436. }
  5437. $lists = array_column($ids, 'index');
  5438. $where[] = ['id', 'in', array_column($ids, 'id')];
  5439. $employees = Employee::with(['customer' => function ($query) {
  5440. $query->field("id,employee_id,DATE_FORMAT(addtime,'%Y-%m-%d') as addtime");
  5441. }])->where($where)->field('id,name,org_id,addtime,position,state,termination_time')->select()->toArray();
  5442. $em_dates = [];
  5443. foreach ($employees as $emp_key => $emp_item) {
  5444. $em_dates[$emp_item['id']] = $emp_item;
  5445. }
  5446. $list = [];
  5447. foreach ($lists as $list_key => $list_item) {
  5448. $arr = explode('@', $list_item);
  5449. $j = $em_dates[$arr[0]];
  5450. $j['x_date'] = date('Y年m月d日', strtotime($arr[1]));
  5451. $j['date'] = $arr[1];
  5452. $j['index'] = $list_item;
  5453. $list[] = $j;
  5454. }
  5455. $eids = array_unique(array_column($list, 'id'));
  5456. $query1 = ['employee_id', 'in', $eids];
  5457. $query2 = ['share_time', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
  5458. $query3 = ['addtime', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]];
  5459. $custoemr_where = [
  5460. ['employee_id', 'in', $eids]
  5461. ];
  5462. $customer = Customer::where($custoemr_where)->column("id,ext,employee_id,house_delivery_time,DATE_FORMAT(addtime,'%Y-%m-%d') as addtime");
  5463. //加微客户 预计转施工时间
  5464. $add_wechat_date = [];
  5465. $house_delivery_time = [];
  5466. $e_cids = [];
  5467. foreach ($customer as $v1) {
  5468. $v2 = $v1['ext'];
  5469. $ext = json_decode($v2, true);
  5470. if (!empty($ext) && is_array($ext)) {
  5471. $ext = array_filter($ext);
  5472. foreach ($ext as $e) {
  5473. if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value'])) {
  5474. @$add_wechat_date[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
  5475. } elseif (isset($e['keyname']) && $e['keyname'] == 'will_transfer_to_construction_date' && !empty($e['value'])) {
  5476. @$house_delivery_time[$v1['employee_id'] . '@' . date('Y-m-d', strtotime($e['value']))] += 1;
  5477. }
  5478. }
  5479. }
  5480. $e_cids[$v1['employee_id']][] = $v1['id'];
  5481. }
  5482. //客户添加日期截至到列表日期的天数
  5483. $vis = CustomerVisitLog::where([['customer_id', 'in', array_column($customer, 'id')], ['customer_employee_id', 'in', $eids]])->field("DATE_FORMAT(addtime,'%Y-%m-%d') as addtime,concat(customer_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) indexx,customer_id,customer_employee_id")->group('indexx')->order('id asc')->select()->toArray();
  5484. $viss = [];
  5485. foreach ($vis as $vis_k => $vis_v) {
  5486. $viss[$vis_v['customer_id']][] = strtotime($vis_v['addtime']);
  5487. }
  5488. foreach ($list as $list_key => $list_val) {
  5489. $customers = $list_val['customer'];
  5490. foreach ($customers as $customers_key => $customers_val) {
  5491. //列表日期
  5492. $customers[$customers_key]['list_date'] = $list_val['date'];
  5493. $start = strtotime($list_val['date']);
  5494. //客户添加日期截至到列表日期的天数
  5495. $customers[$customers_key]['date_diff'] = ceil(($start - strtotime($customers_val['addtime']) + 1) / 86400);
  5496. $customers[$customers_key]['visit_count'] = 0;
  5497. //从客户添加日期截至列表日期的客户的回访次数
  5498. if (isset($viss[$customers_val['id']])) {
  5499. $flip = array_flip($viss[$customers_val['id']]);
  5500. foreach ($flip as $flip_k => $flip_v) {
  5501. if ($flip_k > $start) {
  5502. break;
  5503. }
  5504. $customers[$customers_key]['visit_count'] += 1;
  5505. }
  5506. }
  5507. //客户回访周期
  5508. $customers[$customers_key]['visit_cycle'] = $customers[$customers_key]['visit_count'] == 0 ? 0 : ceil($customers[$customers_key]['date_diff'] / $customers[$customers_key]['visit_count']);
  5509. }
  5510. $cycle = array_filter(array_column($customers, 'visit_cycle'));
  5511. $list[$list_key]['log_time1'] = $cycle ? ceil(array_sum($cycle) / count($cycle)) : 0;
  5512. unset($list[$list_key]['customer']);
  5513. }
  5514. //每人截止到日期的回访次数 和 天数
  5515. $dates = array_column($list, 'date'); //要查询的所有日期
  5516. //调用内容数量,分享内容次数
  5517. $share_where = ['CompanyStrength', 'Article', 'Construction', 'Activity', 'MaterialEvidence', 'Video', 'MaterialCase', 'Building'];
  5518. $share = ShareLog::where([$query1, $query2, ['type', 'in', $share_where]])->field("id,concat(employee_id,'@',DATE_FORMAT(share_time,'%Y-%m-%d')) as indexx,concat(employee_id,type,data_id,DATE_FORMAT(share_time,'%Y-%m-%d')) as str,employee_id")->select()->toArray();
  5519. $shares = [];
  5520. foreach ($share as $share_v) {
  5521. @$shares[$share_v['indexx']] += 1;
  5522. }
  5523. ///培训资料是否看了 课程是否学习过
  5524. $look = TrainCourseView::where([$query1, $query3, ['time', '>', 0]])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx,time");
  5525. $looks = [];
  5526. $look_time = [];
  5527. foreach ($look as $look_item) {
  5528. @$looks[$look_item['indexx']] += 1;
  5529. @$look_time[$look_item['indexx']] += $look_item['time'];
  5530. }
  5531. //考核次数 通过次数
  5532. $don = ExamEmpResult::with(['paperInfo' => function ($query) {
  5533. $query->field('id,base_score')->bind(['base_score' => 'base_score']);
  5534. }])->where([$query1, $query3, ['state', '=', 1]])->field("id,paper_id,final_score,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
  5535. $don1 = $don2 = []; //是否有考核,考核是否通过
  5536. foreach ($don as $vv) {
  5537. @$don1[$vv['indexx']] += 1; //考核次数
  5538. if ($vv['final_score'] >= $vv['base_score']) @$don2[$vv['indexx']] += 1; //考核通过次数
  5539. }
  5540. //客户跟进数量
  5541. $vislog = CustomerVisitLog::where([
  5542. ['employee_id|customer_employee_id', 'in', $eids], $query3
  5543. ])->order('id asc')->column("state,IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
  5544. $log1 = $log2 = []; //跟进次数 , 跟进客户数
  5545. $state1 = CustomerVisitLog::changeState('已量房', 'chaos');
  5546. $state2 = CustomerVisitLog::changeState('已到店', 'chaos');
  5547. $state3 = CustomerVisitLog::changeState('已交定', 'chaos');
  5548. $state4 = CustomerVisitLog::changeState('已签单', 'chaos');
  5549. $state_count1 = $state_count2 = $state_count3 = $state_count4 = $store = [];
  5550. foreach ($vislog as $ke => $va) {
  5551. @$log1[$va['indexx']] += 1;
  5552. @$log2[$va['indexx']][] = $va['customer_id'];
  5553. if (in_array($va['state'], $state1)) {
  5554. @$state_count1[$va['indexx']][] = $va['customer_id'];
  5555. } elseif (in_array($va['state'], $state2)) {
  5556. @$state_count2[$va['indexx']][] = $va['customer_id'];
  5557. @$store[$va['indexx']][$va['customer_id']][] = $va['addtime'];
  5558. } elseif (in_array($va['state'], $state3)) {
  5559. @$state_count3[$va['indexx']][] = $va['customer_id'];
  5560. } elseif (in_array($va['state'], $state4)) {
  5561. @$state_count4[$va['indexx']][] = $va['customer_id'];
  5562. }
  5563. }
  5564. //一次到店,2次到店 ,3次及以上到店
  5565. $s1 = $cus1 = $s2 = $cus2 = $s3 = $cus3 = [];
  5566. foreach ($store as $store_key => $store_item) {
  5567. foreach ($store_item as $store_item_key => $store_item_item) {
  5568. if (count($store_item_item) == 1) {
  5569. @$s1[$store_key] += 1;
  5570. $cus1[] = $store_item_key;
  5571. } elseif (count($store_item_item) == 2) {
  5572. @$s2[$store_key] += 1;
  5573. $cus2[] = $store_item_key;
  5574. } elseif (count($store_item_item) >= 3) {
  5575. @$s3[$store_key] += 1;
  5576. $cus3[] = $store_item_key;
  5577. }
  5578. }
  5579. }
  5580. //预约记录
  5581. $query4 = [
  5582. ['state', '=', 0],
  5583. ['type', 'in', [1, 3]],
  5584. ['employee_id', 'in', $eids],
  5585. ['subscribe_date', 'between', [date('Y-m-d H:i:s', $start_date), date('Y-m-d H:i:s', $end_date + 86399)]]
  5586. ];
  5587. $sub1 = $sub2 = []; //1到店 2量房
  5588. $subscribe = CustomersSubscribe::where($query4)->column("employee_id,customer_id,subscribe_date,type,concat(employee_id,'@',DATE_FORMAT(subscribe_date,'%Y-%m-%d')) as indexx");
  5589. foreach ($subscribe as $v4) {
  5590. $v4['type'] == 1 ? @$sub1[$v4['indexx']] += 1 : @$sub2[$v4['indexx']] += 1;
  5591. }
  5592. //话术浏览次数
  5593. $talk_view = TalkskillViewLog::where([$query1, $query3])->column("concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx");
  5594. $talk_count = [];
  5595. foreach ($talk_view as $t_item) {
  5596. @$talk_count[$t_item['indexx']] += 1;
  5597. }
  5598. //智慧屏讲解次数
  5599. $zhihuis = CustomerVisitLog::where([['employee_id|customer_employee_id', 'in', $eids], $query3, ['remark', 'like', '%讲解智慧屏##%']])
  5600. ->field("IF(employee_id,employee_id,customer_employee_id) as employee,DATE_FORMAT(addtime,'%Y-%m-%d') as date,customer_id,concat(IF(employee_id,employee_id,customer_employee_id),'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->select()->toArray();
  5601. $zhihui = [];
  5602. foreach ($zhihuis as $v) {
  5603. @$zhihui[$v['indexx']] += 1;
  5604. }
  5605. // 外呼系统统计
  5606. $outCallLog = OutCallLog::where([['employee_id', 'in', $eids]])->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
  5607. $outCallLog = array_combine(array_column($outCallLog, 'indexx'), $outCallLog);
  5608. // 手机打电话次数获取
  5609. $outCallMbLog = OutCallMbLog::where([
  5610. ['employee_id', 'in', $eids]
  5611. ])->field("count(id) as phone_count,concat(employee_id,'@',DATE_FORMAT(addtime,'%Y-%m-%d')) as indexx")->group('indexx')->select()->toArray();
  5612. $outCallMbLog = array_combine(array_column($outCallMbLog, 'indexx'), $outCallMbLog);
  5613. $org = Org::where([['id', 'in', array_column($list, 'org_id')]])->column('name', 'id');
  5614. foreach ($list as $key => $val) {
  5615. //部门名称
  5616. $list[$key]['org_name'] = isset($org[$val['org_id']]) ? $org[$val['org_id']] : '无';
  5617. //职位
  5618. // $list[$key]['position'] = $val['position'];
  5619. //电话量
  5620. $list[$key]['phone_count'] = 0;
  5621. //接通量
  5622. $list[$key]['on_phone_count'] = 0;
  5623. //未接通
  5624. $list[$key]['off_phone_count'] = 0;
  5625. //平均通话时长
  5626. $list[$key]['avg_phone_time'] = 0;
  5627. if (isset($outCallLog[$val['index']])) {
  5628. //电话量
  5629. $list[$key]['phone_count'] = $outCallLog[$val['index']]['phone_count'];
  5630. //接通量
  5631. $list[$key]['on_phone_count'] = $outCallLog[$val['index']]['on_phone_count'];
  5632. //未接通
  5633. $list[$key]['off_phone_count'] = $outCallLog[$val['index']]['off_phone_count'];
  5634. //平均通话时长
  5635. $list[$key]['avg_phone_time'] = $list[$key]['on_phone_count'] == 0 ? 0 : round($outCallLog[$val['index']]['phone_time'] / $list[$key]['on_phone_count'], 2);
  5636. }
  5637. if (isset($outCallMbLog[$val['index']])) {
  5638. //电话量
  5639. $list[$key]['phone_count'] += $outCallMbLog[$val['index']]['phone_count'];
  5640. }
  5641. //加微数量
  5642. $list[$key]['add_wechat_count'] = isset($add_wechat_date[$val['index']]) ? $add_wechat_date[$val['index']] : 0;
  5643. //分享内容数量
  5644. $list[$key]['share_count'] = isset($shares[$val['index']]) ? $shares[$val['index']] : 0;
  5645. //智慧屏讲解次数
  5646. $list[$key]['explain_count'] = isset($zhihui[$val['index']]) ? $zhihui[$val['index']] : 0;
  5647. //每个客户讲解次数
  5648. $list[$key]['explain_count1'] = $list[$key]['add_wechat_count'] == 0 ? $list[$key]['explain_count'] : ceil($list[$key]['explain_count'] / $list[$key]['add_wechat_count']);
  5649. //话术浏览次数
  5650. $list[$key]['talkskill_view_count'] = isset($talk_count[$val['index']]) ? $talk_count[$val['index']] : 0;
  5651. //培训观看次数
  5652. $list[$key]['is_look'] = isset($looks[$val['index']]) ? $looks[$val['index']] : 0;
  5653. //培训观看时长
  5654. $list[$key]['look_time'] = isset($look_time[$val['index']]) ? $look_time[$val['index']] : 0;
  5655. //跟进次数
  5656. $list[$key]['log_count'] = isset($log1[$val['index']]) ? $log1[$val['index']] : 0;
  5657. //平均回访时长
  5658. $list[$key]['log_time'] = 0;
  5659. //一次到店客户数
  5660. $list[$key]['store_1'] = isset($s1[$val['index']]) ? $s1[$val['index']] : 0;
  5661. //二次到店客户数
  5662. $list[$key]['store_2'] = isset($s2[$val['index']]) ? $s2[$val['index']] : 0;
  5663. //三次到店客户数
  5664. $list[$key]['store_3'] = isset($s3[$val['index']]) ? $s3[$val['index']] : 0;
  5665. //跟进客户数量
  5666. $list[$key]['log_customer_count'] = isset($log2[$val['index']]) ? count(array_unique($log2[$val['index']])) : 0;
  5667. //量房客户数
  5668. $list[$key]['measuring_room_customer_count'] = isset($state_count1[$val['index']]) ? count(array_unique($state_count1[$val['index']])) : 0;
  5669. //到店客户数
  5670. $list[$key]['sto_customer_count'] = isset($state_count2[$val['index']]) ? count(array_unique($state_count2[$val['index']])) : 0;
  5671. //一次到店率
  5672. $list[$key]['store_1_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_1'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
  5673. //二次到店率
  5674. $list[$key]['store_2_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_2'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
  5675. //三次及以上到店率
  5676. $list[$key]['store_3_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_3'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
  5677. //交定
  5678. $list[$key]['deposit_customer_count'] = isset($state_count3[$val['index']]) ? count(array_unique($state_count3[$val['index']])) : 0;
  5679. // 一次到店交定客户数量
  5680. $list[$key]['store_1_dep'] = $list[$key]['deposit_customer_count'] == 0 ? 0 : count(array_intersect($state_count3[$val['index']], $cus1));
  5681. //一次到店交定率
  5682. $list[$key]['store_1_dep_grawth'] = $list[$key]['sto_customer_count'] == 0 ? '0%' : round($list[$key]['store_1_dep'] / $list[$key]['sto_customer_count'] * 100, 2) . '%';
  5683. //交定率
  5684. $list[$key]['deposit_customer_grawth'] = $list[$key]['log_customer_count'] == 0 ? '0%' : round($list[$key]['deposit_customer_count'] / $list[$key]['log_customer_count'] * 100, 2) . '%';
  5685. //签单
  5686. $list[$key]['sign_customer_count'] = isset($state_count4[$val['index']]) ? count(array_unique($state_count4[$val['index']])) : 0;
  5687. //预计转施工客户数量
  5688. $list[$key]['house_delivery_count'] = isset($house_delivery_time[$val['index']]) ? $house_delivery_time[$val['index']] : 0;
  5689. //预计到店数
  5690. $list[$key]['subscribe_store_count'] = isset($sub1[$val['index']]) ? $sub1[$val['index']] : 0;
  5691. //预计量房数量
  5692. $list[$key]['subscribe_room_count'] = isset($sub2[$val['index']]) ? $sub2[$val['index']] : 0;
  5693. //考核次数
  5694. $list[$key]['is_exam'] = isset($don1[$val['index']]) ? $don1[$val['index']] : 0;
  5695. //考核通过次数
  5696. $list[$key]['is_exam_ok'] = isset($don2[$val['index']]) ? $don2[$val['index']] : 0;
  5697. //回访次数 从客户报备开始截至到日期的回访次数,一天按一次计算
  5698. //平均回访周期
  5699. // $list[$key]['log_time1'] = 0;
  5700. }
  5701. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $limit)]);
  5702. }
  5703. /**
  5704. * 加微客户页面
  5705. */
  5706. public function addWechat()
  5707. {
  5708. $root_id = request()->employee->root_id;
  5709. $index = input('index', '');
  5710. View::assign('index', $index);
  5711. $type = input('type', '');
  5712. if ($type == 'customerLog') {
  5713. //跟进记录
  5714. $field = [
  5715. ['field' => 'name', 'title' => '姓名'],
  5716. ['field' => 'addtime', 'title' => '跟进时间']
  5717. ];
  5718. $url = url("statistics/customerLog", ["index" => $index]);
  5719. } elseif ($type == 'customerLogs') {
  5720. //跟进记录
  5721. $field = [
  5722. ['field' => 'name', 'title' => '姓名'],
  5723. ['field' => 'addtime', 'title' => '回访时间']
  5724. ];
  5725. $url = url("statistics/customerLog", ["index" => $index, 'type' => 'log']);
  5726. } elseif ($type == 'room') {
  5727. //量房客户
  5728. $field = [
  5729. ['field' => 'name', 'title' => '姓名'],
  5730. ['field' => 'addtime', 'title' => '量房时间']
  5731. ];
  5732. $url = url("statistics/customerLog", ["index" => $index, 'type' => 'room']);
  5733. } elseif (in_array($type, ['store1', 'store2'])) {
  5734. //一次到店两次到店客户
  5735. $field = [
  5736. ['field' => 'name', 'title' => '姓名'],
  5737. ['field' => 'addtime', 'title' => '到店时间']
  5738. ];
  5739. $url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
  5740. } elseif ($type == 'store_dep') {
  5741. //到店交定客户
  5742. $field = [
  5743. ['field' => 'name', 'title' => '姓名'],
  5744. ['field' => 'store_time', 'title' => '到店时间'],
  5745. ['field' => 'dep_time', 'title' => '签单时间']
  5746. ];
  5747. $url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
  5748. } elseif ($type == 'dep') {
  5749. //交定客户
  5750. $field = [
  5751. ['field' => 'name', 'title' => '姓名'],
  5752. ['field' => 'addtime', 'title' => '签单时间']
  5753. ];
  5754. $url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
  5755. } elseif ($type == 'explain_count') {
  5756. //智慧屏讲解客户列表
  5757. $field = [
  5758. ['field' => 'name', 'title' => '姓名'],
  5759. ['field' => 'phone', 'title' => '电话'],
  5760. ['field' => 'addtime', 'title' => '讲解时间'],
  5761. ];
  5762. $url = url("statistics/customerLog", ["index" => $index, 'type' => $type]);
  5763. } else {
  5764. //加微客户
  5765. $field = [
  5766. ['field' => 'name', 'title' => '姓名'],
  5767. ['field' => 'phone', 'title' => '电话'],
  5768. ['field' => 'date', 'title' => '加微日期'],
  5769. ];
  5770. $url = url("statistics/addWechatList", ["index" => $index]);
  5771. }
  5772. View::assign('fields', json_encode($field));
  5773. View::assign('url', $url);
  5774. return View::fetch();
  5775. }
  5776. /**
  5777. * 加微客户列表接口
  5778. */
  5779. public function addWechatList()
  5780. {
  5781. $param = Request::only(['page' => 1, 'limit' => 10, 'index' => '']);
  5782. $root_id = request()->employee->root_id;
  5783. $arr = explode('@', $param['index']);
  5784. $time = $arr[1];
  5785. $where = [
  5786. ['employee_id', '=', $arr[0]],
  5787. ['add_wechat_time', 'like', $time]
  5788. ];
  5789. $list = Customer::where($where)->field('phone,name,add_wechat_time date')->page($param['page'], $param['limit'])->select()->toArray();
  5790. foreach ($list as $k => $v) {
  5791. $list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
  5792. }
  5793. $count = Customer::where($where)->count();
  5794. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  5795. }
  5796. /**
  5797. * 跟进列表接口
  5798. */
  5799. public function customerLog()
  5800. {
  5801. $param = Request::only(['page' => 1, 'limit' => 10, 'index' => '', 'type' => '']);
  5802. $root_id = request()->employee->root_id;
  5803. $arr = explode('@', $param['index']);
  5804. $where = [
  5805. ['employee_id|customer_employee_id', '=', $arr[0]],
  5806. ['addtime', 'like', '%' . $arr[1] . '%']
  5807. ];
  5808. if ($param['type'] == 'log') {
  5809. // 跟进记录
  5810. $field = 'addtime,customer_id,state';
  5811. } elseif ($param['type'] == 'room') {
  5812. //量房客户
  5813. $field = 'max(addtime) addtime,customer_id,state';
  5814. $where[] = ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')];
  5815. } elseif (in_array($param['type'], ['store1', 'store2'])) {
  5816. //一次到店客户 , 二次到店客户
  5817. $field = "max(addtime) addtime,customer_id,state";
  5818. $where[] = ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')];
  5819. } elseif ($param['type'] == 'store_dep') {
  5820. //一次到店交定
  5821. $field = 'id,name';
  5822. } elseif ($param['type'] == 'dep') {
  5823. //交定
  5824. $where[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  5825. $field = 'max(addtime) addtime,customer_id,state';
  5826. } elseif ($param['type'] == 'explain_count') {
  5827. //智慧屏讲解客户列表
  5828. $where[] = ['remark', 'like', '%讲解智慧屏##%'];
  5829. $field = 'id,addtime,customer_id';
  5830. } else {
  5831. //跟进客户
  5832. $field = 'max(addtime) addtime,customer_id,state';
  5833. }
  5834. if ($param['type'] != 'store_dep' || $param['type'] == 'explain_count') {
  5835. $list = CustomerVisitLog::with(['customer' => function ($query) {
  5836. $query->field('id,name,phone')->bind(['name' => 'name', 'phone' => 'phone']);
  5837. }])->where($where)->field($field);
  5838. }
  5839. if (in_array($param['type'], ['', 'room', 'dep'])) {
  5840. $list = $list->group('customer_id');
  5841. } elseif (in_array($param['type'], ['store1', 'store2'])) {
  5842. $c = $param['type'] == 'store1' ? 1 : 2;
  5843. $list = $list->group('customer_id')->having('count(customer_id)=' . $c);
  5844. } elseif ($param['type'] == 'store_dep') {
  5845. //一次到店
  5846. $store = CustomerVisitLog::where(array_merge($where, [['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]]))->group('customer_id')->having('count(customer_id)=1')->column('addtime', 'customer_id');
  5847. //交定
  5848. $dep = CustomerVisitLog::where(array_merge($where, [['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]]))->column('addtime', 'customer_id');
  5849. $cids = array_intersect(array_keys($store), array_keys($dep));
  5850. $list = Customer::where([['id', 'in', $cids]])->field($field);
  5851. }
  5852. $count = $list->count();
  5853. $list = $list->page($param['page'], $param['limit'])->select()->toArray();
  5854. if ($param['type'] == 'store_dep') {
  5855. foreach ($list as $k => $v) {
  5856. $list[$k]['store_time'] = $store[$v['id']];
  5857. $list[$k]['dep_time'] = $dep[$v['id']];
  5858. }
  5859. } elseif ($param['type'] == 'explain_count') {
  5860. foreach ($list as $k => $v) {
  5861. $list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
  5862. $list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
  5863. }
  5864. }
  5865. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  5866. }
  5867. /**
  5868. * 管理行为统计页面
  5869. */
  5870. public function manageStatistics()
  5871. {
  5872. $root_id = request()->employee->root_id;
  5873. //房屋状态是否开启
  5874. $house_status_where = [
  5875. ['root_id', '=', $root_id],
  5876. ['keyname', '=', 'house_status'],
  5877. ['status', '=', 0]
  5878. ];
  5879. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  5880. $field = [];
  5881. if (!$house_status_per->isEmpty()) {
  5882. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  5883. $fields = [];
  5884. foreach ($field as $v) {
  5885. $fields[] = [
  5886. 'field' => 'house_status_' . $v['id'],
  5887. 'title' => $v['name'],
  5888. 'align' => 'center',
  5889. 'width' => 100,
  5890. 'rowspan' => 2
  5891. ];
  5892. }
  5893. $field = $fields;
  5894. }
  5895. View::assign('field', json_encode($field));
  5896. //返回组织列表
  5897. $org = Org::where([['path', 'like', $this->root_id . '-%']])->select()->toArray();
  5898. View::assign('org', $org);
  5899. return View::fetch();
  5900. }
  5901. /**
  5902. * 管理层数据统计
  5903. */
  5904. public function manageStatisticsData($page = 1, $limit = 10)
  5905. {
  5906. $param = Request::only(['page' => 1, 'start_date' => '', 'end_date' => '', 'keyword' => '', 'org_id' => '']);
  5907. //日期搜索//2023-02-15页面逻辑修改 传参方式改为 start_date=2023/2/15 - 2023/2/15
  5908. if ($param['start_date']) {
  5909. $count_dates = explode(' - ', $param['start_date']);
  5910. $param['start_date'] = date('Y-m-d 00:00:00', strtotime($count_dates[0]));
  5911. $param['end_date'] = date('Y-m-d 23:59:59', strtotime($count_dates[1]));
  5912. } else {
  5913. $param['start_date'] = $param['end_date'] = '';
  5914. }
  5915. $query_date = ($param['start_date'] && $param['end_date']) ? [['addtime', 'between', [$param['start_date'], $param['end_date']]]] : [];
  5916. $root_id = request()->employee->root_id;
  5917. $where = [
  5918. ['root_id', '=', $root_id],
  5919. ['uid', '>', 0],
  5920. ['state', '=', '在职']
  5921. // ['is_manager', '=', 1]
  5922. ];
  5923. if ($param['start_date'] && $param['end_date']) {
  5924. $sdate = strtotime($param['start_date']);
  5925. $edate = strtotime($param['end_date']);
  5926. } else {
  5927. $edate = time();
  5928. $sdate = 972473642;
  5929. }
  5930. if ($param['keyword']) {
  5931. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  5932. }
  5933. if ($param['org_id']) {
  5934. $o = Org::where([['id', '=', $param['org_id']]], ['path', 'like', $this->root_id . '-%'])->find();
  5935. $orgIds = Org::where([['path', 'like', $o['path'] . '%']])->column('id');
  5936. $where[] = ['org_id', 'in', $orgIds];
  5937. }
  5938. $employee_date = [$param['start_date'], $param['end_date']];
  5939. $list = Employee::with(['customer' => function ($query) use ($employee_date) {
  5940. //手动报备查addtime,分配查询employee_time
  5941. if ($employee_date[0] && $employee_date[1]) {
  5942. $e_where1 = [
  5943. ['addtime', 'between', [$employee_date[0], $employee_date[1]]],
  5944. ['crm_res_id', '=', 0]
  5945. ];
  5946. $e_where2 = [
  5947. ['crm_res_id', '>', 0],
  5948. ['employee_time', 'between', [$employee_date[0], $employee_date[1]]]
  5949. ];
  5950. $query->whereOr([$e_where1, $e_where2])->field('id,employee_id,ext,is_resource,addtime,signed_money,house_type,square,house_status,return_visit,assigned_personnel');
  5951. } else {
  5952. $query->field('id,employee_id,ext,is_resource,addtime,signed_money,house_type,square,house_status,return_visit,assigned_personnel');
  5953. }
  5954. }])->where($where)->field('id,org_id,name')->page($page, $limit)->select()->toArray();
  5955. $count = Employee::where($where)->count();
  5956. //客户ID
  5957. $customer_ids = [];
  5958. foreach ($list as $key => $val) {
  5959. $cid = array_column($val['customer'], 'id');
  5960. $customer_ids = array_merge($customer_ids, $cid);
  5961. }
  5962. //2023-02-21 有效客户查询
  5963. $state1 = Customer::changeState('待确认', 'chaos');
  5964. $state2 = Customer::changeState('无效', 'chaos');
  5965. $queryv1 = [['state', 'not in', array_merge($state1, $state2)], ['id', 'in', $customer_ids]];
  5966. $queryv2 = [['state', 'in', $state1], ['crm_res_id', 'null', null], ['id', 'in', $customer_ids]];
  5967. $valid_vislog = Customer::whereOr([$queryv1, $queryv2])->column('id');
  5968. //最近跟进时间
  5969. $max_addtime = CustomerVisitLog::where([['is_merge', '=', 0], ['customer_id', 'in', $customer_ids]])->group('customer_id')->column('max(addtime) addtime', 'customer_id');
  5970. //查询有效数量 //有效性判断 ,, //有效状态 未到访,已到访,确定到场,已量房,交定,签单
  5971. $state1 = CustomerVisitLog::changeState('未到访', 'chaos');
  5972. $state2 = CustomerVisitLog::changeState('已到访', 'chaos');
  5973. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  5974. $state4 = CustomerVisitLog::changeState('已到店', 'chaos');
  5975. $state5 = CustomerVisitLog::changeState('已交定', 'chaos');
  5976. $state6 = CustomerVisitLog::changeState('已签单', 'chaos');
  5977. $state7 = CustomerVisitLog::changeState('已到场', 'chaos');
  5978. $vis_where[] = ['customer_id', 'in', $customer_ids];
  5979. $vis_where[] = ['state', 'in', array_merge($state1, $state2, $state3, $state4, $state5, $state6, $state7)];
  5980. if($query_date) $vis_where[] = $query_date[0];
  5981. $vislog_list = CustomerVisitLog::where($vis_where)->order('addtime asc')->column('customer_id,state,addtime,next_contact_date');
  5982. // $valid_vislog = array_unique(array_column($vislog_list, 'customer_id'));
  5983. $rooms = $store = $dep = $sign = [];
  5984. foreach ($vislog_list as $vislog_list_item) {
  5985. if (in_array($vislog_list_item['state'], $state3)) {
  5986. $rooms[] = $vislog_list_item['customer_id'];
  5987. } elseif (in_array($vislog_list_item['state'], $state4)) {
  5988. @$store[$vislog_list_item['customer_id']][] = $vislog_list_item['addtime'];
  5989. } elseif (in_array($vislog_list_item['state'], $state5)) {
  5990. @$dep[$vislog_list_item['customer_id']][] = $vislog_list_item['addtime'];
  5991. } elseif (in_array($vislog_list_item['state'], $state6)) {
  5992. $sign[] = $vislog_list_item['customer_id'];
  5993. }
  5994. }
  5995. $rooms = array_unique($rooms); //量房客户
  5996. //到店客户
  5997. $store1 = $store2 = $store3 = [];
  5998. foreach ($store as $store_key => $store_val) {
  5999. if (count($store_val) == 1) {
  6000. $store1[] = $store_key;
  6001. } elseif (count($store_val) == 2) {
  6002. $store2[] = $store_key;
  6003. } else {
  6004. $store3[] = $store_key;
  6005. }
  6006. }
  6007. //交定客户
  6008. $dep1 = $dep2 = $dep3 = [];
  6009. foreach ($dep as $dep_key => $dep_item) {
  6010. if (count($dep_item) == 1) {
  6011. $dep1[] = $dep_key;
  6012. } elseif (count($dep_item) == 2) {
  6013. $dep2[] = $dep_key;
  6014. } else {
  6015. $dep3[] = $dep_key;
  6016. }
  6017. }
  6018. //房屋状态是否开启
  6019. $house_status_where = [
  6020. ['root_id', '=', $root_id],
  6021. ['keyname', '=', 'house_status'],
  6022. ['status', '=', 0]
  6023. ];
  6024. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  6025. $field = [];
  6026. if (!$house_status_per->isEmpty()) {
  6027. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  6028. }
  6029. // 电话统计
  6030. $outCallLog = OutCallLog::where([['root_id', '=', $root_id]])->where($query_date)->field("count(id) as phone_count, sum(if(`status`>0,1,0)) as on_phone_count, sum(if(`status`<0,1,0)) as off_phone_count,sum(billsec) as phone_time,employee_id")->group('employee_id')->select()->toArray();
  6031. $outCallLog = array_combine(array_column($outCallLog, 'employee_id'), $outCallLog);
  6032. // 手机打电话次数获取
  6033. $outCallMbLog = OutCallMbLog::where([
  6034. ['root_id', '=', $root_id]
  6035. ])->field("count(id) as phone_count,employee_id")->group('employee_id')->select()->toArray();
  6036. $outCallMbLog = array_combine(array_column($outCallMbLog, 'employee_id'), $outCallMbLog);
  6037. foreach ($list as $k => $v) {
  6038. //客户ID
  6039. $cids = array_column($v['customer'], 'id');
  6040. //客户数量
  6041. $list[$k]['customer_count'] = count($cids);
  6042. //待回访
  6043. $list[$k]['return_visit_count'] = count(array_filter(array_column($v['customer'], 'return_visit')));
  6044. //待指派
  6045. $assigned_personnel = count(array_filter(array_column($v['customer'], 'assigned_personnel')));
  6046. $list[$k]['no_assigned_personnel'] = $list[$k]['customer_count'] - $assigned_personnel;
  6047. //三天,七天,十五天,三十天,三十天以上未跟进
  6048. $list[$k]['no_visit_3'] = $list[$k]['no_visit_7'] = $list[$k]['no_visit_15'] = $list[$k]['no_visit_30'] = $list[$k]['no_visit_31'] = 0;
  6049. foreach ($v['customer'] as $customer_item) {
  6050. //最近跟进时间
  6051. $visit_time = isset($max_addtime[$customer_item['id']]) ? $max_addtime[$customer_item['id']] : $customer_item['addtime'];
  6052. $day = ceil((time() - strtotime($visit_time)) / 86400);
  6053. if ($day <= 3 && $day > 1) {
  6054. $list[$k]['no_visit_3'] += 1;
  6055. } elseif ($day <= 7 && $day > 3) {
  6056. $list[$k]['no_visit_7'] += 1;
  6057. } elseif ($day < 15 && $day > 7) {
  6058. $list[$k]['no_visit_15'] += 1;
  6059. } elseif ($day <= 30 && $day > 15) {
  6060. $list[$k]['no_visit_30'] += 1;
  6061. } elseif ($day > 30) {
  6062. $list[$k]['no_visit_31'] += 1;
  6063. }
  6064. }
  6065. //有效客户
  6066. $valid = array_intersect($cids, $valid_vislog);
  6067. //有效客户数量
  6068. $list[$k]['valid_count'] = count($valid);
  6069. //有效率
  6070. $list[$k]['valid_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['valid_count']);
  6071. //电话量
  6072. $list[$k]['phone_count'] = 0;
  6073. //接通量
  6074. $list[$k]['on_phone_count'] = 0;
  6075. //接通率
  6076. $list[$k]['on_phone_grawth'] = 0;
  6077. //未接通
  6078. $list[$k]['off_phone_count'] = 0;
  6079. //平均通话时长
  6080. $list[$k]['avg_phone_time'] = 0;
  6081. if (isset($outCallLog[$v['id']])) {
  6082. //电话量
  6083. $list[$k]['phone_count'] = $outCallLog[$v['id']]['phone_count'];
  6084. //接通量
  6085. $list[$k]['on_phone_count'] = $outCallLog[$v['id']]['on_phone_count'];
  6086. //接通率
  6087. $list[$k]['on_phone_grawth'] = $this->grawth($list[$k]['phone_count'], $list[$k]['on_phone_count']);
  6088. //未接通
  6089. $list[$k]['off_phone_count'] = $outCallLog[$v['id']]['off_phone_count'];
  6090. //平均通话时长
  6091. $list[$k]['avg_phone_time'] = $list[$k]['on_phone_count'] == 0 ? 0 : round($outCallLog[$v['id']]['phone_time'] / $list[$k]['on_phone_count'], 2);
  6092. }
  6093. if (isset($outCallMbLog[$v['id']])) {
  6094. //电话量
  6095. $list[$k]['phone_count'] += $outCallMbLog[$v['id']]['phone_count'];
  6096. }
  6097. $customer = $this->addWechatCount($v['customer'], $sdate, $edate);
  6098. //加微数量
  6099. $list[$k]['add_wechat_count'] = count($customer['wechat_count']);
  6100. //出方案客户数
  6101. $list[$k]['drawing_date'] = count($customer['plan_issuing_date']);
  6102. //加微率
  6103. $list[$k]['add_wechat_grawth'] = $this->grawth($list[$k]['valid_count'], $list[$k]['add_wechat_count']);
  6104. //量房客户数
  6105. $list[$k]['room'] = count(array_intersect($rooms, $cids));
  6106. //量房率
  6107. $list[$k]['room_grawth'] = $this->grawth($list[$k]['valid_count'], $list[$k]['room']);
  6108. //到店客户数
  6109. $store1 = array_intersect($store1, $cids);
  6110. $store2 = array_intersect($store2, $cids);
  6111. $store3 = array_intersect($store3, $cids);
  6112. $list[$k]['store1'] = count($store1);
  6113. $list[$k]['store2'] = count($store2);
  6114. $list[$k]['store3'] = count($store3);
  6115. $list[$k]['store1_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store1']);
  6116. $list[$k]['store2_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store2']);
  6117. $list[$k]['store3_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store3']);
  6118. //交定客户数
  6119. $z_dep = array_intersect(array_keys($dep), $cids);
  6120. $dep1 = array_intersect($dep1, $cids);
  6121. $dep2 = array_intersect($dep2, $cids);
  6122. $dep3 = array_intersect($dep3, $cids);
  6123. $list[$k]['dep1'] = count($dep1);
  6124. $list[$k]['dep2'] = count($dep2);
  6125. $list[$k]['dep3'] = count($dep3);
  6126. $list[$k]['dep1_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep1']);
  6127. $list[$k]['dep2_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep2']);
  6128. $list[$k]['dep3_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep3']);
  6129. $list[$k]['dep'] = count($z_dep);
  6130. $list[$k]['dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['dep']);
  6131. //一次到店签单客户数
  6132. $list[$k]['store1_dep'] = count(array_intersect($z_dep, $store1));
  6133. $list[$k]['store1_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store1_dep']);
  6134. $list[$k]['store2_dep'] = count(array_intersect($z_dep, $store2));
  6135. $list[$k]['store2_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store2_dep']);
  6136. $list[$k]['store3_dep'] = count(array_intersect($z_dep, $store3));
  6137. $list[$k]['store3_dep_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['store3_dep']);
  6138. //签单
  6139. $list[$k]['sign_count'] = count(array_intersect($cids, $sign));
  6140. $list[$k]['sign_grawth'] = $this->grawth($list[$k]['customer_count'], $list[$k]['sign_count']);
  6141. //面积
  6142. $list[$k]['square_0_80'] = $customer['square_0_80']; //
  6143. $list[$k]['square_80_100'] = $customer['square_80_100']; //
  6144. $list[$k]['square_100_120'] = $customer['square_100_120']; //
  6145. $list[$k]['square_120_200'] = $customer['square_120_200']; //
  6146. $list[$k]['square_200_500'] = $customer['square_200_500']; //
  6147. $list[$k]['square_500'] = $customer['square_500']; //
  6148. //房屋类型
  6149. $list[$k]['existing_homes_count'] = $customer['existing_homes_count']; //现房数量
  6150. $list[$k]['forward_housing_count'] = $customer['forward_housing_count']; //期房数量
  6151. //房屋状态
  6152. if ($field) {
  6153. $house_status = $this->getHouseStatus($customer['house_status'], $field);
  6154. $list[$k] = array_merge($list[$k], $house_status);
  6155. }
  6156. unset($list[$k]['customer']);
  6157. }
  6158. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功', 'page' => ceil($count / $limit)]);
  6159. }
  6160. /**
  6161. * 率
  6162. */
  6163. private function grawth($zcount, $count)
  6164. {
  6165. $res = $zcount == 0 ? '0%' : round($count / $zcount * 100, 2) . '%';
  6166. return $res;
  6167. }
  6168. /**
  6169. * 加微数量
  6170. */
  6171. private function addWechatCount($wechat, $sdate, $edate)
  6172. {
  6173. $data['wechat_count'] = [];
  6174. $data['plan_issuing_date'] = []; //出方案时间
  6175. $square = $house_type = [];
  6176. $data['house_status'] = [];
  6177. foreach ($wechat as $v) {
  6178. $data['house_status'][] = $v['house_status'];
  6179. $square[] = $v['square'];
  6180. $house_type[] = $v['house_type'];
  6181. //客户添加时间
  6182. $addtime = strtotime($v['addtime']);
  6183. //加微客户,有户型图客户
  6184. $v2 = $v['ext'];
  6185. $ext = json_decode($v2, true);
  6186. if (!empty($ext) && is_array($ext)) {
  6187. $ext = array_filter($ext);
  6188. foreach ($ext as $e) {
  6189. if (isset($e['keyname']) && $e['keyname'] == 'add_wechat_time' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
  6190. $data['wechat_count'][] = $v['id'];
  6191. } elseif (isset($e['keyname']) && $e['keyname'] == 'drawing_date' && !empty($e['value']) && strtotime($e['value']) >= $sdate && strtotime($e['value']) <= $edate) {
  6192. $data['drawing_date'][] = $v['id'];
  6193. }
  6194. }
  6195. }
  6196. }
  6197. //面积
  6198. //房屋面积
  6199. $data['square_0_80'] = $data['square_80_100'] = $data['square_100_120'] = $data['square_120_200'] = $data['square_200_500'] = $data['square_500'] = 0;
  6200. foreach ($square as $square_item) {
  6201. if (!$square_item) continue; //不填不计算
  6202. if ($square_item <= 80) {
  6203. $data['square_0_80'] += 1;
  6204. } elseif ($square_item <= 100) {
  6205. $data['square_80_100'] += 1;
  6206. } elseif ($square_item <= 120) {
  6207. $data['square_100_120'] += 1;
  6208. } elseif ($square_item <= 200) {
  6209. $data['square_120_200'] += 1;
  6210. } elseif ($square_item <= 500) {
  6211. $data['square_200_500'] += 1;
  6212. } else {
  6213. $data['square_500'] += 1;
  6214. }
  6215. }
  6216. //房屋类型
  6217. $data['existing_homes_count'] = 0; //现房数量
  6218. $data['forward_housing_count'] = 0; //期房数量
  6219. foreach ($house_type as $house_type_item) {
  6220. if (strpos($house_type_item, '现房') !== false) {
  6221. $data['existing_homes_count'] += 1;
  6222. } elseif (strpos($house_type_item, '期房') !== false) {
  6223. $data['forward_housing_count'] += 1;
  6224. }
  6225. }
  6226. return $data;
  6227. }
  6228. /**
  6229. * 弹框页面
  6230. */
  6231. public function manageOpen()
  6232. {
  6233. $root_id = request()->employee->root_id;
  6234. $eid = input('eid',);
  6235. View::assign('eid', $eid);
  6236. $type = input('type', '');
  6237. $start_date = input('start_date', '');
  6238. $end_date = input('end_date', '');
  6239. if ($type == 'assign') {
  6240. //待指派
  6241. $field = [
  6242. ['field' => 'name', 'title' => '姓名'],
  6243. ['field' => 'phone', 'title' => '电话']
  6244. ];
  6245. $url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  6246. } else if (in_array($type, ['v3', 'v7', 'v15', 'v30', 'v31'])) {
  6247. //天未指派
  6248. $field = [
  6249. ['field' => 'name', 'title' => '姓名'],
  6250. ['field' => 'phone', 'title' => '电话']
  6251. ];
  6252. $url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  6253. } else if (in_array($type, ['valid', 'wechat', 'room'])) {
  6254. //天未指派
  6255. $field = [
  6256. ['field' => 'name', 'title' => '姓名'],
  6257. ['field' => 'phone', 'title' => '电话']
  6258. ];
  6259. $url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  6260. } elseif ($type == 'invalid') {
  6261. $key = input('key', '');
  6262. //无效客资
  6263. $field = [
  6264. ['field' => 'name', 'title' => '姓名'],
  6265. ['field' => 'phone', 'title' => '电话']
  6266. ];
  6267. $url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date, 'key' => $key]);
  6268. } elseif ($type == 'number_of_explanations') {
  6269. //智慧屏讲解
  6270. $field = [
  6271. ['field' => 'name', 'title' => '姓名'],
  6272. ['field' => 'phone', 'title' => '电话'],
  6273. ['field' => 'addtime', 'title' => '讲解时间']
  6274. ];
  6275. $url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  6276. } else {
  6277. //待回访客户
  6278. $field = [
  6279. ['field' => 'name', 'title' => '姓名'],
  6280. ['field' => 'phone', 'title' => '电话']
  6281. ];
  6282. $url = url("statistics/manageOpenData", ["eid" => $eid, 'type' => $type, 'end_date' => $end_date, 'start_date' => $start_date]);
  6283. }
  6284. View::assign('fields', json_encode($field));
  6285. View::assign('url', $url);
  6286. return View::fetch('add_wechat');
  6287. }
  6288. /**
  6289. * 弹框页面数据
  6290. */
  6291. public function manageOpenData()
  6292. {
  6293. $param = Request::only(['page' => 1, 'limit' => 10, 'eid' => 0, 'type' => '', 'start_date' => '', 'end_date' => '']);
  6294. $root_id = request()->employee->root_id;
  6295. $where = [
  6296. ['employee_id', '=', $param['eid']]
  6297. ];
  6298. if ($param['start_date'] && $param['end_date']) {
  6299. $where[] = ['addtime', 'between', [$param['start_date'], $param['end_date'] . ' 23:59:59']];
  6300. }
  6301. if ($param['type'] == 'assign') {
  6302. //待指派
  6303. $where[] = ['assigned_personnel', '=', NULL];
  6304. $list = Customer::where($where)->where(function ($query) {
  6305. $or1[] = ['crm_res_id', 'null', null];
  6306. $or2[] = ['crm_res_id', '>', 0];
  6307. $or2[] = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
  6308. $query->whereOr([$or1, $or2]);
  6309. })->field('name,phone');
  6310. } elseif (in_array($param['type'], ['v3', 'v7', 'v15', 'v30', 'v31'])) {
  6311. $arr = ['v3' => [1, 3], 'v7' => [3, 7], 'v15' => [7, 15], 'v30' => [15, 30], 'v31' => [30, 999999]];
  6312. //3天未回访
  6313. $cids = Customer::where($where)->column('addtime', 'id');
  6314. $max_addtime = CustomerVisitLog::where([['is_merge', '=', 0], ['customer_id', 'in', array_keys($cids)]])->group('customer_id')->column('max(addtime) addtime', 'customer_id');
  6315. $cid = [];
  6316. foreach ($cids as $k => $v) {
  6317. //最近跟进时间
  6318. $visit_time = isset($max_addtime[$k]) ? $max_addtime[$k] : $v;
  6319. $day = ceil((time() - strtotime($visit_time)) / 86400);
  6320. if ($day > $arr[$param['type']][0] && $day <= $arr[$param['type']][1]) {
  6321. $cid[] = $k;
  6322. }
  6323. }
  6324. if (empty($cid)) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
  6325. $where[] = ['id', 'in', $cid];
  6326. $list = Customer::where($where)->field('name,phone');
  6327. } elseif ($param['type'] == 'valid') {
  6328. //有效客户数量
  6329. $state1 = Customer::changeState('待确认', 'chaos');
  6330. $state2 = Customer::changeState('无效', 'chaos');
  6331. $query1 = [['state', 'not in', array_merge($state1, $state2)], ['employee_id', '=', $param['eid']]];
  6332. $query2 = [['state', 'in', $state1], ['crm_res_id', 'null', null], ['employee_id', '=', $param['eid']]];
  6333. $ids = Customer::whereOr([$query1, $query2])->column('id');
  6334. if ($param['start_date'] && $param['end_date']) {
  6335. $query1 = [['id', 'in', $ids], ['crm_res_id', 'null', null], ['addtime', 'between', [$param['start_date'], $param['end_date']]]];
  6336. $query2 = [['id', 'in', $ids], ['crm_res_id', 'not null', null], ['employee_time', 'between', [$param['start_date'], $param['end_date']]]];
  6337. $list = Customer::whereOr([$query1, $query2])->field('name,phone');
  6338. } else {
  6339. $list = Customer::where([['id', 'in', $ids]])->field('name,phone');
  6340. }
  6341. } elseif ($param['type'] == 'wechat' || $param['type'] == 'drawing') {
  6342. $field = $param['type'] == 'wechat' ? 'add_wechat_time' : 'drawing_date';
  6343. //加微客户
  6344. $where1[] = ['ext', 'like', '%' . $field . '%'];
  6345. $ext = Customer::where(array_merge($where, $where1))->column('id,ext');
  6346. $cid = [];
  6347. foreach ($ext as $v) {
  6348. $json = json_decode($v['ext'], true);
  6349. foreach ($json as $v2) {
  6350. if ($v2['keyname'] == $field && !empty($v2['value'])) {
  6351. $cid[] = $v['id'];
  6352. }
  6353. }
  6354. }
  6355. $where[] = ['id', 'in', $cid];
  6356. $list = Customer::where($where)->field('name,phone');
  6357. } elseif ($param['type'] == 'room') {
  6358. //量房客户
  6359. $customer_ids = Customer::where($where)->column('id');
  6360. $state3 = CustomerVisitLog::changeState('已量房', 'chaos');
  6361. $vis_where[] = ['customer_id', 'in', $customer_ids];
  6362. $vis_where[] = ['state', 'in', $state3];
  6363. $cid = CustomerVisitLog::where($vis_where)->group('customer_id')->column('customer_id');
  6364. $where[] = ['id', 'in', $cid];
  6365. $list = Customer::where($where)->field('name,phone');
  6366. } elseif (in_array($param['type'], ['store1', 'store2', 'store3'])) {
  6367. $having = ['store1' => 'count(id)=1', 'store2' => 'count(id)=2', 'store3' => 'count(id)>=3'];
  6368. //一次到店客户数
  6369. $customer_ids = Customer::where($where)->column('id');
  6370. $state = CustomerVisitLog::changeState('已到店', 'chaos');
  6371. $vis_where[] = ['customer_id', 'in', $customer_ids];
  6372. $vis_where[] = ['state', 'in', $state];
  6373. $cid = CustomerVisitLog::where($vis_where)->group('customer_id')->having($having[$param['type']])->column('customer_id');
  6374. $where[] = ['id', 'in', $cid];
  6375. $list = Customer::where($where)->field('name,phone');
  6376. } elseif (in_array($param['type'], ['store1_dep', 'store2_dep', 'store3_dep'])) {
  6377. $having = ['store1_dep' => 'count(id)=1', 'store2_dep' => 'count(id)=2', 'store3_dep' => 'count(id)>=3'];
  6378. //一次到店客户数
  6379. $customer_ids = Customer::where($where)->column('id');
  6380. $state = CustomerVisitLog::changeState('已到店', 'chaos');
  6381. $state1 = CustomerVisitLog::changeState('已交定', 'chaos');
  6382. $vis_where[] = ['customer_id', 'in', $customer_ids];
  6383. $vis_where[] = ['state', 'in', $state];
  6384. $c1 = CustomerVisitLog::where($vis_where)->group('customer_id')->having($having[$param['type']])->column('customer_id');
  6385. //交定
  6386. $vis_where1[] = ['customer_id', 'in', $customer_ids];
  6387. $vis_where1[] = ['state', 'in', $state1];
  6388. $c2 = CustomerVisitLog::where($vis_where1)->group('customer_id')->column('customer_id');
  6389. $where[] = ['id', 'in', array_intersect($c1, $c2)];
  6390. $list = Customer::where($where)->field('name,phone');
  6391. } elseif ($param['type'] == 'dep' || $param['type'] == 'sign_count') {
  6392. $state = $param['type'] == 'dep' ? '已交定' : '已签单';
  6393. $customer_ids = Customer::where($where)->column('id');
  6394. $state1 = CustomerVisitLog::changeState($state, 'chaos');
  6395. //交定
  6396. $vis_where1[] = ['customer_id', 'in', $customer_ids];
  6397. $vis_where1[] = ['state', 'in', $state1];
  6398. $c2 = CustomerVisitLog::where($vis_where1)->group('customer_id')->column('customer_id');
  6399. $where[] = ['id', 'in', $c2];
  6400. $list = Customer::where($where)->field('name,phone');
  6401. } elseif (in_array($param['type'], ['square_0_80', 'square_80_100', 'square_100_120', 'square_120_200', 'square_200_500', 'square_500'])) {
  6402. $arr = [
  6403. 'square_0_80' => [0, 80],
  6404. 'square_80_100' => [81, 100],
  6405. 'square_100_120' => [101, 120],
  6406. 'square_120_200' => [121, 200],
  6407. 'square_200_500' => [201, 500],
  6408. 'square_500' => [501, 999999],
  6409. ];
  6410. $square = Customer::where($where)->column('square,id');
  6411. $cid = [];
  6412. foreach ($square as $v) {
  6413. if ($v['square'] && $v['square'] > $arr[$param['type']][0] && $v['square'] <= $arr[$param['type']][1]) $cid[] = $v['id'];
  6414. }
  6415. $where[] = ['id', 'in', $cid];
  6416. $list = Customer::where($where)->field('name,phone');
  6417. } elseif ($param['type'] == 'existing_homes_count' || $param['type'] == 'forward_housing_count') {
  6418. //现房 期房客户
  6419. $field = $param['type'] == 'existing_homes_count' ? '现房' : '期房';
  6420. $where[] = ['house_type', 'like', '%' . $field . '%'];
  6421. $list = Customer::where($where)->field('name,phone');
  6422. } elseif ($param['type'] == 'invalid') {
  6423. $invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  6424. $arr = $invalid_customer ? explode(',', $invalid_customer) : ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  6425. $field = (int)input('key', '');
  6426. if (empty($arr) || !isset($arr[$field])) return json(['code' => 0, 'data' => [], 'count' => [], 'msg' => '获取成功']);
  6427. $status = $arr[$field];
  6428. $customer_ids = Customer::where($where)->column('id');
  6429. $state3 = CustomerVisitLog::where([['remark', 'like', '%' . $status . '%'], ['customer_id', 'in', $customer_ids]])->group('customer_id')->column('customer_id');
  6430. $where[] = ['id', 'in', $state3];
  6431. $list = Customer::where($where)->field('name,phone');
  6432. } elseif ($param['type'] == 'number_of_explanations') {
  6433. //智慧屏讲解客户列表
  6434. // $list = CustomerVisitLog::with(['custoemr'=>function($query){}])->where([['employee_id|customer_employee_id','=',$param['eid']],['remark','like','%讲解智慧屏##%']])->field('customer_id,addtime')->select()->toArray();
  6435. } else {
  6436. $where[] = ['return_visit', '=', 1];
  6437. $list = Customer::where($where)->field('name,phone');
  6438. }
  6439. $count = $list->count();
  6440. $list = $list->page($param['page'], $param['limit'])->select()->toArray();
  6441. foreach ($list as $k => $v) {
  6442. $list[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
  6443. $list[$k]['name'] = empty($v['name']) ? '未知' : $v['name'];
  6444. }
  6445. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  6446. }
  6447. /**
  6448. * 统计展示字段 网销端已增加type值到10,
  6449. */
  6450. public function statisticsFields($type)
  6451. {
  6452. // 行为统计
  6453. $type1 = [
  6454. 'x_date' => ['name' => '时间', 'show' => 1, 'width' => 150, 'sort' => 1],
  6455. 'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 2],
  6456. 'org_name' => ['name' => '所属部门', 'show' => 1, 'width' => 100, 'sort' => 3],
  6457. 'phone_count' => ['name' => '电话量', 'show' => 1, 'width' => 75, 'sort' => 4],
  6458. 'on_phone_count' => ['name' => '接通量', 'show' => 1, 'width' => 75, 'sort' => 5],
  6459. 'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 6],
  6460. 'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 130, 'sort' => 7],
  6461. 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 90, 'sort' => 8],
  6462. 'share_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'sort' => 9],
  6463. 'explain_count' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'sort' => 10],
  6464. 'talkskill_view_count' => ['name' => '浏览话术次数', 'show' => 1, 'width' => 130, 'sort' => 11],
  6465. 'is_look' => ['name' => '培训是否看了', 'show' => 1, 'width' => 120, 'sort' => 12],
  6466. 'is_exam' => ['name' => '是否有考核', 'show' => 1, 'width' => 110, 'sort' => 13],
  6467. 'is_exam_ok' => ['name' => '考核是否通过', 'show' => 1, 'width' => 130, 'sort' => 14],
  6468. 'log_customer_count' => ['name' => '客户跟进数', 'show' => 1, 'width' => 100, 'sort' => 15],
  6469. 'log_count' => ['name' => '总回访次数', 'show' => 1, 'width' => 100, 'sort' => 16],
  6470. 'subscribe_room_count' => ['name' => '预计量房客户数', 'show' => 1, 'width' => 150, 'sort' => 17],
  6471. 'subscribe_store_count' => ['name' => '预计到店客户数', 'show' => 1, 'width' => 150, 'sort' => 18],
  6472. 'measuring_room_customer_count' => ['name' => '量房客户数', 'show' => 1, 'width' => 120, 'sort' => 19],
  6473. 'sto_customer_count' => ['name' => '到店客户数', 'show' => 1, 'width' => 120, 'sort' => 20],
  6474. 'deposit_customer_count' => ['name' => '交定/签单客户数', 'show' => 1, 'width' => 150, 'sort' => 21],
  6475. 'house_delivery_count' => ['name' => '预计转施工客户数', 'show' => 1, 'width' => 150, 'sort' => 22],
  6476. 'sign_customer_count' => ['name' => '转单客户数', 'show' => 1, 'width' => 100, 'sort' => 23],
  6477. ];
  6478. // 高层统计
  6479. $type2 = [
  6480. 'x_date' => ['name' => '时间', 'show' => 1, 'width' => 150, 'sort' => 1],
  6481. 'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 2],
  6482. 'org_name' => ['name' => '所属部门', 'show' => 1, 'width' => 100, 'sort' => 3],
  6483. 'position' => ['name' => '职位', 'show' => 1, 'width' => 80, 'sort' => 4],
  6484. 'phone_count' => ['name' => '电话量', 'show' => 1, 'width' => 75, 'sort' => 5],
  6485. 'on_phone_count' => ['name' => '接通量', 'show' => 1, 'width' => 75, 'sort' => 6],
  6486. 'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 7],
  6487. 'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 130, 'sort' => 8],
  6488. 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 90, 'sort' => 9],
  6489. 'share_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'sort' => 10],
  6490. 'explain_count' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'sort' => 11],
  6491. 'explain_count1' => ['name' => '每个客户讲解次数', 'show' => 1, 'width' => 200, 'sort' => 12],
  6492. 'talkskill_view_count' => ['name' => '浏览话术次数', 'show' => 1, 'width' => 130, 'sort' => 13],
  6493. 'is_look' => ['name' => '看培训次数', 'show' => 1, 'width' => 120, 'sort' => 14],
  6494. 'look_time' => ['name' => '看培训时长(S)', 'show' => 1, 'width' => 150, 'sort' => 15],
  6495. 'is_exam' => ['name' => '是否有考核', 'show' => 1, 'width' => 110, 'sort' => 16],
  6496. 'is_exam_ok' => ['name' => '考核是否通过', 'show' => 1, 'width' => 130, 'sort' => 17],
  6497. 'log_customer_count' => ['name' => '客户跟进数', 'show' => 1, 'width' => 100, 'sort' => 18],
  6498. 'log_count' => ['name' => '总回访次数', 'show' => 1, 'width' => 100, 'sort' => 19],
  6499. 'log_time' => ['name' => '平均回访时长', 'show' => 1, 'width' => 150, 'sort' => 20],
  6500. 'log_time1' => ['name' => '平均回访周期', 'show' => 1, 'width' => 150, 'sort' => 21],
  6501. 'measuring_room_customer_count' => ['name' => '量房客户数', 'show' => 1, 'width' => 120, 'sort' => 22],
  6502. 'store_1' => ['name' => '一次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 23],
  6503. 'store_1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 125, 'sort' => 24],
  6504. 'store_2' => ['name' => '二次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 25],
  6505. 'store_2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 125, 'sort' => 26],
  6506. 'store_3' => ['name' => '三次及以上到店客户数', 'show' => 1, 'width' => 250, 'sort' => 27],
  6507. 'store_3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 200, 'sort' => 28],
  6508. 'store_1_dep' => ['name' => '一次到店签单客户数', 'show' => 1, 'width' => 225, 'sort' => 29],
  6509. 'store_1_dep_grawth' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 30],
  6510. 'deposit_customer_count' => ['name' => '总签单数', 'show' => 1, 'width' => 100, 'sort' => 31],
  6511. 'deposit_customer_grawth' => ['name' => '总签单率', 'show' => 1, 'width' => 100, 'sort' => 32],
  6512. ];
  6513. // 管理层统计
  6514. $type3 = [
  6515. 'name' => ['name' => '员工姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
  6516. 'customer_count' => ['name' => '客户数量', 'show' => 1, 'width' => 100, 'sort' => 2],
  6517. 'return_visit_count' => ['name' => '待回访客户数', 'show' => 1, 'width' => 150, 'sort' => 3],
  6518. 'no_assigned_personnel' => ['name' => '待指派客户数', 'show' => 1, 'width' => 150, 'sort' => 4],
  6519. 'no_visit_3' => ['name' => '3天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 5],
  6520. 'no_visit_7' => ['name' => '7天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 6],
  6521. 'no_visit_15' => ['name' => '15天内未跟进客户数', 'show' => 1, 'width' => 225, 'sort' => 7],
  6522. 'no_visit_30' => ['name' => '一个月内未跟进客户数', 'show' => 1, 'width' => 250, 'sort' => 8],
  6523. 'no_visit_31' => ['name' => '一个月以上未跟进客户数', 'show' => 1, 'width' => 275, 'sort' => 9],
  6524. 'valid_count' => ['name' => '有效客户', 'show' => 1, 'width' => 100, 'sort' => 10],
  6525. 'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 75, 'sort' => 11],
  6526. 'phone_count' => ['name' => '电话数量', 'show' => 1, 'width' => 100, 'sort' => 12],
  6527. 'on_phone_count' => ['name' => '接通数量', 'show' => 1, 'width' => 100, 'sort' => 13],
  6528. 'on_phone_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 75, 'sort' => 14],
  6529. 'off_phone_count' => ['name' => '未接通', 'show' => 1, 'width' => 75, 'sort' => 15],
  6530. 'avg_phone_time' => ['name' => '平均通话时长', 'show' => 1, 'width' => 150, 'sort' => 16],
  6531. 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'sort' => 17],
  6532. 'add_wechat_grawth' => ['name' => '加微率', 'show' => 1, 'width' => 75, 'sort' => 18],
  6533. 'room' => ['name' => '量房客户数', 'show' => 1, 'width' => 125, 'sort' => 19],
  6534. 'room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 75, 'sort' => 20],
  6535. 'store1' => ['name' => '一次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 21],
  6536. 'store1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 125, 'sort' => 22],
  6537. 'store2' => ['name' => '二次到店客户数', 'show' => 1, 'width' => 175, 'sort' => 23],
  6538. 'store2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 125, 'sort' => 24],
  6539. 'store3' => ['name' => '三次及以上到店客户数', 'show' => 1, 'width' => 250, 'sort' => 25],
  6540. 'store3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 200, 'sort' => 26],
  6541. 'store1_dep' => ['name' => '一次到店签单客户数', 'show' => 1, 'width' => 225, 'sort' => 27],
  6542. 'store1_dep_grawth' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 28],
  6543. 'store2_dep' => ['name' => '二次到店签单数', 'show' => 1, 'width' => 175, 'sort' => 29],
  6544. 'store2_dep_grawth' => ['name' => '二次到店签单率', 'show' => 1, 'width' => 175, 'sort' => 30],
  6545. 'store3_dep' => ['name' => '三次及以上到店签单数', 'show' => 1, 'width' => 250, 'sort' => 31],
  6546. 'store3_dep_grawth' => ['name' => '三次及以上到店签单率', 'show' => 1, 'width' => 250, 'sort' => 32],
  6547. 'dep' => ['name' => '总签单数', 'show' => 1, 'width' => 100, 'sort' => 33],
  6548. 'dep_grawth' => ['name' => '总签单率', 'show' => 1, 'width' => 100, 'sort' => 34],
  6549. 'drawing_date' => ['name' => '出方案客户数', 'show' => 1, 'width' => 150, 'sort' => 35],
  6550. 'sign_count' => ['name' => '转单客户', 'show' => 1, 'width' => 100, 'sort' => 36],
  6551. 'sign_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 75, 'sort' => 37],
  6552. 'area' => ['name' => '面积分布', 'show' => 1, 'width' => 100, 'sort' => 38],
  6553. 'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'sort' => 40],
  6554. 'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'sort' => 41],
  6555. ];
  6556. // 客户统计列表
  6557. $type4 = [
  6558. 'name' => ['name' => '客户姓名', 'show' => 1, 'width' => 100, 'sort' => 1],
  6559. 'phone' => ['name' => '手机号', 'show' => 1, 'width' => 125, 'sort' => 2],
  6560. 'community_name' => ['name' => '小区', 'show' => 1, 'width' => 150, 'sort' => 3],
  6561. 'house_type' => ['name' => '房屋类型', 'show' => 1, 'width' => 100, 'sort' => 4],
  6562. 'house_delivery_time' => ['name' => '交房时间', 'show' => 1, 'width' => 100, 'sort' => 5],
  6563. 'square' => ['name' => '面积', 'show' => 1, 'width' => 75, 'sort' => 6],
  6564. 'org_name' => ['name' => '部门', 'show' => 1, 'width' => 120, 'sort' => 7],
  6565. 'employee_name' => ['name' => '业务员', 'show' => 1, 'width' => 90, 'sort' => 8],
  6566. 'addtime' => ['name' => '添加时间', 'show' => 1, 'width' => 170, 'sort' => 9],
  6567. 'source_name' => ['name' => '来源渠道', 'show' => 1, 'width' => 100, 'sort' => 10],
  6568. 'assigned_personal_manager' => ['name' => '指派客户经理', 'show' => 1, 'width' => 150, 'sort' => 11],
  6569. 'assigned_personal_designer_org' => ['name' => '指派设计部', 'show' => 1, 'width' => 125, 'sort' => 12],
  6570. 'assigned_personal_designer' => ['name' => '指派设计师', 'show' => 1, 'width' => 125, 'sort' => 13],
  6571. 'no_visit_day' => ['name' => '未跟踪天数', 'show' => 1, 'width' => 125, 'sort' => 14],
  6572. 'wechat' => ['name' => '是否加微信', 'show' => 1, 'width' => 125, 'sort' => 15],
  6573. 'add_wechat_type' => ['name' => '加微类型', 'show' => 1, 'width' => 100, 'sort' => 16],
  6574. 'add_wechat_time' => ['name' => '加微时间', 'show' => 1, 'width' => 120, 'sort' => 17],
  6575. 'group_building_date' => ['name' => '建群时间', 'show' => 1, 'width' => 250, 'sort' => 18],
  6576. 'is_liangfang' => ['name' => '是否量房', 'show' => 1, 'width' => 125, 'sort' => 19],
  6577. 'liangfang_date' => ['name' => '量房时间', 'show' => 1, 'width' => 170, 'sort' => 20],
  6578. 'once_liangfang_days' => ['name' => '量房周期', 'show' => 1, 'width' => 120, 'sort' => 21],
  6579. 'plan_issuing_date' => ['name' => '计划出初步方案时间', 'show' => 1, 'width' => 225, 'sort' => 22],
  6580. 'once_daodian' => ['name' => '是否一次到店', 'show' => 1, 'width' => 150, 'sort' => 23],
  6581. 'daodian_date' => ['name' => '一次到店时间', 'show' => 1, 'width' => 170, 'sort' => 24],
  6582. 'once_daodian_shop' => ['name' => '一次所到店面', 'show' => 1, 'width' => 150, 'sort' => 25],
  6583. 'once_daodian_days' => ['name' => '一次到店周期', 'show' => 1, 'width' => 150, 'sort' => 26],
  6584. 'liangfang_daodian_days' => ['name' => '量房到店周期', 'show' => 1, 'width' => 150, 'sort' => 27],
  6585. 'twice_daodian_date' => ['name' => '二次到店时间', 'show' => 1, 'width' => 170, 'sort' => 28],
  6586. 'twice_daodian_shop' => ['name' => '二次所到店面', 'show' => 1, 'width' => 150, 'sort' => 29],
  6587. 'twice_daodian_days' => ['name' => '二次到店周期', 'show' => 1, 'width' => 150, 'sort' => 30],
  6588. 'is_deposit' => ['name' => '是否交定/签单', 'show' => 1, 'width' => 170, 'sort' => 31],
  6589. 'deposit_date' => ['name' => '交定/签单时间', 'show' => 1, 'width' => 170, 'sort' => 32],
  6590. 'drawing_date' => ['name' => '出图时间', 'show' => 1, 'width' => 100, 'sort' => 33],
  6591. 'sign_date' => ['name' => '转单时间', 'show' => 1, 'width' => 170, 'sort' => 34],
  6592. 'sign_days' => ['name' => '交定转单周期', 'show' => 1, 'width' => 150, 'sort' => 35],
  6593. 'signed_money_data' => ['name' => '合同金额(元)', 'show' => 1, 'width' => 125, 'sort' => 36],
  6594. 'first_visit_no_sign' => ['name' => '一次到店未签单时间', 'show' => 1, 'width' => 200, 'sort' => 37],
  6595. 'manager_visit_times' => ['name' => '客户经理回访次数', 'show' => 1, 'width' => 200, 'sort' => 38],
  6596. 'manager_visit_cycle' => ['name' => '客户经理回访周期', 'show' => 1, 'width' => 100, 'sort' => 39],
  6597. 'employee_visit_times' => ['name' => '经理回访次数', 'show' => 1, 'width' => 150, 'sort' => 40],
  6598. 'employee_visit_cycle' => ['name' => '经理回访平均周期', 'show' => 1, 'width' => 200, 'sort' => 41],
  6599. 'invalid' => ['name' => '无效客户', 'show' => 1, 'width' => 100, 'sort' => 42],
  6600. 'invalid_remark' => ['name' => '无效原因', 'show' => 1, 'width' => 100, 'sort' => 43],
  6601. 'died' => ['name' => '是否死单', 'show' => 1, 'width' => 100, 'sort' => 44],
  6602. 'customer_type' => ['name' => '客户种类', 'show' => 1, 'width' => 100, 'sort' => 11],
  6603. ];
  6604. // 总统计
  6605. // 一级标题
  6606. $type5_1 = [
  6607. 'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100],
  6608. 'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130],
  6609. 'behavior' => ['name' => '行为统计', 'show' => 1, 'width' => 100],
  6610. 'customer' => ['name' => '客户统计', 'show' => 1, 'width' => 100],
  6611. 'house' => ['name' => '房屋信息统计', 'show' => 1, 'width' => 100],
  6612. ];
  6613. // 二级标题
  6614. $type5_2 = [
  6615. 'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100, 'parent' => 'name'],
  6616. 'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130, 'parent' => 'org_name'],
  6617. 'connecting_count' => ['name' => '电话量', 'show' => 1, 'width' => 150, 'parent' => 'behavior'],
  6618. 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'parent' => 'behavior'],
  6619. 'add_wechat_count_grawth' => ['name' => '微信通过率', 'show' => 1, 'width' => 100, 'parent' => 'behavior'],
  6620. 'shares_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'parent' => 'behavior'],
  6621. 'number_of_explanations' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'parent' => 'behavior'],
  6622. 'look_data' => ['name' => '培训资料是否看了', 'show' => 1, 'width' => 150, 'parent' => 'behavior'],
  6623. 'exam_data' => ['name' => '考核通过情况', 'show' => 1, 'width' => 100, 'parent' => 'behavior'],
  6624. 'customer_data' => ['name' => '客户总量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6625. 'valid_count' => ['name' => '有效数量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6626. 'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6627. 'valid_no_wechat' => ['name' => '有效未加微', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6628. 'measuring_room_customer_no_to_store' => ['name' => '量房未到店', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6629. 'drawing_date_count' => ['name' => '户型图', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6630. 'measuring_room_customer' => ['name' => '量房数量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6631. 'measuring_room_zhouqi' => ['name' => '平均量房周期', 'show' => 1, 'width' => 130, 'parent' => 'customer'],
  6632. 'measuring_room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6633. 'yuji_store' => ['name' => '预计到店数', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6634. 'daodian_count' => ['name' => '到店数', 'show' => 1, 'width' => 120, 'parent' => 'customer'],
  6635. 'daodian_avg' => ['name' => '平均到店周期', 'show' => 1, 'width' => 225, 'parent' => 'customer'],
  6636. 'daodian_grawth' => ['name' => '到店率', 'show' => 1, 'width' => 150, 'parent' => 'customer'],
  6637. 'avg_room_store_zhouqi' => ['name' => '平均量房到店周期', 'show' => 1, 'width' => 170, 'parent' => 'customer'],
  6638. 'daodian_deposit' => ['name' => '到店签单数量', 'show' => 1, 'width' => 150, 'parent' => 'customer'],
  6639. 'daodian_deposit_grawth' => ['name' => '到店签单率', 'show' => 1, 'width' => 150, 'parent' => 'customer'],
  6640. 'avg_dep_zhouqi'=> ['name'=> '平均签单周期', 'show'=> 1, 'width'=> 135, 'parent'=> 'customer'],
  6641. 'sign_count' => ['name' => '转单数量', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6642. 'sign_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6643. 'avg_visit_count' => ['name' => '平均回访次数', 'show' => 1, 'width' => 130, 'parent' => 'customer'],
  6644. 'signed_money' => ['name' => '平均合同金额', 'show' => 1, 'width' => 130, 'parent' => 'customer'],
  6645. 'died_customer' => ['name' => '无效客资', 'show' => 1, 'width' => 100, 'parent' => 'customer'],
  6646. 'area' => ['name' => '面积分布', 'show' => 1, 'width' => 170, 'parent' => 'house'],
  6647. 'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'parent' => 'house'],
  6648. 'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'parent' => 'house'],
  6649. ];
  6650. // 三级标题
  6651. $type5_3 = [
  6652. 'name' => ['name' => '业务员名称', 'show' => 1, 'width' => 100, 'parent' => 'name', 'sort' => 1],
  6653. 'org_name' => ['name' => '业务员所在部门', 'show' => 1, 'width' => 130, 'parent' => 'org_name', 'sort' => 2],
  6654. 'connecting_count' => ['name' => '拨打量', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 2],
  6655. 'connecting_capacity' => ['name' => '接通多少', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 3],
  6656. 'connecting_capacity_grawth' => ['name' => '接通率', 'show' => 1, 'width' => 100, 'parent' => 'call_count', 'sort' => 4],
  6657. 'average_duration' => ['name' => '平均通话时长', 'show' => 1, 'width' => 130, 'parent' => 'call_count', 'sort' => 5],
  6658. 'add_wechat_count' => ['name' => '加微数量', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 6],
  6659. 'add_wechat_count_grawth' => ['name' => '微信通过率', 'show' => 1, 'width' => 100, 'parent' => 'behavior', 'sort' => 7],
  6660. 'shares_count' => ['name' => '调用内容数量', 'show' => 1, 'width' => 130, 'parent' => 'behavior', 'sort' => 8],
  6661. 'number_of_explanations' => ['name' => '智慧屏讲解次数', 'show' => 1, 'width' => 150, 'parent' => 'behavior', 'sort' => 9],
  6662. 'look_data' => ['name' => '培训资料是否看了', 'show' => 1, 'width' => 150, 'parent' => 'behavior', 'sort' => 10],
  6663. 'assessment_times' => ['name' => '考核次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 11],
  6664. 'number_of_passes' => ['name' => '通过次数', 'show' => 1, 'width' => 100, 'parent' => 'exam_data', 'sort' => 12],
  6665. 'resource_count' => ['name' => '分配数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 13],
  6666. 'following_up_count' => ['name' => '在跟进数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 14],
  6667. 'deposit_count' => ['name' => '签单数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 15],
  6668. 'reported_quantity' => ['name' => '报备数量', 'show' => 1, 'width' => 100, 'parent' => 'customer_data', 'sort' => 16],
  6669. 'valid_count' => ['name' => '有效数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 17],
  6670. 'valid_grawth' => ['name' => '有效率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 18],
  6671. 'valid_no_wechat' => ['name' => '有效未加微', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 19],
  6672. 'measuring_room_customer_no_to_store' => ['name' => '量房未到店', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 20],
  6673. 'drawing_date_count' => ['name' => '户型图', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 21],
  6674. 'measuring_room_customer' => ['name' => '量房数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 22],
  6675. 'measuring_room_zhouqi' => ['name' => '平均量房周期', 'show' => 1, 'width' => 130, 'parent' => 'customer', 'sort' => 23],
  6676. 'measuring_room_grawth' => ['name' => '量房率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 24],
  6677. 'yuji_store' => ['name' => '预计到店数', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 25],
  6678. 'to_the_store_1' => ['name' => '一次到店', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 26],
  6679. 'to_the_store_2' => ['name' => '二次到店', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 27],
  6680. 'strtore_2v1' => ['name' => '二访比率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_count', 'sort' => 28],
  6681. 'to_the_store_3' => ['name' => '三次及以上到店', 'show' => 1, 'width' => 130, 'parent' => 'daodian_count', 'sort' => 28],
  6682. 'strtore_avg_days_1' => ['name' => '平均一次到店周期', 'show' => 1, 'width' => 150, 'parent' => 'daodian_avg', 'sort' => 29],
  6683. 'strtore_avg_days_2' => ['name' => '平均二次到店周期', 'show' => 1, 'width' => 150, 'parent' => 'daodian_avg', 'sort' => 30],
  6684. 'strtore_avg_days_3' => ['name' => '平均三次及以上到店周期', 'show' => 1, 'width' => 200, 'parent' => 'daodian_avg', 'sort' => 31],
  6685. 'strtore_count_1_grawth' => ['name' => '一次到店率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_grawth', 'sort' => 32],
  6686. 'strtore_count_2_grawth' => ['name' => '二次到店率', 'show' => 1, 'width' => 100, 'parent' => 'daodian_grawth', 'sort' => 33],
  6687. 'strtore_count_3_grawth' => ['name' => '三次及以上到店率', 'show' => 1, 'width' => 150, 'parent' => 'daodian_grawth', 'sort' => 34],
  6688. 'avg_room_store_zhouqi' => ['name' => '平均量房到店周期', 'show' => 1, 'width' => 170, 'parent' => 'customer', 'sort' => 35],
  6689. 'strtore_deposit_count_0' => ['name' => '一次到店签单数量', 'show' => 1, 'width' => 150, 'parent' => 'daodian_deposit', 'sort' => 36],
  6690. 'strtore_deposit_count_1' => ['name' => '二次到店签单数量', 'show' => 1, 'width' => 150, 'parent' => 'daodian_deposit', 'sort' => 37],
  6691. 'strtore_deposit_count_2' => ['name' => '三次到及以上店签单数量', 'show' => 1, 'width' => 200, 'parent' => 'daodian_deposit', 'sort' => 38],
  6692. 'strtore_deposit_grawth_0' => ['name' => '一次到店签单率', 'show' => 1, 'width' => 130, 'parent' => 'daodian_deposit_grawth', 'sort' => 39],
  6693. 'strtore_deposit_grawth_1' => ['name' => '二次到店签单率', 'show' => 1, 'width' => 130, 'parent' => 'daodian_deposit_grawth', 'sort' => 40],
  6694. 'strtore_deposit_grawth_2' => ['name' => '三次到及以上店签单率', 'show' => 1, 'width' => 180, 'parent' => 'daodian_deposit_grawth', 'sort' => 41],
  6695. 'deposit_avg_days' => ['name' => '平均签单周期', 'show' => 1, 'width' => 135, 'parent' => 'customer', 'sort' => 42],
  6696. 'sign_count' => ['name' => '转单数量', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 43],
  6697. 'sign_count_grawth' => ['name' => '转单率', 'show' => 1, 'width' => 100, 'parent' => 'customer', 'sort' => 44],
  6698. 'avg_visit_count' => ['name' => '平均回访次数', 'show' => 1, 'width' => 130, 'parent' => 'customer', 'sort' => 45],
  6699. 'signed_money' => ['name' => '平均合同金额', 'show' => 1, 'width' => 130, 'parent' => 'customer', 'sort' => 46],
  6700. 'square_0_80' => ['name' => '80以下', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 47],
  6701. 'square_80_100' => ['name' => '81-100', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 48],
  6702. 'square_100_120' => ['name' => '101-120', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 49],
  6703. 'square_120_200' => ['name' => '121-200', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 50],
  6704. 'square_200_500' => ['name' => '201-500', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 51],
  6705. 'square_500' => ['name' => '500以上', 'show' => 1, 'width' => 100, 'parent' => 'area', 'sort' => 52],
  6706. 'existing_homes_count' => ['name' => '现房数量', 'show' => 1, 'width' => 100, 'parent' => 'house', 'sort' => 53],
  6707. 'forward_housing_count' => ['name' => '期房数量', 'show' => 1, 'width' => 100, 'parent' => 'house', 'sort' => 54],
  6708. ];
  6709. //网销端已增加type值到10,如增加type请往10后面加
  6710. $root_id = request()->employee->root_id;
  6711. //房屋状态是否开启
  6712. $house_status_where = [
  6713. ['root_id', '=', $root_id],
  6714. ['keyname', '=', 'house_status'],
  6715. ['status', '=', 0]
  6716. ];
  6717. $house_status_per = CustomerPortraitField::where($house_status_where)->findOrEmpty();
  6718. $house_field = [];
  6719. if (!$house_status_per->isEmpty()) {
  6720. $field = CustomerPortraitFieldSelect::where('pid', $house_status_per->id)->column('name,id');
  6721. $fields = [];
  6722. foreach ($field as $v) {
  6723. $fields['house_status_' . $v['id']] = [
  6724. 'name' => $v['name'],
  6725. 'show' => 1,
  6726. 'width' => 100,
  6727. 'parent' => 'house',
  6728. 'sort' => 50
  6729. ];
  6730. }
  6731. $house_field = $fields;
  6732. }
  6733. $type5_2 = array_merge($type5_2, $house_field);
  6734. $type5_3 = array_merge($type5_3, $house_field);
  6735. // 户型
  6736. $housetype_where = [
  6737. ['root_id', '=', $root_id],
  6738. ['keyname', '=', 'housetype_arrow'],
  6739. ['status', '=', 0]
  6740. ];
  6741. $housetype_per = CustomerPortraitField::where($housetype_where)->findOrEmpty();
  6742. $housetype_field = [];
  6743. $housetype_done_field = [];
  6744. if (!$housetype_per->isEmpty()) {
  6745. $field = CustomerPortraitFieldSelect::where('pid', $housetype_per->id)->column('name,id');
  6746. $fields = [];
  6747. $fields_done = [];
  6748. foreach ($field as $v) {
  6749. $fields['housetype_arrow_' . $v['id']] = [
  6750. 'name' => $v['name'],
  6751. 'show' => 1,
  6752. 'width' => 100,
  6753. 'parent' => 'house',
  6754. 'sort' => 51
  6755. ];
  6756. $fields_done['housetype_arrow_lv_' . $v['id']] = [
  6757. 'name' => $v['name'] . '成交率',
  6758. 'show' => 1,
  6759. 'width' => 150,
  6760. 'parent' => 'house',
  6761. 'sort' => 51
  6762. ];
  6763. }
  6764. $housetype_field = $fields;
  6765. $housetype_done_field = $fields_done;
  6766. }
  6767. $type5_2 = array_merge($type5_2, $housetype_field);
  6768. $type5_3 = array_merge($type5_3, $housetype_field);
  6769. $type5_2 = array_merge($type5_2, $housetype_done_field);
  6770. $type5_3 = array_merge($type5_3, $housetype_done_field);
  6771. //无效客资
  6772. $invalid_customer = Setting::where([['name', '=', 'clueTag'], ['root_id', '=', $root_id]])->value('content');
  6773. if ($invalid_customer) {
  6774. $invalid_arr = explode(',', $invalid_customer);
  6775. } else {
  6776. $invalid_arr = ['已装修', '无需求', '有需求', '超地域', '未交房', '在外地', '无购买力', '未接通电话'];
  6777. }
  6778. $invalid_field = [];
  6779. foreach ($invalid_arr as $k => $v) {
  6780. $invalid_field['invalid_customer_' . $k] = [
  6781. 'name' => $v,
  6782. 'show' => 1,
  6783. 'width' => 100,
  6784. 'parent' => 'died_customer',
  6785. 'sort' => 55
  6786. ];
  6787. }
  6788. $new_type_5_3 = [];
  6789. foreach ($type5_3 as $k => $v) {
  6790. $new_type_5_3[$k] = $v;
  6791. if ($k == 'signed_money') {
  6792. $new_type_5_3 = array_merge($new_type_5_3, $invalid_field);
  6793. }
  6794. }
  6795. $type5_3 = $new_type_5_3;
  6796. // 管理层户型分布
  6797. $new_type_3 = [];
  6798. foreach ($type3 as $k => $v) {
  6799. $new_type_3[$k] = $v;
  6800. if ($k == 'area') {
  6801. $new_type_3 = array_merge($new_type_3, $house_field);
  6802. }
  6803. }
  6804. switch ($type) {
  6805. case 1: // 行为统计全部字段
  6806. return $type1;
  6807. break;
  6808. case 2: // 高层统计全部字段
  6809. return $type2;
  6810. break;
  6811. case 3: // 管理层统计全部字段
  6812. return $type3;
  6813. break;
  6814. case 4: // 客户统计列表
  6815. return $type4;
  6816. break;
  6817. case 5: // 总统计
  6818. return $type5_3;
  6819. break;
  6820. default:
  6821. return [];
  6822. break;
  6823. }
  6824. }
  6825. /**
  6826. * 统计表字段
  6827. */
  6828. public function fieldList($type = 0, $is_org = 0)
  6829. {
  6830. $field = $this->statisticsFields($type);
  6831. $root_id = request()->employee->root_id;
  6832. $employee_id = request()->employee->id;
  6833. $save_field = StatisticsSetting::where([['root_id', '=', $root_id], ['employee_id', '=', $employee_id], ['type', '=', $type]])->findOrEmpty();
  6834. if (!$save_field->isEmpty()) {
  6835. $save_content = json_decode($save_field['content'], true);
  6836. if (!empty($save_content)) {
  6837. foreach ($save_content as $k => $v) {
  6838. if (isset($field[$k])) {
  6839. $field[$k]['show'] = $v['show'];
  6840. $field[$k]['sort'] = $v['sort'];
  6841. }
  6842. }
  6843. array_multisort(array_column($field, 'sort'), SORT_ASC, $field);
  6844. }
  6845. }
  6846. if ($type == 5) {
  6847. if ($is_org == 1) {
  6848. $field_5['org_name'] = ['name' => '部门名称', 'show' => 1, 'width' => 100, 'sort' => 1];
  6849. foreach ($field as $k => $v) {
  6850. if ($k == 'name' || $k == 'org_name' || $k == 'look_data') {
  6851. unset($field[$k]);
  6852. }
  6853. }
  6854. $field_5 = array_merge($field_5, $field);
  6855. $field = $field_5;
  6856. } else {
  6857. $field_5['name'] = [];
  6858. $field_5['org_name'] = [];
  6859. foreach ($field as $k => $v) {
  6860. if (isset($field_5[$k])) {
  6861. $field_5[$k] = $v;
  6862. unset($field[$k]);
  6863. }
  6864. }
  6865. $field_5 = array_merge($field_5, $field);
  6866. $field = $field_5;
  6867. }
  6868. }
  6869. return json(['code' => 0, 'data' => $field, 'msg' => 'success']);
  6870. }
  6871. /**
  6872. * 统计字段展示设置
  6873. */
  6874. public function fieldSetting()
  6875. {
  6876. $type = input('type', '', 'intval'); // 类型
  6877. $content = input('content', '', 'trim'); // 内容
  6878. $content = json_decode($content, true);
  6879. $default_field = $this->statisticsFields($type);
  6880. $root_id = request()->employee->root_id;
  6881. $employee_id = request()->employee->id;
  6882. $find = StatisticsSetting::where([['root_id', '=', $root_id], ['employee_id', '=', $employee_id], ['type', '=', $type]])->findOrEmpty();
  6883. foreach ($content as $k => $v) {
  6884. if (isset($v['name']) && isset($v['show']) && isset($v['sort']) && isset($default_field[$v['name']])) {
  6885. $default_field[$v['name']]['show'] = $v['show'];
  6886. $default_field[$v['name']]['sort'] = $v['sort'];
  6887. }
  6888. }
  6889. $save_content = json_encode($default_field);
  6890. if ($find->isEmpty()) {
  6891. $result = StatisticsSetting::insert(['root_id' => $root_id, 'employee_id' => $employee_id, 'type' => $type, 'content' => $save_content]);
  6892. } else {
  6893. $find->content = $save_content;
  6894. $find->save();
  6895. }
  6896. return json(['code' => 0, 'data' => [], 'msg' => '设置成功']);
  6897. }
  6898. /**
  6899. * 导出记录
  6900. */
  6901. public function export2($m)
  6902. {
  6903. $typeList = ['behaviorStatisticsData' => ['type' => 1], 'highBehaviorStatisticsData' => ['type' => 2], 'manageStatisticsData' => ['type' => 3], 'customer_list_data' => ['type' => 4], 'generalStatisticsData' => ['type' => 5, 'is_org' => 2]];
  6904. if (!isset($typeList[$m])) {
  6905. json(['code' => 1, 'msg' => '类型不存在'])->send();
  6906. return;
  6907. }
  6908. $param = Request::post();
  6909. $filename = uniqid();
  6910. $log = [
  6911. 'root_id' => request()->employee->root_id,
  6912. 'file' => $filename . '.csv',
  6913. 'type' => $m,
  6914. 'employee_id' => request()->employee->id,
  6915. 'search' => json_encode($param)
  6916. ];
  6917. $exportLog = ExportLog::create($log);
  6918. json(['code' => 0, 'msg' => '导出中,请稍后在记录中查看'])->send();
  6919. $type = $typeList[$m]['type'];
  6920. $is_org = isset($typeList[$m]['is_org']) ? $typeList[$m]['is_org'] : 0;
  6921. $c = $this->fieldList($type, $is_org);
  6922. $c = $c->getData();
  6923. $title = $c['data'];
  6924. array_multisort(array_column($title, 'sort'), $title);
  6925. touch('../download/' . $filename . '.csv');
  6926. $fp = fopen('../download/' . $filename . '.csv', 'a');
  6927. $t = [];
  6928. foreach ($title as $ti) {
  6929. $t[] = $ti['name'];
  6930. }
  6931. fputcsv($fp, $t);
  6932. $n = 2000;
  6933. $p = 1;
  6934. do {
  6935. $data = $this->$m($p, $n);
  6936. $data = $data->getData();
  6937. foreach ($data['data'] as $row) {
  6938. $r = [];
  6939. foreach ($title as $k => $f) {
  6940. $r[] = isset($row[$k]) ? $row[$k] : '';
  6941. }
  6942. fputcsv($fp, $r);
  6943. }
  6944. $p++;
  6945. } while ($n * ($p - 1) < $data['count']);
  6946. fclose($fp);
  6947. // 文件上传
  6948. $rs = ossUpload('statisticsExport/' . $filename . '.csv', '../download/' . $filename . '.csv');
  6949. if ($rs) {
  6950. unlink('../download/' . $filename . '.csv');
  6951. }
  6952. $exportLog->save(['state' => 1]);
  6953. }
  6954. public function exportList2($type)
  6955. {
  6956. if (!request()->isAjax()) {
  6957. return View::fetch();
  6958. }
  6959. $where = [
  6960. ['type', '=', $type],
  6961. ['root_id', '=', request()->employee->root_id]
  6962. ];
  6963. $data = ExportLog::with(['employee' => function ($query) {
  6964. $query->bind(['opt_name']);
  6965. }])->field('id,state,employee_id,addtime,file')->where($where)->order('addtime desc')->select();
  6966. $count = ExportLog::where($where)->count();
  6967. return json(['code' => 0, 'count' => $count, 'data' => $data]);
  6968. }
  6969. public function export3($m)
  6970. {
  6971. // $param = Request::get();
  6972. // $filename = uniqid();
  6973. // $log = [
  6974. // 'root_id' => request()->employee->root_id,
  6975. // 'file' => $filename . '.csv',
  6976. // 'type' => $m,
  6977. // 'employee_id' => request()->employee->id,
  6978. // 'search' => json_encode($param)
  6979. // ];
  6980. // $exportLog = ExportLog::create($log);
  6981. return json(['code' => 0, 'msg' => '1111'])->send();
  6982. $type = $typeList[$m]['type'];
  6983. $is_org = isset($typeList[$m]['is_org']) ? $typeList[$m]['is_org'] : 0;
  6984. $c = $this->fieldList($type, $is_org);
  6985. $c = $c->getData();
  6986. $title = $c['data'];
  6987. array_multisort(array_column($title, 'sort'), $title);
  6988. touch('../download/' . $filename . '.csv');
  6989. $fp = fopen('../download/' . $filename . '.csv', 'a');
  6990. $t = [];
  6991. foreach ($title as $ti) {
  6992. $t[] = $ti['name'];
  6993. }
  6994. fputcsv($fp, $t);
  6995. $n = 2000;
  6996. $p = 1;
  6997. do {
  6998. $data = $this->$m($p, $n);
  6999. $data = $data->getData();
  7000. foreach ($data['data'] as $row) {
  7001. $r = [];
  7002. foreach ($title as $k => $f) {
  7003. $r[] = isset($row[$k]) ? $row[$k] : '';
  7004. }
  7005. fputcsv($fp, $r);
  7006. }
  7007. $p++;
  7008. } while ($n * ($p - 1) < $data['count']);
  7009. fclose($fp);
  7010. // 文件上传
  7011. $rs = ossUpload('statisticsExport/' . $filename . '.csv', '../download/' . $filename . '.csv');
  7012. if ($rs) {
  7013. unlink('../download/' . $filename . '.csv');
  7014. }
  7015. $exportLog->save(['state' => 1]);
  7016. }
  7017. }