Crm.php 227 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136
  1. <?php
  2. namespace app\sys\controller;
  3. use app\api\controller\FishWh;
  4. use app\event\Msg;
  5. use app\jobs\CrmImport;
  6. use app\logics\CustomerLogic;
  7. use app\logics\ClueLogic;
  8. use app\logics\CustomerRecycleLogic;
  9. use app\logics\OrgLogic;
  10. use app\logics\PoolLogic;
  11. use app\model\Community;
  12. use app\model\Company;
  13. use app\model\Customer;
  14. use app\model\CustomerClue;
  15. use app\model\CustomerDropPool;
  16. use app\model\CustomerDropRecord;
  17. use app\model\CustomerSource;
  18. use app\model\CustomersSubscribe;
  19. use app\model\CustomerVisitLog;
  20. use app\model\CustomerRecycle;
  21. use app\model\Decostyle;
  22. use app\model\OutCallSetting;
  23. use app\model\Pool;
  24. use app\model\Product;
  25. use app\model\Setting;
  26. use app\model\WeworksingleCompanySetting;
  27. use app\model\WeworksingleCustomer;
  28. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  29. use think\facade\View;
  30. use app\model\Footprints;
  31. use app\model\Org;
  32. use app\model\Employee;
  33. use PhpOffice\PhpSpreadsheet\IOFactory;
  34. use app\model\CrmImportLog;
  35. use app\model\CrmImportRecord;
  36. use think\facade\Db;
  37. use app\model\CustomerPortrait;
  38. use toolkits\Aec;
  39. use app\model\EmployeeMsg;
  40. use app\model\Miniprogram;
  41. use app\model\CustomerPortraitField;
  42. use app\model\CustomerState;
  43. use app\model\OutCallLog;
  44. use app\model\CrmImportRetrieveLog;
  45. use app\model\CustomerPortraitFieldSelect;
  46. use app\model\OutCallMbLog;
  47. use clue\Fish;
  48. use clue\Tencent;
  49. use think\facade\Queue;
  50. use think\facade\Request;
  51. class Crm extends Base
  52. {
  53. /**
  54. * 面板
  55. */
  56. public function index()
  57. {
  58. $type = input('type', '', 'intval');
  59. $protected = input('protected', '', 'intval');
  60. $request = request();
  61. if (!$request->isAjax()) {
  62. // 部门
  63. $org = OrgLogic::struc($request->employee->root_id);
  64. View::assign('org', $org);
  65. $orgids = orgSubIds(request()->employee->root_id);
  66. View::assign('orgids', json_encode($orgids));
  67. View::assign('empid', $request->employee->id);
  68. // View::assign('manager', $request->employee->is_manager);
  69. View::assign('type', $type);
  70. View::assign('protected', $protected);
  71. $source = CustomerSource::where('root_id', '=', request()->employee->root_id)->select()->toArray();
  72. View::assign('source', $source);
  73. //设计师获取
  74. $w[] = ['path', 'like', request()->employee->root_id . '-%'];
  75. $w[] = ['org_type', '=', 2];
  76. $orgs = Org::where($w)->column('id');
  77. $designer = Employee::where([['org_id', 'in', $orgs], ['state', '=', '在职'], ['show', '=', 0]])->field('id,name,initials s')->order('s asc')->select()->toArray();
  78. View::assign('designer', $designer);
  79. //小区名称获取
  80. /*$communities = Community::where('root_id', '=', request()->employee->root_id)->where([['type', '=', 0]])->order('pinyin asc')->select()->toArray();
  81. if (!empty($communities)) {
  82. $communities = hanziheadstr($communities);
  83. }
  84. View::assign('communityList', $communities);*/
  85. $xinjushang = 0;
  86. View::assign('xinjushang', $xinjushang);
  87. return View::fetch();
  88. }
  89. $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state', 'protected', 'type', 'time', 'followed_time', 'protected_time', 'square_start', 'square_end', 'source', 'designer', 'customer_type', 'community', 'level', 'name'=> '', 'phone'=> '', 'no_visit_time'=> '']);
  90. $root_id = request()->employee->root_id;
  91. $subOrg = orgSubIds($root_id);
  92. if (!empty($param['org']) && in_array($param['org'], $subOrg)) {
  93. $orgids = orgSubIds($param['org']);
  94. } else {
  95. $orgids = orgSubIds(request()->employee->root_id);
  96. }
  97. if (!empty($param['protected'])) {
  98. $org_employee = Employee::where('org_id', 'in', $orgids)->column('id');
  99. $condition[] = ['employee_id', 'in', $org_employee];
  100. } else {
  101. $condition = [['org_id', 'in', $orgids]];
  102. }
  103. $order = isset($param['order']) ? $param['order'] : 'id desc';
  104. $cids = [];
  105. $condition[] = ['employee_id', 'not null', ''];
  106. $condition[] = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  107. $condition[] = ['died', '<>', 2];
  108. if (!empty($param['time'])) {
  109. //录入时间
  110. $addtimeArr = explode(' - ', $param['time']);
  111. $start = $addtimeArr[0];
  112. $end = date('Y-m-d', strtotime($addtimeArr[1]) + 86400);
  113. $condition[] = ['addtime', '>=', $start];
  114. $condition[] = ['addtime', '<', $end];
  115. }
  116. if (!empty($param['followed_time'])) $condition[] = ['last_contact_date', 'between', explode(' - ', $param['followed_time'])];
  117. if (!empty($param['protected_time']) && !empty($param['protected'])) $condition[] = ['protected_to', 'between', explode(' - ', $param['protected_time'])];
  118. $whereRaw = [];
  119. if (!empty($param['square_start'])) {
  120. $whereRaw[] = "square +0 >=" . $param['square_start'];
  121. }
  122. if (!empty($param['square_end'])) {
  123. $whereRaw[] = "square +0 <=" . $param['square_end'];
  124. }
  125. if (!empty($param['source'])) {
  126. if ($param['source'] == -1) {
  127. $condition[] = ['source_id', '=', null];
  128. } else {
  129. $condition[] = ['source_id', '=', $param['source']];
  130. }
  131. }
  132. if (!empty($param['designer'])) $condition[] = ['designer_id', '=', $param['designer']];
  133. if (!empty($param['customer_type'])) {
  134. switch ($param['customer_type']) {
  135. case 1:
  136. $condition[] = ['crm_res_id', '>', 0];
  137. $condition[] = ['remark', '<>', '公海获取'];
  138. break;
  139. case 2:
  140. $condition[] = ['remark', '=', '活动报名建档'];
  141. break;
  142. case 3:
  143. $condition[] = ['agents_id', '>', 0];
  144. break;
  145. case 4:
  146. $condition[] = ['crm_res_id', 'NULL', null];
  147. $condition[] = ['remark', '<>', '活动报名建档'];
  148. $condition[] = ['agents_id', 'NULL', null];
  149. break;
  150. case 5:
  151. $condition[] = ['remark', '=', '公海获取'];
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. if (!empty($param['community'])) $condition[] = ['community_name', 'like', '%' . $param['community'] . '%'];
  158. if (!empty($param['level'])) $condition[] = ['level', '=', $param['level']];
  159. if (!empty($param['name'])) $condition[] = ['name', 'like', '%' . $param['name'] . '%'];
  160. if (!empty($param['phone'])){
  161. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  162. $value = $aec->encrypt($param['phone']);
  163. $condition[] = ['phone', '=', $value];
  164. }
  165. if (!empty($param['no_visit_time'])){
  166. switch ((int)$param['no_visit_time']) {
  167. case 3:
  168. $no_date_s = strtotime(date('Y-m-d'));
  169. $no_date_e = strtotime(date('Y-m-d')) - 2*24*3600;
  170. $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)];
  171. $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)];
  172. break;
  173. case 7:
  174. $no_date_s = strtotime(date('Y-m-d')) - 2*24*3600;
  175. $no_date_e = strtotime(date('Y-m-d')) - 6*24*3600;
  176. $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)];
  177. $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)];
  178. break;
  179. case 15:
  180. $no_date_s = strtotime(date('Y-m-d')) - 6*24*3600;
  181. $no_date_e = strtotime(date('Y-m-d')) - 14*24*3600;
  182. $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)];
  183. $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)];
  184. break;
  185. case 30:
  186. $no_date_s = strtotime(date('Y-m-d')) - 14*24*3600;
  187. $no_date_e = strtotime(date('Y-m-d')) - 29*24*3600;
  188. $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)];
  189. $condition[] = ['last_contact_date', '>=', date('Y-m-d', $no_date_e)];
  190. break;
  191. case 31:
  192. $no_date_s = strtotime(date('Y-m-d')) - 30*24*3600;
  193. $condition[] = ['last_contact_date', '<', date('Y-m-d', $no_date_s)];
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. setCondition($param, ['emp_id', 'employee_id'], '=', $condition);
  200. $whereOr = []; //or条件二维数据
  201. if (empty($param['state'])) {
  202. //2023-02-07 客户有效状态 为排除待确认和无效, 或者 待确认并且不是资源库过来的
  203. $state1 = Customer::changeState('待确认', 'chaos');
  204. $state2 = Customer::changeState('无效', 'chaos');
  205. $whereOr[] = [['state', 'not in', array_merge($state1,$state2)], [['state', 'in',$state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]];
  206. } elseif ($param['state'] == '待确认') {
  207. $state1 = Customer::changeState('待确认', 'chaos');
  208. //$condition[] = ['crm_res_id', 'NULL', NULL];
  209. //$condition[] = ['state', 'in', $state1];
  210. $whereOr[] = [[['state', 'in',$state1],['crm_res_id','NULL',NULL]],[['state', 'in',$state1],['crm_res_id','NOTNULL',NULL],['valid_time','NOTNULL',NULL]]];
  211. } elseif ($param['state'] == '有效'){
  212. $condition[] = ['state','not in',Customer::changeState('待确认','chaos')];
  213. } else {
  214. $state = CustomerVisitLog::changeState($param['state'], 'chaos');
  215. $where = [
  216. ['org_id', 'in', $orgids],
  217. // ['state', 'REGEXP', '(^|,)(' . implode('|', $state) . ')(,|$)']
  218. ['state', 'in', $state]
  219. ];
  220. $cids = CustomerVisitLog::where($where)->column('customer_id');
  221. }
  222. $where_raw = implode(' and ', $whereRaw);
  223. //保护期筛选判断
  224. if (empty($param['protected'])) {
  225. if ($where_raw) {
  226. $model = Customer::where($condition)->whereRaw($where_raw);
  227. } else {
  228. $model = Customer::where($condition);
  229. }
  230. // or 条件处理
  231. foreach ($whereOr as $v) {
  232. $model = $model->where(function($query) use ($v){
  233. $query->whereOr($v);
  234. });
  235. }
  236. // id集合合并
  237. if (!empty($cids)) {
  238. $m_cids = $model->column('id');
  239. $ids_all = array_intersect($cids, $m_cids);
  240. $c_model = Customer::where('id', 'in', $ids_all);
  241. } else {
  242. $c_model = $model;
  243. }
  244. $data = $c_model->with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  245. $query->field('id,customer_id,state')->group('state,customer_id');
  246. }])->withCount(['visitLog' => function ($query) {
  247. $query->where(function ($query) {
  248. $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]);
  249. });
  250. }])->withCount(['activityFrequency' => function ($query) {
  251. $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]);
  252. }])->page($param['page'], $param['limit'])->order($order)->select()->visible(['id', 'employee_id', 'name', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'crm_res_id', 'remark', 'agents_id'])->toArray();
  253. foreach ($data as $k => $v) {
  254. if ($v['remark'] == '公海获取') {
  255. $customer_type = '公海获取';
  256. } elseif ($v['agents_id']) {
  257. $customer_type = '装修推荐官';
  258. } elseif ($v['remark'] == '活动报名建档') {
  259. $customer_type = '活动报名';
  260. } elseif ($v['crm_res_id']) {
  261. $customer_type = '资源库';
  262. } else {
  263. $customer_type = '自建';
  264. }
  265. $data[$k]['customer_type'] = $customer_type;
  266. if ($data[$k]['state'] == '已签单') {
  267. $data[$k]['state'] = '已转单';
  268. }
  269. if ($data[$k]['state'] == '已交定') {
  270. $data[$k]['state'] = '已签单';
  271. }
  272. $data[$k]['phone'] = substr_replace($v['phone'], '******', 3, 6);
  273. $data[$k]['phone1'] = $v['phone1'] ? substr_replace($v['phone1'], '******', 3, 6) : '';
  274. $data[$k]['phone2'] = $v['phone2'] ? substr_replace($v['phone2'], '******', 3, 6) : '';
  275. //处理历史记录
  276. $allstate = '';
  277. foreach ($v['visitLog'] as $m => $p) {
  278. if ($p['state'] == '已签单') {
  279. $p['state'] = '已转单';
  280. }
  281. if ($p['state'] == '已交定') {
  282. $p['state'] = '已签单';
  283. }
  284. if ($p['state'] == '回访') {
  285. $p['state'] = '待确认';
  286. }
  287. $allstate .= $p['state'] . ',';
  288. }
  289. $data[$k]['allstate'] = trim($allstate, ',');
  290. //增加跟进次数
  291. $data[$k]['lognum'] = CustomerVisitLog::with('employee')->where('customer_id', '=', $v['id'])->count();
  292. }
  293. $count = $c_model->count();
  294. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  295. } else {
  296. if ($where_raw) {
  297. $data_obj = Customer::where($condition)->whereRaw($where_raw);
  298. } else {
  299. $data_obj = Customer::where($condition);
  300. }
  301. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', request()->employee->root_id]])->findOrEmpty();
  302. if (!$setting->isEmpty()) {
  303. $content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  304. $empty = false;
  305. if (!empty($content)) {
  306. foreach ($content as $k => $v) {
  307. if (!isset($v['day']) || !isset($v['state'])) {
  308. $empty = true;
  309. }
  310. }
  311. }
  312. if ($empty) {
  313. $content = [];
  314. }
  315. $or = [];
  316. foreach ($content as $k => $v) {
  317. if ($v['state'] == 1 && $v['day'] > 0) {
  318. $or[] = [
  319. ['state', 'in', Customer::changeState($k, 'chaos')],
  320. ['protected_to', '<', date('Y-m-d H:i:s')],
  321. ['protected_to', 'NOTNULL', null]
  322. ];
  323. }
  324. }
  325. if (!empty($or)) {
  326. $whereOr[] = $or;
  327. } else {
  328. return json(['code' => 0, 'data' => [], 'count' => 0]);
  329. }
  330. foreach ($whereOr as $v) {
  331. $data_obj = $data_obj->where(function($query) use ($v){
  332. $query->whereOr($v);
  333. });
  334. }
  335. } else { //没设置就返回空
  336. return json(['code' => 0, 'data' => [], 'count' => 0]);
  337. }
  338. // id集合合并
  339. if (!empty($cids)) {
  340. $m_cids = $data_obj->column('id');
  341. $ids_all = array_intersect($cids, $m_cids);
  342. $c_model = Customer::where('id', 'in', $ids_all);
  343. } else {
  344. $c_model = $data_obj;
  345. }
  346. $data_obj = $c_model->with(['employee', 'org', 'designer', 'source', 'visitLog' => function ($query) {
  347. $query->field('id,customer_id,state')->group('state,customer_id');
  348. }])->withCount(['visitLog' => function ($query) {
  349. $query->where(function ($query) {
  350. $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]);
  351. });
  352. }])->withCount(['activityFrequency' => function ($query) {
  353. $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]);
  354. }]);
  355. $data = $data_obj->page($param['page'], $param['limit'])->order($order)->select()->visible(['id', 'employee_id', 'name', 'community_name', 'phone', 'level', 'state', 'square', 'revisit_time', 'addtime', 'last_contact_date', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'source_id', 'source.source', 'phone1', 'phone2', 'crm_res_id', 'remark', 'agents_id'])->toArray();
  356. if (!$setting->isEmpty()) {
  357. $setting_content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  358. $empty = false;
  359. if (!empty($setting_content)) {
  360. foreach ($setting_content as $k => $v) {
  361. if (!isset($v['day']) || !isset($v['state'])) {
  362. $empty = true;
  363. }
  364. }
  365. }
  366. if ($empty) {
  367. $setting_content = [];
  368. }
  369. } else {
  370. $setting_content = [];
  371. }
  372. foreach ($data as &$item) {
  373. $item['un_protected'] = false;
  374. if (!empty($item['protected_to']) && time() > strtotime($item['protected_to'])) {
  375. $state_n = Customer::changeState($item['state'], 'n');
  376. if (!empty($setting_content[$state_n]['state']) && $setting_content[$state_n]['state'] == 1) {
  377. $item['un_protected'] = true;
  378. }
  379. }
  380. $item['square'] = floatval($item['square']);
  381. $item['mobile'] = $item['phone'];
  382. $item['mobile1'] = $item['phone1'];
  383. $item['mobile2'] = $item['phone2'];
  384. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  385. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  386. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  387. //处理历史记录
  388. $allstate = '';
  389. foreach ($item['visitLog'] as $m => $p) {
  390. if ($p['state'] == '已签单') {
  391. $p['state'] = '已转单';
  392. }
  393. if ($p['state'] == '已交定') {
  394. $p['state'] = '已签单';
  395. }
  396. if ($p['state'] == '回访') {
  397. $p['state'] = '待确认';
  398. }
  399. $allstate .= $p['state'] . ',';
  400. }
  401. $data[$k]['allstate'] = trim($allstate, ',');
  402. //增加跟进次数
  403. $item['lognum'] = CustomerVisitLog::with('employee')->where('customer_id', '=', $item['id'])->count();
  404. //客户种类
  405. $item['customer_type'] = $item['crm_res_id'] ? '资源库' : ($item['remark'] == '活动报名建档' ? '活动' : ($item['agents_id'] ? '经纪人' : '自建'));
  406. if ($item['state'] == '已交定') {
  407. $item['state'] = '已签单';
  408. }
  409. if ($item['state'] == '已签单') {
  410. $item['state'] = '已转单';
  411. }
  412. }
  413. $count = $c_model->count();
  414. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  415. }
  416. }
  417. /**
  418. * 客户详情
  419. */
  420. public function view()
  421. {
  422. $id = input('id', '', 'intval');
  423. // if (!request()->isAjax()){
  424. // View::assign('id', $id);
  425. // return View::fetch();
  426. // }
  427. $where[] = ['id', '=', $id];
  428. $data = Customer::with(['source', 'designer' => function ($query) {
  429. $query->field('id,name,headimgurl,org_id,uid');
  430. }, 'employee'])->where($where)->find();
  431. if (empty($data)) return json(['code' => 1, 'msg' => '数据不存在']);
  432. $data->phone = substr_replace($data->phone, '******', 3, 6);
  433. $orgids = orgSubIds(request()->employee->root_id);
  434. if (!in_array($data['org_id'], $orgids)) {
  435. return json(['code' => 1, 'msg' => '数据不存在']);
  436. }
  437. $count = CustomerVisitLog::where('customer_id', $id)->count();
  438. $data['visit_count'] = $count;
  439. $data['deposit'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '交定'])])->order('addtime desc')->value('addtime');
  440. $data['sign'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '签单'])])->order('addtime desc')->value('addtime');
  441. // 量房、活动、到店次数统计
  442. $data['liangfangNum'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '确认量房'])])->count();
  443. $data['activityNum'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '确认到场'])])->count();
  444. $data['daodianNum'] = CustomerVisitLog::where([['customer_id', '=', $id], CustomerVisitLog::changeState(['state', '=', '确认到店'])])->count();
  445. $data['deposit_count'] = CustomerVisitLog::where([['customer_id', '=', $id], ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]])->count();
  446. $data['square'] = floatval($data['square']);
  447. // 查询客户是否有预约
  448. $data['subscirbe'] = CustomersSubscribe::where([['customer_id', '=', $id], ['state', '=', 0]])->field('id,state,type,subscribe_date')->select()->toArray();
  449. //查询我最近一次的签单和交定金额
  450. $state = CustomerVisitLog::changeState('已交定', 'chaos');
  451. $data['deposit_money1'] = CustomerVisitLog::where([['employee_id', '=', request()->employee->id], ['customer_id', '=', $id], ['state', 'in', $state]])->order('id desc')->value('money') ?: '';
  452. $data['money'] = $data->getData('signed_money');
  453. // return json(['code' => 0, 'data' => $data]);
  454. //处理新设置的客户扩展字段
  455. $newext = $this->get_portrait_field($data);
  456. //array_splice($newext,1,'basic_info');
  457. $topdata = [];
  458. $arr = ['level', 'source_id', 'phone'];
  459. $arr_check = ['level' => 0, 'source_id' => 0, 'phone' => 0];
  460. $topext = ['intention', 'point', 'age_range', 'first', 'follow', 'wechat'];
  461. foreach ($newext as $key => $val) {
  462. foreach ($val['child'] as $k => $v) {
  463. if (in_array($v['keyname'], $topext)) {
  464. $topdata[] = $v;
  465. }
  466. if (in_array($v['keyname'], $arr)) $arr_check[$v['keyname']] = 1;
  467. }
  468. }
  469. //unset($newext['basic_info']);
  470. $data['topdata'] = $topdata;
  471. $data['newext'] = $newext;
  472. View::assign('data', $data);
  473. View::assign('arr_check', $arr_check);
  474. View::assign('root_id', request()->employee->root_id);
  475. //顶部固定字段权限
  476. return View::fetch();
  477. }
  478. //新获取客户扩展字段
  479. public function get_portrait_field($cusdata)
  480. {
  481. $root_id = request()->employee->root_id;
  482. $w[] = ['root_id', '=', $root_id];
  483. $list = CustomerPortraitField::where([['root_id', '=', $root_id], ['pid', '=', 0], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  484. $city = Company::where('root_id', $root_id)->value('city');
  485. foreach ($list as $key => $val) {
  486. $child = CustomerPortraitField::with(['select'])->where([['root_id', '=', $root_id], ['pid', '<>', 0], ['pid', '=', $val['id']], ['status', '=', 0]])->orderRaw('if(isnull(sort),1,0),sort asc')->select()->toArray();
  487. foreach ($child as $k => $v) {
  488. if ($v['keyname'] == 'current_region') {
  489. $child[$k]['select'][] = ['id' => $root_id, 'name' => $city, 'pid' => $v['id']];
  490. }
  491. if ($v['keyname'] == 'source_id') {
  492. $soudata = CustomerSource::field('id,source as name')->where('root_id', $root_id)->select()->toArray();
  493. $child[$k]['select'] = $soudata;
  494. }
  495. if ($v['keyname'] == 'deco_style') {
  496. $decostyles = Decostyle::field('id,name')->where([['root_id', '=', $root_id], ['type', '=', 0]])->select()->toArray();
  497. $child[$k]['select'] = $decostyles;
  498. }
  499. $child[$k]['value'] = '';
  500. $child[$k]['valname'] = '';
  501. }
  502. $list[$key]['child'] = $child;
  503. }
  504. $new = $list;
  505. $cuslog = new CustomerLogic;
  506. if (!empty($cusdata['ext']) && $cusdata['ext'] != 'null') {
  507. //$extdata=get_object_vars($cusdata['ext']);
  508. $extdata = json_decode($cusdata['ext'], true);
  509. if (isset($extdata['ext1'])) {
  510. $new = $cuslog->old_data_save($cusdata, $list);
  511. } else {
  512. $new = $cuslog->new_data_save($cusdata, $extdata, $list);
  513. }
  514. }
  515. return $new;
  516. //return json(['code' => 0, 'data' => $new]);
  517. }
  518. /**
  519. * 客户意向折线图
  520. */
  521. public function line_chart()
  522. {
  523. $customerId = request()->param('customer_id');
  524. $w[] = ['id', '=', $customerId];
  525. $w[] = ['employee_id', '=', request()->employee->id];
  526. $check = Customer::where($w)->findOrEmpty();
  527. if ($check->isEmpty()) return json(['code' => 0, 'data' => [], 'msg' => '操作成功']);
  528. $w1[] = ['customer_id', '=', $customerId];
  529. $w1[] = ['starts', '>', 0];
  530. $ids = CustomerVisitLog::where($w1)->order('id desc')->limit(30)->column('id');
  531. $w1[] = ['id', 'in', $ids];
  532. $row = CustomerVisitLog::where($w1)->order('id asc')->column('starts');
  533. return json(['code' => 0, 'data' => $row, 'msg' => '操作成功']);
  534. }
  535. public function test(){
  536. // $aw[] = ['id', 'in', array_keys($cidList)];
  537. // array_merge(Customer::changeState('待确认', 'chaos'), Customer::changeState('无效', ' chaos'))
  538. $where = [
  539. ['state', 'not in', Customer::changeState('无效', 'chaos')],
  540. ['died', '<>', 2]
  541. ];
  542. $month = request()->param('month');
  543. if (isset($month)){
  544. // 获取当前年份,或者你也可以从 GET 参数中获取年份
  545. $year = date('Y'); // 你可以修改为 $_GET['year'] 如果你有年份参数
  546. // 创建 DateTime 对象表示该月的第一天
  547. $firstDayOfMonth = \DateTime::createFromFormat('!Y-m-d', "$year-$month-01");
  548. // 获取该月第一天的时间戳
  549. $startTimeStamp = $firstDayOfMonth->getTimestamp();
  550. // 克隆 DateTime 对象并修改到下个月的第一天,然后减去一秒得到该月的最后一天的时间戳
  551. $lastDayOfMonth = clone $firstDayOfMonth;
  552. $lastDayOfMonth->modify('+1 month -1 second');
  553. $endTimeStamp = $lastDayOfMonth->getTimestamp();
  554. // 输出开始时间和结束时间戳
  555. echo "Start Timestamp: $startTimeStamp\n";
  556. echo "End Timestamp: $endTimeStamp\n";
  557. $startdate = strtotime($startTimeStamp);
  558. $enddate = strtotime($endTimeStamp);
  559. $start_time = date('Y-m-d H:i:s', $startdate);
  560. $end_time = date('Y-m-d H:i:s', $enddate + 86400);
  561. $where[] = ['addtime', '>=', $start_time];
  562. $where[] = ['addtime', '<', $end_time];
  563. }
  564. $count = Customer::where($where)->count();
  565. if ($count == 0) {
  566. return json(['code' => 1, 'msg' => '没有可导出的数据']);
  567. }
  568. $res = Customer::with(['User', 'employee', 'org'])
  569. ->where($where)
  570. ->order('revisit_time desc,id desc')
  571. ->visible(['id', 'state', 'revisit_time', 'name', 'User.headimgurl', 'sex', 'community_name', 'level', 'phone','employee.name', 'employee_id', 'org_name']
  572. )->order('id desc')
  573. ->select()
  574. ->toArray();
  575. echo Customer::getLastSql();
  576. $new = [];
  577. // foreach ($res as $key => $val) {
  578. // $new[] = [
  579. // 'name' => $val['name'],
  580. // 'phone' => $val['phone'],
  581. // 'community_name' => $val['community_name'],
  582. // ];
  583. // }
  584. $header = [
  585. "A1" => "客户姓名",
  586. "B1" => "手机号",
  587. "C1" => "小区名称",
  588. "D1" => "所属员工",
  589. "E1" => "客户等级",
  590. "F1" => '来源渠道',
  591. "G1" => '面积',
  592. "H1" => '设计师',
  593. "I1" => '交房时间',
  594. "J1" => '注销时间',
  595. "K1" => '报备时间',
  596. "L1" => '状态',
  597. "M1" => '首次到店'];
  598. $fileName = date('Y-m-d H:i:s') . $month ? $month.'月': ''.'无效客户';
  599. return json(['code' => 0, 'data' => $res, 'count' => $count]);
  600. // $this->getExport($header, true, $new, $fileName);
  601. }
  602. // 导出
  603. public function getExport($header = [], $type = false, $data = [], $fileName)
  604. {
  605. // 实例化类
  606. $preadsheet = new Spreadsheet();
  607. // 创建sheet
  608. $sheet = $preadsheet->getActiveSheet();
  609. // 循环设置表头数据
  610. foreach ($header as $k => $v) {
  611. $sheet->setCellValue($k, $v);
  612. }
  613. // 生成数据
  614. $sheet->fromArray($data, null, "A2");
  615. // 样式设置
  616. $sheet->getDefaultColumnDimension()->setWidth(12);
  617. // 设置下载与后缀
  618. if ($type) {
  619. header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  620. $type = "Xlsx";
  621. $suffix = "xlsx";
  622. } else {
  623. header("Content-Type:application/vnd.ms-excel");
  624. $type = "Xls";
  625. $suffix = "xls";
  626. }
  627. ob_end_clean(); //清楚缓存区
  628. // 激活浏览器窗口
  629. header("Content-Disposition:attachment;filename=$fileName.$suffix");
  630. //缓存控制
  631. header("Cache-Control:max-age=0");
  632. // 调用方法执行下载
  633. $writer = IOFactory::createWriter($preadsheet, $type);
  634. // 数据流
  635. $writer->save("php://output");
  636. }
  637. /**
  638. * 客户线索
  639. */
  640. public function clue()
  641. {
  642. $request = request();
  643. if (!$request->isAjax()) {
  644. // 部门
  645. $org = OrgLogic::struc($request->employee->root_id);
  646. View::assign('org', $org);
  647. $orgids = orgSubIds(request()->employee->root_id);
  648. View::assign('orgids', json_encode($orgids));
  649. View::assign('empid', $request->employee->id);
  650. // View::assign('manager', $request->employee->is_manager);
  651. return View::fetch();
  652. }
  653. $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state', 'phone', 'time']);
  654. $root_id = request()->employee->root_id;
  655. $orgSub = orgSubIds($root_id);
  656. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  657. $orgids = orgSubIds($param['org']);
  658. } else {
  659. $orgids = orgSubIds($root_id);
  660. }
  661. $condition = [['org_id', 'in', $orgids]];
  662. $order = isset($param['order']) ? $param['order'] : 'id desc';
  663. if (!empty($param['phone'])) {
  664. if ($param['phone'] == 1) {
  665. $condition[] = ['phone', '<>', ''];
  666. } elseif ($param['phone'] == 2) {
  667. $condition[] = ['phone', '=', null];
  668. }
  669. }
  670. if (!empty($param['time'])) {
  671. list($startDate, $endDate) = explode(' - ', $param['time']);
  672. $endTime = strtotime($endDate) + 86400;
  673. $condition[] = ['addtime', 'between', [$startDate . ' 00:00:00', date('Y-m-d H:i:s', $endTime)]];
  674. }
  675. // $orgids = orgSubIds(request()->employee->org_id);
  676. // if ($request->employee->is_manager) {
  677. // if (!empty($param['org']) && in_array($param['org'], $orgids)) {
  678. // $orgids = orgSubIds($param['org']);
  679. // $condition[] = ['org_id', 'in', $orgids];
  680. // } else {
  681. // $condition[] = ['org_id', 'in', $orgids];
  682. // //$condition[] = ['org_id', 'in', $request->org];
  683. // }
  684. // if (isset($param['emp_id'])) {
  685. // $condition[] = ['employee_id', '=', $param['emp_id']];
  686. // }
  687. setCondition($param, ['emp_id', 'employee_id'], '=', $condition);
  688. // } else {
  689. // $condition[] = ['employee_id', '=', $request->employee->id];
  690. // }
  691. setCondition($param, 'state', '=', $condition);
  692. $data = ClueLogic::list($condition, $param['page'], $param['limit'], $order);
  693. $count = ClueLogic::count($condition);
  694. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  695. }
  696. /**
  697. * 客户跟进
  698. */
  699. public function tracking()
  700. {
  701. $request = request();
  702. $id = $request->param('id');
  703. $orgids = orgSubIds(request()->employee->root_id);
  704. $customer = Customer::where([['id', '=', $id], ['org_id', 'in', $orgids]])->field('revisit_time')->find();
  705. if (!$request->isAjax()) {
  706. View::assign('revisit_time', str_replace('00:00:00', '', $customer->revisit_time));
  707. View::assign('id', $id);
  708. return View::fetch();
  709. }
  710. $page = $request->param('page', 1);
  711. $limit = $request->param('limit', 10);
  712. $data = CustomerVisitLog::with(['employee'])->where('customer_id', $id)->page($page, $limit)->order('addtime desc')->select();
  713. $count = CustomerVisitLog::where('customer_id', $id)->count();
  714. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  715. }
  716. /**
  717. * 客户展示足迹
  718. */
  719. public function foot()
  720. {
  721. $request = request();
  722. $root_id = request()->employee->root_id;
  723. $subOrg = orgSubIds($root_id);
  724. if (!$request->isAjax()) {
  725. $id = $request->param('id');
  726. $data = Customer::find($id);
  727. if (in_array($data['org_id'], $subOrg)) {
  728. View::assign('uid', $data['uid']);
  729. } else {
  730. View::assign('uid', '');
  731. }
  732. return View::fetch();
  733. }
  734. $uid = $request->param('uid');
  735. $param = $request->only(['customer', 'page', 'limit']);
  736. $data = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->order('addtime desc')->page($param['page'], $param['limit'])->select();
  737. $count = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->count();
  738. foreach ($data as $k => &$v) {
  739. $v['visit_due_time'] = $v['visit_due_time'] ?: 0;
  740. }
  741. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  742. }
  743. /**
  744. * 线索展示足迹
  745. */
  746. public function cluefoot()
  747. {
  748. $request = request();
  749. $root_id = request()->employee->root_id;
  750. $subOrg = orgSubIds($root_id);
  751. if (!$request->isAjax()) {
  752. $id = $request->param('id');
  753. $data = CustomerClue::find($id);
  754. if (in_array($data['org_id'], $subOrg)) {
  755. View::assign('uid', $data['uid']);
  756. } else {
  757. View::assign('uid', '');
  758. }
  759. View::assign('uid', $data['uid']);
  760. return View::fetch();
  761. }
  762. $uid = $request->param('uid');
  763. $param = $request->only(['customer', 'page', 'limit']);
  764. $data = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->page($param['page'], $param['limit'])->select();
  765. $count = Footprints::where([['uid', '=', $uid], ['org_id', 'in', $subOrg]])->count();
  766. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  767. }
  768. /**
  769. * 线索无效
  770. */
  771. public function invalidClue($id)
  772. {
  773. $root_id = request()->employee->root_id;
  774. $subOrg = orgSubIds($root_id);
  775. $result = CustomerClue::where([['id', '=', $id], ['org_id', 'in', $subOrg]])->update(['state' => 2]);
  776. if ($result !== false) {
  777. return json(['code' => 0, 'msg' => '操作成功']);
  778. } else {
  779. return json(['code' => 1, 'msg' => '操作失败']);
  780. }
  781. }
  782. /**
  783. * 导入前日志存储
  784. */
  785. public function importLog()
  786. {
  787. $request = request();
  788. $param = $request->only(['file_name', 'source', 'file']);
  789. $logData = [
  790. 'name' => $param['file_name'],
  791. 'employee_id' => $request->employee->id,
  792. 'org_id' => $request->employee->root_id,
  793. 'count' => 0,
  794. 'avaliable_count' => 0,
  795. 'bad_phone_num' => 0,
  796. 'left_num' => 0,
  797. 'price' => '',
  798. 'source' => $param['source'],
  799. 'repeat_phone_number' => 0,
  800. 'path'=> $param['file'],
  801. 'root_id'=> $request->employee->root_id
  802. ];
  803. $data = CrmImportLog::create($logData);
  804. if ($data == false) {
  805. return json(['code' => 1, 'msg' => '添加失败']);
  806. }
  807. $data->status = 1;
  808. $data->save();
  809. $jobHandlerClassName = 'app\jobs\CrmImport';
  810. $jobQueueName = 'crm_import';
  811. $orderData = ['id'=> $data->id]; //这个是需要传到消费者的数据
  812. Queue::later(0, $jobHandlerClassName, $orderData, $jobQueueName);
  813. return json(['code' => 0, 'msg' => '正在导入中,请刷新列表查看']);
  814. }
  815. /**
  816. * 客户excel添加
  817. */
  818. public function add()
  819. {
  820. $request = request();
  821. $orgIds = orgSubIds($request->employee->root_id);
  822. $logId = $request->param('log_id');
  823. $sourceId = $request->param('source_id');
  824. $log = CrmImportLog::where('id', $logId)->find();
  825. if (empty($log) || !in_array($log->org_id, $orgIds)) return json(['code' => 1, 'msg' => '请求错误,记录不存在']);
  826. $orgIds = orgSubIds($request->employee->root_id);
  827. $datas = $request->param('customers');
  828. if (count($datas) == 0) return json(['code' => 1, 'msg' => '请求错误,未传输数据']);
  829. //客户来源扩展字段 $datass
  830. $ext_where = [
  831. ['root_id', '=', $request->employee->root_id],
  832. ['keyname', '=', 'source_id']
  833. ];
  834. $ext = [];
  835. $source_field_id = CustomerPortraitField::where($ext_where)->value('id');
  836. if($source_field_id) $ext[] = ['id' => (string)$source_field_id, 'keyname' => 'source_id', 'value' => $sourceId];
  837. $unit_number_id = CustomerPortraitField::where([['root_id','=',$request->employee->root_id],['keyname','=','unit_number']])->find();
  838. $house_location = CustomerPortraitField::where([['root_id','=',$request->employee->root_id],['keyname','=','house_location']])->find();
  839. //error 手机号错误数量 repeat 手机号重复数量
  840. $rs = ['ok' => 0, 'error' => 0, 'repeat' => 0];
  841. $phones = [];
  842. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  843. foreach ($datas as $k => &$data) {
  844. $data['phone1'] = $data['phone2'] = '';
  845. ////2023-02-15 修改手机号可以输入多个
  846. $s = true;
  847. $v_phone = [];
  848. $ls_phone = explode('、',$data['phone']);
  849. if(!empty($ls_phone) && is_array($ls_phone)){
  850. $p = 0;
  851. foreach ($ls_phone as $k2 => $v2) {
  852. if ($this->checkphone($v2)) {
  853. $field = $p ? 'phone'.$p : 'phone';
  854. $data[$field] = $aec->encrypt($v2);
  855. $p+=1;
  856. $v_phone[] = $data[$field];
  857. }
  858. if($p>2) break;//只取三个有效手机号
  859. }
  860. if($p>0) $s = false;
  861. }
  862. if($s){
  863. unset($datas[$k]);
  864. $rs['error']++;
  865. continue;
  866. }
  867. // 判断重复
  868. $intersect = array_intersect($v_phone, $phones);
  869. if (!empty($intersect)) {
  870. unset($datas[$k]);
  871. $rs['repeat']++;
  872. continue;
  873. } else {
  874. $phones = array_merge($phones, $v_phone);
  875. }
  876. // 数据处理
  877. $data['house_type'] = mb_strlen($data['house_type']) > 20 ? mb_substr($data['house_type'],0,20) : trim($data['house_type']);
  878. $data['age_range'] = mb_strlen($data['age_range']) > 5 ? mb_substr($data['age_range'],0,5) : trim($data['age_range']);
  879. $data['name'] = mb_strlen($data['name']) > 20 ? mb_substr($data['name'],0,20) : trim($data['name']);
  880. $data['crm_res_id'] = $log->id;
  881. $data['state'] = 0;
  882. $data['sex'] = ($data['sex'] == '男') ? 1 : 2;
  883. $data['org_id'] = $request->employee->root_id;
  884. $data['is_resource'] = 1;
  885. $data['bad_phone'] = 0;
  886. $data['source_id'] = $sourceId;
  887. $ext1 = $ext;
  888. if ($data['unit_number'] && !$unit_number_id->isEmpty()) $ext1[] = ['id' => (string)$unit_number_id->id, 'keyname' => 'unit_number', 'value' => $data['unit_number']];
  889. if ($data['house_location'] && !$house_location->isEmpty()) $ext1[] = ['id' => (string)$house_location->id, 'keyname' => 'house_location', 'value' => $data['house_location']];
  890. $data['ext'] = $ext1 ? json_encode($ext1) : NULL;
  891. unset($data['unit_number']);
  892. unset($data['house_location']);
  893. $data['level'] = in_array($data['level'], ['A','B','C','D']) ? trim($data['level']) : null;
  894. $rs['ok']++;
  895. }
  896. Db::startTrans();
  897. try {
  898. Customer::insertAll($datas);
  899. $log->repeat_phone_number += $rs['repeat'];
  900. $log->bad_phone_num -= $rs['ok'];
  901. $log->avaliable_count += $rs['ok'];
  902. $log->left_num += $rs['ok'];
  903. $log->save();
  904. Db::commit();
  905. } catch (\Exception $e) {
  906. trace($e->getMessage(), 'error');
  907. // 回滚事务
  908. Db::rollback();
  909. return json(['code' => 1, 'msg' => $e->getMessage()]);
  910. }
  911. return json(['code' => 0, 'msg' => '保存成功', 'data' => $rs]);
  912. }
  913. public function editing($id)
  914. {
  915. /* $condition = ['root_id' => request()->employee->root_id];
  916. //小区名称获取
  917. $communities = Community::where($condition)->where([['type', '=', 1]])->select()->toArray();
  918. View::assign('communities', $communities);
  919. //风格获取
  920. $decostyles = Decostyle::where($condition)->where([['type', '=', 1]])->select()->toArray();
  921. View::assign('decostyles', $decostyles);
  922. //关注点产品获取
  923. $products = Product::where($condition)->select()->toArray();
  924. View::assign('products', $products);
  925. //来源获取
  926. $sources = CustomerSource::where($condition)->select()->toArray();
  927. View::assign('sources', $sources);
  928. $customer = Customer::where('id', $id)->with(['source'])->find()->toArray();
  929. $customer['community_id'] = Community::where(['name' => $customer['community_name'], 'root_id' => request()->employee->root_id])->where([['type', '=', 1]])->column('id');
  930. $customer['style_id'] = Decostyle::where(['name' => $customer['deco_style'], 'root_id' => request()->employee->root_id])->where([['type', '=', 1]])->column('id');
  931. $customer['product_id'] = Product::where(['product' => $customer['product'], 'root_id' => request()->employee->root_id])->column('id');
  932. $customer['source'] = CustomerSource::where(['id' => $customer['source_id'], 'root_id' => request()->employee->root_id])->column('source');
  933. $customer['revisit_time'] = $customer['revisit_time'] ? date('Y-m-d', strtotime($customer['revisit_time'])) : '';
  934. $customer['before_pool'] = NULL;
  935. View::assign('customer', json_encode($customer));
  936. View::assign('customer_id', $id); */
  937. return View::fetch();
  938. }
  939. public function prefileaddingcheck()
  940. {
  941. $request = request();
  942. $clueid = $request->param('clueid');
  943. $empid = $request->param('empid');
  944. $orgids = orgSubIds(request()->employee->root_id);
  945. $where[] = ['org_id', 'in', $orgids];
  946. $clue = CustomerClue::find($clueid);
  947. $where[] = ['uid', '=', $clue->uid];
  948. $where[] = ['state', '=', 1];
  949. $where[] = ['id', '<>', $clueid];
  950. $fileadded = CustomerClue::where($where)->find();
  951. if ($fileadded && $fileadded->employee_id != $empid) {
  952. return json(['code' => 1, 'msg' => '不可建档,此客户此前已建档']);
  953. } else {
  954. return json(['code' => 0, 'msg' => '']);
  955. }
  956. }
  957. public function fileadding($empid, $clueid)
  958. {
  959. $condition = ['root_id' => request()->employee->root_id];
  960. //小区名称获取
  961. $communities = Community::where($condition)->where([['type', '=', 1]])->select()->toArray();
  962. View::assign('communities', $communities);
  963. //风格获取
  964. $decostyles = Decostyle::where($condition)->where([['type', '=', 1]])->select()->toArray();
  965. View::assign('decostyles', $decostyles);
  966. //关注点产品获取
  967. $products = Product::where($condition)->select()->toArray();
  968. View::assign('products', $products);
  969. //来源获取
  970. $sources = CustomerSource::where($condition)->select()->toArray();
  971. View::assign('sources', $sources);
  972. View::assign('employee_id', $empid);
  973. $root_id = request()->employee->root_id;
  974. $subOrg = orgSubIds($root_id);
  975. $clue = CustomerClue::where([['id', '=', $clueid], ['org_id', 'in', $subOrg]])->with('user')->findOrEmpty();
  976. if ($clue->isEmpty()) {
  977. $j_clue = [];
  978. } else {
  979. $j_clue = json_encode($clue->toArray());
  980. }
  981. View::assign('clue', $j_clue);
  982. View::assign('uid', $clue['uid']);
  983. View::assign('clue_id', $clueid);
  984. $footprint = Footprints::where(['pipe_type' => $clue['pipe_type'], 'id' => $clue['pipe_id']])->findOrEmpty();
  985. $thefrom = '';
  986. if (!$footprint->isEmpty()) {
  987. $thefrom = $footprint->reg_info;
  988. }
  989. View::assign('frompipe', $thefrom);
  990. return View::fetch();
  991. }
  992. public function choiceadding()
  993. {
  994. $type = input('type');
  995. if ($type == 'product') {
  996. return View::fetch('productadding');
  997. }
  998. if ($type == 'source') {
  999. return View::fetch('sourceadding');
  1000. }
  1001. }
  1002. /*
  1003. * 产品添加
  1004. */
  1005. public function apiproductadding()
  1006. {
  1007. $param = input();
  1008. $condition = [
  1009. 'root_id' => request()->employee->root_id
  1010. ];
  1011. if (isset($param['id'])) {
  1012. $condition['id'] = $param['id'];
  1013. } else {
  1014. $condition['product'] = $param['product'];
  1015. }
  1016. $exitedobj = Product::where($condition)->findOrEmpty();
  1017. if (empty($param['id']) && !$exitedobj->isEmpty()) {
  1018. return json(['code' => 1, 'msg' => '产品已存在']);
  1019. }
  1020. $exitedobj->save([
  1021. 'root_id' => request()->employee->root_id,
  1022. 'product' => $param['product']
  1023. ]);
  1024. return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $exitedobj->id, 'name' => $exitedobj->product]]);
  1025. }
  1026. public function apisourceadding()
  1027. {
  1028. $param = input();
  1029. $condition = [
  1030. 'root_id' => request()->employee->root_id
  1031. ];
  1032. if (isset($param['id'])) {
  1033. $condition['id'] = $param['id'];
  1034. } else {
  1035. $condition['source'] = $param['source'];
  1036. }
  1037. $exitedobj = CustomerSource::where($condition)->findOrEmpty();
  1038. if (empty($param['id']) && !$exitedobj->isEmpty()) {
  1039. return json(['code' => 1, 'msg' => '客户来源已存在']);
  1040. }
  1041. $exitedobj->save([
  1042. 'root_id' => request()->employee->root_id,
  1043. 'source' => $param['source']
  1044. ]);
  1045. return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $exitedobj->id, 'name' => $exitedobj->source]]);
  1046. }
  1047. //
  1048. public function apicustomeradding()
  1049. {
  1050. $data = input();
  1051. $community = Community::where(['id' => $data['community_id'], 'root_id' => request()->employee->root_id])->find();
  1052. // $data['community_name'] = ($community) ? $community->name : '';
  1053. $decostyle = Decostyle::where(['id' => $data['style_id'], 'root_id' => request()->employee->root_id])->find();
  1054. $data['deco_style'] = ($decostyle) ? $decostyle->name : '';
  1055. // $data['product'] = Product::where(['id' => $data['product_id'], 'root_id' => request()->employee->root_id])->column('product');
  1056. $data['source_id'] = ($data['source_id'] == 0) ? null : $data['source_id'];
  1057. //
  1058. unset($data['community_id']);
  1059. unset($data['style_id']);
  1060. unset($data['product_id']);
  1061. //
  1062. $data['employee_id'] = request()->employee->id;
  1063. $data['org_id'] = request()->employee->org_id;
  1064. //首次到访时间小于上次沟通时间小于下次回访时间
  1065. /*if ($data['first_visit_date'] > $data['last_contact_date']) {
  1066. return json(['code' => 1, 'msg' => '首次到访时间要小于上次沟通时间']);
  1067. }
  1068. if ($data['last_contact_date'] > $data['revisit_time']) {
  1069. return json(['code' => 1, 'msg' => '上次沟通时间要小于回访时间']);
  1070. }
  1071. $data['first_visit_date'] = ($data['first_visit_date'] == '')?NULL:$data['first_visit_date'];
  1072. $data['last_contact_date'] = ($data['last_contact_date'] == '')?NULL:$data['last_contact_date'];*/
  1073. $data['revisit_time'] = ($data['revisit_time'] == '') ? NULL : $data['revisit_time'];
  1074. //本公司客户录入唯一
  1075. $orgids = orgSubIds(request()->employee->root_id);
  1076. $condition[] = ['phone', '=', cypherphone(trim($data['phone']))];
  1077. $condition[] = ['org_id', 'in', $orgids];
  1078. $the_customer_by_other = Customer::where($condition)->find();
  1079. if ($the_customer_by_other && $the_customer_by_other->employee_id) {
  1080. $emp = Employee::find($the_customer_by_other->employee_id);
  1081. return json(['code' => 2, 'msg' => '录入失败。' . $data['phone'] . '为 ' . $emp->name . ' 的客户。已于' . $the_customer_by_other->addtime . '录入。']);
  1082. }
  1083. //
  1084. $the_customer = Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->find();
  1085. if ($the_customer) {
  1086. // Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->update($data);
  1087. return json(['code' => 1, 'msg' => '客户已存在']);
  1088. } else {
  1089. $obj = new Customer();
  1090. if ($obj->save($data)) {
  1091. if (isset($data['clue_id'])) {
  1092. CustomerClue::where('id', $data['clue_id'])->update(['state' => 1]);
  1093. }
  1094. return json(['code' => 0, 'msg' => '保存成功']);
  1095. } else {
  1096. return json(['code' => 1, 'msg' => '保存失败']);
  1097. }
  1098. }
  1099. }
  1100. public function apicustomerediting()
  1101. {
  1102. $data = input();
  1103. //本公司客户录入唯一
  1104. $orgids = orgSubIds(request()->employee->root_id);
  1105. $condition[] = ['phone', '=', cypherphone(trim($data['phone']))];
  1106. $condition[] = ['org_id', 'in', $orgids];
  1107. $condition[] = ['id', '<>', $data['id']];
  1108. $the_customer_by_other = Customer::where($condition)->find();
  1109. if ($the_customer_by_other && $the_customer_by_other->employee_id) {
  1110. $emp = Employee::find($the_customer_by_other->employee_id);
  1111. return json(['code' => 2, 'msg' => '录入失败。' . $data['phone'] . '为 ' . $emp->name . ' 的客户。已于' . $the_customer_by_other->addtime . '录入。']);
  1112. }
  1113. ///
  1114. $community = Community::where(['id' => $data['community_id'], 'root_id' => request()->employee->root_id])->find();
  1115. // $data['community_name'] = ($community) ? $community->name : '';
  1116. $decostyle = Decostyle::where(['id' => $data['style_id'], 'root_id' => request()->employee->root_id])->find();
  1117. $data['deco_style'] = ($decostyle) ? $decostyle->name : '';
  1118. // $data['product'] = Product::where(['id' => $data['product_id'], 'root_id' => request()->employee->root_id])->column('product');
  1119. // $data['square'] = $data['square'] . '平方';
  1120. //
  1121. unset($data['community_id']);
  1122. unset($data['style_id']);
  1123. unset($data['product_id']);
  1124. if ($data['source_id'] === 'null') unset($data['source_id']);
  1125. //$data['first_visit_date'] = ($data['first_visit_date'] == '')?NULL:$data['first_visit_date'];
  1126. //$data['last_contact_date'] = ($data['last_contact_date'] == '')?NULL:$data['last_contact_date'];
  1127. $data['revisit_time'] = ($data['revisit_time'] == '') ? NULL : $data['revisit_time'];
  1128. if ($data['state'] == '无效') {
  1129. $data['employee_id'] = NULL;
  1130. $date['is_resource'] = 0;
  1131. }
  1132. $customer = Customer::where(['id' => $data['id']])->find();
  1133. if ($customer) {
  1134. $customer->save($data);
  1135. return json(['code' => 0, 'msg' => '保存成功']);
  1136. } else {
  1137. return json(['code' => 1, 'msg' => '保存失败']);
  1138. }
  1139. }
  1140. public function apicustomerfileadding()
  1141. {
  1142. $data = input();
  1143. //首次到访时间小于上次沟通时间小于下次回访时间
  1144. if ($data['first_visit_date'] > $data['last_contact_date']) {
  1145. return json(['code' => 1, 'msg' => '首次到访时间要小于上次沟通时间']);
  1146. }
  1147. if ($data['last_contact_date'] > $data['revisit_time']) {
  1148. return json(['code' => 1, 'msg' => '上次沟通时间要小于回访时间']);
  1149. }
  1150. $community = Community::where(['id' => $data['community_id'], 'root_id' => request()->employee->root_id])->find();
  1151. $data['community_name'] = ($community) ? $community->name : '';
  1152. $decostyle = Decostyle::where(['id' => $data['style_id'], 'root_id' => request()->employee->root_id])->find();
  1153. $data['deco_style'] = ($decostyle) ? $decostyle->name : '';
  1154. $data['product'] = NULL;
  1155. $data['square'] = $data['square'] . '平方';
  1156. $data['source_id'] = NULL;
  1157. //
  1158. unset($data['community_id']);
  1159. unset($data['style_id']);
  1160. unset($data['product_id']);
  1161. //
  1162. $data['employee_id'] = request()->employee_id;
  1163. $data['org_id'] = request()->employee->org_id;
  1164. $data['first_visit_date'] = ($data['first_visit_date'] == '') ? NULL : $data['first_visit_date'];
  1165. $data['last_contact_date'] = ($data['last_contact_date'] == '') ? NULL : $data['last_contact_date'];
  1166. $data['revisit_time'] = ($data['revisit_time'] == '') ? NULL : $data['revisit_time'];
  1167. //
  1168. $the_customer = Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->find();
  1169. if ($the_customer) {
  1170. Customer::where(['employee_id' => $data['employee_id'], 'phone' => trim($data['phone'])])->update($data);
  1171. } else {
  1172. $obj = new Customer();
  1173. if ($obj->save($data)) {
  1174. if (isset($data['clue_id'])) {
  1175. CustomerClue::where('id', $data['clue_id'])->update(['state' => 1]);
  1176. }
  1177. return json(['code' => 0, 'msg' => '保存成功']);
  1178. } else {
  1179. return json(['code' => 1, 'msg' => '保存失败']);
  1180. }
  1181. }
  1182. }
  1183. ///////////////////////////////
  1184. /// Tracking
  1185. /// //////////////////////////
  1186. public function addtracking()
  1187. {
  1188. $request = request();
  1189. $id = $request->param('id');
  1190. View::assign('id', $id);
  1191. View::assign('empid', $request->employee->id);
  1192. return View::fetch();
  1193. }
  1194. //public function apitrackingadding()
  1195. //{
  1196. // $data = input();
  1197. // if(isset($data['revisit_time'])){
  1198. // Customer::where('id',$data['customer_id'])->update(['revisit_time' => $data['revisit_time']]);
  1199. // }
  1200. //
  1201. // $obj = new Tracking();
  1202. // if ($obj->save($data)) {
  1203. // return json(['code' => 0, 'msg' => '保存成功', 'data' => ['id' => $obj->id]]);
  1204. // }
  1205. //}
  1206. public function apitrackingadding()
  1207. {
  1208. $request = request();
  1209. $param = $request->only(['customer_id', 'type', 'next_contact_date', 'remark', 'img']);
  1210. $param['employee_id'] = $request->employee->id;
  1211. $param['user_id'] = $request->employee->uid;
  1212. !empty($param['next_contact_date']) ?: $param['next_contact_date'] = date('Y-m-d');
  1213. $root_id = request()->employee->root_id;
  1214. $subOrg = orgSubIds($root_id);
  1215. $condition = [
  1216. ['id', '=', $param['customer_id']],
  1217. ['state', 'not in', Customer::changeState('无效', 'chaos')],
  1218. ['died', '<>', 2],
  1219. ['org_id', 'in', $subOrg]
  1220. ];
  1221. $had = Customer::where($condition)->find();
  1222. if (!$had) {
  1223. return json(['code' => 0, 'msg' => '添加失败,数据不存在']);
  1224. }
  1225. //$state = $request->param('state');
  1226. //$param['state'] = $state;
  1227. //根据跟进类型5678,设置保护时间 56类型为arrived到访 7类型为measured量房 8类型为deposited定金
  1228. $param['protected_to'] = NULL;
  1229. /*if (in_array($param['type'], [5, 6, 7, 8])) {
  1230. switch ($param['type']) {
  1231. case 5:
  1232. $protect_days_num = pubpoolSetting('arrived');
  1233. break;
  1234. case 6:
  1235. $protect_days_num = pubpoolSetting('arrived');
  1236. break;
  1237. case 7:
  1238. $protect_days_num = pubpoolSetting('measured');
  1239. break;
  1240. case 8:
  1241. $protect_days_num = pubpoolSetting('deposited');
  1242. break;
  1243. }
  1244. $time_til = time() + $protect_days_num * 86400;
  1245. $had->protected_to = date('Y-m-d H:i:s', $time_til);
  1246. $param['protected_to'] = date('Y-m-d H:i:s', $time_til);
  1247. }*/
  1248. ////
  1249. CustomerVisitLog::create($param);
  1250. //$had->state = $state;
  1251. $had->revisit_time = $param['next_contact_date'];
  1252. $had->save();
  1253. return json(['code' => 0, 'msg' => '添加成功', 'revisit_time' => $param['next_contact_date']]);
  1254. }
  1255. ///////////////////////////////
  1256. /// Pool 公海
  1257. /// //////////////////////////
  1258. public function pool()
  1259. {
  1260. $request = request();
  1261. if (!$request->isAjax()) {
  1262. //公海
  1263. $pool = PoolLogic::struc();
  1264. View::assign('pool', $pool);
  1265. // 部门
  1266. $org = OrgLogic::struc(request()->employee->root_id);
  1267. View::assign('org', $org);
  1268. $orgids = orgSubIds(request()->employee->root_id);
  1269. View::assign('orgids', json_encode($orgids));
  1270. View::assign('empid', $request->employee->id);
  1271. // View::assign('manager', $request->employee->is_manager);
  1272. return View::fetch();
  1273. }
  1274. $param = $request->only(['page', 'limit', 'order', 'pool']);
  1275. $condition = [];
  1276. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1277. $condition[] = ['employee_id', '=', NULL];
  1278. $condition[] = ['is_resource', '=', 0];
  1279. $root_id = request()->employee->root_id;
  1280. $orgSub = orgSubIds($root_id);
  1281. if (isset($param['pool']) && in_array($param['pool'], $orgSub)) {
  1282. //$pool_obj = Pool::find($param['pool']);
  1283. $condition[] = ['org_id', '=', $param['pool']];
  1284. } else {
  1285. $orgids = orgSubIds($request->employee->root_id);
  1286. $condition[] = ['org_id', 'in', $orgids];
  1287. }
  1288. $data = CustomerLogic::poollist($condition, $param['page'], $param['limit'], $order);
  1289. $count = CustomerLogic::count($condition);
  1290. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1291. }
  1292. public function addpool()
  1293. {
  1294. $request = request();
  1295. if (!$request->isAjax()) {
  1296. // 部门层级level
  1297. $level = OrgLogic::level();
  1298. View::assign('level', $level);
  1299. return View::fetch();
  1300. }
  1301. $param = $request->param();
  1302. unset($param['checkbox']);
  1303. $nodeArr = explode(',', $param['nodes']);
  1304. foreach ($nodeArr as $orgid) {
  1305. $orgobj = Org::find($orgid);
  1306. $poolname = $orgobj->name . ' ' . $param['name'] . ' 公海';
  1307. PoolLogic::addpool($poolname, $orgid, $param['level']);
  1308. }
  1309. return json(['code' => 0, 'msg' => '保存成功']);
  1310. }
  1311. public function apilevelnodes()
  1312. {
  1313. $request = request();
  1314. $param = $request->only(['level']);
  1315. $nodes = OrgLogic::levelnodes($param['level']);
  1316. return json(['code' => 0, 'data' => $nodes, 'msg' => '获取成功']);
  1317. }
  1318. ///
  1319. public function apisubnodes()
  1320. {
  1321. $request = request();
  1322. $param = $request->param();
  1323. $nodes = [];
  1324. if ($param['org'] != 0) {
  1325. //$nodes = OrgLogic::subnodes($param['org']);
  1326. $nodes = OrgLogic::subfirstlevelnodes($param['org']);
  1327. }
  1328. return json(['code' => 0, 'data' => $nodes, 'msg' => '获取成功']);
  1329. }
  1330. ///
  1331. public function apisubnodeswithempnum()
  1332. {
  1333. $request = request();
  1334. $param = $request->param();
  1335. $nodes = [];
  1336. if ($param['org'] != 0) {
  1337. $nodes = OrgLogic::subfirstlevelnodeswithempnum($param['org']);
  1338. }
  1339. return json(['code' => 0, 'data' => $nodes, 'msg' => '获取成功']);
  1340. }
  1341. ///
  1342. public function distribute()
  1343. {
  1344. $request = request();
  1345. $param = $request->param();
  1346. $targetstr = substr($param['data'], 0, strlen($param['data']) - 1);
  1347. View::assign('targetstr', $targetstr);
  1348. //公海
  1349. $pool = PoolLogic::struc();
  1350. View::assign('pool', $pool);
  1351. // 部门
  1352. $org = OrgLogic::struc(request()->employee->root_id);
  1353. View::assign('org', $org);
  1354. $orgids = orgSubIds(request()->employee->root_id);
  1355. View::assign('orgids', json_encode($orgids));
  1356. return View::fetch();
  1357. }
  1358. /**
  1359. * 公海管理分配客户
  1360. */
  1361. public function distributing()
  1362. {
  1363. $request = request();
  1364. $param = $request->param();
  1365. $target_customer_ids_arr = explode(',', $param['targets']);
  1366. $root_id = request()->employee->root_id;
  1367. $subOrg = orgSubIds($root_id);
  1368. $target_customer_ids_arr = Customer::where([['id', 'in', $target_customer_ids_arr], ['org_id', 'in', $subOrg]])->column('id');
  1369. if ($param['type'] == 'toemp' && isset($param['emp_id'])) {
  1370. $employee_id = $param['emp_id'];
  1371. $emp_obj = Employee::find($employee_id);
  1372. $org_id = $emp_obj->org_id;
  1373. // 设计师能否报备、获取客户
  1374. $de_where[] = ['root_id', '=', $root_id];
  1375. $de_where[] = ['name', '=', 'designer_get_customer'];
  1376. $info_de_where = Setting::where($de_where)->findOrEmpty();
  1377. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  1378. if (!$info_de_where->isEmpty() && $org_type == 2) {
  1379. if ((int)$info_de_where['content'] != 1) {
  1380. return json(['code' => 1, 'msg' => '无法分配给设计师']);
  1381. }
  1382. }
  1383. //分配给业务员该名下不能存在相同客户(phone、phone1、phone2)
  1384. $pool_crm = Customer::where('id', 'in', $target_customer_ids_arr)->field('name,phone,phone1,phone2')->select();
  1385. $name_str = [];
  1386. foreach ($pool_crm as $item) {
  1387. $phone_arr = [];
  1388. if (!empty($item['phone'])) $phone_arr[] = cypherphone($item['phone']);
  1389. if (!empty($item['phone1'])) $phone_arr[] = cypherphone($item['phone1']);
  1390. if (!empty($item['phone2'])) $phone_arr[] = cypherphone($item['phone2']);
  1391. $repeat_crm = Customer::where([['employee_id', '=', $param['emp_id']], ['phone|phone1|phone2', 'in', $phone_arr]])->count();
  1392. if (!empty($repeat_crm)) $name_str[] = $item['name'] ? $item['name'] : '未知';
  1393. }
  1394. if (!empty($name_str)) {
  1395. $name_str = implode(',', $name_str);
  1396. return json(['code' => 1, 'msg' => '分配失败:员工名下已存在客户[' . $name_str . ']的联系方式']);
  1397. }
  1398. } else {
  1399. $employee_id = null;
  1400. $org_id = $param['org_pool'];
  1401. }
  1402. foreach ($target_customer_ids_arr as $item_id) {
  1403. $before_pool = NULL;
  1404. if ($param['type'] == 'topool') {
  1405. $where = [
  1406. ['customer_id', '=', (int)$item_id]
  1407. ];
  1408. $last_cvl = CustomerVisitLog::where($where)->with('employee')->order('addtime desc')->find();
  1409. if ($last_cvl) {
  1410. $before_pool = [
  1411. 'name' => $last_cvl->employee->name,
  1412. 'state' => $last_cvl->state,
  1413. 'addtime' => $last_cvl->addtime
  1414. ];
  1415. } else {
  1416. $customer = Customer::find((int)$item_id);
  1417. $before_pool = [
  1418. 'name' => '系统',
  1419. 'state' => '待确认',
  1420. 'addtime' => $customer->addtime
  1421. ];
  1422. }
  1423. // add customer visit log for frontend info showing
  1424. $data['employee_id'] = request()->employee->id;
  1425. $data['user_id'] = request()->employee->uid;
  1426. $data['customer_id'] = (int)$item_id;
  1427. $data['state'] = '无效';
  1428. $data['remark'] = '系统回收';
  1429. $data['type'] = 1;
  1430. CustomerVisitLog::create($data);
  1431. }
  1432. $thisCustomer = Customer::find((int)$item_id);
  1433. if ($thisCustomer->state == '无效') {
  1434. $newstate = '待确认';
  1435. } else {
  1436. $newstate = $thisCustomer->state;
  1437. }
  1438. if (!$employee_id && !empty($thisCustomer->employee_id)) {
  1439. event(new Msg($thisCustomer->employee_id, '您有客户被回收到公海啦~快去看看吧!', 'toPool'));
  1440. }
  1441. if ($param['type'] == 'topool') {
  1442. $newdata = [
  1443. 'employee_id' => $employee_id,
  1444. 'org_id' => $org_id,
  1445. 'state' => $newstate,
  1446. 'remark' => '公海获取',
  1447. 'before_pool' => $before_pool ? json_encode($before_pool) : NULL,
  1448. 'ext->ext6' => '',
  1449. 'designer_id' => NULL,
  1450. 'employee_time' => date('Y-m-d H:i:s', time())
  1451. ];
  1452. } else {
  1453. $newdata = [
  1454. 'employee_id' => $employee_id,
  1455. 'org_id' => $org_id,
  1456. 'state' => $newstate,
  1457. 'remark' => '公海获取',
  1458. 'fresh' => 1,
  1459. 'before_pool' => $before_pool ? json_encode($before_pool) : NULL,
  1460. 'ext->ext6' => '',
  1461. 'designer_id' => NULL,
  1462. 'employee_time' => date('Y-m-d H:i:s', time())
  1463. ];
  1464. }
  1465. Customer::where(['id' => (int)$item_id])->update($newdata);
  1466. }
  1467. if ($param['type'] == 'toemp' && isset($param['emp_id'])) {
  1468. // 更新客户保护期
  1469. Customer::changeProtectedTo($target_customer_ids_arr, request()->employee->root_id);
  1470. }
  1471. if (!empty($employee_id)) {
  1472. event(new Msg($employee_id, request()->employee->name . '给您分配了一批客户资源,请前往查看', 'fromPool'));
  1473. }
  1474. return json(['code' => 0, 'msg' => '操作成功']);
  1475. }
  1476. public function withdrawl()
  1477. {
  1478. $request = request();
  1479. $param = $request->param();
  1480. $targetstr = substr($param['data'], 0, strlen($param['data']) - 1);
  1481. View::assign('targetstr', $targetstr);
  1482. //公海
  1483. $pool = PoolLogic::struc();
  1484. View::assign('pool', $pool);
  1485. // 部门
  1486. $org = OrgLogic::struc(request()->employee->root_id);
  1487. View::assign('org', $org);
  1488. $orgids = orgSubIds(request()->employee->root_id);
  1489. View::assign('orgids', json_encode($orgids));
  1490. //View::assign('orgids', json_encode($request->org));
  1491. return View::fetch();
  1492. }
  1493. /**
  1494. * 资源库导入
  1495. */
  1496. public function importing(){
  1497. $request = request();
  1498. // 页面展示
  1499. if (!$request->isPost()) {
  1500. $data = CustomerSource::field('id,source')->where(['root_id' => request()->employee->root_id])->select();
  1501. View::assign('source', $data);
  1502. $old_source = CrmImportLog::where('employee_id', request()->employee->id)->order('id desc')->value('source');
  1503. View::assign('old_source', $old_source);
  1504. return View::fetch();
  1505. }
  1506. $file = request()->file('file');
  1507. $root_id = request()->employee->root_id;
  1508. if (empty($file)) {
  1509. return json(['code' => 1, 'msg' => '请上传导入文件', 'data' => []]);
  1510. }
  1511. $source = input('source', '', 'intval');
  1512. $log['pid'] = 0;
  1513. $log['org_id'] = $root_id;
  1514. $log['employee_id'] = request()->employee->id;
  1515. $log['name'] = $file->getOriginalName();
  1516. $log['count'] = 0;
  1517. $log['avaliable_count'] = 0;
  1518. $log['bad_phone_num'] = 0;
  1519. $log['source'] = $source;
  1520. $log['repeat_phone_number'] = 0;
  1521. $log['status'] = 0;
  1522. $log['root_id'] = $root_id;
  1523. $log['path'] = Request::param('file');
  1524. $log['execute_rows'] = 0;
  1525. $log['upload_source'] = 0;
  1526. $log_id = CrmImportLog::insertGetId($log);
  1527. if (!$log_id) {
  1528. return json(['code'=> 1, 'msg'=> '导入失败']);
  1529. }
  1530. (new CrmImportLog())->save(['id'=> $log_id, 'status'=> 1]);
  1531. $jobHandlerClassName = 'app\jobs\CrmImport';
  1532. $jobQueueName = 'crm_import';
  1533. $orderData = ['id'=> $log_id]; //这个是需要传到消费者的数据
  1534. Queue::later(0, $jobHandlerClassName, $orderData, $jobQueueName);
  1535. return json(['code' => 0, 'msg' => '上传中,请手动刷新上传结果']);
  1536. }
  1537. private function checkphone($value)
  1538. {
  1539. if (preg_match("/^1[356789]\d{9}$/", $value)) {
  1540. return true;
  1541. } else {
  1542. return false;
  1543. }
  1544. }
  1545. public function resource()
  1546. {
  1547. $request = request();
  1548. if (!$request->isAjax()) {
  1549. //公海
  1550. $pool = PoolLogic::struc();
  1551. View::assign('pool', $pool);
  1552. // 部门
  1553. $org = OrgLogic::struc(request()->employee->root_id);
  1554. View::assign('org', $org);
  1555. $orgids = orgSubIds(request()->employee->root_id);
  1556. View::assign('orgids', json_encode($orgids));
  1557. View::assign('empid', $request->employee->id);
  1558. // View::assign('manager', $request->employee->is_manager);
  1559. $data = CustomerSource::field('id,source')->where(['root_id' => request()->employee->root_id])->select();
  1560. View::assign('source', $data);
  1561. //导入人员只显示有记录的人员
  1562. $org_ids = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('id');
  1563. $ids = CrmImportLog::where([['org_id', 'in', $org_ids]])->group('employee_id')->column('employee_id');
  1564. $person = Employee::where([['root_id', '=', request()->employee->root_id], ['grant_id', '>', 0], ['id', 'in', $ids]])->field('opt_name,id')->select();
  1565. View::assign('person', $person);
  1566. $resourcefb = Setting::where(['root_id' => request()->employee->root_id, 'name' => 'resource_fp'])->value('content');
  1567. View::assign('resourcefb', $resourcefb === null ? 1 : (int)$resourcefb);
  1568. return View::fetch('resource_new');
  1569. }
  1570. $param = $request->only(['page', 'limit', 'org_pool', 'state' => '', 'source' => '', 'person' => '', 'start_date' => '', 'end_date' => '']);
  1571. $root_id = request()->employee->root_id;
  1572. $orgSub = orgSubIds($root_id);
  1573. if (isset($param['org_pool']) && in_array($param['org_pool'], $orgSub)) {
  1574. $org = $param['org_pool'];
  1575. } else {
  1576. $org = request()->employee->root_id;
  1577. }
  1578. //$where[] = ['org_id', '=', $org];
  1579. $orgids = orgSubIds(request()->employee->root_id);
  1580. /* $where[] = ['id', '>', 0];
  1581. $where1[] = ['org_id', 'in', $orgids];
  1582. $where1[] = ['pid', '=', 0];
  1583. $whereOr[] = ['org_id', 'in', $orgids];
  1584. $whereOr[] = ['employee_id', 'NOT NULL', null];
  1585. $whereOr[] = ['upload_source', '=', 1]; */
  1586. // 查询逻辑调整
  1587. $where[] = ['org_id', 'in', $orgids];
  1588. $where[] = ['pid', '=', 0];
  1589. //分配状态
  1590. if ($param['state'] !== '') $where[] = ['state', '=', $param['state']];
  1591. //来源渠道
  1592. if ($param['source'] !== '') $where[] = ['source', '=', $param['source']];
  1593. // 导入人员
  1594. if ($param['person'] !== '') $where[] = ['employee_id', '=', $param['person']];
  1595. //开始时间
  1596. if ($param['start_date'] !== '') $where[] = ['addtime', '>=', $param['start_date']];
  1597. //结束时间
  1598. if ($param['end_date'] !== '') $where[] = ['addtime', '<=', $param['end_date'] . ' 23:59:59'];
  1599. $data = CrmImportLog::where($where)->with(['employee', 'org', 'optname'])->page($param['page'])->limit($param['limit'])->order('addtime', 'desc')->select();
  1600. //已分配数量
  1601. //$y_count = Customer::where([['crm_res_id','in',array_column($data->toArray(),'id')]])->group('crm_res_id')->column('count(id)','id');
  1602. $listid = CrmImportLog::where('id', '>', 0)->field('id,pid')->select()->toArray();
  1603. $pids = CrmImportLog::where([['pid', 'in', array_column($data->toArray(), 'id')]])->column('id,pid');
  1604. $arr = [];
  1605. foreach ($pids as $k => $v) {
  1606. $arr[$v['pid']][] = $v['id'];
  1607. }
  1608. foreach ($data as &$item) {
  1609. if ($item->pid == 0) {
  1610. $from = '数据导入';
  1611. } else {
  1612. $cil = CrmImportLog::with('org')->find($item->pid);
  1613. $from = $cil->org_info . ' 资源库';
  1614. }
  1615. $item['from'] = $from;
  1616. $item['belong'] = $item->org_info;
  1617. $item['employee_name'] = $item['employee_opt_name'] ?: $item['employee_name'];
  1618. $item['source_name'] = '';
  1619. if ($item['source']) {
  1620. $source_name = CustomerSource::where('id', intval($item['source']))->value('source');
  1621. if ($source_name) {
  1622. $item['source_name'] = $source_name;
  1623. } else {
  1624. $item['source_name'] = '其他';
  1625. }
  1626. }
  1627. //已分配数量
  1628. //$item['y_count'] = isset($y_count[$item['id']]) ? $y_count[$item['id']] : 0;
  1629. $s_id = isset($arr[$item['id']]) ? array_merge($arr[$item['id']], [$item['id']]) : [$item['id']];
  1630. $ids = $this->getAllLowerLevelId($listid, $item['id'], $is_first_time = true);
  1631. $ids[] = $item['id'];
  1632. //
  1633. if ($item['upload_source'] == 0) {
  1634. $item['y_count'] = $item['avaliable_count'] - $item['left_num'];
  1635. } else {
  1636. $item['y_count'] = Customer::where([['is_distribution', '=', 1], ['crm_res_id', 'in', $ids]])->count();
  1637. }
  1638. //$item['y_count'] = Customer::where([['is_distribution','=',1],['crm_res_id','in',$s_id]])->count();
  1639. $item['state_name'] = $item['state'] == 0 ? '未分配' : '已分配';
  1640. }
  1641. $count = CrmImportLog::where($where)->count();
  1642. //查询资源库分组是否是最高层
  1643. $org_pid = Org::where('id', $org)->value('pid');
  1644. return json(['code' => 0, 'data' => $data, 'count' => $count, 'org' => $org_pid]);
  1645. }
  1646. public function resourcepool()
  1647. {
  1648. $request = request();
  1649. if (!$request->isAjax()) {
  1650. //公海
  1651. $pool = PoolLogic::struc();
  1652. View::assign('pool', $pool);
  1653. // 部门
  1654. $org = OrgLogic::struc(request()->employee->root_id);
  1655. View::assign('org', $org);
  1656. $orgids = orgSubIds(request()->employee->root_id);
  1657. View::assign('orgids', json_encode($orgids));
  1658. View::assign('empid', $request->employee->id);
  1659. // View::assign('manager', $request->employee->is_manager);
  1660. View::assign('cilid', $request->param('cilid'));
  1661. return View::fetch();
  1662. }
  1663. $param = $request->only(['page', 'limit', 'order', 'cilid']);
  1664. $condition = [];
  1665. $order = isset($param['order']) ? $param['order'] : 'id desc';
  1666. $root_id = request()->employee->root_id;
  1667. $orgSub = orgSubIds($root_id);
  1668. $condition[] = ['employee_id', '=', NULL];
  1669. $condition[] = ['is_resource', '=', 1];
  1670. $condition[] = ['crm_res_id', '=', $param['cilid']];
  1671. $condition[] = ['bad_phone', '=', 0];
  1672. $condition[] = ['org_id', 'in', $orgSub];
  1673. $data = CustomerLogic::resourcelist($condition, $param['page'], $param['limit'], $order);
  1674. $count = CustomerLogic::count($condition);
  1675. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1676. }
  1677. public function resdistribute()
  1678. {
  1679. $request = request();
  1680. $param = $request->param();
  1681. $targetstr = substr($param['data'], 0, strlen($param['data']) - 1);
  1682. View::assign('targetstr', $targetstr);
  1683. //批次id
  1684. View::assign('cilid', $param['cilid']);
  1685. //公海
  1686. $pool = PoolLogic::struc();
  1687. View::assign('pool', $pool);
  1688. // 部门
  1689. $org = OrgLogic::struc(request()->employee->root_id);
  1690. View::assign('org', $org);
  1691. //$orgids = orgSubIds(request()->employee->org_id);
  1692. //View::assign('orgids', json_encode($orgids));
  1693. $log = CrmImportLog::find($param['cilid']);
  1694. $theorg = Org::find($log->org_id);
  1695. $orgids = Org::where([['path', 'like', $theorg->path . '%'], ['id', '<>', $log->org_id]])->column('id');
  1696. // $orgids = orgSubIds($log->org_id); // 当前资源库所辖subnodeid
  1697. View::assign('orgids', json_encode($orgids));
  1698. $orgids[] = $theorg->id;
  1699. View::assign('emporgids', json_encode($orgids));
  1700. return View::fetch();
  1701. }
  1702. public function resdistributing()
  1703. {
  1704. $orgs = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['id', '<>', request()->employee->root_id]])->column('id');
  1705. $request = request();
  1706. $param = $request->param();
  1707. $target_customer_ids_arr = explode(',', $param['targets']);
  1708. $root_id = request()->employee->root_id;
  1709. $orgSub = orgSubIds($root_id);
  1710. $target_customer_ids_arr = Customer::where([['id', 'in', $target_customer_ids_arr], ['org_id', 'in', $orgSub]])->column('id');
  1711. $using_num = count($target_customer_ids_arr);
  1712. $repeat = 0; //重复phone
  1713. if ($param['type'] == 'toemp' && isset($param['emp_id'])) {
  1714. $employee_id = $param['emp_id'];
  1715. $emp_obj = Employee::find($employee_id);
  1716. $org_id = $emp_obj->org_id;
  1717. $is_source = 0;
  1718. //2022-10-16 逻辑修改重复录入客户表有重复手机号,手动分配需要去重
  1719. $customers = Customer::where([['id', 'in', $target_customer_ids_arr]])->column('id,phone,phone1,phone2');
  1720. $phone = Customer::where('employee_id', $employee_id)->column('phone,phone1,phone2');
  1721. $phones = [];
  1722. foreach ($phone as $v) {
  1723. $phones = array_merge($phones, array_values($v));
  1724. }
  1725. $phones = array_filter($phones);
  1726. // $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  1727. $cids = [];
  1728. foreach ($customers as $v2) {
  1729. $l_phone = array_filter([$v2['phone'], $v2['phone1'], $v2['phone2']]);
  1730. if (empty(array_intersect($l_phone, $phones))) {
  1731. $cids[] = $v2['id'];
  1732. } else {
  1733. $repeat += 1;
  1734. }
  1735. }
  1736. $using_num -= $repeat;
  1737. if ($cids) {
  1738. Customer::where([['id', 'in', $cids]])->update([
  1739. 'employee_id' => $employee_id,
  1740. 'org_id' => $org_id,
  1741. 'is_resource' => $is_source,
  1742. 'ext->ext6' => '',
  1743. 'designer_id' => NULL,
  1744. 'employee_time' => date('Y-m-d H:i:s', time())
  1745. ]);
  1746. Customer::changeProtectedTo($cids, request()->employee->root_id);
  1747. $this->sendOfficialAccount([$employee_id => count($cids)]);
  1748. //添加一条跟进记录
  1749. $visitlog = [];
  1750. foreach ($cids as $val) {
  1751. $visitlog[] = [
  1752. 'customer_id' => $val,
  1753. 'type' => 1,
  1754. 'remark' => '资源库分配',
  1755. 'employee_id' => request()->employee->id,
  1756. 'user_id' => request()->employee->uid,
  1757. 'state' => '待确认'
  1758. ];
  1759. }
  1760. CustomerVisitLog::insertAll($visitlog);
  1761. }
  1762. // ---旧版本
  1763. // foreach ($target_customer_ids_arr as $item_id) {
  1764. // Customer::where(['id' => (int)$item_id])->update([
  1765. // 'employee_id' => $employee_id,
  1766. // 'org_id' => $org_id,
  1767. // 'is_resource' => $is_source,
  1768. // 'ext->ext6' => '',
  1769. // 'designer_id' => NULL,
  1770. // 'employee_time' => date('Y-m-d H:i:s', time())
  1771. // ]);
  1772. // }
  1773. // // 更新客户保护期
  1774. // Customer::changeProtectedTo($target_customer_ids_arr, request()->employee->root_id);
  1775. //// 公众号消息
  1776. // $this->sendOfficialAccount([$employee_id => count($target_customer_ids_arr)]);
  1777. } else {
  1778. if (empty($param['org_pool'])) return json(['code' => 1, 'msg' => '请选择要分配至的资源库']);
  1779. $employee_id = null;
  1780. $aimed_org = Org::find($param['org_pool']);
  1781. $org_id = $aimed_org->id;
  1782. $is_source = 1;
  1783. foreach ($target_customer_ids_arr as $item_id) {
  1784. Customer::where(['id' => (int)$item_id])->update([
  1785. 'employee_id' => $employee_id,
  1786. 'org_id' => $org_id,
  1787. 'is_resource' => $is_source,
  1788. 'ext->ext6' => '',
  1789. 'designer_id' => NULL
  1790. ]);
  1791. }
  1792. //build log
  1793. $metaLog = CrmImportLog::find($param['cilid']);
  1794. $newlog = new CrmImportLog();
  1795. $newdata = [
  1796. 'pid' => $metaLog->id,
  1797. 'employee_id' => $metaLog->employee_id,
  1798. 'org_id' => $org_id,
  1799. 'name' => $metaLog->name,
  1800. 'count' => $using_num,
  1801. 'state' => 0,
  1802. 'left_num' => $using_num,
  1803. 'source' => $metaLog->source
  1804. ];
  1805. $newlog->save($newdata);
  1806. ///
  1807. Customer::where(['crm_res_id' => $param['cilid'], 'org_id' => $org_id])->update(['crm_res_id' => $newlog->id]);
  1808. }
  1809. $leftNum = CrmImportLog::where('id', $param['cilid'])->column('left_num');
  1810. $newleftNum = ($leftNum[0] - $using_num) > 0 ? ($leftNum[0] - $using_num) : 0;
  1811. if (CrmImportLog::where('id', $param['cilid'])->update(['state' => 1, 'left_num' => $newleftNum]) !== false) {
  1812. return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]);
  1813. } else {
  1814. return json(['code' => 1, 'msg' => '操作失败']);
  1815. }
  1816. }
  1817. public function patchdistribute()
  1818. {
  1819. $request = request();
  1820. $param = $request->param();
  1821. if (!$request->isAjax()) {
  1822. //公海
  1823. $pool = PoolLogic::struc();
  1824. View::assign('pool', $pool);
  1825. // 部门
  1826. $org = OrgLogic::struc(request()->employee->root_id);
  1827. View::assign('org', $org);
  1828. //$orgids = orgSubIds(request()->employee->org_id);
  1829. //$orgids = [request()->employee->org_id]; //只显示自己所在的node选项
  1830. $log = CrmImportLog::find($request->param('cilid'));
  1831. View::assign('resnum', $log->left_num);
  1832. View::assign('cilid', $log->id);
  1833. $orgids = [$log->org_id]; //显示此资源库所辖node选项
  1834. View::assign('orgids', json_encode($orgids));
  1835. return View::fetch();
  1836. }
  1837. $target_customer_ids_arr = explode(',', $param['targets']);
  1838. $root_id = request()->employee->root_id;
  1839. $orgSub = orgSubIds($root_id);
  1840. $target_customer_ids_arr = Customer::where([['id', 'in', $target_customer_ids_arr], ['org_id', 'in', $orgSub]])->column('id');
  1841. if ($param['type'] == 'toemp' && isset($param['emp_id'])) {
  1842. $employee_id = $param['emp_id'];
  1843. $emp_obj = Employee::find($employee_id);
  1844. $org_id = $emp_obj->org_id;
  1845. $employee_time = date('Y-m-d H:i:s', time());
  1846. } else {
  1847. $employee_id = null;
  1848. $aimed_org_id_arr = Pool::where('id', $param['pool'])->column('org_id');
  1849. $org_id = $aimed_org_id_arr[0];
  1850. $employee_time = NULL;
  1851. }
  1852. foreach ($target_customer_ids_arr as $item_id) {
  1853. Customer::where(['id' => (int)$item_id])->update([
  1854. 'employee_id' => $employee_id,
  1855. 'org_id' => $org_id,
  1856. 'ext->ext6' => '',
  1857. 'designer_id' => NULL,
  1858. 'employee_time' => $employee_time
  1859. ]);
  1860. }
  1861. if ($param['type'] == 'toemp' && isset($param['emp_id'])) {
  1862. // 更新客户保护期
  1863. Customer::changeProtectedTo($target_customer_ids_arr, request()->employee->root_id);
  1864. }
  1865. return json(['code' => 0, 'msg' => '操作成功']);
  1866. }
  1867. public function patchdistributing()
  1868. {
  1869. $orgs = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['id', '<>', request()->employee->root_id]])->column('id');
  1870. $request = request();
  1871. $param = $request->param();
  1872. $repeat = []; //重复客户
  1873. $root_id = request()->employee->root_id;
  1874. $orgSub = orgSubIds($root_id);
  1875. if ($param['type'] == 'topool') {
  1876. $leftNum = 0;
  1877. // 更新数据准备
  1878. $target_ids_arr = json_decode($param['targets'], true);
  1879. $target_ids_arr = Customer::where([['id', 'in', $target_ids_arr], ['org_id', 'in', $target_ids_arr]])->column('id');
  1880. $group = count($target_ids_arr);
  1881. $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id');
  1882. // $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id,phone,phone1,phone2');
  1883. // $cid = [];
  1884. // foreach ($resource as $resource_item) {
  1885. // // $cid[] = $resource_item['id'];
  1886. // $query = [];
  1887. // $l_phone = array_filter([$resource_item['phone'],$resource_item['phone1'],$resource_item['phone2']]);
  1888. // $query[] = ['phone|phone1|phone2','in',$l_phone];
  1889. // $query[] = ['org_id','in',$orgs];//所有的公海都不能重复
  1890. // $query[] = ['employee_id','=',null];
  1891. // $check = Customer::where($query)->findOrEmpty();
  1892. // $check->isEmpty() ? $cid[] = $resource_item['id'] : $repeat[] = $check->id;
  1893. // }
  1894. // $leftNum += count($repeat);//剩余的数量等于重复的加上
  1895. // if ($cid) {
  1896. // $resource = $cid;
  1897. // 数据分割
  1898. $resourceChunk = array_chunk($resource, $group);
  1899. // id分组
  1900. $groupIds = [];
  1901. foreach ($resourceChunk as $item) {
  1902. if (count($item) != $group) {
  1903. $leftNum += count($item);
  1904. break;
  1905. }
  1906. foreach ($item as $g => $v) {
  1907. $groupIds[$g][] = $v;
  1908. }
  1909. }
  1910. // 查询老记录
  1911. $metaLog = CrmImportLog::find($param['cilid']);
  1912. // 更新记录
  1913. foreach ($target_ids_arr as $i => $crmResId) {
  1914. $pageNum = count($groupIds[$i]);
  1915. $log = CrmImportLog::create([
  1916. 'pid' => $metaLog->id,
  1917. 'employee_id' => $metaLog->employee_id,
  1918. 'org_id' => $crmResId,
  1919. 'name' => $metaLog->name,
  1920. 'count' => $pageNum,
  1921. 'state' => 0,
  1922. 'left_num' => $pageNum,
  1923. 'source' => $metaLog->source
  1924. ]);
  1925. Customer::where([['id', 'in', $groupIds[$i]]])->update([
  1926. 'employee_id' => NULL,
  1927. 'org_id' => $crmResId,
  1928. 'is_resource' => 1,
  1929. 'crm_res_id' => $log->id,
  1930. 'ext->ext6' => '',
  1931. 'designer_id' => NULL
  1932. ]);
  1933. }
  1934. // }
  1935. }
  1936. if ($param['type'] == 'toemp') {
  1937. //2022-10-16 分配逻辑修改 单个业务员不出现重复 单次导入的表格中一定没有重复手机号
  1938. $employees = json_decode($param['targets'], true); //业务员
  1939. $employees = Employee::where([['id', 'in', $employees]])->column('org_id', 'id');
  1940. $where[] = ['crm_res_id', '=', $param['cilid']];
  1941. $where[] = ['bad_phone', '=', 0];
  1942. $where[] = ['employee_id', '=', NULL];
  1943. $customers = Customer::where($where)->column('id,phone,phone1,phone2'); //要分配的所有客户
  1944. if (empty($employees) || empty($customers)) return json(['code' => 1, 'msg' => '分配失败']);
  1945. $eids = array_keys($employees);
  1946. $arr = [];
  1947. shuffle($customers); //数组顺序打乱随机分配
  1948. // 检测电话是否已经存在
  1949. // $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  1950. foreach ($customers as $v) {
  1951. $l_phone = array_filter([$v['phone'], $v['phone1'], $v['phone2']]);
  1952. //分给没有此手机号的业务员 单个业务员的客户手机号不能重复
  1953. $query = [];
  1954. $query[] = ['phone|phone1|phone2', 'in', $l_phone];
  1955. $query[] = ['employee_id', 'in', $eids];
  1956. $eid = Customer::where($query)->group('employee_id')->column('employee_id');
  1957. $eid = array_values(array_diff($eids, $eid)); //去除已经有此手机号的业务员
  1958. if (empty($eid)) {
  1959. //如果手机号所有人都有则 不分配
  1960. $repeat[] = $v['id'];
  1961. continue;
  1962. }
  1963. if (count($eid) == 1) {
  1964. isset($arr[$eid[0]]) ? array_push($arr[$eid[0]], $v['id']) : $arr[$eid[0]] = [$v['id']];
  1965. // $arr[$eid[0]][] = $v['id'];//只有一人可分 直接分配
  1966. } else { //超过一人分给数量最少的业务员
  1967. //没有分过的业务员优先
  1968. $check = true;
  1969. foreach ($eid as $v2) {
  1970. if (!isset($arr[$v2])) {
  1971. $arr[$v2] = [$v['id']];
  1972. $check = false;
  1973. break;
  1974. }
  1975. }
  1976. //都分过则分给数量最少的业务员
  1977. if ($check) {
  1978. $count = count($arr[$eid[0]]);
  1979. $f_eid = $eid[0];
  1980. foreach ($eid as $v3) {
  1981. if (count($arr[$v3]) < $count)
  1982. $f_eid = $v3;
  1983. $count = count($arr[$v3]);
  1984. }
  1985. array_push($arr[$f_eid], $v['id']);
  1986. }
  1987. }
  1988. }
  1989. if ($arr) {
  1990. $visitlog = [];
  1991. //分配客户
  1992. $send = []; //发送公众号消息数组
  1993. foreach ($arr as $k4 => $v4) {
  1994. Customer::where([['id', 'in', $v4]])->update([
  1995. 'employee_id' => $k4,
  1996. 'org_id' => $employees[$k4],
  1997. 'is_resource' => 0,
  1998. 'ext->ext6' => '',
  1999. 'designer_id' => NULL,
  2000. 'employee_time' => date('Y-m-d H:i:s', time())
  2001. ]);
  2002. // 更新客户保护期
  2003. Customer::changeProtectedTo($v4, request()->employee->root_id);
  2004. $send[$k4] = count($v4);
  2005. //添加一条跟进记录
  2006. foreach ($v4 as $val) {
  2007. $visitlog[] = [
  2008. 'customer_id' => $val,
  2009. 'type' => 1,
  2010. 'remark' => '资源库分配',
  2011. 'employee_id' => request()->employee->id,
  2012. 'user_id' => request()->employee->uid,
  2013. 'state' => '待确认'
  2014. ];
  2015. }
  2016. }
  2017. if ($visitlog) CustomerVisitLog::insertAll($visitlog);
  2018. $this->sendOfficialAccount($send);
  2019. }
  2020. $leftNum = count($repeat);
  2021. // ---旧版本
  2022. // $target_ids_arr = json_decode($param['targets'], true);
  2023. // $where[] = ['crm_res_id', '=', $param['cilid']];
  2024. // $where[] = ['bad_phone', '=', 0];
  2025. // $where[] = ['employee_id', '=', NULL];
  2026. // $resource = Customer::where($where)->select()->toArray();
  2027. // $timesNum = floor(count($resource) / count($target_ids_arr));
  2028. // $leftNum = count($resource) % count($target_ids_arr);
  2029. // $send = [];
  2030. // for ($i = 0; $i < count($target_ids_arr); $i++) {
  2031. // $empObj = Employee::find($target_ids_arr[$i]);
  2032. // for ($t = 0; $t < $timesNum; $t++) {
  2033. // Customer::where('id', $resource[$i + $t * count($target_ids_arr)]['id'])->update([
  2034. // 'employee_id' => $target_ids_arr[$i],
  2035. // 'org_id' => $empObj->org_id,
  2036. // 'is_resource' => 0,
  2037. // 'ext->ext6' => '',
  2038. // 'designer_id' => NULL,
  2039. // 'employee_time' => date('Y-m-d H:i:s', time())
  2040. // ]);
  2041. // // 更新客户保护期
  2042. // Customer::changeProtectedTo($resource[$i + $t * count($target_ids_arr)]['id'], request()->employee->root_id);
  2043. // //消息
  2044. // isset($send[$target_ids_arr[$i]]) ? $send[$target_ids_arr[$i]] += 1 : $send[$target_ids_arr[$i]] = 1;
  2045. // }
  2046. // }
  2047. //发送消息提醒
  2048. // $this->sendOfficialAccount($send);
  2049. }
  2050. if ($param['type'] == 'toinstitue') {
  2051. $target_arr = json_decode($param['targets'], true);
  2052. $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id');
  2053. // $resource = Customer::where(['crm_res_id' => $param['cilid'], 'bad_phone' => 0])->column('id,phone,phone1,phone2');
  2054. // $cid = [];
  2055. // foreach ($resource as $resource_item) {
  2056. // $query = [];
  2057. // $l_phone = array_filter([$resource_item['phone'],$resource_item['phone1'],$resource_item['phone2']]);
  2058. // $query[] = ['phone|phone1|phone2','in',$l_phone];
  2059. // $query[] = ['org_id','in',$orgs];//所有的公海都不能重复
  2060. // $query[] = ['employee_id','=',null];
  2061. // $check = Customer::where($query)->findOrEmpty();
  2062. // $check->isEmpty() ? $cid[] = $resource_item['id'] : $repeat[] = $check->id;
  2063. // }
  2064. // $resource = $cid;
  2065. $totalNum = count($resource);
  2066. $data_arr = [];
  2067. $start = 0;
  2068. foreach ($target_arr['_vals'] as $i => $l) {
  2069. $data_arr[$target_arr['_keys'][$i]] = array_slice($resource, $start, $l);
  2070. $start += $l;
  2071. }
  2072. ///build log
  2073. $metaLog = CrmImportLog::find($param['cilid']);
  2074. foreach ($data_arr as $org_id => $empList) {
  2075. $pageNum = count($empList);
  2076. $log = CrmImportLog::create([
  2077. 'pid' => $metaLog->id,
  2078. 'employee_id' => $metaLog->employee_id,
  2079. 'org_id' => $org_id,
  2080. 'name' => $metaLog->name,
  2081. 'count' => $pageNum,
  2082. 'state' => 0,
  2083. 'left_num' => $pageNum,
  2084. 'source' => $metaLog->source
  2085. ]);
  2086. Customer::where([['id', 'in', $empList]])->update([
  2087. 'employee_id' => NULL,
  2088. 'org_id' => $org_id,
  2089. 'is_resource' => 1,
  2090. 'crm_res_id' => $log->id,
  2091. 'ext->ext6' => '',
  2092. 'designer_id' => NULL
  2093. ]);
  2094. }
  2095. $leftNum = ($totalNum - $start) > 0 ? $totalNum - $start : 0;
  2096. $leftNum += count($repeat); //剩余的加上重复的
  2097. }
  2098. if (CrmImportLog::where('id', $param['cilid'])->update(['state' => 1, 'left_num' => $leftNum]) !== false) {
  2099. return json(['code' => 0, 'msg' => '操作成功', 'repeat' => count($repeat)]);
  2100. } else {
  2101. return json(['code' => 1, 'msg' => '操作失败']);
  2102. }
  2103. }
  2104. /**
  2105. * 发送公众号消息
  2106. * employee_id => count 业务员id =》 分配数量
  2107. */
  2108. public function sendOfficialAccount($send)
  2109. {
  2110. $client_type = Miniprogram::where('root_id', request()->employee->root_id)->value('notify');
  2111. foreach ($send as $k => $v) {
  2112. //业务员客户数量
  2113. $count = Customer::where([['employee_id', '=', $k], ['crm_res_id', '>', 0]])->count();
  2114. $data = $msg = [];
  2115. $data['page'] = "/pages/index/index?office_type=allocation&clientype=" . $client_type;
  2116. $data['param'] = [
  2117. 'first' => '客户分配提醒', //标题
  2118. 'keyword1' => '共计' . $v . '个客户资源',
  2119. 'keyword2' => date('Y-m-d H:i:s'),
  2120. 'remark' => '累计分配' . $count . '个客户资源,请及时跟进' //
  2121. ];
  2122. $msg = [
  2123. 'type' => 'resourceAllocation',
  2124. 'employee_id' => $k
  2125. ];
  2126. EmployeeMsg::temploateMsg($msg, $data);
  2127. unset($data);
  2128. unset($msg);
  2129. }
  2130. return true;
  2131. }
  2132. public function cil_detail()
  2133. {
  2134. $request = request();
  2135. $param = $request->param();
  2136. if (!$request->isAjax()) {
  2137. $rs = Customer::where([['crm_res_id', '=', $param['cilid']], ['employee_id', 'not null', '']])->group('employee_id')->column('employee_id, count(id) as count');
  2138. $emp = Employee::where([['id', 'in', array_column($rs, 'employee_id')]])->column('name', 'id');
  2139. foreach ($rs as &$customer) {
  2140. $customer['name'] = isset($emp[$customer['employee_id']]) ? $emp[$customer['employee_id']] : '';
  2141. }
  2142. $logs = CrmImportLog::where('pid', $param['cilid'])->column('org_id, count');
  2143. $orgs = Org::where([['id', 'in', array_column($logs, 'org_id')]])->column('info', 'id');
  2144. foreach ($logs as &$item) {
  2145. $item['name'] = isset($orgs[$item['org_id']]) ? $orgs[$item['org_id']] : '';
  2146. }
  2147. View::assign('respool', $logs);
  2148. View::assign('emp', $rs);
  2149. return View::fetch();
  2150. }
  2151. }
  2152. /**
  2153. * 来源列表
  2154. */
  2155. public function sourcelist()
  2156. {
  2157. return View::fetch();
  2158. }
  2159. public function typelist()
  2160. {
  2161. return View::fetch();
  2162. }
  2163. public function source($page, $limit)
  2164. {
  2165. $data = CustomerSource::field('id,source')->where(['root_id' => request()->employee->root_id])->page($page, $limit)->order('id desc')->select();
  2166. $count = CustomerSource::where(['root_id' => request()->employee->root_id])->count();
  2167. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2168. }
  2169. public function product($page, $limit)
  2170. {
  2171. $data = Product::field('id,product')->where(['root_id' => request()->employee->root_id])->page($page, $limit)->select();
  2172. $count = Product::where(['root_id' => request()->employee->root_id])->count();
  2173. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2174. }
  2175. public function community($page, $limit)
  2176. {
  2177. $data = Community::field('id,name as community')->where(['root_id' => request()->employee->root_id])->where([['type', '=', 1]])->page($page, $limit)->select();
  2178. $count = Community::where(['root_id' => request()->employee->root_id])->where([['type', '=', 1]])->count();
  2179. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2180. }
  2181. public function decostyle($page, $limit)
  2182. {
  2183. $type = input('type', 1);
  2184. $data = Decostyle::field('id,name as decostyle')->where(['root_id' => request()->employee->root_id])->where([['type', '=', $type]])->order('id desc')->page($page, $limit)->select();
  2185. $count = Decostyle::where(['root_id' => request()->employee->root_id])->where([['type', '=', $type]])->count();
  2186. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2187. }
  2188. public function delproduct($id)
  2189. {
  2190. Product::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete();
  2191. return json(['code' => 0, 'msg' => '删除成功']);
  2192. }
  2193. public function delsource($id)
  2194. {
  2195. CustomerSource::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete();
  2196. return json(['code' => 0, 'msg' => '删除成功']);
  2197. }
  2198. public function delcommunity($id)
  2199. {
  2200. Community::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete();
  2201. return json(['code' => 0, 'msg' => '删除成功']);
  2202. }
  2203. public function deldecostyle($id)
  2204. {
  2205. Decostyle::where(['root_id' => request()->employee->root_id, 'id' => $id])->delete();
  2206. return json(['code' => 0, 'msg' => '删除成功']);
  2207. }
  2208. public function log()
  2209. {
  2210. $request = request();
  2211. $id = input('id', 0);
  2212. view::assign('id', $id);
  2213. $root_id = request()->employee->root_id;
  2214. $ew[] = ['root_id', '=', $root_id];
  2215. $employee = Employee::where($ew)->field('id,name')->select()->toArray();
  2216. view::assign('employee', $employee);
  2217. if (!$request->isAjax()) return View::fetch();
  2218. $page = input('page', 0);
  2219. $limit = input('limit', 0);
  2220. $eid = input('eid', 0);
  2221. if ($eid) {
  2222. $w[] = ['employee_id', '=', $eid];
  2223. }
  2224. $type = input('type', '');
  2225. if ($type) {
  2226. $w[] = ['state', '=', $type];
  2227. }
  2228. $method = input('method', '');
  2229. if ($method) {
  2230. $w[] = ['type', '=', $method];
  2231. }
  2232. $w[] = ['customer_id', '=', $id];
  2233. $res = CustomerVisitLog::with('employee')->where($w)->page($page, $limit)->order('id desc')->select();
  2234. $new_res = [];
  2235. foreach ($res as $k => $v) {
  2236. $new_res[$k] = $v->toArray();
  2237. //返回数字状态
  2238. $new_res[$k]['old_state'] = $v->getData('state');
  2239. $new_res[$k]['name'] = $v['employee']['name'];
  2240. if ($v['state'] == '已签单') {
  2241. $new_res[$k]['state'] = '已转单';
  2242. }
  2243. if ($v['state'] == '已交定') {
  2244. $new_res[$k]['state'] = '已签单';
  2245. }
  2246. }
  2247. $count = CustomerVisitLog::with('employee')->where($w)->count();
  2248. return json(['code' => 0, 'data' => $new_res, 'count' => $count, 'msg' => '获取成功']);
  2249. }
  2250. /**
  2251. * 删除客户
  2252. */
  2253. public function delete()
  2254. {
  2255. $ids = request()->param('ids');
  2256. if ($ids) {
  2257. $orgids = orgSubIds(request()->employee->root_id);
  2258. $condition[] = ['org_id', 'in', $orgids];
  2259. $condition[] = ['id', 'in', $ids];
  2260. $condition[] = ['employee_id', '=', NULL];
  2261. $condition[] = ['is_resource', '=', 0];
  2262. $customers = Customer::where($condition)->column('*');
  2263. $del_false = 0;
  2264. foreach ($customers as $k => $v) {
  2265. Db::startTrans();
  2266. $v['customer_id'] = $v['id'];
  2267. if ($v['revisit_time'] == '0000-00-00 00:00:00') {
  2268. $v['revisit_time'] = null;
  2269. }
  2270. $v['package_id'] = !empty($v['package_id']) ? $v['package_id'] : 0;
  2271. unset($v['id']);
  2272. unset($v['employee_time']);
  2273. unset($v['add_wechat_time']);
  2274. unset($v['group_building_date']);
  2275. unset($v['live_broadcast_date']);
  2276. unset($v['subscribe_meet_outside']);
  2277. unset($v['meet_outside']);
  2278. unset($v['sign_time']);
  2279. unset($v['reputation_gather']);
  2280. unset($v['into_owner_group']);
  2281. $result = (new CustomerRecycle())->insertGetId($v);
  2282. if ($result) {
  2283. $del_result = Customer::where('id', $v['customer_id'])->find()->force()->delete();
  2284. if ($del_result) {
  2285. Db::commit();
  2286. } else {
  2287. $del_false++;
  2288. Db::rollback();
  2289. }
  2290. }
  2291. }
  2292. if ($del_false > 0) {
  2293. return json(['code' => 0, 'msg' => '部分客户删除成功']);
  2294. } else {
  2295. return json(['code' => 0, 'msg' => '删除成功']);
  2296. }
  2297. } else {
  2298. return json(['code' => 0, 'msg' => '删除失败']);
  2299. }
  2300. }
  2301. /**
  2302. * 客户回收站
  2303. */
  2304. public function recycle()
  2305. {
  2306. $request = request();
  2307. if (!$request->isAjax()) {
  2308. // 部门
  2309. $org = OrgLogic::struc($request->employee->root_id);
  2310. View::assign('org', $org);
  2311. $orgids = orgSubIds(request()->employee->root_id);
  2312. View::assign('orgids', json_encode($orgids));
  2313. return View::fetch();
  2314. }
  2315. $param = $request->only(['page', 'limit', 'order', 'org', 'state']);
  2316. $root_id = request()->employee->root_id;
  2317. $orgSub = orgSubIds($root_id);
  2318. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  2319. $orgids = orgSubIds($param['org']);
  2320. } else {
  2321. $orgids = orgSubIds($root_id);
  2322. }
  2323. $condition = [['org_id', 'in', $orgids]];
  2324. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2325. setCondition($param, 'state', '=', $condition);
  2326. $data = CustomerRecycleLogic::list($condition, $param['page'], $param['limit'], $order);
  2327. $count = CustomerRecycleLogic::count($condition);
  2328. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2329. }
  2330. /**
  2331. * 取消删除
  2332. * 2022-10-19 恢复 在公海中查询重复,如果有重复则不让恢复
  2333. */
  2334. public function undelete()
  2335. {
  2336. $customer_ids = request()->param('ids', '', 'trim');
  2337. $orgids = orgSubIds(request()->employee->root_id);
  2338. // $condition[] = ['org_id', 'in', $orgids];
  2339. $customer_ids = array_values(array_filter(explode(',', $customer_ids)));
  2340. if (count($customer_ids) == 1) {
  2341. $condition[] = ['customer_id', '=', $customer_ids[0]];
  2342. } else {
  2343. $condition[] = ['customer_id', 'in', $customer_ids];
  2344. }
  2345. $customers = CustomerRecycle::where($condition)->column('*');
  2346. $error_number = 0;
  2347. //$orgs = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('id');
  2348. $repeat = 0;
  2349. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  2350. foreach ($customers as $k => $v) {
  2351. if (!in_array($v['org_id'], $orgids)) {
  2352. continue;
  2353. }
  2354. //查询公海中是否有重复的数据
  2355. $l_phone = array_values(array_filter([$v['phone'], $v['phone1'], $v['phone2']]));
  2356. $query = [];
  2357. if (count($l_phone) == 1) {
  2358. $query[] = ['phone|phone1|phone2', '=', $l_phone[0]];
  2359. } else {
  2360. $query[] = ['phone|phone1|phone2', 'in', $l_phone];
  2361. }
  2362. $query[] = ['employee_id', '=', null];
  2363. $query[] = ['org_id', 'in', $orgids];
  2364. $query[] = ['is_resource', '=', 0]; //公海
  2365. $check = Customer::where($query)->findOrEmpty();
  2366. if (!$check->isEmpty()) {
  2367. $repeat += 1;
  2368. continue;
  2369. }
  2370. if ($v['phone']) $v['phone'] = $aec->decrypt($v['phone']);
  2371. if ($v['phone1']) $v['phone1'] = $aec->decrypt($v['phone1']);
  2372. if ($v['phone2']) $v['phone2'] = $aec->decrypt($v['phone2']);
  2373. $info = $v;
  2374. unset($info['id']);
  2375. unset($info['deletetime']);
  2376. $info['id'] = $info['customer_id'];
  2377. $info['designer_id'] = NULL;
  2378. unset($info['customer_id']);
  2379. //删除所有的虚拟字段
  2380. unset($info['sign_time'],$info['add_wechat_time'],$info['group_building_date'],$info['live_broadcast_date'],$info['reputation_gather'],$info['into_owner_group'], $info['subscribe_meet_outside'], $info['meet_outside']);
  2381. Db::startTrans();
  2382. $result = Customer::create($info);
  2383. if ($result) {
  2384. //$result['ext->ext6'] = '';
  2385. $result->save();
  2386. $del_result = (new CustomerRecycle())->where('id', '=', $v['id'])->delete();
  2387. if ($del_result !== false) {
  2388. Db::commit();
  2389. } else {
  2390. Db::rollback();
  2391. $error_number++;
  2392. }
  2393. } else {
  2394. Db::rollback();
  2395. $error_number++;
  2396. }
  2397. }
  2398. if ($error_number) {
  2399. return json(['code' => 0, 'msg' => '部分数据操作成功', 'repeat' => $repeat]);
  2400. } else {
  2401. return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]);
  2402. }
  2403. }
  2404. /**
  2405. * 企微客户列表
  2406. * @return string|\think\response\Json
  2407. */
  2408. public function wework()
  2409. {
  2410. $request = request();
  2411. if (!$request->isAjax()) {
  2412. // 判断此家企业是否关联企业微信
  2413. $company_id = Company::where('root_id', '=', request()->employee->root_id)->value('id');
  2414. $company_setting = WeworksingleCompanySetting::where('company_id', '=', $company_id)->find();
  2415. if (empty($company_setting)) {
  2416. // 没配置就是没开,跳转到没开通的页面
  2417. }
  2418. // 部门
  2419. $org = OrgLogic::struc($request->employee->root_id);
  2420. View::assign('org', $org);
  2421. $orgids = orgSubIds(request()->employee->root_id);
  2422. View::assign('orgids', json_encode($orgids));
  2423. View::assign('empid', $request->employee->id);
  2424. // View::assign('manager', $request->employee->is_manager);
  2425. return View::fetch();
  2426. }
  2427. // 外部联系人关联的customer_id
  2428. $company_id = Company::where('root_id', '=', request()->employee->root_id)->value('id');
  2429. $customer_ids = WeworksingleCustomer::where('company_id', '=', $company_id)->column('customer_id');
  2430. $condition[] = ['id', 'in', $customer_ids];
  2431. $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state']);
  2432. $root_id = request()->employee->root_id;
  2433. $orgSub = orgSubIds($root_id);
  2434. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  2435. $orgids = orgSubIds($param['org']);
  2436. } else {
  2437. $orgids = orgSubIds($root_id);
  2438. }
  2439. $condition[] = ['org_id', 'in', $orgids];
  2440. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2441. $condition[] = ['employee_id', 'not null', ''];
  2442. setCondition($param, ['emp_id', 'employee_id'], '=', $condition);
  2443. setCondition($param, 'state', '=', $condition);
  2444. $data = CustomerLogic::list($condition, $param['page'], $param['limit'], $order);
  2445. $count = CustomerLogic::count($condition);
  2446. $customer_ext_ids = array_column($data, 'id');
  2447. $external_list = WeworksingleCustomer::where('customer_id', 'in', $customer_ext_ids)->select();
  2448. foreach ($data as $k => $v) {
  2449. foreach ($external_list as $kk => $vv) {
  2450. if ($v['id'] == $vv['customer_id']) {
  2451. $data[$k]['wework_name'] = $vv['name'];
  2452. $data[$k]['wework_avatar'] = $vv['avatar'];
  2453. }
  2454. }
  2455. }
  2456. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2457. }
  2458. //资源库迭代新增接口
  2459. /**
  2460. * 客户画像
  2461. */
  2462. public function portrait()
  2463. {
  2464. $token = request()->employee;
  2465. $info = CustomerPortrait::where('root_id', $token['root_id'])->findOrEmpty();
  2466. $data = $info->fields;
  2467. $data['basic_info_edit'] = $data['basic_info'];
  2468. $data['basic_info'] = [];
  2469. $arr = ['name', 'phone', 'sex'];
  2470. foreach ($data['basic_info_edit'] as $k => $v) {
  2471. if (in_array($k, $arr)) {
  2472. $data['basic_info'][$k] = $v;
  2473. unset($data['basic_info_edit'][$k]);
  2474. }
  2475. }
  2476. view::assign('data', $data);
  2477. return View::fetch();
  2478. }
  2479. /**
  2480. * 客户画像
  2481. */
  2482. public function portrait_edit()
  2483. {
  2484. $type = input('type', '');
  2485. $field = input('field', '');
  2486. if ($type && $field) {
  2487. $token = request()->employee;
  2488. $info = CustomerPortrait::where('root_id', $token['root_id'])->findOrEmpty();
  2489. $json = $info->fields;
  2490. $json[$type][$field]['status'] = $json[$type][$field]['status'] ? 0 : 1;
  2491. //房屋类型和交房时间绑定一起
  2492. if ($type == 'house_info' && in_array($field, ['house_type', 'house_delivery_time'])) {
  2493. $binding = $field == 'house_type' ? 'house_delivery_time' : 'house_type';
  2494. $json[$type][$binding]['status'] = $json[$type][$field]['status'];
  2495. }
  2496. if ($json[$type][$field]['status'] == 1) {
  2497. $coll = 0;
  2498. $json['info_status'][$type] = 1;
  2499. } else {
  2500. //取消时查询是否全部取消
  2501. $coll = 1;
  2502. foreach ($json[$type] as $k => $v) {
  2503. if ($v['status'] == 1) $coll = 0;
  2504. }
  2505. if ($coll == 1) {
  2506. $json['info_status'][$type] = 0;
  2507. }
  2508. }
  2509. !$info->isEmpty() ?: $info->root_id = $token['root_id'];
  2510. $info->fields = $json;
  2511. $info->save();
  2512. return json(['code' => 0, 'data' => '操作成功', 'coll' => $coll]);
  2513. }
  2514. return json(['code' => 1, 'data' => '操作失败']);
  2515. }
  2516. /**
  2517. * 客户画像
  2518. */
  2519. public function portrait_type()
  2520. {
  2521. $type = input('type', '');
  2522. if ($type) {
  2523. $token = request()->employee;
  2524. $info = CustomerPortrait::where('root_id', $token['root_id'])->findOrEmpty();
  2525. $json = $info->fields;
  2526. $json['info_status'][$type] = $json['info_status'][$type] ? 0 : 1;
  2527. foreach ($json[$type] as $k => $v) {
  2528. $json[$type][$k]['status'] = $json['info_status'][$type];
  2529. }
  2530. !$info->isEmpty() ?: $info->root_id = $token['root_id'];
  2531. $info->fields = $json;
  2532. $info->save();
  2533. return json(['code' => 0, 'data' => '操作成功']);
  2534. }
  2535. return json(['code' => 1, 'data' => '操作失败']);
  2536. }
  2537. /**
  2538. * 自动回收列表
  2539. */
  2540. public function drop_pool()
  2541. {
  2542. $request = request();
  2543. if (!$request->isAjax()) {
  2544. // 部门
  2545. $org = OrgLogic::struc($request->employee->root_id);
  2546. View::assign('org', $org);
  2547. $orgids = orgSubIds(request()->employee->root_id);
  2548. View::assign('orgids', json_encode($orgids));
  2549. View::assign('empid', $request->employee->id);
  2550. // View::assign('manager', $request->employee->is_manager);
  2551. return View::fetch();
  2552. }
  2553. $param = $request->only(['page', 'limit', 'order', 'org', 'emp_id', 'state']);
  2554. $root_id = request()->employee->root_id;
  2555. $orgSub = orgSubIds($root_id);
  2556. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  2557. $orgids = orgSubIds($param['org']);
  2558. } else {
  2559. $orgids = orgSubIds($root_id);
  2560. }
  2561. $condition = [['org_id', 'in', $orgids]];
  2562. $order = isset($param['order']) ? $param['order'] : 'id desc';
  2563. $condition[] = ['employee_id', 'not null', ''];
  2564. setCondition($param, ['emp_id', 'employee_id'], '=', $condition);
  2565. empty($param['state']) ?: setCondition(['state' => Customer::changeState($param['state'], 'chaos')], 'state', 'in', $condition);
  2566. $data = CustomerDropPool::with(['employee', 'org', 'designer'])->withCount(['visitLog' => function ($query) {
  2567. $query->whereOr([[CustomerVisitLog::changeState(['state', '=', '预约量房'])], [CustomerVisitLog::changeState(['state', '=', '预约到店'])], [CustomerVisitLog::changeState(['state', '=', '预约活动'])]]);
  2568. }])->withCount(['activityFrequency' => function ($query) {
  2569. $query->where([CustomerVisitLog::changeState(['state', '=', '确认到场'])]);
  2570. }])
  2571. ->where($condition)
  2572. ->page($param['page'], $param['limit'])->order($order)->select();
  2573. $data = $data->visible(['id', 'employee_id', 'name', 'community_name', 'phone', 'phone1', 'phone2', 'level', 'state', 'square', 'revisit_time', 'addtime', 'org_id', 'protected_to', 'is_resource', 'employee.name', 'org_name', 'designer.name', 'visit_log_count', 'activity_frequency_count', 'droptime'])->toArray();
  2574. foreach ($data as &$item) {
  2575. $item['protected'] = false;
  2576. if (isset($item['protected_to']) && time() < strtotime($item['protected_to'])) {
  2577. $item['protected'] = true;
  2578. }
  2579. $item['square'] = floatval($item['square']);
  2580. $item['mobile'] = $item['phone'];
  2581. $item['mobile1'] = $item['phone1'];
  2582. $item['mobile2'] = $item['phone2'];
  2583. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  2584. }
  2585. $count = CustomerDropPool::where($condition)->count();
  2586. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  2587. }
  2588. /**
  2589. * 主动回收
  2590. */
  2591. public function drop_to_pool()
  2592. {
  2593. $ids = input('id', '', 'trim');
  2594. $orgIds = orgSubIds(request()->employee->root_id);
  2595. $data = Customer::where([['org_id', 'in', $orgIds], ['id', 'in', $ids]])->select();
  2596. foreach ($data as $item) {
  2597. Db::startTrans();
  2598. $drop_data = $item->toArray();
  2599. $drop_data['customer_id'] = $drop_data['id'];
  2600. unset($drop_data['id']);
  2601. unset($drop_data['add_wechat_time']);
  2602. unset($drop_data['group_building_date']);
  2603. unset($drop_data['live_broadcast_date']);
  2604. unset($drop_data['subscribe_meet_outside']);
  2605. unset($drop_data['meet_outside']);
  2606. unset($drop_data['sign_time']);
  2607. unset($drop_data['reputation_gather']);
  2608. unset($drop_data['into_owner_group']);
  2609. $drop_data['package_id'] = !empty($drop_data['package_id']) ? $drop_data['package_id'] : 0;
  2610. $drop_data['deposit_money'] = $item->getData('deposit_money');
  2611. $drop_data['signed_money'] = $item->getData('signed_money');
  2612. $save_result = CustomerDropPool::create($drop_data);
  2613. $delete_result = $item->force()->delete();
  2614. $record_data['customer_id'] = $item->id;
  2615. CustomerDropRecord::create($record_data);
  2616. // 客户预约中改为无效
  2617. CustomersSubscribe::where([['customer_id', '=', $item->id], ['state', '=', 0]])->save(['state' => -1]);
  2618. if ($save_result && $delete_result) {
  2619. Db::commit();
  2620. $param = [
  2621. 'customer_id' => $item->id,
  2622. 'type' => 1,
  2623. 'remark' => '客户回收',
  2624. 'employee_id' => request()->employee->id,
  2625. 'user_id' => request()->employee->uid,
  2626. 'state' => ''
  2627. ];
  2628. CustomerVisitLog::create($param);
  2629. } else {
  2630. Db::rollback();
  2631. }
  2632. }
  2633. return json(['code' => 0, 'msg' => '回收成功']);
  2634. }
  2635. public function drop_to_pool_check()
  2636. {
  2637. $ids = input('id', '', 'trim');
  2638. $customer_list = Customer::where([['id', 'in', $ids]])->select();
  2639. $state_list = $customer_list->column('state');
  2640. $sign_state = Customer::changeState('签单', 'chaos');
  2641. $sign_count = 0;
  2642. foreach ($state_list as $k => $v) {
  2643. if (in_array($v, $sign_state)) {
  2644. $sign_count++;
  2645. }
  2646. }
  2647. $protected = 0;
  2648. $setting = Setting::where([['name', '=', 'pubpool'], ['root_id', '=', request()->employee->root_id]])->find();
  2649. if (!empty($setting)) {
  2650. $content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  2651. $empty = false;
  2652. if (!empty($content)) {
  2653. foreach ($content as $k => $v) {
  2654. if (!isset($v['day']) || !isset($v['state'])) {
  2655. $empty = true;
  2656. }
  2657. }
  2658. }
  2659. if ($empty) {
  2660. $content = [];
  2661. }
  2662. foreach ($customer_list as $k => $v) {
  2663. $n = Customer::changeState($v['state'], 'n');
  2664. foreach ($content as $kk => $vv) {
  2665. if (intval($kk) == intval($n) && $vv['state'] == 1 && $vv['day'] > 0 && !empty($v['protected_to']) && $v['protected_to'] > date('Y-m-d H:i:s', time())) {
  2666. $protected++;
  2667. }
  2668. }
  2669. }
  2670. }
  2671. $msg = '';
  2672. if ($sign_count && $protected) {
  2673. $msg = '包含已签单及未到保护期客户';
  2674. } elseif ($sign_count) {
  2675. $msg = '包含已签单客户';
  2676. } elseif ($protected) {
  2677. $msg = '包含未到保护期客户';
  2678. }
  2679. if ($msg == '') {
  2680. return json(['code' => 0, 'msg' => '']);
  2681. } else {
  2682. return json(['code' => 1, 'msg' => $msg]);
  2683. }
  2684. }
  2685. /**
  2686. * 自动回收设置
  2687. */
  2688. public function drop_pool_setting()
  2689. {
  2690. $root_id = request()->employee->root_id;
  2691. $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty();
  2692. $empty = false; //判断是否是正常数据,因为之前可能有垃圾数据
  2693. if ($setting->isEmpty()) {
  2694. $empty = true;
  2695. }
  2696. if (!empty($setting)) {
  2697. $content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  2698. if (!empty($content)) {
  2699. foreach ($content as $k => $v) {
  2700. if (!isset($v['day']) || !isset($v['state'])) {
  2701. $empty = true;
  2702. }
  2703. }
  2704. }
  2705. }
  2706. // 标准状态
  2707. $sys_content = [
  2708. 0 => ['day' => 0, 'state' => 0, 'name' => '待确认'],
  2709. 1 => ['day' => 0, 'state' => 0, 'name' => '未到访'],
  2710. 2 => ['day' => 0, 'state' => 0, 'name' => '已到店'],
  2711. 3 => ['day' => 0, 'state' => 0, 'name' => '已到场'],
  2712. 4 => ['day' => 0, 'state' => 0, 'name' => '已量房'],
  2713. 5 => ['day' => 0, 'state' => 0, 'name' => '已交定'],
  2714. ];
  2715. if ($empty) {
  2716. $data['name'] = 'pubpool';
  2717. $data['state'] = 1;
  2718. $data['root_id'] = $root_id;
  2719. $data['content'] = json_encode($sys_content);
  2720. Setting::create($data);
  2721. $data['content'] = [
  2722. 'a' => ['day' => 0, 'state' => 0, 'name' => '待确认'],
  2723. 'b' => ['day' => 0, 'state' => 0, 'name' => '已到店'],
  2724. 'c' => ['day' => 0, 'state' => 0, 'name' => '已到场'],
  2725. 'd' => ['day' => 0, 'state' => 0, 'name' => '已量房'],
  2726. 'e' => ['day' => 0, 'state' => 0, 'name' => '已交定'],
  2727. 'f' => ['day' => 0, 'state' => 0, 'name' => '未到访'],
  2728. ];
  2729. View::assign('info', $data);
  2730. } else {
  2731. $key_val = [
  2732. 'a' => 0,
  2733. 'b' => 2,
  2734. 'c' => 3,
  2735. 'd' => 4,
  2736. 'e' => 5,
  2737. 'f' => 1
  2738. ];
  2739. $content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  2740. $new_content = [];
  2741. foreach ($key_val as $k => $v) {
  2742. $new_content[$k] = $sys_content[$v];
  2743. foreach ($content as $kk => $vv) {
  2744. if ($kk == $v) {
  2745. $new_content[$k] = $vv;
  2746. }
  2747. }
  2748. }
  2749. $setting['content'] = $new_content;
  2750. View::assign('info', $setting);
  2751. }
  2752. return View::fetch();
  2753. }
  2754. /**
  2755. * 修改自动回收设置
  2756. * @return string
  2757. */
  2758. public function drop_pool_setting_edit()
  2759. {
  2760. if (input('aa', '')) {
  2761. $arr = Customer::where('1=1')->order('id desc')->limit(4)->column('id,protected_to');
  2762. foreach ($arr as $v) {
  2763. $date = $v['protected_to'] ?: date('Y-m-d H:i:s');
  2764. $date = date('Y-m-d H:i:s', strtotime($date) - 86400);
  2765. Customer::where('id', $v['id'])->update(['protected_to' => $date]);
  2766. }
  2767. }
  2768. $param = request()->only(['a', 'a_switch', 'b', 'b_switch', 'c', 'c_switch', 'd', 'd_switch', 'e', 'e_switch', 'f', 'f_switch']);
  2769. $root_id = request()->employee->root_id;
  2770. $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty();
  2771. if ($setting->isEmpty()) {
  2772. $data['name'] = 'pubpool';
  2773. $data['state'] = 1; //$param['off'] ?? 0;
  2774. $data['root_id'] = $root_id;
  2775. $content = [
  2776. 0 => ['day' => intval($param['a']), 'state' => $param['a_switch'] ?? 0, 'name' => '待确认'],
  2777. 1 => ['day' => intval($param['f']), 'state' => $param['f_switch'] ?? 0, 'name' => '未到访'],
  2778. 2 => ['day' => intval($param['b']), 'state' => $param['b_switch'] ?? 0, 'name' => '已到店'],
  2779. 3 => ['day' => intval($param['c']), 'state' => $param['c_switch'] ?? 0, 'name' => '已到场'],
  2780. 4 => ['day' => intval($param['d']), 'state' => $param['d_switch'] ?? 0, 'name' => '已量房'],
  2781. 5 => ['day' => intval($param['e']), 'state' => $param['e_switch'] ?? 0, 'name' => '已交定'],
  2782. ];
  2783. $data['content'] = json_encode($content);
  2784. if (isset($param['a_switch']) && intval($param['a']) < 1) {
  2785. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2786. }
  2787. if (isset($param['b_switch']) && intval($param['b']) < 1) {
  2788. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2789. }
  2790. if (isset($param['c_switch']) && intval($param['c']) < 1) {
  2791. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2792. }
  2793. if (isset($param['d_switch']) && intval($param['d']) < 1) {
  2794. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2795. }
  2796. if (isset($param['e_switch']) && intval($param['e']) < 1) {
  2797. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2798. }
  2799. if (isset($param['f_switch']) && intval($param['f']) < 1) {
  2800. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2801. }
  2802. $result = Setting::create($data);
  2803. if ($result) {
  2804. $orgIds = orgSubIds($setting['root_id']);
  2805. // 待确认的客户修改保护期
  2806. if (isset($param['a_switch'])) {
  2807. $a_where[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  2808. $a_where[] = ['org_id', 'in', $orgIds];
  2809. $a_where[] = ['employee_id', '<>', 0];
  2810. Customer::where($a_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['a']) * 24 * 3600)]);
  2811. }
  2812. // 已到店的客户修改保护期
  2813. if (isset($param['b_switch'])) {
  2814. $b_where[] = ['state', 'in', Customer::changeState('已到店', 'chaos')];
  2815. $b_where[] = ['org_id', 'in', $orgIds];
  2816. $b_where[] = ['employee_id', '<>', 0];
  2817. Customer::where($b_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['b']) * 24 * 3600)]);
  2818. }
  2819. // 已到场的客户修改保护期
  2820. if (isset($param['c_switch'])) {
  2821. $c_where[] = ['state', 'in', Customer::changeState('已到场', 'chaos')];
  2822. $c_where[] = ['org_id', 'in', $orgIds];
  2823. $c_where[] = ['employee_id', '<>', 0];
  2824. Customer::where($c_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['c']) * 24 * 3600)]);
  2825. }
  2826. // 已量房的客户修改保护期
  2827. if (isset($param['d_switch'])) {
  2828. $d_where[] = ['state', 'in', Customer::changeState('已量房', 'chaos')];
  2829. $d_where[] = ['org_id', 'in', $orgIds];
  2830. $d_where[] = ['employee_id', '<>', 0];
  2831. Customer::where($d_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['d']) * 24 * 3600)]);
  2832. }
  2833. // 已交定的客户修改保护期
  2834. if (isset($param['e_switch'])) {
  2835. $e_where[] = ['state', 'in', Customer::changeState('已交定', 'chaos')];
  2836. $e_where[] = ['org_id', 'in', $orgIds];
  2837. $e_where[] = ['employee_id', '<>', 0];
  2838. Customer::where($e_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['e']) * 24 * 3600)]);
  2839. }
  2840. // 未到访的客户修改保护期
  2841. if (isset($param['f_switch'])) {
  2842. $f_where[] = ['state', 'in', Customer::changeState('未到访', 'chaos')];
  2843. $f_where[] = ['org_id', 'in', $orgIds];
  2844. $f_where[] = ['employee_id', '<>', 0];
  2845. Customer::where($f_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['f']) * 24 * 3600)]);
  2846. }
  2847. return json(['code' => 0, 'msg' => '保存成功']);
  2848. } else {
  2849. return json(['code' => 1, 'msg' => '保存失败']);
  2850. }
  2851. } else {
  2852. $old_content = json_decode($setting['content'], 'true');
  2853. if (isset($param['a_switch']) && intval($param['a']) < 1) {
  2854. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2855. }
  2856. if (isset($param['b_switch']) && intval($param['b']) < 1) {
  2857. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2858. }
  2859. if (isset($param['c_switch']) && intval($param['c']) < 1) {
  2860. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2861. }
  2862. if (isset($param['d_switch']) && intval($param['d']) < 1) {
  2863. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2864. }
  2865. if (isset($param['e_switch']) && intval($param['e']) < 1) {
  2866. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2867. }
  2868. if (isset($param['f_switch']) && intval($param['f']) < 1) {
  2869. return json(['code' => 1, 'msg' => '天数至少为一天']);
  2870. }
  2871. $content = [
  2872. 0 => ['day' => intval($param['a']), 'state' => $param['a_switch'] ?? 0, 'name' => '待确认'],
  2873. 1 => ['day' => intval($param['f']), 'state' => $param['f_switch'] ?? 0, 'name' => '未到访'],
  2874. 2 => ['day' => intval($param['b']), 'state' => $param['b_switch'] ?? 0, 'name' => '已到店'],
  2875. 3 => ['day' => intval($param['c']), 'state' => $param['c_switch'] ?? 0, 'name' => '已到场'],
  2876. 4 => ['day' => intval($param['d']), 'state' => $param['d_switch'] ?? 0, 'name' => '已量房'],
  2877. 5 => ['day' => intval($param['e']), 'state' => $param['e_switch'] ?? 0, 'name' => '已交定'],
  2878. ];
  2879. $setting->content = json_encode($content);
  2880. $result = $setting->save();
  2881. if ($result) {
  2882. $orgIds = orgSubIds($setting['root_id']);
  2883. // 待确认的客户修改保护期
  2884. if (isset($param['a_switch'])) {
  2885. $a_n = Customer::changeState('待确认', 'n');
  2886. if ($old_content[$a_n]['state'] == 0) {
  2887. $a_where[] = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  2888. $a_where[] = ['org_id', 'in', $orgIds];
  2889. $a_where[] = ['employee_id', '<>', 0];
  2890. Customer::where($a_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['a']) * 24 * 3600)]);
  2891. }
  2892. }
  2893. // 已到店的客户修改保护期
  2894. if (isset($param['b_switch'])) {
  2895. $b_n = Customer::changeState('已到店', 'n');
  2896. if ($old_content[$b_n]['state'] == 0) {
  2897. $b_where[] = ['state', 'in', Customer::changeState('已到店', 'chaos')];
  2898. $b_where[] = ['org_id', 'in', $orgIds];
  2899. $b_where[] = ['employee_id', '<>', 0];
  2900. Customer::where($b_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['b']) * 24 * 3600)]);
  2901. }
  2902. }
  2903. // 已到场的客户修改保护期
  2904. if (isset($param['c_switch'])) {
  2905. $c_n = Customer::changeState('已到场', 'n');
  2906. if ($old_content[$c_n]['state'] == 0) {
  2907. $c_where[] = ['state', 'in', Customer::changeState('已到场', 'chaos')];
  2908. $c_where[] = ['org_id', 'in', $orgIds];
  2909. $c_where[] = ['employee_id', '<>', 0];
  2910. Customer::where($c_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['c']) * 24 * 3600)]);
  2911. }
  2912. }
  2913. // 已量房的客户修改保护期
  2914. if (isset($param['d_switch'])) {
  2915. $d_n = Customer::changeState('已量房', 'n');
  2916. if ($old_content[$d_n]['state'] == 0) {
  2917. $d_where[] = ['state', 'in', Customer::changeState('已量房', 'chaos')];
  2918. $d_where[] = ['org_id', 'in', $orgIds];
  2919. $d_where[] = ['employee_id', '<>', 0];
  2920. Customer::where($d_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['d']) * 24 * 3600)]);
  2921. }
  2922. }
  2923. // 已交定的客户修改保护期
  2924. if (isset($param['e_switch'])) {
  2925. $e_n = Customer::changeState('已交定', 'n');
  2926. if ($old_content[$e_n]['state'] == 0) {
  2927. $e_where[] = ['state', 'in', Customer::changeState('已交定', 'chaos')];
  2928. $e_where[] = ['org_id', 'in', $orgIds];
  2929. $e_where[] = ['employee_id', '<>', 0];
  2930. Customer::where($e_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['e']) * 24 * 3600)]);
  2931. }
  2932. }
  2933. // 已交定的客户修改保护期
  2934. if (isset($param['f_switch'])) {
  2935. $f_n = Customer::changeState('已交定', 'n');
  2936. if ($old_content[$f_n]['state'] == 0) {
  2937. $f_where[] = ['state', 'in', Customer::changeState('未到访', 'chaos')];
  2938. $f_where[] = ['org_id', 'in', $orgIds];
  2939. $f_where[] = ['employee_id', '<>', 0];
  2940. Customer::where($f_where)->save(['protected_to' => date('Y-m-d H:i:s', time() + intval($param['f']) * 24 * 3600)]);
  2941. }
  2942. }
  2943. return json(['code' => 0, 'msg' => '保存成功']);
  2944. } else {
  2945. return json(['code' => 1, 'msg' => '保存失败']);
  2946. }
  2947. }
  2948. }
  2949. /**
  2950. * 还原
  2951. */
  2952. public function undrop()
  2953. {
  2954. $id = input('id', '', 'trim');
  2955. if (!$id) {
  2956. return json(['code' => 1, 'msg' => '参数错误']);
  2957. }
  2958. $root_id = request()->employee->root_id;
  2959. $orgSub = orgSubIds($root_id);
  2960. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  2961. $orgids = orgSubIds($param['org']);
  2962. } else {
  2963. $orgids = orgSubIds($root_id);
  2964. }
  2965. $list = CustomerDropPool::where([['id', 'in', $id], ['org_id', 'in', $orgids]])->select();
  2966. $root_id = request()->employee->root_id;
  2967. $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty();
  2968. $error = 0;
  2969. $repeat = 0;
  2970. foreach ($list as $k => $v) {
  2971. //查询重复
  2972. $l_phone = array_filter([$v->getData('phone'), $v->getData('phone1'), $v->getData('phone2')]);
  2973. $query = [];
  2974. $query[] = ['phone|phone1|phone2', 'in', $l_phone];
  2975. $query[] = ['employee_id', '=', $v->employee_id];
  2976. $check = Customer::where($query)->findOrEmpty();
  2977. if (!$check->isEmpty()) {
  2978. $repeat += 1;
  2979. continue;
  2980. }
  2981. $customer_state = Customer::changeState($v->state, 'n');
  2982. $info = $v->toArray();
  2983. $info['protected_to'] = null;
  2984. if (!empty($setting)) {
  2985. $setting_content = json_decode($setting['content'], true);
  2986. if (!empty($setting_content[$customer_state]['state']) && $setting_content[$customer_state]['state'] != 0 && !empty($setting_content[$customer_state]['day'])) {
  2987. $day = $setting_content[$customer_state]['day'];
  2988. $info['protected_to'] = date('Y-m-d H:i:s', time() + $day * 24 * 3600);
  2989. }
  2990. }
  2991. $info['deposit_money'] = $v->getData('deposit_money');
  2992. $info['signed_money'] = $v->getData('signed_money');
  2993. $info['id'] = $info['customer_id'];
  2994. unset($info['customer_id']);
  2995. unset($info['droptime']);
  2996. unset($info['add_wechat_time']);
  2997. unset($info['group_building_date']);
  2998. unset($info['live_broadcast_date']);
  2999. unset($info['subscribe_meet_outside']);
  3000. unset($info['meet_outside']);
  3001. unset($info['sign_time']);
  3002. unset($info['reputation_gather']);
  3003. unset($info['into_owner_group']);
  3004. Db::startTrans();
  3005. $result = Customer::create($info);
  3006. $d_result = $v->delete();
  3007. if ($result && $d_result) {
  3008. Db::commit();
  3009. $param = [
  3010. 'customer_id' => $info['id'],
  3011. 'type' => 1,
  3012. 'remark' => '客户还原',
  3013. 'employee_id' => request()->employee->id,
  3014. 'user_id' => request()->employee->uid,
  3015. 'state' => ''
  3016. ];
  3017. CustomerVisitLog::create($param);
  3018. } else {
  3019. Db::rollback();
  3020. $error++;
  3021. }
  3022. }
  3023. if ($error == 0) {
  3024. return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]);
  3025. } else {
  3026. return json(['code' => 0, 'msg' => '操作成功,部分数据失败', 'repeat' => $repeat]);
  3027. }
  3028. }
  3029. /**
  3030. * 重新指派
  3031. * @return string
  3032. */
  3033. public function drop_pool_resdistribute()
  3034. {
  3035. $request = request();
  3036. $param = $request->param();
  3037. if (!request()->isAjax()) {
  3038. View::assign('from', $param['from']);
  3039. $targetstr = substr($param['data'], 0, strlen($param['data']) - 1);
  3040. View::assign('targetstr', $targetstr);
  3041. // 部门
  3042. $org = OrgLogic::struc(request()->employee->root_id);
  3043. View::assign('org', $org);
  3044. $orgids = orgSubIds(request()->employee->root_id);
  3045. View::assign('orgids', json_encode($orgids));
  3046. View::assign('emporgids', json_encode($orgids));
  3047. View::assign('check', input('check', ''));
  3048. return View::fetch();
  3049. }
  3050. $target_ids_arr = explode(',', $param['targets']);
  3051. if ($param['type'] == 'toemp' && !empty($param['emp_id'])) {
  3052. $employee_id = $param['emp_id'];
  3053. $emp_obj = Employee::find($employee_id);
  3054. $org_id = $emp_obj->org_id;
  3055. // 设计师能否报备、获取客户
  3056. $root_id = $request->employee->root_id;
  3057. $de_where[] = ['root_id', '=', $root_id];
  3058. $de_where[] = ['name', '=', 'designer_get_customer'];
  3059. $info_de_where = Setting::where($de_where)->findOrEmpty();
  3060. $org_type = Org::where('id', '=', $org_id)->value('org_type');
  3061. if (!$info_de_where->isEmpty() && $org_type == 2) {
  3062. if ((int)$info_de_where['content'] != 1) {
  3063. return json(['code' => 1, 'msg' => '无法分配给设计师']);
  3064. }
  3065. }
  3066. } else {
  3067. return json(['code' => 1, 'msg' => '操作失败']);
  3068. }
  3069. //2022-10-21 19:25:59-----------------
  3070. //2022-10-21 重复录入 重复验证 check=1,2@3,4 表示ID=2和3的客户手机号重复 1,4不重复 ,1,2,4和业务员有重复则不让添加
  3071. $repeat_customer = [];
  3072. $vislog_id = []; //跟进记录合并数组
  3073. if (!empty($param['check'])) {
  3074. $target_ids_arr = [];
  3075. $check_arr = explode(',', $param['check']);
  3076. //先已第一个位置 以后可能会已保护期排序
  3077. foreach ($check_arr as $check_arr_key => $check_arr_val) {
  3078. //默认第一个ID 为准
  3079. $temporary_arr = explode('@', $check_arr_val);
  3080. $vislog_id[$temporary_arr[0]] = array_values($temporary_arr);
  3081. }
  3082. foreach ($vislog_id as $vislog_id_key => $vislog_id_val) {
  3083. $temporary_customer = ($param['from'] == 'index') ? Customer::where('id', $vislog_id_key)->find() : CustomerDropPool::where('id', $vislog_id_key)->find();
  3084. $query = [];
  3085. $query[] = ['employee_id', '=', $employee_id];
  3086. $l_phone = array_filter([$temporary_customer->getData('phone'), $temporary_customer->getData('phone1'), $temporary_customer->getData('phone2')]);
  3087. $query[] = ['phone|phone1|phone2', 'in', $l_phone];
  3088. $check = Customer::where($query)->findOrEmpty();
  3089. if ($check->isEmpty()) {
  3090. $target_ids_arr[] = $vislog_id_key;
  3091. } else {
  3092. $repeat_customer[] = $temporary_customer->name; //重复的客户
  3093. }
  3094. }
  3095. if ($repeat_customer) {
  3096. return json(['code' => 1, 'msg' => '您选择的客户“' . implode('”,“', $repeat_customer) . '”手机号已经属于业务员' . $emp_obj->name . '请重新选择']);
  3097. }
  3098. if ($param['from'] != 'index') {
  3099. $drop = $vislog_id;
  3100. $vislog_ids = [];
  3101. foreach ($vislog_id as $k => $v) {
  3102. $ls_id = CustomerDropPool::where('id', $k)->value('customer_id');
  3103. $vislog_ids[$ls_id] = CustomerDropPool::where([['id', 'in', $v]])->column('customer_id');
  3104. }
  3105. $vislog_id = $vislog_ids;
  3106. }
  3107. unset($param['check']);
  3108. }
  3109. //2022-10-21 19:25:59-----------------
  3110. $root_id = request()->employee->root_id;
  3111. $setting = Setting::where([['root_id', '=', $root_id], ['name', '=', 'pubpool']])->findOrEmpty();
  3112. $error = 0;
  3113. $repeat = []; //重复数量
  3114. //2022-10-17 重新分配逻辑修改,数据重复合并数据"1.重复客户分给同一个业务员时,提示分配人员(重复客户分配给一个业务员将合并客户信息,是否继续),点击继续 则合并客户(客户信息按最后置为回收站内的客户信息显示)
  3115. // 2.分配到不同业务员时,客户不合并,各自显示各自的客户信息
  3116. // 3.分配的业务员名下已存在该客户手机号时,提示分配人员(该业务员已有改客户手机号)"
  3117. $z_type = $param['from'];
  3118. if (!empty($target_ids_arr)) {
  3119. if ($param['from'] == 'index') {
  3120. $list = Customer::where([['id', 'in', $target_ids_arr]])->select();
  3121. if (!$list->isEmpty()) {
  3122. foreach ($list as $k => $v) {
  3123. //如果该客户已经属于业务员则不需要分配 ,提示分配人
  3124. if ($v->employee_id == $employee_id) {
  3125. $repeat[] = [$v->name, $v->getData('phone')];
  3126. continue;
  3127. }
  3128. $customer_state = Customer::changeState($v['state'], 'n');
  3129. $v_data['protected_to'] = NULL;
  3130. if (!$setting->isEmpty()) {
  3131. $setting_content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  3132. if (!empty($setting_content[$customer_state]['state']) && $setting_content[$customer_state]['state'] != 0 && !empty($setting_content[$customer_state]['day'])) {
  3133. $day = $setting_content[$customer_state]['day'];
  3134. $v_data['protected_to'] = date('Y-m-d H:i:s', time() + $day * 24 * 3600);
  3135. }
  3136. }
  3137. //重复手机号验证 ,如果重复则合并客户信息和跟进记录
  3138. // $query = [];
  3139. // $l_phone = array_filter([$v->getData('phone'),$v->getData('phone1'),$v->getData('phone2')]);
  3140. // $query[] = ['phone|phone1|phone2','in',$l_phone];
  3141. // $query[] = ['employee_id','=',$employee_id];
  3142. // $check = Customer::where($query)->column('*');
  3143. // if ($check) {
  3144. // $this->customerMerge($v->id,$check,'emp',$v_data['protected_to']);///
  3145. // continue;
  3146. // }
  3147. $v_data['employee_id'] = $employee_id;
  3148. $v_data['org_id'] = $org_id;
  3149. $v_data['designer_id'] = NULL;
  3150. $v_data['ext->ext6'] = '';
  3151. $v_data['revisit_time'] = NULL;
  3152. $result = Customer::where('id', $v->id)->update($v_data);
  3153. if ($result === false) {
  3154. $error++;
  3155. } else {
  3156. $param = [
  3157. 'customer_id' => $v->id,
  3158. 'type' => 1,
  3159. 'remark' => '客户再分配',
  3160. 'employee_id' => request()->employee->id,
  3161. 'user_id' => request()->employee->uid,
  3162. 'state' => ''
  3163. ];
  3164. CustomerVisitLog::create($param);
  3165. }
  3166. }
  3167. }
  3168. } elseif ($param['from'] == 'drop') {
  3169. $list = CustomerDropPool::where([['id', 'in', $target_ids_arr]])->select();
  3170. if (!$list->isEmpty()) {
  3171. foreach ($list as $k => $v) {
  3172. $customer_state = Customer::changeState($v['state'], 'n');
  3173. $info = $v->toArray();
  3174. $info['protected_to'] = NULL;
  3175. if (!$setting->isEmpty()) {
  3176. $setting_content = !empty($setting['content']) ? json_decode($setting['content'], true) : [];
  3177. if (!empty($setting_content[$customer_state]['state']) && $setting_content[$customer_state]['state'] != 0 && !empty($setting_content[$customer_state]['day'])) {
  3178. $day = $setting_content[$customer_state]['day'];
  3179. $info['protected_to'] = date('Y-m-d H:i:s', time() + $day * 24 * 3600);
  3180. }
  3181. }
  3182. // //重复手机号验证 ,如果重复则合并客户信息和跟进记录
  3183. // $query = [];
  3184. // $l_phone = array_filter([$v->getData('phone'),$v->getData('phone1'),$v->getData('phone2')]);
  3185. // $query[] = ['phone|phone1|phone2','in',$l_phone];
  3186. // $query[] = ['employee_id','=',$employee_id];
  3187. // $check = Customer::where($query)->column('*');
  3188. // if ($check) {
  3189. // $this->customerMerge($v->id,$check,'drop',$info['protected_to']);
  3190. // continue;
  3191. // }
  3192. $info['employee_id'] = $employee_id;
  3193. $info['org_id'] = $org_id;
  3194. $info['id'] = $info['customer_id'];
  3195. $info['signed_money'] = $v->getData('signed_money');
  3196. $info['deposit_money'] = $v->getData('deposit_money');
  3197. $info['designer_id'] = NULL;
  3198. $info['revisit_time'] = NULL;
  3199. unset($info['customer_id']);
  3200. unset($info['droptime']);
  3201. Db::startTrans();
  3202. $result = Customer::create($info);
  3203. $d_result = $v->delete();
  3204. if (!empty($drop[$v->id])) CustomerDropPool::where([['id', 'in', $drop[$v->id]]])->delete();
  3205. if ($result && $d_result) {
  3206. $result['ext->ext6'] = '';
  3207. $result->save();
  3208. Db::commit();
  3209. $param = [
  3210. 'customer_id' => $info['id'],
  3211. 'type' => 1,
  3212. 'remark' => '客户再分配',
  3213. 'employee_id' => request()->employee->id,
  3214. 'user_id' => request()->employee->uid,
  3215. 'state' => ''
  3216. ];
  3217. CustomerVisitLog::create($param);
  3218. } else {
  3219. Db::rollback();
  3220. $error++;
  3221. }
  3222. }
  3223. }
  3224. }
  3225. }
  3226. $repeat = count($repeat);
  3227. if ($error == 0) {
  3228. //2022-10-21 19:25:59-----------------
  3229. // $this->customerMerge($vislog_id, $z_type);
  3230. //2022-10-21 19:25:59-----------------
  3231. return json(['code' => 0, 'msg' => '操作成功', 'repeat' => $repeat]);
  3232. } else {
  3233. return json(['code' => 0, 'msg' => '操作成功,部分数据失败', 'repeat' => $repeat]);
  3234. }
  3235. }
  3236. //资源库迭代新增接口
  3237. /**
  3238. *
  3239. */
  3240. public function distribution()
  3241. {
  3242. $id = input('id', 0); //资源id
  3243. if (!$this->checkResourceIsMy($id)) return;
  3244. $count = input('count', 0); //可分配资源数量
  3245. View::assign('id', $id);
  3246. View::assign('count', $count);
  3247. $customer_id = input('customer_id', '');
  3248. View::assign('customer_id', $customer_id);
  3249. return View::fetch();
  3250. }
  3251. /**
  3252. * 获取部门树
  3253. * 分配弹框页面接口
  3254. */
  3255. public function get_orgs()
  3256. {
  3257. $root_id = request()->employee->root_id;
  3258. $keyword = input('keyword', '');
  3259. $where = [
  3260. ['path', 'like', $root_id . '-%'],
  3261. ['status', '=', 1]
  3262. ];
  3263. $count = Employee::where([['root_id', '=', $root_id], ['state', 'like', '%在职%'], ['uid', '>', 0]])->group('org_id')->column('count(*) count', 'org_id');
  3264. $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toArray();
  3265. foreach ($allnodes as $k => $v) {
  3266. $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0; //本部门人数
  3267. $allnodes[$k]['all_count'] = 0; //包含子部门总数
  3268. $allnodes[$k]['org_class'] = 'org_' . $v['id'];
  3269. $allnodes[$k]['per_class'] = 'per_org_' . $v['id'];
  3270. $allnodes[$k]['type'] = 'org';
  3271. foreach ($allnodes as $k2 => $v2) {
  3272. if (strpos($v2['path'], $v['path']) !== false) {
  3273. $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']] + $allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
  3274. }
  3275. }
  3276. $allnodes[$k]['title'] = $allnodes[$k]['title'] . ' (' . $allnodes[$k]['all_count'] . ')';
  3277. }
  3278. $tree = $this->tree($allnodes, 0);
  3279. return json(['code' => 0, 'data' => $tree, 'checkOrg' => '']);
  3280. }
  3281. public function tree($data, $pid = 0)
  3282. {
  3283. $new_arr = [];
  3284. foreach ($data as $k => $v) {
  3285. if ($v['pid'] == $pid) {
  3286. $children = $this->tree($data, $v['id']);
  3287. $v['children'] = $children;
  3288. if (empty($v['children'])) $v['disabled'] = true;
  3289. $new_arr[] = $v;
  3290. }
  3291. }
  3292. return $new_arr;
  3293. }
  3294. /**
  3295. * 获取公司人员
  3296. * 分配弹框页面接口
  3297. */
  3298. public function get_persons()
  3299. {
  3300. $request = request();
  3301. $root_id = $request->employee->root_id;
  3302. $param = $request->only(['page' => 1, 'limit' => 10000, 'keyword' => '', 'type' => 1, 'ids' => '']);
  3303. $where = [
  3304. ['root_id', '=', $root_id],
  3305. ['uid', '>', 0],
  3306. ['state', '=', '在职']
  3307. ];
  3308. if ($param['type'] == 1 && $param['keyword']) {
  3309. $org_ids = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['name', 'like', '%' . $param['keyword'] . '%']])->column('id');
  3310. $where[] = ['org_id', 'in', $org_ids];
  3311. } elseif ($param['type'] == 2 && $param['keyword']) {
  3312. $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  3313. }
  3314. $list = Employee::with(['org' => function ($query) {
  3315. $query->field(['id', 'name'])->bind(['org_name' => 'name']);
  3316. }])->where($where)->field('id,org_id,name')->page($param['page'], $param['limit'])->order('id asc')->select();
  3317. $count = Employee::where($where)->count();
  3318. if ($param['ids']) {
  3319. $arr = explode(',', $param['ids']);
  3320. foreach ($list as $k => $v) {
  3321. $list[$k]['checked'] = in_array($v['id'], $arr);
  3322. }
  3323. } else {
  3324. foreach ($list as $k => $v) {
  3325. $list[$k]['checked'] = false;
  3326. }
  3327. }
  3328. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  3329. }
  3330. /**
  3331. * 获取公司部门
  3332. * 分配弹框页面接口
  3333. */
  3334. public function get_org()
  3335. {
  3336. $request = request();
  3337. $root_id = $request->employee->root_id;
  3338. $where = [
  3339. ['path', 'like', $root_id . '-%'],
  3340. ['status', '=', 1]
  3341. ];
  3342. $list = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select();
  3343. return json(['code' => 0, 'data' => $list]);
  3344. }
  3345. /**
  3346. * 分配资源
  3347. * 分配弹框页面接口
  3348. * type=org分配给部门,person分配给员工,
  3349. * resources_id批量分配时资源id ,必传
  3350. * content 被分配部门id/员工id和数量 [{con_id:1,count:2},{con_id:89,count:4}]
  3351. * customer_id 手动分配时选择的客户id
  3352. */
  3353. public function distribution_resources()
  3354. {
  3355. $request = request();
  3356. $root_id = $request->employee->root_id;
  3357. $param = $request->only(['type' => 'org', 'resources_id' => 0, 'content' => [], 'customer_id' => '']);
  3358. if (!$this->checkResourceIsMy($param['resources_id'])) return;
  3359. if (empty($param['content']) || !is_array($param['content'])) return json(['code' => 1, 'data' => '请选择分配对象', 'msg' => '请选择分配对象']);
  3360. $state = Customer::changeState('待确认', 'chaos');
  3361. $customer = $param['customer_id'] ? explode(',', $param['customer_id']) : Customer::where([['crm_res_id', '=', $param['resources_id']], ['employee_id', 'null', null], ['bad_phone', '=', 0], ['is_resource', '=', 1],['state','in',$state]])->column('id');
  3362. shuffle($customer); //随机分配
  3363. $count = count($customer);
  3364. $all_count = array_sum(array_column($param['content'], 'count'));
  3365. if ($all_count > $count) return json(['code' => 1, 'data' => '分配数量不能大于资源总数', 'msg' => '分配数量不能大于资源总数']);
  3366. // 查询老记录
  3367. $metaLog = CrmImportLog::find($param['resources_id']);
  3368. $repeat = [];
  3369. //指派类型
  3370. if ($param['type'] == 'org') {
  3371. foreach ($param['content'] as $k => $v) {
  3372. $con = array_slice($customer, 0, $v['count']);
  3373. $param['content'][$k]['customer'] = $con;
  3374. $customer = array_diff($customer, $con);
  3375. }
  3376. //指派到部门
  3377. foreach ($param['content'] as $key => $val) {
  3378. if ($val['count'] > 0) {
  3379. $log = CrmImportLog::create([
  3380. 'pid' => $param['resources_id'],
  3381. 'employee_id' => null,
  3382. 'org_id' => $val['con_id'],
  3383. 'name' => $metaLog->name,
  3384. 'count' => $val['count'],
  3385. 'state' => 0,
  3386. 'left_num' => $val['count'],
  3387. 'source' => $metaLog->source
  3388. ]);
  3389. $where[] = ['id', 'in', $val['customer']];
  3390. Customer::where($where)->update([
  3391. 'employee_id' => NULL,
  3392. 'org_id' => $val['con_id'],
  3393. 'is_resource' => 1,
  3394. 'crm_res_id' => $log->id,
  3395. 'is_distribution' => 1,
  3396. 'employee_time'=>date('Y-m-d H:i:s')
  3397. ]);
  3398. unset($where);
  3399. }
  3400. }
  3401. } elseif ($param['type'] == 'person') {
  3402. //分配到人去重
  3403. $eids = array_column($param['content'], 'con_id'); //所有人
  3404. // 设计师能否报备、获取客户
  3405. $de_where[] = ['root_id', '=', $root_id];
  3406. $de_where[] = ['name', '=', 'designer_get_customer'];
  3407. $info_de_where = Setting::where($de_where)->findOrEmpty();
  3408. $employee_org_ids = Employee::where('id', 'in', $eids)->column('org_id');
  3409. $org_type = Org::where('id', 'in', $employee_org_ids)->column('org_type');
  3410. if (!$info_de_where->isEmpty() && in_array(2, $org_type)) {
  3411. if ((int)$info_de_where['content'] != 1) {
  3412. return json(['code' => 1, 'msg' => '无法分配给设计师']);
  3413. }
  3414. }
  3415. $phones = Customer::where([['employee_id', 'in', $eids]])->column('employee_id,name,phone,phone1,phone2'); //所有人的手机号
  3416. $phone_emp = []; //待分配的业务员
  3417. foreach ($phones as $k => $v) {
  3418. $phone_emp[$v['employee_id']] = isset($phone_emp[$v['employee_id']]) ? $phone_emp[$v['employee_id']] : [];
  3419. array_push($phone_emp[$v['employee_id']], $v['phone'], $v['phone1'], $v['phone2']);
  3420. }
  3421. //待分配的业务员 已有的手机号
  3422. foreach ($phone_emp as $k2 => $v2) {
  3423. $phone_emp[$k2] = array_unique(array_filter($v2));
  3424. }
  3425. foreach ($param['content'] as $k3 => $v3) {
  3426. //已有手机号
  3427. $param['content'][$k3]['phones'] = isset($phone_emp[$v3['con_id']]) ? $phone_emp[$v3['con_id']] : [];
  3428. //待分配的客户
  3429. $param['content'][$k3]['cids'] = [];
  3430. }
  3431. //分配
  3432. $customer = Customer::where([['id', 'in', $customer], ['employee_id', '=', null]])->column('id,name,phone,phone1,phone2');
  3433. foreach ($customer as $k4 => $v4) {
  3434. $ls_phones = array_filter([$v4['phone'], $v4['phone1'], $v4['phone2']]);
  3435. $i = 0;
  3436. foreach ($param['content'] as $k5 => $v5) {
  3437. //没有分配完成
  3438. if ($v5['count'] > count($v5['cids'])) {
  3439. //并且手机号不重复
  3440. if ($v5['phones'] == []) {
  3441. $param['content'][$k5]['cids'][] = $v4['id'];
  3442. $param['content'][$k5]['phones'] = array_merge($v5['phones'], $ls_phones);
  3443. $i = 1;
  3444. break;
  3445. } else {
  3446. $phone_diff = array_intersect($v5['phones'], $ls_phones);
  3447. if ($phone_diff) {
  3448. continue; //手机号重复 分配给下一个客户
  3449. } else {
  3450. $param['content'][$k5]['cids'][] = $v4['id'];
  3451. $param['content'][$k5]['phones'] = array_merge($v5['phones'], $ls_phones);
  3452. $i = 1;
  3453. break;
  3454. }
  3455. }
  3456. }
  3457. }
  3458. //无法分配
  3459. if ($i == 0) {
  3460. $repeat[$v4['id']] = $v4['name'];
  3461. }
  3462. }
  3463. //分配
  3464. $visitlog = [];
  3465. $date = date('Y-m-d');
  3466. foreach ($param['content'] as $k6 => $v6) {
  3467. if ($v6['cids']) {
  3468. $emp = Employee::where('id', $v6['con_id'])->findOrEmpty();
  3469. Customer::where([['id', 'in', $v6['cids']]])->update([
  3470. 'employee_id' => $v6['con_id'],
  3471. 'org_id' => $emp->org_id,
  3472. 'is_resource' => 0,
  3473. 'is_distribution' => 1,
  3474. 'employee_time' => date('Y-m-d H:i:s'),
  3475. 'remark' => '',
  3476. 'valid_time'=>null,
  3477. 'state'=>0
  3478. ]);
  3479. CustomerVisitLog::where([['customer_id','in',$v6['cids']],['data_type','=','out_call']])->delete();
  3480. OutCallLog::where([['customer_id','in',$v6['cids']]])->update(['is_redistribution'=>1]);
  3481. OutCallMbLog::where([['customer_id','in',$v6['cids']]])->update(['is_redistribution'=>1]);
  3482. //添加跟进记录
  3483. foreach ($v6['cids'] as $v7) {
  3484. $visitlog[] = [
  3485. 'customer_id' => $v7,
  3486. 'type' => 1,
  3487. 'remark' => '资源库分配',
  3488. 'employee_id' => $v6['con_id'],
  3489. 'user_id' => $emp->uid,
  3490. 'state' => '待确认',
  3491. 'next_contact_date' => $date
  3492. ];
  3493. }
  3494. }
  3495. }
  3496. if ($visitlog) CustomerVisitLog::insertAll($visitlog);
  3497. //2023-02-28分配之后记录分配记录
  3498. $log = [];
  3499. $time = time();
  3500. foreach ($param['content'] as $key8 => $val8) {
  3501. if (!empty($val8['cids'])) {
  3502. $log[] = [
  3503. 'employee_id'=>$val8['con_id'],
  3504. 'root_id' => $root_id,
  3505. 'crm_res_id' => $param['resources_id'],
  3506. 'type' => 0,
  3507. 'source_type' => 0,
  3508. 'count'=>count($val8['cids']),
  3509. 'customer_ids' => implode(',',$val8['cids']),
  3510. 'operator_employee_id' => $request->employee->id,
  3511. 'addtime' => $time
  3512. ];
  3513. }
  3514. }
  3515. if($log) CrmImportRetrieveLog::insertAll($log);
  3516. } else {
  3517. return json(['code' => 1, 'data' => '数据错误', 'msg' => '数据错误']);
  3518. }
  3519. //查询未分配数量
  3520. $left_num = Customer::where([['crm_res_id', '=', $param['resources_id']], ['employee_id', '=', null],['state','in',$state]])->count();
  3521. CrmImportLog::where('id', $param['resources_id'])->update(['state' => 1, 'left_num' => $left_num]);
  3522. $j = 0;
  3523. if ($param['type'] == 'person') {
  3524. foreach ($param['content'] as $v7) {
  3525. if ($v7['count'] > count($v7['cids'])) $j = 1;
  3526. }
  3527. }
  3528. if ($repeat && $j == 1) {
  3529. $name = implode(',', array_values($repeat));
  3530. return json(['code' => 1, 'data' => '客户:' . $name . '。手机号重复无法分配', 'msg' => '客户:' . $name . '。手机号重复无法分配']);
  3531. } else {
  3532. return json(['code' => 0, 'data' => '分配完成', 'msg' => '分配完成']);
  3533. }
  3534. }
  3535. /**
  3536. * 详情页获取基本信息
  3537. * 详情页
  3538. */
  3539. public function read()
  3540. {
  3541. $request = request();
  3542. $root_id = $request->employee->root_id;
  3543. $param = $request->only(['id' => 0]);
  3544. $data = CrmImportLog::with(['employee', 'source' => function ($query) {
  3545. $query->field('id,source')->bind(['source_name' => 'source']);
  3546. }])->where('id', $param['id'])->findOrEmpty();
  3547. $data->state = $data->state == 0 ? '未分配' : '已分配';
  3548. //已分配数量
  3549. //$rids = CrmImportLog::where('pid|id', $param['id'])->column('id');
  3550. $listid = CrmImportLog::where('id', '>', 0)->field('id,pid')->select()->toArray();
  3551. $rids = $this->getAllLowerLevelId($listid, $param['id'], $is_first_time = true);
  3552. $rids[] = $param['id'];
  3553. $data->y_count = Customer::where([['crm_res_id', 'in', $rids], ['is_distribution', '=', 1]])->count();
  3554. return $data;
  3555. }
  3556. /**
  3557. * 已分配页面获取客户跟进数据
  3558. * 详情页
  3559. */
  3560. public function get_customer_data()
  3561. {
  3562. $request = request();
  3563. $root_id = $request->employee->root_id;
  3564. $param = $request->only(['id' => 0]);
  3565. $orgSub = orgSubIds($root_id);
  3566. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  3567. $orgids = orgSubIds($param['org']);
  3568. } else {
  3569. $orgids = orgSubIds($root_id);
  3570. }
  3571. //分配记录
  3572. $ids = CrmImportLog::where([['pid|id', '=', $param['id']], ['org_id', 'in', $orgids]])->column('id');
  3573. //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户
  3574. $customer_id1 = Customer::where([['crm_res_id', 'in', $ids], ['employee_id', '>', 0], ['is_distribution', '=', 1]])->column('id');
  3575. $customer_id2 = Customer::where([['crm_res_id', 'in', $ids], ['employee_id', 'null', null], ['is_distribution', '=', 1]])->column('id');
  3576. $customer_id3 = CustomerVisitLog::where([['customer_id', 'in', $customer_id2]])->group('customer_id')->column('customer_id');
  3577. $customer_id = array_merge($customer_id1, $customer_id3);
  3578. //有效状态 未到访,已到访,确定到场,已量房,交定,签单
  3579. // $state1 = CustomerVisitLog::changeState('待确认', 'chaos');
  3580. $state2 = CustomerVisitLog::changeState('未到访', 'chaos');
  3581. $state3 = CustomerVisitLog::changeState('已到访', 'chaos');
  3582. $state4 = CustomerVisitLog::changeState('已到场', 'chaos');
  3583. $state5 = CustomerVisitLog::changeState('已量房', 'chaos');
  3584. $state6 = CustomerVisitLog::changeState('已交定', 'chaos');
  3585. $state7 = CustomerVisitLog::changeState('已签单', 'chaos');
  3586. $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7);
  3587. //有效客户 排除待回访,和无效
  3588. //2023-02-02 逻辑修改 新分配的客户为待确认状态,确认标记有效后到我的客户,状态为有效
  3589. $query1 = ['id', 'in', $customer_id];
  3590. $query2 = ['is_distribution', '=', 1];
  3591. $query3 = ['is_distribution', '=', 0];
  3592. $query4 = ['died', '<>', 2]; //排除死单
  3593. $query5 = ['customer_id', 'in', $customer_id];
  3594. $query6 = ['state', 'in', Customer::changeState('待确认', 'chaos')];
  3595. $query7 = ['state', 'not in', Customer::changeState('待确认', 'chaos')];
  3596. $query8 = ['state', 'not in', Customer::changeState('无效', 'chaos')];
  3597. $query9 = ['state', 'in', Customer::changeState('无效', 'chaos')];
  3598. //有效
  3599. $data['valid'] = Customer::where([$query1, $query2, $query7, $query8])->count();
  3600. // $data['valid'] = CustomerVisitLog::where([
  3601. // ['customer_id', 'in', $customer_id],
  3602. // ['state', 'in', $state]
  3603. // ])->group('customer_id')->count();
  3604. //无效
  3605. $data['invalid'] = Customer::where([$query1, $query2, $query9])->count();
  3606. //待确认
  3607. $data['confirmed'] = Customer::where([$query1, $query2, $query6])->count();
  3608. // $state8 = CustomerVisitLog::changeState('预约到店', 'chaos');
  3609. // $state9 = CustomerVisitLog::changeState('预约量房', 'chaos');
  3610. // $state10 = CustomerVisitLog::changeState('预约活动', 'chaos');
  3611. // $state11 = CustomerVisitLog::changeState('无效', 'chaos');
  3612. // $state12 = CustomerVisitLog::changeState('未到店', 'chaos');
  3613. // $state13 = CustomerVisitLog::changeState('未量房', 'chaos');
  3614. // $state14 = CustomerVisitLog::changeState('已卖卡', 'chaos');
  3615. // $state15 = CustomerVisitLog::changeState('未到场', 'chaos');
  3616. // $l_ids = CustomerVisitLog::where([
  3617. // ['customer_id', 'in', $customer_id],
  3618. // ['state', 'in', array_merge($state15, $state2, $state3, $state4, $state5, $state6, $state7, $state9, $state8, $state10, $state11, $state12, $state13, $state14)]
  3619. // ])->group('customer_id')->column('customer_id');
  3620. // $l_ids = array_diff($customer_id, $l_ids);
  3621. // $data['confirmed'] = CustomerVisitLog::where([
  3622. // ['customer_id', 'in', $l_ids]
  3623. // ])->group('customer_id')->count();
  3624. //已到店,,曾经已到店
  3625. $data['arrived_at_the_store'] = CustomerVisitLog::where([
  3626. $query5,
  3627. ['state', 'in', CustomerVisitLog::changeState('已到店', 'chaos')]
  3628. ])->group('customer_id')->count();
  3629. //已量房
  3630. $data['measured_room'] = CustomerVisitLog::where([
  3631. $query5,
  3632. ['state', 'in', CustomerVisitLog::changeState('已量房', 'chaos')]
  3633. ])->group('customer_id')->count();
  3634. //已到场
  3635. $data['already_present'] = CustomerVisitLog::where([
  3636. $query5,
  3637. ['state', 'in', CustomerVisitLog::changeState('已到场', 'chaos')]
  3638. ])->group('customer_id')->count();
  3639. //已交定
  3640. $data['settled'] = CustomerVisitLog::where([
  3641. $query5,
  3642. ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]
  3643. ])->group('customer_id')->count();
  3644. //已签单
  3645. $data['sign_the_bill'] = CustomerVisitLog::where([
  3646. $query5,
  3647. ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]
  3648. ])->group('customer_id')->count();
  3649. //2023-02-02 已联系包括’分配资源‘中的客户已经调用外呼或者标记时remark有值的客户
  3650. //2023-04-21 点击拨打按钮就算已联系
  3651. $data['visit'] = OutCallMbLog::where([$query5])->group('customer_id')->count();
  3652. //未联系
  3653. $data['unvisit'] = count($customer_id) - $data['visit'];
  3654. //电话未联系和未接通
  3655. $call_list = OutCallLog::where([['customer_id', 'in', $customer_id]])->order('addtime desc')->field('id,status,customer_id')->select()->toArray();
  3656. $call_ids = [];
  3657. $call_status = [];
  3658. $no_connect = 0;
  3659. foreach ($call_list as $key => $val) {
  3660. if (!in_array($val['customer_id'], $call_ids)) {
  3661. $call_ids[] = $val['customer_id'];
  3662. $call_status[$val['customer_id']] = $val['status'];
  3663. }
  3664. }
  3665. foreach ($call_status as $key => $val) {
  3666. if (in_array($val, [-1, -2])) $no_connect++;
  3667. }
  3668. $yfp = Customer::where([['crm_res_id', 'in', $ids], ['is_distribution', '=', 1]])->count();
  3669. $data['no_connect'] = $no_connect;
  3670. $data['no_call'] = $yfp - count($call_ids);
  3671. return $data;
  3672. }
  3673. /**
  3674. * 已分配客户筛选多种状态处理
  3675. */
  3676. public function cus_arrstatus($state, $where)
  3677. {
  3678. $arr_state = explode(',', $state);
  3679. $allids = [];
  3680. $newids = [];
  3681. $cids = Customer::where($where)->column('id');
  3682. foreach ($arr_state as $key => $val) {
  3683. $arr = ['已到店', '已量房', '已到场', '已交定', '已签单'];
  3684. $v_where[] = ['customer_id', 'in', $cids];
  3685. if (in_array($val, $arr)) {
  3686. $v_where[] = ['state', 'in', CustomerVisitLog::changeState($val, 'chaos')];
  3687. $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id');
  3688. unset($v_where);
  3689. $allids[] = $ids;
  3690. } elseif ($val == '待确认') {
  3691. //排除有效就是待确认
  3692. $ids = Customer::where($where)->where('state', 'in', Customer::changeState('待确认', 'chaos'))->column('id');
  3693. $allids[] = $ids;
  3694. } elseif ($val == '有效') {
  3695. //2023-02-02修改 有效状态 客户表state字段不为待确认和无效,就是有效
  3696. $state1 = Customer::changeState('待确认', 'chaos');
  3697. $state2 = Customer::changeState('无效', 'chaos');
  3698. $allids[] = Customer::where($where)->where('state', 'not in', array_merge($state1,$state2))->column('id');
  3699. }elseif($val == '无效'){
  3700. $state = Customer::changeState('无效', 'chaos');
  3701. $allids[] = Customer::where($where)->where('state', 'in', $state)->column('id');
  3702. }
  3703. }
  3704. foreach ($allids as $key => $val) {
  3705. foreach ($val as $k => $v) {
  3706. $newids[] = $v;
  3707. }
  3708. }
  3709. $newids = array_unique($newids);
  3710. return $newids;
  3711. }
  3712. /**
  3713. * 查询所有资源的下级
  3714. */
  3715. function getAllLowerLevelId($list, $p_id = '0', $isFirstTime = true)
  3716. {
  3717. static $arr = [];
  3718. if ($isFirstTime) {
  3719. $arr = [];
  3720. }
  3721. foreach ($list as $key => $val) {
  3722. if ($val['pid'] == $p_id) {
  3723. $arr[] = $val['id'];
  3724. $this->getAllLowerLevelId($list, $val['id'], false);
  3725. }
  3726. }
  3727. return $arr;
  3728. }
  3729. /**
  3730. * 已分配客户列表
  3731. * 分配弹框页面接口
  3732. */
  3733. public function get_customers()
  3734. {
  3735. $request = request();
  3736. $root_id = $request->employee->root_id;
  3737. $param = $request->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'type' => 1, 'id' => 0, 'keyword3' => '', 'state' => '', 'recovery' => 0, 'ids' => '', 'call_state']);
  3738. if (!$this->checkResourceIsMy($param['id'])) return;
  3739. //分配记录
  3740. //$ids = CrmImportLog::where('pid|id', $param['id'])->column('id');
  3741. $orgSub = orgSubIds($root_id);
  3742. if (!empty($param['org']) && in_array($param['org'], $orgSub)) {
  3743. $orgids = orgSubIds($param['org']);
  3744. } else {
  3745. $orgids = orgSubIds($root_id);
  3746. }
  3747. $listid = CrmImportLog::where([['id','>',0], ['org_id', 'in', $orgids]])->field('id,pid')->select()->toArray();
  3748. $ids = $this->getAllLowerLevelId($listid, $param['id'], $is_first_time = true);
  3749. $ids[] = $param['id'];
  3750. //已分配的数量包括,已经分配 和 曾经分配过当前处于无效状态的客户 和 包括分配到部门资源库的
  3751. $customer_id = Customer::where([['crm_res_id', 'in', $ids], ['is_distribution', '=', 1]])->column('id'); //分配到人
  3752. // $customer_id2 = Customer::where([['crm_res_id', 'in', $ids]])->column('id');
  3753. // $customer_id3 = CustomerVisitLog::where([['customer_id', 'in', $customer_id2]])->group('customer_id')->column('customer_id');
  3754. // $customer_id4 = Customer::where([['crm_res_id', 'in', array_diff($ids, [$param['id']])], ['employee_id', '=', null]])->column('id');
  3755. // $customer_id = array_unique(array_merge($customer_id1, $customer_id3, $customer_id4));
  3756. $where[] = ['id', 'in', $customer_id];
  3757. if ($param['type'] == 1 && $param['keyword']) {
  3758. $org_ids = Org::where([['path', 'like', request()->employee->root_id . '-%'], ['name', 'like', '%' . $param['keyword'] . '%']])->column('id');
  3759. $where[] = ['org_id', 'in', $org_ids];
  3760. } elseif ($param['type'] == 2 && $param['keyword']) {
  3761. $emp_ids = Employee::where([['name', 'like', '%' . $param['keyword'] . '%'], ['uid', '>', 0], ['root_id', '=', request()->employee->root_id], ['state', '=', '在职']])->column('id');
  3762. // $where[] = ['name', 'like', '%' . $param['keyword'] . '%'];
  3763. $where[] = ['employee_id', 'in', $emp_ids];
  3764. }
  3765. //小区,手机号搜索
  3766. if ($param['keyword3']) {
  3767. $ids = [];
  3768. $keyword = Customer::where($where)->field('id,community_name,phone,phone1,phone2')->select();
  3769. foreach ($keyword as $key => $val) {
  3770. $str = $val['community_name'] . $val['phone'] . $val['phone1'] . $val['phone2'];
  3771. if (strpos($str, $param['keyword3']) !== false) $ids[] = $val['id'];
  3772. }
  3773. $where[] = ['id', 'in', $ids];
  3774. }
  3775. //状态搜索
  3776. if ($param['state']) {
  3777. $state_ids = $this->cus_arrstatus($param['state'], $where);
  3778. unset($where);
  3779. $where[] = ['id', 'in', $state_ids];
  3780. // $arr = ['已到店','已量房','已到场','已交定','已签单'];
  3781. // $cids = Customer::where($where)->column('id');
  3782. // $v_where[] = ['customer_id','in',$cids];
  3783. // if (in_array($param['state'],$arr)) {
  3784. // $v_where[] = ['state','in',CustomerVisitLog::changeState($param['state'], 'chaos')];
  3785. // $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id');
  3786. // unset($where);
  3787. // $where[] = ['id','in',$ids];
  3788. // } elseif ($param['state']=='待确认') {
  3789. // //排除有效就是待确认
  3790. // $cids = Customer::where($where)->column('id');
  3791. // $state2 = CustomerVisitLog::changeState('未到访', 'chaos');
  3792. // $state3 = CustomerVisitLog::changeState('已到访', 'chaos');
  3793. // $state4 = CustomerVisitLog::changeState('已到场', 'chaos');
  3794. // $state5 = CustomerVisitLog::changeState('已量房', 'chaos');
  3795. // $state6 = CustomerVisitLog::changeState('已交定', 'chaos');
  3796. // $state7 = CustomerVisitLog::changeState('已签单', 'chaos');
  3797. // $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7);
  3798. // $v_where[] = ['customer_id','in',$cids];
  3799. // $v_where[] = ['state','in',$state];
  3800. // $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id');
  3801. // $where[] = ['id','not in',$ids];
  3802. // } elseif ($param['state']=='有效'){
  3803. // $cids = Customer::where($where)->column('id');
  3804. // $state2 = CustomerVisitLog::changeState('未到访', 'chaos');
  3805. // $state3 = CustomerVisitLog::changeState('已到访', 'chaos');
  3806. // $state4 = CustomerVisitLog::changeState('已到场', 'chaos');
  3807. // $state5 = CustomerVisitLog::changeState('已量房', 'chaos');
  3808. // $state6 = CustomerVisitLog::changeState('已交定', 'chaos');
  3809. // $state7 = CustomerVisitLog::changeState('已签单', 'chaos');
  3810. // $state = array_merge($state2, $state3, $state4, $state5, $state6, $state7);
  3811. // $v_where[] = ['customer_id','in',$cids];
  3812. // $v_where[] = ['state','in',$state];
  3813. // $ids = CustomerVisitLog::where($v_where)->group('customer_id')->column('customer_id');
  3814. // unset($where);
  3815. // $where[] = ['id','in',$ids];
  3816. // }
  3817. }
  3818. //打电话状态筛选
  3819. if (!empty($param['call_state'])) {
  3820. //电话未联系和未接通
  3821. $call_list = OutCallLog::where([['customer_id', 'in', $customer_id]])->order('addtime desc')->field('id,status,customer_id')->select()->toArray();
  3822. $call_ids = [];
  3823. $call_status = [];
  3824. $no_connect_ids = [];
  3825. $call_all = [];
  3826. foreach ($call_list as $key => $val) {
  3827. if (!in_array($val['customer_id'], $call_ids)) {
  3828. $call_ids[] = $val['customer_id'];
  3829. $call_status[$val['customer_id']] = $val['status'];
  3830. }
  3831. $call_all[$val['customer_id']][] = $val;
  3832. }
  3833. foreach ($call_status as $key => $val) {
  3834. if (in_array($val, [-1, -2])) $no_connect_ids[] = $key;
  3835. }
  3836. $no_call_ids = array_diff($customer_id, $call_ids) ? array_diff($customer_id, $call_ids) : [];
  3837. unset($where);
  3838. if ($param['call_state'] == '已联系') {
  3839. //2023-02-02 已联系包括打电话和remark不为空的客户
  3840. $id1 = array_column($call_list, 'customer_id');
  3841. $id2 = Customer::whereOr([[['id', 'in', $customer_id], ['remark', '<>', '']], [['id', 'in', $id1]]])->column('id');
  3842. $where[] = ['id', 'in', $id2];
  3843. // if (!empty($state_ids) && !empty($call_ids)) {
  3844. // $call_ids = array_intersect($state_ids, $call_ids);
  3845. // }
  3846. // $where[] = ['id', 'in', $call_ids];
  3847. }
  3848. if ($param['call_state'] == '未联系') {
  3849. //2023-02-02 未打电话并且remark为空
  3850. $id1 = array_column($call_list, 'customer_id');
  3851. $id2 = Customer::whereOr([[['id', 'in', $customer_id], ['remark', '<>', '']], [['id', 'in', $id1]]])->column('id');
  3852. $where[] = ['id', 'not in', $id2];
  3853. // if (!empty($state_ids) && !empty($no_call_ids)) {
  3854. // $no_call_ids = array_intersect($state_ids, $no_call_ids);
  3855. // }
  3856. $where[] = ['id', 'in', $no_call_ids];
  3857. }
  3858. if ($param['call_state'] == '未接通') {
  3859. if (!empty($state_ids) && !empty($no_connect_ids)) {
  3860. $no_connect_ids = array_intersect($state_ids, $no_connect_ids);
  3861. }
  3862. $where[] = ['id', 'in', $no_connect_ids];
  3863. }
  3864. }
  3865. //回收客户
  3866. if ($param['recovery'] == 1) {
  3867. if (empty($param['ids'])) return json(['code' => 1, 'data' => '请选择要回收的客户', 'msg' => '请选择要回收的客户']);
  3868. $where = [['id', 'in', explode(',', $param['ids'])]];
  3869. $list = Customer::where($where)->field('id,name,employee_id,org_id,designer_id,crm_res_id,state')->select()->toArray();
  3870. //---start 2023-01-04--------//已经交定签单不能回收
  3871. // $state_ids = array_column($list, 'id');
  3872. // $state11 = CustomerVisitLog::changeState('已交定', 'chaos');
  3873. // $state12 = CustomerVisitLog::changeState('已签单', 'chaos');
  3874. // $log_where = [
  3875. // ['customer_id', 'in', $state_ids],
  3876. // ['state', 'in', array_merge($state11, $state12)]
  3877. // ];
  3878. // $repeat_ids = CustomerVisitLog::where($log_where)->group('customer_id')->column('customer_id');
  3879. // foreach ($list as $item_key => $item_val) {
  3880. // if (in_array($item_val['id'], $repeat_ids)) {
  3881. // unset($list[$item_key]);
  3882. // $repeat_name[] = $item_val['name'];
  3883. // }
  3884. // }
  3885. $repeat_name = [];
  3886. foreach ($list as $item_key => $item_val) {
  3887. if (!in_array($item_val['state'], ['待确认', 0], true)) {
  3888. unset($list[$item_key]);
  3889. $repeat_name[] = $item_val['name'];
  3890. }
  3891. }
  3892. //---end 2023-01-04--------//已经交定签单不能回收
  3893. $info = CrmImportLog::where('id', $param['id'])->find();
  3894. $listids = CrmImportLog::where('id','>',0)->column('pid,id');
  3895. $cids = array_column($list,'id');
  3896. $recory = Customer::where([['id','in',$cids],['employee_id','not null',null]])->column('employee_id,id');
  3897. Db::startTrans();
  3898. try {
  3899. foreach($list as $k=>$v){
  3900. $this->recovery_crm($v,$param['id'],$listids);
  3901. }
  3902. Db::commit();
  3903. }catch (\Exception $e) {
  3904. Db::rollback();
  3905. //var_dump($e->getMessage());
  3906. return json(['code' => 1, 'msg' => '回收数据失败.']);
  3907. }
  3908. //保存回收记录 只保存分配到人的客户记录
  3909. $recorys = [];
  3910. foreach ($recory as $key => $val) {
  3911. $recorys[$val['employee_id']][] = $val['id'];
  3912. }
  3913. $saves = [];
  3914. $time = time();
  3915. foreach ($recorys as $key1 => $val1) {
  3916. $saves[] = [
  3917. 'root_id' => request()->employee->root_id,
  3918. 'employee_id' => $key1,
  3919. 'crm_res_id' => $param['id'],
  3920. 'type' => 1,
  3921. 'source_type' => 0,
  3922. 'count' => count($val1),
  3923. 'customer_ids' => implode(',',$val1),
  3924. 'addtime' => $time,
  3925. 'operator_employee_id' => $request->employee->id
  3926. ];
  3927. }
  3928. if($saves) CrmImportRetrieveLog::insertAll($saves);
  3929. if ($repeat_name) {
  3930. $name = implode(',', $repeat_name);
  3931. return json(['code' => 1, 'data' => '客户“' . $name . '”已经标记为有效无法回收', 'msg' => '客户“' . $name . '”已经标记为有效无法回收']);
  3932. } else {
  3933. return json(['code' => 0, 'data' => '回收完成', 'msg' => '回收完成']);
  3934. }
  3935. }
  3936. $list = Customer::with(['orgs' => function ($query) {
  3937. $query->field(['info', 'id', 'name'])->bind(['org_name' => 'name']);
  3938. }, 'designer' => function ($query) {
  3939. $query->field(['id', 'name'])->bind(['designer_name' => 'name']);
  3940. }, 'employee' => function ($query) {
  3941. $query->field(['id', 'name'])->bind(['sale_name' => 'name']);
  3942. }])->withCount(['outcall'])->where($where)->page($param['page'], $param['limit'])->field('id,employee_id,name,community_name,org_id,designer_id,crm_res_id,state,employee_time')->order('employee_id asc,id asc')->select();
  3943. $recovery = Customer::where($where)->column('id');
  3944. $count = count($recovery);
  3945. $recovery = implode(',',$recovery);
  3946. //分配时间
  3947. $times_arr = CrmImportLog::where([['id', 'in', array_column($list->toArray(), 'crm_res_id')]])->column('addtime', 'id');
  3948. foreach ($list as $key => $val) {
  3949. $list[$key]['allocate_time'] = $times_arr[$val['crm_res_id']];
  3950. }
  3951. // 状态统计获取
  3952. $customersStateList = CustomerVisitLog::where([['customer_id', 'in', array_column($list->toArray(), 'id')]])->group('state,customer_id')->field('count(state) as num, state, customer_id')->select()->toArray();
  3953. $customersState = [];
  3954. foreach ($customersStateList as $s) {
  3955. if (!isset($customersState[$s['customer_id']]))
  3956. $customersState[$s['customer_id']] = ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'ydf' => 0, 'wdf' => 0];
  3957. $customersState[$s['customer_id']]['count'] += $s['num'];
  3958. // 到店,量房,活动,定金,签单
  3959. if ($s['state'] == '已到店') $customersState[$s['customer_id']]['shop'] += $s['num'];
  3960. elseif ($s['state'] == '已量房') $customersState[$s['customer_id']]['measure'] += $s['num'];
  3961. elseif ($s['state'] == '已到场') $customersState[$s['customer_id']]['activity'] += $s['num'];
  3962. elseif ($s['state'] == '已交定') $customersState[$s['customer_id']]['deposit'] += $s['num'];
  3963. elseif ($s['state'] == '已签单') $customersState[$s['customer_id']]['signed'] += $s['num'];
  3964. elseif ($s['state'] == '已到访') $customersState[$s['customer_id']]['ydf'] += $s['num'];
  3965. elseif ($s['state'] == '未到访') $customersState[$s['customer_id']]['wdf'] += $s['num'];
  3966. }
  3967. $list = $list->toArray();
  3968. $state1 = Customer::changeState('待确认', 'chaos');
  3969. $state2 = Customer::changeState('无效', 'chaos');
  3970. foreach ($list as $key => &$value) {
  3971. $value['stateNum'] = $customersState[$value['id']] ?? ['count' => 0, 'shop' => 0, 'measure' => 0, 'activity' => 0, 'deposit' => 0, 'signed' => 0, 'ydf' => 0, 'wdf' => 0];
  3972. // if(!empty($call_all) && !empty($call_all[$value['id']])){
  3973. // $value['call_num'] = count($call_all[$value['id']]);
  3974. // }else{
  3975. // $value['call_num'] = 0;
  3976. // }
  3977. //2023-02-02 修改逻辑 分配后为待确认状态,不为待确认和无效就是有效状态
  3978. $value['state'] = in_array($value['state'], $state1, true) ? '待确认' : (in_array($value['state'], $state2) ? '无效' : '有效');
  3979. }
  3980. return json(['code' => 0, 'data' => $list, 'count' => $count,'recovery'=>$recovery]);
  3981. }
  3982. /**
  3983. * 循环调用回收客户
  3984. */
  3985. public function recovery_crm($crm, $crm_res_id, $listids)
  3986. {
  3987. $crm_log = CrmImportLog::where('id', $crm['crm_res_id'])->find();
  3988. if ($crm_res_id != $crm_log['id']) {
  3989. $upids = $this->getInfo($crm_log['id'], $listids, $crm_res_id);
  3990. foreach ($upids as $k => $v) {
  3991. $res_data = CrmImportLog::find($v['id']);
  3992. $count = $res_data->count - 1;
  3993. $left_num = $res_data->left_num - 1;
  3994. $res_data->count = $count ? $count : 0;
  3995. if ($crm_log->id == $res_data->id && empty($crm['employee_id'])) $res_data->left_num = $left_num ? $left_num : 0;
  3996. $res_data->save();
  3997. }
  3998. }
  3999. $ben_log = CrmImportLog::where('id', $crm_res_id)->find();
  4000. $ben_left_num = $ben_log->left_num + 1;
  4001. $ben_log->left_num = $ben_left_num ?: 0;
  4002. $ben_log->save();
  4003. $is_distribution = $ben_log->pid ? 1 : 0;
  4004. Customer::where([['id', '=', $crm['id']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]);
  4005. }
  4006. /**
  4007. * 电话联系次数详情
  4008. */
  4009. public function call_cuslist()
  4010. {
  4011. $request = request();
  4012. $param = $request->only(['customer_id' => 0, 'page' => 1, 'limit' => 10]);
  4013. if (!request()->isAjax()) {
  4014. View::assign('customer_id', $param['customer_id']);
  4015. return View::fetch();
  4016. }
  4017. $info = Customer::where('id', $param['customer_id'])->find();
  4018. if (empty($info)) return json(['code' => 1, 'msg' => '客户不存在']);
  4019. $list = OutCallLog::where('customer_id', $param['customer_id'])->field('id,fwd_start_time,status')->page($param['page'], $param['limit'])->select()->toArray();
  4020. foreach ($list as $key => $val) {
  4021. $list[$key]['status_name'] = '无';
  4022. if ($val['status'] == 1) $list[$key]['status_name'] = '接通';
  4023. if ($val['status'] == -1) $list[$key]['status_name'] = '未接通';
  4024. if ($val['status'] == -2) $list[$key]['status_name'] = '未接听';
  4025. }
  4026. $count = OutCallLog::where('customer_id', $param['customer_id'])->count();
  4027. return json(['code' => 0, 'data' => $list, 'msg' => '获取成功', 'count' => $count]);
  4028. }
  4029. /**
  4030. * 已分配客户列表
  4031. * 分配弹框页面接口
  4032. */
  4033. public function recovery_customers()
  4034. {
  4035. $request = request();
  4036. // $root_id = $request->employee->root_id;
  4037. $param = $request->only(['customer_id' => 0, 'crm_res_id']);
  4038. if (!$this->checkResourceIsMy($param['crm_res_id'])) return;
  4039. $info = Customer::where('id', $param['customer_id'])->findOrEmpty();
  4040. if (!$info->isEmpty() && $info->crm_res_id) {
  4041. // $state11 = CustomerVisitLog::changeState('已交定', 'chaos');
  4042. // $state12 = CustomerVisitLog::changeState('已签单', 'chaos');
  4043. // $log_where = [
  4044. // ['customer_id', '=', $param['customer_id']],
  4045. // ['state', 'in', array_merge($state11, $state12)]
  4046. // ];
  4047. // $repeat = CustomerVisitLog::where($log_where)->findOrEmpty();
  4048. // if (!$repeat->isEmpty()) {
  4049. // return json(['code' => 1, 'data' => '客户“' . $info->name . '”已经交定或签单无法回收', 'msg' => '客户“' . $info->name . '”已经交定或签单无法回收']);
  4050. // }
  4051. if (!in_array($info->state, [0, '待确认'], true)) {
  4052. return json(['code' => 1, 'data' => '客户“' . $info->name . '”已标记为有效', 'msg' => '客户“' . $info->name . '”已标记为有效无法回收']);
  4053. }
  4054. // //客户所在资源库
  4055. // $crm_log = CrmImportLog::where('id', $info->crm_res_id)->find();
  4056. // //如果有上级资源库上级资源库+1 本级资源库-1
  4057. // if ($crm_log->pid) {
  4058. // //本级资源库-1
  4059. // $count = $crm_log->count - 1;
  4060. // $crm_log->count = $count ?: 0;
  4061. // $crm_log->save();
  4062. // //上级资源库
  4063. // $pid_info = CrmImportLog::where('id', $crm_log->pid)->find();
  4064. // Customer::where([['id', '=', $param['customer_id']]])->update(['crm_res_id' => $pid_info->id, 'org_id' => $pid_info->org_id, 'employee_id' => null,'employee_time'=>null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => 0,'state'=>'待确认']);
  4065. // $pid = $pid_info->id;
  4066. // //查询未分配数量
  4067. // $left_num = Customer::where([['crm_res_id', '=', $crm_log->id], ['employee_id', '=', null]])->count();
  4068. // CrmImportLog::where('id', $crm_log->id)->update(['left_num' => $left_num]);
  4069. // } else {
  4070. // $pid = $crm_log->id;
  4071. // Customer::where([['id', '=', $param['customer_id']]])->update(['org_id' => $crm_log->org_id, 'employee_id' => null, 'employee_time'=>null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => 0,'state'=>'待确认']);
  4072. // }
  4073. // //查询未分配数量
  4074. // $left_num = Customer::where([['crm_res_id', '=', $pid], ['employee_id', '=', null],['is_distribution','=',0]])->count();
  4075. // CrmImportLog::where('id', $pid)->update(['left_num' => $left_num]);
  4076. Db::startTrans();
  4077. try {
  4078. $crm_log = CrmImportLog::where('id', $info->crm_res_id)->find();
  4079. if ($param['crm_res_id'] != $crm_log['id']) {
  4080. $listids = CrmImportLog::where('id', '>', 0)->column('pid,id');
  4081. // var_dump($crm_log['id']);
  4082. // exit;
  4083. $upids = $this->getInfo($crm_log['id'], $listids, $param['crm_res_id']);
  4084. // var_dump($upids);
  4085. // exit;
  4086. foreach ($upids as $k => $v) {
  4087. $res_data = CrmImportLog::find($v['id']);
  4088. $count = $res_data->count - 1;
  4089. $left_num = $res_data->left_num - 1;
  4090. $res_data->count = $count ? $count : 0;
  4091. if ($info->crm_res_id == $res_data->id && empty($info->employee_id)) $res_data->left_num = $left_num ? $left_num : 0;
  4092. $res_data->save();
  4093. }
  4094. }
  4095. $ben_log = CrmImportLog::where('id', $param['crm_res_id'])->find();
  4096. $ben_left_num = $ben_log->left_num + 1;
  4097. $ben_log->left_num = $ben_left_num ?: 0;
  4098. $ben_log->save();
  4099. $is_distribution = $ben_log->pid ? 1 : 0;
  4100. Customer::where([['id', '=', $param['customer_id']]])->update(['crm_res_id' => $ben_log->id, 'org_id' => $ben_log->org_id, 'employee_id' => null, 'employee_time' => null, 'is_resource' => 1, 'fresh' => 1, 'is_distribution' => $is_distribution, 'state' => 0]);
  4101. //保存回收记录
  4102. if ($info->employee_id) {
  4103. CrmImportRetrieveLog::insert([
  4104. 'root_id' => request()->employee->root_id,
  4105. 'employee_id' => $info->employee_id,
  4106. 'crm_res_id' => $param['crm_res_id'],
  4107. 'type' => 1,
  4108. 'source_type' => 0,
  4109. 'count' => 1,
  4110. 'customer_ids' => $param['customer_id'],
  4111. 'addtime' => time(),
  4112. 'operator_employee_id' => request()->employee->id
  4113. ]);
  4114. }
  4115. Db::commit();
  4116. } catch (\Exception $e) {
  4117. Db::rollback();
  4118. var_dump($e->getMessage());
  4119. return json(['code' => 1, 'msg' => '回收失败.']);
  4120. }
  4121. return json(['code' => 0, 'data' => '回收成功', 'msg' => '回收成功']);
  4122. } else {
  4123. return json(['code' => 0, 'data' => '回收成功', 'msg' => '回收成功']);
  4124. }
  4125. }
  4126. /**
  4127. * 查询所有的上级id
  4128. */
  4129. function getInfo($id, $all, $pid)
  4130. {
  4131. $result = [];
  4132. if ($id != $pid) { //将id等于0作为递归终点,如果要将无法查找到作为递归终点,也要在下面的循环遍历之后判断
  4133. foreach ($all as $item) {
  4134. if ($item['id'] == $id) {
  4135. $result[] = $item;
  4136. break;
  4137. }
  4138. }
  4139. $parents = $this->getInfo($result[0]['pid'], $all, $pid);
  4140. if (!empty($parents)) {
  4141. $result = array_merge($result, $parents);
  4142. }
  4143. }
  4144. return $result;
  4145. }
  4146. /**
  4147. * 未分配分配客户列表
  4148. * 分配弹框页面接口
  4149. */
  4150. public function get_uncustomers()
  4151. {
  4152. $request = request();
  4153. $root_id = $request->employee->root_id;
  4154. $param = $request->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'org_id' => 0, 'id' => 0]);
  4155. if (!$this->checkResourceIsMy($param['id'])) return;
  4156. $where[] = ['employee_id', '=', NULL];
  4157. $where[] = ['crm_res_id', '=', $param['id']];
  4158. $where[] = ['is_distribution', '=', 0];
  4159. if ($param['keyword']) {
  4160. $ids = [];
  4161. $keyword = Customer::where($where)->field('id,community_name,phone,phone1,phone2')->select();
  4162. foreach ($keyword as $key => $val) {
  4163. $str = $val['community_name'] . $val['phone'] . $val['phone1'] . $val['phone2'];
  4164. if (strpos($str, $param['keyword']) !== false) $ids[] = $val['id'];
  4165. }
  4166. $where[] = ['id', 'in', $ids];
  4167. }
  4168. $customer_ids = Customer::where($where)->column('id');
  4169. //排除已经分配后又置为无效的数据
  4170. // $query[] = ['customer_id', 'in', $customer_ids];
  4171. // $customer_ids1 = CustomerVisitLog::where($query)->column('customer_id');
  4172. // $where[] = ['id', 'in', array_diff($customer_ids, $customer_ids1)];
  4173. $list = Customer::where($where)->field('id,name,community_name,square,phone,phone1,phone2')->page($param['page'], $param['limit'])->select();
  4174. foreach ($list as &$item) {
  4175. $item['phone'] = substr_replace($item['phone'], '******', 3, 6);
  4176. $item['phone1'] = $item['phone1'] ? substr_replace($item['phone1'], '******', 3, 6) : '';
  4177. $item['phone2'] = $item['phone2'] ? substr_replace($item['phone2'], '******', 3, 6) : '';
  4178. }
  4179. $count = Customer::where($where)->count();
  4180. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  4181. }
  4182. /**
  4183. * 获取导入失败的客户
  4184. */
  4185. public function get_error_customers(){
  4186. $param = request()->only(['page' => 1, 'limit' => 10, 'keyword' => '', 'id' => 0]);
  4187. $list = CrmImportRecord::where([['log_id', '=', $param['id']], ['content', 'not null', null]])->order('id asc')->column('content');
  4188. $new_list = [];
  4189. foreach($list as $k => $v){
  4190. if (!empty($v)) {
  4191. $one = json_decode($v);
  4192. $new_list = array_merge($new_list, $one);
  4193. }
  4194. }
  4195. $count = count($new_list);
  4196. $data = array_slice($new_list, ($param['page'] - 1) * $param['limit'], $param['limit']);
  4197. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  4198. }
  4199. /**
  4200. * 获取子部门
  4201. */
  4202. public function get_child_orgs($id)
  4203. {
  4204. $info = Org::where('id', $id)->findOrEmpty();
  4205. $path = $info->path;
  4206. $ids = Org::where([['path', 'like', $path . '%']])->column('id,name');
  4207. return json(['code' => 0, 'data' => array_column($ids, 'id'), 'param' => $ids]);
  4208. }
  4209. /**
  4210. * 新详情页
  4211. * 详情页
  4212. */
  4213. public function read_new()
  4214. {
  4215. $request = request();
  4216. $id = input('id', 0);
  4217. if (!$this->checkResourceIsMy($id)) return;
  4218. $type = input('type', 0); //handdistribution
  4219. //基本信息
  4220. $data = $this->read($id);
  4221. View::assign('data', $data);
  4222. $counts = $this->get_customer_data($id);
  4223. View::assign('counts', $counts);
  4224. View::assign('id', $id);
  4225. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name')->select()->toArray();
  4226. View::assign('org', $org);
  4227. View::assign('type', $type);
  4228. $root_id = $request->employee->root_id;
  4229. View::assign('root_id', $root_id);
  4230. return View::fetch();
  4231. }
  4232. /**
  4233. * 删除资源
  4234. */
  4235. public function delete_new()
  4236. {
  4237. $request = request();
  4238. $id = input('id', 0);
  4239. $root_id = $request->employee->root_id;
  4240. if (!$this->checkResourceIsMy($id)) return;
  4241. $orgids = orgSubIds($root_id);
  4242. if ($id > 0) {
  4243. $where[] = ['id', '=', $id];
  4244. $where[] = ['org_id', 'in', $orgids];
  4245. $info = CrmImportLog::where($where)->findOrEmpty();
  4246. $listids = CrmImportLog::where([['id', '>', $info['id']], ['org_id', 'in', $orgids]])->column('id,pid');
  4247. $ids = $this->getAllLowerLevelId($listids, $info['id']);
  4248. $info->delete();
  4249. CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete();
  4250. //记录操作删除资源库信息
  4251. $msg = '操作回收客户员工id:' . $request->employee->id . '要删除资源库的id' . $id;
  4252. trace($msg,'资源删除');
  4253. return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']);
  4254. // if ($info->state == 0) {
  4255. // $info->delete();
  4256. // CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete();
  4257. // //CrmImportLog::where($where)->delete();
  4258. // if ($info->count > 0) Customer::where([['crm_res_id', '=', $id]])->useSoftDelete('delete_time', time())->delete();
  4259. // return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']);
  4260. // } else {
  4261. // $iscus = Customer::where([['crm_res_id', 'in', $ids], ['org_id', 'in', $orgids], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->count();
  4262. // $bencus = Customer::where([['crm_res_id', '=', $info['id']], ['org_id', 'in', $orgids], ['employee_id', 'NOTNULL', null], ['state', 'in', Customer::changeState('待确认', 'chaos')]])->count();
  4263. // if ($iscus && $bencus) return json(['code' => 1, 'msg' => '请先回收未转化为客户的资源']);
  4264. // //删除已经回收的客户
  4265. // //$isdelcus = Customer::where([['crm_res_id','=',$info['id']],['state','in',Customer::changeState('待确认','chaos')]])->delete();
  4266. // $info->delete();
  4267. // CrmImportLog::where([['id', 'in', $ids]])->useSoftDelete('delete_time', time())->delete();
  4268. // return json(['code' => 0, 'data' => '删除成功', 'msg' => '删除成功']);
  4269. // }
  4270. }
  4271. return json(['code' => 1, 'data' => '删除失败', 'msg' => '删除失败']);
  4272. }
  4273. /**
  4274. * 客户转移
  4275. */
  4276. public function transfer()
  4277. {
  4278. if (!request()->isAjax()) {
  4279. $customer_id = input('data', '', 'trim'); //客户的id
  4280. View::assign('customer_id', $customer_id);
  4281. $customer_ids = array_filter(explode(',', $customer_id));
  4282. View::assign('count', count($customer_ids));
  4283. return View::fetch();
  4284. } else {
  4285. $request = request();
  4286. $param = $request->only(['content' => [], 'customer_id' => '']);
  4287. if (empty($param['content']) || !is_array($param['content'])) return json(['code' => 1, 'data' => '请选择分配对象', 'msg' => '请选择分配对象']);
  4288. $customer_id = explode(',', $param['customer_id']);
  4289. $root_id = request()->employee->root_id;
  4290. $orgSub = orgSubIds($root_id);
  4291. $customer_id = Customer::where([['id', 'in', $customer_id], ['org_id', 'in', $orgSub]])->column('id');
  4292. shuffle($customer_id); //随机分配
  4293. $count = count($customer_id);
  4294. $all_count = array_sum(array_column($param['content'], 'count'));
  4295. if ($all_count > $count) return json(['code' => 1, 'data' => '客户转移数量不能大于被转移客户总数', 'msg' => '客户转移数量不能大于被转移客户总数']);
  4296. $repeat = []; // 无法分配
  4297. //指派类型
  4298. //分配到人去重
  4299. $eids = array_column($param['content'], 'con_id');
  4300. $phones = Customer::where([['employee_id', 'in', $eids]])->column('employee_id,name,phone,phone1,phone2');
  4301. $phone_emp = []; //待分配的业务员
  4302. foreach ($phones as $k => $v) {
  4303. $phone_emp[$v['employee_id']] = isset($phone_emp[$v['employee_id']]) ? $phone_emp[$v['employee_id']] : [];
  4304. array_push($phone_emp[$v['employee_id']], $v['phone'], $v['phone1'], $v['phone2']);
  4305. }
  4306. foreach ($phone_emp as $k => $v) {
  4307. $phone_emp[$k] = array_unique(array_filter($v));
  4308. }
  4309. foreach ($param['content'] as $k => $v) {
  4310. //原有手机号
  4311. $param['content'][$k]['phones'] = isset($phone_emp[$v['con_id']]) ? $phone_emp[$v['con_id']] : [];
  4312. //分配的客户
  4313. $param['content'][$k]['cids'] = [];
  4314. }
  4315. //分配
  4316. $customer = Customer::where([['id', 'in', $customer_id]])->column('id,name,phone,phone1,phone2');
  4317. $t_customer = []; //已转移客户
  4318. foreach ($customer as $k => $v) {
  4319. $ls_phones = array_filter([$v['phone'], $v['phone1'], $v['phone2']]);
  4320. $i = 0;
  4321. foreach ($param['content'] as $k1 => $v1) {
  4322. //没有分配完成
  4323. if ($v1['count'] > count($v1['cids'])) {
  4324. //并且手机号不重复
  4325. if ($v1['phones'] == []) {
  4326. $param['content'][$k1]['cids'][] = $v['id'];
  4327. $i = 1;
  4328. break;
  4329. } else {
  4330. $phone_intersect = array_intersect($v1['phones'], $ls_phones);
  4331. if ($phone_intersect) {
  4332. continue; //手机号重复 分配给下一个员工
  4333. } else {
  4334. $param['content'][$k1]['cids'][] = $v['id'];
  4335. $i = 1;
  4336. break;
  4337. }
  4338. }
  4339. }
  4340. }
  4341. //无法分配
  4342. if ($i == 0) {
  4343. $repeat[$v['id']] = $v['name'];
  4344. }
  4345. }
  4346. //分配
  4347. $visitlog = [];
  4348. $date = date('Y-m-d');
  4349. $old_employee = []; // 原先的员工
  4350. $old_manager = []; // 原先的员工领导
  4351. $new_manager = []; // 新员工领导
  4352. $org_list = [];
  4353. foreach ($param['content'] as $k1 => $v1) {
  4354. if ($v1['cids']) {
  4355. $new_org = Employee::where('id', '=', $v1['con_id'])->value('org_id');
  4356. $t_customer = array_merge($t_customer, $v1['cids']);
  4357. $emp = Employee::where('id', $v1['con_id'])->findOrEmpty();
  4358. $old_employee_s = Customer::where([['id', 'in', $v1['cids']]])->column('employee_id,name');
  4359. $old_employee = array_merge($old_employee, $old_employee_s);
  4360. // 同一部门内转移不给领导通知
  4361. foreach ($old_employee_s as $k_old => $v_old) {
  4362. $org_id = Employee::where('id', '=', $v_old['employee_id'])->value('org_id');
  4363. if ($new_org != $org_id) {
  4364. // 旧部门领导
  4365. if (isset($org_list[$org_id])) {
  4366. $manager = $org_list[$org_id];
  4367. } else {
  4368. $manager = Employee::where([['org_id', '=', $org_id], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id');
  4369. if ($manager) {
  4370. $org_list[$org_id] = $manager;
  4371. }
  4372. }
  4373. if ($manager) {
  4374. foreach ($manager as $m) {
  4375. $old_manager[$m][] = $v_old['name'] ? $v_old['name'] : '未知';
  4376. }
  4377. }
  4378. // 新部门领导
  4379. if (isset($org_list[$new_org])) {
  4380. $manager = $org_list[$new_org];
  4381. } else {
  4382. $manager = Employee::where([['org_id', '=', $new_org], ['is_manager', '=', 1], ['state', '=', '在职']])->column('id');
  4383. if ($manager) {
  4384. $org_list[$new_org] = $manager;
  4385. }
  4386. }
  4387. if ($manager) {
  4388. foreach ($manager as $m) {
  4389. $new_manager[$m][] = $v_old['name'] ? $v_old['name'] : '未知';
  4390. }
  4391. }
  4392. }
  4393. $old_employee_s[$k_old]['name'] = $v_old['name'] ? $v_old['name'] : '未知';
  4394. }
  4395. Customer::where([['id', 'in', $v1['cids']]])->update([
  4396. 'employee_id' => $v1['con_id'],
  4397. 'org_id' => $emp->org_id,
  4398. 'is_resource' => 0,
  4399. 'is_distribution' => 1
  4400. ]);
  4401. event(new Msg($v1['con_id'], '有' . count($v1['cids']) . '个客户转移给您,请及时查看!', 'customerTransfer'));
  4402. $title = '您好!你有' . count($v1['cids']) . '个新的分配客户,请及时查看';
  4403. $this->sendOfficialAccountTransfer($v1['con_id'], array_column($old_employee_s, 'name'), $title);
  4404. //添加跟进记录
  4405. foreach ($v1['cids'] as $v2) {
  4406. $visitlog[] = [
  4407. 'customer_id' => $v2,
  4408. 'type' => '',
  4409. 'remark' => '客户转移',
  4410. 'employee_id' => $v1['con_id'],
  4411. 'user_id' => $emp->uid,
  4412. 'state' => '待确认',
  4413. 'next_contact_date' => $date
  4414. ];
  4415. }
  4416. }
  4417. }
  4418. $old_msg_employee = [];
  4419. foreach ($old_employee as $k => $v) {
  4420. $old_msg_employee[$v['employee_id']][] = $v['name'] ? $v['name'] : '未知';
  4421. }
  4422. // 通知原先的员工
  4423. foreach ($old_msg_employee as $k => $v) {
  4424. event(new Msg($k, '您名下有' . count($v) . '个客户已被转移', 'customerTransfer'));
  4425. $title = '您好!你名下有' . count($v) . '个客户已被转移';
  4426. $this->sendOfficialAccountTransfer($k, $v, $title);
  4427. }
  4428. // 通知原先部门领导
  4429. foreach ($old_manager as $k => $v) {
  4430. event(new Msg($k, '您部门有' . count($v) . '个客户已被转移', 'customerTransfer'));
  4431. $title = '您好!你部门有' . count($v) . '个客户已被转移';
  4432. $this->sendOfficialAccountTransfer($k, $v, $title);
  4433. }
  4434. // 通知新部门领导
  4435. foreach ($new_manager as $k => $v) {
  4436. event(new Msg($k, '您部门新分配' . count($v) . '个客户', 'customerTransfer'));
  4437. $title = '您好!你部门新分配' . count($v) . '个客户,请及时处理';
  4438. $this->sendOfficialAccountTransfer($k, $v, $title);
  4439. }
  4440. if ($visitlog) CustomerVisitLog::insertAll($visitlog);
  4441. //查询未分配数量
  4442. // $diff = array_diff($customer_id, $t_customer);
  4443. if ($repeat) {
  4444. $name = implode(',', array_values($repeat));
  4445. return json(['code' => 1, 'data' => '客户:' . $name . '。手机号重复无法分配', 'msg' => '客户:' . $name . '。手机号重复无法分配']);
  4446. } else {
  4447. return json(['code' => 0, 'data' => '分配完成', 'msg' => '分配完成']);
  4448. }
  4449. }
  4450. }
  4451. /**
  4452. * 发送公众号消息(客户转移)
  4453. * employee_id => count 业务员id =》 分配数量
  4454. */
  4455. public function sendOfficialAccountTransfer($employee, $customer, $title)
  4456. {
  4457. $client_type = Miniprogram::where('root_id', request()->employee->root_id)->value('notify');
  4458. $keword1 = implode(',', $customer);
  4459. //业务员客户数量
  4460. $data = $msg = [];
  4461. $data['page'] = "/pages/index/index?office_type=allocation&clientype=" . $client_type;
  4462. $data['param'] = [
  4463. 'first' => $title, //标题
  4464. 'keyword1' => $keword1,
  4465. 'keyword2' => date('Y-m-d H:i:s'),
  4466. 'remark' => '感谢您的使用' //
  4467. ];
  4468. $msg = [
  4469. 'type' => 'customerTransfer',
  4470. 'employee_id' => $employee
  4471. ];
  4472. EmployeeMsg::temploateMsg($msg, $data);
  4473. return true;
  4474. }
  4475. /**
  4476. * 资源标签设置
  4477. */
  4478. public function label()
  4479. {
  4480. $employee = request()->employee;
  4481. $labelSetting = Setting::where(['root_id' => $employee->root_id, 'name' => 'crm_label', 'state' => 1])->findOrEmpty();
  4482. $label = $labelSetting->isEmpty() ? [] : explode(',', $labelSetting->content);
  4483. view::assign('label', $label);
  4484. return View::fetch();
  4485. }
  4486. /**
  4487. * 添加资源标识
  4488. */
  4489. public function addLabel($value)
  4490. {
  4491. $employee = request()->employee;
  4492. $labelSetting = Setting::where(['root_id' => $employee->root_id, 'name' => 'crm_label', 'state' => 1])->findOrEmpty();
  4493. if ($labelSetting->isEmpty()) {
  4494. $labelSetting->root_id = $employee->root_id;
  4495. $labelSetting->name = 'crm_label';
  4496. $labelSetting->state = 1;
  4497. } else {
  4498. $label = explode(',', $labelSetting->content);
  4499. }
  4500. $label[] = $value;
  4501. $labelSetting->content = implode(',', $label);
  4502. $labelSetting->save();
  4503. return json(['code' => 0, 'msg' => '添加成功']);
  4504. }
  4505. /**
  4506. * 删除资源标识
  4507. */
  4508. public function deleteLabel($value)
  4509. {
  4510. $employee = request()->employee;
  4511. $labelSetting = Setting::where(['root_id' => $employee->root_id, 'name' => 'crm_label', 'state' => 1])->find();
  4512. if ($labelSetting->isEmpty()) return json(['code' => 0, 'msg' => '删除成功']);
  4513. $label = explode(',', $labelSetting->content);
  4514. $key = array_search($value, $label);
  4515. unset($label[$key]);
  4516. $labelSetting->content = implode(',', $label);
  4517. $labelSetting->save();
  4518. return json(['code' => 0, 'msg' => '删除成功']);
  4519. }
  4520. /**
  4521. * 资源库自动回收设置
  4522. */
  4523. public function auto_recovery()
  4524. {
  4525. $employee = request()->employee;
  4526. $param = request()->only(['day' => 0]);
  4527. $data = Setting::where(['root_id' => $employee->root_id, 'name' => 'auto_recovery', 'state' => 1])->findOrEmpty();
  4528. if (!request()->isAjax()) {
  4529. View::assign('day', $data->isEmpty() ? 0 : $data->content);
  4530. return View::fetch();
  4531. }
  4532. if ($data->isEmpty()) {
  4533. $data->root_id = $employee->root_id;
  4534. $data->name = 'auto_recovery';
  4535. $data->state = 1;
  4536. $data->content = $param['day'];
  4537. } else {
  4538. $data->content = $param['day'];
  4539. }
  4540. $data->save();
  4541. return json(['code' => 0, 'msg' => '设置成功']);
  4542. }
  4543. /**
  4544. * 检测资源的操作人是否是上传人
  4545. */
  4546. private function checkResourceIsMy($resourceid){
  4547. $resourcefb = Setting::where(['root_id'=>request()->employee->root_id, 'name'=>'resource_fp'])->value('content');
  4548. if($resourcefb !== null && $resourcefb == 0) return true;
  4549. $resource = CrmImportLog::find($resourceid);
  4550. if($resource->upload_source == 0 && $resource->employee_id != request()->employee->id) return false;
  4551. return true;
  4552. }
  4553. /**
  4554. * 网销渠道设置
  4555. */
  4556. public function channel_index(){
  4557. $request = request();
  4558. $root_id = $request->employee->root_id;
  4559. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  4560. $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  4561. $data = ['fish_account' => '', 'fish_adv' => '', 'fish_open' => 0, 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_refresh_time' => 0,'tx_account' => '', 'fish_update_time' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => ''];
  4562. if (isset($channel_setting)&&$channel_setting) $data = json_decode($channel_setting, true);
  4563. $appid = config('app.fish_clue_appid');
  4564. $secret = config('app.fish_clue_secret');
  4565. $url = (new Fish($appid, $secret))->authorizedAddress($root_id); //生成授权地址
  4566. $tx_url = (new Tencent(config('app.tx_clue_appid'),config('app.tx_clue_secret')))->authorizedAddress($root_id);
  4567. View::assign('url', $url);
  4568. View::assign('fish_account_token', $aec->encrypt($root_id));
  4569. View::assign('tx_url', $tx_url);
  4570. View::assign('data', $data);
  4571. // todo 威弘
  4572. $channel_setting_wh = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
  4573. // $aec = new Aec(config('app.aec_key'), config('app.aec_iv'));
  4574. $data_wh = ['fish_account' => '', 'fish_adv' => '', 'fish_open' => 0, 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_refresh_time' => 0,'tx_account' => '', 'fish_update_time' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => ''];
  4575. if (isset($channel_setting_wh)&&$channel_setting_wh) $data_wh = json_decode($channel_setting_wh, true);
  4576. // if(empty($data_wh['fish_account']))$data_wh['fish_account'] = $aec->encrypt($root_id);
  4577. $appid_wh = config('app.fish_clue_appid_wh');
  4578. $secret_wh = config('app.fish_clue_secret_wh');
  4579. $url_wh = (new \clue\FishWh($appid_wh, $secret_wh))->authorizedAddress($root_id); //生成授权地址
  4580. View::assign('url_wh', $url_wh);
  4581. // $data_wh['fish_account_wh'] = $aec->encrypt($root_id);
  4582. $data_wh['fish_adv_wh'] = $data_wh['fish_adv']??'';
  4583. View::assign('data_wh', $data_wh);
  4584. return View::fetch();
  4585. }
  4586. /**
  4587. * 设置网销渠道
  4588. */
  4589. public function channel_edit()
  4590. {
  4591. $request = request();
  4592. $root_id = $request->employee->root_id;
  4593. $param = $request->only(['fish_account' => '', 'fish_access_token' => '', 'fish_refresh_token' => '', 'fish_update_time' => '', 'fish_refresh_time' => 0, 'fish_adv' => '', 'fish_open' => 0, 'tx_account' => '', 'tx_adv' => '', 'tx_open' => 0, 'tx_access_token' => '', 'tx_refresh_token' => '', 'tx_update_time' => '']);
  4594. //todo 威弘
  4595. $param_wh_post = $request->only([
  4596. 'fish_account_wh' => '',
  4597. 'fish_access_token' => '',
  4598. 'fish_refresh_token' => '',
  4599. 'fish_update_time' => '',
  4600. 'fish_refresh_time' => 0,
  4601. 'fish_adv_wh' => '',
  4602. 'fish_open' => 0,
  4603. 'tx_account' => '',
  4604. 'tx_adv' => '',
  4605. 'tx_open' => 0,
  4606. 'tx_access_token' => '',
  4607. 'tx_refresh_token' => '',
  4608. 'tx_update_time' => ''
  4609. ]);
  4610. $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting');
  4611. if (isset($channel_setting) && $channel_setting) {
  4612. $data = json_decode($channel_setting, true);
  4613. $param['fish_access_token'] = $data['fish_access_token'];
  4614. $param['fish_refresh_token'] = $data['fish_refresh_token'];
  4615. $param['fish_update_time'] = $data['fish_update_time'];
  4616. $param['fish_refresh_time'] = $data['fish_refresh_time'];
  4617. $param['tx_access_token'] = $data['tx_access_token'];
  4618. $param['tx_refresh_token'] = $data['tx_refresh_token'];
  4619. $param['tx_update_time'] = isset($data['tx_update_time'])?$data['tx_update_time']:'';
  4620. }
  4621. Company::where(['root_id' => $root_id])->update(['channel_setting' => json_encode($param)]);
  4622. //todo 威弘
  4623. $param_wh['fish_open'] = 0;
  4624. $param_wh['tx_account'] = '';
  4625. $param_wh['tx_adv'] = '';
  4626. $param_wh['tx_open'] = 0;
  4627. $param_wh['fish_account'] = $param_wh_post['fish_account_wh'];
  4628. $param_wh['fish_adv'] = $param_wh_post['fish_adv_wh'];
  4629. $param_wh['fish_account_wh'] = $param_wh_post['fish_account_wh'];
  4630. $param_wh['fish_access_token'] = $param_wh_post['fish_access_token'];
  4631. $param_wh['fish_refresh_token'] = $param_wh_post['fish_refresh_token'];
  4632. $param_wh['fish_update_time'] = $param_wh_post['fish_update_time'];
  4633. $param_wh['fish_refresh_time'] = $param_wh_post['fish_refresh_time'];
  4634. $param_wh['tx_access_token'] = $param_wh_post['fish_refresh_token'];
  4635. $param_wh['tx_refresh_token'] = $param_wh_post['fish_update_time'];
  4636. $param_wh['tx_update_time'] = $param_wh_post['fish_refresh_time'];
  4637. $channel_setting_wh = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
  4638. if (isset($channel_setting_wh) && $channel_setting_wh) {
  4639. $data_wh = json_decode($channel_setting_wh, true);
  4640. $param_wh['fish_access_token'] = $data_wh['fish_access_token'];
  4641. $param_wh['fish_refresh_token'] = $data_wh['fish_refresh_token'];
  4642. $param_wh['fish_update_time'] = $data_wh['fish_update_time'];
  4643. $param_wh['fish_refresh_time'] = $data_wh['fish_refresh_time'];
  4644. $param_wh['tx_access_token'] = $data_wh['tx_access_token'];
  4645. $param_wh['tx_refresh_token'] = $data_wh['tx_refresh_token'];
  4646. $param_wh['tx_update_time'] = isset($data_wh['tx_update_time'])?$data_wh['tx_update_time']:'';
  4647. }
  4648. Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($param_wh)]);
  4649. $fish = CustomerSource::where(['root_id' => $root_id, 'source' => '飞鱼线索'])->find();
  4650. if (!$fish) CustomerSource::create(['root_id' => $root_id, 'source' => '飞鱼线索']);
  4651. $tx = CustomerSource::where(['root_id' => $root_id, 'source' => '腾讯线索'])->find();
  4652. if (!$tx) CustomerSource::create(['root_id' => $root_id, 'source' => '腾讯线索']);
  4653. $op = CustomerSource::where(['root_id' => $root_id, 'source' => '欧派线索'])->find();
  4654. if (!$op) CustomerSource::create(['root_id' => $root_id, 'source' => '欧派线索']);
  4655. return json(['code' => 0, 'msg' => '设置成功']);
  4656. }
  4657. }