CrmStatistics.php 215 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486
  1. <?php
  2. /**
  3. * 客户统计
  4. */
  5. namespace app\api\controller;
  6. use app\model\Customer;
  7. use app\model\CustomerClue;
  8. use app\model\CustomersSubscribe;
  9. use app\model\CustomerVisitLog;
  10. use app\model\DailyWechatArticleCopy;
  11. use app\model\Employee;
  12. use app\model\Footprints;
  13. use app\model\Org;
  14. use app\model\OutCallLog;
  15. use app\model\ShareLog;
  16. use think\facade\Db;
  17. use think\facade\Log;
  18. use think\facade\Request;
  19. use app\model\CustomerPortraitFieldSelect;
  20. use app\model\CustomerPortraitField;
  21. use app\model\Activity;
  22. use app\model\CustomerPackage;
  23. use app\model\OutCallMbLog;
  24. use app\model\Setting;
  25. use app\model\User;
  26. class CrmStatistics extends Base
  27. {
  28. /**
  29. * 获取展示设置
  30. */
  31. public function getSetting() {
  32. $token = request()->token;
  33. $root_id = $token['root_org'];
  34. $where = [
  35. ['name', '=', 'CustomerStatisticsSettingForMini'],
  36. ['root_id', '=', $root_id]
  37. ];
  38. $data = Setting::where($where)->findOrEmpty();
  39. if ($data->isEmpty()) {
  40. $content = ['valid', 'call', 'jiav', 'contact', 'subscribe', 'measure_room', 'arrival', 'deposit', 'signed', 'maika'];
  41. } else {
  42. $content = explode(',', $data['content']);
  43. }
  44. return json(['code' => self::success, 'msg' => 'success', 'data'=> $content]);
  45. }
  46. /**
  47. * 获取部门
  48. */
  49. public function get_org(){
  50. $token = request()->token;
  51. $path = Org::where('id', '=', $token['org_id'])->value('path');
  52. $list = Org::where([['path', 'like', $path . '%'], ['status', '=', 1]])->order('path asc, pid asc')->select()->toArray();
  53. return json(['code'=> self::success, 'data'=> $list]);
  54. }
  55. /**
  56. * 统计首页
  57. */
  58. public function index(){
  59. $token = $this->request->token;
  60. $start_time_get = input('start_time', time(), 'strtotime');
  61. $end_time_get = input('end_time', time(), 'strtotime');
  62. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  63. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  64. $sub_org = orgSubIds($token['org_id']);
  65. // 所选部门员工统计数据
  66. $e_where[] = ['org_id', 'in', $sub_org];
  67. $e_where[] = ['state', '=', '在职'];
  68. $e_where[] = ['uid', '<>', 0];
  69. $employee_ids = Employee::where($e_where)->column('id');
  70. // 客户分配数
  71. $wuxiao = Customer::changeState('无效', 'chaos');
  72. $not_sure = Customer::changeState('待确认', 'chaos');
  73. $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();
  74. $data['customer_count'] = $customer_count;
  75. // 打电话次数
  76. $call_count = OutCallLog::where([['employee_id', 'in', $employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
  77. // 手机打电话次数
  78. $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
  79. $data['call_count'] = $call_count + $mbCallCount;
  80. // 加微统计
  81. $jiav_count = Customer::where([['employee_id', 'in', $employee_ids], ['add_wechat_time', 'between', [$start_time, $end_time]]])->count();
  82. $data['jiav_count'] = $jiav_count;
  83. //沟通跟进次数
  84. // 内容调用次数
  85. $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
  86. $share_where[] = ['employee_id', 'in', $employee_ids];
  87. $shared_count = ShareLog::where($share_where)->count();
  88. // 微信沟通次数
  89. $wx_contact_count = CustomerVisitLog::where([['org_id', 'in', $sub_org], ['addtime', 'between', [$start_time, $end_time]], ['type', '=', 3]])->count();
  90. $contact_count = $call_count + $mbCallCount + $shared_count + $wx_contact_count;
  91. $data['contact_count'] = $contact_count;
  92. // 预约活动数量
  93. $hd_subscribe_count = CustomersSubscribe::where([['employee_id', 'in', $employee_ids], ['subscribe_date', 'between', [$start_time, $end_time]], ['type', '=', 2], ['state', 'in', [0, 1]]])->count();
  94. $data['hd_subscribe_count'] = $hd_subscribe_count;
  95. // 跟踪记录
  96. // $visit_log = CustomerVisitLog::where([['org_id', 'in', $sub_org], ['confirm_date', 'between', [$start_time, $end_time]]])->column('customer_id,state,money');
  97. $db_where[] = ['Customer.org_id', 'in', $sub_org];
  98. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  99. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  100. $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,state,money')
  101. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  102. ->where($db_where)
  103. ->select()
  104. ->toArray();
  105. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  106. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  107. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  108. $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  109. $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
  110. // 见面状态
  111. // $state3 = CustomerVisitLog::changeState('已到场', 'chaos');
  112. // $make_state0 = array_merge($dd_state, $lf_state, $state3);
  113. $lf_count = [];
  114. $dd_count = [];
  115. $jd_count = [];
  116. $qd_count = [];
  117. $mk_count = [];
  118. $jd_money = 0;
  119. foreach ($visit_log as $k => $v) {
  120. if (in_array($v['state'], $lf_state) && !in_array($v['customer_id'], $lf_count)) {
  121. $lf_count[] = $v['customer_id'];
  122. }
  123. if (in_array($v['state'], $dd_state) && !in_array($v['customer_id'], $dd_count)) {
  124. $dd_count[] = $v['customer_id'];
  125. }
  126. if (in_array($v['state'], $jd_state) && !in_array($v['customer_id'], $jd_count)) {
  127. $jd_count[] = $v['customer_id'];
  128. $jd_money += $v['money'];
  129. }
  130. if (in_array($v['state'], $qd_state) && !in_array($v['customer_id'], $qd_count)) {
  131. $qd_count[] = $v['customer_id'];
  132. }
  133. if (in_array($v['state'], $mk_state) && !in_array($v['customer_id'], $mk_count)) {
  134. $mk_count[] = $v['customer_id'];
  135. }
  136. }
  137. $data['lf_count'] = count($lf_count);
  138. $data['dd_count'] = count($dd_count);
  139. $data['jd_count'] = count($jd_count);
  140. $data['jd_money'] = $jd_money;
  141. $data['qd_count'] = count($qd_count);
  142. $data['mk_count'] = count($mk_count);
  143. $qd_money = Customer::where('id', 'in', $qd_count)->sum('signed_money');
  144. $data['qd_money'] = $qd_money;
  145. $data['org_name'] = Org::where('id', request()->token['org_id'])->value('name');
  146. return json(['code'=> self::success, 'data'=> $data]);
  147. }
  148. /**
  149. * 到店统计(全部)
  150. */
  151. public function arrival(){
  152. $token = $this->request->token;
  153. $start_time_get = input('start_time', time(), 'strtotime');
  154. $end_time_get = input('end_time', time(), 'strtotime');
  155. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  156. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  157. $root_sub = orgSubIds($token['root_org']);
  158. // 所选部门
  159. $org_id = input('org_id', $token['org_id'], 'intval');
  160. if (!in_array($org_id, $root_sub)){
  161. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  162. }
  163. // 部门信息
  164. $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
  165. //员工数量
  166. $sub_org = orgSubIds($org_id);
  167. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  168. $data['employee_count'] = count($sub_employee_ids);
  169. // 所有客户量
  170. $wuxiao = Customer::changeState('无效', 'chaos');
  171. $c_where[] = ['org_id', 'in', $sub_org];
  172. $c_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  173. $c_where[] = ['state', 'NOT NULL', null];
  174. $c_where[] = ['state', 'not in', $wuxiao];
  175. $c_where[] = ['died', '<>', 2];
  176. $customer_list = Customer::where($c_where)->where(function($query){
  177. $not_sure = Customer::changeState('待确认', 'chaos');
  178. $or1[] = ['crm_res_id', 'null', null];
  179. $or2[] = ['crm_res_id', 'not null', null];
  180. $or2[] = ['state', 'not in', $not_sure];
  181. $query->whereOr([$or1, $or2]);
  182. })->column('id,addtime');
  183. // 见面状态
  184. // $state1 = CustomerVisitLog::changeState('已量房', 'chaos');
  185. // $state2 = CustomerVisitLog::changeState('已到店', 'chaos');
  186. // $state3 = CustomerVisitLog::changeState('已到场', 'chaos');
  187. // $make_state0 = array_merge($state1, $state2, $state3);
  188. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  189. $db_where[] = ['Customer.org_id', 'in', $sub_org];
  190. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  191. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  192. $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
  193. $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date,employee_id')
  194. ->view('Customer', 'addtime customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
  195. ->where($db_where)
  196. ->select()
  197. ->toArray();
  198. $all_customer_count = count($customer_list);
  199. $list = [];
  200. foreach ($visit_log as $k => $v){
  201. $list[$v['customer_id']][] = $v;
  202. }
  203. $data['all_count'] = count($visit_log);
  204. // 到店次数
  205. $one = [];
  206. $two = [];
  207. $three = [];
  208. $one_arrival = 0; // 一次到店量
  209. $two_arrival = 0;
  210. $three_arrival = 0;
  211. $one_days = 0; // 一次到店天数间隔
  212. $two_days = 0;
  213. $three_days = 0;
  214. $lf_count = 0; // 量房转到店
  215. $lf_days = 0;
  216. $wc_count = 0; // 外场转到店
  217. $wc_days = 0;
  218. $nc_count = 0; // 内场活动到店
  219. $one_customer_ids = [];
  220. $two_customer_ids = [];
  221. $three_customer_ids = [];
  222. // 先查出来后面要用到的已量房、已到场记录
  223. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  224. $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  225. $lw_where[] = ['Customer.org_id', 'in', $sub_org];
  226. $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  227. $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
  228. $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
  229. $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
  230. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  231. ->where($lw_where)
  232. ->order('confirm_date asc')
  233. ->select()
  234. ->toArray();
  235. $lf_list = [];
  236. $wc_list = [];
  237. foreach ($lw_log_list as $k => $v) {
  238. if (in_array($v['state'], $lf_state, true)) {
  239. if (empty($lf_list[$v['customer_id']])) {
  240. $lf_list[$v['customer_id']] = $v['confirm_date'];
  241. }
  242. }
  243. if (in_array($v['state'], $wc_state, true)) {
  244. if (empty($wc_list[$v['customer_id']])) {
  245. $wc_list[$v['customer_id']] = $v['confirm_date'];
  246. }
  247. }
  248. }
  249. foreach ($list as $k => $v){
  250. if (count($v) == 1) {
  251. $one_arrival ++;
  252. $one_customer_ids[] = $k;
  253. $long = (strtotime($v[0]['confirm_date']) - strtotime($v[0]['customer_addtime'])) > 0 ? (strtotime($v[0]['confirm_date']) - strtotime($v[0]['customer_addtime'])) : 0;
  254. $days = ceil($long / (24*3600));
  255. $one_days += $days;
  256. }
  257. if (count($v) == 2) {
  258. $two_arrival ++;
  259. $two_customer_ids[] = $k;
  260. $days = ceil((strtotime($v[1]['confirm_date']) - strtotime($v[0]['confirm_date'])) / (24*3600));
  261. $two_days += $days;
  262. }
  263. if (count($v) > 2) {
  264. $three_arrival ++;
  265. $three_customer_ids[] = $k;
  266. $days = ceil((strtotime($v[2]['confirm_date']) - strtotime($v[1]['confirm_date'])) / (24*3600));
  267. $three_days += $days;
  268. }
  269. if (isset($lf_list[$k])) {
  270. $lf_count ++;
  271. $days = ceil((strtotime($v[0]['confirm_date']) - strtotime($lf_list[$k])) / (24*3600));
  272. $lf_days += $days;
  273. }
  274. if (isset($wc_list[$k])) {
  275. $wc_count ++;
  276. $days = ceil((strtotime($v[0]['confirm_date']) - strtotime($wc_list[$k])) / (24*3600));
  277. $wc_days += $days;
  278. }
  279. // 内场活动到店
  280. if (!empty($v[0]['aid'])){
  281. $nc_count ++;
  282. }
  283. }
  284. // 签单
  285. $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  286. // $qd_where[] = ['customer_id', 'in', $all_customer];
  287. $qd_where[] = ['state', 'in', $qd_state];
  288. $qd_where[] = ['org_id', 'in', $sub_org];
  289. $qd_customer_ids = CustomerVisitLog::where($qd_where)->column('customer_id');
  290. $one_qd = count(array_unique(array_intersect($qd_customer_ids, $one_customer_ids)));
  291. $two_qd = count(array_unique(array_intersect($qd_customer_ids, $two_customer_ids)));
  292. $three_qd = count(array_unique(array_intersect($qd_customer_ids, $three_customer_ids)));
  293. $one['count'] = $one_arrival;
  294. $one['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
  295. $one['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
  296. $one['lf_count'] = $lf_count;
  297. $one['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
  298. $one['wc_count'] = $wc_count;
  299. $one['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
  300. $one['nc_count'] = $nc_count;
  301. $one['qd_count'] = $one_qd;
  302. $two['count'] = $two_arrival;
  303. $two['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
  304. $two['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
  305. $two['qd_count'] = $two_qd;
  306. $three['count'] = $three_arrival;
  307. $three['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
  308. $three['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
  309. $three['qd_count'] = $three_qd;
  310. // 邀约计算
  311. $all_customer = array_values(array_unique(array_merge($one_customer_ids, $two_customer_ids, $three_customer_ids)));
  312. $subscribe_where[] = ['state', 'in', [0, 1]];
  313. $subscribe_where[] = ['type', '=', 1];
  314. $subscribe_where[] = ['customer_id', 'in', $all_customer];
  315. $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
  316. $xs_count_one = 0; // 销售一次
  317. $sj_count_one = 0; // 设计一次
  318. $xs_count_two = 0; // 销售二次
  319. $sj_count_two = 0; // 设计二次
  320. $xs_count_three = 0; // 销售三次
  321. $sj_count_three = 0; // 设计三次
  322. foreach ($subscribe as $k => $v){
  323. if (empty($v['designer_id']) && in_array($v['customer_id'], $one_customer_ids)){
  324. $xs_count_one ++;
  325. } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $one_customer_ids)){
  326. $sj_count_one ++;
  327. }
  328. if (empty($v['designer_id']) && in_array($v['customer_id'], $two_customer_ids)){
  329. $xs_count_two ++;
  330. } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $two_customer_ids)){
  331. $sj_count_two ++;
  332. }
  333. if (empty($v['designer_id']) && in_array($v['customer_id'], $three_customer_ids)){
  334. $xs_count_three ++;
  335. } elseif (!empty($v['designer_id']) && in_array($v['customer_id'], $three_customer_ids)){
  336. $sj_count_three ++;
  337. }
  338. }
  339. $one['xs_yaoyue'] = $xs_count_one;
  340. $one['xs_yaoyue_lv'] = $one_arrival > 0 ? round($xs_count_one * 100 / $one_arrival, 2) : 0;
  341. $one['sj_yaoyue'] = $sj_count_one;
  342. $one['sj_yaoyue_lv'] = $one_arrival > 0 ? round($sj_count_one * 100 / $one_arrival, 2) : 0;
  343. $two['xs_yaoyue'] = $xs_count_two;
  344. $two['xs_yaoyue_lv'] = $two_arrival > 0 ? round($xs_count_two * 100 / $two_arrival, 2) : 0;
  345. $two['sj_yaoyue'] = $sj_count_two;
  346. $two['sj_yaoyue_lv'] = $two_arrival > 0 ? round($sj_count_two * 100 / $two_arrival, 2) : 0;
  347. $three['xs_yaoyue'] = $xs_count_three;
  348. $three['xs_yaoyue_lv'] = $three_arrival > 0 ? round($xs_count_three * 100 / $three_arrival, 2) : 0;
  349. $three['sj_yaoyue'] = $sj_count_three;
  350. $three['sj_yaoyue_lv'] = $three_arrival > 0 ? round($sj_count_three * 100 / $three_arrival, 2) : 0;
  351. $data['one'] = $one;
  352. $data['two'] = $two;
  353. $data['three'] = $three;
  354. return json(['code'=> self::success, 'data'=> $data, 'msg'=> 'success']);
  355. }
  356. /**
  357. * 到店统计(部门)
  358. */
  359. public function arrivalOrg(){
  360. $token = $this->request->token;
  361. $start_time_get = input('start_time', time(), 'strtotime');
  362. $end_time_get = input('end_time', time(), 'strtotime');
  363. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  364. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  365. $times = input('times', 0, 'intval');
  366. if (!in_array($times, [1,2,3])){
  367. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  368. }
  369. $root_sub = orgSubIds($token['root_org']);
  370. // 所选部门
  371. $org_id = input('org_id', '', 'intval');
  372. if (!$org_id) {
  373. $org_id = $token['org_id'];
  374. }
  375. if (!in_array($org_id, $root_sub)){
  376. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  377. }
  378. // 所有客户量
  379. $sub_org_ids = orgSubIds($org_id);
  380. $c_where[] = ['org_id', 'in', $sub_org_ids];
  381. $c_where[] = ['state', 'NOT NULL', null];
  382. $customer_all = Customer::where($c_where)->where(function($query){
  383. $not_sure = Customer::changeState('待确认', 'chaos');
  384. $or1[] = ['crm_res_id', 'null', null];
  385. $or2[] = ['crm_res_id', 'not null', null];
  386. $or2[] = ['state', 'not in', $not_sure];
  387. $query->whereOr([$or1, $or2]);
  388. })->column('id,addtime,employee_id,died,sign_time,state');
  389. $customer = []; // 时间段内有效
  390. $wuxiao = Customer::changeState('无效', 'chaos');
  391. $start_totime = strtotime($start_time);
  392. $end_totime = strtotime($end_time);
  393. $customer_employee_id = []; // 客户所属员工id
  394. // 每个员工的客户
  395. $employee_customer = [];
  396. foreach ($customer_all as $k => $v) {
  397. $customer_employee_id[$v['id']] = $v['employee_id'];
  398. if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
  399. $customer[] = $v;
  400. if (isset($employee_customer[$v['employee_id']])) {
  401. $employee_customer[$v['employee_id']][] = $v;
  402. } else {
  403. $employee_customer[$v['employee_id']] = [];
  404. $employee_customer[$v['employee_id']][] = $v;
  405. }
  406. }
  407. }
  408. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  409. $db_where[] = ['Customer.org_id', 'in', $sub_org_ids];
  410. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  411. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  412. $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
  413. $log_list = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date,employee_id')
  414. ->view('Customer', 'sign_time customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
  415. ->where($db_where)
  416. ->order('confirm_date asc')
  417. ->select()
  418. ->toArray();
  419. // 先查出来后面要用到的已量房、已到场记录
  420. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  421. $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  422. $lw_where[] = ['Customer.org_id', 'in', $sub_org_ids];
  423. $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  424. $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
  425. $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
  426. $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
  427. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  428. ->where($lw_where)
  429. ->order('confirm_date asc')
  430. ->select()
  431. ->toArray();
  432. $lf_list = [];
  433. $wc_list = [];
  434. foreach ($lw_log_list as $k => $v) {
  435. if (in_array($v['state'], $lf_state, true)) {
  436. if (empty($lf_list[$v['customer_id']])) {
  437. $lf_list[$v['customer_id']] = $v['confirm_date'];
  438. }
  439. }
  440. if (in_array($v['state'], $wc_state, true)) {
  441. if (empty($wc_list[$v['customer_id']])) {
  442. $wc_list[$v['customer_id']] = $v['confirm_date'];
  443. }
  444. }
  445. }
  446. // 查出来签单的数据
  447. $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  448. $qd_where[] = ['Customer.org_id', 'in', $sub_org_ids];
  449. $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  450. $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state];
  451. $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
  452. ->view('Customer', 'org_id', 'Customer.id=CustomerVisitLog.customer_id')
  453. ->where($qd_where)
  454. ->select()
  455. ->toArray();
  456. $qd_list = [];
  457. foreach ($qd_log_list as $k => $v) {
  458. $qd_list[$v['org_id']][] = $v['customer_id'];
  459. }
  460. // 每个员工的跟进记录
  461. $employee_log = [];
  462. foreach ($log_list as $k => $v) {
  463. if (isset($customer_employee_id[$v['customer_id']])) {
  464. $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
  465. } else {
  466. $employee_log[$v['employee_id']][] = $v;
  467. }
  468. }
  469. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  470. foreach ($sub_org as $k => $v) {
  471. // 是否有子部门
  472. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  473. if ($have_sub_org > 0) {
  474. $sub_org[$k]['have_sub_org'] = true;
  475. } else {
  476. $sub_org[$k]['have_sub_org'] = false;
  477. }
  478. $v_sub_org = orgSubIds($v['id']);
  479. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  480. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  481. // 所有客户量
  482. $cusdata = [];
  483. $s_list = []; // log
  484. foreach ($org_employee_ids as $kk => $vv) {
  485. if (isset($employee_customer[$vv])) {
  486. $cusdata = array_merge($cusdata, $employee_customer[$vv]);
  487. }
  488. if (isset($employee_log[$vv])) {
  489. $s_list = array_merge($s_list, $employee_log[$vv]);
  490. }
  491. }
  492. $all_customer_count = count($cusdata);
  493. $list = [];
  494. foreach ($s_list as $kk => $vv){
  495. $list[$vv['customer_id']][] = $vv;
  496. }
  497. $customer_ids = [];
  498. switch ($times){
  499. case 1:
  500. // 到店次数
  501. $one_arrival = 0; // 一次到店量
  502. $one_days = 0; // 一次到店天数间隔
  503. $lf_count = 0; // 量房转到店
  504. $lf_days = 0;
  505. $wc_count = 0; // 外场转到店
  506. $wc_days = 0;
  507. $nc_count = 0; // 内场活动到店
  508. foreach ($list as $kk => $vv){
  509. if (count($vv) == 1) {
  510. $one_arrival ++;
  511. $customer_ids[] = $kk;
  512. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600));
  513. $one_days += $days;
  514. if (isset($lf_list[$kk])) {
  515. $lf_count ++;
  516. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600));
  517. $lf_days += $days;
  518. }
  519. if (isset($wc_list[$kk])) {
  520. $wc_count ++;
  521. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600));
  522. $wc_days += $days;
  523. }
  524. // 内场活动到店
  525. if (!empty($vv[0]['aid'])){
  526. $nc_count ++;
  527. }
  528. }
  529. }
  530. $qd_find = [];
  531. foreach ($sub_org_ids as $o_id) {
  532. if (isset($qd_list[$o_id])) {
  533. $qd_find = array_merge($qd_find, $qd_list[$o_id]);
  534. }
  535. }
  536. $one_qd = count(array_unique(array_intersect($qd_find, $customer_ids)));
  537. $sub_org[$k]['count'] = $one_arrival;
  538. $sub_org[$k]['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
  539. $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
  540. $sub_org[$k]['lf_count'] = $lf_count;
  541. $sub_org[$k]['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
  542. $sub_org[$k]['wc_count'] = $wc_count;
  543. $sub_org[$k]['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
  544. $sub_org[$k]['nc_count'] = $nc_count;
  545. $sub_org[$k]['qd_count'] = $one_qd;
  546. break;
  547. case 2:
  548. // 到店次数
  549. $two_arrival = 0; // 到店量
  550. $two_days = 0; // 到店天数间隔
  551. foreach ($list as $kk => $vv){
  552. if (count($vv) == 2) {
  553. $two_arrival ++;
  554. $customer_ids[] = $kk;
  555. $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600));
  556. $two_days += $days;
  557. }
  558. }
  559. $qd_find = [];
  560. foreach ($sub_org_ids as $o_id) {
  561. if (isset($qd_list[$o_id])) {
  562. $qd_find = array_merge($qd_find, $qd_list[$o_id]);
  563. }
  564. }
  565. $two_qd = count(array_unique(array_intersect($qd_find, $customer_ids)));
  566. $sub_org[$k]['count'] = $two_arrival;
  567. $sub_org[$k]['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
  568. $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
  569. $sub_org[$k]['qd_count'] = $two_qd;
  570. break;
  571. case 3:
  572. $three_arrival = 0; // 到店量
  573. $three_days = 0; // 到店天数间隔
  574. foreach ($list as $kk => $vv){
  575. if (count($vv) > 2) {
  576. $three_arrival ++;
  577. $customer_ids[] = $kk;
  578. $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600));
  579. $three_days += $days;
  580. }
  581. }
  582. $qd_find = [];
  583. foreach ($sub_org_ids as $o_id) {
  584. if (isset($qd_list[$o_id])) {
  585. $qd_find = array_merge($qd_find, $qd_list[$o_id]);
  586. }
  587. }
  588. $three_qd = count(array_unique(array_intersect($qd_find, $customer_ids)));
  589. $sub_org[$k]['count'] = $three_arrival;
  590. $sub_org[$k]['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
  591. $sub_org[$k]['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
  592. $sub_org[$k]['qd_count'] = $three_qd;
  593. break;
  594. default:
  595. break;
  596. }
  597. // 邀约计算
  598. unset($subscribe_where);
  599. $subscribe_where[] = ['state', 'in', [0, 1]];
  600. $subscribe_where[] = ['type', '=', 1];
  601. $subscribe_where[] = ['customer_id', 'in', $customer_ids];
  602. $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
  603. $xs_count = 0; // 销售一次
  604. $sj_count = 0; // 设计一次
  605. foreach ($subscribe as $sub_v){
  606. if (empty($sub_v['designer_id'])){
  607. $xs_count ++;
  608. } else {
  609. $sj_count ++;
  610. }
  611. }
  612. $sub_org[$k]['xs_yaoyue'] = $xs_count;
  613. $sub_org[$k]['xs_yaoyue_lv'] = $sub_org[$k]['count'] > 0 ? round($xs_count * 100 / $sub_org[$k]['count'], 2) : 0;
  614. $sub_org[$k]['sj_yaoyue'] = $sj_count;
  615. $sub_org[$k]['sj_yaoyue_lv'] = $sub_org[$k]['count'] > 0 ? round($sj_count * 100 / $sub_org[$k]['count'], 2) : 0;
  616. }
  617. $order = input('order', '', 'trim');
  618. if ($order) {
  619. $order_arr = explode(' ', $order);
  620. $sort_field = $order_arr[0] ?? 'count';
  621. $sort_order = $order_arr[1] ?? 'desc';
  622. switch ($sort_field){
  623. case 'count':
  624. $field = 'count';
  625. break;
  626. case 'wc_count':
  627. $field = 'wc_count';
  628. break;
  629. case 'qd_count':
  630. $field = 'qd_count';
  631. break;
  632. case 'lv':
  633. $field = 'lv';
  634. break;
  635. default:
  636. $field = 'notFound';
  637. break;
  638. }
  639. if ($sort_order == 'asc'){
  640. $sort = SORT_ASC;
  641. } else {
  642. $sort = SORT_DESC;
  643. }
  644. $arr = [];
  645. foreach ($sub_org as $v){
  646. if (!isset($v[$field])){
  647. break;
  648. }
  649. $arr[] = $v[$field];
  650. }
  651. if (!empty($arr)){
  652. array_multisort($arr, $sort, $sub_org);
  653. }
  654. }
  655. return json(['code'=> self::success, 'data'=> $sub_org, 'msg'=> 'success']);
  656. }
  657. /**
  658. * 到店统计(员工)
  659. */
  660. public function arrivalEmployee(){
  661. $token = $this->request->token;
  662. $start_time_get = input('start_time', time(), 'strtotime');
  663. $end_time_get = input('end_time', time(), 'strtotime');
  664. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  665. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  666. $times = input('times', 0, 'intval');
  667. if (!in_array($times, [1,2,3])){
  668. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  669. }
  670. $root_sub = orgSubIds($token['root_org']);
  671. // 所选部门
  672. $org_id = input('org_id', $token['org_id'], 'intval');
  673. if (!in_array($org_id, $root_sub)){
  674. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  675. }
  676. // 所选部门员工统计数据
  677. $e_where[] = ['org_id', '=', $org_id];
  678. $e_where[] = ['state', '=', '在职'];
  679. $e_where[] = ['uid', '<>', 0];
  680. $employee_list = Employee::with(['user'=> function($query){
  681. $query->field('id,headimgurl');
  682. }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
  683. $all_where[] = ['org_id', '=', $org_id];
  684. $all_where[] = ['state', 'NOT NULL', null];
  685. $all_customer_list = Customer::where($all_where)->where(function($query){
  686. $not_sure = Customer::changeState('待确认', 'chaos');
  687. $or1[] = ['state', 'not in', $not_sure];
  688. $or1[] = ['crm_res_id', '>', 0];
  689. $or2[] = ['crm_res_id', 'null', null];
  690. $query->whereOr([$or1, $or2]);
  691. })->column('id,employee_id,died,sign_time,state');
  692. $customer = []; // 时间段内有效
  693. $wuxiao = Customer::changeState('无效', 'chaos');
  694. $start_totime = strtotime($start_time);
  695. $end_totime = strtotime($end_time);
  696. // 每个员工的客户
  697. $employee_customer = [];
  698. $customer_employee_id = []; // 客户所属员工id
  699. foreach ($all_customer_list as $k => $v) {
  700. $customer_employee_id[$v['id']] = $v['employee_id'];
  701. if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
  702. $customer[] = $v;
  703. if (isset($employee_customer[$v['employee_id']])) {
  704. $employee_customer[$v['employee_id']][] = $v;
  705. } else {
  706. $employee_customer[$v['employee_id']] = [];
  707. $employee_customer[$v['employee_id']][] = $v;
  708. }
  709. }
  710. }
  711. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  712. $db_where[] = ['Customer.org_id', '=', $org_id];
  713. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  714. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  715. $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
  716. $visit_log = Db::view('CustomerVisitLog', 'id,customer_id,addtime,employee_id,confirm_date,aid')
  717. ->view('Customer', 'addtime customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
  718. ->where($db_where)
  719. ->select()
  720. ->toArray();
  721. // 先查出来后面要用到的已量房、已到场记录
  722. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  723. $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  724. $lw_where[] = ['Customer.org_id', '=', $org_id];
  725. $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  726. $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
  727. $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
  728. $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
  729. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  730. ->where($lw_where)
  731. ->order('confirm_date asc')
  732. ->select()
  733. ->toArray();
  734. $lf_list = [];
  735. $wc_list = [];
  736. foreach ($lw_log_list as $k => $v) {
  737. if (in_array($v['state'], $lf_state, true)) {
  738. if (empty($lf_list[$v['customer_id']])) {
  739. $lf_list[$v['customer_id']] = $v['confirm_date'];
  740. }
  741. }
  742. if (in_array($v['state'], $wc_state, true)) {
  743. if (empty($wc_list[$v['customer_id']])) {
  744. $wc_list[$v['customer_id']] = $v['confirm_date'];
  745. }
  746. }
  747. }
  748. // 查出来签单的数据
  749. $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  750. $qd_where[] = ['Customer.org_id', '=', $org_id];
  751. $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  752. $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state];
  753. $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
  754. ->view('Customer', 'employee_id', 'Customer.id=CustomerVisitLog.customer_id')
  755. ->where($qd_where)
  756. ->select()
  757. ->toArray();
  758. $qd_list = [];
  759. foreach ($qd_log_list as $k => $v) {
  760. $qd_list[$v['customer_id']] = 1;
  761. }
  762. $employee_log = [];
  763. foreach ($visit_log as $k => $v) {
  764. if (isset($customer_employee_id[$v['customer_id']])) {
  765. $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
  766. } else {
  767. $employee_log[$v['employee_id']][] = $v;
  768. }
  769. }
  770. foreach ($employee_list as $k => $v) {
  771. // 所有客户量
  772. $all_customer_count = 0;
  773. if (isset($employee_customer[$v['id']])) {
  774. $all_customer_count = count($employee_customer[$v['id']]);
  775. }
  776. $list = [];
  777. $v_visit_log = [];
  778. if (isset($employee_log[$v['id']])) {
  779. $v_visit_log = $employee_log[$v['id']];
  780. }
  781. foreach ($v_visit_log as $kk => $vv){
  782. $list[$vv['customer_id']][] = $vv;
  783. }
  784. $customer_ids = [];
  785. switch ($times){
  786. case 1:
  787. // 到店次数
  788. $one_arrival = 0; // 一次到店量
  789. $one_days = 0; // 一次到店天数间隔
  790. $lf_count = 0; // 量房转到店
  791. $lf_days = 0;
  792. $wc_count = 0; // 外场转到店
  793. $wc_days = 0;
  794. $nc_count = 0; // 内场活动到店
  795. $one_qd = 0;
  796. foreach ($list as $kk => $vv){
  797. if (count($vv) == 1) {
  798. $one_arrival ++;
  799. $customer_id[] = $kk;
  800. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600));
  801. $one_days += $days;
  802. if (isset($qd_list[$kk])) {
  803. $one_qd ++;
  804. }
  805. if (isset($lf_list[$kk])) {
  806. $lf_count ++;
  807. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600));
  808. $lf_days += $days;
  809. }
  810. if (isset($wc_list[$kk])) {
  811. $wc_count ++;
  812. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600));
  813. $wc_days += $days;
  814. }
  815. // 内场活动到店
  816. if (!empty($vv[0]['aid'])){
  817. $nc_count ++;
  818. }
  819. }
  820. }
  821. $employee_list[$k]['count'] = $one_arrival;
  822. $employee_list[$k]['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
  823. $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
  824. $employee_list[$k]['lf_count'] = $lf_count;
  825. $employee_list[$k]['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
  826. $employee_list[$k]['wc_count'] = $wc_count;
  827. $employee_list[$k]['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
  828. $employee_list[$k]['nc_count'] = $nc_count;
  829. $employee_list[$k]['qd_count'] = $one_qd;
  830. break;
  831. case 2:
  832. // 到店次数
  833. $two_arrival = 0; // 到店量
  834. $two_days = 0; // 到店天数间隔
  835. $two_qd = 0;
  836. foreach ($list as $kk => $vv){
  837. if (count($vv) == 2) {
  838. $two_arrival ++;
  839. $customer_ids[] = $kk;
  840. $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600));
  841. $two_days += $days;
  842. if (isset($qd_list[$kk])) {
  843. $two_qd ++;
  844. }
  845. }
  846. }
  847. $employee_list[$k]['count'] = $two_arrival;
  848. $employee_list[$k]['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
  849. $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
  850. $employee_list[$k]['qd_count'] = $two_qd;
  851. break;
  852. case 3:
  853. $three_arrival = 0; // 到店量
  854. $three_days = 0; // 到店天数间隔
  855. $three_qd = 0;
  856. foreach ($list as $kk => $vv){
  857. if (count($vv) > 2) {
  858. $three_arrival ++;
  859. $customer_ids[] = $kk;
  860. $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600));
  861. $three_days += $days;
  862. if (isset($qd_list[$kk])) {
  863. $three_qd ++;
  864. }
  865. }
  866. }
  867. $employee_list[$k]['count'] = $three_arrival;
  868. $employee_list[$k]['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
  869. $employee_list[$k]['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
  870. $employee_list[$k]['qd_count'] = $three_qd;
  871. break;
  872. default:
  873. break;
  874. }
  875. // 邀约计算
  876. unset($subscribe_where);
  877. $subscribe_where[] = ['state', 'in', [0, 1]];
  878. $subscribe_where[] = ['type', '=', 1];
  879. $subscribe_where[] = ['customer_id', 'in', $customer_ids];
  880. $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
  881. $xs_count = 0; // 销售一次
  882. $sj_count = 0; // 设计一次
  883. foreach ($subscribe as $sub_v){
  884. if (empty($sub_v['designer_id'])){
  885. $xs_count ++;
  886. } else {
  887. $sj_count ++;
  888. }
  889. }
  890. $employee_list[$k]['xs_yaoyue'] = $xs_count;
  891. $employee_list[$k]['xs_yaoyue_lv'] = $employee_list[$k]['count'] > 0 ? round($xs_count * 100 / $employee_list[$k]['count'], 2) : 0;
  892. $employee_list[$k]['sj_yaoyue'] = $sj_count;
  893. $employee_list[$k]['sj_yaoyue_lv'] = $employee_list[$k]['count'] > 0 ? round($sj_count * 100 / $employee_list[$k]['count'], 2) : 0;
  894. }
  895. $order = input('order', '', 'trim');
  896. if ($order) {
  897. $order_arr = explode(' ', $order);
  898. $sort_field = $order_arr[0] ?? 'count';
  899. $sort_order = $order_arr[1] ?? 'desc';
  900. switch ($sort_field){
  901. case 'count':
  902. $field = 'count';
  903. break;
  904. case 'wc_count':
  905. $field = 'wc_count';
  906. break;
  907. case 'qd_count':
  908. $field = 'qd_count';
  909. break;
  910. case 'lv':
  911. $field = 'lv';
  912. break;
  913. default:
  914. $field = 'notFound';
  915. break;
  916. }
  917. if ($sort_order == 'asc'){
  918. $sort = SORT_ASC;
  919. } else {
  920. $sort = SORT_DESC;
  921. }
  922. $arr = [];
  923. foreach ($employee_list as $v){
  924. if (!isset($v[$field])){
  925. break;
  926. }
  927. $arr[] = $v[$field];
  928. }
  929. if (!empty($arr)){
  930. array_multisort($arr, $sort, $employee_list);
  931. }
  932. }
  933. $page = input('page', 1, 'intval');
  934. $limit = input('limit', 10, 'intval');
  935. $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit);
  936. return json(['code'=> self::success, 'data'=> $employee_list, 'msg'=> 'success']);
  937. }
  938. /**
  939. * 到店统计(客户)
  940. */
  941. public function arrivalCustomer(){
  942. $token = $this->request->token;
  943. $start_time_get = input('start_time', time(), 'strtotime');
  944. $end_time_get = input('end_time', time(), 'strtotime');
  945. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  946. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  947. $times = input('times', 0, 'intval');
  948. if (!in_array($times, [1,2,3])){
  949. return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
  950. }
  951. $employee_id = input('employee_id', '', 'intval');
  952. $sub_org = orgSubIds($token['org_id']);
  953. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  954. if (!in_array($employee_id, $sub_employee_ids)){
  955. return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
  956. }
  957. $dd_state = CustomerVisitLog::changeState('已到店', 'chaos');
  958. $wuxiao = Customer::changeState('无效', 'chaos');
  959. $db_where[] = ['Customer.employee_id', '=', $employee_id];
  960. $db_where[] = ['Customer.state', 'NOT NULL', null];
  961. $db_where[] = ['Customer.state', 'not in', $wuxiao];
  962. $db_where[] = ['Customer.died', '<>', 2];
  963. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  964. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  965. $db_where[] = ['CustomerVisitLog.state', 'in', $dd_state];
  966. $s_list = Db::view('CustomerVisitLog', 'id,customer_id,addtime,aid,confirm_date')
  967. ->view('Customer', 'sign_time customer_addtime', 'Customer.id=CustomerVisitLog.customer_id')
  968. ->where($db_where)
  969. ->order('confirm_date asc')
  970. ->select()
  971. ->toArray();
  972. $list = [];
  973. foreach ($s_list as $k => $v){
  974. $list[$v['customer_id']][] = $v;
  975. }
  976. // 先查出来后面要用到的已量房、已到场记录
  977. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  978. $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  979. $lw_where[] = ['Customer.employee_id', '=', $employee_id];
  980. $lw_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  981. $lw_where[] = ['CustomerVisitLog.confirm_date', '<', $end_time];
  982. $lw_where[] = ['CustomerVisitLog.state', 'in', array_merge($lf_state, $wc_state)];
  983. $lw_log_list = Db::view('CustomerVisitLog', 'id,customer_id,confirm_date,state')
  984. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  985. ->where($lw_where)
  986. ->order('confirm_date asc')
  987. ->select()
  988. ->toArray();
  989. $lf_list = [];
  990. $wc_list = [];
  991. foreach ($lw_log_list as $k => $v) {
  992. if (in_array($v['state'], $lf_state, true)) {
  993. if (empty($lf_list[$v['customer_id']])) {
  994. $lf_list[$v['customer_id']] = $v['confirm_date'];
  995. }
  996. }
  997. if (in_array($v['state'], $wc_state, true)) {
  998. if (empty($wc_list[$v['customer_id']])) {
  999. $wc_list[$v['customer_id']] = $v['confirm_date'];
  1000. }
  1001. }
  1002. }
  1003. // 查出来签单的数据
  1004. $qd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  1005. $qd_where[] = ['Customer.employee_id', '=', $employee_id];
  1006. $qd_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  1007. $qd_where[] = ['CustomerVisitLog.state', 'in', $qd_state];
  1008. $qd_log_list = Db::view('CustomerVisitLog', 'id,customer_id')
  1009. ->view('Customer', 'org_id', 'Customer.id=CustomerVisitLog.customer_id')
  1010. ->where($qd_where)
  1011. ->select()
  1012. ->toArray();
  1013. $qd_list = [];
  1014. foreach ($qd_log_list as $k => $v) {
  1015. $qd_list[$v['customer_id']] = 1;
  1016. }
  1017. $wuxiao = Customer::changeState('无效', 'chaos');
  1018. $not_sure = Customer::changeState('待确认', 'chaos');
  1019. $wuxiao_not_sure = array_merge($wuxiao, $not_sure);
  1020. $all_where[] = ['employee_id', '=', $employee_id];
  1021. $all_where[] = ['state', 'NOT NULL', null];
  1022. $all_where[] = ['state', 'NOT IN', $wuxiao_not_sure];
  1023. $all_where[] = ['addtime', 'between', [$start_time, $end_time]];
  1024. $all_where[] = ['died', '<>', 2];
  1025. $all_customer_count = Customer::where($all_where)->where(function($query){
  1026. $not_sure = Customer::changeState('待确认', 'chaos');
  1027. $or1[] = ['state', 'not in', $not_sure];
  1028. $or1[] = ['crm_res_id', '>', 0];
  1029. $or2[] = ['crm_res_id', 'null', null];
  1030. $query->whereOr([$or1, $or2]);
  1031. })->count();
  1032. $employee_data['count'] = 0;
  1033. $employee_data['days'] = 0;
  1034. $employee_data['lv'] = 0;
  1035. $employee_data['lf_count'] = 0;
  1036. $employee_data['lf_days'] = 0;
  1037. $employee_data['wc_count'] = 0;
  1038. $employee_data['wc_days'] = 0;
  1039. $employee_data['nc_count'] = 0;
  1040. $employee_data['qd_count'] = 0;
  1041. $customer_ids = [];
  1042. switch ($times){
  1043. case 1:
  1044. // 到店次数
  1045. $one_arrival = 0; // 一次到店量
  1046. $one_days = 0; // 一次到店天数间隔
  1047. $lf_count = 0; // 量房转到店
  1048. $lf_days = 0;
  1049. $wc_count = 0; // 外场转到店
  1050. $wc_days = 0;
  1051. $nc_count = 0; // 内场活动到店
  1052. $one_qd = 0;
  1053. foreach ($list as $kk => $vv){
  1054. if (count($vv) == 1) {
  1055. $one_arrival ++;
  1056. $customer_ids[] = $kk;
  1057. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($vv[0]['customer_addtime'])) / (24*3600));
  1058. $one_days += $days;
  1059. if (isset($qd_list[$kk])) {
  1060. $one_qd ++;
  1061. }
  1062. if (isset($lf_list[$kk])) {
  1063. $lf_count ++;
  1064. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($lf_list[$kk])) / (24*3600));
  1065. $lf_days += $days;
  1066. }
  1067. if (isset($wc_list[$kk])) {
  1068. $wc_count ++;
  1069. $days = ceil((strtotime($vv[0]['confirm_date']) - strtotime($wc_list[$kk])) / (24*3600));
  1070. $wc_days += $days;
  1071. }
  1072. // 内场活动到店
  1073. if (!empty($vv[0]['aid'])){
  1074. $nc_count ++;
  1075. }
  1076. }
  1077. }
  1078. $employee_data['count'] = $one_arrival;
  1079. $employee_data['days'] = $one_arrival > 0 ? round($one_days / $one_arrival, 1) : 0;
  1080. $employee_data['lv'] = $all_customer_count > 0 ? round($one_arrival * 100 / $all_customer_count, 2) : 0;
  1081. $employee_data['lf_count'] = $lf_count;
  1082. $employee_data['lf_days'] = $lf_count > 0 ? round($lf_days / $lf_count, 1) : 0;
  1083. $employee_data['wc_count'] = $wc_count;
  1084. $employee_data['wc_days'] = $wc_count > 0 ? round($wc_days / $wc_count, 1) : 0;
  1085. $employee_data['nc_count'] = $nc_count;
  1086. $employee_data['qd_count'] = $one_qd;
  1087. break;
  1088. case 2:
  1089. // 到店次数
  1090. $two_arrival = 0; // 到店量
  1091. $two_days = 0; // 到店天数间隔
  1092. $two_qd = 0;
  1093. foreach ($list as $kk => $vv){
  1094. if (count($vv) == 2) {
  1095. $two_arrival ++;
  1096. $customer_ids[] = $kk;
  1097. $days = ceil((strtotime($vv[1]['confirm_date']) - strtotime($vv[0]['confirm_date'])) / (24*3600));
  1098. $two_days += $days;
  1099. if (isset($qd_list[$kk])) {
  1100. $two_qd ++;
  1101. }
  1102. }
  1103. }
  1104. $employee_data['count'] = $two_arrival;
  1105. $employee_data['days'] = $two_arrival > 0 ? round($two_days / $two_arrival, 1) : 0;
  1106. $employee_data['lv'] = $all_customer_count > 0 ? round($two_arrival * 100 / $all_customer_count, 2) : 0;
  1107. $employee_data['qd_count'] = $two_qd;
  1108. break;
  1109. case 3:
  1110. $three_arrival = 0; // 到店量
  1111. $three_days = 0; // 到店天数间隔
  1112. $three_qd = 0;
  1113. foreach ($list as $kk => $vv){
  1114. if (count($vv) > 2) {
  1115. $three_arrival ++;
  1116. $customer_ids[] = $kk;
  1117. $days = ceil((strtotime($vv[2]['confirm_date']) - strtotime($vv[1]['confirm_date'])) / (24*3600));
  1118. $three_days += $days;
  1119. if (isset($qd_list[$kk])) {
  1120. $three_qd ++;
  1121. }
  1122. }
  1123. }
  1124. $employee_data['count'] = $three_arrival;
  1125. $employee_data['days'] = $three_arrival > 0 ? round($three_days / $three_arrival, 1) : 0;
  1126. $employee_data['lv'] = $all_customer_count > 0 ? round($three_arrival * 100 / $all_customer_count, 2) : 0;
  1127. $employee_data['qd_count'] = $three_qd;
  1128. break;
  1129. default:
  1130. break;
  1131. }
  1132. // 邀约计算
  1133. $subscribe_where[] = ['state', 'in', [0, 1]];
  1134. $subscribe_where[] = ['type', '=', 1];
  1135. $subscribe_where[] = ['customer_id', 'in', $customer_ids];
  1136. $subscribe = CustomersSubscribe::where($subscribe_where)->field('id,customer_id,employee_id,designer_id')->select()->toArray();
  1137. $xs_count = 0; // 销售一次
  1138. $sj_count = 0; // 设计一次
  1139. foreach ($subscribe as $sub_v){
  1140. if (empty($sub_v['designer_id'])){
  1141. $xs_count ++;
  1142. } else {
  1143. $sj_count ++;
  1144. }
  1145. }
  1146. $employee_data['xs_yaoyue'] = $xs_count;
  1147. $employee_data['xs_yaoyue_lv'] = $employee_data['count'] > 0 ? round($xs_count * 100 / $employee_data['count'], 2) : 0;
  1148. $employee_data['sj_yaoyue'] = $sj_count;
  1149. $employee_data['sj_yaoyue_lv'] = $employee_data['count'] > 0 ? round($sj_count * 100 / $employee_data['count'], 2) : 0;
  1150. $employee_name = Employee::where('id', $employee_id)->value('name');
  1151. $employee_data['employee_name'] = $employee_name;
  1152. $data['employee'] = $employee_data;
  1153. $customer_id = [];
  1154. $customer_log_latest = [];
  1155. foreach ($list as $k => $v) {
  1156. switch ($times) {
  1157. case 1:
  1158. if (count($v) == 1) {
  1159. $customer_id[] = $k;
  1160. $customer_log_latest[$k] = $v[0]['confirm_date'];
  1161. }
  1162. break;
  1163. case 2:
  1164. if (count($v) == 2) {
  1165. $customer_id[] = $k;
  1166. $customer_log_latest[$k] = $v[1]['confirm_date'];
  1167. }
  1168. break;
  1169. case 3:
  1170. if (count($v) > 2) {
  1171. $customer_id[] = $k;
  1172. $add_key = count($v) - 1;
  1173. $customer_log_latest[$k] = $v[$add_key]['confirm_date'];
  1174. }
  1175. break;
  1176. default:
  1177. break;
  1178. }
  1179. }
  1180. $type = input('type', '', 'trim');
  1181. if ($type){
  1182. switch ($type){
  1183. case 'waichang':
  1184. $customer_id = [];
  1185. // 外场转到店
  1186. $wc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  1187. foreach ($customer_log_latest as $k => $v) {
  1188. unset($wc_where);
  1189. $wc_where[] = ['customer_id', '=', $k];
  1190. $wc_where[] = ['state', 'in', $wc_state];
  1191. $wc_where[] = ['confirm_date', '<', $v];
  1192. $wc_find = CustomerVisitLog::where($wc_where)->order('confirm_date asc')->findOrEmpty();
  1193. if (!$wc_find->isEmpty()){
  1194. $customer_id[] = $k;
  1195. }
  1196. }
  1197. break;
  1198. case 'liangfang':
  1199. $customer_id = [];
  1200. // 量房转到店
  1201. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  1202. foreach ($customer_log_latest as $k => $v) {
  1203. unset($lf_where);
  1204. $lf_where[] = ['customer_id', '=', $k];
  1205. $lf_where[] = ['state', 'in', $lf_state];
  1206. $lf_where[] = ['confirm_date', '<', $v];
  1207. $lf_find = CustomerVisitLog::where($lf_where)->order('confirm_date asc')->findOrEmpty();
  1208. if (!$lf_find->isEmpty()) {
  1209. $customer_id[] = $k;
  1210. }
  1211. }
  1212. break;
  1213. case 'qiandan':
  1214. // 签单
  1215. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  1216. $jd_where[] = ['customer_id', 'in', $customer_id];
  1217. $jd_where[] = ['state', 'in', $jd_state];
  1218. $customer_id = CustomerVisitLog::where($jd_where)->column('customer_id');
  1219. break;
  1220. default:
  1221. $customer_id = [];
  1222. break;
  1223. }
  1224. }
  1225. $page = input('page', 1, 'intval');
  1226. $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select();
  1227. $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();
  1228. // 状态统计获取
  1229. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  1230. $customersState = [];
  1231. foreach ($customersStateList as $s) {
  1232. if (!isset($customersState[$s['customer_id']]))
  1233. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  1234. $customersState[$s['customer_id']]['count'] += $s['num'];
  1235. // 到店,量房,活动,定金,签单
  1236. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  1237. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  1238. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  1239. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  1240. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  1241. }
  1242. foreach ($customers as $k => $v) {
  1243. $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  1244. }
  1245. $data['customer'] = $customers;
  1246. return json(['code'=> self::success, 'data'=> $data, 'msg'=> 'success']);
  1247. }
  1248. /**
  1249. * 量房统计
  1250. */
  1251. public function measure_house_index()
  1252. {
  1253. $token = $this->request->token;
  1254. $root_sub = orgSubIds($token['root_org']);
  1255. // 所选部门
  1256. $org_id = input('org_id', '', 'intval');
  1257. $org_id = !empty($org_id) ? $org_id : $token['org_id'];
  1258. if (!in_array($org_id, $root_sub)){
  1259. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  1260. }
  1261. $start_time_get = input('start_time', time(), 'strtotime');
  1262. $end_time_get = input('end_time', time(), 'strtotime');
  1263. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  1264. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  1265. //员工数量
  1266. $org_name = Org::where('id', $org_id)->value('name');
  1267. $sub_org = orgSubIds($org_id);
  1268. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  1269. // 所有客户量
  1270. $wuxiao = Customer::changeState('无效', 'chaos');
  1271. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  1272. $c_where[] = ['org_id', 'in', $sub_org];
  1273. $c_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  1274. $c_where[] = ['state', 'NOT NULL', null];
  1275. $c_where[] = ['state', 'not in', $wuxiao];
  1276. $c_where[] = ['died', '<>', 2];
  1277. $cusdata = Customer::where($c_where)->where(function($query){
  1278. $not_sure = Customer::changeState('待确认', 'chaos');
  1279. $or1[] = ['crm_res_id', 'null', null];
  1280. $or2[] = ['crm_res_id', 'not null', null];
  1281. $or2[] = ['state', 'not in', $not_sure];
  1282. $query->whereOr([$or1, $or2]);
  1283. })->column('id,addtime');
  1284. $db_where[] = ['Customer.org_id', 'in', $sub_org];
  1285. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  1286. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  1287. $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
  1288. $list = Db::view('CustomerVisitLog', 'id,customer_id,measure_room_img_type,addtime,confirm_date')
  1289. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  1290. ->where($db_where)
  1291. ->select()
  1292. ->toArray();
  1293. $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
  1294. $meatime = [];
  1295. $ylids = [];
  1296. foreach($list as $key => $val){
  1297. if(!empty($val['measure_room_img_type'])){
  1298. $allmea = explode(',', $val['measure_room_img_type']);
  1299. foreach($allmea as $k => $v){
  1300. if(isset($v) && strstr($v, '量房图片')) $mesimg++;
  1301. if(isset($v) && strstr($v, '量房报告图片')) $demand++;
  1302. if(isset($v) && strstr($v, '客户需求图片')) $report++;
  1303. }
  1304. }
  1305. if(!in_array($val['customer_id'], array_keys($meatime))) $meatime[$val['customer_id']] = $val['confirm_date'];
  1306. if(!in_array($val['customer_id'], $ylids)) $ylids[] = $val['customer_id'];
  1307. }
  1308. $ylcus = Customer::where('id', 'in', $ylids)->field('id,addtime')->select()->toArray();
  1309. $agvtime = [];
  1310. foreach($ylcus as $key => $val){
  1311. if(!empty($meatime[$val['id']])) $agvtime[] = strtotime($meatime[$val['id']]) - strtotime($val['addtime']) > 0 ? strtotime($meatime[$val['id']]) - strtotime($val['addtime']) : 0;
  1312. }
  1313. $meahounum = count($list);
  1314. $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
  1315. $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0;
  1316. $data = ['org_name'=> $org_name, 'employee_count'=> count($sub_employee_ids), 'report'=> $report, 'demand'=> $demand, 'mesimg'=> $mesimg, 'meahounum'=> $meahounum, 'agvcycle'=> $agvcycle, 'mearate'=> $mearate];
  1317. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  1318. }
  1319. /**
  1320. * 量房部门数据
  1321. */
  1322. public function measure_house_org(){
  1323. $token = $this->request->token;
  1324. $param = $this->request->only(['org_id', 'start_time','end_time','order']);
  1325. $start_time_get = input('start_time', time(), 'strtotime');
  1326. $end_time_get = input('end_time', time(), 'strtotime');
  1327. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  1328. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  1329. $root_sub = orgSubIds($token['root_org']);
  1330. // 所选部门
  1331. $org_id = input('org_id', '', 'intval');
  1332. if (!$org_id) {
  1333. $org_id = $token['org_id'];
  1334. }
  1335. if (!in_array($org_id, $root_sub)){
  1336. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  1337. }
  1338. // 所选部门
  1339. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  1340. // 所有客户量 用于后面计算百分比
  1341. $sub_org_ids = orgSubIds($org_id);
  1342. $c_where[] = ['org_id', 'in', $sub_org_ids];
  1343. $c_where[] = ['state', 'NOT NULL', null];
  1344. $customer_all = Customer::where($c_where)->where(function($query){
  1345. $not_sure = Customer::changeState('待确认', 'chaos');
  1346. $or1[] = ['crm_res_id', 'null', null];
  1347. $or2[] = ['crm_res_id', 'not null', null];
  1348. $or2[] = ['state', 'not in', $not_sure];
  1349. $query->whereOr([$or1, $or2]);
  1350. })->column('id,addtime,employee_id,died,sign_time,state');
  1351. $customer = []; // 时间段内有效
  1352. $wuxiao = Customer::changeState('无效', 'chaos');
  1353. $start_totime = strtotime($start_time);
  1354. $end_totime = strtotime($end_time);
  1355. $customer_employee_id = []; // 客户所属员工id
  1356. // 每个员工的客户
  1357. $employee_customer = [];
  1358. foreach ($customer_all as $k => $v) {
  1359. $customer_employee_id[$v['id']] = $v['employee_id'];
  1360. if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
  1361. $customer[] = $v;
  1362. if (isset($employee_customer[$v['employee_id']])) {
  1363. $employee_customer[$v['employee_id']][] = $v;
  1364. } else {
  1365. $employee_customer[$v['employee_id']] = [];
  1366. $employee_customer[$v['employee_id']][] = $v;
  1367. }
  1368. }
  1369. }
  1370. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  1371. $db_where[] = ['Customer.org_id', 'in', $sub_org_ids];
  1372. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  1373. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  1374. $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
  1375. $log_list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date,employee_id')
  1376. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  1377. ->where($db_where)
  1378. ->order('confirm_date asc')
  1379. ->select()
  1380. ->toArray();
  1381. // 每个员工的跟进记录
  1382. $employee_log = [];
  1383. foreach ($log_list as $k => $v) {
  1384. if (isset($customer_employee_id[$v['customer_id']])) {
  1385. $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
  1386. } else {
  1387. $employee_log[$v['employee_id']][] = $v;
  1388. }
  1389. }
  1390. foreach ($sub_org as $k => $v){
  1391. // 是否有子部门
  1392. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  1393. if ($have_sub_org > 0){
  1394. $sub_org[$k]['have_sub_org'] = true;
  1395. } else {
  1396. $sub_org[$k]['have_sub_org'] = false;
  1397. }
  1398. unset($meatime);
  1399. unset($ylids);
  1400. unset($agvtime);
  1401. $v_sub_org = orgSubIds($v['id']);
  1402. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  1403. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  1404. $cusdata = [];
  1405. foreach ($org_employee_ids as $kk => $vv) {
  1406. if (isset($employee_customer[$vv])) {
  1407. $cusdata = array_merge($cusdata, $employee_customer[$vv]);
  1408. }
  1409. }
  1410. $list = [];
  1411. foreach ($org_employee_ids as $kk => $vv) {
  1412. if (isset($employee_log[$vv])) {
  1413. $list = array_merge($list, $employee_log[$vv]);
  1414. }
  1415. }
  1416. $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
  1417. $meatime = [];
  1418. $ylids = [];
  1419. foreach($list as $key => $val){
  1420. if(!empty($val['measure_room_img_type'])){
  1421. $allmea = explode(',', $val['measure_room_img_type']);
  1422. foreach($allmea as $m=>$p){
  1423. if(isset($p) && strstr($p,'量房图片')) $mesimg++;
  1424. if(isset($p) && strstr($p,'量房报告图片')) $demand++;
  1425. if(isset($p) && strstr($p,'客户需求图片')) $report++;
  1426. }
  1427. }
  1428. if(!in_array($val['customer_id'], array_keys($meatime))) $meatime[$val['customer_id']] = $val['confirm_date'];
  1429. if(!in_array($val['customer_id'], $ylids)) $ylids[] = $val['customer_id'];
  1430. }
  1431. $ylcus = Customer::where('id', 'in', $ylids)->field('id,sign_time')->select()->toArray();
  1432. $agvtime = [];
  1433. foreach($ylcus as $key => $val){
  1434. if(!empty($meatime[$val['id']])) $agvtime[] = (strtotime($meatime[$val['id']]) - strtotime($val['sign_time'])) > 0 ? strtotime($meatime[$val['id']]) - strtotime($val['sign_time']) : 0;
  1435. }
  1436. $meahounum = count($list);
  1437. $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
  1438. $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0;
  1439. $sub_org[$k]['report'] = $report;
  1440. $sub_org[$k]['demand'] = $demand;
  1441. $sub_org[$k]['mesimg'] = $mesimg;
  1442. $sub_org[$k]['meahounum'] = $meahounum;
  1443. $sub_org[$k]['agvcycle'] = $agvcycle;
  1444. $sub_org[$k]['mearate'] = $mearate;
  1445. }
  1446. if (!empty($param['order'])) {
  1447. $order_arr = explode(' ', $param['order']);
  1448. $sort_field = $order_arr[0] ?? 'meahounum';
  1449. $sort_order = $order_arr[1] ?? 'desc';
  1450. switch ($sort_field){
  1451. case 'meahounum':
  1452. $field = 'meahounum';
  1453. break;
  1454. case 'agvcycle':
  1455. $field = 'agvcycle';
  1456. break;
  1457. case 'mearate':
  1458. $field = 'mearate';
  1459. break;
  1460. default:
  1461. $field = 'notFound';
  1462. break;
  1463. }
  1464. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  1465. $arr = [];
  1466. foreach ($sub_org as $v){
  1467. if (!isset($v[$field])){
  1468. break;
  1469. }
  1470. $arr[] = $v[$field];
  1471. }
  1472. if (!empty($arr)){
  1473. array_multisort($arr, $sort, $sub_org);
  1474. }
  1475. }
  1476. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
  1477. }
  1478. /**
  1479. * 量房员工数据
  1480. */
  1481. public function measure_house_employee(){
  1482. $token = $this->request->token;
  1483. $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
  1484. $start_time_get = input('start_time', time(), 'strtotime');
  1485. $end_time_get = input('end_time', time(), 'strtotime');
  1486. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  1487. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  1488. // 所选部门
  1489. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  1490. $root_sub = orgSubIds($token['root_org']);
  1491. if (!in_array($org_id, $root_sub)){
  1492. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  1493. }
  1494. // 所选部门员工统计数据
  1495. $e_where[] = ['org_id', '=', $org_id];
  1496. $e_where[] = ['state', '=', '在职'];
  1497. $e_where[] = ['uid', '<>', 0];
  1498. $employee_list = Employee::with(['user'=> function($query){
  1499. $query->field('id,headimgurl');
  1500. }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
  1501. // 所有客户量
  1502. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  1503. $c_where[] = ['org_id', '=', $org_id];
  1504. $c_where[] = ['state', 'NOT NULL', null];
  1505. $customer_all = Customer::where($c_where)->where(function($query){
  1506. $not_sure = Customer::changeState('待确认', 'chaos');
  1507. $or1[] = ['crm_res_id', 'null', null];
  1508. $or2[] = ['crm_res_id', 'not null', null];
  1509. $or2[] = ['state', 'not in', $not_sure];
  1510. $query->whereOr([$or1, $or2]);
  1511. })->column('id,addtime,employee_id,died,sign_time,state');
  1512. $customer = []; // 时间段内有效
  1513. $wuxiao = Customer::changeState('无效', 'chaos');
  1514. $start_totime = strtotime($start_time);
  1515. $end_totime = strtotime($end_time);
  1516. // 每个员工的客户
  1517. $employee_customer = [];
  1518. $customer_employee_id = []; // 客户所属员工id
  1519. foreach ($customer_all as $k => $v) {
  1520. $customer_employee_id[$v['id']] = $v['employee_id'];
  1521. if ($v['died'] != 2 && strtotime($v['sign_time']) > $start_totime && strtotime($v['sign_time']) < $end_totime && !in_array($v['state'], $wuxiao, true)) {
  1522. $customer[] = $v;
  1523. if (isset($employee_customer[$v['employee_id']])) {
  1524. $employee_customer[$v['employee_id']][] = $v;
  1525. } else {
  1526. $employee_customer[$v['employee_id']] = [];
  1527. $employee_customer[$v['employee_id']][] = $v;
  1528. }
  1529. }
  1530. }
  1531. $db_where[] = ['Customer.org_id', '=', $org_id];
  1532. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  1533. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  1534. $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
  1535. $log_list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date,employee_id')
  1536. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  1537. ->where($db_where)
  1538. ->order('confirm_date asc')
  1539. ->select()
  1540. ->toArray();
  1541. // 每个员工的跟进记录
  1542. $employee_log = [];
  1543. foreach ($log_list as $k => $v) {
  1544. if (isset($customer_employee_id[$v['customer_id']])) {
  1545. $employee_log[$customer_employee_id[$v['customer_id']]][] = $v;
  1546. } else {
  1547. $employee_log[$v['employee_id']][] = $v;
  1548. }
  1549. }
  1550. foreach($employee_list as $key=>$val){
  1551. $cusdata = [];
  1552. if (isset($employee_customer[$val['id']])) {
  1553. $cusdata = $employee_customer[$val['id']];
  1554. }
  1555. $list = [];
  1556. if (isset($employee_log[$val['id']])) {
  1557. $list = $employee_log[$val['id']];
  1558. }
  1559. $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
  1560. $meatime =[];
  1561. $ylids = [];
  1562. foreach($list as $k => $v){
  1563. if(!empty($v['measure_room_img_type'])){
  1564. $allmea = explode(',', $v['measure_room_img_type']);
  1565. foreach($allmea as $kk => $vv){
  1566. if(isset($vv) && strstr($vv,'量房图片')) $mesimg++;
  1567. if(isset($vv) && strstr($vv,'量房报告图片')) $demand++;
  1568. if(isset($vv) && strstr($vv,'客户需求图片')) $report++;
  1569. }
  1570. }
  1571. if(!in_array($v['customer_id'], array_keys($meatime))) $meatime[$v['customer_id']] = $v['confirm_date'];
  1572. if(!in_array($v['customer_id'], $ylids)) $ylids[] = $v['customer_id'];
  1573. }
  1574. $ylcus = Customer::where('id','in',$ylids)->field('id,sign_time')->select()->toArray();
  1575. $agvtime = [];
  1576. foreach($ylcus as $k => $v){
  1577. if(!empty($meatime[$v['id']])) $agvtime[] = (strtotime($meatime[$v['id']]) - strtotime($v['sign_time'])) > 0 ? strtotime($meatime[$v['id']]) - strtotime($v['sign_time']) : 0;
  1578. }
  1579. $meahounum = count($list);
  1580. $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
  1581. $mearate = !empty($cusdata) && !empty($meatime) ? round(count($meatime)/count($cusdata),2) : 0;
  1582. $employee_list[$key]['employee_count'] = count($employee_list);
  1583. $employee_list[$key]['report'] = $report;
  1584. $employee_list[$key]['demand'] = $demand;
  1585. $employee_list[$key]['mesimg'] = $mesimg;
  1586. $employee_list[$key]['meahounum'] = $meahounum;
  1587. $employee_list[$key]['agvcycle'] = $agvcycle;
  1588. $employee_list[$key]['mearate'] = $mearate;
  1589. }
  1590. if (!empty($param['order'])) {
  1591. $order_arr = explode(' ', $param['order']);
  1592. $sort_field = $order_arr[0] ?? 'meahounum';
  1593. $sort_order = $order_arr[1] ?? 'desc';
  1594. switch ($sort_field){
  1595. case 'meahounum':
  1596. $field = 'meahounum';
  1597. break;
  1598. case 'agvcycle':
  1599. $field = 'agvcycle';
  1600. break;
  1601. case 'mearate':
  1602. $field = 'mearate';
  1603. break;
  1604. default:
  1605. $field = 'notFound';
  1606. break;
  1607. }
  1608. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  1609. $arr = [];
  1610. foreach ($employee_list as $v){
  1611. if (!isset($v[$field])){
  1612. break;
  1613. }
  1614. $arr[] = $v[$field];
  1615. }
  1616. if (!empty($arr)){
  1617. array_multisort($arr, $sort, $employee_list);
  1618. }
  1619. }
  1620. $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
  1621. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
  1622. }
  1623. /**
  1624. * 量房员工客户数据
  1625. */
  1626. public function measure_house_empcus()
  1627. {
  1628. $token = $this->request->token;
  1629. $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]);
  1630. $start_time_get = input('start_time', time(), 'strtotime');
  1631. $end_time_get = input('end_time', time(), 'strtotime');
  1632. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  1633. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  1634. $employee_id = input('employee_id', '', 'intval');
  1635. $sub_org = orgSubIds($token['org_id']);
  1636. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  1637. if (!in_array($employee_id, $sub_employee_ids)){
  1638. return json(['code'=> self::success, 'data'=> ['empdata'=> [], 'list'=> []], 'msg'=> 'success']);
  1639. }
  1640. $lf_state = CustomerVisitLog::changeState('已量房', 'chaos');
  1641. $wuxiao = Customer::changeState('无效', 'chaos');
  1642. $db_where[] = ['Customer.employee_id', '=', $employee_id];
  1643. $db_where[] = ['Customer.state', 'NOT NULL', null];
  1644. $db_where[] = ['Customer.state', 'not in', $wuxiao];
  1645. $db_where[] = ['Customer.died', '<>', 2];
  1646. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  1647. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  1648. $db_where[] = ['CustomerVisitLog.state', 'in', $lf_state];
  1649. $list = Db::view('CustomerVisitLog', 'id,customer_id,img,measure_room_img_type,addtime,confirm_date')
  1650. ->view('Customer', [], 'Customer.id=CustomerVisitLog.customer_id')
  1651. ->where($db_where)
  1652. ->order('confirm_date asc')
  1653. ->select()
  1654. ->toArray();
  1655. $cusids = array_unique(array_column($list,'customer_id'));
  1656. $cuslist = Customer::with(['employee'=>function($query){
  1657. $query->field('id,name');
  1658. }])->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();
  1659. $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
  1660. if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
  1661. // 状态统计获取
  1662. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  1663. $customersState = [];
  1664. foreach ($customersStateList as $s) {
  1665. if (!isset($customersState[$s['customer_id']]))
  1666. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  1667. $customersState[$s['customer_id']]['count'] += $s['num'];
  1668. // 到店,量房,活动,定金,签单
  1669. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  1670. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  1671. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  1672. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  1673. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  1674. }
  1675. foreach($cuslist as $key=>$val){
  1676. $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  1677. if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
  1678. }
  1679. $report = $demand = $mesimg = $meahounum = $agvcycle = $mearate = 0;
  1680. $meatime = [];
  1681. $ylids = [];
  1682. foreach ($list as $k => $v) {
  1683. if (!empty($v['measure_room_img_type'])) {
  1684. $allmea = explode(',', $v['measure_room_img_type']);
  1685. foreach ($allmea as $kk => $vv) {
  1686. if (isset($vv) && strstr($vv, '量房图片')) $mesimg++;
  1687. if (isset($vv) && strstr($vv, '量房报告图片')) $demand++;
  1688. if (isset($vv) && strstr($vv, '客户需求图片')) $report++;
  1689. }
  1690. }
  1691. if (!in_array($v['customer_id'], array_keys($meatime))) $meatime[$v['customer_id']] = $v['confirm_date'];
  1692. if (!in_array($v['customer_id'], $ylids)) $ylids[] = $v['customer_id'];
  1693. }
  1694. $wuxiao = Customer::changeState('无效', 'chaos');
  1695. $not_sure = Customer::changeState('待确认', 'chaos');
  1696. $wuxiao_not_sure = array_merge($wuxiao, $not_sure);
  1697. $all_where[] = ['employee_id', '=', $employee_id];
  1698. $all_where[] = ['state', 'NOT NULL', null];
  1699. $all_where[] = ['state', 'NOT IN', $wuxiao_not_sure];
  1700. $all_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  1701. $all_where[] = ['died', '<>', 2];
  1702. $cusdata = Customer::where($all_where)->count();
  1703. $ylcus = Customer::where('id', 'in', $ylids)->field('id,sign_time')->select()->toArray();
  1704. $agvtime = [];
  1705. foreach ($ylcus as $k => $v) {
  1706. if (!empty($meatime[$v['id']])) $agvtime[] = (strtotime($meatime[$v['id']]) - strtotime($v['sign_time'])) > 0 ? strtotime($meatime[$v['id']]) - strtotime($v['sign_time']) : 0;
  1707. }
  1708. $meahounum = count($list);
  1709. $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime) / count($agvtime)) / 86400) : 0;
  1710. $mearate = $cusdata && !empty($meatime) ? round(count($meatime) / $cusdata, 2) : 0;
  1711. $empdata = ['empname'=> $employeedata['name'], 'employee_count'=> 1, 'report'=> $report, 'demand'=> $demand, 'mesimg'=> $mesimg, 'meahounum'=> $meahounum, 'agvcycle'=> $agvcycle, 'mearate'=> $mearate];
  1712. $data = ['empdata'=> $empdata, 'list'=> $cuslist];
  1713. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  1714. }
  1715. /**
  1716. * 转单首页统计
  1717. */
  1718. public function sign_index()
  1719. {
  1720. $token = $this->request->token;
  1721. $param = $this->request->only(['org_id', 'start_time','end_time']);
  1722. //员工数量
  1723. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  1724. $org_name = Org::where('id',$org_id)->value('name');
  1725. $sub_org = orgSubIds($org_id);
  1726. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  1727. //$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();
  1728. $cusdata = Customer::where([['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray();
  1729. //$where[] = ['org_id','in',$sub_org];
  1730. $where[] = ['employee_id','in',$sub_employee_ids];
  1731. $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
  1732. $where[] = ['confirm_date', 'between', [$param['start_time'] . ' 00:00:00', $param['end_time'] . ' 23:59:59']];
  1733. $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
  1734. $signids = array_unique(array_column($list,'customer_id'));
  1735. //$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();
  1736. $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id');
  1737. $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id');
  1738. $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
  1739. $signnum = count($list);
  1740. $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
  1741. $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0;
  1742. $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
  1743. $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0;
  1744. $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0;
  1745. $data = ['org_name'=>$org_name,'shoprate'=>$shoprate,'depositrate'=>$depositrate,'signmoney'=>$signmoney,'signnum'=>$signnum,'signagv'=>$signagv,'signrate'=>$signrate];
  1746. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  1747. }
  1748. /**
  1749. * 转单部门数据
  1750. */
  1751. public function sign_org()
  1752. {
  1753. $token = $this->request->token;
  1754. $param = $this->request->only(['org_id', 'start_time','end_time','order']);
  1755. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  1756. // 所选部门
  1757. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  1758. foreach ($sub_org as $k => $v){
  1759. // 是否有子部门
  1760. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  1761. if ($have_sub_org > 0){
  1762. $sub_org[$k]['have_sub_org'] = true;
  1763. } else {
  1764. $sub_org[$k]['have_sub_org'] = false;
  1765. }
  1766. unset($where);
  1767. unset($meatime);
  1768. unset($ylids);
  1769. unset($agvtime);
  1770. $v_sub_org = orgSubIds($v['id']);
  1771. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  1772. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  1773. //$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();
  1774. $cusdata = Customer::where([['employee_id','in',$org_employee_ids]])->field('id,addtime')->select()->toArray();
  1775. //$where[] = ['org_id','in',$v_sub_org];
  1776. $where[] = ['employee_id','in',$org_employee_ids];
  1777. $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
  1778. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  1779. $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
  1780. $signids = array_unique(array_column($list,'customer_id'));
  1781. //$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();
  1782. $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id');
  1783. $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id');
  1784. $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
  1785. $signnum = count($list);
  1786. $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
  1787. $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0;
  1788. $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
  1789. $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0;
  1790. $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0;
  1791. $sub_org[$k]['shoprate'] = $shoprate;
  1792. $sub_org[$k]['depositrate'] = $depositrate;
  1793. $sub_org[$k]['signmoney'] = $signmoney;
  1794. $sub_org[$k]['signnum'] = $signnum;
  1795. $sub_org[$k]['signagv'] = $signagv;
  1796. $sub_org[$k]['signrate'] = $signrate;
  1797. }
  1798. if (!empty($param['order'])) {
  1799. $order_arr = explode(' ', $param['order']);
  1800. $sort_field = $order_arr[0] ?? 'signnum';
  1801. $sort_order = $order_arr[1] ?? 'desc';
  1802. switch ($sort_field){
  1803. case 'signnum':
  1804. $field = 'signnum';
  1805. break;
  1806. case 'signmoney':
  1807. $field = 'signmoney';
  1808. break;
  1809. default:
  1810. $field = 'notFound';
  1811. break;
  1812. }
  1813. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  1814. $arr = [];
  1815. foreach ($sub_org as $v){
  1816. if (!isset($v[$field])){
  1817. break;
  1818. }
  1819. $arr[] = $v[$field];
  1820. }
  1821. if (!empty($arr)){
  1822. array_multisort($arr, $sort, $sub_org);
  1823. }
  1824. }
  1825. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
  1826. }
  1827. /**
  1828. * 转单员工数据
  1829. */
  1830. public function sign_employee()
  1831. {
  1832. $token = $this->request->token;
  1833. $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
  1834. // 所选部门
  1835. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  1836. // 所选部门员工统计数据
  1837. $e_where[] = ['org_id', '=', $org_id];
  1838. $e_where[] = ['state', '=', '在职'];
  1839. $e_where[] = ['uid', '<>', 0];
  1840. $employee_list = Employee::with(['user'=> function($query){
  1841. $query->field('id,headimgurl');
  1842. }])->where($e_where)->field('id,name,org_id,root_id,uid')->page($param['page'],$param['limit'])->select()->toArray();
  1843. foreach($employee_list as $key=>$val){
  1844. unset($where);
  1845. //$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();
  1846. $cusdata = Customer::where([['employee_id','=',$val['id']]])->field('id,addtime')->select()->toArray();
  1847. $where[] = ['employee_id','=',$val['id']];
  1848. //$where[] = ['customer_id','in',$cusids];
  1849. $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
  1850. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  1851. $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
  1852. $signids = array_unique(array_column($list,'customer_id'));
  1853. //$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();
  1854. $shopids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已到店','chaos')]])->group('customer_id')->column('customer_id');
  1855. $depids = CustomerVisitLog::where([['customer_id','in',$signids],['state','in',CustomerVisitLog::changeState('已交定','chaos')]])->group('customer_id')->column('customer_id');
  1856. $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
  1857. $signnum = count($list);
  1858. $signrate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
  1859. $signagv = !empty($list) ? round(array_sum(array_column($list,'money'))/count($list),2) : 0;
  1860. $signmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
  1861. $depositrate = !empty($list) && !empty($depids) ? round(count($depids)/count($list),2)*100 : 0;
  1862. $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids)/count($list),2)*100 : 0;
  1863. $employee_list[$key]['employee_count'] = count($employee_list);
  1864. $employee_list[$key]['shoprate'] = $shoprate;
  1865. $employee_list[$key]['depositrate'] = $depositrate;
  1866. $employee_list[$key]['signmoney'] = $signmoney;
  1867. $employee_list[$key]['signnum'] = $signnum;
  1868. $employee_list[$key]['signagv'] = $signagv;
  1869. $employee_list[$key]['signrate'] = $signrate;
  1870. }
  1871. if (!empty($param['order'])) {
  1872. $order_arr = explode(' ', $param['order']);
  1873. $sort_field = $order_arr[0] ?? 'signnum';
  1874. $sort_order = $order_arr[1] ?? 'desc';
  1875. switch ($sort_field){
  1876. case 'signnum':
  1877. $field = 'signnum';
  1878. break;
  1879. case 'signmoney':
  1880. $field = 'signmoney';
  1881. break;
  1882. default:
  1883. $field = 'notFound';
  1884. break;
  1885. }
  1886. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  1887. $arr = [];
  1888. foreach ($employee_list as $v){
  1889. if (!isset($v[$field])){
  1890. break;
  1891. }
  1892. $arr[] = $v[$field];
  1893. }
  1894. if (!empty($arr)){
  1895. array_multisort($arr, $sort, $employee_list);
  1896. }
  1897. }
  1898. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
  1899. }
  1900. /**
  1901. * 签单客户数据
  1902. */
  1903. public function sign_empcus()
  1904. {
  1905. $token = $this->request->token;
  1906. $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]);
  1907. $where[] = ['employee_id','=',$param['employee_id']];
  1908. $where[] = ['state','in',CustomerVisitLog::changeState('已签单','chaos')];
  1909. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  1910. $list = CustomerVisitLog::where($where)->field('id,customer_id,addtime,money')->order('addtime asc')->select()->toArray();
  1911. $cusids = array_unique(array_column($list,'customer_id'));
  1912. $cuslist = Customer::with(['employee'=>function($query){
  1913. $query->field('id,name');
  1914. }])->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();
  1915. $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
  1916. if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
  1917. // 状态统计获取
  1918. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  1919. $customersState = [];
  1920. foreach ($customersStateList as $s) {
  1921. if (!isset($customersState[$s['customer_id']]))
  1922. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  1923. $customersState[$s['customer_id']]['count'] += $s['num'];
  1924. // 到店,量房,活动,定金,签单
  1925. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  1926. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  1927. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  1928. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  1929. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  1930. }
  1931. foreach($cuslist as $key=>$val){
  1932. $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  1933. if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
  1934. }
  1935. $cusdata = Customer::where([['employee_id','=',$param['employee_id']]])->field('id,addtime')->select()->toArray();
  1936. $shopids = CustomerVisitLog::where([['customer_id', 'in', $cusids], ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]])->group('customer_id')->column('customer_id');
  1937. $depids = CustomerVisitLog::where([['customer_id', 'in', $cusids], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->group('customer_id')->column('customer_id');
  1938. $shoprate = $depositrate = $signmoney = $signnum = $signagv = $signrate = 0;
  1939. $signnum = count($list);
  1940. $signrate = !empty($cusdata) && !empty($list) ? round(count($list) / count($cusdata), 2) * 100 : 0;
  1941. $signagv = !empty($list) ? round(array_sum(array_column($list, 'money')) / count($list), 2) : 0;
  1942. $signmoney = !empty($list) ? array_sum(array_column($list, 'money')) : 0;
  1943. $depositrate = !empty($list) && !empty($depids) ? round(count($depids) / count($list), 2) * 100 : 0;
  1944. $shoprate = !empty($list) && !empty($shopids) ? round(count($shopids) / count($list), 2) * 100 : 0;
  1945. $empdata = ['empname'=>$employeedata['name'],'employee_count'=>1,'shoprate'=>$shoprate,'depositrate'=>$depositrate,'signmoney'=>$signmoney,'signnum'=>$signnum,'signagv'=>$signagv,'signrate'=>$signrate];
  1946. $data = ['empdata'=>$empdata,'list'=>$cuslist];
  1947. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  1948. }
  1949. /**
  1950. * 交定(签单)统计首页
  1951. */
  1952. public function deposit_index()
  1953. {
  1954. $token = $this->request->token;
  1955. $param = $this->request->only(['org_id', 'start_time','end_time']);
  1956. //员工数量
  1957. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  1958. $org_name = Org::where('id',$org_id)->value('name');
  1959. $sub_org = orgSubIds($org_id);
  1960. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  1961. //$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();
  1962. $cusdata = Customer::where([['employee_id','in',$sub_employee_ids]])->field('id,addtime')->select()->toArray();
  1963. //$where[] = ['org_id','in',$sub_org];
  1964. $where[] = ['employee_id','in',$sub_employee_ids];
  1965. $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
  1966. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  1967. $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
  1968. $depids = array_unique(array_column($list,'customer_id'));
  1969. $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;
  1970. $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
  1971. foreach($list as $key=>$val){
  1972. if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date'];
  1973. if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id'];
  1974. }
  1975. $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
  1976. $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray();
  1977. $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();
  1978. foreach($yddvlg as $key=>$val){
  1979. $yddarr[$val['customer_id']][]=$val;
  1980. }
  1981. foreach($yddarr as $key=>$val){
  1982. if(empty($val)) continue;
  1983. if(count($val)==1){
  1984. $oneshop_depnum++;
  1985. $oneshop_ids[] = $key;
  1986. }
  1987. if(count($val)==2){
  1988. $twoshop_depnum++;
  1989. $twoshop_ids[] = $key;
  1990. }
  1991. if(count($val)>=3){
  1992. $threeshop_depnum++;
  1993. $threeshop_ids[] = $key;
  1994. }
  1995. }
  1996. foreach($yjdcus as $key=>$val){
  1997. if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  1998. if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  1999. if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2000. if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2001. if(!empty($val['package_id'])){
  2002. $allpid = explode(',',$val['package_id']);
  2003. foreach($allpid as $k=>$v){
  2004. $alljdpid[]=$v;
  2005. }
  2006. }
  2007. }
  2008. foreach($package as $key=>$val){
  2009. $package[$key]['num'] = 0;
  2010. foreach($alljdpid as $k=>$v){
  2011. if($val['id']==$v){
  2012. $package[$key]['num']++;
  2013. }
  2014. }
  2015. }
  2016. $depnum = count($list);
  2017. $deprate = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
  2018. $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
  2019. $depmoney = !empty($list) ? array_sum(array_column($list,'money')) : 0;
  2020. //$deppacnum = count($alljdpid);
  2021. $oneshop_depnum = $oneshop_depnum;
  2022. $twoshop_depnum = $twoshop_depnum;
  2023. $threeshop_depnum = $threeshop_depnum;
  2024. $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0;
  2025. $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0;
  2026. $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0;
  2027. $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0;
  2028. $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0;
  2029. $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0;
  2030. $package_depnum = !empty($package) ? array_sum(array_column($package,'num')) : 0;
  2031. $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];
  2032. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  2033. }
  2034. /**
  2035. * 签订部门数据
  2036. */
  2037. public function deposit_org()
  2038. {
  2039. $token = $this->request->token;
  2040. $param = $this->request->only(['org_id', 'start_time','end_time','order']);
  2041. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  2042. // 所选部门
  2043. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  2044. foreach ($sub_org as $k => $v){
  2045. // 是否有子部门
  2046. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  2047. if ($have_sub_org > 0){
  2048. $sub_org[$k]['have_sub_org'] = true;
  2049. } else {
  2050. $sub_org[$k]['have_sub_org'] = false;
  2051. }
  2052. unset($where);
  2053. $v_sub_org = orgSubIds($v['id']);
  2054. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2055. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  2056. $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();
  2057. //$where[] = ['org_id','in',$v_sub_org];
  2058. $where[] = ['employee_id','in',$org_employee_ids];
  2059. $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
  2060. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  2061. $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
  2062. $depids = array_unique(array_column($list,'customer_id'));
  2063. $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;
  2064. $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
  2065. foreach($list as $key=>$val){
  2066. if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date'];
  2067. if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id'];
  2068. }
  2069. $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
  2070. $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray();
  2071. $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();
  2072. foreach($yddvlg as $key=>$val){
  2073. $yddarr[$val['customer_id']][]=$val;
  2074. }
  2075. foreach($yddarr as $key=>$val){
  2076. if(empty($val)) continue;
  2077. if(count($val)==1){
  2078. $oneshop_depnum++;
  2079. $oneshop_ids[] = $key;
  2080. }
  2081. if(count($val)==2){
  2082. $twoshop_depnum++;
  2083. $twoshop_ids[] = $key;
  2084. }
  2085. if(count($val)>=3){
  2086. $threeshop_depnum++;
  2087. $threeshop_ids[] = $key;
  2088. }
  2089. }
  2090. foreach($yjdcus as $key=>$val){
  2091. if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2092. if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2093. if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2094. if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2095. if(!empty($val['package_id'])){
  2096. $allpid = explode(',',$val['package_id']);
  2097. foreach($allpid as $m=>$p){
  2098. $alljdpid[]=$p;
  2099. }
  2100. }
  2101. }
  2102. foreach($package as $key=>$val){
  2103. $package[$key]['num'] = 0;
  2104. foreach($alljdpid as $m=>$p){
  2105. //if(in_array($val['id'],$alljdpid)){
  2106. if($val['id']==$p){
  2107. $package[$key]['num']++;
  2108. }
  2109. }
  2110. }
  2111. $sub_org[$k]['depnum'] = count($list);
  2112. $sub_org[$k]['deprate'] = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
  2113. $sub_org[$k]['agvcycle'] = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
  2114. $sub_org[$k]['depmoney'] = !empty($list) ? array_sum(array_column($list,'money')) : 0;
  2115. $sub_org[$k]['deppacnum'] = count($alljdpid);
  2116. $sub_org[$k]['oneshop_depnum'] = $oneshop_depnum;
  2117. $sub_org[$k]['twoshop_depnum'] = $twoshop_depnum;
  2118. $sub_org[$k]['threeshop_depnum'] = $threeshop_depnum;
  2119. $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0;
  2120. $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0;
  2121. $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0;
  2122. $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0;
  2123. $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0;
  2124. $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0;
  2125. $sub_org[$k]['oneshop_deprate'] = round($oneshop_deprate,2);
  2126. $sub_org[$k]['twoshop_deprate'] = round($twoshop_deprate,2);
  2127. $sub_org[$k]['threeshop_deprate'] = round($threeshop_deprate,2);
  2128. $sub_org[$k]['one_agvcycle'] = $one_agvcycle;
  2129. $sub_org[$k]['two_agvcycle'] = $two_agvcycle;
  2130. $sub_org[$k]['three_agvcycle'] = $three_agvcycle;
  2131. $sub_org[$k]['package'] = $package;
  2132. $sub_org[$k]['package_depnum'] = !empty($package) ? array_sum(array_column($package,'num')) : 0;
  2133. }
  2134. if (!empty($param['order'])) {
  2135. $order_arr = explode(' ', $param['order']);
  2136. $sort_field = $order_arr[0] ?? 'depnum';
  2137. $sort_order = $order_arr[1] ?? 'desc';
  2138. switch ($sort_field){
  2139. case 'depnum':
  2140. $field = 'depnum';
  2141. break;
  2142. case 'package_depnum':
  2143. $field = 'package_depnum';
  2144. break;
  2145. case 'depmoney':
  2146. $field = 'depmoney';
  2147. break;
  2148. default:
  2149. $field = 'notFound';
  2150. break;
  2151. }
  2152. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  2153. $arr = [];
  2154. foreach ($sub_org as $v){
  2155. if (!isset($v[$field])){
  2156. break;
  2157. }
  2158. $arr[] = $v[$field];
  2159. }
  2160. if (!empty($arr)){
  2161. array_multisort($arr, $sort, $sub_org);
  2162. }
  2163. }
  2164. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
  2165. }
  2166. /**
  2167. * 签订员工数据
  2168. */
  2169. public function deposit_employee()
  2170. {
  2171. $token = $this->request->token;
  2172. $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
  2173. // 所选部门
  2174. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  2175. // 所选部门员工统计数据
  2176. $e_where[] = ['org_id', '=', $org_id];
  2177. $e_where[] = ['state', '=', '在职'];
  2178. $e_where[] = ['uid', '<>', 0];
  2179. $employee_list = Employee::with(['user'=> function($query){
  2180. $query->field('id,headimgurl');
  2181. }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
  2182. foreach($employee_list as $k=>$v){
  2183. unset($where);
  2184. $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();
  2185. $where[] = ['employee_id','=',$v['id']];
  2186. //$where[] = ['customer_id','in',$cusids];
  2187. $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
  2188. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  2189. $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
  2190. $signids = array_unique(array_column($list,'customer_id'));
  2191. $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;
  2192. $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
  2193. foreach($list as $key=>$val){
  2194. if(!in_array($val['customer_id'],array_keys($deptime))) $deptime[$val['customer_id']]=$val['confirm_date'];
  2195. if(!in_array($val['customer_id'],$yjdids)) $yjdids[]=$val['customer_id'];
  2196. }
  2197. $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
  2198. $yjdcus = Customer::where('id','in',$yjdids)->field('id,sign_time,package_id')->select()->toArray();
  2199. $yddvlg = CustomerVisitLog::where([['customer_id','in',$yjdids],['state','in',CustomerVisitLog::changeState('已到店','chaos')],['employee_id','=',$v['id']]])->field('id,addtime,customer_id,state')->select()->toArray();
  2200. foreach($yddvlg as $key=>$val){
  2201. $yddarr[$val['customer_id']][]=$val;
  2202. }
  2203. foreach($yddarr as $key=>$val){
  2204. if(empty($val)) continue;
  2205. if(count($val)==1){
  2206. $oneshop_depnum++;
  2207. $oneshop_ids[] = $key;
  2208. }
  2209. if(count($val)==2){
  2210. $twoshop_depnum++;
  2211. $twoshop_ids[] = $key;
  2212. }
  2213. if(count($val)>=3){
  2214. $threeshop_depnum++;
  2215. $threeshop_ids[] = $key;
  2216. }
  2217. }
  2218. foreach($yjdcus as $key=>$val){
  2219. if(!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2220. if(!empty($deptime[$val['id']]) && in_array($val['id'],$oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2221. if(!empty($deptime[$val['id']]) && in_array($val['id'],$twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2222. if(!empty($deptime[$val['id']]) && in_array($val['id'],$threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2223. if(!empty($val['package_id'])){
  2224. $allpid = explode(',',$val['package_id']);
  2225. foreach($allpid as $m=>$p){
  2226. $alljdpid[]=$p;
  2227. }
  2228. }
  2229. }
  2230. foreach($package as $key=>$val){
  2231. $package[$key]['num'] = 0;
  2232. foreach($alljdpid as $m=>$p){
  2233. //if(in_array($val['id'],$alljdpid)){
  2234. if($val['id']==$p){
  2235. $package[$key]['num']++;
  2236. }
  2237. }
  2238. }
  2239. $employee_list[$k]['depnum'] = count($list);
  2240. $employee_list[$k]['deprate'] = !empty($cusdata) && !empty($list) ? round(count($list)/count($cusdata),2)*100 : 0;
  2241. $employee_list[$k]['agvcycle'] = !empty($agvtime) ? ceil((array_sum($agvtime)/count($agvtime))/86400) : 0;
  2242. $employee_list[$k]['depmoney'] = !empty($list) ? array_sum(array_column($list,'money')) : 0;
  2243. $employee_list[$k]['deppacnum'] = count($alljdpid);
  2244. $employee_list[$k]['oneshop_depnum'] = $oneshop_depnum;
  2245. $employee_list[$k]['twoshop_depnum'] = $twoshop_depnum;
  2246. $employee_list[$k]['threeshop_depnum'] = $threeshop_depnum;
  2247. $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? round($oneshop_depnum/count($cusdata),2)*100 : 0;
  2248. $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? round($twoshop_depnum/count($cusdata),2)*100 : 0;
  2249. $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? round($threeshop_depnum/count($cusdata),2)*100 : 0;
  2250. $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime)/count($one_agvtime))/86400) : 0;
  2251. $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime)/count($two_agvtime))/86400) : 0;
  2252. $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime)/count($three_agvtime))/86400) : 0;
  2253. $employee_list[$k]['oneshop_deprate'] = round($oneshop_deprate,2);
  2254. $employee_list[$k]['twoshop_deprate'] = round($twoshop_deprate,2);
  2255. $employee_list[$k]['threeshop_deprate'] = round($threeshop_deprate,2);
  2256. $employee_list[$k]['one_agvcycle'] = round($one_agvcycle,2);
  2257. $employee_list[$k]['two_agvcycle'] = round($two_agvcycle,2);
  2258. $employee_list[$k]['three_agvcycle'] = round($three_agvcycle,2);
  2259. $employee_list[$k]['package'] = $package;
  2260. $employee_list[$k]['package_depnum'] = !empty($package) ? array_sum(array_column($package,'num')) : 0;
  2261. }
  2262. if (!empty($param['order'])) {
  2263. $order_arr = explode(' ', $param['order']);
  2264. $sort_field = $order_arr[0] ?? 'depnum';
  2265. $sort_order = $order_arr[1] ?? 'desc';
  2266. switch ($sort_field){
  2267. case 'depnum':
  2268. $field = 'depnum';
  2269. break;
  2270. case 'package_depnum':
  2271. $field = 'package_depnum';
  2272. break;
  2273. case 'depmoney':
  2274. $field = 'depmoney';
  2275. break;
  2276. default:
  2277. $field = 'notFound';
  2278. break;
  2279. }
  2280. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  2281. $arr = [];
  2282. foreach ($employee_list as $v){
  2283. if (!isset($v[$field])){
  2284. break;
  2285. }
  2286. $arr[] = $v[$field];
  2287. }
  2288. if (!empty($arr)){
  2289. array_multisort($arr, $sort, $employee_list);
  2290. }
  2291. }
  2292. $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
  2293. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
  2294. }
  2295. /**
  2296. * 签订员工客户列表
  2297. */
  2298. public function depsignmea_empcus()
  2299. {
  2300. $token = $this->request->token;
  2301. $param = $this->request->only(['employee_id', 'start_time','end_time','status','page'=>1,'limit'=>10]);
  2302. //if(empty($param['state']) || !in_array($param['state'],['已量房','已交定','已签单'])) return json(['code'=> 1, 'msg'=> 'state参数错误']);
  2303. $where[] = ['employee_id','=',$param['employee_id']];
  2304. $where[] = ['state','in',CustomerVisitLog::changeState('已交定','chaos')];
  2305. $where[] = ['confirm_date','between',[$param['start_time'].' 00:00:00',$param['end_time'].' 23:59:59']];
  2306. $list = CustomerVisitLog::where($where)->field('id,customer_id,confirm_date,money')->order('confirm_date asc')->select()->toArray();
  2307. $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;
  2308. $youjdpid = $yjdids = $deptime = $agvtime = $one_agvtime = $two_agvtime = $three_agvtime = $alljdpid = $yddarr = $oneshop_ids = $twoshop_ids = $threeshop_ids = [];
  2309. foreach ($list as $key => $val) {
  2310. if (!in_array($val['customer_id'], array_keys($deptime))) $deptime[$val['customer_id']] = $val['confirm_date'];
  2311. if (!in_array($val['customer_id'], $yjdids)) $yjdids[] = $val['customer_id'];
  2312. }
  2313. $package = CustomerPackage::where([['root_id', '=', $this->request->token['root_org']]])->field('id,name')->select()->toArray();
  2314. $yjdcus = Customer::where('id', 'in', $yjdids)->field('id,sign_time,package_id')->select()->toArray();
  2315. $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();
  2316. foreach ($yddvlg as $key => $val) {
  2317. $yddarr[$val['customer_id']][] = $val;
  2318. }
  2319. foreach ($yddarr as $key => $val) {
  2320. if (empty($val)) continue;
  2321. if (count($val) == 1) {
  2322. $oneshop_depnum++;
  2323. $oneshop_ids[] = $key;
  2324. }
  2325. if (count($val) == 2) {
  2326. $twoshop_depnum++;
  2327. $twoshop_ids[] = $key;
  2328. }
  2329. if (count($val) >= 3) {
  2330. $threeshop_depnum++;
  2331. $threeshop_ids[] = $key;
  2332. }
  2333. }
  2334. foreach ($yjdcus as $key => $val) {
  2335. if (!empty($deptime[$val['id']])) $agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2336. if (!empty($deptime[$val['id']]) && in_array($val['id'], $oneshop_ids)) $one_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2337. if (!empty($deptime[$val['id']]) && in_array($val['id'], $twoshop_ids)) $two_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2338. if (!empty($deptime[$val['id']]) && in_array($val['id'], $threeshop_ids)) $three_agvtime[] = strtotime($deptime[$val['id']]) - strtotime($val['sign_time']);
  2339. if (!empty($val['package_id'])) {
  2340. $allpid = explode(',', $val['package_id']);
  2341. foreach ($allpid as $m => $p) {
  2342. $alljdpid[] = $p;
  2343. }
  2344. $youjdpid[] = $val['id'];
  2345. }
  2346. }
  2347. foreach ($package as $key => $val) {
  2348. $package[$key]['num'] = 0;
  2349. foreach ($alljdpid as $m => $p) {
  2350. //if(in_array($val['id'],$alljdpid)){
  2351. if ($val['id'] == $p) {
  2352. $package[$key]['num']++;
  2353. }
  2354. }
  2355. }
  2356. $cusids = !empty($list) ? array_unique(array_column($list,'customer_id')) : [];
  2357. if(!empty($param['status'])){
  2358. if($param['status']==1) $cusids = $oneshop_ids;
  2359. if($param['status']==2) $cusids = $twoshop_ids;
  2360. if($param['status']==3) $cusids = $threeshop_ids;
  2361. if($param['status']==4) $cusids = $youjdpid;
  2362. }
  2363. $cuslist = Customer::with(['employee'=>function($query){
  2364. $query->field('id,name');
  2365. }])->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();
  2366. $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();
  2367. $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
  2368. if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
  2369. // 状态统计获取
  2370. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $cusids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  2371. $customersState = [];
  2372. foreach ($customersStateList as $s) {
  2373. if (!isset($customersState[$s['customer_id']]))
  2374. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  2375. $customersState[$s['customer_id']]['count'] += $s['num'];
  2376. // 到店,量房,活动,定金,签单
  2377. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  2378. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  2379. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  2380. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  2381. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  2382. }
  2383. foreach($cuslist as $key=>$val){
  2384. $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  2385. if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
  2386. }
  2387. $depnum = count($list);
  2388. $deprate = !empty($cusdata) && !empty($list) ? sprintf("%.2f",count($list) / count($cusdata)) * 100 : 0;
  2389. $agvcycle = !empty($agvtime) ? ceil((array_sum($agvtime) / count($agvtime)) / 86400) : 0;
  2390. $depmoney = !empty($list) ? array_sum(array_column($list, 'money')) : 0;
  2391. $deppacnum = count($alljdpid);
  2392. $oneshop_depnum = $oneshop_depnum;
  2393. $twoshop_depnum = $twoshop_depnum;
  2394. $threeshop_depnum = $threeshop_depnum;
  2395. $oneshop_deprate = !empty($oneshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$oneshop_depnum / count($cusdata)) * 100 : 0;
  2396. $twoshop_deprate = !empty($twoshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$twoshop_depnum / count($cusdata)) * 100 : 0;
  2397. $threeshop_deprate = !empty($threeshop_depnum) && !empty($cusdata) ? sprintf("%.2f",$threeshop_depnum / count($cusdata)) * 100 : 0;
  2398. $one_agvcycle = !empty($one_agvtime) ? ceil((array_sum($one_agvtime) / count($one_agvtime)) / 86400) : 0;
  2399. $two_agvcycle = !empty($two_agvtime) ? ceil((array_sum($two_agvtime) / count($two_agvtime)) / 86400) : 0;
  2400. $three_agvcycle = !empty($three_agvtime) ? ceil((array_sum($three_agvtime) / count($three_agvtime)) / 86400) : 0;
  2401. $package = $package;
  2402. $package_depnum = !empty($package) ? array_sum(array_column($package, 'num')) : 0;
  2403. $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];
  2404. $data = ['empdata'=>$empdata,'list'=>$cuslist];
  2405. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  2406. }
  2407. /**
  2408. * 打电话统计(全部)
  2409. */
  2410. public function call(){
  2411. $token = $this->request->token;
  2412. $start_time_get = input('start_time', time(), 'strtotime');
  2413. $end_time_get = input('end_time', time(), 'strtotime');
  2414. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2415. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2416. $root_sub = orgSubIds($token['root_org']);
  2417. // 所选部门
  2418. $org_id = input('org_id', $token['org_id'], 'intval');
  2419. if (!in_array($org_id, $root_sub)){
  2420. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  2421. }
  2422. // 数据总览
  2423. // 部门信息
  2424. $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
  2425. //员工数量
  2426. $sub_org = orgSubIds($org_id);
  2427. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2428. $data['employee_count'] = count($sub_employee_ids);
  2429. $where[] = ['employee_id', 'in', $sub_employee_ids];
  2430. $where[] = ['addtime', 'between', [$start_time, $end_time]];
  2431. $list = OutCallLog::where($where)->column('id,status,billsec');
  2432. $count = count($list);
  2433. $data['count'] = $count;
  2434. $ok = 0; // 接通数量
  2435. $long = 0; // 通话时长 s
  2436. foreach ($list as $k => $v) {
  2437. if ($v['status'] == 1){
  2438. $ok ++;
  2439. }
  2440. $long += $v['billsec'];
  2441. }
  2442. $data['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : $ok * 100;
  2443. $data['long'] = round($long / 60, 2);
  2444. $data['average_long'] = $count > 0 ? round($long / 60 / $count, 2) : round($long / 60, 2);
  2445. // 手机打电话次数
  2446. $mbCount = OutCallMbLog::where($where)->count();
  2447. $data['count'] += $mbCount;
  2448. return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
  2449. }
  2450. /**
  2451. * 打电话统计(部门)
  2452. */
  2453. public function callOrg(){
  2454. $token = $this->request->token;
  2455. $start_time_get = input('start_time', time(), 'strtotime');
  2456. $end_time_get = input('end_time', time(), 'strtotime');
  2457. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2458. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2459. $root_sub = orgSubIds($token['root_org']);
  2460. // 所选部门
  2461. $org_id = input('org_id', $token['org_id'], 'intval');
  2462. if (!in_array($org_id, $root_sub)){
  2463. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  2464. }
  2465. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->column('id,name,pid');
  2466. foreach ($sub_org as $k => $v){
  2467. // 是否有子部门
  2468. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  2469. if ($have_sub_org > 0){
  2470. $sub_org[$k]['have_sub_org'] = true;
  2471. } else {
  2472. $sub_org[$k]['have_sub_org'] = false;
  2473. }
  2474. $v_sub_org = orgSubIds($v['id']);
  2475. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2476. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  2477. unset($where);
  2478. $where[] = ['employee_id', 'in', $org_employee_ids];
  2479. $where[] = ['addtime', 'between', [$start_time, $end_time]];
  2480. $list = OutCallLog::where($where)->column('id,status,billsec');
  2481. $count = count($list);
  2482. $sub_org[$k]['count'] = $count;
  2483. $ok = 0; // 接通数量
  2484. $long = 0; // 通话时长 s
  2485. foreach ($list as $kk => $vv) {
  2486. if ($vv['status'] == 1){
  2487. $ok ++;
  2488. }
  2489. $long += $vv['billsec'];
  2490. }
  2491. $sub_org[$k]['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : 0;
  2492. $m_long = round($long / 60, 2);
  2493. $sub_org[$k]['long'] = $m_long;
  2494. $sub_org[$k]['average_long'] = $count > 0 ? round($long / 60 / $count, 2) : $m_long;
  2495. // 手机打电话次数
  2496. $mbCount = OutCallMbLog::where($where)->count();
  2497. $sub_org[$k]['count'] += $mbCount;
  2498. }
  2499. $order = input('order', '', 'trim');
  2500. if (!empty($order)) {
  2501. $order_arr = explode(' ', $order);
  2502. $sort_field = $order_arr[0] ?? 'count';
  2503. $sort_order = $order_arr[1] ?? 'desc';
  2504. switch ($sort_field){
  2505. case 'count':
  2506. $field = 'count';
  2507. break;
  2508. default:
  2509. $field = 'notFound';
  2510. break;
  2511. }
  2512. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  2513. $arr = [];
  2514. foreach ($sub_org as $v){
  2515. if (!isset($v[$field])){
  2516. break;
  2517. }
  2518. $arr[] = $v[$field];
  2519. }
  2520. if (!empty($arr)){
  2521. array_multisort($arr, $sort, $sub_org);
  2522. }
  2523. }
  2524. return json(['code'=> self::success, 'data'=> $sub_org, 'msg'=> '请求成功']);
  2525. }
  2526. /**
  2527. * 打电话统计(员工)
  2528. */
  2529. public function callEmployee(){
  2530. $token = $this->request->token;
  2531. $start_time_get = input('start_time', time(), 'strtotime');
  2532. $end_time_get = input('end_time', time(), 'strtotime');
  2533. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2534. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2535. $root_sub = orgSubIds($token['root_org']);
  2536. // 所选部门
  2537. $org_id = input('org_id', $token['org_id'], 'intval');
  2538. if (!in_array($org_id, $root_sub)){
  2539. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  2540. }
  2541. // 所选部门员工统计数据
  2542. $e_where[] = ['org_id', '=', $org_id];
  2543. $e_where[] = ['state', '=', '在职'];
  2544. $e_where[] = ['uid', '<>', 0];
  2545. $employee_list = Employee::with(['user'=> function($query){
  2546. $query->field('id,headimgurl');
  2547. }])->where($e_where)->column('id,name,org_id,root_id,uid');
  2548. $employee_ids = array_column($employee_list, 'id');
  2549. $where[] = ['employee_id', 'in', $employee_ids];
  2550. $where[] = ['addtime', 'between', [$start_time, $end_time]];
  2551. $list = OutCallLog::where($where)->column('id,employee_id,status,billsec');
  2552. $employee_data = [];
  2553. foreach ($list as $k => $v) {
  2554. if (isset($employee_data[$v['employee_id']])) {
  2555. $employee_data[$v['employee_id']][] = $v;
  2556. } else {
  2557. $employee_data[$v['employee_id']] = [];
  2558. $employee_data[$v['employee_id']][] = $v;
  2559. }
  2560. }
  2561. // 手机打电话次数
  2562. $mbCount = OutCallMbLog::where($where)->column('id,employee_id');
  2563. $mbCount_data = [];
  2564. foreach ($mbCount as $k => $v) {
  2565. if (isset($mbCount_data[$v['employee_id']])) {
  2566. $mbCount_data[$v['employee_id']][] = $v;
  2567. } else {
  2568. $mbCount_data[$v['employee_id']] = [];
  2569. $mbCount_data[$v['employee_id']][] = $v;
  2570. }
  2571. }
  2572. foreach ($employee_list as $k => $v){
  2573. $count = 0;
  2574. $ok = 0; // 接通数量
  2575. $long = 0; // 通话时长 s
  2576. if (isset($employee_data[$v['id']])) {
  2577. $count = count($employee_data[$v['id']]);
  2578. foreach ($employee_data[$v['id']] as $kk => $vv) {
  2579. if ($vv['status'] == 1){
  2580. $ok ++;
  2581. }
  2582. $long += $vv['billsec'];
  2583. }
  2584. }
  2585. $employee_list[$k]['lv'] = $count > 0 ? round($ok * 100 / $count, 2) : 0;
  2586. $m_long = round($long / 60, 2);
  2587. $employee_list[$k]['long'] = $m_long;
  2588. $employee_list[$k]['average_long'] = $count > 0 ? round($m_long / $count, 2) : $m_long;
  2589. $cmb_count = 0;
  2590. if (isset($mbCount_data[$v['id']])) {
  2591. $cmb_count = count($mbCount_data[$v['id']]);
  2592. }
  2593. $employee_list[$k]['count'] = $count + $cmb_count;
  2594. }
  2595. $order = input('order', '', 'trim');
  2596. if (!empty($order)) {
  2597. $order_arr = explode(' ', $order);
  2598. $sort_field = $order_arr[0] ?? 'count';
  2599. $sort_order = $order_arr[1] ?? 'desc';
  2600. switch ($sort_field){
  2601. case 'count':
  2602. $field = 'count';
  2603. break;
  2604. default:
  2605. $field = 'notFound';
  2606. break;
  2607. }
  2608. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  2609. $arr = [];
  2610. foreach ($employee_list as $v){
  2611. if (!isset($v[$field])){
  2612. break;
  2613. }
  2614. $arr[] = $v[$field];
  2615. }
  2616. if (!empty($arr)){
  2617. array_multisort($arr, $sort, $employee_list);
  2618. }
  2619. }
  2620. $page = input('page', 1, 'intval');
  2621. $limit = input('limit', 10, 'intval');
  2622. $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit);
  2623. return json(['code'=> self::success, 'data'=> $employee_list, 'msg'=> '请求成功']);
  2624. }
  2625. /**
  2626. * 打电话统计(客户列表)
  2627. */
  2628. public function callCustomer(){
  2629. $token = $this->request->token;
  2630. $start_time_get = input('start_time', time(), 'strtotime');
  2631. $end_time_get = input('end_time', time(), 'strtotime');
  2632. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2633. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2634. $employee_id = input('employee_id', '', 'intval');
  2635. $sub_org = orgSubIds($token['org_id']);
  2636. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2637. if (!in_array($employee_id, $sub_employee_ids)){
  2638. return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
  2639. }
  2640. $where[] = ['employee_id', '=', $employee_id];
  2641. $where[] = ['addtime', 'between', [$start_time, $end_time]];
  2642. $list = OutCallLog::where($where)->select()->toArray();
  2643. $customer_id = array_column($list, 'customer_id');
  2644. $employee_data['count'] = count($list);
  2645. $ok = 0; // 接通数量
  2646. $long = 0; // 通话时长 s
  2647. foreach ($list as $kk => $vv) {
  2648. if ($vv['status'] == 1){
  2649. $ok ++;
  2650. }
  2651. $long += $vv['billsec'];
  2652. }
  2653. $employee_data['lv'] = count($list) > 0 ? round($ok * 100 / count($list), 2) : 0;
  2654. $m_long = round($long / 60, 2);
  2655. $employee_data['long'] = $m_long;
  2656. $employee_data['average_long'] = count($list) > 0 ? round($m_long / count($list), 2) : $m_long;
  2657. $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name');
  2658. // 手机打电话次数
  2659. $mbCount = OutCallMbLog::where($where)->count();
  2660. $employee_data['count'] += $mbCount;
  2661. $data['employee'] = $employee_data;
  2662. $page = input('page', 1, 'intval');
  2663. $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select();
  2664. $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();
  2665. // 状态统计获取
  2666. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  2667. $customersState = [];
  2668. foreach ($customersStateList as $s) {
  2669. if (!isset($customersState[$s['customer_id']]))
  2670. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  2671. $customersState[$s['customer_id']]['count'] += $s['num'];
  2672. // 到店,量房,活动,定金,签单
  2673. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  2674. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  2675. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  2676. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  2677. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  2678. }
  2679. foreach ($customers as $k => $v) {
  2680. $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  2681. }
  2682. $data['customer'] = $customers;
  2683. return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
  2684. }
  2685. /**
  2686. * 活动列表
  2687. */
  2688. public function activity(){
  2689. $token = request()->token;
  2690. $list = Activity::where([['root_id', '=', $token['root_org']], ['cate', '=', 1]])->order('addtime desc')->field('id,title')->select()->toArray();
  2691. return json(['code'=> self::success, 'data'=> $list]);
  2692. }
  2693. /**
  2694. * 预约活动(主页)
  2695. */
  2696. public function subscribeActivity(){
  2697. $token = $this->request->token;
  2698. $id = input('id', '', 'intval');
  2699. $start_time_get = input('start_time', time(), 'strtotime');
  2700. $end_time_get = input('end_time', time(), 'strtotime');
  2701. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2702. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2703. $root_sub = orgSubIds($token['root_org']);
  2704. // 所选部门
  2705. $org_id = input('org_id', $token['org_id'], 'intval');
  2706. if (!in_array($org_id, $root_sub)){
  2707. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  2708. }
  2709. // 数据总览
  2710. // 部门信息
  2711. $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
  2712. //员工数量
  2713. $sub_org = orgSubIds($org_id);
  2714. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2715. $data['employee_count'] = count($sub_employee_ids);
  2716. $sub_where[] = ['employee_id', 'in', $sub_employee_ids];
  2717. $sub_where[] = ['aid', '=', $id];
  2718. $sub_where[] = ['type', '=', 2];
  2719. $sub_where[] = ['state', 'in', [0,1]];
  2720. $sub_where[] = ['subscribe_date', '>=', $start_time];
  2721. $sub_where[] = ['subscribe_date', '<', $end_time];
  2722. $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
  2723. $sub_count = count($sub_customer);
  2724. $data['subscribe_count'] = $sub_count;
  2725. $dc_where[] = ['customer_employee_id', 'in', $sub_employee_ids];
  2726. $dc_where[] = ['aid', '=', $id];
  2727. $dc_where[] = ['confirm_date', '>=', $start_time];
  2728. $dc_where[] = ['confirm_date', '<', $end_time];
  2729. $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  2730. $dc_where[] = ['state', 'in', $dc_state];
  2731. $daochang = CustomerVisitLog::where($dc_where)->count();
  2732. $data['daochang_count'] = $daochang;
  2733. $data['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ;
  2734. $jd_where[] = ['customer_id', 'in', $sub_customer];
  2735. $jd_where[] = ['confirm_date', '>=', $start_time];
  2736. $jd_where[] = ['confirm_date', '<', $end_time];
  2737. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  2738. $jd_where[] = ['state', 'in', $jd_state];
  2739. $jiaoding = CustomerVisitLog::where($jd_where)->count();
  2740. $data['jiaoding_count'] = $jiaoding;
  2741. $data['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
  2742. $zd_where[] = ['customer_id', 'in', $sub_customer];
  2743. $zd_where[] = ['confirm_date', '>=', $start_time];
  2744. $zd_where[] = ['confirm_date', '<', $end_time];
  2745. $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  2746. $zd_where[] = ['state', 'in', $zd_state];
  2747. $zhuandan = CustomerVisitLog::where($zd_where)->count();
  2748. $data['zhuandan_count'] = $zhuandan;
  2749. $data['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
  2750. return json(['code'=> self::success, 'data'=> $data]);
  2751. }
  2752. /**
  2753. * 预约活动(部门)
  2754. */
  2755. public function subscribeActivityOrg(){
  2756. $token = $this->request->token;
  2757. $id = input('id', '', 'intval');
  2758. $start_time_get = input('start_time', time(), 'strtotime');
  2759. $end_time_get = input('end_time', time(), 'strtotime');
  2760. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2761. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2762. $root_sub = orgSubIds($token['root_org']);
  2763. // 所选部门
  2764. $org_id = input('org_id', $token['org_id'], 'intval');
  2765. if (!in_array($org_id, $root_sub)){
  2766. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  2767. }
  2768. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  2769. foreach ($sub_org as $k => $v) {
  2770. // 是否有子部门
  2771. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  2772. if ($have_sub_org > 0) {
  2773. $sub_org[$k]['have_sub_org'] = true;
  2774. } else {
  2775. $sub_org[$k]['have_sub_org'] = false;
  2776. }
  2777. $v_sub_org = orgSubIds($v['id']);
  2778. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2779. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  2780. unset($sub_where);
  2781. $sub_where[] = ['employee_id', 'in', $org_employee_ids];
  2782. $sub_where[] = ['aid', '=', $id];
  2783. $sub_where[] = ['type', '=', 2];
  2784. $sub_where[] = ['state', 'in', [0,1]];
  2785. $sub_where[] = ['subscribe_date', '>=', $start_time];
  2786. $sub_where[] = ['subscribe_date', '<', $end_time];
  2787. $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
  2788. $sub_count = count($sub_customer);
  2789. $sub_org[$k]['subscribe_count'] = $sub_count;
  2790. unset($dc_where);
  2791. $dc_where[] = ['customer_employee_id', 'in', $org_employee_ids];
  2792. $dc_where[] = ['aid', '=', $id];
  2793. $dc_where[] = ['confirm_date', '>=', $end_time];
  2794. $dc_where[] = ['confirm_date', '<', $end_time];
  2795. $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  2796. $dc_where[] = ['state', 'in', $dc_state];
  2797. $daochang = CustomerVisitLog::where($dc_where)->count();
  2798. $sub_org[$k]['daochang_count'] = $daochang;
  2799. $sub_org[$k]['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ;
  2800. unset($jd_where);
  2801. $jd_where[] = ['customer_id', 'in', $sub_customer];
  2802. $jd_where[] = ['confirm_date', '>=', $start_time];
  2803. $jd_where[] = ['confirm_date', '<', $end_time];
  2804. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  2805. $jd_where[] = ['state', 'in', $jd_state];
  2806. $jiaoding = CustomerVisitLog::where($jd_where)->count();
  2807. $sub_org[$k]['jiaoding_count'] = $jiaoding;
  2808. $sub_org[$k]['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
  2809. unset($zd_where);
  2810. $zd_where[] = ['customer_id', 'in', $sub_customer];
  2811. $zd_where[] = ['confirm_date', '>=', $start_time];
  2812. $zd_where[] = ['confirm_date', '<', $end_time];
  2813. $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  2814. $zd_where[] = ['state', 'in', $zd_state];
  2815. $zhuandan = CustomerVisitLog::where($zd_where)->count();
  2816. $sub_org[$k]['zhuandan_count'] = $zhuandan;
  2817. $sub_org[$k]['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
  2818. }
  2819. $order = input('order', '', 'trim');
  2820. if ($order) {
  2821. $order_arr = explode(' ', $order);
  2822. $sort_field = $order_arr[0] ?? 'yuyue';
  2823. $sort_order = $order_arr[1] ?? 'desc';
  2824. switch ($sort_field){
  2825. case 'yuyue':
  2826. $field = 'subscribe_count';
  2827. break;
  2828. case 'daochang':
  2829. $field = 'daochang_count';
  2830. break;
  2831. case 'qiandan':
  2832. $field = 'jiaoding_count';
  2833. break;
  2834. case 'zhuandan':
  2835. $field = 'zhuandan_count';
  2836. break;
  2837. default:
  2838. $field = 'notFound';
  2839. break;
  2840. }
  2841. if ($sort_order == 'asc'){
  2842. $sort = SORT_ASC;
  2843. } else {
  2844. $sort = SORT_DESC;
  2845. }
  2846. $arr = [];
  2847. foreach ($sub_org as $v){
  2848. if (!isset($v[$field])){
  2849. break;
  2850. }
  2851. $arr[] = $v[$field];
  2852. }
  2853. if (!empty($arr)){
  2854. array_multisort($arr, $sort, $sub_org);
  2855. }
  2856. }
  2857. $page = input('page', 1, 'intval');
  2858. $limit = input('limit', 10, 'intval');
  2859. $list = array_slice($sub_org, ($page-1)*$limit, $page*$limit-1);
  2860. return json(['code'=> self::success, 'data'=> $list]);
  2861. }
  2862. /**
  2863. * 预约活动(员工)
  2864. */
  2865. public function subscribeActivityEmployee(){
  2866. $token = $this->request->token;
  2867. $id = input('id', '', 'intval');
  2868. $start_time_get = input('start_time', time(), 'strtotime');
  2869. $end_time_get = input('end_time', time(), 'strtotime');
  2870. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2871. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2872. $root_sub = orgSubIds($token['root_org']);
  2873. // 所选部门
  2874. $org_id = input('org_id', $token['org_id'], 'intval');
  2875. if (!in_array($org_id, $root_sub)){
  2876. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  2877. }
  2878. // 所选部门员工统计数据
  2879. $e_where[] = ['org_id', '=', $org_id];
  2880. $e_where[] = ['state', '=', '在职'];
  2881. $e_where[] = ['uid', '<>', 0];
  2882. $employee_list = Employee::with(['user'=> function($query){
  2883. $query->field('id,headimgurl');
  2884. }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
  2885. foreach ($employee_list as $k => $v) {
  2886. unset($sub_where);
  2887. $sub_where[] = ['employee_id', '=', $v['id']];
  2888. $sub_where[] = ['aid', '=', $id];
  2889. $sub_where[] = ['type', '=', 2];
  2890. $sub_where[] = ['state', 'in', [0,1]];
  2891. $sub_where[] = ['subscribe_date', '>=', $start_time];
  2892. $sub_where[] = ['subscribe_date', '<', $end_time];
  2893. $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
  2894. $sub_count = count($sub_customer);
  2895. $employee_list[$k]['subscribe_count'] = $sub_count;
  2896. unset($dc_where);
  2897. $dc_where[] = ['customer_employee_id', '=', $v['id']];
  2898. $dc_where[] = ['aid', '=', $id];
  2899. $dc_where[] = ['confirm_date', '>=', $start_time];
  2900. $dc_where[] = ['confirm_date', '<', $end_time];
  2901. $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  2902. $dc_where[] = ['state', 'in', $dc_state];
  2903. $daochang = CustomerVisitLog::where($dc_where)->count();
  2904. $employee_list[$k]['daochang_count'] = $daochang;
  2905. $employee_list[$k]['daochang_lv'] = $sub_count > 0 ? round($daochang * 100 / $sub_count, 2) : 0 ;
  2906. unset($jd_where);
  2907. $jd_where[] = ['customer_id', 'in', $sub_customer];
  2908. $jd_where[] = ['customer_employee_id', '=', $v['id']];
  2909. $jd_where[] = ['confirm_date', '>=', $start_time];
  2910. $jd_where[] = ['confirm_date', '<', $end_time];
  2911. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  2912. $jd_where[] = ['state', 'in', $jd_state];
  2913. $jiaoding = CustomerVisitLog::where($jd_where)->count();
  2914. $employee_list[$k]['jiaoding_count'] = $jiaoding;
  2915. $employee_list[$k]['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
  2916. unset($zd_where);
  2917. $zd_where[] = ['customer_id', 'in', $sub_customer];
  2918. $zd_where[] = ['customer_employee_id', '=', $v['id']];
  2919. $zd_where[] = ['confirm_date', '>=', $start_time];
  2920. $zd_where[] = ['confirm_date', '<', $end_time];
  2921. $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  2922. $zd_where[] = ['state', 'in', $zd_state];
  2923. $zhuandan = CustomerVisitLog::where($zd_where)->count();
  2924. $employee_list[$k]['zhuandan_count'] = $zhuandan;
  2925. $employee_list[$k]['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
  2926. }
  2927. $order = input('order', '', 'trim');
  2928. if ($order) {
  2929. $order_arr = explode(' ', $order);
  2930. $sort_field = $order_arr[0] ?? 'yuyue';
  2931. $sort_order = $order_arr[1] ?? 'desc';
  2932. switch ($sort_field){
  2933. case 'yuyue':
  2934. $field = 'subscribe_count';
  2935. break;
  2936. case 'daochang':
  2937. $field = 'daochang_count';
  2938. break;
  2939. case 'qiandan':
  2940. $field = 'jiaoding_count';
  2941. break;
  2942. case 'zhuandan':
  2943. $field = 'zhuandan_count';
  2944. break;
  2945. default:
  2946. $field = 'notFound';
  2947. break;
  2948. }
  2949. if ($sort_order == 'asc'){
  2950. $sort = SORT_ASC;
  2951. } else {
  2952. $sort = SORT_DESC;
  2953. }
  2954. $arr = [];
  2955. foreach ($employee_list as $v){
  2956. if (!isset($v[$field])){
  2957. break;
  2958. }
  2959. $arr[] = $v[$field];
  2960. }
  2961. if (!empty($arr)){
  2962. array_multisort($arr, $sort, $employee_list);
  2963. }
  2964. }
  2965. $page = input('page', 1, 'intval');
  2966. $limit = input('limit', 10, 'intval');
  2967. $list = array_slice($employee_list, ($page-1)*$limit, $limit);
  2968. return json(['code'=> self::success, 'data'=> $list]);
  2969. }
  2970. /**
  2971. * 预约活动(客户)
  2972. */
  2973. public function subscribeActivityCustomer(){
  2974. $token = $this->request->token;
  2975. $start_time_get = input('start_time', time(), 'strtotime');
  2976. $end_time_get = input('end_time', time(), 'strtotime');
  2977. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  2978. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  2979. $employee_id = input('employee_id', '', 'intval');
  2980. $sub_org = orgSubIds($token['org_id']);
  2981. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  2982. if (!in_array($employee_id, $sub_employee_ids)){
  2983. return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
  2984. }
  2985. $aid = input('id', '', 'intval');
  2986. $sub_where[] = ['employee_id', '=', $employee_id];
  2987. $sub_where[] = ['aid', '=', $aid];
  2988. $sub_where[] = ['type', '=', 2];
  2989. $sub_where[] = ['state', 'in', [0,1]];
  2990. $sub_where[] = ['subscribe_date', '>=', $start_time];
  2991. $sub_where[] = ['subscribe_date', '<', $end_time];
  2992. $sub_customer = CustomersSubscribe::where($sub_where)->group('customer_id')->column('customer_id');
  2993. $sub_count = count($sub_customer);
  2994. $employee_data['subscribe_count'] = $sub_count;
  2995. $dc_where[] = ['customer_employee_id', '=', $employee_id];
  2996. $dc_where[] = ['aid', '=', $aid];
  2997. $dc_where[] = ['confirm_date', '>=', $start_time];
  2998. $dc_where[] = ['confirm_date', '<', $end_time];
  2999. $dc_state = CustomerVisitLog::changeState('已到场', 'chaos');
  3000. $dc_where[] = ['state', 'in', $dc_state];
  3001. $daochang_ids = CustomerVisitLog::where($dc_where)->column('customer_id');
  3002. $employee_data['daochang_count'] = count($daochang_ids);
  3003. $employee_data['daochang_lv'] = $sub_count > 0 ? round(count($daochang_ids) * 100 / $sub_count, 2) : 0 ;
  3004. $jd_where[] = ['customer_id', 'in', $sub_customer];
  3005. $jd_where[] = ['customer_employee_id', '=', $employee_id];
  3006. $jd_where[] = ['confirm_date', '>=', $start_time];
  3007. $jd_where[] = ['confirm_date', '<', $end_time];
  3008. $jd_state = CustomerVisitLog::changeState('已交定', 'chaos');
  3009. $jd_where[] = ['state', 'in', $jd_state];
  3010. $jiaoding = CustomerVisitLog::where($jd_where)->count();
  3011. $employee_data['jiaoding_count'] = $jiaoding;
  3012. $employee_data['jiaoding_lv'] = $sub_count > 0 ? round($jiaoding * 100 / $sub_count, 2) : 0 ;
  3013. $zd_where[] = ['customer_id', 'in', $sub_customer];
  3014. $zd_where[] = ['customer_employee_id', '=', $employee_id];
  3015. $zd_where[] = ['confirm_date', '>=', $start_time];
  3016. $zd_where[] = ['confirm_date', '<', $end_time];
  3017. $zd_state = CustomerVisitLog::changeState('已签单', 'chaos');
  3018. $zd_where[] = ['state', 'in', $zd_state];
  3019. $zhuandan = CustomerVisitLog::where($zd_where)->count();
  3020. $employee_data['zhuandan_count'] = $zhuandan;
  3021. $employee_data['zhuandan_lv'] = $sub_count > 0 ? round($zhuandan * 100 / $sub_count, 2) : 0 ;
  3022. $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name');
  3023. /* $where[] = ['employee_id', '=', $employee_id];
  3024. $where[] = ['aid', '=', $aid];
  3025. $where[] = ['type', '=', 2];
  3026. $where[] = ['state', 'in', [0,1]];
  3027. $where[] = ['subscribe_date', '>=', $start_time];
  3028. $where[] = ['subscribe_date', '<', $end_time];
  3029. $customer_id = CustomersSubscribe::where($where)->column('customer_id'); */
  3030. // 直接到场的客户
  3031. $customer_id = array_values(array_unique(array_merge($daochang_ids, $sub_customer)));
  3032. $type = input('type', 'subscribe', 'trim');
  3033. switch ($type){
  3034. case 'subscribe':
  3035. break;
  3036. case 'daochang':
  3037. $state = CustomerVisitLog::changeState('已到场', 'chaos');
  3038. $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id');
  3039. break;
  3040. case 'qiandan':
  3041. $state = CustomerVisitLog::changeState('已交定', 'chaos');
  3042. $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id');
  3043. break;
  3044. case 'zhuandan':
  3045. $state = CustomerVisitLog::changeState('已签单', 'chaos');
  3046. $customer_id = CustomerVisitLog::where([['customer_id', 'in', $customer_id], ['state', 'in', $state], ['aid', '=', $aid]])->column('customer_id');
  3047. break;
  3048. default:
  3049. break;
  3050. }
  3051. $page = input('page', 1, 'intval');
  3052. $c_where[] = ['id', 'in', $customer_id];
  3053. $c_where[] = ['employee_id', '=', $employee_id];
  3054. $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where($c_where)->page($page, 10)->select();
  3055. $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();
  3056. // 状态统计获取
  3057. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  3058. $customersState = [];
  3059. foreach ($customersStateList as $s) {
  3060. if (!isset($customersState[$s['customer_id']]))
  3061. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3062. $customersState[$s['customer_id']]['count'] += $s['num'];
  3063. // 到店,量房,活动,定金,签单
  3064. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  3065. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  3066. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  3067. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  3068. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  3069. }
  3070. foreach ($customers as $k => $v) {
  3071. $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3072. }
  3073. $data['employee'] = $employee_data;
  3074. $data['customer'] = $customers;
  3075. return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
  3076. }
  3077. /**
  3078. * 客户分配数
  3079. */
  3080. public function customerDistribution()
  3081. {
  3082. $token = $this->request->token;
  3083. // $token['org_id'] = 43;
  3084. // $token['employee_id'] = 125;
  3085. // $token['root_org'] = 23;
  3086. $date = date('Y-m-d');
  3087. $param = Request::only(['start_time' => $date, 'end_time' => $date,'employee_id'=>0,'org_id'=>0]);
  3088. $param['start_time'] = date('Y-m-d',strtotime($param['start_time']));
  3089. $param['end_time'] = date('Y-m-d',strtotime($param['end_time']));
  3090. $where[] = ['sign_time', 'between', [$param['start_time'].' 00:00:00', $param['end_time'] . ' 23:59:59']];
  3091. $where[] = ['employee_id','>',0];
  3092. if ($param['employee_id']) {
  3093. $where[] = ['employee_id','=',$param['employee_id']];
  3094. }else{
  3095. $org_id = $param['org_id'] ?: $token['org_id'];
  3096. $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path');
  3097. if(empty($path)) return json(['code' => 0, 'data' => [],'title'=>[]]);
  3098. $org_ids = Org::where([['path','like',$path.'%']])->column('id');
  3099. $where[] = ['org_id','in',$org_ids];
  3100. }
  3101. $wuxiao = Customer::changeState('无效', 'chaos');
  3102. $not_sure = Customer::changeState('待确认', 'chaos');
  3103. $where[] = ['state', 'not in', array_merge($wuxiao, $not_sure)];
  3104. $where[] = ['died', '<>', 2];
  3105. //房屋状态
  3106. $pid = CustomerPortraitField::where([['keyname', '=', 'house_type'], ['root_id', '=', $token['root_org']]])->value('id');
  3107. $house_status = CustomerPortraitFieldSelect::where([['pid', '=', $pid]])->order('id asc')->column("name,id,concat('house_type','#',id) as keyname,0 as count");
  3108. $count = ['id' => 0, 'count' => 0, 'keyname' => 'count', 'name' => '有效客户数', 'key' => '客户'];
  3109. $existing_homes = ['id' => 0, 'count' => 0, 'keyname' => 'existing_homes', 'name' => '现房数量占比', 'key' => '现房'];
  3110. $forward_housing = ['id' => 0, 'count' => 0, 'keyname' => 'forward_housing', 'name' => '期房数量占比', 'key' => '期房'];
  3111. $ershoufang = ['id' => 0, 'count' => 0, 'keyname' => 'ershoufang', 'name' => '二手房数量占比', 'key' => '二手房'];
  3112. $customer = Customer::where($where)->column('house_type');
  3113. $count['count'] = count($customer);
  3114. foreach ($customer as $k => $v) {
  3115. switch ($v) {
  3116. case '现房':
  3117. $existing_homes['count'] ++;
  3118. break;
  3119. case '期房':
  3120. $forward_housing['count'] ++;
  3121. break;
  3122. case '二手房':
  3123. $ershoufang['count'] ++;
  3124. break;
  3125. default:
  3126. break;
  3127. }
  3128. }
  3129. foreach ($house_status as $k2 => $v2) {
  3130. $house_status[$k2]['name'] = $v2['name'] . '数量占比';
  3131. }
  3132. array_unshift($house_status, $count, $existing_homes, $forward_housing,$ershoufang);
  3133. //比例
  3134. if ($count['count']) {
  3135. foreach ($house_status as $k3 => $v3) {
  3136. $house_status[$k3]['grawth'] = round($v3['count'] / $count['count'] * 100, 2) . '%';
  3137. }
  3138. } else {
  3139. foreach ($house_status as $k3 => $v3) {
  3140. $house_status[$k3]['grawth'] = '0%';
  3141. }
  3142. }
  3143. $org_id = $param['org_id'] ?: $token['org_id'];
  3144. $house_status[0]['org_name'] = Org::where('id',$org_id)->value('name');
  3145. return json(['code' => 0,'title'=>$house_status[0], 'data' => array_slice($house_status,1,3)]);
  3146. }
  3147. /**
  3148. * 客户分配列表
  3149. */
  3150. public function customerDistributionList()
  3151. {
  3152. $token = $this->request->token;
  3153. // $token['org_id'] = 23;
  3154. // $token['employee_id'] = 0;
  3155. // $token['root_org'] = 23;
  3156. $date = date('Y-m-d');
  3157. $param = Request::only(['key' => '', 'page' => 1, 'limit' => 10, 'start_time' => $date, 'end_time' => $date,'employee_id'=>0,'org_id'=>0]);
  3158. $param['start_time'] = date('Y-m-d',strtotime($param['start_time']));
  3159. $param['end_time'] = date('Y-m-d',strtotime($param['end_time']));
  3160. // $arr = ['现房', '期房'];
  3161. $where[] = ['sign_time', 'between', [$param['start_time'].' 00:00:00', $param['end_time'] . ' 23:59:59']];
  3162. $where[] = ['employee_id','>',0];
  3163. if ($param['employee_id']) {
  3164. $where[] = ['employee_id','=',$param['employee_id']];
  3165. }else{
  3166. $org_id = $param['org_id'] ?: $token['org_id'];
  3167. $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path');
  3168. if(empty($path)) return json(['code' => 0, 'data' => [],'count'=>0]);
  3169. $org_ids = Org::where([['path','like',$path.'%']])->column('id');
  3170. $where[] = ['org_id','in',$org_ids];
  3171. }
  3172. if(empty($param['key'])) return json(['code' => 0, 'data' =>[], 'count' => []]);
  3173. $where[] = ['house_type', '=', $param['key']];
  3174. $wuxiao = Customer::changeState('无效', 'chaos');
  3175. $not_sure = Customer::changeState('待确认', 'chaos');
  3176. $where[] = ['state', 'not in', array_merge($wuxiao, $not_sure)];
  3177. $where[] = ['died', '<>', 2];
  3178. $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();
  3179. if($list) $list = $this->getCustomer($list);
  3180. $count = Customer::where($where)->count();
  3181. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  3182. }
  3183. /**
  3184. * 客户跟进数据
  3185. */
  3186. private function getCustomer($arr){
  3187. $customerIdList = array_column($arr,'id');
  3188. //统计
  3189. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customerIdList]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  3190. $customersState = [];
  3191. foreach ($customersStateList as $s) {
  3192. if (!isset($customersState[$s['customer_id']]))
  3193. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3194. $customersState[$s['customer_id']]['count'] += $s['num'];
  3195. // 到店,量房,活动,定金,签单
  3196. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  3197. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  3198. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  3199. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  3200. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  3201. }
  3202. foreach ($arr as $key => $item) {
  3203. $arr[$key]['stateNum'] = isset($customersState[$item['id']]) ? $customersState[$item['id']] : ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3204. }
  3205. return $arr;
  3206. }
  3207. /**
  3208. * 加微统计
  3209. */
  3210. public function weChatStatistics()
  3211. {
  3212. $token = $this->request->token;
  3213. $date = date('Y-m-d');
  3214. $param = Request::only(['start_time'=> $date, 'end_time'=> $date, 'employee_id'=> 0, 'org_id'=> 0, 'page'=> 0, 'limit'=> 10]);
  3215. $param['start_time'] = date('Y-m-d 00:00:00', strtotime($param['start_time']));
  3216. $param['end_time'] = date('Y-m-d 23:59:59', strtotime($param['end_time']));
  3217. $where[] = ['employee_id', '>', 0];
  3218. if ($param['employee_id']) {
  3219. $where[] = ['employee_id', '=', $param['employee_id']];
  3220. } else {
  3221. $org_id = $param['org_id'] ? : $token['org_id'];
  3222. $path = Org::where([['path','like',$token['root_org'].'-%'],['id','=',$org_id]])->value('path');
  3223. if(empty($path)) return json(['code' => 0, 'data' => []]);
  3224. $org_ids = Org::where([['path', 'like', $path.'%']])->column('id');
  3225. $where[] = ['org_id', 'in', $org_ids];
  3226. }
  3227. // 所有客户
  3228. $c_where = $where;
  3229. $wuxiao = Customer::changeState('无效', 'chaos');
  3230. $c_where[] = ['sign_time', 'between', [$param['start_time'], $param['end_time']]];
  3231. $c_where[] = ['state', 'NOT NULL', null];
  3232. $c_where[] = ['state', 'not in', $wuxiao];
  3233. $c_where[] = ['died', '<>', 2];
  3234. $customer = Customer::where($c_where)->where(function($query){
  3235. $not_sure = Customer::changeState('待确认', 'chaos');
  3236. $or1[] = ['crm_res_id', 'null', null];
  3237. $or2[] = ['crm_res_id', 'not null', null];
  3238. $or2[] = ['state', 'not in', $not_sure];
  3239. $query->whereOr([$or1, $or2]);
  3240. })->column('id,addtime,employee_id');
  3241. $org_customer = [];
  3242. $emp_customer = [];
  3243. foreach ($customer as $v2) {
  3244. @$org_customer[$v2['org_id']] += 1;
  3245. @$emp_customer[$v2['employee_id']] += 1;
  3246. }
  3247. //日期范围内所有加微客户
  3248. $where[] = ['add_wechat_time', 'between', [$param['start_time'], $param['end_time']]];
  3249. $wechat = Customer::where($where)->column('id,employee_id,org_id');
  3250. $org_customer_wechat = [];
  3251. $emp_customer_wechat = [];
  3252. foreach ($wechat as $v3) {
  3253. @$org_customer_wechat[$v3['org_id']]+=1;
  3254. @$emp_customer_wechat[$v3['employee_id']]+=1;
  3255. }
  3256. $title['img'] = '';
  3257. $title['count'] = count($customer);
  3258. $title['wechat_count'] = count($wechat);
  3259. $title['grawth'] = ($title['count'] == 0 || $title['wechat_count'] == 0) ? '0%' : round($title['wechat_count'] / $title['count']*100, 2).'%';
  3260. if ($param['employee_id']) {
  3261. $user = Employee::with(['user'=> function($query){
  3262. $query->field('id,headimgurl')->bind(['img'=> 'headimgurl']);
  3263. }])->where('id', $param['employee_id'])->field('uid')->find();
  3264. $title['img'] = $user->img;
  3265. }
  3266. if ($param['employee_id']) {
  3267. //客户列表 有分页 无排序
  3268. $list = array_slice($wechat, ($param['page'] - 1) * $param['limit'], $param['limit']);
  3269. $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();
  3270. if($list) $list = $this->getCustomer($list);
  3271. $count = $title['wechat_count'];
  3272. return json(['code' => 0, 'data' => $list, 'count' => $count,'title'=>$title]);
  3273. }else{
  3274. $sort = Request::only(['order'=>'desc','field'=>'count']);
  3275. $order = $sort['order']=='desc' ? SORT_DESC : SORT_ASC;
  3276. $field = $sort['field']=='count' ? 'wechat_count' : 'order';
  3277. //直属业务员,直属子部门列表 无分页 有排序
  3278. //直属业务员 列表
  3279. $emp = Employee::where([['org_id','=',$org_id],['uid','>',0],['state','=','在职']])->column('id,name,uid');
  3280. $users = User::where([['id','in',array_column($emp,'uid')]])->column('headimgurl','id');
  3281. foreach ($emp as $k5 => $v5) {
  3282. $emp[$k5]['count'] = isset($emp_customer[$v5['id']]) ? $emp_customer[$v5['id']] : 0;
  3283. $emp[$k5]['wechat_count'] = isset($emp_customer_wechat[$v5['id']]) ? $emp_customer_wechat[$v5['id']] : 0;
  3284. $emp[$k5]['order'] = ($emp[$k5]['count']==0 || $emp[$k5]['wechat_count']==0) ? 0 : round($emp[$k5]['wechat_count']/$emp[$k5]['count']*100,2);
  3285. $emp[$k5]['img'] = $users[$v5['uid']];
  3286. $emp[$k5]['grawth'] = $emp[$k5]['order'].'%';
  3287. }
  3288. if($emp) array_multisort(array_column($emp, $field), $order, $emp);
  3289. //部门列表
  3290. $info = Org::where('id', $org_id)->find();
  3291. //直属子部门
  3292. $orgs = Org::where([['pid', '=', $org_id]])->column('id,name,path');
  3293. //所有子部门
  3294. $s_orgs = Org::where([['path','like',$info->path.'%']])->column('id,pid,path');
  3295. $org_ids = [];
  3296. foreach ($orgs as $k6 => $v6) {
  3297. $orgs[$k6]['count'] = 0;
  3298. $orgs[$k6]['wechat_count'] = 0;
  3299. $orgs[$k6]['emp_count'] = 0;
  3300. $orgs[$k6]['child_org_ids'] = [];
  3301. foreach ($s_orgs as $v7) {
  3302. if(stripos($v7['path'],$v6['path']) === 0){
  3303. if(isset($org_customer[$v7['id']])) $orgs[$k6]['count']+=$org_customer[$v7['id']];
  3304. if(isset($org_customer_wechat[$v7['id']])) $orgs[$k6]['wechat_count']+=$org_customer_wechat[$v7['id']];
  3305. $orgs[$k6]['child_org_ids'][] = $v7['id'];
  3306. $org_ids[] = $v7['id'];
  3307. }
  3308. }
  3309. $orgs[$k6]['order'] = ($orgs[$k6]['count']==0 || $orgs[$k6]['wechat_count']==0) ? 0 : round($orgs[$k6]['wechat_count']/$orgs[$k6]['count']*100,2);
  3310. $orgs[$k6]['grawth'] = $orgs[$k6]['order'].'%';
  3311. }
  3312. if($orgs) array_multisort(array_column($orgs,$field),$order, $orgs);
  3313. //部门下员工数量
  3314. $emps = Employee::where([['org_id','in',$org_ids],['state','=','在职'],['uid','>',0]])->group('org_id')->column('count(id)','org_id');
  3315. foreach ($orgs as $k8 => $v8) {
  3316. foreach ($v8['child_org_ids'] as $v9) {
  3317. if(isset($emps[$v9])) $orgs[$k8]['emp_count']+=$emps[$v9];
  3318. }
  3319. }
  3320. $org_id = $param['org_id'] ?: $token['org_id'];
  3321. $title['org_name'] = Org::where('id',$org_id)->value('name');
  3322. return json(['code'=> 0, 'data'=> ['emp'=> $emp, 'org'=> $orgs, 'title'=> $title]]);
  3323. }
  3324. }
  3325. /**
  3326. * 沟通跟进次数
  3327. */
  3328. public function followUpTimes()
  3329. {
  3330. $token = $this->request->token;
  3331. $start_time_get = input('start_time', time(), 'strtotime');
  3332. $end_time_get = input('end_time', time(), 'strtotime');
  3333. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3334. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3335. $root_sub = orgSubIds($token['root_org']);
  3336. // 所选部门
  3337. $org_id = input('org_id', $token['org_id'], 'intval');
  3338. if (!in_array($org_id, $root_sub)){
  3339. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  3340. }
  3341. // 数据总览
  3342. // 部门信息
  3343. $data['org_name'] = Org::where('id', '=', $org_id)->value('name');
  3344. //员工数量
  3345. $sub_org = orgSubIds($org_id);
  3346. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  3347. $data['employee_count'] = count($sub_employee_ids);
  3348. // 电话沟通次数
  3349. $call_where[] = ['employee_id', 'in', $sub_employee_ids];
  3350. $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3351. $call_where[] = ['status', '=', 1];
  3352. //电话沟通
  3353. $call_count = 0;
  3354. $call_time = 0;
  3355. $call_avg = 0;
  3356. $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
  3357. if ($call_log) {
  3358. $call_count = count($call_log);
  3359. $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
  3360. $call_employee = count(array_unique(array_column($call_log,'employee_id')));
  3361. $call_avg = round(array_sum(array_column($call_log,'billsec')) / 60 / $call_employee, 2);
  3362. }
  3363. // 手机打电话次数
  3364. $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $sub_employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
  3365. $call_count = $call_count + $mbCallCount;
  3366. $data['call_count'] = $call_count;
  3367. $data['call_time'] = $call_time;
  3368. $data['call_avg'] = $call_avg;
  3369. // 内容调用次数
  3370. $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
  3371. $share_where[] = ['employee_id', 'in', $sub_employee_ids];
  3372. // 转发次数
  3373. $share_all = ShareLog::where($share_where)->select();
  3374. $share_count = $share_all->count();
  3375. $wechat_where[] = ['employee_id', 'in', $sub_employee_ids];
  3376. $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3377. //查询每日发圈转发次数
  3378. $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
  3379. $data['share_count'] = $k_share_count;
  3380. // 浏览时长
  3381. $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
  3382. $foot_where[] = ['employee_id', 'in', $sub_employee_ids];
  3383. $foot_where[] = ['pipe_type', 'in', $clue_type];
  3384. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3385. $foot_count = Footprints::where($foot_where)->count(); // 次数
  3386. $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
  3387. $data['visit_long'] = round($visit_long / 60, 2);
  3388. $data['visit_avg'] = $foot_count > 0 ? round($visit_long / $foot_count /60, 2) : round($visit_long / 60, 2);
  3389. //微信沟通
  3390. $visit_where[] = ['employee_id', 'in', $sub_employee_ids];
  3391. $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
  3392. $visit_where[] = ['type', '=', 3];
  3393. $wechat_visit_count = CustomerVisitLog::where($visit_where)->count();
  3394. $data['wechat_visit_count'] = $wechat_visit_count;
  3395. $all_customer_where[] = ['employee_id', 'in', $sub_employee_ids];
  3396. $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  3397. $customer_count = Customer::where($all_customer_where)->count();
  3398. $data['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
  3399. $data['all_count'] = $data['call_count'] + $k_share_count + $data['wechat_visit_count'];
  3400. return json(['code'=> self::success, 'data'=> $data]);
  3401. }
  3402. /**
  3403. * 沟通跟进次数 (部门)
  3404. */
  3405. public function followUpTimesOrg()
  3406. {
  3407. $token = $this->request->token;
  3408. $start_time_get = input('start_time', time(), 'strtotime');
  3409. $end_time_get = input('end_time', time(), 'strtotime');
  3410. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3411. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3412. $root_sub = orgSubIds($token['root_org']);
  3413. // 所选部门
  3414. $org_id = input('org_id', $token['org_id'], 'intval');
  3415. if (!in_array($org_id, $root_sub)){
  3416. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  3417. }
  3418. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  3419. foreach ($sub_org as $k => $v) {
  3420. // 是否有子部门
  3421. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  3422. if ($have_sub_org > 0) {
  3423. $sub_org[$k]['have_sub_org'] = true;
  3424. } else {
  3425. $sub_org[$k]['have_sub_org'] = false;
  3426. }
  3427. $v_sub_org = orgSubIds($v['id']);
  3428. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  3429. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  3430. // 电话沟通次数
  3431. unset($call_where);
  3432. $call_where[] = ['employee_id', 'in', $org_employee_ids];
  3433. $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3434. $call_where[] = ['status', '=', 1];
  3435. //电话沟通
  3436. $call_count = 0;
  3437. $call_time = 0;
  3438. $call_avg = 0;
  3439. $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
  3440. if ($call_log) {
  3441. $call_count = count($call_log);
  3442. $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
  3443. $call_employee = count(array_unique(array_column($call_log,'employee_id')));
  3444. $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2);
  3445. }
  3446. // 手机打电话次数
  3447. $mbCallCount = OutCallMbLog::where([['employee_id', 'in', $org_employee_ids], ['addtime', 'between', [$start_time, $end_time]]])->count();
  3448. $call_count = $call_count + $mbCallCount;
  3449. $sub_org[$k]['call_count'] = $call_count;
  3450. $sub_org[$k]['call_time'] = $call_time;
  3451. $sub_org[$k]['call_avg'] = $call_avg;
  3452. // 内容调用次数
  3453. unset($share_where);
  3454. $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
  3455. $share_where[] = ['employee_id', 'in', $org_employee_ids];
  3456. // 转发次数
  3457. $share_all = ShareLog::where($share_where)->select();
  3458. $share_count = $share_all->count();
  3459. unset($wechat_where);
  3460. $wechat_where[] = ['employee_id', 'in', $org_employee_ids];
  3461. $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3462. //查询每日发圈转发次数
  3463. $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
  3464. $sub_org[$k]['share_count'] = $k_share_count;
  3465. // 浏览时长
  3466. $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
  3467. unset($foot_where);
  3468. $foot_where[] = ['employee_id', 'in', $org_employee_ids];
  3469. $foot_where[] = ['pipe_type', 'in', $clue_type];
  3470. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3471. $foot_count = Footprints::where($foot_where)->count(); // 次数
  3472. $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
  3473. $sub_org[$k]['visit_long'] = round($visit_long / 60, 2);
  3474. $sub_org[$k]['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2);
  3475. //微信沟通
  3476. unset($visit_where);
  3477. $visit_where[] = ['employee_id', 'in', $org_employee_ids];
  3478. $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
  3479. $visit_where[] = ['type', '=', 3];
  3480. $wechat_visit_count = CustomerVisitLog::where($visit_where)->count();
  3481. $sub_org[$k]['wechat_visit_count'] = $wechat_visit_count;
  3482. unset($all_customer_where);
  3483. $all_customer_where[] = ['employee_id', 'in', $org_employee_ids];
  3484. $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  3485. $customer_count = Customer::where($all_customer_where)->count();
  3486. $sub_org[$k]['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
  3487. $sub_org[$k]['all_count'] = $call_count + $k_share_count + $wechat_visit_count;
  3488. }
  3489. $order = input('order', '', 'trim');
  3490. if ($order) {
  3491. $order_arr = explode(' ', $order);
  3492. $sort_field = $order_arr[0] ?? 'call';
  3493. $sort_order = $order_arr[1] ?? 'desc';
  3494. switch ($sort_field){
  3495. case 'call':
  3496. $field = 'call_count';
  3497. break;
  3498. case 'share':
  3499. $field = 'share_count';
  3500. break;
  3501. case 'wechat':
  3502. $field = 'wechat_visit_count';
  3503. break;
  3504. default:
  3505. $field = 'notFound';
  3506. break;
  3507. }
  3508. if ($sort_order == 'asc'){
  3509. $sort = SORT_ASC;
  3510. } else {
  3511. $sort = SORT_DESC;
  3512. }
  3513. $arr = [];
  3514. foreach ($sub_org as $v){
  3515. if (!isset($v[$field])){
  3516. break;
  3517. }
  3518. $arr[] = $v[$field];
  3519. }
  3520. if (!empty($arr)){
  3521. array_multisort($arr, $sort, $sub_org);
  3522. }
  3523. }
  3524. return json(['code'=> self::success, 'data'=> $sub_org]);
  3525. }
  3526. /**
  3527. * 沟通跟进次数(员工)
  3528. */
  3529. public function followUpTimesEmployee()
  3530. {
  3531. $token = $this->request->token;
  3532. $start_time_get = input('start_time', time(), 'strtotime');
  3533. $end_time_get = input('end_time', time(), 'strtotime');
  3534. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3535. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3536. $root_sub = orgSubIds($token['root_org']);
  3537. // 所选部门
  3538. $org_id = input('org_id', $token['org_id'], 'intval');
  3539. if (!in_array($org_id, $root_sub)){
  3540. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  3541. }
  3542. // 所选部门员工统计数据
  3543. $e_where[] = ['org_id', '=', $org_id];
  3544. $e_where[] = ['state', '=', '在职'];
  3545. $e_where[] = ['uid', '<>', 0];
  3546. $employee_list = Employee::with(['user'=> function($query){
  3547. $query->field('id,headimgurl');
  3548. }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
  3549. foreach ($employee_list as $k => $v) {
  3550. // 电话沟通次数
  3551. unset($call_where);
  3552. $call_where[] = ['employee_id', '=', $v['id']];
  3553. $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3554. $call_where[] = ['status', '=', 1];
  3555. //电话沟通
  3556. $call_count = 0;
  3557. $call_time = 0;
  3558. $call_avg = 0;
  3559. $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
  3560. if ($call_log) {
  3561. $call_count = count($call_log);
  3562. $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
  3563. $call_employee = count(array_unique(array_column($call_log,'employee_id')));
  3564. $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2);
  3565. }
  3566. // 手机打电话次数
  3567. $mbCallCount = OutCallMbLog::where([['employee_id', '=', $v['id']], ['addtime', 'between', [$start_time, $end_time]]])->count();
  3568. $call_count = $call_count + $mbCallCount;
  3569. $employee_list[$k]['call_count'] = $call_count;
  3570. $employee_list[$k]['call_time'] = $call_time;
  3571. $employee_list[$k]['call_avg'] = $call_avg;
  3572. // 内容调用次数
  3573. unset($share_where);
  3574. $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
  3575. $share_where[] = ['employee_id', '=', $v['id']];
  3576. // 转发次数
  3577. $share_all = ShareLog::where($share_where)->select();
  3578. $share_count = $share_all->count();
  3579. unset($wechat_where);
  3580. $wechat_where[] = ['employee_id', '=', $v['id']];
  3581. $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3582. //查询每日发圈转发次数
  3583. $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
  3584. $employee_list[$k]['share_count'] = $k_share_count;
  3585. // 浏览时长
  3586. $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
  3587. unset($foot_where);
  3588. $foot_where[] = ['employee_id', '=', $v['id']];
  3589. $foot_where[] = ['pipe_type', 'in', $clue_type];
  3590. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3591. $foot_count = Footprints::where($foot_where)->count(); // 次数
  3592. $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
  3593. $employee_list[$k]['visit_long'] = round($visit_long / 60, 2);
  3594. $employee_list[$k]['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2);
  3595. //微信沟通
  3596. unset($visit_where);
  3597. $visit_where[] = ['employee_id', '=', $v['id']];
  3598. $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
  3599. $visit_where[] = ['type', '=', 3];
  3600. $wechat_visit_count = CustomerVisitLog::where($visit_where)->count();
  3601. $employee_list[$k]['wechat_visit_count'] = $wechat_visit_count;
  3602. unset($all_customer_where);
  3603. $all_customer_where[] = ['employee_id', '=', $v['id']];
  3604. $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  3605. $customer_count = Customer::where($all_customer_where)->count();
  3606. $employee_list[$k]['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
  3607. $employee_list[$k]['all_count'] = $call_count + $k_share_count + $wechat_visit_count;
  3608. }
  3609. $order = input('order', '', 'trim');
  3610. if ($order) {
  3611. $order_arr = explode(' ', $order);
  3612. $sort_field = $order_arr[0] ?? 'call';
  3613. $sort_order = $order_arr[1] ?? 'desc';
  3614. switch ($sort_field){
  3615. case 'call':
  3616. $field = 'call_count';
  3617. break;
  3618. case 'share':
  3619. $field = 'share_count';
  3620. break;
  3621. case 'wechat':
  3622. $field = 'wechat_visit_count';
  3623. break;
  3624. default:
  3625. $field = 'notFound';
  3626. break;
  3627. }
  3628. if ($sort_order == 'asc'){
  3629. $sort = SORT_ASC;
  3630. } else {
  3631. $sort = SORT_DESC;
  3632. }
  3633. $arr = [];
  3634. foreach ($employee_list as $v){
  3635. if (!isset($v[$field])){
  3636. break;
  3637. }
  3638. $arr[] = $v[$field];
  3639. }
  3640. if (!empty($arr)){
  3641. array_multisort($arr, $sort, $employee_list);
  3642. }
  3643. }
  3644. $page = input('page', 1, 'intval');
  3645. $limit = input('limit', 10, 'intval');
  3646. $employee_list = array_slice($employee_list, ($page - 1) * $limit, $limit);
  3647. return json(['code'=> self::success, 'data'=> $employee_list]);
  3648. }
  3649. /**
  3650. * 沟通跟进次数 (客户)
  3651. */
  3652. public function followUpTimesCustomer(){
  3653. $token = $this->request->token;
  3654. $start_time_get = input('start_time', time(), 'strtotime');
  3655. $end_time_get = input('end_time', time(), 'strtotime');
  3656. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3657. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3658. $employee_id = input('employee_id', '', 'intval');
  3659. $sub_org = orgSubIds($token['org_id']);
  3660. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  3661. if (!in_array($employee_id, $sub_employee_ids)){
  3662. return json(['code'=> self::success, 'data'=> ['employee'=> [], 'customer'=> []], 'msg'=> 'success']);
  3663. }
  3664. // 电话沟通次数
  3665. $call_where[] = ['employee_id', '=', $employee_id];
  3666. $call_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3667. $call_where[] = ['status', '=', 1];
  3668. //电话沟通
  3669. $call_count = 0;
  3670. $call_time = 0;
  3671. $call_avg = 0;
  3672. $call_log = OutCallLog::where($call_where)->column('employee_id,customer_id,billsec');
  3673. if ($call_log) {
  3674. $call_count = count($call_log);
  3675. $call_time = round(array_sum(array_column($call_log,'billsec')) / 60, 2);
  3676. $call_employee = count(array_unique(array_column($call_log,'employee_id')));
  3677. $call_avg = round((array_sum(array_column($call_log,'billsec')) / 60 / $call_employee), 2);
  3678. }
  3679. // 手机打电话次数
  3680. $mbCallCount = OutCallMbLog::where([['employee_id', '=', $employee_id], ['addtime', 'between', [$start_time, $end_time]]])->count();
  3681. $call_count = $call_count + $mbCallCount;
  3682. $employee_data['call_count'] = $call_count;
  3683. $employee_data['call_time'] = $call_time;
  3684. $employee_data['call_avg'] = $call_avg;
  3685. $customer_call = array_column($call_log, 'customer_id'); // 打电话的客户id
  3686. // 内容调用次数
  3687. $share_where[] = ['share_time', 'between', [$start_time, $end_time]];
  3688. $share_where[] = ['employee_id', '=', $employee_id];
  3689. // 转发次数
  3690. $share_all = ShareLog::where($share_where)->select();
  3691. $share_count = $share_all->count();
  3692. $wechat_where[] = ['employee_id', '=', $employee_id];
  3693. $wechat_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3694. //查询每日发圈转发次数
  3695. $k_share_count = $share_count + DailyWechatArticleCopy::where($wechat_where)->count();
  3696. $employee_data['share_count'] = $k_share_count;
  3697. $clue_ids = CustomerClue::where([['employee_id', '=', $employee_id], ['addtime', 'between', [$start_time, $end_time]]])->column('id');
  3698. $customer_share = Customer::where('clue_id', 'in', $clue_ids)->column('id');
  3699. // 浏览时长
  3700. $clue_type = ['materialCase', 'video', 'article', 'CompanyStrength', 'companyStrength', 'materialEvidence', 'building', 'toolAll', 'companyVr', 'lottery', 'styleTest', 'likeTest', 'priceCalculation', 'spellgroup', 'agents', 'weiwang', 'card'];
  3701. unset($foot_where);
  3702. $foot_where[] = ['employee_id', '=', $employee_id];
  3703. $foot_where[] = ['pipe_type', 'in', $clue_type];
  3704. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  3705. $foot_count = Footprints::where($foot_where)->count(); // 次数
  3706. $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
  3707. $employee_data['visit_long'] = round($visit_long / 60, 2);
  3708. $employee_data['visit_avg'] = $foot_count > 0 ? round(($visit_long / $foot_count /60), 2) : round($visit_long / 60, 2);
  3709. //微信沟通
  3710. $visit_where[] = ['employee_id', '=', $employee_id];
  3711. $visit_where[] = ['confirm_date', 'between', [$start_time, $end_time]];
  3712. $visit_where[] = ['type', '=', 3];
  3713. $wechat_visit = CustomerVisitLog::where($visit_where)->select()->toArray();
  3714. $wechat_visit_count = count($wechat_visit);
  3715. $employee_data['wechat_visit_count'] = $wechat_visit_count;
  3716. $customer_wechat = array_column($wechat_visit, 'customer_id');
  3717. $all_customer_where[] = ['employee_id', '=', $employee_id];
  3718. $all_customer_where[] = ['sign_time', 'between', [$start_time, $end_time]];
  3719. $customer_count = Customer::where($all_customer_where)->count();
  3720. $employee_data['wechat_visit_avg'] = $customer_count > 0 ? round($wechat_visit_count / $customer_count, 2) : $wechat_visit_count;
  3721. $employee_data['all_count'] = $call_count + $k_share_count + $wechat_visit_count;
  3722. $employee_data['employee_name'] = Employee::where('id', $employee_id)->value('name');
  3723. $data['employee'] = $employee_data;
  3724. $type = input('type', '', 'trim');
  3725. switch ($type){
  3726. case 'call':
  3727. $customer_id = $customer_call;
  3728. break;
  3729. case 'share':
  3730. $customer_id = $customer_share;
  3731. break;
  3732. case 'wechat':
  3733. $customer_id = $customer_wechat;
  3734. break;
  3735. default:
  3736. $customer_id = array_values(array_filter(array_unique(array_merge($customer_call, $customer_share, $customer_wechat))));
  3737. break;
  3738. }
  3739. $page = input('page', 1, 'intval');
  3740. $customers = Customer::with(['designer', 'designerOrg', 'employee', 'agent'])->where([['id', 'in', $customer_id], ['employee_id', '=', $employee_id]])->page($page, 10)->select();
  3741. $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();
  3742. // 状态统计获取
  3743. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($customers, 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  3744. $customersState = [];
  3745. foreach ($customersStateList as $s) {
  3746. if (!isset($customersState[$s['customer_id']]))
  3747. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3748. $customersState[$s['customer_id']]['count'] += $s['num'];
  3749. // 到店,量房,活动,定金,签单
  3750. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  3751. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  3752. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  3753. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  3754. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  3755. }
  3756. foreach ($customers as $k => $v) {
  3757. $customers[$k]['stateNum'] = $customersState[$v['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3758. }
  3759. $data['customer'] = $customers;
  3760. return json(['code'=> self::success, 'data'=> $data, 'msg'=> '请求成功']);
  3761. }
  3762. /**
  3763. * 卖卡首页统计
  3764. */
  3765. public function maika_index()
  3766. {
  3767. $token = $this->request->token;
  3768. $root_sub = orgSubIds($token['root_org']);
  3769. // 所选部门
  3770. $org_id = input('org_id', '', 'intval');
  3771. $org_id = !empty($org_id) ? $org_id : $token['org_id'];
  3772. if (!in_array($org_id, $root_sub)){
  3773. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  3774. }
  3775. $start_time_get = input('start_time', time(), 'strtotime');
  3776. $end_time_get = input('end_time', time(), 'strtotime');
  3777. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3778. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3779. //员工数量
  3780. $org_name = Org::where('id', $org_id)->value('name');
  3781. $sub_org = orgSubIds($org_id);
  3782. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  3783. $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3784. $db_where[] = ['Customer.org_id', 'in', $sub_org];
  3785. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  3786. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  3787. $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
  3788. $count = Db::view('CustomerVisitLog', 'id')
  3789. ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id')
  3790. ->where($db_where)
  3791. ->group('Customer.id')
  3792. ->count();
  3793. $data = ['org_name'=> $org_name, 'employee_count'=> count($sub_employee_ids), 'count'=> $count];
  3794. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  3795. }
  3796. /**
  3797. * 卖卡部门数据
  3798. */
  3799. public function maika_org(){
  3800. $token = $this->request->token;
  3801. $param = $this->request->only(['org_id', 'start_time','end_time','order']);
  3802. $start_time_get = input('start_time', time(), 'strtotime');
  3803. $end_time_get = input('end_time', time(), 'strtotime');
  3804. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3805. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3806. $root_sub = orgSubIds($token['root_org']);
  3807. // 所选部门
  3808. $org_id = input('org_id', '', 'intval');
  3809. if (!$org_id) {
  3810. $org_id = $token['org_id'];
  3811. }
  3812. if (!in_array($org_id, $root_sub)){
  3813. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  3814. }
  3815. // 所选部门
  3816. $sub_org = Org::where([['pid', '=', $org_id], ['status', '=', 1]])->field('id,name,pid')->select()->toArray();
  3817. // 所有客户量 用于后面计算百分比
  3818. $sub_org_ids = orgSubIds($org_id);
  3819. $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3820. $db_where[] = ['Customer.org_id', 'in', $sub_org_ids];
  3821. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  3822. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  3823. $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
  3824. $log_list = Db::view('CustomerVisitLog', 'id')
  3825. ->view('Customer', 'id cid,org_id', 'Customer.id=CustomerVisitLog.customer_id')
  3826. ->where($db_where)
  3827. ->order('confirm_date asc')
  3828. ->select()
  3829. ->toArray();
  3830. // 每个员工的跟进记录
  3831. $org_log = [];
  3832. foreach ($log_list as $k => $v) {
  3833. if (empty($org_log[$v['org_id']])) {
  3834. $org_log[$v['org_id']] = [];
  3835. } elseif (in_array($v['cid'], $org_log[$v['org_id']])) {
  3836. continue;
  3837. }
  3838. $org_log[$v['org_id']][] = $v['cid'];
  3839. }
  3840. foreach ($sub_org as $k => $v){
  3841. // 是否有子部门
  3842. $have_sub_org = Org::where('pid', '=', $v['id'])->count();
  3843. if ($have_sub_org > 0){
  3844. $sub_org[$k]['have_sub_org'] = true;
  3845. } else {
  3846. $sub_org[$k]['have_sub_org'] = false;
  3847. }
  3848. $v_sub_org = orgSubIds($v['id']);
  3849. $org_employee_ids = Employee::where([['org_id', 'in', $v_sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  3850. $sub_org[$k]['employee_count'] = count($org_employee_ids);
  3851. $sub_org[$k]['count'] = !empty($org_log[$v['id']]) ? count($org_log[$v['id']]) : 0;
  3852. }
  3853. if (!empty($param['order'])) {
  3854. $order_arr = explode(' ', $param['order']);
  3855. $sort_field = $order_arr[0] ?? 'count';
  3856. $sort_order = $order_arr[1] ?? 'desc';
  3857. switch ($sort_field){
  3858. case 'count':
  3859. $field = 'count';
  3860. break;
  3861. default:
  3862. $field = 'count';
  3863. break;
  3864. }
  3865. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  3866. $arr = [];
  3867. foreach ($sub_org as $v){
  3868. if (!isset($v[$field])){
  3869. break;
  3870. }
  3871. $arr[] = $v[$field];
  3872. }
  3873. if (!empty($arr)){
  3874. array_multisort($arr, $sort, $sub_org);
  3875. }
  3876. }
  3877. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $sub_org]);
  3878. }
  3879. /**
  3880. * 卖卡员工数据
  3881. */
  3882. public function maika_employee(){
  3883. $token = $this->request->token;
  3884. $param = $this->request->only(['org_id', 'start_time','end_time','order','page'=>1,'limit'=>10]);
  3885. $start_time_get = input('start_time', time(), 'strtotime');
  3886. $end_time_get = input('end_time', time(), 'strtotime');
  3887. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3888. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3889. // 所选部门
  3890. $org_id = !empty($param['org_id']) ? $param['org_id'] : $token['org_id'];
  3891. $root_sub = orgSubIds($token['root_org']);
  3892. if (!in_array($org_id, $root_sub)){
  3893. return json(['code'=> self::success, 'data'=> [], 'msg'=> 'success']);
  3894. }
  3895. // 所选部门员工统计数据
  3896. $e_where[] = ['org_id', '=', $org_id];
  3897. $e_where[] = ['state', '=', '在职'];
  3898. $e_where[] = ['uid', '<>', 0];
  3899. $employee_list = Employee::with(['user'=> function($query){
  3900. $query->field('id,headimgurl');
  3901. }])->where($e_where)->field('id,name,org_id,root_id,uid')->select()->toArray();
  3902. // 所有客户量
  3903. $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3904. // 每个员工的客户
  3905. $db_where[] = ['Customer.org_id', '=', $org_id];
  3906. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  3907. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  3908. $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
  3909. $log_list = Db::view('CustomerVisitLog', 'id,customer_employee_id')
  3910. ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id')
  3911. ->where($db_where)
  3912. ->order('confirm_date asc')
  3913. ->select()
  3914. ->toArray();
  3915. // 每个员工的跟进记录
  3916. $employee_log = [];
  3917. foreach ($log_list as $k => $v) {
  3918. if (empty($employee_log[$v['customer_employee_id']])) {
  3919. $employee_log[$v['customer_employee_id']] = [];
  3920. } elseif (in_array($v['cid'], $employee_log[$v['customer_employee_id']])) {
  3921. continue;
  3922. }
  3923. $employee_log[$v['customer_employee_id']][] = $v['cid'];
  3924. }
  3925. foreach($employee_list as $k => $v){
  3926. $employee_list[$k]['employee_count'] = count($employee_list);
  3927. $employee_list[$k]['count'] = !empty($employee_log[$v['id']]) ? count($employee_log[$v['id']]) : 0;
  3928. }
  3929. if (!empty($param['order'])) {
  3930. $order_arr = explode(' ', $param['order']);
  3931. $sort_field = $order_arr[0] ?? 'count';
  3932. $sort_order = $order_arr[1] ?? 'desc';
  3933. switch ($sort_field){
  3934. case 'count':
  3935. $field = 'count';
  3936. break;
  3937. default:
  3938. $field = 'count';
  3939. break;
  3940. }
  3941. $sort = $sort_order == 'asc' ? SORT_ASC : SORT_DESC;
  3942. $arr = [];
  3943. foreach ($employee_list as $v){
  3944. if (!isset($v[$field])){
  3945. break;
  3946. }
  3947. $arr[] = $v[$field];
  3948. }
  3949. if (!empty($arr)){
  3950. array_multisort($arr, $sort, $employee_list);
  3951. }
  3952. }
  3953. $employee_list = array_slice($employee_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
  3954. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $employee_list]);
  3955. }
  3956. /**
  3957. * 卖卡员工客户数据
  3958. */
  3959. public function maika_customer()
  3960. {
  3961. $token = $this->request->token;
  3962. $param = $this->request->only(['employee_id', 'start_time','end_time','page'=>1,'limit'=>10]);
  3963. $start_time_get = input('start_time', time(), 'strtotime');
  3964. $end_time_get = input('end_time', time(), 'strtotime');
  3965. $start_time = date('Y-m-d 00:00:00', $start_time_get);
  3966. $end_time = date('Y-m-d 23:59:59', $end_time_get);
  3967. $employee_id = input('employee_id', '', 'intval');
  3968. $sub_org = orgSubIds($token['org_id']);
  3969. $sub_employee_ids = Employee::where([['org_id', 'in', $sub_org], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  3970. if (!in_array($employee_id, $sub_employee_ids)){
  3971. return json(['code'=> self::success, 'data'=> ['empdata'=> [], 'list'=> []], 'msg'=> 'success']);
  3972. }
  3973. $mk_state = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3974. $wuxiao = Customer::changeState('无效', 'chaos');
  3975. $db_where[] = ['Customer.employee_id', '=', $employee_id];
  3976. $db_where[] = ['Customer.state', 'NOT NULL', null];
  3977. $db_where[] = ['Customer.state', 'not in', $wuxiao];
  3978. $db_where[] = ['Customer.died', '<>', 2];
  3979. $db_where[] = ['CustomerVisitLog.is_merge', '=', 0];
  3980. $db_where[] = ['CustomerVisitLog.confirm_date', 'between', [$start_time, $end_time]];
  3981. $db_where[] = ['CustomerVisitLog.state', 'in', $mk_state];
  3982. $customer_ids = Db::view('CustomerVisitLog', 'id')
  3983. ->view('Customer', 'id cid', 'Customer.id=CustomerVisitLog.customer_id')
  3984. ->where($db_where)
  3985. ->group('Customer.id')
  3986. ->column('Customer.id');
  3987. $cuslist = Customer::with(['employee'=>function($query){
  3988. $query->field('id,name');
  3989. }])->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');
  3990. $employeedata = Employee::where('id',$param['employee_id'])->field('id,name,phone')->find();
  3991. if(empty($employeedata)) return json(['code'=> 1, 'msg'=> '员工为空']);
  3992. // 状态统计获取
  3993. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', $customer_ids]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  3994. $customersState = [];
  3995. foreach ($customersStateList as $s) {
  3996. if (!isset($customersState[$s['customer_id']]))
  3997. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  3998. $customersState[$s['customer_id']]['count'] += $s['num'];
  3999. // 到店,量房,活动,定金,签单
  4000. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  4001. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  4002. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  4003. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  4004. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  4005. }
  4006. foreach($cuslist as $key=>$val){
  4007. $cuslist[$key]['stateNum'] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0];
  4008. if(!empty($customersState[$val['id']])) $cuslist[$key]['stateNum'] = $customersState[$val['id']];
  4009. }
  4010. $empdata = ['empname'=> $employeedata['name'], 'employee_count'=> 1, 'count'=> count($customer_ids)];
  4011. $data = ['empdata'=> $empdata, 'list'=> $cuslist];
  4012. return json(['code'=> 0, 'msg'=> '获取成功', 'data'=> $data]);
  4013. }
  4014. }