Talkskill.php 152 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619
  1. <?php
  2. namespace app\sys\controller;
  3. use app\event\Msg;
  4. use app\model\TalkskillChosen;
  5. use app\model\TalkskillContent;
  6. use app\model\TalkskillSuccess;
  7. use think\facade\View;
  8. use think\facade\Request;
  9. use app\model\Employee;
  10. use app\model\TalkskillComment;
  11. use app\model\Talkskill as TalkskillModel;
  12. use app\model\TalkskillLabel;
  13. use app\model\TalkskillHotKeyword;
  14. use app\model\Credits;
  15. use app\model\CreditsSetting;
  16. use PhpOffice\PhpSpreadsheet\IOFactory;
  17. use Exception;
  18. use think\facade\Db;
  19. use app\model\org;
  20. use app\model\Company;
  21. use think\facade\Console;
  22. use app\model\TalkskillUsersCate;
  23. use app\model\TalkskillCates;
  24. use app\model\Customer;
  25. use app\model\CustomerClue;
  26. class Talkskill
  27. {
  28. /*
  29. * 话术列表视图
  30. */
  31. public function talkskilltable()
  32. {
  33. $type = request()->param('type');
  34. $name = request()->param('name');
  35. View::assign('type', $type);
  36. View::assign('name', $name);
  37. $root_id = request()->employee->root_id;
  38. $param['id'] = $type;
  39. //二级分类
  40. $w[] = ['type','=','cate'];
  41. $w[] = ['root_id','=',$root_id];
  42. $w[] = ['pid','=',$param['id']];
  43. $cate = TalkskillCates::where($w)->field('name,id')->order('id asc')->select()->toArray();
  44. view::assign('cate', $cate);
  45. //房屋类型
  46. $w1[] = ['type','=','house_type'];
  47. $w1[] = ['root_id','=',$root_id];
  48. $w1[] = ['pid','=',0];
  49. $house_type = TalkskillCates::where($w1)->field('name,id')->order('id asc')->select()->toArray();
  50. view::assign('house_type', $house_type);
  51. //适用人群
  52. $w3[] = ['root_id','=',$root_id];
  53. $user_cate = TalkskillUsersCate::where($w3)->field('name,id')->order('id asc')->select()->toArray();
  54. view::assign('user_cate', $user_cate);
  55. // 上传人
  56. $empIds = TalkskillModel::where([['root_id', '=', $root_id]])->whereRaw("FIND_IN_SET('" . $name . "' , cate1)")->column('employee_id');
  57. $employee = Employee::where([['id', 'in', $empIds]])->select();
  58. View::assign('employee', $employee);
  59. return View::fetch();
  60. }
  61. /*
  62. * 话术列表数据
  63. */
  64. public function talkskilllist()
  65. {
  66. $param = Request::param();
  67. $where = [
  68. ['root_id', '=', request()->employee->root_id],
  69. ['del', '=', 0],
  70. ['content', '<>', ''],
  71. ['approve', '=', 1]
  72. ];
  73. if (!empty($param['keyword'])) {
  74. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  75. }
  76. $sql = TalkskillModel::with('employee')->where($where);
  77. if (!empty($param['name'])) {
  78. $sql->whereRaw("FIND_IN_SET('" . $param['name'] . "' , cate1)");
  79. }
  80. if (!empty($param['cate'])) {
  81. $sql->whereRaw("FIND_IN_SET('" . $param['cate'] . "' , cate2)");
  82. }
  83. if (!empty($param['house_type'])) {
  84. $sql->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  85. }
  86. if (!empty($param['user_cate'])) {
  87. $sql->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  88. }
  89. if (!empty($param['addtime'])) {
  90. list($start_date, $end_date) = explode(' - ', $param['addtime']);
  91. $sql->whereRaw("addtime >= '".$start_date."'");
  92. $sql->whereRaw("addtime < '". date('Y-m-d H:i:s', strtotime($end_date) + 86400) ."'");
  93. }
  94. if (!empty($param['employee_id'])) {
  95. $sql->whereRaw("employee_id=".$param['employee_id']);
  96. }
  97. // 获取数据
  98. $list = $sql->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  99. $count = $sql->count();
  100. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  101. }
  102. /*
  103. * 话术添加页
  104. */
  105. public function talkskilladd()
  106. {
  107. $root_id = request()->employee->root_id;
  108. $w[] = ['root_id','=',$root_id];
  109. $w[] = ['pid','=',0];
  110. $w[] = ['type','=','cate'];
  111. $list = TalkskillCates::where($w)->field('id,name')->order('id asc')->select();
  112. View::assign('type',$list);
  113. return View::fetch();
  114. }
  115. /*
  116. * 话术分类添加
  117. */
  118. public function add_talkskill_label()
  119. {
  120. $value = Request::param('value');
  121. if (empty($value)) return json(['code' => 1, 'msg' => '请输入场景名称']);
  122. $isAdd = TalkskillLabel::where(['label_name' => $value, 'root_id' => request()->employee->root_id])->count();
  123. if ($isAdd > 0) return json(['code' => 1, 'msg' => '场景已存在']);
  124. $objId = TalkskillLabel::insertGetId(['label_name' => $value, 'root_id' => request()->employee->root_id]);
  125. return json(['code' => 0, 'msg' => '添加成功', 'data' => ['id' => $objId, 'name' => $value]]);
  126. }
  127. /*
  128. * 话术分类列表
  129. */
  130. public function label_list()
  131. {
  132. $id = request()->param('id');
  133. $labelArray = [];
  134. if (!empty($id)) {
  135. $labelArray = TalkskillModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->value('label');
  136. $labelArray = explode(',', $labelArray);
  137. }
  138. $label = TalkskillLabel::where('root_id', request()->employee->root_id)->select();
  139. $data = [];
  140. foreach ($label as $val) {
  141. $data[] = [
  142. 'name' => $val['label_name'],
  143. 'value' => $val['id'],
  144. 'selected' => in_array($val['id'], $labelArray) ? true : false
  145. ];
  146. }
  147. $data = hanziInitsort($data,'name');
  148. return json($data);
  149. }
  150. /*
  151. * 话术添加
  152. */
  153. public function add_talkskill()
  154. {
  155. $param = Request::only(['title', 'user_cate','content', 'label'=>0, 'id', 'type', 'files_data', 'images_data', 'fiels_exist', 'images_exist', 'operationType', 'state','box'=>'','house_type'=>'','cate1'=>'','cate2'=>'']);
  156. $param['user_cate'] = str_replace(' ','',$param['user_cate']);
  157. $data = [
  158. 'title' => $param['title'],
  159. 'content' => $param['content'],
  160. 'label' => $param['label'],
  161. 'root_id' => request()->employee->root_id,
  162. 'type' => 'admin',
  163. 'imgs' => isset($param['images_data']) && $param['images_data'] ? implode(',', $param['images_data']) : null,
  164. 'videos' => isset($param['files_data']) && $param['files_data'] ? implode(',', $param['files_data']) : null,
  165. 'user_cate' => $param['user_cate'],
  166. 'house_type' => $param['house_type'],
  167. 'cate1' => $param['cate1'],
  168. 'cate2' => $param['cate2'],
  169. 'employee_id'=>request()->employee->id
  170. ];
  171. if (isset($param['images_exist']) && $param['images_exist']) {
  172. $data['imgs'] = $data['imgs'] ? $data['imgs'] . ',' . implode(',', $param['images_exist']) : implode(',', $param['images_exist']);
  173. }
  174. if (isset($param['fiels_exist']) && $param['fiels_exist']) {
  175. $data['videos'] = $data['videos'] ? $data['videos'] . ',' . implode(',', $param['fiels_exist']) : implode(',', $param['fiels_exist']);
  176. }
  177. if (empty($param['id'])) {
  178. dataStatistics(request()->employee->root_id,'talkskill_count',1,'inc');//manage应用首页统计数据
  179. TalkskillModel::insert($data);
  180. } else {
  181. $data['id'] = $param['id'];
  182. unset($data['type']);
  183. $talkill = TalkskillModel::where(['id' => $data['id'], 'root_id' => request()->employee->root_id])->find();
  184. $talkill->save($data);
  185. if ($param['operationType'] == 'submit') {
  186. event(new Msg($talkill->employee_id, '您提问的“' . $param['title'] . '”问题已有回复,请点击查看', 'talkskill'));
  187. }
  188. if ($param['operationType'] == 'share') {
  189. $approve = $this->approve($param['id'], $param['state'],$param['box']);
  190. if ($approve == false) return json(['code' => 1, 'msg' => '操作失败']);
  191. }
  192. }
  193. return json(['code' => 0, 'msg' => '操作成功']);
  194. }
  195. /*
  196. * 话术编辑查看
  197. */
  198. public function talkskilldetail()
  199. {
  200. $where = [
  201. 'id' => Request::param('id'),
  202. 'root_id' => request()->employee->root_id,
  203. 'del' => 0
  204. ];
  205. $data = TalkskillModel::where($where)->find();
  206. $data = $data->getData();
  207. $data['imgs'] = empty($data['imgs']) ? '' : explode(',', $data['imgs']);
  208. $data['videos'] = empty($data['videos']) ? '' : explode(',', $data['videos']);
  209. $data['cate1_count'] = $data['cate1'] ? count(array_filter(explode(',',$data['cate1']))) : 0;
  210. $data['cate2_count'] = $data['cate2'] ? count(array_filter(explode(',',$data['cate2']))) : 0;
  211. $data['house_type_count'] = $data['house_type'] ? count(array_filter(explode(',',$data['house_type']))) : 0;
  212. $data['user_cate_count'] = $data['user_cate'] ? count(array_filter(explode(',',$data['user_cate']))) : 0;
  213. View::assign('data', $data);
  214. View::assign('ali_oss_bindurl', config('app.ali_oss_bindurl'));
  215. //操作类型:正常编辑,提问回复,共享修改
  216. $operationType = Request::param('operationType');
  217. View::assign('operationType', isset($operationType) ? $operationType : '');
  218. $list = TalkskillUsersCate::where([['root_id', '=', request()->employee->root_id]])->order('id asc')->select();
  219. View::assign('type',$list);
  220. return View::fetch();
  221. }
  222. /*
  223. * 话术编辑页面下拉
  224. */
  225. public function talkskilldetail_select()
  226. {
  227. $id = Request::param('id');
  228. $root_id = request()->employee->root_id;
  229. $where = [
  230. 'id' => $id,
  231. 'root_id' => $root_id,
  232. 'del' => 0
  233. ];
  234. $data = TalkskillModel::where($where)->find();
  235. $cate2 = explode(',',$data->cate2);
  236. $house_types = explode(',',$data->house_type);
  237. //话术一级
  238. $root_id = request()->employee->root_id;
  239. $wc[] = ['type','=','cate'];
  240. $wc[] = ['root_id','=',$root_id];
  241. $wc[] = ['pid','=',0];
  242. $cate1 = TalkskillCates::where($wc)->field('name,name value,id')->order('id asc')->select()->toArray();
  243. foreach ($cate1 as $k0 => $v0) {
  244. $cate1[$k0]['selected'] = ($data['cate1'] && strpos($data['cate1'],$v0['name'])!==false) ? true : false;
  245. }
  246. $data1['cate'] = $cate1;
  247. //话术二级下拉
  248. $root_id = request()->employee->root_id;
  249. $pid = TalkskillCates::where([['name','=',$data['cate1']],['root_id','=',$root_id],['pid','=',0]])->value('id');
  250. $w[] = ['type','=','cate'];
  251. $w[] = ['root_id','=',$root_id];
  252. $w[] = ['pid','=',$pid];
  253. $cate = TalkskillCates::where($w)->field('name,name value,id')->order('id asc')->select()->toArray();
  254. foreach ($cate as $k => $v) {
  255. $cate[$k]['selected'] = ($cate2 && in_array($v['name'],$cate2)) ? true : false;
  256. }
  257. $data1['cate2'] = $cate;
  258. //房屋类型
  259. $w1[] = ['type','=','house_type'];
  260. $w1[] = ['root_id','=',$root_id];
  261. $w1[] = ['pid','=',0];
  262. $house_type = TalkskillCates::where($w1)->field('name,name value,id')->order('id asc')->select()->toArray();
  263. foreach ($house_type as $k2 => $v2) {
  264. $house_type[$k2]['selected'] = ($house_type && in_array($v2['name'],$house_types)) ? true : false;
  265. }
  266. $data1['house_type'] = $house_type;
  267. //适用人群
  268. $w3[] = ['root_id','=',$root_id];
  269. $user_cate = TalkskillUsersCate::where($w3)->field('name,name value,id')->order('id asc')->select()->toArray();
  270. foreach ($user_cate as $k3 => $v3) {
  271. $user_cate[$k3]['selected'] = ($data['user_cate'] && strpos($data['user_cate'],$v3['name'])!==false) ? true : false;
  272. }
  273. $data1['user_date'] = $user_cate;
  274. return json(['code' => 0, 'data' => $data1]);
  275. }
  276. /**
  277. * 话术详情
  278. */
  279. public function talkskillview(){
  280. $id = Request::param('id');
  281. View::assign('id', $id);
  282. $root_id = !empty(Request::param('root_id')) ? Request::param('root_id') : request()->employee->root_id;
  283. View::assign('root_id', $root_id);
  284. $where = [
  285. 'id' => $id,
  286. 'root_id' => $root_id,
  287. 'del' => 0
  288. ];
  289. $data = TalkskillModel::where($where)->find()->toArray();
  290. switch ($data['type']){
  291. case 'admin':
  292. $data['type_name'] = '官方创建';
  293. break;
  294. case 'submit':
  295. $employee = Employee::find($data['employee_id']);
  296. $data['type_name'] = $employee['name'] . '提问';
  297. break;
  298. case 'share':
  299. $data['type_name'] = '共享话术';
  300. break;
  301. default:
  302. $data['type_name'] = '官方创建';
  303. break;
  304. }
  305. View::assign('data', $data);
  306. return View::fetch();
  307. }
  308. /*
  309. * 话术删除
  310. */
  311. public function talkskilldel()
  312. {
  313. $where = [
  314. 'id' => Request::param('id'),
  315. 'root_id' => request()->employee->root_id
  316. ];
  317. TalkskillModel::where($where)->update(['del' => 1]);
  318. dataStatistics(request()->employee->root_id,'talkskill_count',1,'dec');//manage应用首页统计数据
  319. return json(['code' => 0, 'msg' => '操作成功']);
  320. }
  321. /*
  322. * 话术评论列表
  323. */
  324. public function talkskillcomment($id)
  325. {
  326. if (!Request()->isAjax()) {
  327. View::assign('id', $id);
  328. return View::fetch();
  329. }
  330. $param = Request()->param();
  331. $root_id = !empty(Request::param('root_id')) ? Request::param('root_id') : request()->employee->root_id;
  332. // 请求条件初始化
  333. $where = [
  334. 'root_id' => $root_id,
  335. 'taid' => $param['id'],
  336. 'approve' => 1
  337. ];
  338. $data = TalkskillComment::with(['User'])->where($where)->page($param['page'], $param['limit'])->order(['approve' => 'asc', 'use' => 'desc', 'praise' => 'desc'])->select()->toArray();
  339. $count = TalkskillComment::where($where)->count();
  340. return json(['code' => 0, 'data' => $data, 'count' => $count, 'msg' => '获取成功']);
  341. }
  342. /**
  343. * 评论删除
  344. */
  345. public function talkskillCommentDel($id)
  346. {
  347. $rs = TalkskillComment::where(['id' => $id, 'root_id' => request()->employee->root_id])->find();
  348. if ($rs) {
  349. TalkskillModel::where(['id' => $rs['taid'], 'root_id' => request()->employee->root_id])->dec('commentCount')->update();
  350. $rs->delete();
  351. }
  352. }
  353. /*
  354. * 业务员提交的问题
  355. */
  356. public function feedbacklist()
  357. {
  358. if (!request()->isAjax()) return View::fetch();
  359. $param = Request::param();
  360. $where = [
  361. ['root_id', '=', request()->employee->root_id],
  362. ['del', '=', 0],
  363. ['type', '=', 'submit'],
  364. ['content', '=', '']
  365. ];
  366. if (!empty($param['keyword'])) $where[] = ['title|content', 'like', '%' . $param['keyword'] . '%'];
  367. // 获取数据
  368. $list = TalkskillModel::with(['employee'])->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select();
  369. $count = TalkskillModel::where($where)->count();
  370. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  371. }
  372. /*
  373. * 业务员上传的话术审核列表
  374. */
  375. public function approve_talkskill()
  376. {
  377. if (!request()->isAjax()) return View::fetch();
  378. $param = Request::param();
  379. $where = [
  380. ['root_id', '=', request()->employee->root_id],
  381. ['del', '=', 0],
  382. ['approve', '=', 0],
  383. ['type', '=', 'share']
  384. ];
  385. if (!empty($param['keyword'])) {
  386. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  387. }
  388. // 获取数据
  389. $list = TalkskillModel::with(['employee'])->where($where)->page($param['page'], $param['limit'])->order(['addtime' => 'desc'])->select()->toArray();
  390. $count = TalkskillModel::where($where)->count();
  391. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  392. }
  393. /*
  394. * 话术提问列表
  395. */
  396. public function talkskill_questions()
  397. {
  398. if (!request()->isAjax()) return View::fetch();
  399. $param = Request::param();
  400. $where = [
  401. ['root_id', '=', request()->employee->root_id],
  402. ['del', '=', 0],
  403. ['approve', '=', 0],
  404. ['type', '=', 'share']
  405. ];
  406. if (!empty($param['keyword'])) {
  407. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  408. }
  409. // 获取数据
  410. $list = TalkskillModel::with(['employee'])->where($where)->page($param['page'], $param['limit'])->order(['addtime' => 'desc'])->select()->toArray();
  411. $count = TalkskillModel::where($where)->count();
  412. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  413. }
  414. /*
  415. * 批量审核
  416. */
  417. public function talkskill_approve()
  418. {
  419. $root_id = request()->employee->root_id;
  420. $ids = input('ids');
  421. $approve = input('status', 0);
  422. $box = input('box','');//驳回原因
  423. Db::startTrans();
  424. try {
  425. $where[] = ['root_id', '=', $root_id];
  426. $where[] = ['id', 'in', $ids];
  427. $u['approve'] = $approve == 1 ? 1 : 2; //审批,0待审核,1审核通过,2不通过
  428. $u['aprove_employee_id'] = request()->employee->id;
  429. $u['aprove_time'] = date('Y-m-d H:i:s');
  430. $u['approve_from'] = 0;
  431. TalkskillModel::where($where)->update($u);
  432. //查询增加的贡献值
  433. $contribution = CreditsSetting::where(['code' => 'talkskill_credits', 'root_id' => request()->employee->root_id])->value('value');
  434. if (empty($contribution)) $contribution = 1;
  435. //贡献值
  436. $list = TalkskillModel::where($where)->select();
  437. foreach ($list as $k => $v) {
  438. if ($approve == 1) {
  439. //贡献值入库
  440. $sum = Credits::where(['root_id' => $root_id, 'employee_id' => $v['employee_id'], 'type' => 1])->sum('credits');
  441. $array = [
  442. 'employee_id' => $v['employee_id'],
  443. 'root_id' => $root_id,
  444. 'credits' => $contribution,
  445. 'sum' => $sum + $contribution,
  446. 'type' => 1,
  447. 'json' => $v['id'],
  448. 'remark' => '共享话术审核通过'
  449. ];
  450. Credits::create($array);
  451. }
  452. $s = $approve == 1 ? '已' : '未';
  453. if ($s=='已') {
  454. //累计勋章
  455. Console::call('medal',['talkskill',(string)$v['employee_id'],(string)$root_id]);
  456. event(new Msg($v['employee_id'], '您共享的“'.$v['title'].'”' . $s . '审核通过', 'talkskillApprove'));
  457. }else{
  458. event(new Msg($v['employee_id'], '您共享的“'.$v['title'].'”' . '被驳回,原因是:'.$box, 'talkskillApprove'));
  459. }
  460. }
  461. Db::commit();
  462. } catch (Exception $e) {
  463. // 回滚事务
  464. Db::rollback();
  465. return json(["code" => 1, 'msg' => '操作失败']);
  466. }
  467. return json(["code" => 0, 'msg' => '操作成功']);
  468. }
  469. /*
  470. * 审核业务员上传的话术
  471. */
  472. private function approve($id, $state,$box='')
  473. {
  474. $data = TalkskillModel::where(['id' => $id, 'del' => 0, 'type' => 'share', 'approve' => 0])->find();
  475. if (empty($data)) return false;
  476. $data->approve = $state == 1 ? 1 : 2;
  477. $data->aprove_employee_id = request()->employee->id;
  478. $data->aprove_time = date('Y-m-d H:i:s');
  479. $data->box = $box;
  480. $data->approve_from = 0;
  481. $data->save();
  482. if ($data->employee_id != 0) {
  483. $s = $data->approve == 1 ? '已' : '未';
  484. if ($s=='已') {
  485. //累计勋章
  486. Console::call('medal',['talkskill',(string)$data->employee_id,(string)$data->root_id]);
  487. event(new Msg($data->employee_id, '您共享的“'.$data['title'].'”' . $s . '审核通过', 'talkskillApprove'));
  488. dataStatistics(request()->employee->root_id,'talkskill_count',1,'inc');//manage应用首页统计数据
  489. }else{
  490. event(new Msg($data->employee_id, '您共享的“'.$data['title'].'”' . '被驳回,原因是:'.$box, 'talkskillApprove'));
  491. }
  492. }
  493. if ($state != 1) return true;
  494. //查询增加的贡献值
  495. $contribution = CreditsSetting::where(['code' => 'talkskill_credits', 'root_id' => request()->employee->root_id])->value('value');
  496. if (empty($contribution)) $contribution = 1;
  497. //积分入库
  498. $sum = Credits::where(['root_id' => request()->employee->root_id, 'employee_id' => $data['employee_id'], 'type' => 1])->sum('credits');
  499. $array = [
  500. 'employee_id' => $data['employee_id'],
  501. 'root_id' => request()->employee->root_id,
  502. 'credits' => $contribution,
  503. 'sum' => $sum + $contribution,
  504. 'type' => 1,
  505. 'remark' => '共享话术'
  506. ];
  507. Credits::create($array);
  508. return true;
  509. }
  510. /*
  511. * 业务员评论的话术审核列表
  512. */
  513. public function approve_comment()
  514. {
  515. if (!request()->isAjax()) return View::fetch();
  516. $param = Request::param();
  517. $where = [
  518. ['root_id', '=', request()->employee->root_id],
  519. ['approve', '=', 0],
  520. ];
  521. if (!empty($param['keyword'])) {
  522. $where[] = ['comments', 'like', '%' . $param['keyword'] . '%'];
  523. }
  524. // 获取数据
  525. $list = TalkskillComment::with(['talkskill'])->where($where)->page($param['page'], $param['limit'])->order(['addtime' => 'desc'])->select()->toArray();
  526. $uid = array_column($list, 'uid');
  527. $name = Employee::where([['root_id', '=', request()->employee->root_id], ['uid', 'in', $uid]])->column('name', 'uid');
  528. foreach ($list as &$item) {
  529. $item['name'] = isset($name[$item['uid']]) ? $name[$item['uid']] : '';
  530. }
  531. $count = TalkskillComment::where($where)->count();
  532. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  533. }
  534. /*
  535. * 业务员评论的话术展示
  536. */
  537. public function comment_detail()
  538. {
  539. $id = request()->param('id');
  540. $comment = TalkskillComment::with(['talkskill'])->where(['root_id' => request()->employee->root_id, 'id' => $id])->find();
  541. View::assign('data', $comment);
  542. return View::fetch();
  543. }
  544. //批量审核回答
  545. public function all_approve_comment()
  546. {
  547. $root_id = request()->employee->root_id;
  548. $ids = input('ids');
  549. $approve = input('status', 0);
  550. Db::startTrans();
  551. try {
  552. $where[] = ['root_id', '=', $root_id];
  553. $where[] = ['id', 'in', $ids];
  554. $u['approve'] = $approve == 1 ? 1 : 2; //审批,0待审核,1审核通过,2不通过
  555. $u['aprove_employee_id'] = request()->employee->id;
  556. $u['aprove_time'] = date('Y-m-d H:i:s');
  557. $u['approve_from'] = 0;
  558. TalkskillComment::where($where)->update($u);
  559. //查询增加的贡献值
  560. $contribution = CreditsSetting::where(['code' => 'talkskill_contribution_value', 'root_id' => request()->employee->root_id])->value('value');
  561. if (empty($contribution)) $contribution = 1;
  562. //贡献值
  563. $list = TalkskillComment::where($where)->select();
  564. foreach ($list as $k => $v) {
  565. //业务员
  566. $employee_id = Employee::where(['root_id' => request()->employee->root_id, 'uid' => $v['uid'], 'state' => '在职'])->value('id');
  567. if (empty($employee_id)) continue;
  568. if ($approve == 1) {
  569. //审核通过后话术表增加回答数量
  570. TalkskillModel::where(['id' => $v['taid']])->inc('commentCount')->update();
  571. //更新最新回答时间
  572. $this->updateOrderDate($v['taid']);
  573. //贡献值入库
  574. $sum = Credits::where(['root_id' => $root_id, 'employee_id' => $employee_id, 'type' => 1])->sum('credits');
  575. $array = [
  576. 'employee_id' => $employee_id,
  577. 'root_id' => $root_id,
  578. 'credits' => $contribution,
  579. 'sum' => $sum + $contribution,
  580. 'type' => 1,
  581. 'json' => $v['id'],
  582. 'remark' => '话术评论审核通过增加贡献值'
  583. ];
  584. Credits::create($array);
  585. }
  586. $s = $approve == 1 ? '已' : '未';
  587. if ($s=='已') {
  588. //累计勋章
  589. Console::call('medal',['comment',(string)$employee_id,(string)request()->employee->root_id]);
  590. }
  591. event(new Msg($employee_id, '您提交的话术回答“'.$v['comments'].'”' . $s . '审核通过', 'talkskillApprove'));
  592. }
  593. Db::commit();
  594. } catch (Exception $e) {
  595. // 回滚事务
  596. Db::rollback();
  597. return json(["code" => 1, 'msg' => '操作失败']);
  598. }
  599. return json(["code" => 0, 'msg' => '操作成功']);
  600. }
  601. /*
  602. * 审核业务员评论的内容
  603. */
  604. public function comment()
  605. {
  606. $param = Request::param(['id', 'state', 'comments','box'=>'']);
  607. $data = TalkskillComment::where(['id' => $param['id'], 'root_id' => request()->employee->root_id, 'approve' => 0])->find();
  608. if (empty($data)) return json(['code' => 1, 'msg' => '审核失败']);
  609. $data->approve = $param['state'] == 1 ? 1 : 2;
  610. $data->comments = $param['comments'];
  611. $data->aprove_employee_id = request()->employee->id;
  612. $data->aprove_time = date('Y-m-d H:i:s');
  613. $data->box = $param['box'];
  614. $data->approve_from = 0;
  615. $data->save();
  616. $employee_id = Employee::where(['root_id' => request()->employee->root_id, 'uid' => $data['uid'], 'state' => '在职'])->value('id');
  617. if (empty($employee_id)) return json(['code' => 1, 'msg' => '员工不存在']);
  618. // 添加消息
  619. $s = $data->approve == 1 ? '已' : '未';
  620. if ($s=='已') {
  621. $this->updateOrderDate($data->taid);
  622. //累计勋章
  623. Console::call('medal',['comment',(string)$employee_id,(string)request()->employee->root_id]);
  624. event(new Msg($employee_id, '您提交的话术回答“'.$data->comments.'”' . $s . '审核通过', 'talkskillApprove'));
  625. }else{
  626. event(new Msg($employee_id, '您提交的话术回答“'.$data->comments.'”' . '被驳回,原因是:'.$param['box'], 'talkskillApprove'));
  627. }
  628. if ($param['state'] != 1) return json(['code' => 0, 'msg' => '操作成功']);
  629. //审核通过后话术表增加回答数量
  630. TalkskillModel::where(['id' => $data['taid']])->inc('commentCount')->update();
  631. //查询增加的贡献值
  632. $contribution = CreditsSetting::where(['code' => 'talkskill_contribution_value', 'root_id' => request()->employee->root_id])->value('value');
  633. if (empty($contribution)) $contribution = 1;
  634. //积分入库
  635. $sum = Credits::where(['root_id' => request()->employee->root_id, 'employee_id' => $employee_id, 'type' => 1])->sum('credits');
  636. $array = [
  637. 'employee_id' => $employee_id,
  638. 'root_id' => request()->employee->root_id,
  639. 'credits' => $contribution,
  640. 'sum' => $sum + $contribution,
  641. 'type' => 1,
  642. 'remark' => '话术评论审核通过增加贡献值'
  643. ];
  644. Credits::create($array);
  645. return json(['code' => 0, 'msg' => '操作成功']);
  646. }
  647. /*
  648. * 更新最新回答时间
  649. * $id 话术id
  650. */
  651. public function updateOrderDate($id)
  652. {
  653. $data = TalkskillComment::where(['taid' => $id, 'root_id' => request()->employee->root_id, 'approve' => 1])->order('id desc')->findOrEmpty();
  654. if (!$data->isEmpty()) {
  655. TalkskillModel::where('id',$id)->update(['order_date'=>date('Y-m-d H:i:s')]);
  656. }
  657. return true;
  658. }
  659. /*
  660. * 分类管理
  661. */
  662. public function cate_talkskill()
  663. {
  664. $w[] = ['root_id','=',request()->employee->root_id];
  665. $w[] = ['pid','=',0];
  666. $w[] = ['type','=','cate'];
  667. $type = TalkskillCates::where($w)->field('id,name label_name')->order('id asc')->select();
  668. View::assign('type', $type);
  669. $root_id = request()->employee->root_id;
  670. $employee_id = request()->employee->id;
  671. // echo $root_id;die;
  672. //话术总数
  673. $where = [
  674. ['del','=',0],
  675. ['approve','=',1],
  676. ['root_id','=',$root_id]
  677. ];
  678. $c = TalkskillModel::where($where)->field('id,root_id,type,employee_id')->select()->toArray();
  679. //count话术总数,gf_upload官方上传,yewu_upload业务员上传,yewu_tiwen业务员提问
  680. $data['count'] = $data['gf_upload'] = $data['yewu_upload'] = $data['yewu_tiwen'] = 0;
  681. $data['count'] = count($c);
  682. foreach ($c as $k => $v) {
  683. if ($v['employee_id']==0 && $v['type']=='admin') {
  684. $data['gf_upload']+=1;
  685. }elseif ($v['type']=='share') {
  686. $data['yewu_upload']+=1;
  687. }elseif ($v['type']=='submit') {
  688. $data['yewu_tiwen']+=1;
  689. }
  690. }
  691. //label_count应用场景数量,gf_huida官方回答,gf_huashu_grwth官方话术占比, yewu_huida业务员回答, yewu_huashu_grawth业务话术占比
  692. $data['label_count'] = count($type);
  693. $w1[] = ['type','=','submit'];
  694. $w1[] = ['content', 'not null', ''];
  695. $w1[] = ['content','<>',''];
  696. $w1 = array_merge($where,$w1);
  697. $data['gf_huida'] = TalkskillModel::where($w1)->count();
  698. $data['gf_huashu_grwth'] = $data['count']==0 ? '0%' : round($data['gf_upload']/$data['count']*100,2).'%';
  699. $w2[] = ['root_id','=',$root_id];
  700. $w2[] = ['approve','=',1];
  701. $data['yewu_huida'] = TalkskillComment::where($w2)->count();
  702. $data['count'] = $data['count']+$data['yewu_huida'];
  703. $data['yewu_huashu_grawth'] = $data['count']==0 ? '0%' : round($data['yewu_upload']/$data['count']*100,2).'%';
  704. $data['yewu_tiwen_grawth'] = $data['count']==0 ? '0%' : round($data['yewu_tiwen']/$data['count']*100,2).'%';
  705. // echo $data['yewu_huida'];die;
  706. View::assign('data',$data);
  707. return View::fetch();
  708. }
  709. /*
  710. * 分类管理列表
  711. */
  712. public function cate_talkskill_list()
  713. {
  714. $param = Request::only(['page' => 1, 'limit' => 10, 'keyword','type'=>'']);
  715. $root_id = request()->employee->root_id;
  716. $count = 0;
  717. //一级场景
  718. $cw[] = ['root_id','=',$root_id];
  719. $cw[] = ['pid','=',0];
  720. $cw[] = ['type','=','cate'];
  721. if (!empty($param['type'])) {
  722. $cw[] = ['name','=',$param['type']];
  723. }
  724. $data = TalkskillCates::where($cw)->field('*,name label_name')->page($param['page'] , $param['limit'])->order('id asc')->select()->toArray();
  725. $count = TalkskillCates::where($cw)->count();
  726. if ($data) {
  727. $where2[] = ['cate1','in',array_column($data, 'name')];
  728. $where2[] = ['root_id', '=', $root_id];
  729. $where2[] = ['del', '=', 0];
  730. $where2[] = ['approve', '=', 1];
  731. if (isset($param['keyword']) && $param['keyword']) {
  732. $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  733. }
  734. // if (!empty($param['type'])) {
  735. // $where2[] = ['cate1','=',$param['type']];
  736. // }
  737. $label_count = TalkskillModel::where($where2)->field('addtime,label label_id,id,use_count,cate1,view_num')->order('id asc')->select()->toArray();
  738. $label_counts = $dates = $person_count = $viewCount = [];
  739. foreach ($label_count as $v) {
  740. //话术数量
  741. $label_counts[$v['cate1']] = isset($label_counts[$v['cate1']]) ? $label_counts[$v['cate1']] += 1 : 1;
  742. //最近话术上传时间
  743. $dates[$v['cate1']] = $v['addtime'];
  744. //使用人次
  745. $person_count[$v['cate1']] = isset($person_count[$v['cate1']]) ? $person_count[$v['cate1']] + $v['use_count'] : $v['use_count'];
  746. //浏览人次
  747. $viewCount[$v['cate1']] = isset($viewCount[$v['cate1']]) ? $viewCount[$v['cate1']] + $v['view_num'] : $v['view_num'];
  748. }
  749. foreach ($data as $k => $v) {
  750. $data[$k]['talkill_count'] = isset($label_counts[$v['name']]) ? $label_counts[$v['name']] : 0;
  751. $data[$k]['update_time'] = isset($dates[$v['name']]) ? $dates[$v['name']] : '';
  752. $data[$k]['use_count'] = isset($person_count[$v['name']]) ? $person_count[$v['name']] : 0;
  753. $data[$k]['view_num'] = isset($viewCount[$v['name']]) ? $viewCount[$v['name']] : 0;
  754. //来源
  755. if($v['from_type'] == 0)
  756. {
  757. $data[$k]['from'] = '店面发布';
  758. }elseif($v['from_type'] == 1){
  759. $data[$k]['from'] = '集团指派';
  760. }else{
  761. $company = Company::where('root_id',$v['from_root_id'])->value('company_name');
  762. $data[$k]['from'] = $company.'共享';
  763. }
  764. }
  765. }
  766. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  767. //旧版
  768. // $param = Request::param();
  769. // $condition = [
  770. // ['root_id', '=', request()->employee->root_id],
  771. // ['from_type', 'in', [0,2]]
  772. // ];
  773. // if (!empty($param['type'])) {
  774. // $condition[] = ['id', '=', $param['type']];
  775. // }
  776. // $data = TalkskillLabel::where($condition)->page($param['page'] , $param['limit'])->order('id desc')->select()->toArray();
  777. // $count = TalkskillLabel::where($condition)->count();
  778. // $ids = array_column($data, 'id');
  779. // $where1 = [];
  780. // foreach ($ids as $v) {
  781. // $where1[] = "FIND_IN_SET(" . $v . " , label)";
  782. // }
  783. // $where2 = [
  784. // ['root_id', '=', request()->employee->root_id],
  785. // ['del', '=', 0],
  786. // ['approve', '=', 1],
  787. // ['content', '<>', ''],
  788. // ];
  789. // if (isset($param['keyword']) && $param['keyword']) {
  790. // $where2[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  791. // }
  792. // $label_count = TalkskillModel::where($where2)->whereRaw(implode(' or ', $where1))->field('addtime,label label_id,id,use_count')->select()->toArray();
  793. // $talkill_count = $update_time = $use_count = [];
  794. // foreach ($label_count as $v) {
  795. // $label_ids = explode(',', $v['label_id']);
  796. // foreach ($label_ids as $v2) {
  797. // //话术数量
  798. // $talkill_count[$v2] = isset($talkill_count[$v2]) ? $talkill_count[$v2] += 1 : 1;
  799. // //最近话术上传时间
  800. // $update_time[$v2] = isset($update_time[$v2]) ? ($update_time[$v2] > $v['addtime'] ? $update_time[$v2] : $v['addtime']) : $v['addtime'];
  801. // //使用人次
  802. // $use_count[$v2] = isset($use_count[$v2]) ? $use_count[$v2] + $v['use_count'] : $v['use_count'];
  803. // }
  804. // }
  805. // foreach ($data as $k => $v) {
  806. // $data[$k]['talkill_count'] = isset($talkill_count[$v['id']]) ? $talkill_count[$v['id']] : 0;
  807. // $data[$k]['update_time'] = isset($update_time[$v['id']]) ? $update_time[$v['id']] : '';
  808. // $data[$k]['use_count'] = isset($use_count[$v['id']]) ? $use_count[$v['id']] : 0;
  809. // }
  810. // return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  811. }
  812. /*
  813. * 分类管理添加
  814. */
  815. public function cate_talkskill_add()
  816. {
  817. $param = Request::param();
  818. $condition[] = ['label_name', '=', $param['name']];
  819. $condition[] = ['root_id', '=', request()->employee->root_id];
  820. $model = TalkskillLabel::where($condition);
  821. $models = new TalkskillLabel;
  822. $info = $model->find();
  823. if ($info) {
  824. return json(['code' => 1, 'msg' => '名称已存在']);
  825. }
  826. $models->label_name = $param['name'];
  827. $models->root_id = request()->employee->root_id;
  828. $models->save();
  829. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  830. }
  831. /*
  832. * 分类管理编辑
  833. */
  834. public function cate_talkskill_edit()
  835. {
  836. $param = Request::param();
  837. $conditions = [['id', '=', $param['id']]];
  838. $condition[] = ['label_name', '=', $param['name']];
  839. $condition[] = ['root_id', '=', request()->employee->root_id];
  840. $data['label_name'] = $param['name'];
  841. $model = TalkskillLabel::where($condition);
  842. $models = TalkskillLabel::where($conditions);
  843. $info = $model->find();
  844. if ($info) {
  845. return json(['code' => 1, 'msg' => '名称已存在']);
  846. }
  847. $models->update($data);
  848. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  849. }
  850. /*
  851. * 员工热词搜索列表
  852. */
  853. public function hot_word()
  854. {
  855. if (!request()->isAjax()) return View::fetch();
  856. $param = Request::param();
  857. $condition = [['root_id', '=', request()->employee->root_id]];
  858. if (!empty($param['keyword'])) $condition[] = ['keyword', 'like', '%' . $param['keyword'] . '%'];
  859. $list = TalkskillHotKeyword::where($condition)->page($param['page'], $param['limit'])->order('use_count desc')->select()->toArray();
  860. $count = TalkskillHotKeyword::where($condition)->count();
  861. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  862. }
  863. /*
  864. * 员工热词搜索删除
  865. */
  866. public function how_keyword_del()
  867. {
  868. $id = Request::param('id');
  869. TalkskillHotKeyword::where(['id' => $id, 'root_id' => request()->employee->root_id])->delete();
  870. return json(['code' => 0, 'msg' => '删除成功']);
  871. }
  872. private function isEmptyRow($row)
  873. {
  874. foreach ($row as $cell) {
  875. if (null !== $cell) return false;
  876. }
  877. return true;
  878. }
  879. /*
  880. * 导入话术
  881. */
  882. public function importing()
  883. {
  884. $request = request();
  885. if (!$request->isAjax()) {
  886. return View::fetch();
  887. }
  888. // var_dump(request()->employee->root_id);die;
  889. $fileExtendName = substr(strrchr($_FILES['file']["name"], '.'), 1);
  890. $filename = $_FILES['file']['tmp_name'];
  891. if ($fileExtendName == 'xlsx') {
  892. $objReader = IOFactory::createReader('Xlsx');
  893. } else {
  894. $objReader = IOFactory::createReader('Xls');
  895. }
  896. $objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
  897. $sheet = $objPHPExcel->getSheet(0); //excel中的第一张sheet
  898. $highestRow = $sheet->getHighestDataRow();
  899. $highestColumn = $sheet->getHighestDataColumn();
  900. //// to make crm import log record
  901. $fileoriname = $_FILES['file']['name'];
  902. $a = 0;
  903. $realnum = 0;
  904. $badphonenum = 0;
  905. $saves = $cates = [];
  906. for ($i = 2; $i <= $highestRow; $i++) {
  907. $rowData = $sheet->rangeToArray('A' . $i . ':' . $highestColumn . $i, NULL, TRUE, FALSE);
  908. if ($this->isEmptyRow(reset($rowData))) {
  909. continue;
  910. } else {
  911. $realnum++;
  912. } // skip empty row
  913. $save = [];
  914. $save['title'] = trim((string) $objPHPExcel->getActiveSheet()->getCell('A' . $i)->getValue());
  915. $save['content'] = trim((string) $objPHPExcel->getActiveSheet()->getCell('B' . $i)->getValue());
  916. $save['label'] = trim((string) $objPHPExcel->getActiveSheet()->getCell('C' . $i)->getValue());
  917. $save['user_cate'] = trim((string) $objPHPExcel->getActiveSheet()->getCell('D' . $i)->getValue());
  918. $save['root_id'] = request()->employee->root_id;
  919. $saves[] = $save;
  920. }
  921. //处理分类
  922. $cates = array_unique(array_filter(array_column($saves, 'label')));
  923. $cates = $cates ? explode(',', implode(',', $cates)) : [];
  924. $f_cates = TalkskillLabel::where([['root_id', '=', request()->employee->root_id]])->column('label_name');
  925. $cates = $cates ? array_diff($cates, $f_cates) : [];
  926. $cate_s = [];
  927. foreach ($cates as $k => $v) {
  928. $c = [];
  929. $c['label_name'] = $v;
  930. $c['root_id'] = request()->employee->root_id;
  931. $cate_s[] = $c;
  932. }
  933. //适用人群
  934. $user_cates = array_unique(array_filter(array_column($saves,'user_cate')));
  935. $f_user_cate = TalkskillUsersCate::where([['root_id', '=', request()->employee->root_id]])->column('name');
  936. $user_cates = $user_cates ? array_diff($user_cates, $f_user_cate) : [];
  937. $user_cates_s = [];
  938. foreach ($user_cates as $k5 => $v5) {
  939. $user_cates_s[] = [
  940. 'name' => $v5,
  941. 'root_id' => request()->employee->root_id
  942. ];
  943. }
  944. TalkskillUsersCate::insertAll($user_cates_s);
  945. $label = new TalkskillLabel;
  946. $label = $label->saveAll($cate_s)->toArray();
  947. $f_cates = TalkskillLabel::where([['root_id', '=', request()->employee->root_id]])->column('id', 'label_name');
  948. foreach ($saves as $k => $v) {
  949. $label = explode(',', $v['label']);
  950. $l_label = [];
  951. foreach ($label as $k2 => $v2) {
  952. if (isset($f_cates[trim($v2)])) $l_label[] = $f_cates[trim($v2)];
  953. }
  954. $saves[$k]['label'] = $l_label ? implode(',', $l_label) : '';
  955. }
  956. $taskill = new TalkskillModel;
  957. $taskill->saveAll($saves);
  958. return json(["code" => 0, 'msg' => '导入成功']);
  959. }
  960. public function talkskill_delete()
  961. {
  962. $root_id = request()->employee->root_id;
  963. $ids = input('ids');
  964. $where[] = ['root_id', '=', $root_id];
  965. $where[] = ['id', 'in', $ids];
  966. TalkskillModel::where($where)->update(['del' => 1]);
  967. dataStatistics(request()->employee->root_id,'talkskill_count',count(array_filter($ids)),'dec');//manage应用首页统计数据
  968. return json(["code" => 0, 'msg' => '删除成功']);
  969. }
  970. public function get_org_data(){
  971. $root_id = request()->employee->root_id;
  972. $org_id = input('org_id',0);
  973. $org_id = $org_id ?: request()->employee->root_id;
  974. $type = input('type','talkskill');
  975. //查询直系子部门
  976. $org_info = Org::where('id',$org_id)->value('path');
  977. $orgs = $allnodes = org::where([['path','like',$org_info.'%']])->field('id,name,path')->select()->toArray();
  978. $where[] = ['pid','=',$org_id];
  979. $orgids = org::where($where)->column('id');
  980. //
  981. $employee_ids = Employee::where([['org_id','in',array_column($orgs,'id')],['root_id','=',request()->employee->root_id],['uid','>',0]])->column('id');
  982. $w2[] = ['employee_id','in',$employee_ids];
  983. $w2[] = ['root_id','=',request()->employee->root_id];
  984. $w2[] = ['del','=',0];
  985. $w2[] = ['approve','=',1];
  986. if ($type=='talkskill') {
  987. $w2[] = ['type','=','share'];
  988. } elseif ($type=='submit') {
  989. $w2[] = ['type','=','submit'];
  990. }
  991. $employee_talkskill = TalkskillModel::with(['employees'=>function($query){
  992. $query->visible(['id','name','org_id'])->bind(['eid'=>'id','ename'=>'name','uid','org_id']);
  993. }])->where($w2)
  994. ->visible([])
  995. ->select();
  996. $w3[] = ['approve','=',1];
  997. $w3[] = ['root_id','=',request()->employee->root_id];
  998. $comment = TalkskillComment::where($w3)->group('taid')->column('count(*)','taid');
  999. $all_comment = array_sum(array_values($comment));
  1000. $org = $count = $t_comment = $employee = [];
  1001. foreach ($employee_talkskill as $k => $v) {
  1002. //话术评论数量
  1003. $employee_talkskill['comment_count'] = $v['comment_count'] = isset($comment[$v['id']]) ? $comment[$v['id']] : 0;
  1004. //部门话术数组
  1005. $org[$v['org_id']][] = $v;
  1006. //部门话术数量
  1007. $count[$v['org_id']] = isset($count[$v['org_id']]) ? $count[$v['org_id']]+=1 : 1;//部门话术数量
  1008. //部门评论数量
  1009. $t_cmment[$v['org_id']] = isset($t_cmment[$v['org_id']]) ? $t_cmment[$v['org_id']]+$v['comment_count'] : $v['comment_count'];
  1010. }
  1011. // var_dump(array_sum(array_values($t_cmment)));die;
  1012. // var_dump(array_column($employee_talkskill,'commentCount'));die;
  1013. foreach ($allnodes as $k => $v) {
  1014. $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0;//本部门话术数量
  1015. $allnodes[$k]['comment_count'] = isset($t_cmment[$v['id']]) ? $t_cmment[$v['id']] : 0;//本部门评论数量
  1016. $allnodes[$k]['all_count'] = 0;//包含子部门话术总数
  1017. $allnodes[$k]['all_comment_count'] = 0;//包含子部门评论总数
  1018. foreach ($allnodes as $k2 => $v2) {
  1019. if (strpos($v2['path'],$v['path']) !== false) {
  1020. $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']]+$allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
  1021. $allnodes[$k]['all_comment_count'] = isset($t_cmment[$v2['id']]) ? $t_cmment[$v2['id']]+$allnodes[$k]['all_comment_count'] : $allnodes[$k]['all_comment_count'];
  1022. }
  1023. }
  1024. }
  1025. $orgs = $allnodes;
  1026. $res = [];
  1027. $res['person'] = $res['data'] = [];
  1028. foreach ($orgs as $k => $v) {
  1029. $orgs[$k]['value'] = $v['value'] = $type=='talkskill'||$type=='submit' ? $v['all_count'] : $v['all_comment_count'];
  1030. if ($v['id']==$org_id) {
  1031. $res['person'] = $v;
  1032. }
  1033. if(in_array($v['id'],$orgids)){
  1034. $res['data'][] = $v;
  1035. }
  1036. }
  1037. $sum = count($employee_talkskill);//全部话术总数
  1038. $html = '<ul class="woverflow layui-data-ul-b">';
  1039. $orgs = $res['data'];
  1040. if ($orgs) {
  1041. foreach ($orgs as $k => $v) {
  1042. $orgs[$k]['grawth'] = '';
  1043. $orgs[$k]['name_grawth'] = $v['name'];
  1044. $orgs[$k]['name'] = $v['name'];
  1045. $index = (int)(mt_rand(100000,9999999).mt_rand(100000,9999999));
  1046. if(in_array($v['id'],$orgids)){
  1047. if ($v['value']) {
  1048. $html .= '<li id="type'.$index.$v['id'].'" type="'.$type.'" onclick="getorgdata('.$v['id'].','.$index.')"><span class="layui_bm">'.$orgs[$k]['name_grawth'].'</span><span> : </span><span class="layui-bolu">'.$v['value'].'条</span></li>';
  1049. }else{//没有数据不能点击
  1050. $html .= '<li id="type'.$index.$v['id'].'" type="'.$type.'" ><span class="layui_bm">'.$orgs[$k]['name_grawth'].'</span><span> : </span><span class="layui-bolux">'.$v['value'].'条</span></li>';
  1051. }
  1052. }
  1053. }
  1054. $person_count_type = $type=='talkskill'||$type=='submit' ? $res['person']['all_count'] : ($org_id==$root_id ? $all_comment : $res['person']['all_comment_count']);
  1055. // array_unshift($orgs,['name'=>$res['person']['name'],'value'=>$person_count_type]);
  1056. }else{
  1057. $person_count_type = $type=='talkskill'||$type=='submit' ? $res['person']['all_count'] : ($org_id==$root_id ? $all_comment : $res['person']['all_comment_count']);
  1058. $orgs = [['name'=>$res['person']['name'],'value'=>$person_count_type]];
  1059. $html .= '<span>暂无子部门</span>';
  1060. }
  1061. $res['data'] = $orgs;
  1062. $res['ok'] = $sum==0 ? 0 : 1;
  1063. $html .= '</ul>';
  1064. $res['html'] = $html;
  1065. $res['person_html'] = '<p id="person'.$org_id.'" type="'.$type.'" onclick="getperson('.$org_id.')" class="layui-p-bt">'.$res['person']['name'].':<span class="layui-bolu">'.$person_count_type.'条</span></p>';
  1066. $res['html'] = $res['person_html'].$res['html'];
  1067. //导航栏
  1068. $org_info = org::where('id',$org_id)->value('path');
  1069. $path = array_filter(explode('-',$org_info));
  1070. $org_ids = org::where([['id','in',$path]])->field('id,name')->column('id,name','id');
  1071. $paths = [];
  1072. foreach ($path as $k => $v) {
  1073. $arr = $org_ids[$v];
  1074. $index = (int)(mt_rand(100000,9999999).mt_rand(100000,9999999));
  1075. $paths[] = '<span style="color:#249efb;" id="type'.$index.$arr['id'].'" type="'.$type.'" onclick="getorgdata('.$arr['id'].','.$index.')">'.$arr['name'].'</span>';
  1076. }
  1077. // foreach ($org_ids as $k => $v) {
  1078. // $index = (int)(mt_rand(100000,9999999).mt_rand(100000,9999999));
  1079. // $paths[] = '<span style="color:#249efb;" id="type'.$index.$v['id'].'" type="'.$type.'" onclick="getorgdata('.$v['id'].','.$index.')">'.$v['name'].'</span>';
  1080. // }
  1081. $res['path'] = implode('/',$paths);
  1082. $res['html'] = $res['html'].'<br/>'.$res['path'];
  1083. return json_encode($res);
  1084. }
  1085. public function get_person(){
  1086. $org_id = input('org_id',0);
  1087. $org_id = $org_id ?: request()->employee->root_id;
  1088. $type = input('type','talkskill');
  1089. $w2[] = ['employee.org_id','=',$org_id];
  1090. $w2[] = ['employee.root_id','=',request()->employee->root_id];
  1091. $w2[] = ['talkskill.root_id','=',request()->employee->root_id];
  1092. $w2[] = ['employee.state','=','在职'];
  1093. $w2[] = ['del','=',0];
  1094. $w2[] = ['approve','=',1];
  1095. $person1 = TalkskillModel::withJoin(['employee'=>[]],'inner');
  1096. $w3[] = ['org_id','=',$org_id];
  1097. $w3[] = ['root_id','=',request()->employee->root_id];
  1098. $w3[] = ['state','=','在职'];
  1099. $person = Employee::where($w3)->field('name,id')->select()->toArray();
  1100. if ($type=='talkskill') {
  1101. $w2[] = ['talkskill.employee_id','>',0];
  1102. $w2[] = ['talkskill.type','=','share'];
  1103. $person1 = $person1->where($w2)
  1104. // ->field('employee.org_id,talkskill.id,employee.uid,employee.id as eid,employee.name as ename')
  1105. ->group('employee_id')
  1106. ->column('count(*)','employee_id');
  1107. // ->toArray();
  1108. foreach ($person as $k => $v) {
  1109. $person[$k]['value'] = isset($person1[$v['id']]) ? $person1[$v['id']] : 0;
  1110. }
  1111. }elseif ($type=='submit') {
  1112. $w2[] = ['talkskill.employee_id','>',0];
  1113. $w2[] = ['talkskill.type','=','submit'];
  1114. $person1 = $person1->where($w2)
  1115. // ->field('employee.org_id,talkskill.id,employee.uid,employee.id as eid,employee.name as ename')
  1116. ->group('employee_id')
  1117. ->column('count(*)','employee_id');
  1118. // ->toArray();
  1119. foreach ($person as $k => $v) {
  1120. $person[$k]['value'] = isset($person1[$v['id']]) ? $person1[$v['id']] : 0;
  1121. }
  1122. }
  1123. else{
  1124. $person1 = $person1->where($w2)->field('employee.org_id,talkskill.id,employee.uid,employee.id as eid,employee.name as ename')->select()->toArray();
  1125. $taids = $person1 ? array_column($person1,'id') : [];
  1126. $w4[] = ['taid','in',$taids];
  1127. $comment = TalkskillComment::where($w4)->group('taid')->column('count(*)','taid');
  1128. $comment_all = [];
  1129. foreach ($person1 as $k => &$v) {
  1130. $v['comment_count'] = isset($comment[$v['id']]) ? $comment[$v['id']] : 0;
  1131. $comment_all[$v['eid']] = isset($comment_all[$v['eid']]) ? $comment_all[$v['eid']]+$v['comment_count'] : $v['comment_count'];
  1132. }
  1133. foreach ($person as $k => $v) {
  1134. $person[$k]['value'] = isset($comment_all[$v['id']]) ? $comment_all[$v['id']] : 0;
  1135. }
  1136. }
  1137. $res['data'] = $person;
  1138. $html = '';
  1139. foreach ($person as $k => $v) {
  1140. $html .= '<span>'.$v['name'].':'.$v['value'].'条</span>&nbsp;&nbsp;&nbsp;&nbsp;';
  1141. }
  1142. $res['html'] = $html;
  1143. //导航栏
  1144. $org_info = org::where('id',$org_id)->value('path');
  1145. $path = array_filter(explode('-',$org_info));
  1146. $org_ids = org::where([['id','in',$path]])->field('id,name')->order('id asc')->select()->toArray();
  1147. $paths = [];
  1148. foreach ($org_ids as $k => $v) {
  1149. $paths[] = '<span style="color:#249efb;" id="type'.$v['id'].'" type="'.$type.'" onclick="getorgdata('.$v['id'].')">'.$v['name'].'</span>';
  1150. }
  1151. $res['path'] = implode('/',$paths);
  1152. $res['html'] = $res['html'].'<br/><br/><br/><br/><br/><br/>'.$res['path'];
  1153. return json_encode($res);
  1154. }
  1155. /*
  1156. * 一级获取场景
  1157. */
  1158. public function get_cate1($root_id)
  1159. {
  1160. $w[] = ['root_id','=',$root_id];
  1161. $w[] = ['type','=','cate'];
  1162. $w[] = ['pid','=',0];
  1163. $data = TalkskillCates::where($w)->field('*,name label_name')->select();
  1164. return $data;
  1165. }
  1166. /*
  1167. * 集团后台话术场景
  1168. */
  1169. public function groupTalkskillLabel()
  1170. {
  1171. $param = request()->param();
  1172. $root = isset($param['root_id']) ? $param['root_id'] : request()->employee->root_id;
  1173. if (!Request::isAjax()) {
  1174. $type = $this->get_cate1($root);
  1175. View::assign('type', $type);
  1176. View::assign('from',isset($param['from']) ? $param['from'] : 'add');
  1177. View::assign('root_id',isset($param['root_id']) ? $param['root_id'] : request()->employee->root_id);
  1178. return View::fetch();
  1179. }
  1180. $w[] = ['root_id','=',$root];
  1181. $w[] = ['type','=','cate'];
  1182. $w[] = ['pid','=',0];
  1183. $w[] = ['from_type','=',0];//只能共享自建话术
  1184. if (!empty($param['type'])) $w[] = ['id','=',$param['type']];
  1185. $data = TalkskillCates::where($w)->field('*,name label_name')->page($param['page'] , $param['limit'])->order('id asc')->select()->toArray();
  1186. $count = TalkskillCates::where($w)->count();
  1187. //查询场景下的话术
  1188. $where = [
  1189. ['root_id', '=', $root],
  1190. ['del', '=', 0],
  1191. ['approve', '=', 1],
  1192. ['content', '<>', ''],
  1193. // ['from_type', '=', 0]//只能共享自建话术
  1194. ];
  1195. if (isset($param['keyword']) && $param['keyword']) {
  1196. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  1197. }
  1198. $label_count = TalkskillModel::where($where)->group('cate1')->column('count(id)','cate1');
  1199. $label_Maxtime = TalkskillModel::where($where)->group('cate1')->column('max(addtime)','cate1');
  1200. //查询共享到的店面
  1201. foreach ($data as &$v) {
  1202. $v['talkill_count'] = isset($label_count[$v['name']]) ? $label_count[$v['name']] : 0;
  1203. $v['update_time'] = isset($label_Maxtime[$v['name']]) ? $label_Maxtime[$v['name']] : '';
  1204. $v['shareCompany'] = TalkskillCates::where([['root_id','<>',$root],['text','like','%@'.$v['id'].'%']])->group('root_id')->count();
  1205. }
  1206. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  1207. }
  1208. /*
  1209. * 集团话术列表数据
  1210. */
  1211. public function groupTalkskillList()
  1212. {
  1213. if (!Request::isAjax()) {
  1214. $param = request()->param();
  1215. $root_id = !empty($param['root_id']) ? $param['root_id'] : request()->employee->root_id;
  1216. View::assign('type', $param['type']);
  1217. View::assign('from', $param['from']);
  1218. View::assign('root_id', $param['root_id']);
  1219. //二级分类
  1220. $param['id'] = $param['type'];
  1221. $w[] = ['type','=','cate'];
  1222. $w[] = ['root_id','=',$root_id];
  1223. $w[] = ['pid','=',$param['id']];
  1224. $cate = TalkskillCates::where($w)->field('name,id')->order('id asc')->select()->toArray();
  1225. view::assign('cate', $cate);
  1226. //房屋类型
  1227. $w1[] = ['type','=','house_type'];
  1228. $w1[] = ['root_id','=',$root_id];
  1229. $w1[] = ['pid','=',0];
  1230. $house_type = TalkskillCates::where($w1)->field('name,id')->order('id asc')->select()->toArray();
  1231. view::assign('house_type', $house_type);
  1232. //适用人群
  1233. $w3[] = ['root_id','=',$root_id];
  1234. $user_cate = TalkskillUsersCate::where($w3)->field('name,id')->order('id asc')->select()->toArray();
  1235. view::assign('user_cate', $user_cate);
  1236. return View::fetch();
  1237. }
  1238. $param = Request::param();
  1239. $cate1 = TalkskillCates::where('id',$param['type'])->value('name');
  1240. $root_id = !empty($param['root_id']) ? $param['root_id'] : request()->employee->root_id;
  1241. $where = [
  1242. ['cate1', '=', $cate1],
  1243. ['del', '=', 0],
  1244. ['content', '<>', ''],
  1245. ['approve', '=', 1],
  1246. ['from_type', '=', 0],
  1247. ['root_id', '=', $root_id]
  1248. ];
  1249. if (!empty($param['keyword'])) {
  1250. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  1251. }
  1252. $sql = TalkskillModel::where($where);
  1253. if (!empty($param['cate'])) {
  1254. $sql->whereRaw("FIND_IN_SET('" . $param['cate'] . "' , cate2)");
  1255. }
  1256. if (!empty($param['house_type'])) {
  1257. $sql->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  1258. }
  1259. if (!empty($param['user_cate'])) {
  1260. $sql->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  1261. }
  1262. // 获取数据
  1263. $list = $sql->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  1264. $count = $sql->count();
  1265. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  1266. }
  1267. /*
  1268. * 共享的店面
  1269. */
  1270. public function groupShareCompany()
  1271. {
  1272. if (!Request::isAjax()) {
  1273. $id = request()->param('id');
  1274. $from = request()->param('from');
  1275. view::assign('id',$id);
  1276. view::assign('from',$from);
  1277. return View::fetch();
  1278. }
  1279. $param = request()->param();
  1280. $from = $param['from'] == 'company' ? 'from2' : 'from1';
  1281. $w[] = ['text','like','%@'.$param['id'].'%'];
  1282. $w[] = ['pid','=',0];
  1283. $text = TalkskillCates::where($w)->column('text,root_id');//共享/指派的所有店面和集团;
  1284. $root_ids = [];
  1285. foreach ($text as $k => $v) {
  1286. $text = array_unique(array_filter(explode(',',$v['text'])));
  1287. foreach ($text as $k2 => $v2) {
  1288. $arr = explode('@',$v2);
  1289. if (count($arr)==4 && $arr[3]==$from) {
  1290. $root_ids[$v['root_id']] = $arr[2];//店面id=>时间
  1291. }
  1292. }
  1293. }
  1294. $where[] = ['root_id','in',array_keys($root_ids)];
  1295. // $where[] = ['root_id','in',$root_ids];
  1296. // if($param['from'] == 'company')
  1297. // {
  1298. // $companyId = Company::where('root_id', request()->employee->root_id)->value('id');
  1299. // $lastCompany = Company::where('company_group',$companyId)->column('root_id');
  1300. // // $where = [
  1301. // // ['from_content_id','=',$param['id']],
  1302. // // ['from_root_id','in',$lastCompany]
  1303. // // ];
  1304. // $where[] = ['root_id','in',$lastCompany];
  1305. // }else{
  1306. // // $where = [
  1307. // // ['from_content_id','=',$param['id']],
  1308. // // ['from_root_id','=',request()->employee->root_id]
  1309. // // ];
  1310. // $where[] = ['root_id','=',request()->employee->root_id];
  1311. // }
  1312. $data = Company::where($where)->page($param['page'],$param['limit'])->select();
  1313. foreach ($data as $k1 => $v1) {
  1314. $data[$k1]['addtime'] = $root_ids[$v1['root_id']];
  1315. }
  1316. $count = Company::where($where)->count();
  1317. // $data = TalkskillLabel::with(['company'])->where($where)->page($param['page'],$param['limit'])->select();
  1318. // $count = TalkskillLabel::with(['company'])->where($where)->count();
  1319. return json(['code' => 0, 'data' => $data,'count'=>$count]);
  1320. }
  1321. /*
  1322. * 集团后台话术店面共享
  1323. */
  1324. public function groupCompanyTalkskill()
  1325. {
  1326. if (!Request::isAjax()) {
  1327. return View::fetch();
  1328. }
  1329. $param = Request::param();
  1330. //查询店面
  1331. $companyId = Company::where('root_id', request()->employee->root_id)->value('id');
  1332. $data = Company::where('company_group',$companyId)->field('root_id,company_name')->page($param['page'],$param['limit'])->select()->toArray();
  1333. $count = Company::where('company_group',$companyId)->count();
  1334. //内容
  1335. $root_id = array_column($data,'root_id');
  1336. $train = TalkskillModel::where([['root_id','in',$root_id],['del','=',0],['approve', '=', 1],['content', '<>', ''],['from_type','=',0]])->group('root_id')->column('count(id)','root_id');
  1337. foreach($data as &$v)
  1338. {
  1339. $v['count'] = isset($train[$v['root_id']])?$train[$v['root_id']]:0;
  1340. }
  1341. return json(['code' => 0, 'data' => $data, 'count' => $count]);
  1342. }
  1343. /*
  1344. * 指派
  1345. */
  1346. public function groupAssign()
  1347. {
  1348. $param = request()->param();
  1349. if (!Request::isAjax()) {
  1350. View::assign('id',$param['id']);
  1351. View::assign('from',$param['from']);
  1352. View::assign('root_id',isset($param['root_id'])?$param['root_id']:'');
  1353. return View::fetch();
  1354. }
  1355. $id = input('id',0);
  1356. $companyId = Company::where('root_id', request()->employee->root_id)->value('id');
  1357. $where = [['company_group','=',$companyId]];
  1358. if ($param['from'] == 'company') {
  1359. //排除自己共享给自己的课程
  1360. $companyId = Company::where('root_id', request()->employee->root_id)->value('id');
  1361. $lastCompany = Company::where('company_group',$companyId)->column('root_id');
  1362. $label = TalkskillCates::where([['id','=',$id],['root_id','in',$lastCompany]])->find();
  1363. $where[] = ['root_id','<>',$label->root_id];
  1364. }
  1365. $data = Company::where($where)->field('root_id,company_name')->page($param['page'],$param['limit'])->select();
  1366. $count = Company::where($where)->count();
  1367. return json(['code' => 0, 'data' => $data,'count'=>$count]);
  1368. }
  1369. /*
  1370. * 集团指派
  1371. */
  1372. public function groupAssignAdd()
  1373. {
  1374. $param = request()->param();
  1375. //共享时查询关联数据
  1376. if($param['from'] == 'company')//共享
  1377. {
  1378. $companyId = Company::where('root_id', request()->employee->root_id)->value('id');
  1379. $lastCompany = Company::where('company_group',$companyId)->column('root_id');
  1380. $where = [
  1381. ['id','=',$param['id']],
  1382. ['root_id','in',$lastCompany]
  1383. ];
  1384. }else{//指派
  1385. $where = [
  1386. ['id','=',$param['id']],
  1387. ['root_id','=',request()->employee->root_id]
  1388. ];
  1389. }
  1390. $talkskillLabel = TalkskillCates::where($where)->find();
  1391. if(!$talkskillLabel) return json(['code' => 1, 'msg' => '数据不存在']);
  1392. Db::startTrans();
  1393. try {
  1394. $ids = explode(',',$param['ids']);//店面id
  1395. //场景,二级分类,话术
  1396. foreach($ids as $v)
  1397. {
  1398. $from = $param['from'] == 'add' ? 1 : 2;
  1399. $this->share_cate1($talkskillLabel,$v,$from);
  1400. }
  1401. Db::commit();
  1402. return json(['code' => 0, 'msg' => '操作成功']);
  1403. } catch (\Exception $e) {
  1404. Db::rollback();
  1405. return json(['code' => 1, 'msg' => '操作失败']);
  1406. }
  1407. }
  1408. /*
  1409. * 共享复制场景
  1410. * $data 场景信息
  1411. * $root_id 复制的id
  1412. */
  1413. public function share_cate1($data,$root_id,$from)
  1414. {
  1415. //如果场景名称已存在则修改
  1416. //复制场景
  1417. $w[] = [
  1418. ['root_id','=',$root_id],
  1419. ['name','=',$data->name],
  1420. ['pid','=',0],
  1421. ['type','=','cate']
  1422. ];
  1423. $info = TalkskillCates::where($w)->findOrEmpty();
  1424. $time = '@'.date('Y-m-d H:i:s').'@from'.$from;
  1425. if ($info->isEmpty()) {
  1426. $pid = TalkskillCates::insertGetId([
  1427. 'pid' => 0,
  1428. 'name' => $data->name,
  1429. 'type' => 'cate',
  1430. 'root_id' => $root_id,
  1431. 'from_root_id' => $data->root_id,
  1432. 'from_content_id' => $data->id,
  1433. 'from_type' => $from,
  1434. 'text' => $data->root_id.'@'.$data->id.$time
  1435. ]);
  1436. }else{
  1437. $pid = $info->id;
  1438. $arr = $data->root_id.'@'.$data->id.$time;
  1439. $text = $info->text ? $info->text.','.$arr : $arr;
  1440. $info->text = $text;
  1441. $info->save();
  1442. }
  1443. //共享二级分类
  1444. $cate2 = TalkskillCates::where('pid',$data->id)->column('id,name');//要复制的分类
  1445. $cate2s = TalkskillCates::where('pid',$pid)->column('name');//原有分类
  1446. $save = [];
  1447. foreach ($cate2 as $k9 => $v9) {
  1448. if (!$cate2s || !in_array($v9['name'],$cate2s)) {
  1449. $save[] = [
  1450. 'pid' => $pid,
  1451. 'name' => $v9['name'],
  1452. 'type' => 'cate',
  1453. 'root_id' => $root_id,
  1454. 'from_root_id' => $data->root_id,
  1455. 'from_content_id' => $v9['id'],
  1456. 'from_type' => $from,
  1457. 'text' => $data->root_id.'@'.$v9['id'].$time
  1458. ];
  1459. }elseif ($cate2s && in_array($v9['name'],$cate2s)) {//
  1460. $cate2_info = TalkskillCates::where([['pid','=',$pid],['name','=',$v9['name']]])->findOrEmpty();
  1461. if (!$cate2_info->isEmpty()) {
  1462. $arr = $data->root_id.'@'.$v9['id'].$time;
  1463. $text = $cate2_info->text ? $cate2_info->text.','.$arr : $arr;
  1464. $cate2_info->text = $text;
  1465. $cate2_info->save();
  1466. }
  1467. }
  1468. }
  1469. if($save) TalkskillCates::insertAll($save);
  1470. //复制话术
  1471. $n_talk = TalkskillModel::where([['root_id','=',$root_id],['from_content_id','>',0]])->column('from_content_id');//排除已经复制的话术
  1472. $talkskill = TalkskillModel::where([['cate1','=',$data->name],['root_id','=',$data->root_id],['id','not in',$n_talk]])->column('*');
  1473. $talk = [];
  1474. foreach ($talkskill as $k3 => $v3) {
  1475. $talk[] = [
  1476. 'title' => $v3['title'],
  1477. 'content' => $v3['content'],
  1478. 'root_id' => $root_id,
  1479. 'type' => 'admin',
  1480. 'employee_id' => request()->employee->id,
  1481. 'imgs' => $v3['imgs'],
  1482. 'videos' => $v3['videos'],
  1483. 'approve' => 1,
  1484. 'from_type' => $from,
  1485. 'from_root_id' => $data->root_id,
  1486. 'from_content_id' => $v3['id'],
  1487. 'approve_from' => 0,
  1488. 'user_cate' => $v3['user_cate'],
  1489. 'house_type' => $v3['house_type'],
  1490. 'cate1' => $v3['cate1'],
  1491. 'cate2' => $v3['cate2']
  1492. ];
  1493. }
  1494. if($talk) TalkskillModel::insertAll($talk);
  1495. return true;
  1496. }
  1497. /*
  1498. * 集团指派
  1499. */
  1500. // public function groupAssignAdd()
  1501. // {
  1502. // $param = request()->param();
  1503. // //共享时查询关联数据
  1504. // if($param['from'] == 'company')
  1505. // {
  1506. // $companyId = Company::where('root_id', request()->employee->root_id)->value('id');
  1507. // $lastCompany = Company::where('company_group',$companyId)->column('root_id');
  1508. // $where = [
  1509. // ['id','=',$param['id']],
  1510. // ['root_id','in',$lastCompany]
  1511. // ];
  1512. // }else{
  1513. // $where = [
  1514. // ['id','=',$param['id']],
  1515. // ['root_id','=',request()->employee->root_id]
  1516. // ];
  1517. // }
  1518. // $talkskillLabel = TalkskillLabel::where($where)->find();
  1519. // if(!$talkskillLabel) return json(['code' => 1, 'msg' => '数据不存在']);
  1520. // Db::startTrans();
  1521. // try {
  1522. // $ids = explode(',',$param['ids']);
  1523. // foreach($ids as $v)
  1524. // {
  1525. // $from = $param['from'] == 'add' ? 1 : 2;
  1526. // //场景共享
  1527. // $newTypeId = TalkskillLabel::insertGetId([
  1528. // 'from_type'=>$from,
  1529. // 'from_root_id'=>$talkskillLabel['root_id'],
  1530. // 'from_content_id'=>$talkskillLabel['id'],
  1531. // 'label_name'=>$talkskillLabel['label_name'] ,
  1532. // 'root_id'=>$v
  1533. // ]);
  1534. // $talkskill = TalkskillModel::where([
  1535. // ['label','=',$talkskillLabel['id']],
  1536. // ['del', '=', 0],
  1537. // ['content', '<>', ''],
  1538. // ['approve', '=', 1],
  1539. // ['from_type', '=', 0],
  1540. // ])->column('*');
  1541. // $newTalkskill = [];
  1542. // //话术数据
  1543. // foreach($talkskill as $val)
  1544. // {
  1545. // $data = [
  1546. // 'title' => $val['title'],
  1547. // 'content' => $val['content'],
  1548. // 'label' => $newTypeId,
  1549. // 'root_id' => $v,
  1550. // 'type' => 'admin',
  1551. // 'approve' => $val['approve'],
  1552. // 'from_type' => $from,
  1553. // 'from_root_id' => $val['root_id'],
  1554. // 'from_content_id' => $val['id'],
  1555. // ];
  1556. // $newTalkskill[] = $data;
  1557. // }
  1558. // TalkskillModel::insertAll($newTalkskill);
  1559. // }
  1560. // Db::commit();
  1561. // return json(['code' => 0, 'msg' => '操作成功']);
  1562. // } catch (\Exception $e) {
  1563. // Db::rollback();
  1564. // return json(['code' => 1, 'msg' => '操作失败']);
  1565. // }
  1566. // }
  1567. /*
  1568. * 审核记录
  1569. */
  1570. public function history()
  1571. {
  1572. $root_id = request()->employee->root_id;
  1573. $param = Request::only(['type'=>'share','org_id'=>0,'cate'=>'']);
  1574. View::assign('type', $param['type']);
  1575. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
  1576. view::assign('org', $org);
  1577. $w1[] = [['root_id','=',request()->employee->root_id],['uid','>',0]];
  1578. if ($param['type']=='share') {
  1579. $eid = TalkskillModel::where([['del','=',0],['root_id','=',$root_id],['employee_id','>',0],['type','=','share'],['approve','>',0]])->group('employee_id')->column('employee_id');
  1580. $w1[] = ['id','in',$eid];
  1581. }else{
  1582. $tid = TalkskillModel::where([['del','=',0],['root_id','=',$root_id],['employee_id','>',0],['type','=','share'],['approve','>',0]])->column('id');
  1583. $uid = TalkskillComment::where([['root_id','=',$root_id],['uid','>',0],['approve','>',0],['taid','in',$tid]])->group('uid')->column('uid');
  1584. $w1[] = ['uid','in',$uid];
  1585. }
  1586. if ($param['cate']) {
  1587. if ($param['org_id']) {
  1588. $w1[] = ['org_id','=',$param['org_id']];
  1589. }
  1590. $employee = Employee::where($w1)->field('id,name')->select()->toArray();
  1591. return json(['code' => 0,'data'=>$employee]);
  1592. }else{
  1593. $employee = Employee::where($w1)->field('id,name')->select()->toArray();
  1594. }
  1595. view::assign('employee', $employee);
  1596. $data['count'] = $data['pass_count'] = $data['no_count'] = 0;
  1597. if ($param['type']=='share') {
  1598. $w[] = ['root_id','=',$root_id];
  1599. $w[] = ['approve','>',0];
  1600. $w[] = ['type','=','share'];
  1601. $w[] = ['employee_id','>',0];
  1602. $w[] = ['del','=',0];
  1603. $data['count'] = TalkskillModel::where($w)->count();
  1604. $data['pass_count'] = TalkskillModel::where($w)->where('approve',1)->count();
  1605. $data['no_count'] = TalkskillModel::where($w)->where('approve',2)->count();
  1606. }else{
  1607. $w[] = ['root_id','=',$root_id];
  1608. $w[] = ['approve','>',0];
  1609. $w[] = ['uid','>',0];
  1610. $data['count'] = TalkskillComment::where($w)->count();
  1611. $data['pass_count'] = TalkskillComment::where($w)->where('approve',1)->count();
  1612. $data['no_count'] = TalkskillComment::where($w)->where('approve',2)->count();
  1613. }
  1614. view::assign('data', $data);
  1615. return View::fetch();
  1616. }
  1617. /*
  1618. * 审核记录数据接口
  1619. */
  1620. public function history_list()
  1621. {
  1622. $param = Request::only(['type'=>'share','page'=>1,'limit'=>10,'org'=>0,'cate'=>0,'employee_id'=>0,'time'=>'']);
  1623. $root_id = request()->employee->root_id;
  1624. if($param['cate']) $w[] = ['approve','=',$param['cate']];
  1625. if($param['time']){
  1626. $time = explode(' - ',$param['time']);
  1627. $w[] = ['addtime','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']];
  1628. }
  1629. $w[] = ['root_id','=',$root_id];
  1630. $w[] = ['approve','>',0];
  1631. //话术审核
  1632. if ($param['type']=='share') {
  1633. if($param['employee_id']) $w[] = ['employee_id','=',$param['employee_id']];
  1634. $w[] = ['type','=','share'];
  1635. $w[] = ['employee_id','>',0];
  1636. $w[] = ['del','=',0];
  1637. if ($param['org']) {
  1638. $eids = Employee::where([['org_id','=',$param['org']]])->column('id');
  1639. $w[] = ['employee_id','in',$eids];
  1640. }
  1641. $res = TalkskillModel::with(['employee','approveEmployee'])->order('id desc')->where($w)->page($param['page'],$param['limit'])->select();
  1642. foreach ($res as $k => $v) {
  1643. $v->status = $v->approve==1 ? '通过' : '驳回';
  1644. $v->org_name = isset($v->employee->org->name) ?$v->employee->org->name : '';
  1645. $v->approve_name = (strtotime($v->aprove_time)<=1656665072 || $v->approve_from==0) ? $v->opt_name : $v->approve_name;
  1646. $v->approve_from = (strtotime($v->aprove_time)<=1656665072 || $v->approve_from==0) ? 0 : 1;
  1647. }
  1648. $count = TalkskillModel::where($w)->count();
  1649. }else{//回答审核
  1650. if($param['employee_id']){
  1651. $wx[] = ['id','=',$param['employee_id']];
  1652. $wx[] = ['root_id','=',$root_id];
  1653. $uid = Employee::where($wx)->value('uid');
  1654. $w[] = ['uid','=',$uid];
  1655. }
  1656. if ($param['org']) {
  1657. $eids = Employee::where([['org_id','=',$param['org']]])->column('uid');
  1658. $w[] = ['uid','in',$eids];
  1659. }
  1660. $w[] = ['uid','>',0];
  1661. $res = TalkskillComment::with(['talkskills'=>function($query) use ($root_id){
  1662. $query->where([['root_id','=',$root_id]])->bind(['title']);
  1663. },'approveEmployee','employee'=>function($query) use ($root_id){
  1664. $query->where([['root_id','=',$root_id], ['state', 'in', ['在职']]]);
  1665. }])->where($w)->order('id desc')->page($param['page'],$param['limit'])->field('*,comments content')->visible([
  1666. ])->select();
  1667. $count = TalkskillComment::where($w)->count();
  1668. foreach ($res as $k => $v) {
  1669. $v->name = isset($v->employee->name) ?$v->employee->name : '';
  1670. $v->status = $v->approve==1 ? '通过' : '驳回';
  1671. $v->org_name = isset($v->employee->org->name) ?$v->employee->org->name : '';
  1672. $v->approve_name = (strtotime($v->aprove_time)<=1656665072 || $v->approve_from==0) ? $v->opt_name : $v->approve_name;
  1673. $v->approve_from = (strtotime($v->aprove_time)<=1656665072 || $v->approve_from==0) ? 0 : 1;
  1674. }
  1675. }
  1676. return json(['code' => 0, 'data'=>$res,'count'=>$count,'msg' => '操作成功']);
  1677. }
  1678. /*
  1679. * 使用人群
  1680. */
  1681. public function person()
  1682. {
  1683. return View::fetch();
  1684. }
  1685. /*
  1686. * 使用人群列表
  1687. */
  1688. public function person_list()
  1689. {
  1690. $root_id = request()->employee->root_id;
  1691. $param = Request::only(['page' => 1, 'limit' => 10]);
  1692. $w[] = ['root_id', '=', $root_id];
  1693. $list = TalkskillUsersCate::where($w)->page($param['page'], $param['limit'])->order('id asc')->select();
  1694. $count = TalkskillUsersCate::where($w)->count();
  1695. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  1696. }
  1697. /*
  1698. * 使用人群添加
  1699. */
  1700. public function person_add()
  1701. {
  1702. $root_id = request()->employee->root_id;
  1703. $param = Request::only(['name' => '']);
  1704. $param['name'] = str_replace(' ','',$param['name']);
  1705. $w[] = ['root_id', '=', $root_id];
  1706. $w[] = ['name', '=', $param['name']];
  1707. $find = TalkskillUsersCate::where($w)->findOrEmpty();
  1708. if (!$find->isEmpty()) return json(['code' => 1, 'data' => '使用人群已存在', 'msg' => '使用人群已存在']);
  1709. $id = TalkskillUsersCate::insertGetId(['root_id' => $root_id, 'name' => $param['name']]);
  1710. return json(['code' => 0, 'data' => '添加成功', 'msg' => '添加成功','id'=>$id]);
  1711. }
  1712. /*
  1713. * 使用人群编辑
  1714. */
  1715. public function person_edit()
  1716. {
  1717. $root_id = request()->employee->root_id;
  1718. $param = Request::only(['id' => 0, 'name' => '']);
  1719. $w[] = ['root_id', '=', $root_id];
  1720. $w[] = ['id', '<>', $param['id']];
  1721. $w[] = ['name', '=', $param['name']];
  1722. $find = TalkskillUsersCate::where($w)->findOrEmpty();
  1723. if (!$find->isEmpty()) return json(['code' => 1, 'data' => '使用人群已存在', 'msg' => '使用人群已存在']);
  1724. $name = TalkskillUsersCate::where('id', $param['id'])->value('name');
  1725. TalkskillUsersCate::where('id', $param['id'])->update(['name' => $param['name']]);
  1726. $w1[] = ['user_cate', '=', $name];
  1727. $w1[] = ['root_id', '=', $root_id];
  1728. TalkskillModel::where($w1)->update(['user_cate' => $param['name']]);
  1729. TalkskillComment::where($w1)->update(['user_cate' => $param['name']]);
  1730. return json(['code' => 0, 'data' => '修改成功', 'msg' => '修改成功']);
  1731. }
  1732. /*
  1733. * 字典管理
  1734. */
  1735. public function dictionary(){
  1736. return View::fetch();
  1737. }
  1738. /*
  1739. * type 1场景 ,2房屋类型,3适用人群,4二级分类,新增页下拉
  1740. */
  1741. public function dictionary_list(){
  1742. $root_id = request()->employee->root_id;
  1743. $param = Request::only(['type' => 1, 'page'=>1, 'limit'=>10, 'pid'=>0, 'scene_type'=> '']);
  1744. if ($param['type']==1) {
  1745. //场景列表
  1746. $w[] = ['pid','=',0];
  1747. $w[] = ['type','=','cate'];
  1748. $w[] = ['root_id','=',$root_id];
  1749. if ($param['scene_type']){
  1750. $w[] = ['scene_type', '=', $param['scene_type']];
  1751. }
  1752. $list = TalkskillCates::where($w)->field('name value,name,id')->order('id asc')->page($param['page'],$param['limit'])->select();
  1753. $count = TalkskillCates::where($w)->count();
  1754. } elseif ($param['type']==2) {
  1755. //房屋类型列表
  1756. $w[] = ['pid','=',0];
  1757. $w[] = ['type','=','house_type'];
  1758. $w[] = ['root_id','=',$root_id];
  1759. $list = TalkskillCates::where($w)->field('name value,name,id')->order('id asc')->page($param['page'],$param['limit'])->select();
  1760. $count = TalkskillCates::where($w)->count();
  1761. } elseif ($param['type']==3) {
  1762. //适用人群列表
  1763. $w[] = ['root_id','=',$root_id];
  1764. $list = TalkskillUsersCate::where($w)->field('name value,name,id')->page($param['page'],$param['limit'])->select();
  1765. $count = TalkskillUsersCate::where($w)->count();
  1766. } elseif ($param['type']==4 && $param['pid']) {
  1767. //二级分类
  1768. $w[] = ['pid','=',$param['pid']];
  1769. $w[] = ['type','=','cate'];
  1770. $w[] = ['root_id','=',$root_id];
  1771. $list = TalkskillCates::where($w)->field('name value,name,id')->order('id asc')->page($param['page'],$param['limit'])->select();
  1772. $count = TalkskillCates::where($w)->count();
  1773. } else{
  1774. $list = [];
  1775. $count = 0;
  1776. }
  1777. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  1778. }
  1779. /*
  1780. * 字典管理-场景列表
  1781. */
  1782. public function dictionary_ajax_cate(){
  1783. $root_id = request()->employee->root_id;
  1784. $param = Request::only(['page'=>1,'limit'=>10]);
  1785. //一级分类和二级分类
  1786. $w[] = ['type','=','cate'];
  1787. $w[] = ['root_id','=',$root_id];
  1788. $w[] = ['pid','=',0];
  1789. $list = TalkskillCates::where($w)->field('name,id,show,scene_type')->page($param['page'],$param['limit'])->order('id asc')->select()->toArray();
  1790. $count = TalkskillCates::where($w)->count();
  1791. $w1[] = ['pid','in',array_column($list,'id')];
  1792. $child = TalkskillCates::where($w1)->group('pid')->column('count(id) count','pid');
  1793. foreach ($list as $k => $v) {
  1794. $list[$k]['count'] = isset($child[$v['id']]) ? $child[$v['id']] : 0;
  1795. }
  1796. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  1797. }
  1798. /*
  1799. * 字典管理-房屋类型
  1800. */
  1801. public function dictionary_ajax_house_type(){
  1802. $root_id = request()->employee->root_id;
  1803. $param = Request::only(['page'=>1,'limit'=>10]);
  1804. //房屋类型
  1805. $w[] = ['type','=','house_type'];
  1806. $w[] = ['root_id','=',$root_id];
  1807. $w[] = ['pid','=',0];
  1808. $list = TalkskillCates::where($w)->field('name,id,show')->page($param['page'],$param['limit'])->order('id asc')->select()->toArray();
  1809. $count = TalkskillCates::where($w)->count();
  1810. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  1811. }
  1812. /*
  1813. * 字典管理-适用人群
  1814. */
  1815. public function dictionary_ajax_user_cate(){
  1816. $root_id = request()->employee->root_id;
  1817. $param = Request::only(['page'=>1,'limit'=>10]);
  1818. //房屋类型
  1819. $w[] = ['root_id','=',$root_id];
  1820. $list = TalkskillUsersCate::where($w)->field('name,id,show')->page($param['page'],$param['limit'])->order('id asc')->select()->toArray();
  1821. $count = TalkskillUsersCate::where($w)->count();
  1822. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  1823. }
  1824. /*
  1825. * 字典管理-新增数据
  1826. */
  1827. public function dictionary_ajax_add(){
  1828. $root_id = request()->employee->root_id;
  1829. $param = Request::only(['type'=>0, 'name'=>'', 'is_chosen'=> '']);
  1830. if ($param['type']==0 && $param['name']) {
  1831. //新增场景
  1832. $w = [
  1833. ['type','=','cate'],
  1834. ['pid','=',0],
  1835. ['root_id','=',$root_id],
  1836. ['name','=',$param['name']]
  1837. ];
  1838. $info = TalkskillCates::where($w)->findOrEmpty();
  1839. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '场景已存在','msg'=>'场景已存在']);
  1840. $save = [
  1841. 'pid' => 0,
  1842. 'name' => $param['name'],
  1843. 'type' => 'cate',
  1844. 'root_id' => $root_id
  1845. ];
  1846. if ($param['is_chosen']) {
  1847. $save['scene_type'] = 'chosen';
  1848. }
  1849. TalkskillCates::insertGetId($save);
  1850. }elseif($param['type']==1 && $param['name']){
  1851. //房屋类型
  1852. $w = [
  1853. ['type','=','house_type'],
  1854. ['pid','=',0],
  1855. ['root_id','=',$root_id],
  1856. ['name','=',$param['name']]
  1857. ];
  1858. $info = TalkskillCates::where($w)->findOrEmpty();
  1859. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '房屋类型已存在','msg'=>'房屋类型已存在']);
  1860. $save = [
  1861. 'pid' => 0,
  1862. 'name' => $param['name'],
  1863. 'type' => 'house_type',
  1864. 'root_id' => $root_id
  1865. ];
  1866. TalkskillCates::insertGetId($save);
  1867. } elseif ($param['type']==2 && $param['name']) {
  1868. //适用部门
  1869. $w = [
  1870. ['root_id','=',$root_id],
  1871. ['name','=',$param['name']]
  1872. ];
  1873. $info = TalkskillUsersCate::where($w)->findOrEmpty();
  1874. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '适用部门已存在','msg'=>'适用部门已存在']);
  1875. $save = [
  1876. 'name' => $param['name'],
  1877. 'root_id' => $root_id
  1878. ];
  1879. TalkskillUsersCate::insertGetId($save);
  1880. }else{
  1881. return json(['code' => 1, 'data' => '保存失败','msg'=>'保存失败']);
  1882. }
  1883. return json(['code' => 0, 'data' => '保存成功','msg'=>'保存成功']);
  1884. }
  1885. /*
  1886. * 字典管理-编辑数据
  1887. */
  1888. public function dictionary_ajax_edit(){
  1889. $root_id = request()->employee->root_id;
  1890. $param = Request::only(['type'=>0, 'name'=>'', 'id'=>0, 'is_chosen']);
  1891. if ($param['type']==0 && $param['name']) {
  1892. $where = [
  1893. ['root_id','=',$root_id],
  1894. ['id','=',$param['id']]
  1895. ];
  1896. $info = $find = TalkskillCates::where($where)->findOrEmpty();
  1897. if($info->isEmpty()) return json(['code' => 1, 'data' => '数据错误', 'msg' => '数据错误']);
  1898. //新增场景
  1899. $w = [
  1900. ['type','=','cate'],
  1901. ['pid','=',0],
  1902. ['root_id','=',$root_id],
  1903. ['name','=',$param['name']],
  1904. ['id','<>',$param['id']]
  1905. ];
  1906. $info = TalkskillCates::where($w)->findOrEmpty();
  1907. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '场景已存在', 'msg' => '场景已存在']);
  1908. $save['name'] = $param['name'];
  1909. if (isset($param['is_chosen'])) {
  1910. $save['scene_type'] = '';
  1911. if ($param['is_chosen']) {
  1912. $save['scene_type'] = 'chosen';
  1913. }
  1914. }
  1915. TalkskillCates::where('id',$param['id'])->update($save);
  1916. //修改
  1917. $wt = [
  1918. ['root_id','=',$root_id],
  1919. ['cate1','=',$find->name],
  1920. ];
  1921. TalkskillModel::where($wt)->update(['cate1'=>$param['name']]);
  1922. }elseif($param['type']==1 && $param['name']){
  1923. $where = [
  1924. ['root_id','=',$root_id],
  1925. ['id','=',$param['id']]
  1926. ];
  1927. $info = $find = TalkskillCates::where($where)->findOrEmpty();
  1928. if($info->isEmpty()) return json(['code' => 1, 'data' => '数据错误', 'msg' => '数据错误']);
  1929. //
  1930. $w = [
  1931. ['type','=','house_type'],
  1932. ['pid','=',0],
  1933. ['root_id','=',$root_id],
  1934. ['name','=',$param['name']],
  1935. ['id','<>',$param['id']]
  1936. ];
  1937. $info = TalkskillCates::where($w)->findOrEmpty();
  1938. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '房屋类型已存在', 'msg' => '房屋类型已存在']);
  1939. TalkskillCates::where('id',$param['id'])->update(['name'=>$param['name']]);
  1940. //修改话术中的房屋类型
  1941. $talk = TalkskillModel::where([['root_id','=',$root_id],['house_type','like','%'.$find->name.'%']])->column('id,house_type');
  1942. foreach ($talk as $v) {
  1943. $arr = explode(',',$v['house_type']);
  1944. if (in_array($find->name,$arr)) {
  1945. $arr = array_diff($arr,[$find->name]);
  1946. $arr[] = $param['name'];
  1947. TalkskillModel::where('id',$v['id'])->update(['house_type'=>implode(',',$arr)]);
  1948. }
  1949. }
  1950. } elseif ($param['type']==2 && $param['name']) {
  1951. $where = [
  1952. ['root_id','=',$root_id],
  1953. ['id','=',$param['id']]
  1954. ];
  1955. $info = $find = TalkskillUsersCate::where($where)->findOrEmpty();
  1956. if($info->isEmpty()) return json(['code' => 1, 'data' => '数据错误', 'msg' => '数据错误']);
  1957. //适用部门
  1958. $w = [
  1959. ['root_id','=',$root_id],
  1960. ['name','=',$param['name']],
  1961. ['id','<>',$param['id']]
  1962. ];
  1963. $info = TalkskillUsersCate::where($w)->findOrEmpty();
  1964. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '适用部门已存在', 'msg' => '适用部门已存在']);
  1965. TalkskillUsersCate::where('id',$param['id'])->update(['name'=>$param['name']]);
  1966. //修改话术中的适用部门
  1967. $talk = TalkskillModel::where([['root_id','=',$root_id],['user_cate','like','%'.$find->name.'%']])->column('id,user_cate');
  1968. foreach ($talk as $v) {
  1969. $arr = explode(',',$v['user_cate']);
  1970. if (in_array($find->name,$arr)) {
  1971. $arr = array_diff($arr,[$find->name]);
  1972. $arr[] = $param['name'];
  1973. TalkskillModel::where('id',$v['id'])->update(['user_cate'=>implode(',',$arr)]);
  1974. }
  1975. }
  1976. }else{
  1977. return json(['code' => 1, 'data' => '编辑失败','msg'=>'编辑失败']);
  1978. }
  1979. return json(['code' => 0, 'data' => '保存成功', 'msg' => '保存成功']);
  1980. }
  1981. /*
  1982. * 新增话术新页面
  1983. */
  1984. public function new_add_talkskill(){
  1985. $id = input('id',0);
  1986. View::assign('id',$id);
  1987. $name = input('name','');
  1988. View::assign('name',$name);
  1989. return View::fetch();
  1990. }
  1991. /*
  1992. * 新增话术新页面下拉
  1993. */
  1994. public function new_add_talkskill_select(){
  1995. $root_id = request()->employee->root_id;
  1996. $param = Request::only(['id'=>0]);
  1997. //二级分类
  1998. $w[] = ['type','=','cate'];
  1999. $w[] = ['root_id','=',$root_id];
  2000. $w[] = ['pid','=',$param['id']];
  2001. $cate = TalkskillCates::where($w)->field('name,name value,id')->order('id asc')->select()->toArray();
  2002. $data['cate2'] = $cate;
  2003. //房屋类型
  2004. $w1[] = ['type','=','house_type'];
  2005. $w1[] = ['root_id','=',$root_id];
  2006. $w1[] = ['pid','=',0];
  2007. $house_type = TalkskillCates::where($w1)->field('name,name value,id')->order('id asc')->select()->toArray();
  2008. $data['house_type'] = $house_type;
  2009. //适用人群
  2010. $w3[] = ['root_id','=',$root_id];
  2011. $user_cate = TalkskillUsersCate::where($w3)->field('name,name value,id')->order('id asc')->select()->toArray();
  2012. $data['user_cate'] = $user_cate;
  2013. return json(['code' => 0, 'data' => $data]);
  2014. }
  2015. /*
  2016. * 二级分类列表新页面
  2017. */
  2018. public function cate2_view(){
  2019. $id = input('id',0);
  2020. View::assign('id',$id);
  2021. return View::fetch();
  2022. }
  2023. /*
  2024. * 获取二级分类列表
  2025. */
  2026. public function get_cate2_list(){
  2027. $param = Request::only(['page'=>1,'limit'=>10,'id'=>0]);
  2028. $root_id = request()->employee->root_id;
  2029. $w = [
  2030. ['pid','=',$param['id']],
  2031. ['root_id','=',$root_id]
  2032. ];
  2033. $list = TalkskillCates::where($w)->page($param['page'],$param['limit'])->order('id asc')->select()->toArray();
  2034. $count = TalkskillCates::where($w)->count();
  2035. return json(['code' => 0, 'data' => $list,'count'=>$count]);
  2036. }
  2037. /*
  2038. * 二级分类编辑
  2039. */
  2040. public function cate2_edit(){
  2041. $param = Request::only(['name'=>'','id'=>0,'pid'=>0]);
  2042. $root_id = request()->employee->root_id;
  2043. $w = [
  2044. ['root_id','=',$root_id],
  2045. ['type','=','cate'],
  2046. ['pid','>',0]
  2047. ];
  2048. if ($param['id']) {
  2049. $w[] = ['id','=',$param['id']];
  2050. $info = TalkskillCates::where($w)->findOrEmpty();
  2051. if($info->isEmpty()) return json(['code' => 1, 'data' => '数据不存在', 'msg' => '数据不存在']);
  2052. }
  2053. $w1[] = ['name','=',$param['name']];
  2054. $w1[] = ['root_id','=',$root_id];
  2055. $w1[] = ['type','=','cate'];
  2056. $w1[] = ['pid','=',$param['pid']];
  2057. if ($param['id']) {
  2058. $w1[] = ['id','<>',$param['id']];
  2059. }
  2060. $info = TalkskillCates::where($w1)->findOrEmpty();
  2061. if(!$info->isEmpty()) return json(['code' => 1, 'data' => '分类已存在', 'msg' => '分类已存在']);
  2062. if ($param['id']) {
  2063. $cate1 = TalkskillCates::where('id',$param['pid'])->value('name');
  2064. $cate2 = TalkskillCates::where('id',$param['id'])->value('name');
  2065. TalkskillCates::where('id',$param['id'])->update(['name'=>$param['name']]);
  2066. //修改话术二级分类
  2067. $talk = TalkskillModel::where([['root_id','=',$root_id],['cate1','=',$cate1],['cate2','like','%'.$cate2.'%']])->column('id,cate2');
  2068. foreach ($talk as $v) {
  2069. $arr = explode(',',$v['cate2']);
  2070. if (in_array($cate2,$arr)) {
  2071. $arr = array_diff($arr,[$cate2]);
  2072. $arr[] = $param['name'];
  2073. TalkskillModel::where('id',$v['id'])->update(['cate2'=>implode(',',$arr)]);
  2074. }
  2075. }
  2076. return json(['code' => 0, 'data' => '修改成功', 'msg' => '修改成功']);
  2077. }else{
  2078. TalkskillCates::insertGetId([
  2079. 'pid' => $param['pid'],
  2080. 'name' => $param['name'],
  2081. 'type' => 'cate',
  2082. 'root_id' => $root_id
  2083. ]);
  2084. return json(['code' => 0, 'data' => '保存成功', 'msg' => '保存成功']);
  2085. }
  2086. }
  2087. /*
  2088. * 场景删除
  2089. */
  2090. public function cate_del()
  2091. {
  2092. $param = Request::param();
  2093. if($param['type'] == 2)
  2094. {
  2095. $data = TalkskillUsersCate::where(['root_id'=>request()->employee->root_id,'id'=>$param['id']])->find();
  2096. }else{
  2097. $data = TalkskillCates::where(['root_id'=>request()->employee->root_id,'id'=>$param['id']])->find();
  2098. }
  2099. if(empty($data)) return json(['code' => 1, 'msg' => '数据不存在']);
  2100. Db::startTrans();
  2101. try {
  2102. if($param['type'] == 0)
  2103. {
  2104. //场景是否存在其它分类
  2105. $type_id = TalkskillCates::where(['root_id'=>request()->employee->root_id,'pid'=>0,'show'=>1,'type'=>'cate'])->value('id');
  2106. if(empty($type_id)) $type_id = TalkskillCates::insertGetId(['pid'=>0,'name'=>'其它','root_id'=>request()->employee->root_id,'show'=>1,'type'=>'cate']);
  2107. //更新关联内容
  2108. TalkskillModel::where(['root_id'=>request()->employee->root_id,'cate1'=>$data['name']])->update(['cate1'=>'其它']);
  2109. TalkskillChosen::where(['root_id'=> request()->employee->root_id, 'cate1'=> $data['id']])->update(['cate1'=> $type_id]);
  2110. TalkskillSuccess::where(['root_id'=> request()->employee->root_id, 'cate1'=> $data['id']])->update(['cate1'=> $type_id]);
  2111. TalkskillCates::where(['root_id'=>request()->employee->root_id,'pid'=>$data['id'],'type'=>'cate'])->update(['pid'=>$type_id]);
  2112. //去重
  2113. $groupId = TalkskillCates::where(['root_id'=>request()->employee->root_id,'pid'=>$type_id,'show'=>1,'type'=>'cate'])->min('id');
  2114. TalkskillCates::where([['type','=','cate'],['root_id','=',request()->employee->root_id],['pid','=',$type_id],['show','=',1],['id','<>',$groupId]])->delete();
  2115. //删除分类
  2116. if($data['scene_type']=='chosen'){
  2117. TalkskillCates::where(['root_id'=>request()->employee->root_id,'id'=>$type_id,'type'=>'cate'])->update(['scene_type'=>'chosen']);
  2118. }
  2119. $data->delete();
  2120. }elseif($param['type'] == 1){
  2121. //房屋类型是否存在其它分类
  2122. $type_id = TalkskillCates::where(['root_id'=>request()->employee->root_id,'pid'=>0,'show'=>1,'type'=>'house_type'])->value('id');
  2123. if(empty($type_id)) $type_id = TalkskillCates::insertGetId(['pid'=>0,'name'=>'其它','root_id'=>request()->employee->root_id,'show'=>1,'type'=>'house_type']);
  2124. $for = TalkskillModel::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['name'] . "' , house_type)")->field('id,house_type')->select();
  2125. foreach($for as $item)
  2126. {
  2127. $string = str_replace($data['name'],"其它",$item['house_type']);
  2128. TalkskillModel::where(['root_id'=>request()->employee->root_id,'id'=>$item['id']])->update(['house_type'=>$string]);
  2129. }
  2130. $chosen_for = TalkskillChosen::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['id'] . "' , house_type)")->field('id,house_type')->select();
  2131. foreach($chosen_for as $item)
  2132. {
  2133. $c_old_house_type = explode(',', $item['house_type']);
  2134. foreach ($c_old_house_type as $k => $v){
  2135. if($v == $data['id']){
  2136. $c_old_house_type[$k] = $type_id;
  2137. }
  2138. }
  2139. $string = implode(',', $c_old_house_type);
  2140. TalkskillChosen::where(['root_id'=>request()->employee->root_id, 'id'=> $item['id']])->save(['house_type'=> $string]);
  2141. }
  2142. $success_for = TalkskillSuccess::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['id'] . "' , house_type)")->field('id,house_type')->select();
  2143. foreach($success_for as $item)
  2144. {
  2145. $s_old_house_type = explode(',', $item['house_type']);
  2146. foreach ($s_old_house_type as $k => $v){
  2147. if($v == $data['id']){
  2148. $s_old_house_type[$k] = $type_id;
  2149. }
  2150. }
  2151. $string = implode(',', $s_old_house_type);
  2152. TalkskillSuccess::where(['root_id'=>request()->employee->root_id, 'id'=> $item['id']])->save(['house_type'=> $string]);
  2153. }
  2154. if($data['scene_type']=='chosen'){
  2155. TalkskillCates::where(['root_id'=>request()->employee->root_id,'id'=>$type_id,'type'=>'house_type'])->update(['scene_type'=>'chosen']);
  2156. }
  2157. $data->delete();
  2158. }elseif($param['type'] == 2){
  2159. //适用部门是否存在其它分类
  2160. $type_id = TalkskillUsersCate::where(['root_id'=>request()->employee->root_id,'show'=>1])->value('id');
  2161. if(empty($type_id)) $type_id = TalkskillUsersCate::insertGetId(['name'=>'其它','root_id'=>request()->employee->root_id,'show'=>1]);
  2162. $for = TalkskillModel::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['name'] . "' , user_cate)")->field('id,user_cate')->select();
  2163. foreach($for as $item)
  2164. {
  2165. $string = str_replace($data['name'],"其它",$item['user_cate']);
  2166. TalkskillModel::where(['root_id'=>request()->employee->root_id,'id'=>$item['id']])->update(['user_cate'=>$string]);
  2167. }
  2168. $chosen_for = TalkskillChosen::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['id'] . "' , user_cate)")->field('id,user_cate')->select();
  2169. foreach($chosen_for as $item)
  2170. {
  2171. $c_old_user_cate = explode(',', $item['user_cate']);
  2172. foreach ($c_old_user_cate as $k => $v){
  2173. if($v == $data['id']){
  2174. $c_old_user_cate[$k] = $type_id;
  2175. }
  2176. }
  2177. $string = implode(',', $c_old_user_cate);
  2178. TalkskillChosen::where(['root_id'=>request()->employee->root_id, 'id'=> $item['id']])->save(['user_cate'=> $string]);
  2179. }
  2180. $success_for = TalkskillSuccess::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['id'] . "' , user_cate)")->field('id,user_cate')->select();
  2181. foreach($success_for as $item)
  2182. {
  2183. $s_old_user_cate = explode(',', $item['user_cate']);
  2184. foreach ($s_old_user_cate as $k => $v){
  2185. if($v == $data['id']){
  2186. $s_old_user_cate[$k] = $type_id;
  2187. }
  2188. }
  2189. $string = implode(',', $s_old_user_cate);
  2190. TalkskillSuccess::where(['root_id'=>request()->employee->root_id, 'id'=> $item['id']])->save(['user_cate'=> $string]);
  2191. }
  2192. $data->delete();
  2193. }elseif($param['type'] == 3){
  2194. $label_id = TalkskillCates::where(['root_id'=>request()->employee->root_id,'pid'=>$data['pid'],'show'=>1,'type'=>'cate'])->value('id');
  2195. if(empty($label_id)) $label_id = TalkskillCates::insertGetId(['pid'=>$data['pid'],'name'=>'其它','root_id'=>request()->employee->root_id,'show'=>1,'type'=>'cate']);
  2196. $for = TalkskillModel::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['name'] . "' , cate2)")->field('id,cate2')->select();
  2197. foreach($for as $item)
  2198. {
  2199. $string = str_replace($data['name'],"其它",$item['cate2']);
  2200. TalkskillModel::where(['root_id'=>request()->employee->root_id,'id'=>$item['id']])->update(['cate2'=>$string]);
  2201. }
  2202. $chosen_for = TalkskillChosen::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['id'] . "' , cate2)")->field('id,cate2')->select();
  2203. foreach($chosen_for as $item)
  2204. {
  2205. $c_old_cate2 = explode(',', $item['cate2']);
  2206. foreach ($c_old_cate2 as $k => $v){
  2207. if($v == $data['id']){
  2208. $c_old_cate2[$k] = $label_id;
  2209. }
  2210. }
  2211. $string = implode(',', $c_old_cate2);
  2212. TalkskillChosen::where(['root_id'=>request()->employee->root_id, 'id'=> $item['id']])->save(['cate2'=> $string]);
  2213. }
  2214. $success_for = TalkskillSuccess::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET('" . $data['id'] . "' , cate2)")->field('id,cate2')->select();
  2215. foreach($success_for as $item)
  2216. {
  2217. $s_old_cate2 = explode(',', $item['cate2']);
  2218. foreach ($s_old_cate2 as $k => $v){
  2219. if($v == $data['id']){
  2220. $s_old_cate2[$k] = $label_id;
  2221. }
  2222. }
  2223. $string = implode(',', $s_old_cate2);
  2224. TalkskillSuccess::where(['root_id'=>request()->employee->root_id, 'id'=> $item['id']])->save(['cate2'=> $string]);
  2225. }
  2226. //删除分类
  2227. $data->delete();
  2228. }
  2229. Db::commit();
  2230. return json(['code' => 0, 'msg' => '删除成功']);
  2231. } catch (\Exception $e) {
  2232. Db::rollback();
  2233. return json(['code' => 1, 'msg' => '删除失败']);
  2234. }
  2235. }
  2236. /*
  2237. * 精选话术分类场景页面
  2238. */
  2239. public function chosen_cate()
  2240. {
  2241. $where[] = ['root_id', '=', request()->employee->root_id];
  2242. $where[] = ['pid', '=', 0];
  2243. $where[] = ['type', '=', 'cate'];
  2244. $where[] = ['scene_type', '=', 'chosen'];
  2245. $type = TalkskillCates::where($where)->order('id asc')->select();
  2246. View::assign('type', $type);
  2247. return View::fetch();
  2248. }
  2249. /*
  2250. * 精选话术分类场景列表
  2251. */
  2252. public function chosen_cate_list()
  2253. {
  2254. $param = Request::only(['page' => 1, 'limit' => 10, 'keyword', 'type'=> '']);
  2255. $root_id = request()->employee->root_id;
  2256. //一级场景
  2257. $where[] = ['root_id', '=', $root_id];
  2258. $where[] = ['pid', '=', 0];
  2259. $where[] = ['type', '=', 'cate'];
  2260. $where[] = ['scene_type', '=', 'chosen'];
  2261. if (!empty($param['type'])) {
  2262. $where[] = ['id', '=', $param['type']];
  2263. }
  2264. $data = TalkskillCates::where($where)->page($param['page'] , $param['limit'])->order('id asc')->select()->toArray();
  2265. $count = TalkskillCates::where($where)->count();
  2266. if ($data) {
  2267. $condition = [
  2268. ['cate1', 'in', array_column($data, 'id')],
  2269. ['root_id', '=', $root_id],
  2270. ['del', '=', 0]
  2271. ];
  2272. if (isset($param['keyword']) && $param['keyword']) {
  2273. $chosen_id = TalkskillChosen::where(array_merge($condition, [['title', 'like', '%' . trim($param['keyword']) . '%']]))->column('id');
  2274. //同时检索回答内容
  2275. $content_talkskill_id = TalkskillContent::where([
  2276. ['root_id', '=', $root_id],
  2277. ['talkskill_type', '=', 'chosen'],
  2278. ['content', 'like', '%' . trim($param['keyword']) . '%']
  2279. ])->group('talkskill_id')->column('talkskill_id');
  2280. $new_arr = array_merge($chosen_id, $content_talkskill_id);
  2281. $condition[] = ['id', 'in', $new_arr];
  2282. }
  2283. $talkskill_count = TalkskillChosen::where($condition)->group('cate1')->column('count(id)','cate1');
  2284. $talkskill_view = TalkskillChosen::where($condition)->group('cate1')->column('sum(view_times)','cate1');
  2285. $talkskill_addtime = TalkskillChosen::where($condition)->group('cate1')->column('max(addtime)','cate1');
  2286. foreach ($data as &$v) {
  2287. //话术数量
  2288. $v['talkskill_count'] = isset($talkskill_count[$v['id']]) ? $talkskill_count[$v['id']] : 0;
  2289. //最近话术上传时间
  2290. $v['update_time'] = isset($talkskill_addtime[$v['id']]) ? $talkskill_addtime[$v['id']] : '';
  2291. //浏览人次
  2292. $v['view_times'] = isset($talkskill_view[$v['id']]) ? $talkskill_view[$v['id']] : 0;
  2293. //搜索值返回
  2294. $v['keyword'] = isset($param['keyword']) ? $param['keyword'] : '';
  2295. }
  2296. }
  2297. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count ]);
  2298. }
  2299. /**
  2300. * 精选话术
  2301. * @return string
  2302. */
  2303. public function chosen_index(){
  2304. if (!request()->isAjax()){
  2305. $type = request()->param('type');
  2306. View::assign('type', $type);
  2307. $root_id = request()->employee->root_id;
  2308. //二级分类
  2309. $where[] = ['type', '=', 'cate'];
  2310. $where[] = ['root_id', '=', $root_id];
  2311. $where[] = ['pid', '=', $type];
  2312. $cate = TalkskillCates::where($where)->field('name,id')->order('id asc')->select()->toarray();
  2313. view::assign('cate', $cate);
  2314. //房屋类型
  2315. $where_h[] = ['type', '=', 'house_type'];
  2316. $where_h[] = ['root_id', '=', $root_id];
  2317. $where_h[] = ['pid', '=', 0];
  2318. $house_type = TalkskillCates::where($where_h)->field('name,id')->order('id asc')->select()->toarray();
  2319. view::assign('house_type', $house_type);
  2320. //适用人群
  2321. $where_u[] = ['root_id', '=', $root_id];
  2322. $user_cate = TalkskillUsersCate::where($where_u)->field('name,id')->order('id asc')->select()->toarray();
  2323. view::assign('user_cate', $user_cate);
  2324. //通过场景列表筛选关键进入的带入关键词
  2325. $keyword = request()->param('keyword');
  2326. view::assign('keyword', isset($keyword)?$keyword:'');
  2327. $empIds = TalkskillChosen::where([['root_id', '=', $root_id],['cate1', '=', $type],['del', '=', 0]])->column('employee_id');
  2328. $employee = Employee::where([['id', 'in', $empIds]])->select();
  2329. View::assign('employee', $employee);
  2330. return View::fetch();
  2331. } else {
  2332. $param = Request::param();
  2333. $where = [
  2334. ['root_id', '=', request()->employee->root_id],
  2335. ['del', '=', 0],
  2336. ['cate1', '=', $param['type']]
  2337. ];
  2338. if (!empty($param['keyword'])) {
  2339. $chosen_id = TalkskillChosen::where(array_merge($where, [['title', 'like', '%' . trim($param['keyword']) . '%']]))->column('id');
  2340. //同时检索回答内容
  2341. $content_talkskill_id = TalkskillContent::where([
  2342. ['root_id', '=', request()->employee->root_id],
  2343. ['talkskill_type', '=', 'chosen'],
  2344. ['content', 'like', '%' . trim($param['keyword']) . '%']
  2345. ])->group('talkskill_id')->column('talkskill_id');
  2346. $new_arr = array_merge($chosen_id, $content_talkskill_id);
  2347. $where[] = ['id', 'in', $new_arr];
  2348. }
  2349. $sql = TalkskillChosen::with(['employee'=>function($query){
  2350. $query->field(['id','opt_name']);
  2351. }])->where($where);
  2352. if (!empty($param['cate'])) {
  2353. $sql->whereRaw("FIND_IN_SET('" . $param['cate'] . "' , cate2)");
  2354. }
  2355. if (!empty($param['house_type'])) {
  2356. $sql->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  2357. }
  2358. if (!empty($param['user_cate'])) {
  2359. $sql->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  2360. }
  2361. if (!empty($param['addtime'])) {
  2362. list($start_date, $end_date) = explode(' - ', $param['addtime']);
  2363. $sql->whereRaw("addtime >= '".$start_date."'");
  2364. $sql->whereRaw("addtime < '". date('Y-m-d H:i:s', strtotime($end_date) + 86400) ."'");
  2365. }
  2366. if (!empty($param['employee_id'])) {
  2367. $sql->whereRaw("employee_id=".$param['employee_id']);
  2368. }
  2369. // 获取数据
  2370. $list = $sql->page($param['page'], $param['limit'])->order('addtime desc')->select()->each(function ($item){
  2371. $item->cate1_name = TalkskillCates::where('id', '=', $item['cate1'])->value('name');
  2372. $cate2_name = TalkskillCates::where('id', 'in', $item['cate2'])->column('name');
  2373. $item->cate2_name = implode(',', $cate2_name);
  2374. $house_type_name = TalkskillCates::where('id', 'in', $item['house_type'])->column('name');
  2375. $item->house_type_name = implode(',', $house_type_name);
  2376. $user_cate_name = TalkskillUsersCate::where('id', 'in', $item['user_cate'])->column('name');
  2377. $item->user_cate_name = implode(',', $user_cate_name);
  2378. $content_img = TalkskillContent::where([['talkskill_id','=',$item->id], ['talkskill_type','=','chosen'], ['root_id','=',request()->employee->root_id], ['img','<>','']])->count();
  2379. $item->img = $content_img>0 ? true : false;
  2380. })->toArray();
  2381. $count = $sql->count();
  2382. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  2383. }
  2384. }
  2385. /*
  2386. * 精选话术新增新页面
  2387. */
  2388. public function chosen_add_new(){
  2389. $id = input('id',0);
  2390. View::assign('id',$id);
  2391. return View::fetch();
  2392. }
  2393. /*
  2394. * 精选话术添加
  2395. */
  2396. public function chosen_add()
  2397. {
  2398. if (!request()->isAjax()){
  2399. $root_id = request()->employee->root_id;
  2400. //场景
  2401. $where[] = ['type', '=', 'cate'];
  2402. $where[] = ['root_id', '=', $root_id];
  2403. $where[] = ['pid', '=', 0];
  2404. $where[] = ['scene_type', '=', 'chosen'];
  2405. $cate = TalkskillCates::where($where)->field('name,id')->order('id asc')->select()->toarray();
  2406. view::assign('cate', $cate);
  2407. //房屋类型
  2408. $where_h[] = ['type', '=', 'house_type'];
  2409. $where_h[] = ['root_id', '=', $root_id];
  2410. $where_h[] = ['pid', '=', 0];
  2411. $house_type = TalkskillCates::where($where_h)->field('name,id')->order('id asc')->select()->toarray();
  2412. view::assign('house_type', $house_type);
  2413. //适用人群
  2414. $where_u[] = ['root_id', '=', $root_id];
  2415. $user_cate = TalkskillUsersCate::where($where_u)->field('name,id')->order('id asc')->select()->toarray();
  2416. view::assign('user_cate', $user_cate);
  2417. return View::fetch();
  2418. } else {
  2419. $param = Request::only(['id', 'title', 'user_cate', 'content', 'house_type'=> '', 'cate1'=> '', 'cate2'=> '']);
  2420. $data = [
  2421. 'title' => $param['title'],
  2422. 'root_id' => request()->employee->root_id,
  2423. 'user_cate' => $param['user_cate'],
  2424. 'house_type' => $param['house_type'],
  2425. 'cate1' => $param['cate1'],
  2426. 'cate2' => $param['cate2'],
  2427. 'employee_id'=> request()->employee->id
  2428. ];
  2429. $result = TalkskillChosen::create($data);
  2430. dataStatistics(request()->employee->root_id,'talkskill_count',1,'inc');//manage应用首页统计数据
  2431. $id = $result->id;
  2432. if (!empty($param['content'])){
  2433. foreach ($param['content'] as $k => $v){
  2434. $content = [
  2435. 'talkskill_id'=> $id,
  2436. 'content' => $v['content'],
  2437. 'type'=> $v['type'],
  2438. 'talkskill_type'=> 'chosen',
  2439. 'root_id'=> request()->employee->root_id,
  2440. 'img'=> ''
  2441. ];
  2442. $img = Request::param('images_'.$k);
  2443. if(isset($img) && !empty($img)){
  2444. $content['img'] = implode(',',$img);
  2445. }
  2446. TalkskillContent::create($content);
  2447. }
  2448. }
  2449. return json(['code' => 0, 'msg' => '操作成功']);
  2450. }
  2451. }
  2452. /*
  2453. * 精选话术编辑
  2454. */
  2455. public function chosen_edit()
  2456. {
  2457. if (!request()->isAjax()){
  2458. $id = input('id', '', 'intval');
  2459. $where = [
  2460. 'id' => $id,
  2461. 'root_id' => request()->employee->root_id,
  2462. 'del' => 0
  2463. ];
  2464. $data = TalkskillChosen::where($where)->find();
  2465. if (!empty($data)){
  2466. $content = TalkskillContent::where([['talkskill_id', '=', $id], ['talkskill_type', '=', 'chosen']])->order('id asc')->select()->toArray();
  2467. foreach($content as &$val){
  2468. $val['content'] = json_encode($val['content']);
  2469. $val['img'] = $val['img'] ? explode(',',$val['img']) : '';
  2470. }
  2471. $data['content'] = $content;
  2472. }
  2473. View::assign('oss_bindurl', config('app.ali_oss_bindurl'));
  2474. View::assign('data', $data);
  2475. $root_id = request()->employee->root_id;
  2476. //场景
  2477. $where_c[] = ['type', '=', 'cate'];
  2478. $where_c[] = ['root_id', '=', $root_id];
  2479. $where_c[] = ['pid', '=', 0];
  2480. $where_c[] = ['scene_type', '=', 'chosen'];
  2481. $cate = TalkskillCates::where($where_c)->field('name,id')->order('id asc')->select()->toarray();
  2482. view::assign('cate', $cate);
  2483. //房屋类型
  2484. $where_h[] = ['type', '=', 'house_type'];
  2485. $where_h[] = ['root_id', '=', $root_id];
  2486. $where_h[] = ['pid', '=', 0];
  2487. $house_type = TalkskillCates::where($where_h)->field('name,id')->order('id asc')->select()->toarray();
  2488. view::assign('house_type', $house_type);
  2489. //适用人群
  2490. $where_u[] = ['root_id', '=', $root_id];
  2491. $user_cate = TalkskillUsersCate::where($where_u)->field('name,id')->order('id asc')->select()->toarray();
  2492. view::assign('user_cate', $user_cate);
  2493. return View::fetch();
  2494. } else {
  2495. $param = Request::only(['id', 'title', 'user_cate', 'content', 'house_type'=> '', 'cate1'=> '', 'cate2'=> '']);
  2496. $data = [
  2497. 'id' => $param['id'],
  2498. 'title' => $param['title'],
  2499. 'root_id' => request()->employee->root_id,
  2500. 'user_cate' => $param['user_cate'],
  2501. 'house_type' => $param['house_type'],
  2502. 'cate1' => $param['cate1'],
  2503. 'cate2' => $param['cate2'],
  2504. 'employee_id'=> request()->employee->id
  2505. ];
  2506. $talkskill = TalkskillChosen::where(['id' => $data['id'], 'root_id' => request()->employee->root_id])->find();
  2507. $talkskill->save($data);
  2508. $old_content = TalkskillContent::where([['talkskill_id', '=', $param['id']], ['talkskill_type', '=', 'chosen']])->select();
  2509. if ($old_content->isEmpty()) {
  2510. if (!empty($param['content'])){
  2511. foreach ($param['content'] as $k => $v){
  2512. $content = [
  2513. 'talkskill_id'=> $param['id'],
  2514. 'content' => $v['content'],
  2515. 'type'=> $v['type'],
  2516. 'talkskill_type'=> 'chosen',
  2517. 'root_id'=> request()->employee->root_id,
  2518. 'img'=> ''
  2519. ];
  2520. $img = Request::param('img_'.$k);
  2521. $imgnew = Request::param('imgnew_'.$k);
  2522. if(isset($img) && !empty($img)){
  2523. $content['img'] = implode(',',$img);
  2524. }
  2525. if(isset($imgnew) && !empty($imgnew)){
  2526. $content['img'] = $content['img'] ? $content['img'].','.implode(',',$imgnew) : implode(',',$imgnew) ;
  2527. }
  2528. TalkskillContent::create($content);
  2529. }
  2530. }
  2531. } else {
  2532. if (empty($param['content'])){ //全删
  2533. $old_content->delete();
  2534. } else {
  2535. // 删除去掉的话术
  2536. foreach ($old_content as $k => $v){
  2537. $delete = true;
  2538. foreach ($param['content'] as $kk => $vv) {
  2539. if (!empty($vv['id']) && $v['id'] == $vv['id']){
  2540. $delete = false;
  2541. }
  2542. }
  2543. if ($delete){
  2544. $v->delete();
  2545. }
  2546. }
  2547. // 更新及添加新话术
  2548. foreach ($param['content'] as $k => $v) {
  2549. if (!empty($v['id'])){
  2550. $img = Request::param('img_'.$k);
  2551. $imgnew = Request::param('imgnew_'.$k);
  2552. $v['img'] = '';
  2553. if(isset($img) && !empty($img)){
  2554. $v['img'] = implode(',',$img);
  2555. }
  2556. if(isset($imgnew) && !empty($imgnew)){
  2557. $v['img'] = $v['img'] ? $v['img'].','.implode(',',$imgnew) : implode(',',$imgnew) ;
  2558. }
  2559. TalkskillContent::where('id', '=', $v['id'])->save($v);
  2560. } else {
  2561. $content = [
  2562. 'talkskill_id'=> $param['id'],
  2563. 'content' => $v['content'],
  2564. 'type'=> $v['type'],
  2565. 'talkskill_type'=> 'chosen',
  2566. 'root_id'=> request()->employee->root_id,
  2567. 'img'=> ''
  2568. ];
  2569. $img = Request::param('img_'.$k);
  2570. $imgnew = Request::param('imgnew_'.$k);
  2571. if(isset($img) && !empty($img)){
  2572. $content['img'] = implode(',',$img);
  2573. }
  2574. if(isset($imgnew) && !empty($imgnew)){
  2575. $content['img'] = $content['img'] ? $content['img'].','.implode(',',$imgnew) : implode(',',$imgnew) ;
  2576. }
  2577. TalkskillContent::create($content);
  2578. }
  2579. }
  2580. }
  2581. }
  2582. return json(['code' => 0, 'msg' => '操作成功']);
  2583. }
  2584. }
  2585. /*
  2586. * 精选话术删除
  2587. */
  2588. public function chosen_delete()
  2589. {
  2590. $where[] = ['root_id','=',request()->employee->root_id];
  2591. $param = Request::only(['id'=>0,'ids'=>[]]);
  2592. $i = 1;
  2593. if ($param['id']) {
  2594. $where[] = ['id','=',$param['id']];
  2595. }elseif (!empty($param['ids']) && is_array($param['ids'])) {
  2596. $where[] = ['id','in',$param['ids']];
  2597. $i = count(array_filter($param['ids']));
  2598. }else{
  2599. return json(['code' => 1, 'msg' => '操作失败']);
  2600. }
  2601. TalkskillChosen::where($where)->update(['del' => 1]);
  2602. dataStatistics(request()->employee->root_id,'talkskill_count',$i,'dec');//manage应用首页统计数据
  2603. return json(['code' => 0, 'msg' => '操作成功']);
  2604. }
  2605. /*
  2606. * 成功案例分类列表页面
  2607. */
  2608. public function success_cate()
  2609. {
  2610. $where[] = ['root_id', '=', request()->employee->root_id];
  2611. $where[] = ['pid', '=', 0];
  2612. $where[] = ['type', '=', 'cate'];
  2613. $type = TalkskillCates::where($where)->order('id asc')->select();
  2614. View::assign('type', $type);
  2615. return View::fetch();
  2616. }
  2617. /**
  2618. * 成功案例分类
  2619. */
  2620. public function success_cate_list(){
  2621. $param = Request::only(['page' => 1, 'limit' => 10, 'keyword', 'type'=> '']);
  2622. $root_id = request()->employee->root_id;
  2623. //一级场景
  2624. $where[] = ['root_id', '=', $root_id];
  2625. $where[] = ['pid', '=', 0];
  2626. $where[] = ['type', '=', 'cate'];
  2627. if (!empty($param['type'])) {
  2628. $where[] = ['id', '=', $param['type']];
  2629. }
  2630. $data = TalkskillCates::where($where)->page($param['page'] , $param['limit'])->order('id asc')->select()->toArray();
  2631. $count = TalkskillCates::where($where)->count();
  2632. if ($data) {
  2633. $where2[] = ['cate1', 'in', array_column($data, 'id')];
  2634. $where2[] = ['root_id', '=', $root_id];
  2635. $where2[] = ['del', '=', 0];
  2636. if (isset($param['keyword']) && $param['keyword']) {
  2637. $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  2638. }
  2639. $talkskill = TalkskillSuccess::where($where2)->order('id asc')->select()->toarray();
  2640. $label_counts = $dates = $view_count = [];
  2641. foreach ($talkskill as $v) {
  2642. //话术数量
  2643. $label_counts[$v['cate1']] = isset($label_counts[$v['cate1']]) ? $label_counts[$v['cate1']] += 1 : 1;
  2644. //最近话术上传时间
  2645. $dates[$v['cate1']] = $v['addtime'];
  2646. //浏览人次
  2647. $view_count[$v['cate1']] = isset($view_count[$v['cate1']]) ? $view_count[$v['cate1']] + $v['view_times'] : $v['view_times'];
  2648. }
  2649. foreach ($data as $k => $v) {
  2650. $data[$k]['talkskill_count'] = isset($label_counts[$v['id']]) ? $label_counts[$v['id']] : 0;
  2651. $data[$k]['update_time'] = isset($dates[$v['id']]) ? $dates[$v['id']] : '';
  2652. $data[$k]['view_times'] = isset($view_count[$v['id']]) ? $view_count[$v['id']] : 0;
  2653. }
  2654. }
  2655. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  2656. }
  2657. /**
  2658. * 成功话术首页
  2659. */
  2660. public function success_index(){
  2661. if (!request()->isAjax()){
  2662. $root_id = request()->employee->root_id;
  2663. //一级分类
  2664. $where[] = ['type', '=', 'cate'];
  2665. $where[] = ['root_id', '=', $root_id];
  2666. $where[] = ['pid', '=', 0];
  2667. $cate = TalkskillCates::where($where)->order('id asc')->column('*','id');
  2668. view::assign('cate', $cate);
  2669. //二级分类
  2670. $label = TalkskillCates::where([['pid', '>', 0], ['type', '=', 'cate'], ['root_id', '=', $root_id]])->select()->toArray();
  2671. $joinlabel = [];
  2672. foreach ($label as $val) {
  2673. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  2674. }
  2675. View::assign('cate2', json_encode($joinlabel));
  2676. //房屋类型
  2677. $where_h[] = ['type', '=', 'house_type'];
  2678. $where_h[] = ['root_id', '=', $root_id];
  2679. $where_h[] = ['pid', '=', 0];
  2680. $house_type = TalkskillCates::where($where_h)->field('name,id')->order('id asc')->select()->toarray();
  2681. view::assign('house_type', $house_type);
  2682. //适用人群
  2683. $where_u[] = ['root_id', '=', $root_id];
  2684. $user_cate = TalkskillUsersCate::where($where_u)->field('name,id')->order('id asc')->select()->toarray();
  2685. view::assign('user_cate', $user_cate);
  2686. return View::fetch();
  2687. } else {
  2688. $param = Request::param();
  2689. $where = [
  2690. ['root_id', '=', request()->employee->root_id],
  2691. ['del', '=', 0],
  2692. ];
  2693. if (!empty($param['keyword'])) {
  2694. $column_id = TalkskillContent::where([
  2695. ['root_id', '=', request()->employee->root_id],
  2696. ['content', 'like', '%' . $param['keyword'] . '%'],
  2697. ['talkskill_type', '=', 'success']
  2698. // ['type', '=', 0]
  2699. ])->group('talkskill_id')->column('talkskill_id');
  2700. $success_id = TalkskillSuccess::where(array_merge($where, [['title', 'like', '%' . $param['keyword'] . '%']]))->column('id');
  2701. $new_arr = array_merge($column_id, $success_id);
  2702. $where[] = ['id', 'in', $new_arr];
  2703. }
  2704. if(!empty($param['addtime'])){
  2705. $times = explode(' - ',$param['addtime']);
  2706. $where[] = ['addtime','between',[$times[0].' 00:00:00',$times[1].' 23:59:59']];
  2707. }
  2708. $sql = TalkskillSuccess::where($where);
  2709. if (!empty($param['cate1'])) {
  2710. $sql->whereRaw("FIND_IN_SET('" . $param['cate1'] . "' , cate1)");
  2711. }
  2712. if (!empty($param['cate2'])) {
  2713. $sql->whereRaw("FIND_IN_SET('" . $param['cate2'] . "' , cate2)");
  2714. }
  2715. if (!empty($param['house_type'])) {
  2716. $sql->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  2717. }
  2718. if (!empty($param['user_cate'])) {
  2719. $sql->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  2720. }
  2721. // 获取数据
  2722. $list = $sql->page($param['page'], $param['limit'])->order('addtime desc')->select()->each(function ($item){
  2723. $item->cate1_name = TalkskillCates::where('id', '=', $item['cate1'])->value('name');
  2724. $cate2_name = TalkskillCates::where('id', 'in', $item['cate2'])->column('name');
  2725. $item->cate2_name = implode(',', $cate2_name);
  2726. $house_type_name = TalkskillCates::where('id', 'in', $item['house_type'])->column('name');
  2727. $item->house_type_name = implode(',', $house_type_name);
  2728. $user_cate_name = TalkskillUsersCate::where('id', 'in', $item['user_cate'])->column('name');
  2729. $item->user_cate_name = implode(',', $user_cate_name);
  2730. $content_img = TalkskillContent::where([['talkskill_id','=',$item->id], ['talkskill_type','=','success'], ['root_id','=',request()->employee->root_id], ['img','<>','']])->count();
  2731. $item->img = $content_img>0 ? true : false;
  2732. })->toArray();
  2733. $count = $sql->count();
  2734. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  2735. }
  2736. }
  2737. /*
  2738. * 成功话术新增新页面
  2739. */
  2740. public function success_add_new(){
  2741. $id = input('id',0);
  2742. View::assign('id', $id);
  2743. return View::fetch();
  2744. }
  2745. /*
  2746. * 成功案例添加
  2747. */
  2748. public function success_add()
  2749. {
  2750. if (!request()->isAjax()){
  2751. $root_id = request()->employee->root_id;
  2752. //场景
  2753. $where[] = ['type', '=', 'cate'];
  2754. $where[] = ['root_id', '=', $root_id];
  2755. $where[] = ['pid', '=', 0];
  2756. $cate = TalkskillCates::where($where)->field('name,id')->order('id asc')->select()->toarray();
  2757. view::assign('cate', $cate);
  2758. //房屋类型
  2759. $where_h[] = ['type', '=', 'house_type'];
  2760. $where_h[] = ['root_id', '=', $root_id];
  2761. $where_h[] = ['pid', '=', 0];
  2762. $house_type = TalkskillCates::where($where_h)->field('name,id')->order('id asc')->select()->toarray();
  2763. view::assign('house_type', $house_type);
  2764. //适用人群
  2765. $where_u[] = ['root_id', '=', $root_id];
  2766. $user_cate = TalkskillUsersCate::where($where_u)->field('name,id')->order('id asc')->select()->toarray();
  2767. view::assign('user_cate', $user_cate);
  2768. return View::fetch();
  2769. } else {
  2770. $param = Request::only(['title', 'user_cate', 'content', 'house_type'=> '', 'cate1'=> '', 'cate2'=> '']);
  2771. $data = [
  2772. 'title' => $param['title'],
  2773. 'root_id' => request()->employee->root_id,
  2774. 'user_cate' => $param['user_cate'],
  2775. 'house_type' => $param['house_type'],
  2776. 'cate1' => $param['cate1'],
  2777. 'cate2' => $param['cate2'],
  2778. 'employee_id'=> request()->employee->id
  2779. ];
  2780. $result = TalkskillSuccess::create($data);
  2781. dataStatistics(request()->employee->root_id,'talkskill_count',1,'inc');//manage应用首页统计数据
  2782. $id = $result->id;
  2783. if (!empty($param['content'])){
  2784. foreach ($param['content'] as $k => $v){
  2785. $content = [
  2786. 'talkskill_id'=> $id,
  2787. 'content' => $v['content'],
  2788. 'type'=> $v['type'],
  2789. 'talkskill_type'=> 'success',
  2790. 'root_id'=> request()->employee->root_id,
  2791. 'img'=> ''
  2792. ];
  2793. $img = Request::param('images_'.$k);
  2794. if(isset($img) && !empty($img)){
  2795. $content['img'] = implode(',',$img);
  2796. }
  2797. TalkskillContent::create($content);
  2798. }
  2799. }
  2800. return json(['code' => 0, 'msg' => '操作成功']);
  2801. }
  2802. }
  2803. /*
  2804. * 成功案例编辑
  2805. */
  2806. public function success_edit()
  2807. {
  2808. $id = input('id', '', 'intval');
  2809. if (!request()->isAjax()){
  2810. $where = [
  2811. 'id' => $id,
  2812. 'root_id' => request()->employee->root_id,
  2813. 'del' => 0
  2814. ];
  2815. $data = TalkskillSuccess::where($where)->find();
  2816. if (!empty($data)){
  2817. $content = TalkskillContent::where([['talkskill_id', '=', $id], ['talkskill_type', '=', 'success']])->order('id asc')->select()->toArray();
  2818. foreach($content as &$val){
  2819. $val['content'] = json_encode($val['content']);
  2820. $val['img'] = $val['img'] ? explode(',',$val['img']) : '';
  2821. }
  2822. $data['content'] = $content;
  2823. }
  2824. View::assign('oss_bindurl', config('app.ali_oss_bindurl'));
  2825. View::assign('data', $data);
  2826. $root_id = request()->employee->root_id;
  2827. //场景
  2828. $where_c[] = ['type', '=', 'cate'];
  2829. $where_c[] = ['root_id', '=', $root_id];
  2830. $where_c[] = ['pid', '=', 0];
  2831. $cate = TalkskillCates::where($where_c)->field('name,id')->order('id asc')->select()->toarray();
  2832. view::assign('cate', $cate);
  2833. //房屋类型
  2834. $where_h[] = ['type', '=', 'house_type'];
  2835. $where_h[] = ['root_id', '=', $root_id];
  2836. $where_h[] = ['pid', '=', 0];
  2837. $house_type = TalkskillCates::where($where_h)->field('name,id')->order('id asc')->select()->toarray();
  2838. view::assign('house_type', $house_type);
  2839. //适用人群
  2840. $where_u[] = ['root_id', '=', $root_id];
  2841. $user_cate = TalkskillUsersCate::where($where_u)->field('name,id')->order('id asc')->select()->toarray();
  2842. view::assign('user_cate', $user_cate);
  2843. return View::fetch();
  2844. } else {
  2845. $param = Request::only(['id', 'title', 'user_cate', 'content', 'house_type'=> '', 'cate1'=> '', 'cate2'=> '']);
  2846. $data = [
  2847. 'id' => $param['id'],
  2848. 'title' => $param['title'],
  2849. 'root_id' => request()->employee->root_id,
  2850. 'user_cate' => $param['user_cate'],
  2851. 'house_type' => $param['house_type'],
  2852. 'cate1' => $param['cate1'],
  2853. 'cate2' => $param['cate2'],
  2854. 'employee_id'=> request()->employee->id
  2855. ];
  2856. $talkskill = TalkskillSuccess::where(['id' => $data['id'], 'root_id' => request()->employee->root_id])->find();
  2857. $talkskill->save($data);
  2858. $old_content = TalkskillContent::where([['talkskill_id', '=', $param['id']], ['talkskill_type', '=', 'success']])->select();
  2859. if ($old_content->isEmpty()) {
  2860. if (!empty($param['content'])){
  2861. foreach ($param['content'] as $k => $v){
  2862. $content = [
  2863. 'talkskill_id'=> $param['id'],
  2864. 'content' => $v['content'],
  2865. 'type'=> $v['type'],
  2866. 'talkskill_type'=> 'success',
  2867. 'root_id'=> request()->employee->root_id,
  2868. 'img'=> ''
  2869. ];
  2870. $img = Request::param('img_'.$k);
  2871. $imgnew = Request::param('imgnew_'.$k);
  2872. if(isset($img) && !empty($img)){
  2873. $content['img'] = implode(',',$img);
  2874. }
  2875. if(isset($imgnew) && !empty($imgnew)){
  2876. $content['img'] = $content['img'] ? $content['img'].','.implode(',',$imgnew) : implode(',',$imgnew) ;
  2877. }
  2878. TalkskillContent::create($content);
  2879. }
  2880. }
  2881. } else {
  2882. if (empty($param['content'])){ //全删
  2883. $old_content->delete();
  2884. } else {
  2885. // 删除去掉的话术
  2886. foreach ($old_content as $k => $v){
  2887. $delete = true;
  2888. foreach ($param['content'] as $kk => $vv) {
  2889. if (!empty($vv['id']) && $v['id'] == $vv['id']){
  2890. $delete = false;
  2891. }
  2892. }
  2893. if ($delete){
  2894. $v->delete();
  2895. }
  2896. }
  2897. // 更新及添加新话术
  2898. foreach ($param['content'] as $k => $v) {
  2899. if (!empty($v['id'])){
  2900. $img = Request::param('img_'.$k);
  2901. $imgnew = Request::param('imgnew_'.$k);
  2902. $v['img'] = '';
  2903. if(isset($img) && !empty($img)){
  2904. $v['img'] = implode(',',$img);
  2905. }
  2906. if(isset($imgnew) && !empty($imgnew)){
  2907. $v['img'] = $v['img'] ? $v['img'].','.implode(',',$imgnew) : implode(',',$imgnew) ;
  2908. }
  2909. TalkskillContent::where('id', '=', $v['id'])->save($v);
  2910. } else {
  2911. $content = [
  2912. 'talkskill_id'=> $param['id'],
  2913. 'content' => $v['content'],
  2914. 'type'=> $v['type'],
  2915. 'talkskill_type'=> 'success',
  2916. 'root_id'=> request()->employee->root_id,
  2917. 'img'=> ''
  2918. ];
  2919. $img = Request::param('img_'.$k);
  2920. $imgnew = Request::param('imgnew_'.$k);
  2921. if(isset($img) && !empty($img)){
  2922. $content['img'] = implode(',',$img);
  2923. }
  2924. if(isset($imgnew) && !empty($imgnew)){
  2925. $content['img'] = $content['img'] ? $content['img'].','.implode(',',$imgnew) : implode(',',$imgnew) ;
  2926. }
  2927. TalkskillContent::create($content);
  2928. }
  2929. }
  2930. }
  2931. }
  2932. }
  2933. return json(['code' => 0, 'msg' => '操作成功']);
  2934. }
  2935. /*
  2936. * 成功案例删除
  2937. */
  2938. public function success_delete()
  2939. {
  2940. $where[] = ['root_id','=',request()->employee->root_id];
  2941. $param = Request::only(['id'=>0,'ids'=>[]]);
  2942. $i = 1;
  2943. if ($param['id']) {
  2944. $where[] = ['id','=',$param['id']];
  2945. }elseif (!empty($param['ids']) && is_array($param['ids'])) {
  2946. $where[] = ['id','in',$param['ids']];
  2947. $i = count(array_filter($param['ids']));
  2948. }else{
  2949. return json(['code' => 1, 'msg' => '操作失败']);
  2950. }
  2951. dataStatistics(request()->employee->root_id,'talkskill_count',$i,'dec');//manage应用首页统计数据
  2952. TalkskillSuccess::where($where)->update(['del' => 1]);
  2953. return json(['code' => 0, 'msg' => '操作成功']);
  2954. }
  2955. /**
  2956. * 此方法用于从某个企业复制<精选话术>至另一个企业
  2957. */
  2958. public function copy_talkskill_chosen($from_root_id = 0 , $new_root_id = 0)
  2959. {
  2960. if (!$from_root_id || !$new_root_id) return '错误';
  2961. if ($from_root_id == $new_root_id) return '错误';
  2962. $company = Company::where([['root_id','in',[$from_root_id,$new_root_id]]])->count();
  2963. if($company != 2) return '错误';
  2964. //使用人群
  2965. $new_user_cate = [];
  2966. $user_cate = TalkskillUsersCate::where(['root_id'=>$from_root_id])->order('addtime')->select();
  2967. foreach($user_cate as $cate){
  2968. $have_usercate = TalkskillUsersCate::where(['root_id'=>$new_root_id,'name'=>$cate['name']])->value('id');
  2969. if(!empty($have_usercate)){
  2970. $uc = $have_usercate;
  2971. }else{
  2972. $uc = TalkskillUsersCate::insertGetId(['root_id'=>$new_root_id,'name'=>$cate['name']]);
  2973. }
  2974. $new_user_cate[$cate['id']] = $uc;
  2975. }
  2976. //房屋类型
  2977. $cates = TalkskillCates::where([['root_id','=',$from_root_id],['type','=','house_type']])->order('addtime')->select();
  2978. $new_cates = [];
  2979. foreach($cates as $item){
  2980. $have_cates = TalkskillCates::where([['root_id','=',$new_root_id],['type','=','house_type'],['name','=',$item['name']]])->value('id');
  2981. if(!empty($have_cates)){
  2982. $add_house_type_id = $have_cates;
  2983. }else{
  2984. $add_house_type_id = TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'house_type','name'=>$item['name']]);
  2985. }
  2986. $new_cates[$item['id']] = $add_house_type_id;
  2987. }
  2988. //分类
  2989. $cate1 = TalkskillCates::where([['pid','=',0],['scene_type','=','chosen'],['root_id','=',$from_root_id],['type','=','cate']])->order('addtime')->select()->toArray();
  2990. $new_cate1 = [];
  2991. $new_cate2 = [];
  2992. foreach($cate1 as $value){
  2993. $have_cate1 = TalkskillCates::where(['scene_type'=>'chosen','root_id'=>$new_root_id,'type'=>'cate','name'=>$value['name'],'pid'=>0])->value('id');
  2994. if(!empty($have_cate1)){
  2995. $add_cate1_id = $have_cate1;
  2996. }else{
  2997. $add_cate1_id = TalkskillCates::insertGetId(['scene_type'=>'chosen','root_id'=>$new_root_id,'type'=>'cate','name'=>$value['name'],'pid'=>0]);
  2998. }
  2999. $new_cate1[$value['id']] = $add_cate1_id;
  3000. $cate2 = TalkskillCates::where([['pid','=',$value['id']],['root_id','=',$from_root_id],['type','=','cate']])->order('addtime')->select();
  3001. foreach($cate2 as $value2){
  3002. $have_cate2 = TalkskillCates::where(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value2['name'],'pid'=>$add_cate1_id])->value('id');
  3003. if(!empty($have_cate2)){
  3004. $add_cate2_id = $have_cate2;
  3005. }else{
  3006. $add_cate2_id = TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value2['name'],'pid'=>$add_cate1_id]);
  3007. }
  3008. $new_cate2[$value2['id']] = $add_cate2_id;
  3009. }
  3010. }
  3011. $data = TalkskillChosen::where([['root_id','=',$from_root_id],['del','=',0]])->select()->toArray();
  3012. foreach($data as $d){
  3013. $user_cate_str = explode(',',$d['user_cate']);
  3014. $create_user_cate = [];
  3015. foreach($user_cate_str as $u){
  3016. $create_user_cate[] = isset($new_user_cate[$u]) ? $new_user_cate[$u] : '';
  3017. }
  3018. $house_type_str = explode(',',$d['house_type']);
  3019. $create_house_type = [];
  3020. foreach($house_type_str as $h){
  3021. $create_house_type[] = isset($new_cates[$h]) ? $new_cates[$h] : '';
  3022. }
  3023. $cate2_str = explode(',',$d['cate2']);
  3024. $create_cate2 = [];
  3025. foreach($cate2_str as $c2){
  3026. $create_cate2[] = isset($new_cate2[$c2]) ? $new_cate2[$c2] : '';
  3027. }
  3028. $insert_data = [
  3029. 'root_id'=>$new_root_id,
  3030. 'title'=>$d['title'],
  3031. 'user_cate'=>implode(',',$create_user_cate),
  3032. 'house_type'=>implode(',',$create_house_type),
  3033. 'cate1'=>isset($new_cate1[$d['cate1']]) ? $new_cate1[$d['cate1']] : '',
  3034. 'cate2'=>implode(',',$create_cate2),
  3035. ];
  3036. $addChosenId = TalkskillChosen::insertGetId($insert_data);
  3037. $content = TalkskillContent::where(['root_id'=>$d['root_id'],'talkskill_id'=>$d['id'],'talkskill_type'=>'chosen'])->order('addtime')->select();
  3038. $contentSaveAll = [];
  3039. foreach($content as $co){
  3040. $contentSaveAll[] = [
  3041. 'root_id' => $new_root_id,
  3042. 'talkskill_id' => $addChosenId,
  3043. 'content' => $co['content'],
  3044. 'type' => $co['type'],
  3045. 'talkskill_type' => $co['talkskill_type'],
  3046. 'img' => $co['img'],
  3047. ];
  3048. }
  3049. TalkskillContent::insertAll($contentSaveAll);
  3050. }
  3051. return '复制成功';
  3052. }
  3053. /**
  3054. * 此方法用于从某个企业复制<成功案例话术>至另一个企业
  3055. */
  3056. public function copy_talkskill_success($from_root_id = 0 , $new_root_id = 0)
  3057. {
  3058. if (!$from_root_id || !$new_root_id) return '错误';
  3059. if ($from_root_id == $new_root_id) return '错误';
  3060. $company = Company::where([['root_id','in',[$from_root_id,$new_root_id]]])->count();
  3061. if($company != 2) return '错误';
  3062. //使用人群
  3063. $new_user_cate = [];
  3064. $user_cate = TalkskillUsersCate::where(['root_id'=>$from_root_id])->order('addtime')->select();
  3065. foreach($user_cate as $cate){
  3066. $have_usercate = TalkskillUsersCate::where(['root_id'=>$new_root_id,'name'=>$cate['name']])->value('id');
  3067. if(!empty($have_usercate)){
  3068. $uc = $have_usercate;
  3069. }else{
  3070. $uc = TalkskillUsersCate::insertGetId(['root_id'=>$new_root_id,'name'=>$cate['name']]);
  3071. }
  3072. $new_user_cate[$cate['id']] = $uc;
  3073. }
  3074. //房屋类型
  3075. $cates = TalkskillCates::where([['root_id','=',$from_root_id],['type','=','house_type']])->order('addtime')->select();
  3076. $new_cates = [];
  3077. foreach($cates as $item){
  3078. $have_cates = TalkskillCates::where([['root_id','=',$new_root_id],['type','=','house_type'],['name','=',$item['name']]])->value('id');
  3079. if(!empty($have_cates)){
  3080. $add_house_type_id = $have_cates;
  3081. }else{
  3082. $add_house_type_id = TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'house_type','name'=>$item['name']]);
  3083. }
  3084. $new_cates[$item['id']] = $add_house_type_id;
  3085. }
  3086. //分类
  3087. $cate1 = TalkskillCates::where([['pid','=',0],['root_id','=',$from_root_id],['type','=','cate']])->order('addtime')->select()->toArray();
  3088. $new_cate1 = [];
  3089. $new_cate2 = [];
  3090. foreach($cate1 as $value){
  3091. $have_cate1 = TalkskillCates::where(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value['name'],'pid'=>0])->value('id');
  3092. if(!empty($have_cate1)){
  3093. $add_cate1_id = $have_cate1;
  3094. }else{
  3095. $add_cate1_id = TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value['name'],'pid'=>0]);
  3096. }
  3097. $new_cate1[$value['id']] = $add_cate1_id;
  3098. $cate2 = TalkskillCates::where([['pid','=',$value['id']],['root_id','=',$from_root_id],['type','=','cate']])->order('addtime')->select();
  3099. foreach($cate2 as $value2){
  3100. $have_cate2 = TalkskillCates::where(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value2['name'],'pid'=>$add_cate1_id])->value('id');
  3101. if(!empty($have_cate)){
  3102. $add_cate2_id = $have_cate2;
  3103. }else{
  3104. $add_cate2_id = TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value2['name'],'pid'=>$add_cate1_id]);
  3105. }
  3106. $new_cate2[$value2['id']] = $add_cate2_id;
  3107. }
  3108. }
  3109. $data = TalkskillSuccess::where([['root_id','=',$from_root_id],['del','=',0]])->select()->toArray();
  3110. foreach($data as $d){
  3111. $user_cate_str = explode(',',$d['user_cate']);
  3112. $create_user_cate = [];
  3113. foreach($user_cate_str as $u){
  3114. $create_user_cate[] = isset($new_user_cate[$u]) ? $new_user_cate[$u] : '';
  3115. }
  3116. $house_type_str = explode(',',$d['house_type']);
  3117. $create_house_type = [];
  3118. foreach($house_type_str as $h){
  3119. $create_house_type[] = isset($new_cates[$h]) ? $new_cates[$h] : '';
  3120. }
  3121. $cate2_str = explode(',',$d['cate2']);
  3122. $create_cate2 = [];
  3123. foreach($cate2_str as $c2){
  3124. $create_cate2[] = isset($new_cate2[$c2]) ? $new_cate2[$c2] : '';
  3125. }
  3126. $insert_data = [
  3127. 'root_id'=>$new_root_id,
  3128. 'title'=>$d['title'],
  3129. 'user_cate'=>implode(',',$create_user_cate),
  3130. 'house_type'=>implode(',',$create_house_type),
  3131. 'cate1'=>isset($new_cate1[$d['cate1']]) ? $new_cate1[$d['cate1']] : '',
  3132. 'cate2'=>implode(',',$create_cate2),
  3133. ];
  3134. $addChosenId = TalkskillSuccess::insertGetId($insert_data);
  3135. $content = TalkskillContent::where(['root_id'=>$d['root_id'],'talkskill_id'=>$d['id'],'talkskill_type'=>'success'])->order('addtime')->select();
  3136. $contentSaveAll = [];
  3137. foreach($content as $co){
  3138. $contentSaveAll[] = [
  3139. 'root_id' => $new_root_id,
  3140. 'talkskill_id' => $addChosenId,
  3141. 'content' => $co['content'],
  3142. 'type' => $co['type'],
  3143. 'talkskill_type' => $co['talkskill_type'],
  3144. 'img' => $co['img'],
  3145. ];
  3146. }
  3147. TalkskillContent::insertAll($contentSaveAll);
  3148. }
  3149. return '复制成功';
  3150. }
  3151. /**
  3152. * 此方法用于从某个企业复制<话术管理>至另一个企业
  3153. */
  3154. public function copy_talkskill($from_root_id = 0 , $new_root_id = 0)
  3155. {
  3156. if (!$from_root_id || !$new_root_id) return '错误';
  3157. if ($from_root_id == $new_root_id) return '错误';
  3158. $company = Company::where([['root_id','in',[$from_root_id,$new_root_id]]])->count();
  3159. if($company != 2) return '错误';
  3160. //使用人群
  3161. $user_cate = TalkskillUsersCate::where(['root_id'=>$from_root_id])->order('addtime')->select();
  3162. foreach($user_cate as $cate){
  3163. $have_usercate = TalkskillUsersCate::where(['root_id'=>$new_root_id,'name'=>$cate['name']])->value('id');
  3164. if(empty($have_usercate)){
  3165. TalkskillUsersCate::insertGetId(['root_id'=>$new_root_id,'name'=>$cate['name']]);
  3166. }
  3167. }
  3168. //房屋类型
  3169. $cates = TalkskillCates::where([['root_id','=',$from_root_id],['type','=','house_type']])->order('addtime')->select();
  3170. foreach($cates as $item){
  3171. $have_cates = TalkskillCates::where([['root_id','=',$new_root_id],['type','=','house_type'],['name','=',$item['name']]])->value('id');
  3172. if(empty($have_cates)){
  3173. TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'house_type','name'=>$item['name']]);
  3174. }
  3175. }
  3176. //分类
  3177. $cate1 = TalkskillCates::where([['pid','=',0],['root_id','=',$from_root_id],['type','=','cate']])->order('addtime')->select()->toArray();
  3178. foreach($cate1 as $value){
  3179. $have_cate1 = TalkskillCates::where(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value['name'],'pid'=>0])->value('id');
  3180. if(empty($have_cate1)){
  3181. $add_cate1_id = TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value['name'],'pid'=>0]);
  3182. }else{
  3183. $add_cate1_id = $have_cate1;
  3184. }
  3185. $cate2 = TalkskillCates::where([['pid','=',$value['id']],['root_id','=',$from_root_id],['type','=','cate']])->order('addtime')->select();
  3186. foreach($cate2 as $value2){
  3187. $have_cate2 = TalkskillCates::where(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value2['name'],'pid'=>$add_cate1_id])->value('id');
  3188. if(empty($have_cate2)){
  3189. TalkskillCates::insertGetId(['root_id'=>$new_root_id,'type'=>'cate','name'=>$value2['name'],'pid'=>$add_cate1_id]);
  3190. }
  3191. }
  3192. }
  3193. //label标签
  3194. $label_arr = [];
  3195. $label = TalkskillLabel::where('root_id',$from_root_id)->order('addtime')->select();
  3196. foreach($label as $item){
  3197. $have_label = TalkskillLabel::where([['root_id','=',$new_root_id],['label_name','=',$item['label_name']]])->value('id');
  3198. if(empty($have_label)){
  3199. $label_id = TalkskillLabel::insertGetId(['root_id'=>$new_root_id,'label_name'=>$item['label_name']]);
  3200. }else{
  3201. $label_id = $have_label;
  3202. }
  3203. $label_arr[] = $label_id;
  3204. }
  3205. $data = TalkskillModel::where([['root_id','=',$from_root_id],['del','=',0],['type','=','admin']])->select()->toArray();
  3206. foreach($data as $d){
  3207. $insert_data = [
  3208. 'root_id'=>$new_root_id,
  3209. 'title'=>$d['title'],
  3210. 'content'=>$d['content'],
  3211. 'label'=>isset($label_arr[$d['label']]) ? $label_arr[$d['label']] : 0,
  3212. 'type'=>'admin',
  3213. 'imgs'=>$d['imgs'],
  3214. 'videos'=>$d['videos'],
  3215. 'media_id'=>$d['media_id'],
  3216. 'weixin_media'=>$d['weixin_media'],
  3217. 'user_cate'=>$d['user_cate'],
  3218. 'house_type'=>$d['house_type'],
  3219. 'cate1'=>$d['cate1'],
  3220. 'cate2'=>$d['cate2'],
  3221. ];
  3222. TalkskillModel::insertGetId($insert_data);
  3223. }
  3224. return '复制成功';
  3225. }
  3226. }