1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486 |
- <?php
- /**
- * 客户统计
- */
- namespace app\api\controller;
- use app\model\Customer;
- use app\model\CustomerClue;
- use app\model\CustomersSubscribe;
- use app\model\CustomerVisitLog;
- use app\model\DailyWechatArticleCopy;
- use app\model\Employee;
- use app\model\Footprints;
- use app\model\Org;
- use app\model\OutCallLog;
- use app\model\ShareLog;
- use think\facade\Db;
- use think\facade\Log;
- use think\facade\Request;
- use app\model\CustomerPortraitFieldSelect;
- use app\model\CustomerPortraitField;
- use app\model\Activity;
- use app\model\CustomerPackage;
- use app\model\OutCallMbLog;
- use app\model\Setting;
- use app\model\User;
- class CrmStatistics extends Base
- {
- /**
- * 获取展示设置
- */
- public function getSetting() {
- $token = request()->token;
- $root_id = $token['root_org'];
- $where = [
- ['name', '=', 'CustomerStatisticsSettingForMini'],
- ['root_id', '=', $root_id]
- ];
- $data = Setting::where($where)->findOrEmpty();
- if ($data->isEmpty()) {
- $content = ['valid', 'call', 'jiav', 'contact', 'subscribe', 'measure_room', 'arrival', 'deposit', 'signed', 'maika'];
- } else {
- $content = explode(',', $data['content']);
- }
- return json(['code' => self::success, 'msg' => 'success', 'data'=> $content]);
- }
- /**
- * 获取部门
- */
- public function get_org(){
- $token = request()->token;
- $path = Org::where('id', '=', $token['org_id'])->value('path');
- $list = Org::where([['path', 'like', $path . '%'], ['status', '=', 1]])->order('path asc, pid asc')->select()->toArray();
- return json(['code'=> self::success, 'data'=> $list]);
- }
- /**
- * 统计首页
- */
- public function index(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $sub_org = orgSubIds($token['org_id']);
- // 所选部门员工统计数据
- $e_where[] = ['org_id', 'in', $sub_org];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_ids = Employee::where($e_where)->column('id');
- // 客户分配数
- $wuxiao = Customer::changeState('无效', 'chaos');
- $not_sure = Customer::changeState('待确认', 'chaos');
- $customer_count = Customer::where([['employee_id', 'in', $employee_ids], ['sign_time', 'between', [$start_time, $end_time]], ['died', '<>', 2], ['state', 'not in', array_merge($wuxiao, $not_sure)]])->count();
- $data['customer_count'] = $customer_count;
- // 打电话次数
- $call_count = OutCallLog::where([['employee_id', 'in', $employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
- // 手机打电话次数
- $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
- $data['call_count'] = $call_count + $mbCallCount;
- // 加微统计
- $jiav_count = Customer::where([['employee_id', 'in', $employee_ids], ['add_wechat_time', 'between', [$start_time, $end_time]]])->count();
- $data['jiav_count'] = $jiav_count;
- //沟通跟进次数
- // 内容调用次数
- $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
- $share_where[] = ['employee_id', 'in', $employee_ids];
- $shared_count = ShareLog::where($share_where)->count();
- // 微信沟通次数
- $wx_contact_count = CustomerVisitLog::where([['org_id', 'in', $sub_org], ['addtime', 'between', [$start_time, $end_time]], ['type', '=', 3]])->count();
- $contact_count = $call_count + $mbCallCount + $shared_count + $wx_contact_count;
- $data['contact_count'] = $contact_count;
- // 预约活动数量
- $hd_subscribe_count = CustomersSubscribe::where([['employee_id', 'in', $employee_ids], ['subscribe_date', 'between', [$start_time, $end_time]], ['type', '=', 2], ['state', 'in', [0, 1]]])->count();
- $data['hd_subscribe_count'] = $hd_subscribe_count;
- // 跟踪记录
- // $visit_log = CustomerVisitLog::where([['org_id', 'in', $sub_org], ['confirm_date', 'between', [$start_time, $end_time]]])->column('customer_id,state,money');
- $db_where[] = ['Customer.org_id', 'in', $sub_org];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,state,money')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->select()
- ->toArray();
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
- $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
- $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
-
- // 见面状态
- // $state3 = CustomerVisitLog::changeState('已到场', 'chaos');
- // $make_state0 = array_merge($dd_state, $lf_state, $state3);
- $lf_count = [];
- $dd_count = [];
- $jd_count = [];
- $qd_count = [];
- $mk_count = [];
- $jd_money = 0;
- foreach ($visit_log as $k => $v) {
- if (in_array($v['state'], $lf_state) && !in_array($v['customer_id'], $lf_count)) {
- $lf_count[] = $v['customer_id'];
- }
- if (in_array($v['state'], $dd_state) && !in_array($v['customer_id'], $dd_count)) {
- $dd_count[] = $v['customer_id'];
- }
- if (in_array($v['state'], $jd_state) && !in_array($v['customer_id'], $jd_count)) {
- $jd_count[] = $v['customer_id'];
- $jd_money += $v['money'];
- }
- if (in_array($v['state'], $qd_state) && !in_array($v['customer_id'], $qd_count)) {
- $qd_count[] = $v['customer_id'];
- }
- if (in_array($v['state'], $mk_state) && !in_array($v['customer_id'], $mk_count)) {
- $mk_count[] = $v['customer_id'];
- }
- }
- $data['lf_count'] = count($lf_count);
- $data['dd_count'] = count($dd_count);
- $data['jd_count'] = count($jd_count);
- $data['jd_money'] = $jd_money;
- $data['qd_count'] = count($qd_count);
- $data['mk_count'] = count($mk_count);
- $qd_money = Customer::where('id', 'in', $qd_count)->sum('signed_money');
- $data['qd_money'] = $qd_money;
- $data['org_name'] = Org::where('id', request()->token['org_id'])->value('name');
- return json(['code'=> self::success, 'data'=> $data]);
- }
- /**
- * 到店统计(全部)
- */
- public function arrival(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 部门信息
- $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
- //员工数量
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $data['employee_count'] = count($sub_employee_ids);
- // 所有客户量
- $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['org_id', 'in', $sub_org];
- $c_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $c_where[] = ['state', 'NOT NULL', null];
- $c_where[] = ['state', 'not in', $wuxiao];
- $c_where[] = ['died', '<>', 2];
-
- $customer_list = Customer::where($c_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- })->column('id,addtime');
- // 见面状态
- // $state1 = CustomerVisitLog::changeState('已量房', 'chaos');
- // $state2 = CustomerVisitLog::changeState('已到店', 'chaos');
- // $state3 = CustomerVisitLog::changeState('已到场', 'chaos');
- // $make_state0 = array_merge($state1, $state2, $state3);
- $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
- $db_where[] = ['Customer.org_id', 'in', $sub_org];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
- $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date,employee_id')
- ->view('Customer', 'addtime customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->select()
- ->toArray();
- $all_customer_count = count($customer_list);
- $list = [];
- foreach ($visit_log as $k => $v){
- $list[$v['customer_id']][] = $v;
- }
- $data['all_count'] = count($visit_log);
- // 到店次数
- $one = [];
- $two = [];
- $three = [];
- $one_arrival = 0; // 一次到店量
- $two_arrival = 0;
- $three_arrival = 0;
- $one_days = 0; // 一次到店天数间隔
- $two_days = 0;
- $three_days = 0;
- $lf_count = 0; // 量房转到店
- $lf_days = 0;
- $wc_count = 0; // 外场转到店
- $wc_days = 0;
- $nc_count = 0; // 内场活动到店
- $one_customer_ids = [];
- $two_customer_ids = [];
- $three_customer_ids = [];
- // 先查出来后面要用到的已量房、已到场记录
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $lw_where[] = ['Customer.org_id', 'in', $sub_org];
- $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
- $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
- $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($lw_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
-
- $lf_list = [];
- $wc_list = [];
- foreach ($lw_log_list as $k => $v) {
- if (in_array($v['state'], $lf_state, true)) {
- if (empty($lf_list[$v['customer_id']])) {
- $lf_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- if (in_array($v['state'], $wc_state, true)) {
- if (empty($wc_list[$v['customer_id']])) {
- $wc_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- }
- foreach ($list as $k => $v){
- if (count($v) == 1) {
- $one_arrival ++;
- $one_customer_ids[] = $k;
- $long = (strtotime($v[0]['confirm_date']) - strtotime($v[0]['customer_addtime'])) > 0 ? (strtotime($v[0]['confirm_date']) - strtotime($v[0]['customer_addtime'])) : 0;
- $days = ceil($long / (24*3600));
- $one_days += $days;
- }
- if (count($v) == 2) {
- $two_arrival ++;
- $two_customer_ids[] = $k;
- $days = ceil((strtotime($v[1]['confirm_date']) - strtotime($v[0]['confirm_date'])) / (24*3600));
- $two_days += $days;
- }
- if (count($v) > 2) {
- $three_arrival ++;
- $three_customer_ids[] = $k;
- $days = ceil((strtotime($v[2]['confirm_date']) - strtotime($v[1]['confirm_date'])) / (24*3600));
- $three_days += $days;
- }
- if (isset($lf_list[$k])) {
- $lf_count ++;
- $days = ceil((strtotime($v[0]['confirm_date']) - strtotime($lf_list[$k])) / (24*3600));
- $lf_days += $days;
- }
- if (isset($wc_list[$k])) {
- $wc_count ++;
- $days = ceil((strtotime($v[0]['confirm_date']) - strtotime($wc_list[$k])) / (24*3600));
- $wc_days += $days;
- }
- // 内场活动到店
- if (!empty($v[0]['aid'])){
- $nc_count ++;
- }
- }
-
- // 签单
- $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- // $qd_where[] = ['customer_id', 'in', $all_customer];
- $qd_where[] = ['state', 'in', $qd_state];
- $qd_where[] = ['org_id', 'in', $sub_org];
- $qd_customer_ids = CustomerVisitLog::where($qd_where)->column('customer_id');
- $one_qd = count(array_unique(array_intersect($qd_customer_ids, $one_customer_ids)));
- $two_qd = count(array_unique(array_intersect($qd_customer_ids, $two_customer_ids)));
- $three_qd = count(array_unique(array_intersect($qd_customer_ids, $three_customer_ids)));
- $one['count'] = $one_arrival;
- $one['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
- $one['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
- $one['lf_count'] = $lf_count;
- $one['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
- $one['wc_count'] = $wc_count;
- $one['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
- $one['nc_count'] = $nc_count;
- $one['qd_count'] = $one_qd;
- $two['count'] = $two_arrival;
- $two['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
- $two['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
- $two['qd_count'] = $two_qd;
- $three['count'] = $three_arrival;
- $three['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
- $three['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
- $three['qd_count'] = $three_qd;
- // 邀约计算
- $all_customer = array_values(array_unique(array_merge($one_customer_ids, $two_customer_ids, $three_customer_ids)));
- $subscribe_where[] = ['state', 'in', [0, 1]];
- $subscribe_where[] = ['type', '=', 1];
- $subscribe_where[] = ['customer_id', 'in', $all_customer];
- $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
- $xs_count_one = 0; // 销售一次
- $sj_count_one = 0; // 设计一次
- $xs_count_two = 0; // 销售二次
- $sj_count_two = 0; // 设计二次
- $xs_count_three = 0; // 销售三次
- $sj_count_three = 0; // 设计三次
- foreach ($subscribe as $k => $v){
- if (empty($v['designer_id']) && in_array($v['customer_id'], $one_customer_ids)){
- $xs_count_one ++;
- } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $one_customer_ids)){
- $sj_count_one ++;
- }
- if (empty($v['designer_id']) && in_array($v['customer_id'], $two_customer_ids)){
- $xs_count_two ++;
- } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $two_customer_ids)){
- $sj_count_two ++;
- }
- if (empty($v['designer_id']) && in_array($v['customer_id'], $three_customer_ids)){
- $xs_count_three ++;
- } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $three_customer_ids)){
- $sj_count_three ++;
- }
- }
- $one['xs_yaoyue'] = $xs_count_one;
- $one['xs_yaoyue_lv'] = $one_arrival > 0 ? round($xs_count_one * 100 / $one_arrival, 2) : 0;
- $one['sj_yaoyue'] = $sj_count_one;
- $one['sj_yaoyue_lv'] = $one_arrival > 0 ? round($sj_count_one * 100 / $one_arrival, 2) : 0;
- $two['xs_yaoyue'] = $xs_count_two;
- $two['xs_yaoyue_lv'] = $two_arrival > 0 ? round($xs_count_two * 100 / $two_arrival, 2) : 0;
- $two['sj_yaoyue'] = $sj_count_two;
- $two['sj_yaoyue_lv'] = $two_arrival > 0 ? round($sj_count_two * 100 / $two_arrival, 2) : 0;
- $three['xs_yaoyue'] = $xs_count_three;
- $three['xs_yaoyue_lv'] = $three_arrival > 0 ? round($xs_count_three * 100 / $three_arrival, 2) : 0;
- $three['sj_yaoyue'] = $sj_count_three;
- $three['sj_yaoyue_lv'] = $three_arrival > 0 ? round($sj_count_three * 100 / $three_arrival, 2) : 0;
- $data['one'] = $one;
- $data['two'] = $two;
- $data['three'] = $three;
- return json(['code'=> self::success, 'data'=> $data, 'msg'=> 'success']);
- }
- /**
- * 到店统计(部门)
- */
- public function arrivalOrg(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $times = input('times', 0, 'intval');
- if (!in_array($times, [1,2,3])){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', '', 'intval');
- if (!$org_id) {
- $org_id = $token['org_id'];
- }
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所有客户量
- $sub_org_ids = orgSubIds($org_id);
- $c_where[] = ['org_id', 'in', $sub_org_ids];
- $c_where[] = ['state', 'NOT NULL', null];
-
- $customer_all = Customer::where($c_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- })->column('id,addtime,employee_id,died,sign_time,state');
- $customer = []; // 时间段内有效
- $wuxiao = Customer::changeState('无效', 'chaos');
- $start_totime = strtotime($start_time);
- $end_totime = strtotime($end_time);
- $customer_employee_id = []; // 客户所属员工id
- // 每个员工的客户
- $employee_customer = [];
- foreach ($customer_all as $k => $v) {
- $customer_employee_id[$v['id']] = $v['employee_id'];
- if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
- $customer[] = $v;
- if (isset($employee_customer[$v['employee_id']])) {
- $employee_customer[$v['employee_id']][] = $v;
- } else {
- $employee_customer[$v['employee_id']] = [];
- $employee_customer[$v['employee_id']][] = $v;
- }
- }
- }
- $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
- $db_where[] = ['Customer.org_id', 'in', $sub_org_ids];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
- $log_list = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date,employee_id')
- ->view('Customer', 'sign_time customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- // 先查出来后面要用到的已量房、已到场记录
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $lw_where[] = ['Customer.org_id', 'in', $sub_org_ids];
- $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
- $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
- $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($lw_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
-
- $lf_list = [];
- $wc_list = [];
- foreach ($lw_log_list as $k => $v) {
- if (in_array($v['state'], $lf_state, true)) {
- if (empty($lf_list[$v['customer_id']])) {
- $lf_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- if (in_array($v['state'], $wc_state, true)) {
- if (empty($wc_list[$v['customer_id']])) {
- $wc_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- }
- // 查出来签单的数据
- $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $qd_where[] = ['Customer.org_id', 'in', $sub_org_ids];
- $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state];
- $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
- ->view('Customer', 'org_id', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($qd_where)
- ->select()
- ->toArray();
- $qd_list = [];
- foreach ($qd_log_list as $k => $v) {
- $qd_list[$v['org_id']][] = $v['customer_id'];
- }
- // 每个员工的跟进记录
- $employee_log = [];
- foreach ($log_list as $k => $v) {
- if (isset($customer_employee_id[$v['customer_id']])) {
- $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
- } else {
- $employee_log[$v['employee_id']][] = $v;
- }
- }
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- foreach ($sub_org as $k => $v) {
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0) {
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- // 所有客户量
- $cusdata = [];
- $s_list = []; // log
- foreach ($org_employee_ids as $kk => $vv) {
- if (isset($employee_customer[$vv])) {
- $cusdata = array_merge($cusdata, $employee_customer[$vv]);
- }
- if (isset($employee_log[$vv])) {
- $s_list = array_merge($s_list, $employee_log[$vv]);
- }
- }
- $all_customer_count = count($cusdata);
- $list = [];
- foreach ($s_list as $kk => $vv){
- $list[$vv['customer_id']][] = $vv;
- }
- $customer_ids = [];
- switch ($times){
- case 1:
- // 到店次数
- $one_arrival = 0; // 一次到店量
- $one_days = 0; // 一次到店天数间隔
- $lf_count = 0; // 量房转到店
- $lf_days = 0;
- $wc_count = 0; // 外场转到店
- $wc_days = 0;
- $nc_count = 0; // 内场活动到店
- foreach ($list as $kk => $vv){
- if (count($vv) == 1) {
- $one_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600));
- $one_days += $days;
- if (isset($lf_list[$kk])) {
- $lf_count ++;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600));
- $lf_days += $days;
- }
-
- if (isset($wc_list[$kk])) {
- $wc_count ++;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600));
- $wc_days += $days;
- }
-
- // 内场活动到店
- if (!empty($vv[0]['aid'])){
- $nc_count ++;
- }
- }
- }
- $qd_find = [];
- foreach ($sub_org_ids as $o_id) {
- if (isset($qd_list[$o_id])) {
- $qd_find = array_merge($qd_find, $qd_list[$o_id]);
- }
- }
- $one_qd = count(array_unique(array_intersect($qd_find, $customer_ids)));
- $sub_org[$k]['count'] = $one_arrival;
- $sub_org[$k]['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
- $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
- $sub_org[$k]['lf_count'] = $lf_count;
- $sub_org[$k]['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
- $sub_org[$k]['wc_count'] = $wc_count;
- $sub_org[$k]['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
- $sub_org[$k]['nc_count'] = $nc_count;
- $sub_org[$k]['qd_count'] = $one_qd;
- break;
- case 2:
- // 到店次数
- $two_arrival = 0; // 到店量
- $two_days = 0; // 到店天数间隔
- foreach ($list as $kk => $vv){
- if (count($vv) == 2) {
- $two_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600));
- $two_days += $days;
- }
- }
- $qd_find = [];
- foreach ($sub_org_ids as $o_id) {
- if (isset($qd_list[$o_id])) {
- $qd_find = array_merge($qd_find, $qd_list[$o_id]);
- }
- }
- $two_qd = count(array_unique(array_intersect($qd_find, $customer_ids)));
- $sub_org[$k]['count'] = $two_arrival;
- $sub_org[$k]['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
- $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
- $sub_org[$k]['qd_count'] = $two_qd;
- break;
- case 3:
- $three_arrival = 0; // 到店量
- $three_days = 0; // 到店天数间隔
- foreach ($list as $kk => $vv){
- if (count($vv) > 2) {
- $three_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600));
- $three_days += $days;
- }
- }
- $qd_find = [];
- foreach ($sub_org_ids as $o_id) {
- if (isset($qd_list[$o_id])) {
- $qd_find = array_merge($qd_find, $qd_list[$o_id]);
- }
- }
- $three_qd = count(array_unique(array_intersect($qd_find, $customer_ids)));
- $sub_org[$k]['count'] = $three_arrival;
- $sub_org[$k]['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
- $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
- $sub_org[$k]['qd_count'] = $three_qd;
- break;
- default:
- break;
- }
- // 邀约计算
- unset($subscribe_where);
- $subscribe_where[] = ['state', 'in', [0, 1]];
- $subscribe_where[] = ['type', '=', 1];
- $subscribe_where[] = ['customer_id', 'in', $customer_ids];
- $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
- $xs_count = 0; // 销售一次
- $sj_count = 0; // 设计一次
- foreach ($subscribe as $sub_v){
- if (empty($sub_v['designer_id'])){
- $xs_count ++;
- } else {
- $sj_count ++;
- }
- }
- $sub_org[$k]['xs_yaoyue'] = $xs_count;
- $sub_org[$k]['xs_yaoyue_lv'] = $sub_org[$k]['count'] > 0 ? round($xs_count * 100 / $sub_org[$k]['count'], 2) : 0;
- $sub_org[$k]['sj_yaoyue'] = $sj_count;
- $sub_org[$k]['sj_yaoyue_lv'] = $sub_org[$k]['count'] > 0 ? round($sj_count * 100 / $sub_org[$k]['count'], 2) : 0;
- }
- $order = input('order', '', 'trim');
- if ($order) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'count';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'count':
- $field = 'count';
- break;
- case 'wc_count':
- $field = 'wc_count';
- break;
- case 'qd_count':
- $field = 'qd_count';
- break;
- case 'lv':
- $field = 'lv';
- break;
- default:
- $field = 'notFound';
- break;
- }
- if ($sort_order == 'asc'){
- $sort = SORT_ASC;
- } else {
- $sort = SORT_DESC;
- }
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> self::success, 'data'=> $sub_org, 'msg'=> 'success']);
- }
- /**
- * 到店统计(员工)
- */
- public function arrivalEmployee(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $times = input('times', 0, 'intval');
- if (!in_array($times, [1,2,3])){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
- $all_where[] = ['org_id', '=', $org_id];
- $all_where[] = ['state', 'NOT NULL', null];
-
- $all_customer_list = Customer::where($all_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['state', 'not in', $not_sure];
- $or1[] = ['crm_res_id', '>', 0];
- $or2[] = ['crm_res_id', 'null', null];
- $query->whereOr([$or1, $or2]);
- })->column('id,employee_id,died,sign_time,state');
-
- $customer = []; // 时间段内有效
- $wuxiao = Customer::changeState('无效', 'chaos');
- $start_totime = strtotime($start_time);
- $end_totime = strtotime($end_time);
- // 每个员工的客户
- $employee_customer = [];
- $customer_employee_id = []; // 客户所属员工id
- foreach ($all_customer_list as $k => $v) {
- $customer_employee_id[$v['id']] = $v['employee_id'];
- if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
- $customer[] = $v;
- if (isset($employee_customer[$v['employee_id']])) {
- $employee_customer[$v['employee_id']][] = $v;
- } else {
- $employee_customer[$v['employee_id']] = [];
- $employee_customer[$v['employee_id']][] = $v;
- }
- }
- }
-
- $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
- $db_where[] = ['Customer.org_id', '=', $org_id];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
- $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,addtime,employee_id,confirm_date,aid')
- ->view('Customer', 'addtime customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->select()
- ->toArray();
- // 先查出来后面要用到的已量房、已到场记录
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $lw_where[] = ['Customer.org_id', '=', $org_id];
- $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
- $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
- $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($lw_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
-
- $lf_list = [];
- $wc_list = [];
- foreach ($lw_log_list as $k => $v) {
- if (in_array($v['state'], $lf_state, true)) {
- if (empty($lf_list[$v['customer_id']])) {
- $lf_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- if (in_array($v['state'], $wc_state, true)) {
- if (empty($wc_list[$v['customer_id']])) {
- $wc_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- }
- // 查出来签单的数据
- $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $qd_where[] = ['Customer.org_id', '=', $org_id];
- $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state];
- $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
- ->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($qd_where)
- ->select()
- ->toArray();
- $qd_list = [];
- foreach ($qd_log_list as $k => $v) {
- $qd_list[$v['customer_id']] = 1;
- }
- $employee_log = [];
- foreach ($visit_log as $k => $v) {
- if (isset($customer_employee_id[$v['customer_id']])) {
- $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
- } else {
- $employee_log[$v['employee_id']][] = $v;
- }
- }
- foreach ($employee_list as $k => $v) {
- // 所有客户量
- $all_customer_count = 0;
- if (isset($employee_customer[$v['id']])) {
- $all_customer_count = count($employee_customer[$v['id']]);
- }
- $list = [];
- $v_visit_log = [];
- if (isset($employee_log[$v['id']])) {
- $v_visit_log = $employee_log[$v['id']];
- }
- foreach ($v_visit_log as $kk => $vv){
- $list[$vv['customer_id']][] = $vv;
- }
- $customer_ids = [];
- switch ($times){
- case 1:
- // 到店次数
- $one_arrival = 0; // 一次到店量
- $one_days = 0; // 一次到店天数间隔
- $lf_count = 0; // 量房转到店
- $lf_days = 0;
- $wc_count = 0; // 外场转到店
- $wc_days = 0;
- $nc_count = 0; // 内场活动到店
- $one_qd = 0;
- foreach ($list as $kk => $vv){
- if (count($vv) == 1) {
- $one_arrival ++;
- $customer_id[] = $kk;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600));
- $one_days += $days;
- if (isset($qd_list[$kk])) {
- $one_qd ++;
- }
- if (isset($lf_list[$kk])) {
- $lf_count ++;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600));
- $lf_days += $days;
- }
-
- if (isset($wc_list[$kk])) {
- $wc_count ++;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600));
- $wc_days += $days;
- }
-
- // 内场活动到店
- if (!empty($vv[0]['aid'])){
- $nc_count ++;
- }
- }
- }
- $employee_list[$k]['count'] = $one_arrival;
- $employee_list[$k]['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
- $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
- $employee_list[$k]['lf_count'] = $lf_count;
- $employee_list[$k]['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
- $employee_list[$k]['wc_count'] = $wc_count;
- $employee_list[$k]['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
- $employee_list[$k]['nc_count'] = $nc_count;
- $employee_list[$k]['qd_count'] = $one_qd;
- break;
- case 2:
- // 到店次数
- $two_arrival = 0; // 到店量
- $two_days = 0; // 到店天数间隔
- $two_qd = 0;
- foreach ($list as $kk => $vv){
- if (count($vv) == 2) {
- $two_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600));
- $two_days += $days;
- if (isset($qd_list[$kk])) {
- $two_qd ++;
- }
- }
- }
- $employee_list[$k]['count'] = $two_arrival;
- $employee_list[$k]['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
- $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
- $employee_list[$k]['qd_count'] = $two_qd;
- break;
- case 3:
- $three_arrival = 0; // 到店量
- $three_days = 0; // 到店天数间隔
- $three_qd = 0;
- foreach ($list as $kk => $vv){
- if (count($vv) > 2) {
- $three_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600));
- $three_days += $days;
- if (isset($qd_list[$kk])) {
- $three_qd ++;
- }
- }
- }
- $employee_list[$k]['count'] = $three_arrival;
- $employee_list[$k]['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
- $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
- $employee_list[$k]['qd_count'] = $three_qd;
- break;
- default:
- break;
- }
- // 邀约计算
- unset($subscribe_where);
- $subscribe_where[] = ['state', 'in', [0, 1]];
- $subscribe_where[] = ['type', '=', 1];
- $subscribe_where[] = ['customer_id', 'in', $customer_ids];
- $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
- $xs_count = 0; // 销售一次
- $sj_count = 0; // 设计一次
- foreach ($subscribe as $sub_v){
- if (empty($sub_v['designer_id'])){
- $xs_count ++;
- } else {
- $sj_count ++;
- }
- }
- $employee_list[$k]['xs_yaoyue'] = $xs_count;
- $employee_list[$k]['xs_yaoyue_lv'] = $employee_list[$k]['count'] > 0 ? round($xs_count * 100 / $employee_list[$k]['count'], 2) : 0;
- $employee_list[$k]['sj_yaoyue'] = $sj_count;
- $employee_list[$k]['sj_yaoyue_lv'] = $employee_list[$k]['count'] > 0 ? round($sj_count * 100 / $employee_list[$k]['count'], 2) : 0;
- }
- $order = input('order', '', 'trim');
- if ($order) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'count';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'count':
- $field = 'count';
- break;
- case 'wc_count':
- $field = 'wc_count';
- break;
- case 'qd_count':
- $field = 'qd_count';
- break;
- case 'lv':
- $field = 'lv';
- break;
- default:
- $field = 'notFound';
- break;
- }
- if ($sort_order == 'asc'){
- $sort = SORT_ASC;
- } else {
- $sort = SORT_DESC;
- }
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $page = input('page', 1, 'intval');
- $limit = input('limit', 10, 'intval');
- $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit);
- return json(['code'=> self::success, 'data'=> $employee_list, 'msg'=> 'success']);
- }
- /**
- * 到店统计(客户)
- */
- public function arrivalCustomer(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $times = input('times', 0, 'intval');
- if (!in_array($times, [1,2,3])){
- return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
- }
- $employee_id = input('employee_id', '', 'intval');
- $sub_org = orgSubIds($token['org_id']);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- if (!in_array($employee_id, $sub_employee_ids)){
- return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
- }
- $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
- $wuxiao = Customer::changeState('无效', 'chaos');
- $db_where[] = ['Customer.employee_id', '=', $employee_id];
- $db_where[] = ['Customer.state', 'NOT NULL', null];
- $db_where[] = ['Customer.state', 'not in', $wuxiao];
- $db_where[] = ['Customer.died', '<>', 2];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
- $s_list = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date')
- ->view('Customer', 'sign_time customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- $list = [];
- foreach ($s_list as $k => $v){
- $list[$v['customer_id']][] = $v;
- }
- // 先查出来后面要用到的已量房、已到场记录
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $lw_where[] = ['Customer.employee_id', '=', $employee_id];
- $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
- $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
- $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($lw_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
-
- $lf_list = [];
- $wc_list = [];
- foreach ($lw_log_list as $k => $v) {
- if (in_array($v['state'], $lf_state, true)) {
- if (empty($lf_list[$v['customer_id']])) {
- $lf_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- if (in_array($v['state'], $wc_state, true)) {
- if (empty($wc_list[$v['customer_id']])) {
- $wc_list[$v['customer_id']] = $v['confirm_date'];
- }
- }
- }
- // 查出来签单的数据
- $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $qd_where[] = ['Customer.employee_id', '=', $employee_id];
- $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state];
- $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
- ->view('Customer', 'org_id', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($qd_where)
- ->select()
- ->toArray();
- $qd_list = [];
- foreach ($qd_log_list as $k => $v) {
- $qd_list[$v['customer_id']] = 1;
- }
- $wuxiao = Customer::changeState('无效', 'chaos');
- $not_sure = Customer::changeState('待确认', 'chaos');
- $wuxiao_not_sure = array_merge($wuxiao, $not_sure);
- $all_where[] = ['employee_id', '=', $employee_id];
- $all_where[] = ['state', 'NOT NULL', null];
- $all_where[] = ['state', 'NOT IN', $wuxiao_not_sure];
- $all_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $all_where[] = ['died', '<>', 2];
-
- $all_customer_count = Customer::where($all_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['state', 'not in', $not_sure];
- $or1[] = ['crm_res_id', '>', 0];
- $or2[] = ['crm_res_id', 'null', null];
- $query->whereOr([$or1, $or2]);
- })->count();
- $employee_data['count'] = 0;
- $employee_data['days'] = 0;
- $employee_data['lv'] = 0;
- $employee_data['lf_count'] = 0;
- $employee_data['lf_days'] = 0;
- $employee_data['wc_count'] = 0;
- $employee_data['wc_days'] = 0;
- $employee_data['nc_count'] = 0;
- $employee_data['qd_count'] = 0;
- $customer_ids = [];
- switch ($times){
- case 1:
- // 到店次数
- $one_arrival = 0; // 一次到店量
- $one_days = 0; // 一次到店天数间隔
- $lf_count = 0; // 量房转到店
- $lf_days = 0;
- $wc_count = 0; // 外场转到店
- $wc_days = 0;
- $nc_count = 0; // 内场活动到店
- $one_qd = 0;
- foreach ($list as $kk => $vv){
- if (count($vv) == 1) {
- $one_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600));
- $one_days += $days;
- if (isset($qd_list[$kk])) {
- $one_qd ++;
- }
- if (isset($lf_list[$kk])) {
- $lf_count ++;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600));
- $lf_days += $days;
- }
-
- if (isset($wc_list[$kk])) {
- $wc_count ++;
- $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600));
- $wc_days += $days;
- }
-
- // 内场活动到店
- if (!empty($vv[0]['aid'])){
- $nc_count ++;
- }
- }
- }
- $employee_data['count'] = $one_arrival;
- $employee_data['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
- $employee_data['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
- $employee_data['lf_count'] = $lf_count;
- $employee_data['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
- $employee_data['wc_count'] = $wc_count;
- $employee_data['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
- $employee_data['nc_count'] = $nc_count;
- $employee_data['qd_count'] = $one_qd;
- break;
- case 2:
- // 到店次数
- $two_arrival = 0; // 到店量
- $two_days = 0; // 到店天数间隔
- $two_qd = 0;
- foreach ($list as $kk => $vv){
- if (count($vv) == 2) {
- $two_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600));
- $two_days += $days;
- if (isset($qd_list[$kk])) {
- $two_qd ++;
- }
- }
- }
- $employee_data['count'] = $two_arrival;
- $employee_data['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
- $employee_data['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
- $employee_data['qd_count'] = $two_qd;
- break;
- case 3:
- $three_arrival = 0; // 到店量
- $three_days = 0; // 到店天数间隔
- $three_qd = 0;
- foreach ($list as $kk => $vv){
- if (count($vv) > 2) {
- $three_arrival ++;
- $customer_ids[] = $kk;
- $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600));
- $three_days += $days;
- if (isset($qd_list[$kk])) {
- $three_qd ++;
- }
- }
- }
- $employee_data['count'] = $three_arrival;
- $employee_data['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
- $employee_data['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
- $employee_data['qd_count'] = $three_qd;
- break;
- default:
- break;
- }
- // 邀约计算
- $subscribe_where[] = ['state', 'in', [0, 1]];
- $subscribe_where[] = ['type', '=', 1];
- $subscribe_where[] = ['customer_id', 'in', $customer_ids];
- $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
- $xs_count = 0; // 销售一次
- $sj_count = 0; // 设计一次
- foreach ($subscribe as $sub_v){
- if (empty($sub_v['designer_id'])){
- $xs_count ++;
- } else {
- $sj_count ++;
- }
- }
- $employee_data['xs_yaoyue'] = $xs_count;
- $employee_data['xs_yaoyue_lv'] = $employee_data['count'] > 0 ? round($xs_count * 100 / $employee_data['count'], 2) : 0;
- $employee_data['sj_yaoyue'] = $sj_count;
- $employee_data['sj_yaoyue_lv'] = $employee_data['count'] > 0 ? round($sj_count * 100 / $employee_data['count'], 2) : 0;
- $employee_name = Employee::where('id', $employee_id)->value('name');
- $employee_data['employee_name'] = $employee_name;
- $data['employee'] = $employee_data;
- $customer_id = [];
- $customer_log_latest = [];
- foreach ($list as $k => $v) {
- switch ($times) {
- case 1:
- if (count($v) == 1) {
- $customer_id[] = $k;
- $customer_log_latest[$k] = $v[0]['confirm_date'];
- }
- break;
- case 2:
- if (count($v) == 2) {
- $customer_id[] = $k;
- $customer_log_latest[$k] = $v[1]['confirm_date'];
- }
- break;
- case 3:
- if (count($v) > 2) {
- $customer_id[] = $k;
- $add_key = count($v) - 1;
- $customer_log_latest[$k] = $v[$add_key]['confirm_date'];
- }
- break;
- default:
- break;
- }
- }
- $type = input('type', '', 'trim');
- if ($type){
- switch ($type){
- case 'waichang':
- $customer_id = [];
- // 外场转到店
- $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- foreach ($customer_log_latest as $k => $v) {
- unset($wc_where);
- $wc_where[] = ['customer_id', '=', $k];
- $wc_where[] = ['state', 'in', $wc_state];
- $wc_where[] = ['confirm_date', '<', $v];
- $wc_find = CustomerVisitLog::where($wc_where)->order('confirm_date asc')->findOrEmpty();
- if (!$wc_find->isEmpty()){
- $customer_id[] = $k;
- }
- }
- break;
- case 'liangfang':
- $customer_id = [];
- // 量房转到店
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- foreach ($customer_log_latest as $k => $v) {
- unset($lf_where);
- $lf_where[] = ['customer_id', '=', $k];
- $lf_where[] = ['state', 'in', $lf_state];
- $lf_where[] = ['confirm_date', '<', $v];
- $lf_find = CustomerVisitLog::where($lf_where)->order('confirm_date asc')->findOrEmpty();
- if (!$lf_find->isEmpty()) {
- $customer_id[] = $k;
- }
- }
- break;
- case 'qiandan':
- // 签单
- $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
- $jd_where[] = ['customer_id', 'in', $customer_id];
- $jd_where[] = ['state', 'in', $jd_state];
- $customer_id = CustomerVisitLog::where($jd_where)->column('customer_id');
- break;
- default:
- $customer_id = [];
- break;
- }
- }
- $page = input('page', 1, 'intval');
- $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select();
- $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray();
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach ($customers as $k => $v) {
- $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- }
- $data['customer'] = $customers;
- return json(['code'=> self::success, 'data'=> $data, 'msg'=> 'success']);
- }
- /**
- * 量房统计
- */
- public function measure_house_index()
- {
- $token = $this->request->token;
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', '', 'intval');
- $org_id = !empty($org_id) ? $org_id : $token['org_id'];
-
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- //员工数量
- $org_name = Org::where('id', $org_id)->value('name');
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- // 所有客户量
- $wuxiao = Customer::changeState('无效', 'chaos');
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $c_where[] = ['org_id', 'in', $sub_org];
- $c_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $c_where[] = ['state', 'NOT NULL', null];
- $c_where[] = ['state', 'not in', $wuxiao];
- $c_where[] = ['died', '<>', 2];
-
- $cusdata = Customer::where($c_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- })->column('id,addtime');
- $db_where[] = ['Customer.org_id', 'in', $sub_org];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
- $list = Db::view('CustomerVisitLog', 'id,customer_id,measure_room_img_type,addtime,confirm_date')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->select()
- ->toArray();
- $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
- $meatime = [];
- $ylids = [];
- foreach($list as $key => $val){
- if(!empty($val['measure_room_img_type'])){
- $allmea = explode(',', $val['measure_room_img_type']);
- foreach($allmea as $k => $v){
- if(isset($v) && strstr($v, '量房图片')) $mesimg++;
- if(isset($v) && strstr($v, '量房报告图片')) $demand++;
- if(isset($v) && strstr($v, '客户需求图片')) $report++;
- }
- }
- if(!in_array($val['customer_id'], array_keys($meatime))) $meatime[$val['customer_id']] = $val['confirm_date'];
- if(!in_array($val['customer_id'], $ylids)) $ylids[] = $val['customer_id'];
- }
- $ylcus = Customer::where('id', 'in', $ylids)->field('id,addtime')->select()->toArray();
- $agvtime = [];
- foreach($ylcus as $key => $val){
- if(!empty($meatime[$val['id']])) $agvtime[] = strtotime($meatime[$val['id']]) - strtotime($val['addtime']) > 0 ? strtotime($meatime[$val['id']]) - strtotime($val['addtime']) : 0;
- }
- $meahounum = count($list);
- $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
- $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0;
- $data = ['org_name'=> $org_name, 'employee_count'=> count($sub_employee_ids), 'report'=> $report, 'demand'=> $demand, 'mesimg'=> $mesimg, 'meahounum'=> $meahounum, 'agvcycle'=> $agvcycle, 'mearate'=> $mearate];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 量房部门数据
- */
- public function measure_house_org(){
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order']);
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', '', 'intval');
- if (!$org_id) {
- $org_id = $token['org_id'];
- }
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- // 所有客户量 用于后面计算百分比
- $sub_org_ids = orgSubIds($org_id);
- $c_where[] = ['org_id', 'in', $sub_org_ids];
- $c_where[] = ['state', 'NOT NULL', null];
-
- $customer_all = Customer::where($c_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- })->column('id,addtime,employee_id,died,sign_time,state');
- $customer = []; // 时间段内有效
- $wuxiao = Customer::changeState('无效', 'chaos');
- $start_totime = strtotime($start_time);
- $end_totime = strtotime($end_time);
- $customer_employee_id = []; // 客户所属员工id
- // 每个员工的客户
- $employee_customer = [];
- foreach ($customer_all as $k => $v) {
- $customer_employee_id[$v['id']] = $v['employee_id'];
- if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
- $customer[] = $v;
- if (isset($employee_customer[$v['employee_id']])) {
- $employee_customer[$v['employee_id']][] = $v;
- } else {
- $employee_customer[$v['employee_id']] = [];
- $employee_customer[$v['employee_id']][] = $v;
- }
- }
- }
-
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $db_where[] = ['Customer.org_id', 'in', $sub_org_ids];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
- $log_list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date,employee_id')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- // 每个员工的跟进记录
- $employee_log = [];
- foreach ($log_list as $k => $v) {
- if (isset($customer_employee_id[$v['customer_id']])) {
- $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
- } else {
- $employee_log[$v['employee_id']][] = $v;
- }
- }
- foreach ($sub_org as $k => $v){
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0){
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- unset($meatime);
- unset($ylids);
- unset($agvtime);
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- $cusdata = [];
- foreach ($org_employee_ids as $kk => $vv) {
- if (isset($employee_customer[$vv])) {
- $cusdata = array_merge($cusdata, $employee_customer[$vv]);
- }
- }
-
- $list = [];
- foreach ($org_employee_ids as $kk => $vv) {
- if (isset($employee_log[$vv])) {
- $list = array_merge($list, $employee_log[$vv]);
- }
- }
- $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
- $meatime = [];
- $ylids = [];
- foreach($list as $key => $val){
- if(!empty($val['measure_room_img_type'])){
- $allmea = explode(',', $val['measure_room_img_type']);
- foreach($allmea as $m=>$p){
- if(isset($p) && strstr($p,'量房图片')) $mesimg++;
- if(isset($p) && strstr($p,'量房报告图片')) $demand++;
- if(isset($p) && strstr($p,'客户需求图片')) $report++;
- }
- }
- if(!in_array($val['customer_id'], array_keys($meatime))) $meatime[$val['customer_id']] = $val['confirm_date'];
- if(!in_array($val['customer_id'], $ylids)) $ylids[] = $val['customer_id'];
- }
- $ylcus = Customer::where('id', 'in', $ylids)->field('id,sign_time')->select()->toArray();
- $agvtime = [];
- foreach($ylcus as $key => $val){
- if(!empty($meatime[$val['id']])) $agvtime[] = (strtotime($meatime[$val['id']]) - strtotime($val['sign_time'])) > 0 ? strtotime($meatime[$val['id']]) - strtotime($val['sign_time']) : 0;
- }
- $meahounum = count($list);
- $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
- $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0;
- $sub_org[$k]['report'] = $report;
- $sub_org[$k]['demand'] = $demand;
- $sub_org[$k]['mesimg'] = $mesimg;
- $sub_org[$k]['meahounum'] = $meahounum;
- $sub_org[$k]['agvcycle'] = $agvcycle;
- $sub_org[$k]['mearate'] = $mearate;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'meahounum';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'meahounum':
- $field = 'meahounum';
- break;
- case 'agvcycle':
- $field = 'agvcycle';
- break;
- case 'mearate':
- $field = 'mearate';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
- }
- /**
- * 量房员工数据
- */
- public function measure_house_employee(){
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- // 所选部门
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- $root_sub = orgSubIds($token['root_org']);
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
- // 所有客户量
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $c_where[] = ['org_id', '=', $org_id];
- $c_where[] = ['state', 'NOT NULL', null];
-
- $customer_all = Customer::where($c_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- })->column('id,addtime,employee_id,died,sign_time,state');
- $customer = []; // 时间段内有效
- $wuxiao = Customer::changeState('无效', 'chaos');
- $start_totime = strtotime($start_time);
- $end_totime = strtotime($end_time);
- // 每个员工的客户
- $employee_customer = [];
- $customer_employee_id = []; // 客户所属员工id
- foreach ($customer_all as $k => $v) {
- $customer_employee_id[$v['id']] = $v['employee_id'];
- if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
- $customer[] = $v;
- if (isset($employee_customer[$v['employee_id']])) {
- $employee_customer[$v['employee_id']][] = $v;
- } else {
- $employee_customer[$v['employee_id']] = [];
- $employee_customer[$v['employee_id']][] = $v;
- }
- }
- }
- $db_where[] = ['Customer.org_id', '=', $org_id];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
- $log_list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date,employee_id')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- // 每个员工的跟进记录
- $employee_log = [];
- foreach ($log_list as $k => $v) {
- if (isset($customer_employee_id[$v['customer_id']])) {
- $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
- } else {
- $employee_log[$v['employee_id']][] = $v;
- }
- }
- foreach($employee_list as $key=>$val){
- $cusdata = [];
- if (isset($employee_customer[$val['id']])) {
- $cusdata = $employee_customer[$val['id']];
- }
- $list = [];
- if (isset($employee_log[$val['id']])) {
- $list = $employee_log[$val['id']];
- }
- $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
- $meatime =[];
- $ylids = [];
- foreach($list as $k => $v){
- if(!empty($v['measure_room_img_type'])){
- $allmea = explode(',', $v['measure_room_img_type']);
- foreach($allmea as $kk => $vv){
- if(isset($vv) && strstr($vv,'量房图片')) $mesimg++;
- if(isset($vv) && strstr($vv,'量房报告图片')) $demand++;
- if(isset($vv) && strstr($vv,'客户需求图片')) $report++;
- }
- }
- if(!in_array($v['customer_id'], array_keys($meatime))) $meatime[$v['customer_id']] = $v['confirm_date'];
- if(!in_array($v['customer_id'], $ylids)) $ylids[] = $v['customer_id'];
- }
- $ylcus = Customer::where('id','in',$ylids)->field('id,sign_time')->select()->toArray();
- $agvtime = [];
- foreach($ylcus as $k => $v){
- if(!empty($meatime[$v['id']])) $agvtime[] = (strtotime($meatime[$v['id']]) - strtotime($v['sign_time'])) > 0 ? strtotime($meatime[$v['id']]) - strtotime($v['sign_time']) : 0;
- }
- $meahounum = count($list);
- $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
- $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0;
- $employee_list[$key]['employee_count'] = count($employee_list);
- $employee_list[$key]['report'] = $report;
- $employee_list[$key]['demand'] = $demand;
- $employee_list[$key]['mesimg'] = $mesimg;
- $employee_list[$key]['meahounum'] = $meahounum;
- $employee_list[$key]['agvcycle'] = $agvcycle;
- $employee_list[$key]['mearate'] = $mearate;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'meahounum';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'meahounum':
- $field = 'meahounum';
- break;
- case 'agvcycle':
- $field = 'agvcycle';
- break;
- case 'mearate':
- $field = 'mearate';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
- }
- /**
- * 量房员工客户数据
- */
- public function measure_house_empcus()
- {
- $token = $this->request->token;
- $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]);
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $employee_id = input('employee_id', '', 'intval');
- $sub_org = orgSubIds($token['org_id']);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- if (!in_array($employee_id, $sub_employee_ids)){
- return json(['code'=> self::success, 'data'=> ['empdata'=> [], 'list'=> []], 'msg'=> 'success']);
- }
- $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
- $wuxiao = Customer::changeState('无效', 'chaos');
- $db_where[] = ['Customer.employee_id', '=', $employee_id];
- $db_where[] = ['Customer.state', 'NOT NULL', null];
- $db_where[] = ['Customer.state', 'not in', $wuxiao];
- $db_where[] = ['Customer.died', '<>', 2];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
- $list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date')
- ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- $cusids = array_unique(array_column($list,'customer_id'));
- $cuslist = Customer::with(['employee'=>function($query){
- $query->field('id,name');
- }])->where('id', 'in', $cusids)->where('employee_id', '=', $param['employee_id'])->field('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime')->page($param['page'],$param['limit'])->select()->toArray();
- $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
- if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach($cuslist as $key=>$val){
- $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
- }
- $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
- $meatime = [];
- $ylids = [];
- foreach ($list as $k => $v) {
- if (!empty($v['measure_room_img_type'])) {
- $allmea = explode(',', $v['measure_room_img_type']);
- foreach ($allmea as $kk => $vv) {
- if (isset($vv) && strstr($vv, '量房图片')) $mesimg++;
- if (isset($vv) && strstr($vv, '量房报告图片')) $demand++;
- if (isset($vv) && strstr($vv, '客户需求图片')) $report++;
- }
- }
- if (!in_array($v['customer_id'], array_keys($meatime))) $meatime[$v['customer_id']] = $v['confirm_date'];
- if (!in_array($v['customer_id'], $ylids)) $ylids[] = $v['customer_id'];
- }
- $wuxiao = Customer::changeState('无效', 'chaos');
- $not_sure = Customer::changeState('待确认', 'chaos');
- $wuxiao_not_sure = array_merge($wuxiao, $not_sure);
- $all_where[] = ['employee_id', '=', $employee_id];
- $all_where[] = ['state', 'NOT NULL', null];
- $all_where[] = ['state', 'NOT IN', $wuxiao_not_sure];
- $all_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $all_where[] = ['died', '<>', 2];
-
- $cusdata = Customer::where($all_where)->count();
- $ylcus = Customer::where('id', 'in', $ylids)->field('id,sign_time')->select()->toArray();
- $agvtime = [];
- foreach ($ylcus as $k => $v) {
- if (!empty($meatime[$v['id']])) $agvtime[] = (strtotime($meatime[$v['id']]) - strtotime($v['sign_time'])) > 0 ? strtotime($meatime[$v['id']]) - strtotime($v['sign_time']) : 0;
- }
- $meahounum = count($list);
- $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime) / count($agvtime)) / 86400) : 0;
- $mearate = $cusdata && !empty($meatime) ? round(count($meatime) / $cusdata, 2) : 0;
- $empdata = ['empname'=> $employeedata['name'], 'employee_count'=> 1, 'report'=> $report, 'demand'=> $demand, 'mesimg'=> $mesimg, 'meahounum'=> $meahounum, 'agvcycle'=> $agvcycle, 'mearate'=> $mearate];
- $data = ['empdata'=> $empdata, 'list'=> $cuslist];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 转单首页统计
- */
- public function sign_index()
- {
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time']);
- //员工数量
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- $org_name = Org::where('id',$org_id)->value('name');
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray();
- $cusdata = Customer::where([['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray();
- //$where[] = ['org_id','in',$sub_org];
- $where[] = ['employee_id','in',$sub_employee_ids];
- $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
- $where[] = ['confirm_date', 'between', [$param['start_time'] . ' 00:00:00', $param['end_time'] . ' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
- $signids = array_unique(array_column($list,'customer_id'));
- //$shopdep_list = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',array_merge(CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已交定','chaos'))]])->field('id,customer_id,state')->select()->toArray();
- $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id');
- $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id');
- $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
- $signnum = count($list);
- $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
- $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0;
- $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
- $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0;
- $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0;
- $data = ['org_name'=>$org_name,'shoprate'=>$shoprate,'depositrate'=>$depositrate,'signmoney'=>$signmoney,'signnum'=>$signnum,'signagv'=>$signagv,'signrate'=>$signrate];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 转单部门数据
- */
- public function sign_org()
- {
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order']);
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- // 所选部门
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- foreach ($sub_org as $k => $v){
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0){
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- unset($where);
- unset($meatime);
- unset($ylids);
- unset($agvtime);
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray();
- $cusdata = Customer::where([['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray();
- //$where[] = ['org_id','in',$v_sub_org];
- $where[] = ['employee_id','in',$org_employee_ids];
- $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
- $signids = array_unique(array_column($list,'customer_id'));
- //$shopdep_list = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',array_merge(CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已交定','chaos'))]])->field('id,customer_id,state')->select()->toArray();
- $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id');
- $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id');
- $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
- $signnum = count($list);
- $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
- $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0;
- $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
- $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0;
- $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0;
- $sub_org[$k]['shoprate'] = $shoprate;
- $sub_org[$k]['depositrate'] = $depositrate;
- $sub_org[$k]['signmoney'] = $signmoney;
- $sub_org[$k]['signnum'] = $signnum;
- $sub_org[$k]['signagv'] = $signagv;
- $sub_org[$k]['signrate'] = $signrate;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'signnum';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'signnum':
- $field = 'signnum';
- break;
- case 'signmoney':
- $field = 'signmoney';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
- }
- /**
- * 转单员工数据
- */
- public function sign_employee()
- {
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
- // 所选部门
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->page($param['page'],$param['limit'])->select()->toArray();
- foreach($employee_list as $key=>$val){
- unset($where);
- //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','=',$val['id']]])->field('id,addtime')->select()->toArray();
- $cusdata = Customer::where([['employee_id','=',$val['id']]])->field('id,addtime')->select()->toArray();
- $where[] = ['employee_id','=',$val['id']];
- //$where[] = ['customer_id','in',$cusids];
- $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
- $signids = array_unique(array_column($list,'customer_id'));
- //$shopdep_list = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',array_merge(CustomerVisitLog::changeState('已到店','chaos'),CustomerVisitLog::changeState('已交定','chaos'))]])->field('id,customer_id,state')->select()->toArray();
- $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id');
- $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id');
- $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
- $signnum = count($list);
- $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
- $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0;
- $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
- $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0;
- $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0;
- $employee_list[$key]['employee_count'] = count($employee_list);
- $employee_list[$key]['shoprate'] = $shoprate;
- $employee_list[$key]['depositrate'] = $depositrate;
- $employee_list[$key]['signmoney'] = $signmoney;
- $employee_list[$key]['signnum'] = $signnum;
- $employee_list[$key]['signagv'] = $signagv;
- $employee_list[$key]['signrate'] = $signrate;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'signnum';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'signnum':
- $field = 'signnum';
- break;
- case 'signmoney':
- $field = 'signmoney';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
-
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
- }
- /**
- * 签单客户数据
- */
- public function sign_empcus()
- {
- $token = $this->request->token;
- $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]);
- $where[] = ['employee_id','=',$param['employee_id']];
- $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
- $cusids = array_unique(array_column($list,'customer_id'));
- $cuslist = Customer::with(['employee'=>function($query){
- $query->field('id,name');
- }])->where('id','in',$cusids)->where('employee_id','=',$param['employee_id'])->field('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime')->page($param['page'],$param['limit'])->select()->toArray();
- $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
- if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach($cuslist as $key=>$val){
- $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
- }
- $cusdata = Customer::where([['employee_id','=',$param['employee_id']]])->field('id,addtime')->select()->toArray();
- $shopids = CustomerVisitLog::where([['customer_id', 'in', $cusids], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]])->group('customer_id')->column('customer_id');
- $depids = CustomerVisitLog::where([['customer_id', 'in', $cusids], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->group('customer_id')->column('customer_id');
- $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
- $signnum = count($list);
- $signrate = !empty($cusdata) && !empty($list) ? round(count($list) / count($cusdata), 2) * 100 : 0;
- $signagv = !empty($list) ? round(array_sum(array_column($list, 'money')) / count($list), 2) : 0;
- $signmoney = !empty($list) ? array_sum(array_column($list, 'money')) : 0;
- $depositrate = !empty($list) && !empty($depids) ? round(count($depids) / count($list), 2) * 100 : 0;
- $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids) / count($list), 2) * 100 : 0;
- $empdata = ['empname'=>$employeedata['name'],'employee_count'=>1,'shoprate'=>$shoprate,'depositrate'=>$depositrate,'signmoney'=>$signmoney,'signnum'=>$signnum,'signagv'=>$signagv,'signrate'=>$signrate];
- $data = ['empdata'=>$empdata,'list'=>$cuslist];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 交定(签单)统计首页
- */
- public function deposit_index()
- {
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time']);
- //员工数量
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- $org_name = Org::where('id',$org_id)->value('name');
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- //$cusdata = Customer::where([['addtime','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray();
- $cusdata = Customer::where([['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray();
- //$where[] = ['org_id','in',$sub_org];
- $where[] = ['employee_id','in',$sub_employee_ids];
- $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
- $depids = array_unique(array_column($list,'customer_id'));
- $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0;
- $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
- foreach($list as $key=>$val){
- if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date'];
- if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id'];
- }
- $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
- $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray();
- $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['org_id','in',$sub_org]])->field('id,addtime,customer_id,state')->select()->toArray();
-
- foreach($yddvlg as $key=>$val){
- $yddarr[$val['customer_id']][]=$val;
- }
- foreach($yddarr as $key=>$val){
- if(empty($val)) continue;
- if(count($val)==1){
- $oneshop_depnum++;
- $oneshop_ids[] = $key;
- }
- if(count($val)==2){
- $twoshop_depnum++;
- $twoshop_ids[] = $key;
- }
- if(count($val)>=3){
- $threeshop_depnum++;
- $threeshop_ids[] = $key;
- }
- }
- foreach($yjdcus as $key=>$val){
- if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($val['package_id'])){
- $allpid = explode(',',$val['package_id']);
- foreach($allpid as $k=>$v){
- $alljdpid[]=$v;
- }
- }
- }
- foreach($package as $key=>$val){
- $package[$key]['num'] = 0;
- foreach($alljdpid as $k=>$v){
- if($val['id']==$v){
- $package[$key]['num']++;
- }
- }
- }
- $depnum = count($list);
- $deprate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
- $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
- $depmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
- //$deppacnum = count($alljdpid);
- $oneshop_depnum = $oneshop_depnum;
- $twoshop_depnum = $twoshop_depnum;
- $threeshop_depnum = $threeshop_depnum;
- $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0;
- $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0;
- $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0;
- $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0;
- $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0;
- $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0;
- $package_depnum = !empty($package) ? array_sum(array_column($package,'num')) : 0;
- $data = ['org_name'=>$org_name,'package'=>$package,'package_depnum'=>$package_depnum,'depnum'=>$depnum,'deprate'=>$deprate,'agvcycle'=>$agvcycle,'depmoney'=>$depmoney,'deppacnum'=>$deppacnum,'oneshop_depnum'=>$oneshop_depnum,'twoshop_depnum'=>$twoshop_depnum,'threeshop_depnum'=>$threeshop_depnum,'oneshop_deprate'=>round($oneshop_deprate,2),'twoshop_deprate'=>round($twoshop_deprate,2),'threeshop_deprate'=>round($threeshop_deprate,2),'one_agvcycle'=>$one_agvcycle,'two_agvcycle'=>$two_agvcycle,'three_agvcycle'=>$three_agvcycle];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 签订部门数据
- */
- public function deposit_org()
- {
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order']);
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- // 所选部门
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- foreach ($sub_org as $k => $v){
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0){
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- unset($where);
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- $cusdata = Customer::where([['sign_time','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray();
- //$where[] = ['org_id','in',$v_sub_org];
- $where[] = ['employee_id','in',$org_employee_ids];
- $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
- $depids = array_unique(array_column($list,'customer_id'));
- $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0;
- $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
- foreach($list as $key=>$val){
- if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date'];
- if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id'];
- }
- $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
- $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray();
- $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['org_id','in',$v_sub_org]])->field('id,addtime,customer_id,state')->select()->toArray();
-
- foreach($yddvlg as $key=>$val){
- $yddarr[$val['customer_id']][]=$val;
- }
- foreach($yddarr as $key=>$val){
- if(empty($val)) continue;
- if(count($val)==1){
- $oneshop_depnum++;
- $oneshop_ids[] = $key;
- }
- if(count($val)==2){
- $twoshop_depnum++;
- $twoshop_ids[] = $key;
- }
- if(count($val)>=3){
- $threeshop_depnum++;
- $threeshop_ids[] = $key;
- }
- }
- foreach($yjdcus as $key=>$val){
- if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($val['package_id'])){
- $allpid = explode(',',$val['package_id']);
- foreach($allpid as $m=>$p){
- $alljdpid[]=$p;
- }
- }
- }
- foreach($package as $key=>$val){
- $package[$key]['num'] = 0;
- foreach($alljdpid as $m=>$p){
- //if(in_array($val['id'],$alljdpid)){
- if($val['id']==$p){
- $package[$key]['num']++;
- }
- }
- }
- $sub_org[$k]['depnum'] = count($list);
- $sub_org[$k]['deprate'] = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
- $sub_org[$k]['agvcycle'] = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
- $sub_org[$k]['depmoney'] = !empty($list) ? array_sum(array_column($list,'money')) : 0;
- $sub_org[$k]['deppacnum'] = count($alljdpid);
- $sub_org[$k]['oneshop_depnum'] = $oneshop_depnum;
- $sub_org[$k]['twoshop_depnum'] = $twoshop_depnum;
- $sub_org[$k]['threeshop_depnum'] = $threeshop_depnum;
- $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0;
- $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0;
- $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0;
- $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0;
- $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0;
- $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0;
- $sub_org[$k]['oneshop_deprate'] = round($oneshop_deprate,2);
- $sub_org[$k]['twoshop_deprate'] = round($twoshop_deprate,2);
- $sub_org[$k]['threeshop_deprate'] = round($threeshop_deprate,2);
- $sub_org[$k]['one_agvcycle'] = $one_agvcycle;
- $sub_org[$k]['two_agvcycle'] = $two_agvcycle;
- $sub_org[$k]['three_agvcycle'] = $three_agvcycle;
- $sub_org[$k]['package'] = $package;
- $sub_org[$k]['package_depnum'] = !empty($package) ? array_sum(array_column($package,'num')) : 0;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'depnum';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'depnum':
- $field = 'depnum';
- break;
- case 'package_depnum':
- $field = 'package_depnum';
- break;
- case 'depmoney':
- $field = 'depmoney';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
- }
- /**
- * 签订员工数据
- */
- public function deposit_employee()
- {
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
- // 所选部门
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
- foreach($employee_list as $k=>$v){
- unset($where);
- $cusdata = Customer::where([['sign_time','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','=',$v['id']]])->field('id,sign_time')->select()->toArray();
- $where[] = ['employee_id','=',$v['id']];
- //$where[] = ['customer_id','in',$cusids];
- $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
- $signids = array_unique(array_column($list,'customer_id'));
- $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0;
- $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
- foreach($list as $key=>$val){
- if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date'];
- if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id'];
- }
- $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
- $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray();
- $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['employee_id','=',$v['id']]])->field('id,addtime,customer_id,state')->select()->toArray();
-
- foreach($yddvlg as $key=>$val){
- $yddarr[$val['customer_id']][]=$val;
- }
- foreach($yddarr as $key=>$val){
- if(empty($val)) continue;
- if(count($val)==1){
- $oneshop_depnum++;
- $oneshop_ids[] = $key;
- }
- if(count($val)==2){
- $twoshop_depnum++;
- $twoshop_ids[] = $key;
- }
- if(count($val)>=3){
- $threeshop_depnum++;
- $threeshop_ids[] = $key;
- }
- }
- foreach($yjdcus as $key=>$val){
- if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if(!empty($val['package_id'])){
- $allpid = explode(',',$val['package_id']);
- foreach($allpid as $m=>$p){
- $alljdpid[]=$p;
- }
- }
- }
- foreach($package as $key=>$val){
- $package[$key]['num'] = 0;
- foreach($alljdpid as $m=>$p){
- //if(in_array($val['id'],$alljdpid)){
- if($val['id']==$p){
- $package[$key]['num']++;
- }
- }
- }
- $employee_list[$k]['depnum'] = count($list);
- $employee_list[$k]['deprate'] = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
- $employee_list[$k]['agvcycle'] = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
- $employee_list[$k]['depmoney'] = !empty($list) ? array_sum(array_column($list,'money')) : 0;
- $employee_list[$k]['deppacnum'] = count($alljdpid);
- $employee_list[$k]['oneshop_depnum'] = $oneshop_depnum;
- $employee_list[$k]['twoshop_depnum'] = $twoshop_depnum;
- $employee_list[$k]['threeshop_depnum'] = $threeshop_depnum;
- $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0;
- $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0;
- $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0;
- $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0;
- $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0;
- $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0;
- $employee_list[$k]['oneshop_deprate'] = round($oneshop_deprate,2);
- $employee_list[$k]['twoshop_deprate'] = round($twoshop_deprate,2);
- $employee_list[$k]['threeshop_deprate'] = round($threeshop_deprate,2);
- $employee_list[$k]['one_agvcycle'] = round($one_agvcycle,2);
- $employee_list[$k]['two_agvcycle'] = round($two_agvcycle,2);
- $employee_list[$k]['three_agvcycle'] = round($three_agvcycle,2);
- $employee_list[$k]['package'] = $package;
- $employee_list[$k]['package_depnum'] = !empty($package) ? array_sum(array_column($package,'num')) : 0;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'depnum';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'depnum':
- $field = 'depnum';
- break;
- case 'package_depnum':
- $field = 'package_depnum';
- break;
- case 'depmoney':
- $field = 'depmoney';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
- }
- /**
- * 签订员工客户列表
- */
- public function depsignmea_empcus()
- {
- $token = $this->request->token;
- $param = $this->request->only(['employee_id', 'start_time','end_time','status','page'=>1,'limit'=>10]);
- //if(empty($param['state']) || !in_array($param['state'],['已量房','已交定','已签单'])) return json(['code'=> 1, 'msg'=> 'state参数错误']);
- $where[] = ['employee_id','=',$param['employee_id']];
- $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
- $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
- $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
-
- $deppacnum = $demand = $depmoney = $depnum = $agvcycle = $one_agvcycle = $two_agvcycle = $three_agvcycle = $deprate = $oneshop_depnum = $twoshop_depnum = $threeshop_depnum = $oneshop_deprate = $twoshop_deprate = $threeshop_deprate = $oneshop_depagv = $twoshop_depagv = $threeshop_depagv = 0;
- $youjdpid = $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
- foreach ($list as $key => $val) {
- if (!in_array($val['customer_id'], array_keys($deptime))) $deptime[$val['customer_id']] = $val['confirm_date'];
- if (!in_array($val['customer_id'], $yjdids)) $yjdids[] = $val['customer_id'];
- }
- $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
- $yjdcus = Customer::where('id', 'in', $yjdids)->field('id,sign_time,package_id')->select()->toArray();
- $yddvlg = CustomerVisitLog::where([['customer_id', 'in', $yjdids], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')], ['employee_id', '=', $param['employee_id']]])->field('id,addtime,customer_id,state')->select()->toArray();
- foreach ($yddvlg as $key => $val) {
- $yddarr[$val['customer_id']][] = $val;
- }
- foreach ($yddarr as $key => $val) {
- if (empty($val)) continue;
- if (count($val) == 1) {
- $oneshop_depnum++;
- $oneshop_ids[] = $key;
- }
- if (count($val) == 2) {
- $twoshop_depnum++;
- $twoshop_ids[] = $key;
- }
- if (count($val) >= 3) {
- $threeshop_depnum++;
- $threeshop_ids[] = $key;
- }
- }
- foreach ($yjdcus as $key => $val) {
- if (!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if (!empty($deptime[$val['id']]) && in_array($val['id'], $oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if (!empty($deptime[$val['id']]) && in_array($val['id'], $twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if (!empty($deptime[$val['id']]) && in_array($val['id'], $threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
- if (!empty($val['package_id'])) {
- $allpid = explode(',', $val['package_id']);
- foreach ($allpid as $m => $p) {
- $alljdpid[] = $p;
- }
- $youjdpid[] = $val['id'];
- }
- }
- foreach ($package as $key => $val) {
- $package[$key]['num'] = 0;
- foreach ($alljdpid as $m => $p) {
- //if(in_array($val['id'],$alljdpid)){
- if ($val['id'] == $p) {
- $package[$key]['num']++;
- }
- }
- }
- $cusids = !empty($list) ? array_unique(array_column($list,'customer_id')) : [];
- if(!empty($param['status'])){
- if($param['status']==1) $cusids = $oneshop_ids;
- if($param['status']==2) $cusids = $twoshop_ids;
- if($param['status']==3) $cusids = $threeshop_ids;
- if($param['status']==4) $cusids = $youjdpid;
- }
- $cuslist = Customer::with(['employee'=>function($query){
- $query->field('id,name');
- }])->where('id','in',$cusids)->where('employee_id','=',$param['employee_id'])->field('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime')->page($param['page'],$param['limit'])->select()->toArray();
- $cusdata = Customer::where([['sign_time','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']],['employee_id','=',$param['employee_id']]])->field('id,addtime')->select()->toArray();
- $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
- if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach($cuslist as $key=>$val){
- $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
- }
- $depnum = count($list);
- $deprate = !empty($cusdata) && !empty($list) ? sprintf("%.2f",count($list) / count($cusdata)) * 100 : 0;
- $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime) / count($agvtime)) / 86400) : 0;
- $depmoney = !empty($list) ? array_sum(array_column($list, 'money')) : 0;
- $deppacnum = count($alljdpid);
- $oneshop_depnum = $oneshop_depnum;
- $twoshop_depnum = $twoshop_depnum;
- $threeshop_depnum = $threeshop_depnum;
- $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$oneshop_depnum / count($cusdata)) * 100 : 0;
- $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$twoshop_depnum / count($cusdata)) * 100 : 0;
- $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$threeshop_depnum / count($cusdata)) * 100 : 0;
- $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime) / count($one_agvtime)) / 86400) : 0;
- $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime) / count($two_agvtime)) / 86400) : 0;
- $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime) / count($three_agvtime)) / 86400) : 0;
- $package = $package;
- $package_depnum = !empty($package) ? array_sum(array_column($package, 'num')) : 0;
- $empdata = ['empname'=>$employeedata['name'],'depnum'=>$depnum,'deprate'=>round($deprate,2),'agvcycle'=>$agvcycle,'depmoney'=>$depmoney,'deppacnum'=>$deppacnum,'oneshop_depnum'=>$oneshop_depnum,'twoshop_depnum'=>$twoshop_depnum,'threeshop_depnum'=>$threeshop_depnum,'oneshop_deprate'=>round($oneshop_deprate,2),'twoshop_deprate'=>round($twoshop_deprate,2),'threeshop_deprate'=>round($threeshop_deprate,2),'one_agvcycle'=>$one_agvcycle,'two_agvcycle'=>$two_agvcycle,'three_agvcycle'=>$three_agvcycle,'package'=>$package,'package_depnum'=>$package_depnum];
- $data = ['empdata'=>$empdata,'list'=>$cuslist];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 打电话统计(全部)
- */
- public function call(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 数据总览
- // 部门信息
- $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
- //员工数量
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $data['employee_count'] = count($sub_employee_ids);
- $where[] = ['employee_id', 'in', $sub_employee_ids];
- $where[] = ['addtime', 'between', [$start_time, $end_time]];
- $list = OutCallLog::where($where)->column('id,status,billsec');
- $count = count($list);
- $data['count'] = $count;
- $ok = 0; // 接通数量
- $long = 0; // 通话时长 s
- foreach ($list as $k => $v) {
- if ($v['status'] == 1){
- $ok ++;
- }
- $long += $v['billsec'];
- }
- $data['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : $ok * 100;
- $data['long'] = round($long / 60, 2);
- $data['average_long'] = $count > 0 ? round($long / 60 / $count, 2) : round($long / 60, 2);
- // 手机打电话次数
- $mbCount = OutCallMbLog::where($where)->count();
- $data['count'] += $mbCount;
- return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
- }
- /**
- * 打电话统计(部门)
- */
- public function callOrg(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->column('id,name,pid');
- foreach ($sub_org as $k => $v){
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0){
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- unset($where);
- $where[] = ['employee_id', 'in', $org_employee_ids];
- $where[] = ['addtime', 'between', [$start_time, $end_time]];
- $list = OutCallLog::where($where)->column('id,status,billsec');
- $count = count($list);
- $sub_org[$k]['count'] = $count;
- $ok = 0; // 接通数量
- $long = 0; // 通话时长 s
- foreach ($list as $kk => $vv) {
- if ($vv['status'] == 1){
- $ok ++;
- }
- $long += $vv['billsec'];
- }
- $sub_org[$k]['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : 0;
- $m_long = round($long / 60, 2);
- $sub_org[$k]['long'] = $m_long;
- $sub_org[$k]['average_long'] = $count > 0 ? round($long / 60 / $count, 2) : $m_long;
- // 手机打电话次数
- $mbCount = OutCallMbLog::where($where)->count();
- $sub_org[$k]['count'] += $mbCount;
- }
- $order = input('order', '', 'trim');
- if (!empty($order)) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'count';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'count':
- $field = 'count';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> self::success, 'data'=> $sub_org, 'msg'=> '请求成功']);
- }
- /**
- * 打电话统计(员工)
- */
- public function callEmployee(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->column('id,name,org_id,root_id,uid');
- $employee_ids = array_column($employee_list, 'id');
- $where[] = ['employee_id', 'in', $employee_ids];
- $where[] = ['addtime', 'between', [$start_time, $end_time]];
- $list = OutCallLog::where($where)->column('id,employee_id,status,billsec');
- $employee_data = [];
- foreach ($list as $k => $v) {
- if (isset($employee_data[$v['employee_id']])) {
- $employee_data[$v['employee_id']][] = $v;
- } else {
- $employee_data[$v['employee_id']] = [];
- $employee_data[$v['employee_id']][] = $v;
- }
- }
- // 手机打电话次数
- $mbCount = OutCallMbLog::where($where)->column('id,employee_id');
- $mbCount_data = [];
- foreach ($mbCount as $k => $v) {
- if (isset($mbCount_data[$v['employee_id']])) {
- $mbCount_data[$v['employee_id']][] = $v;
- } else {
- $mbCount_data[$v['employee_id']] = [];
- $mbCount_data[$v['employee_id']][] = $v;
- }
- }
- foreach ($employee_list as $k => $v){
- $count = 0;
- $ok = 0; // 接通数量
- $long = 0; // 通话时长 s
- if (isset($employee_data[$v['id']])) {
- $count = count($employee_data[$v['id']]);
- foreach ($employee_data[$v['id']] as $kk => $vv) {
- if ($vv['status'] == 1){
- $ok ++;
- }
- $long += $vv['billsec'];
- }
- }
-
- $employee_list[$k]['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : 0;
- $m_long = round($long / 60, 2);
- $employee_list[$k]['long'] = $m_long;
- $employee_list[$k]['average_long'] = $count > 0 ? round($m_long / $count, 2) : $m_long;
- $cmb_count = 0;
- if (isset($mbCount_data[$v['id']])) {
- $cmb_count = count($mbCount_data[$v['id']]);
- }
- $employee_list[$k]['count'] = $count + $cmb_count;
- }
- $order = input('order', '', 'trim');
- if (!empty($order)) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'count';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'count':
- $field = 'count';
- break;
- default:
- $field = 'notFound';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $page = input('page', 1, 'intval');
- $limit = input('limit', 10, 'intval');
- $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit);
- return json(['code'=> self::success, 'data'=> $employee_list, 'msg'=> '请求成功']);
- }
- /**
- * 打电话统计(客户列表)
- */
- public function callCustomer(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $employee_id = input('employee_id', '', 'intval');
- $sub_org = orgSubIds($token['org_id']);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- if (!in_array($employee_id, $sub_employee_ids)){
- return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
- }
- $where[] = ['employee_id', '=', $employee_id];
- $where[] = ['addtime', 'between', [$start_time, $end_time]];
- $list = OutCallLog::where($where)->select()->toArray();
- $customer_id = array_column($list, 'customer_id');
- $employee_data['count'] = count($list);
- $ok = 0; // 接通数量
- $long = 0; // 通话时长 s
- foreach ($list as $kk => $vv) {
- if ($vv['status'] == 1){
- $ok ++;
- }
- $long += $vv['billsec'];
- }
- $employee_data['lv'] = count($list) > 0 ? round($ok * 100 / count($list), 2) : 0;
- $m_long = round($long / 60, 2);
- $employee_data['long'] = $m_long;
- $employee_data['average_long'] = count($list) > 0 ? round($m_long / count($list), 2) : $m_long;
- $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name');
- // 手机打电话次数
- $mbCount = OutCallMbLog::where($where)->count();
- $employee_data['count'] += $mbCount;
-
- $data['employee'] = $employee_data;
- $page = input('page', 1, 'intval');
- $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select();
- $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray();
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach ($customers as $k => $v) {
- $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- }
- $data['customer'] = $customers;
- return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
- }
- /**
- * 活动列表
- */
- public function activity(){
- $token = request()->token;
- $list = Activity::where([['root_id', '=', $token['root_org']], ['cate', '=', 1]])->order('addtime desc')->field('id,title')->select()->toArray();
- return json(['code'=> self::success, 'data'=> $list]);
- }
- /**
- * 预约活动(主页)
- */
- public function subscribeActivity(){
- $token = $this->request->token;
- $id = input('id', '', 'intval');
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 数据总览
- // 部门信息
- $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
- //员工数量
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $data['employee_count'] = count($sub_employee_ids);
- $sub_where[] = ['employee_id', 'in', $sub_employee_ids];
- $sub_where[] = ['aid', '=', $id];
- $sub_where[] = ['type', '=', 2];
- $sub_where[] = ['state', 'in', [0,1]];
- $sub_where[] = ['subscribe_date', '>=', $start_time];
- $sub_where[] = ['subscribe_date', '<', $end_time];
- $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
- $sub_count = count($sub_customer);
- $data['subscribe_count'] = $sub_count;
- $dc_where[] = ['customer_employee_id', 'in', $sub_employee_ids];
- $dc_where[] = ['aid', '=', $id];
- $dc_where[] = ['confirm_date', '>=', $start_time];
- $dc_where[] = ['confirm_date', '<', $end_time];
- $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $dc_where[] = ['state', 'in', $dc_state];
- $daochang = CustomerVisitLog::where($dc_where)->count();
- $data['daochang_count'] = $daochang;
- $data['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ;
- $jd_where[] = ['customer_id', 'in', $sub_customer];
- $jd_where[] = ['confirm_date', '>=', $start_time];
- $jd_where[] = ['confirm_date', '<', $end_time];
- $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
- $jd_where[] = ['state', 'in', $jd_state];
- $jiaoding = CustomerVisitLog::where($jd_where)->count();
- $data['jiaoding_count'] = $jiaoding;
- $data['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
- $zd_where[] = ['customer_id', 'in', $sub_customer];
- $zd_where[] = ['confirm_date', '>=', $start_time];
- $zd_where[] = ['confirm_date', '<', $end_time];
- $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $zd_where[] = ['state', 'in', $zd_state];
- $zhuandan = CustomerVisitLog::where($zd_where)->count();
- $data['zhuandan_count'] = $zhuandan;
- $data['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
- return json(['code'=> self::success, 'data'=> $data]);
- }
- /**
- * 预约活动(部门)
- */
- public function subscribeActivityOrg(){
- $token = $this->request->token;
- $id = input('id', '', 'intval');
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- foreach ($sub_org as $k => $v) {
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0) {
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- unset($sub_where);
- $sub_where[] = ['employee_id', 'in', $org_employee_ids];
- $sub_where[] = ['aid', '=', $id];
- $sub_where[] = ['type', '=', 2];
- $sub_where[] = ['state', 'in', [0,1]];
- $sub_where[] = ['subscribe_date', '>=', $start_time];
- $sub_where[] = ['subscribe_date', '<', $end_time];
- $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
- $sub_count = count($sub_customer);
- $sub_org[$k]['subscribe_count'] = $sub_count;
- unset($dc_where);
- $dc_where[] = ['customer_employee_id', 'in', $org_employee_ids];
- $dc_where[] = ['aid', '=', $id];
- $dc_where[] = ['confirm_date', '>=', $end_time];
- $dc_where[] = ['confirm_date', '<', $end_time];
- $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $dc_where[] = ['state', 'in', $dc_state];
- $daochang = CustomerVisitLog::where($dc_where)->count();
- $sub_org[$k]['daochang_count'] = $daochang;
- $sub_org[$k]['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ;
- unset($jd_where);
- $jd_where[] = ['customer_id', 'in', $sub_customer];
- $jd_where[] = ['confirm_date', '>=', $start_time];
- $jd_where[] = ['confirm_date', '<', $end_time];
- $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
- $jd_where[] = ['state', 'in', $jd_state];
- $jiaoding = CustomerVisitLog::where($jd_where)->count();
- $sub_org[$k]['jiaoding_count'] = $jiaoding;
- $sub_org[$k]['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
- unset($zd_where);
- $zd_where[] = ['customer_id', 'in', $sub_customer];
- $zd_where[] = ['confirm_date', '>=', $start_time];
- $zd_where[] = ['confirm_date', '<', $end_time];
- $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $zd_where[] = ['state', 'in', $zd_state];
- $zhuandan = CustomerVisitLog::where($zd_where)->count();
- $sub_org[$k]['zhuandan_count'] = $zhuandan;
- $sub_org[$k]['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
- }
- $order = input('order', '', 'trim');
- if ($order) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'yuyue';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'yuyue':
- $field = 'subscribe_count';
- break;
- case 'daochang':
- $field = 'daochang_count';
- break;
- case 'qiandan':
- $field = 'jiaoding_count';
- break;
- case 'zhuandan':
- $field = 'zhuandan_count';
- break;
- default:
- $field = 'notFound';
- break;
- }
- if ($sort_order == 'asc'){
- $sort = SORT_ASC;
- } else {
- $sort = SORT_DESC;
- }
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- $page = input('page', 1, 'intval');
- $limit = input('limit', 10, 'intval');
- $list = array_slice($sub_org, ($page-1)*$limit, $page*$limit-1);
- return json(['code'=> self::success, 'data'=> $list]);
- }
- /**
- * 预约活动(员工)
- */
- public function subscribeActivityEmployee(){
- $token = $this->request->token;
- $id = input('id', '', 'intval');
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
- foreach ($employee_list as $k => $v) {
- unset($sub_where);
- $sub_where[] = ['employee_id', '=', $v['id']];
- $sub_where[] = ['aid', '=', $id];
- $sub_where[] = ['type', '=', 2];
- $sub_where[] = ['state', 'in', [0,1]];
- $sub_where[] = ['subscribe_date', '>=', $start_time];
- $sub_where[] = ['subscribe_date', '<', $end_time];
- $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
- $sub_count = count($sub_customer);
- $employee_list[$k]['subscribe_count'] = $sub_count;
- unset($dc_where);
- $dc_where[] = ['customer_employee_id', '=', $v['id']];
- $dc_where[] = ['aid', '=', $id];
- $dc_where[] = ['confirm_date', '>=', $start_time];
- $dc_where[] = ['confirm_date', '<', $end_time];
- $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $dc_where[] = ['state', 'in', $dc_state];
- $daochang = CustomerVisitLog::where($dc_where)->count();
- $employee_list[$k]['daochang_count'] = $daochang;
- $employee_list[$k]['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ;
- unset($jd_where);
- $jd_where[] = ['customer_id', 'in', $sub_customer];
- $jd_where[] = ['customer_employee_id', '=', $v['id']];
- $jd_where[] = ['confirm_date', '>=', $start_time];
- $jd_where[] = ['confirm_date', '<', $end_time];
- $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
- $jd_where[] = ['state', 'in', $jd_state];
- $jiaoding = CustomerVisitLog::where($jd_where)->count();
- $employee_list[$k]['jiaoding_count'] = $jiaoding;
- $employee_list[$k]['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
- unset($zd_where);
- $zd_where[] = ['customer_id', 'in', $sub_customer];
- $zd_where[] = ['customer_employee_id', '=', $v['id']];
- $zd_where[] = ['confirm_date', '>=', $start_time];
- $zd_where[] = ['confirm_date', '<', $end_time];
- $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $zd_where[] = ['state', 'in', $zd_state];
- $zhuandan = CustomerVisitLog::where($zd_where)->count();
- $employee_list[$k]['zhuandan_count'] = $zhuandan;
- $employee_list[$k]['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
- }
- $order = input('order', '', 'trim');
- if ($order) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'yuyue';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'yuyue':
- $field = 'subscribe_count';
- break;
- case 'daochang':
- $field = 'daochang_count';
- break;
- case 'qiandan':
- $field = 'jiaoding_count';
- break;
- case 'zhuandan':
- $field = 'zhuandan_count';
- break;
- default:
- $field = 'notFound';
- break;
- }
- if ($sort_order == 'asc'){
- $sort = SORT_ASC;
- } else {
- $sort = SORT_DESC;
- }
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $page = input('page', 1, 'intval');
- $limit = input('limit', 10, 'intval');
- $list = array_slice($employee_list, ($page-1)*$limit, $limit);
- return json(['code'=> self::success, 'data'=> $list]);
- }
- /**
- * 预约活动(客户)
- */
- public function subscribeActivityCustomer(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $employee_id = input('employee_id', '', 'intval');
- $sub_org = orgSubIds($token['org_id']);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- if (!in_array($employee_id, $sub_employee_ids)){
- return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
- }
- $aid = input('id', '', 'intval');
- $sub_where[] = ['employee_id', '=', $employee_id];
- $sub_where[] = ['aid', '=', $aid];
- $sub_where[] = ['type', '=', 2];
- $sub_where[] = ['state', 'in', [0,1]];
- $sub_where[] = ['subscribe_date', '>=', $start_time];
- $sub_where[] = ['subscribe_date', '<', $end_time];
- $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
- $sub_count = count($sub_customer);
- $employee_data['subscribe_count'] = $sub_count;
- $dc_where[] = ['customer_employee_id', '=', $employee_id];
- $dc_where[] = ['aid', '=', $aid];
- $dc_where[] = ['confirm_date', '>=', $start_time];
- $dc_where[] = ['confirm_date', '<', $end_time];
- $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
- $dc_where[] = ['state', 'in', $dc_state];
- $daochang_ids = CustomerVisitLog::where($dc_where)->column('customer_id');
- $employee_data['daochang_count'] = count($daochang_ids);
- $employee_data['daochang_lv'] = $sub_count > 0 ? round(count($daochang_ids) * 100 / $sub_count, 2) : 0 ;
- $jd_where[] = ['customer_id', 'in', $sub_customer];
- $jd_where[] = ['customer_employee_id', '=', $employee_id];
- $jd_where[] = ['confirm_date', '>=', $start_time];
- $jd_where[] = ['confirm_date', '<', $end_time];
- $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
- $jd_where[] = ['state', 'in', $jd_state];
- $jiaoding = CustomerVisitLog::where($jd_where)->count();
- $employee_data['jiaoding_count'] = $jiaoding;
- $employee_data['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
- $zd_where[] = ['customer_id', 'in', $sub_customer];
- $zd_where[] = ['customer_employee_id', '=', $employee_id];
- $zd_where[] = ['confirm_date', '>=', $start_time];
- $zd_where[] = ['confirm_date', '<', $end_time];
- $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
- $zd_where[] = ['state', 'in', $zd_state];
- $zhuandan = CustomerVisitLog::where($zd_where)->count();
- $employee_data['zhuandan_count'] = $zhuandan;
- $employee_data['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
- $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name');
- /* $where[] = ['employee_id', '=', $employee_id];
- $where[] = ['aid', '=', $aid];
- $where[] = ['type', '=', 2];
- $where[] = ['state', 'in', [0,1]];
- $where[] = ['subscribe_date', '>=', $start_time];
- $where[] = ['subscribe_date', '<', $end_time];
- $customer_id = CustomersSubscribe::where($where)->column('customer_id'); */
- // 直接到场的客户
- $customer_id = array_values(array_unique(array_merge($daochang_ids, $sub_customer)));
- $type = input('type', 'subscribe', 'trim');
- switch ($type){
- case 'subscribe':
- break;
- case 'daochang':
- $state = CustomerVisitLog::changeState('已到场', 'chaos');
- $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id');
- break;
- case 'qiandan':
- $state = CustomerVisitLog::changeState('已交定', 'chaos');
- $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id');
- break;
- case 'zhuandan':
- $state = CustomerVisitLog::changeState('已签单', 'chaos');
- $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id');
- break;
- default:
- break;
- }
- $page = input('page', 1, 'intval');
- $c_where[] = ['id', 'in', $customer_id];
- $c_where[] = ['employee_id', '=', $employee_id];
- $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where($c_where)->page($page, 10)->select();
- $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray();
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach ($customers as $k => $v) {
- $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- }
- $data['employee'] = $employee_data;
- $data['customer'] = $customers;
- return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
- }
- /**
- * 客户分配数
- */
- public function customerDistribution()
- {
- $token = $this->request->token;
- // $token['org_id'] = 43;
- // $token['employee_id'] = 125;
- // $token['root_org'] = 23;
- $date = date('Y-m-d');
- $param = Request::only(['start_time' => $date, 'end_time' => $date,'employee_id'=>0,'org_id'=>0]);
- $param['start_time'] = date('Y-m-d',strtotime($param['start_time']));
- $param['end_time'] = date('Y-m-d',strtotime($param['end_time']));
- $where[] = ['sign_time', 'between', [$param['start_time'].' 00:00:00', $param['end_time'] . ' 23:59:59']];
- $where[] = ['employee_id','>',0];
- if ($param['employee_id']) {
- $where[] = ['employee_id','=',$param['employee_id']];
- }else{
- $org_id = $param['org_id'] ?: $token['org_id'];
- $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path');
- if(empty($path)) return json(['code' => 0, 'data' => [],'title'=>[]]);
- $org_ids = Org::where([['path','like',$path.'%']])->column('id');
- $where[] = ['org_id','in',$org_ids];
- }
- $wuxiao = Customer::changeState('无效', 'chaos');
- $not_sure = Customer::changeState('待确认', 'chaos');
- $where[] = ['state', 'not in', array_merge($wuxiao, $not_sure)];
- $where[] = ['died', '<>', 2];
- //房屋状态
- $pid = CustomerPortraitField::where([['keyname', '=', 'house_type'], ['root_id', '=', $token['root_org']]])->value('id');
- $house_status = CustomerPortraitFieldSelect::where([['pid', '=', $pid]])->order('id asc')->column("name,id,concat('house_type','#',id) as keyname,0 as count");
- $count = ['id' => 0, 'count' => 0, 'keyname' => 'count', 'name' => '有效客户数', 'key' => '客户'];
- $existing_homes = ['id' => 0, 'count' => 0, 'keyname' => 'existing_homes', 'name' => '现房数量占比', 'key' => '现房'];
- $forward_housing = ['id' => 0, 'count' => 0, 'keyname' => 'forward_housing', 'name' => '期房数量占比', 'key' => '期房'];
- $ershoufang = ['id' => 0, 'count' => 0, 'keyname' => 'ershoufang', 'name' => '二手房数量占比', 'key' => '二手房'];
- $customer = Customer::where($where)->column('house_type');
- $count['count'] = count($customer);
- foreach ($customer as $k => $v) {
- switch ($v) {
- case '现房':
- $existing_homes['count'] ++;
- break;
- case '期房':
- $forward_housing['count'] ++;
- break;
- case '二手房':
- $ershoufang['count'] ++;
- break;
- default:
- break;
- }
- }
- foreach ($house_status as $k2 => $v2) {
- $house_status[$k2]['name'] = $v2['name'] . '数量占比';
- }
- array_unshift($house_status, $count, $existing_homes, $forward_housing,$ershoufang);
- //比例
- if ($count['count']) {
- foreach ($house_status as $k3 => $v3) {
- $house_status[$k3]['grawth'] = round($v3['count'] / $count['count'] * 100, 2) . '%';
- }
- } else {
- foreach ($house_status as $k3 => $v3) {
- $house_status[$k3]['grawth'] = '0%';
- }
- }
- $org_id = $param['org_id'] ?: $token['org_id'];
- $house_status[0]['org_name'] = Org::where('id',$org_id)->value('name');
- return json(['code' => 0,'title'=>$house_status[0], 'data' => array_slice($house_status,1,3)]);
- }
- /**
- * 客户分配列表
- */
- public function customerDistributionList()
- {
- $token = $this->request->token;
- // $token['org_id'] = 23;
- // $token['employee_id'] = 0;
- // $token['root_org'] = 23;
- $date = date('Y-m-d');
- $param = Request::only(['key' => '', 'page' => 1, 'limit' => 10, 'start_time' => $date, 'end_time' => $date,'employee_id'=>0,'org_id'=>0]);
- $param['start_time'] = date('Y-m-d',strtotime($param['start_time']));
- $param['end_time'] = date('Y-m-d',strtotime($param['end_time']));
- // $arr = ['现房', '期房'];
- $where[] = ['sign_time', 'between', [$param['start_time'].' 00:00:00', $param['end_time'] . ' 23:59:59']];
- $where[] = ['employee_id','>',0];
- if ($param['employee_id']) {
- $where[] = ['employee_id','=',$param['employee_id']];
- }else{
- $org_id = $param['org_id'] ?: $token['org_id'];
- $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path');
- if(empty($path)) return json(['code' => 0, 'data' => [],'count'=>0]);
- $org_ids = Org::where([['path','like',$path.'%']])->column('id');
- $where[] = ['org_id','in',$org_ids];
- }
- if(empty($param['key'])) return json(['code' => 0, 'data' =>[], 'count' => []]);
- $where[] = ['house_type', '=', $param['key']];
- $wuxiao = Customer::changeState('无效', 'chaos');
- $not_sure = Customer::changeState('待确认', 'chaos');
- $where[] = ['state', 'not in', array_merge($wuxiao, $not_sure)];
- $where[] = ['died', '<>', 2];
- $list = Customer::where($where)->page($param['page'], $param['limit'])->field('id,sex,name,phone,level,community_name,square,last_contact_date,phone1,phone2')->select()->toArray();
- if($list) $list = $this->getCustomer($list);
- $count = Customer::where($where)->count();
- return json(['code' => 0, 'data' => $list, 'count' => $count]);
- }
- /**
- * 客户跟进数据
- */
- private function getCustomer($arr){
- $customerIdList = array_column($arr,'id');
- //统计
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customerIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach ($arr as $key => $item) {
- $arr[$key]['stateNum'] = isset($customersState[$item['id']]) ? $customersState[$item['id']] : ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- }
- return $arr;
- }
- /**
- * 加微统计
- */
- public function weChatStatistics()
- {
- $token = $this->request->token;
- $date = date('Y-m-d');
- $param = Request::only(['start_time'=> $date, 'end_time'=> $date, 'employee_id'=> 0, 'org_id'=> 0, 'page'=> 0, 'limit'=> 10]);
- $param['start_time'] = date('Y-m-d 00:00:00', strtotime($param['start_time']));
- $param['end_time'] = date('Y-m-d 23:59:59', strtotime($param['end_time']));
- $where[] = ['employee_id', '>', 0];
- if ($param['employee_id']) {
- $where[] = ['employee_id', '=', $param['employee_id']];
- } else {
- $org_id = $param['org_id'] ? : $token['org_id'];
- $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path');
- if(empty($path)) return json(['code' => 0, 'data' => []]);
- $org_ids = Org::where([['path', 'like', $path.'%']])->column('id');
- $where[] = ['org_id', 'in', $org_ids];
- }
- // 所有客户
- $c_where = $where;
- $wuxiao = Customer::changeState('无效', 'chaos');
- $c_where[] = ['sign_time', 'between', [$param['start_time'], $param['end_time']]];
- $c_where[] = ['state', 'NOT NULL', null];
- $c_where[] = ['state', 'not in', $wuxiao];
- $c_where[] = ['died', '<>', 2];
-
- $customer = Customer::where($c_where)->where(function($query){
- $not_sure = Customer::changeState('待确认', 'chaos');
- $or1[] = ['crm_res_id', 'null', null];
- $or2[] = ['crm_res_id', 'not null', null];
- $or2[] = ['state', 'not in', $not_sure];
- $query->whereOr([$or1, $or2]);
- })->column('id,addtime,employee_id');
- $org_customer = [];
- $emp_customer = [];
- foreach ($customer as $v2) {
- @$org_customer[$v2['org_id']] += 1;
- @$emp_customer[$v2['employee_id']] += 1;
- }
- //日期范围内所有加微客户
- $where[] = ['add_wechat_time', 'between', [$param['start_time'], $param['end_time']]];
- $wechat = Customer::where($where)->column('id,employee_id,org_id');
- $org_customer_wechat = [];
- $emp_customer_wechat = [];
- foreach ($wechat as $v3) {
- @$org_customer_wechat[$v3['org_id']]+=1;
- @$emp_customer_wechat[$v3['employee_id']]+=1;
- }
- $title['img'] = '';
- $title['count'] = count($customer);
- $title['wechat_count'] = count($wechat);
- $title['grawth'] = ($title['count'] == 0 || $title['wechat_count'] == 0) ? '0%' : round($title['wechat_count'] / $title['count']*100, 2).'%';
- if ($param['employee_id']) {
- $user = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl')->bind(['img'=> 'headimgurl']);
- }])->where('id', $param['employee_id'])->field('uid')->find();
- $title['img'] = $user->img;
- }
- if ($param['employee_id']) {
- //客户列表 有分页 无排序
- $list = array_slice($wechat, ($param['page'] - 1) * $param['limit'], $param['limit']);
- $list = Customer::where([['id','in',array_column($list,'id')]])->field('id,sex,name,phone,level,community_name,square,last_contact_date,phone1,phone2')->select()->toArray();
- if($list) $list = $this->getCustomer($list);
- $count = $title['wechat_count'];
- return json(['code' => 0, 'data' => $list, 'count' => $count,'title'=>$title]);
- }else{
- $sort = Request::only(['order'=>'desc','field'=>'count']);
- $order = $sort['order']=='desc' ? SORT_DESC : SORT_ASC;
- $field = $sort['field']=='count' ? 'wechat_count' : 'order';
- //直属业务员,直属子部门列表 无分页 有排序
- //直属业务员 列表
- $emp = Employee::where([['org_id','=',$org_id],['uid','>',0],['state','=','在职']])->column('id,name,uid');
- $users = User::where([['id','in',array_column($emp,'uid')]])->column('headimgurl','id');
- foreach ($emp as $k5 => $v5) {
- $emp[$k5]['count'] = isset($emp_customer[$v5['id']]) ? $emp_customer[$v5['id']] : 0;
- $emp[$k5]['wechat_count'] = isset($emp_customer_wechat[$v5['id']]) ? $emp_customer_wechat[$v5['id']] : 0;
- $emp[$k5]['order'] = ($emp[$k5]['count']==0 || $emp[$k5]['wechat_count']==0) ? 0 : round($emp[$k5]['wechat_count']/$emp[$k5]['count']*100,2);
- $emp[$k5]['img'] = $users[$v5['uid']];
- $emp[$k5]['grawth'] = $emp[$k5]['order'].'%';
- }
- if($emp) array_multisort(array_column($emp, $field), $order, $emp);
-
- //部门列表
- $info = Org::where('id', $org_id)->find();
- //直属子部门
- $orgs = Org::where([['pid', '=', $org_id]])->column('id,name,path');
- //所有子部门
- $s_orgs = Org::where([['path','like',$info->path.'%']])->column('id,pid,path');
- $org_ids = [];
- foreach ($orgs as $k6 => $v6) {
- $orgs[$k6]['count'] = 0;
- $orgs[$k6]['wechat_count'] = 0;
- $orgs[$k6]['emp_count'] = 0;
- $orgs[$k6]['child_org_ids'] = [];
- foreach ($s_orgs as $v7) {
- if(stripos($v7['path'],$v6['path']) === 0){
- if(isset($org_customer[$v7['id']])) $orgs[$k6]['count']+=$org_customer[$v7['id']];
- if(isset($org_customer_wechat[$v7['id']])) $orgs[$k6]['wechat_count']+=$org_customer_wechat[$v7['id']];
- $orgs[$k6]['child_org_ids'][] = $v7['id'];
- $org_ids[] = $v7['id'];
- }
- }
- $orgs[$k6]['order'] = ($orgs[$k6]['count']==0 || $orgs[$k6]['wechat_count']==0) ? 0 : round($orgs[$k6]['wechat_count']/$orgs[$k6]['count']*100,2);
- $orgs[$k6]['grawth'] = $orgs[$k6]['order'].'%';
- }
- if($orgs) array_multisort(array_column($orgs,$field),$order, $orgs);
- //部门下员工数量
- $emps = Employee::where([['org_id','in',$org_ids],['state','=','在职'],['uid','>',0]])->group('org_id')->column('count(id)','org_id');
- foreach ($orgs as $k8 => $v8) {
- foreach ($v8['child_org_ids'] as $v9) {
- if(isset($emps[$v9])) $orgs[$k8]['emp_count']+=$emps[$v9];
- }
- }
- $org_id = $param['org_id'] ?: $token['org_id'];
- $title['org_name'] = Org::where('id',$org_id)->value('name');
- return json(['code'=> 0, 'data'=> ['emp'=> $emp, 'org'=> $orgs, 'title'=> $title]]);
- }
- }
- /**
- * 沟通跟进次数
- */
- public function followUpTimes()
- {
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 数据总览
- // 部门信息
- $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
- //员工数量
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $data['employee_count'] = count($sub_employee_ids);
- // 电话沟通次数
- $call_where[] = ['employee_id', 'in', $sub_employee_ids];
- $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $call_where[] = ['status', '=', 1];
- //电话沟通
- $call_count = 0;
- $call_time = 0;
- $call_avg = 0;
- $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
- if ($call_log) {
- $call_count = count($call_log);
- $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
- $call_employee = count(array_unique(array_column($call_log,'employee_id')));
- $call_avg = round(array_sum(array_column($call_log,'billsec')) / 60 / $call_employee, 2);
- }
- // 手机打电话次数
- $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $sub_employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
- $call_count = $call_count + $mbCallCount;
- $data['call_count'] = $call_count;
- $data['call_time'] = $call_time;
- $data['call_avg'] = $call_avg;
- // 内容调用次数
- $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
- $share_where[] = ['employee_id', 'in', $sub_employee_ids];
- // 转发次数
- $share_all = ShareLog::where($share_where)->select();
- $share_count = $share_all->count();
- $wechat_where[] = ['employee_id', 'in', $sub_employee_ids];
- $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
- //查询每日发圈转发次数
- $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
- $data['share_count'] = $k_share_count;
- // 浏览时长
- $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
- $foot_where[] = ['employee_id', 'in', $sub_employee_ids];
- $foot_where[] = ['pipe_type', 'in', $clue_type];
- $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $foot_count = Footprints::where($foot_where)->count(); // 次数
- $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
- $data['visit_long'] = round($visit_long / 60, 2);
- $data['visit_avg'] = $foot_count > 0 ? round($visit_long / $foot_count /60, 2) : round($visit_long / 60, 2);
- //微信沟通
- $visit_where[] = ['employee_id', 'in', $sub_employee_ids];
- $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
- $visit_where[] = ['type', '=', 3];
- $wechat_visit_count = CustomerVisitLog::where($visit_where)->count();
- $data['wechat_visit_count'] = $wechat_visit_count;
- $all_customer_where[] = ['employee_id', 'in', $sub_employee_ids];
- $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $customer_count = Customer::where($all_customer_where)->count();
- $data['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
- $data['all_count'] = $data['call_count'] + $k_share_count + $data['wechat_visit_count'];
- return json(['code'=> self::success, 'data'=> $data]);
- }
- /**
- * 沟通跟进次数 (部门)
- */
- public function followUpTimesOrg()
- {
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- foreach ($sub_org as $k => $v) {
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0) {
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- // 电话沟通次数
- unset($call_where);
- $call_where[] = ['employee_id', 'in', $org_employee_ids];
- $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $call_where[] = ['status', '=', 1];
- //电话沟通
- $call_count = 0;
- $call_time = 0;
- $call_avg = 0;
- $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
- if ($call_log) {
- $call_count = count($call_log);
- $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
- $call_employee = count(array_unique(array_column($call_log,'employee_id')));
- $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2);
- }
- // 手机打电话次数
- $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $org_employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
- $call_count = $call_count + $mbCallCount;
- $sub_org[$k]['call_count'] = $call_count;
- $sub_org[$k]['call_time'] = $call_time;
- $sub_org[$k]['call_avg'] = $call_avg;
- // 内容调用次数
- unset($share_where);
- $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
- $share_where[] = ['employee_id', 'in', $org_employee_ids];
- // 转发次数
- $share_all = ShareLog::where($share_where)->select();
- $share_count = $share_all->count();
- unset($wechat_where);
- $wechat_where[] = ['employee_id', 'in', $org_employee_ids];
- $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
- //查询每日发圈转发次数
- $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
- $sub_org[$k]['share_count'] = $k_share_count;
- // 浏览时长
- $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
- unset($foot_where);
- $foot_where[] = ['employee_id', 'in', $org_employee_ids];
- $foot_where[] = ['pipe_type', 'in', $clue_type];
- $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $foot_count = Footprints::where($foot_where)->count(); // 次数
- $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
- $sub_org[$k]['visit_long'] = round($visit_long / 60, 2);
- $sub_org[$k]['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2);
- //微信沟通
- unset($visit_where);
- $visit_where[] = ['employee_id', 'in', $org_employee_ids];
- $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
- $visit_where[] = ['type', '=', 3];
- $wechat_visit_count = CustomerVisitLog::where($visit_where)->count();
- $sub_org[$k]['wechat_visit_count'] = $wechat_visit_count;
- unset($all_customer_where);
- $all_customer_where[] = ['employee_id', 'in', $org_employee_ids];
- $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $customer_count = Customer::where($all_customer_where)->count();
- $sub_org[$k]['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
- $sub_org[$k]['all_count'] = $call_count + $k_share_count + $wechat_visit_count;
- }
- $order = input('order', '', 'trim');
- if ($order) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'call';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'call':
- $field = 'call_count';
- break;
- case 'share':
- $field = 'share_count';
- break;
- case 'wechat':
- $field = 'wechat_visit_count';
- break;
- default:
- $field = 'notFound';
- break;
- }
- if ($sort_order == 'asc'){
- $sort = SORT_ASC;
- } else {
- $sort = SORT_DESC;
- }
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> self::success, 'data'=> $sub_org]);
- }
- /**
- * 沟通跟进次数(员工)
- */
- public function followUpTimesEmployee()
- {
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', $token['org_id'], 'intval');
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
- foreach ($employee_list as $k => $v) {
- // 电话沟通次数
- unset($call_where);
- $call_where[] = ['employee_id', '=', $v['id']];
- $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $call_where[] = ['status', '=', 1];
- //电话沟通
- $call_count = 0;
- $call_time = 0;
- $call_avg = 0;
- $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
- if ($call_log) {
- $call_count = count($call_log);
- $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
- $call_employee = count(array_unique(array_column($call_log,'employee_id')));
- $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2);
- }
- // 手机打电话次数
- $mbCallCount = OutCallMbLog::where([['employee_id', '=', $v['id']], ['addtime', 'between', [$start_time, $end_time]]])->count();
- $call_count = $call_count + $mbCallCount;
- $employee_list[$k]['call_count'] = $call_count;
- $employee_list[$k]['call_time'] = $call_time;
- $employee_list[$k]['call_avg'] = $call_avg;
- // 内容调用次数
- unset($share_where);
- $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
- $share_where[] = ['employee_id', '=', $v['id']];
- // 转发次数
- $share_all = ShareLog::where($share_where)->select();
- $share_count = $share_all->count();
- unset($wechat_where);
- $wechat_where[] = ['employee_id', '=', $v['id']];
- $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
- //查询每日发圈转发次数
- $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
- $employee_list[$k]['share_count'] = $k_share_count;
- // 浏览时长
- $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
- unset($foot_where);
- $foot_where[] = ['employee_id', '=', $v['id']];
- $foot_where[] = ['pipe_type', 'in', $clue_type];
- $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $foot_count = Footprints::where($foot_where)->count(); // 次数
- $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
- $employee_list[$k]['visit_long'] = round($visit_long / 60, 2);
- $employee_list[$k]['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2);
- //微信沟通
- unset($visit_where);
- $visit_where[] = ['employee_id', '=', $v['id']];
- $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
- $visit_where[] = ['type', '=', 3];
- $wechat_visit_count = CustomerVisitLog::where($visit_where)->count();
- $employee_list[$k]['wechat_visit_count'] = $wechat_visit_count;
- unset($all_customer_where);
- $all_customer_where[] = ['employee_id', '=', $v['id']];
- $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $customer_count = Customer::where($all_customer_where)->count();
- $employee_list[$k]['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
- $employee_list[$k]['all_count'] = $call_count + $k_share_count + $wechat_visit_count;
- }
- $order = input('order', '', 'trim');
- if ($order) {
- $order_arr = explode(' ', $order);
- $sort_field = $order_arr[0] ?? 'call';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'call':
- $field = 'call_count';
- break;
- case 'share':
- $field = 'share_count';
- break;
- case 'wechat':
- $field = 'wechat_visit_count';
- break;
- default:
- $field = 'notFound';
- break;
- }
- if ($sort_order == 'asc'){
- $sort = SORT_ASC;
- } else {
- $sort = SORT_DESC;
- }
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $page = input('page', 1, 'intval');
- $limit = input('limit', 10, 'intval');
- $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit);
- return json(['code'=> self::success, 'data'=> $employee_list]);
- }
- /**
- * 沟通跟进次数 (客户)
- */
- public function followUpTimesCustomer(){
- $token = $this->request->token;
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $employee_id = input('employee_id', '', 'intval');
- $sub_org = orgSubIds($token['org_id']);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- if (!in_array($employee_id, $sub_employee_ids)){
- return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
- }
- // 电话沟通次数
- $call_where[] = ['employee_id', '=', $employee_id];
- $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $call_where[] = ['status', '=', 1];
- //电话沟通
- $call_count = 0;
- $call_time = 0;
- $call_avg = 0;
- $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
- if ($call_log) {
- $call_count = count($call_log);
- $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
- $call_employee = count(array_unique(array_column($call_log,'employee_id')));
- $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2);
- }
- // 手机打电话次数
- $mbCallCount = OutCallMbLog::where([['employee_id', '=', $employee_id], ['addtime', 'between', [$start_time, $end_time]]])->count();
- $call_count = $call_count + $mbCallCount;
- $employee_data['call_count'] = $call_count;
- $employee_data['call_time'] = $call_time;
- $employee_data['call_avg'] = $call_avg;
- $customer_call = array_column($call_log, 'customer_id'); // 打电话的客户id
- // 内容调用次数
- $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
- $share_where[] = ['employee_id', '=', $employee_id];
- // 转发次数
- $share_all = ShareLog::where($share_where)->select();
- $share_count = $share_all->count();
- $wechat_where[] = ['employee_id', '=', $employee_id];
- $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
- //查询每日发圈转发次数
- $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
- $employee_data['share_count'] = $k_share_count;
- $clue_ids = CustomerClue::where([['employee_id', '=', $employee_id], ['addtime', 'between', [$start_time, $end_time]]])->column('id');
- $customer_share = Customer::where('clue_id', 'in', $clue_ids)->column('id');
- // 浏览时长
- $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
- unset($foot_where);
- $foot_where[] = ['employee_id', '=', $employee_id];
- $foot_where[] = ['pipe_type', 'in', $clue_type];
- $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
- $foot_count = Footprints::where($foot_where)->count(); // 次数
- $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
- $employee_data['visit_long'] = round($visit_long / 60, 2);
- $employee_data['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2);
- //微信沟通
- $visit_where[] = ['employee_id', '=', $employee_id];
- $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
- $visit_where[] = ['type', '=', 3];
- $wechat_visit = CustomerVisitLog::where($visit_where)->select()->toArray();
- $wechat_visit_count = count($wechat_visit);
- $employee_data['wechat_visit_count'] = $wechat_visit_count;
- $customer_wechat = array_column($wechat_visit, 'customer_id');
- $all_customer_where[] = ['employee_id', '=', $employee_id];
- $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
- $customer_count = Customer::where($all_customer_where)->count();
- $employee_data['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
- $employee_data['all_count'] = $call_count + $k_share_count + $wechat_visit_count;
- $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name');
- $data['employee'] = $employee_data;
- $type = input('type', '', 'trim');
- switch ($type){
- case 'call':
- $customer_id = $customer_call;
- break;
- case 'share':
- $customer_id = $customer_share;
- break;
- case 'wechat':
- $customer_id = $customer_wechat;
- break;
- default:
- $customer_id = array_values(array_filter(array_unique(array_merge($customer_call, $customer_share, $customer_wechat))));
- break;
- }
- $page = input('page', 1, 'intval');
- $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select();
- $customers = $customers->visible(['id', 'employee_id', 'designer_id', 'name', 'sex', 'level', 'community_name', 'state', 'addtime', 'revisit_time', 'designer.name', 'designerOrg.name', 'fresh', 'square', 'last_contact_date', 'updatetime', 'employee.name', 'agent.agent_name'])->toArray();
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach ($customers as $k => $v) {
- $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- }
- $data['customer'] = $customers;
- return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
- }
- /**
- * 卖卡首页统计
- */
- public function maika_index()
- {
- $token = $this->request->token;
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', '', 'intval');
- $org_id = !empty($org_id) ? $org_id : $token['org_id'];
-
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- //员工数量
- $org_name = Org::where('id', $org_id)->value('name');
- $sub_org = orgSubIds($org_id);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
- $db_where[] = ['Customer.org_id', 'in', $sub_org];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
- $count = Db::view('CustomerVisitLog', 'id')
- ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->group('Customer.id')
- ->count();
- $data = ['org_name'=> $org_name, 'employee_count'=> count($sub_employee_ids), 'count'=> $count];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- /**
- * 卖卡部门数据
- */
- public function maika_org(){
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order']);
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $root_sub = orgSubIds($token['root_org']);
- // 所选部门
- $org_id = input('org_id', '', 'intval');
- if (!$org_id) {
- $org_id = $token['org_id'];
- }
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门
- $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
- // 所有客户量 用于后面计算百分比
- $sub_org_ids = orgSubIds($org_id);
-
- $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
- $db_where[] = ['Customer.org_id', 'in', $sub_org_ids];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
- $log_list = Db::view('CustomerVisitLog', 'id')
- ->view('Customer', 'id cid,org_id', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- // 每个员工的跟进记录
- $org_log = [];
- foreach ($log_list as $k => $v) {
- if (empty($org_log[$v['org_id']])) {
- $org_log[$v['org_id']] = [];
- } elseif (in_array($v['cid'], $org_log[$v['org_id']])) {
- continue;
- }
- $org_log[$v['org_id']][] = $v['cid'];
- }
- foreach ($sub_org as $k => $v){
- // 是否有子部门
- $have_sub_org = Org::where('pid', '=', $v['id'])->count();
- if ($have_sub_org > 0){
- $sub_org[$k]['have_sub_org'] = true;
- } else {
- $sub_org[$k]['have_sub_org'] = false;
- }
- $v_sub_org = orgSubIds($v['id']);
- $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- $sub_org[$k]['employee_count'] = count($org_employee_ids);
- $sub_org[$k]['count'] = !empty($org_log[$v['id']]) ? count($org_log[$v['id']]) : 0;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'count';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'count':
- $field = 'count';
- break;
- default:
- $field = 'count';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($sub_org as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $sub_org);
- }
- }
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
- }
- /**
- * 卖卡员工数据
- */
- public function maika_employee(){
- $token = $this->request->token;
- $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- // 所选部门
- $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
- $root_sub = orgSubIds($token['root_org']);
- if (!in_array($org_id, $root_sub)){
- return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
- }
- // 所选部门员工统计数据
- $e_where[] = ['org_id', '=', $org_id];
- $e_where[] = ['state', '=', '在职'];
- $e_where[] = ['uid', '<>', 0];
- $employee_list = Employee::with(['user'=> function($query){
- $query->field('id,headimgurl');
- }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
- // 所有客户量
- $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
- // 每个员工的客户
- $db_where[] = ['Customer.org_id', '=', $org_id];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
- $log_list = Db::view('CustomerVisitLog', 'id,customer_employee_id')
- ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->order('confirm_date asc')
- ->select()
- ->toArray();
- // 每个员工的跟进记录
- $employee_log = [];
- foreach ($log_list as $k => $v) {
- if (empty($employee_log[$v['customer_employee_id']])) {
- $employee_log[$v['customer_employee_id']] = [];
- } elseif (in_array($v['cid'], $employee_log[$v['customer_employee_id']])) {
- continue;
- }
- $employee_log[$v['customer_employee_id']][] = $v['cid'];
- }
- foreach($employee_list as $k => $v){
- $employee_list[$k]['employee_count'] = count($employee_list);
- $employee_list[$k]['count'] = !empty($employee_log[$v['id']]) ? count($employee_log[$v['id']]) : 0;
- }
- if (!empty($param['order'])) {
- $order_arr = explode(' ', $param['order']);
- $sort_field = $order_arr[0] ?? 'count';
- $sort_order = $order_arr[1] ?? 'desc';
- switch ($sort_field){
- case 'count':
- $field = 'count';
- break;
- default:
- $field = 'count';
- break;
- }
- $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
- $arr = [];
- foreach ($employee_list as $v){
- if (!isset($v[$field])){
- break;
- }
- $arr[] = $v[$field];
- }
- if (!empty($arr)){
- array_multisort($arr, $sort, $employee_list);
- }
- }
- $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
- }
- /**
- * 卖卡员工客户数据
- */
- public function maika_customer()
- {
- $token = $this->request->token;
- $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]);
- $start_time_get = input('start_time', time(), 'strtotime');
- $end_time_get = input('end_time', time(), 'strtotime');
- $start_time = date('Y-m-d 00:00:00', $start_time_get);
- $end_time = date('Y-m-d 23:59:59', $end_time_get);
- $employee_id = input('employee_id', '', 'intval');
- $sub_org = orgSubIds($token['org_id']);
- $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
- if (!in_array($employee_id, $sub_employee_ids)){
- return json(['code'=> self::success, 'data'=> ['empdata'=> [], 'list'=> []], 'msg'=> 'success']);
- }
- $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
- $wuxiao = Customer::changeState('无效', 'chaos');
- $db_where[] = ['Customer.employee_id', '=', $employee_id];
- $db_where[] = ['Customer.state', 'NOT NULL', null];
- $db_where[] = ['Customer.state', 'not in', $wuxiao];
- $db_where[] = ['Customer.died', '<>', 2];
- $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
- $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
- $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
- $customer_ids = Db::view('CustomerVisitLog', 'id')
- ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id')
- ->where($db_where)
- ->group('Customer.id')
- ->column('Customer.id');
- $cuslist = Customer::with(['employee'=>function($query){
- $query->field('id,name');
- }])->where('id', 'in', $customer_ids)->page($param['page'],$param['limit'])->column('id,name,phone,state,addtime,community_name,employee_id,last_contact_date,level,sex,square,updatetime');
- $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
- if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
-
- // 状态统计获取
- $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customer_ids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
- $customersState = [];
- foreach ($customersStateList as $s) {
- if (!isset($customersState[$s['customer_id']]))
- $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- $customersState[$s['customer_id']]['count'] += $s['num'];
- // 到店,量房,活动,定金,签单
- if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
- elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
- elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
- elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
- elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
- }
- foreach($cuslist as $key=>$val){
- $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
- if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
- }
- $empdata = ['empname'=> $employeedata['name'], 'employee_count'=> 1, 'count'=> count($customer_ids)];
- $data = ['empdata'=> $empdata, 'list'=> $cuslist];
- return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
- }
- }
|