Crm.php 252 KB

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