1
0

Talkskill.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. <?php
  2. namespace app\api\controller;
  3. use app\event\Msg;
  4. use app\model\UserCollect;
  5. use app\model\Talkskill as TalkskillModel;
  6. use app\model\TalkskillLabel;
  7. use app\model\TalkskillComment;
  8. use app\model\TalkskillHotKeyword;
  9. use app\model\TalkskillPraise;
  10. use app\model\Employee;
  11. use think\facade\Request;
  12. use app\model\CreditsLog;
  13. use app\model\Credits;
  14. use app\model\CreditsSetting;
  15. use app\model\Company;
  16. use think\facade\Db;
  17. use Exception;
  18. use wx\Oplatform;
  19. use think\facade\Cache;
  20. use think\facade\Console;
  21. use app\model\EmployeeMedal;
  22. use app\model\TalkskillUsersCate;
  23. use app\model\TalkskillCates;
  24. use app\model\TalkskillChosen;
  25. use app\model\TalkskillSuccess;
  26. use app\model\TalkskillContent;
  27. use app\model\TalkskillViewLog;
  28. class Talkskill extends Base
  29. {
  30. /*
  31. * 热搜词记录
  32. */
  33. public function hot_keyword()
  34. {
  35. $list = TalkskillHotKeyword::where('root_id', $this->request->token['root_org'])->order('use_count desc')->limit(15)->select();
  36. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
  37. }
  38. /*
  39. * 话术标签列表
  40. */
  41. public function label_list()
  42. {
  43. $token = $this->request->token;
  44. // $token['root_org'] = 23;
  45. $data = TalkskillLabel::where('root_id', $token['root_org'])->select();
  46. $config = CreditsSetting::where('root_id', $token['root_org'])->column('value', 'code');
  47. $config['talkskill_credits'] = isset($config['talkskill_credits']) ? $config['talkskill_credits'] : '1';
  48. $config['talkskill_contribution_value'] = isset($config['talkskill_contribution_value']) ? $config['talkskill_contribution_value'] : '1';
  49. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'config' => $config]);
  50. }
  51. /*
  52. * 匿名提问
  53. */
  54. public function submit_questions()
  55. {
  56. $token = $this->request->token;
  57. // $token['employee_id'] = 1;
  58. // $token['root_org'] = 1;
  59. $questions = Request::only(['questions', 'imgs','media_id'=>'','weixin_media'=>'']);
  60. $data = [
  61. 'title' => trim($questions['questions']),
  62. 'root_id' => $token['root_org'],
  63. 'type' => 'submit',
  64. 'employee_id' => $token['employee_id'],
  65. 'content' => '',
  66. 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
  67. 'media_id'=>$questions['media_id'],
  68. 'weixin_media'=>$questions['weixin_media']
  69. ];
  70. TalkskillModel::create($data);
  71. return json(['code' => 0, 'msg' => '提交成功']);
  72. }
  73. /*
  74. * 上传话术
  75. */
  76. public function submit_talkskill()
  77. {
  78. $token = $this->request->token;
  79. $questions = $param = Request::only(['user_cate','title', 'content', 'label', 'imgs','media_id'=>'','weixin_media'=>'','house_type'=>'','cate1'=>'','cate2'=>'']);
  80. // $token['employee_id'] = 1;
  81. // $token['root_org'] = 1;
  82. $data = [
  83. 'title' => $questions['title'],
  84. 'root_id' => $token['root_org'],
  85. 'type' => 'share',
  86. 'employee_id' => $token['employee_id'],
  87. 'content' => $questions['content'],
  88. 'label' => $questions['label'],
  89. 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
  90. 'approve' => 0,
  91. 'media_id'=>$questions['media_id'],
  92. 'weixin_media'=>$questions['weixin_media'],
  93. 'user_cate' => $questions['user_cate'],
  94. 'house_type' => $questions['house_type'],
  95. 'cate1' => $questions['cate1'],
  96. 'cate2' => $questions['cate2']
  97. ];
  98. TalkskillModel::create($data);
  99. return json(['code' => 0, 'msg' => '提交成功']);
  100. }
  101. /*
  102. * 话术列表
  103. */
  104. public function lists()
  105. {
  106. $param = Request::only(['label', 'user_cate','keyword', 'hot_keyword', 'page', 'is_my', 'status','cate2'=>'','cate1'=>'','house_type'=>'','order'=>'use_count desc', 'messy']);
  107. $token = $this->request->token;
  108. $limit = 10;
  109. $where = [
  110. ['del', '=', 0],
  111. ['root_id', '=', $token['root_org']]
  112. ];
  113. if (isset($param['is_my']) && $param['is_my'] == 1) { //我共享的
  114. $where[] = ['type', '=', 'share'];
  115. $where[] = ['employee_id', '=', $token['employee_id']];
  116. if (isset($param['status']) && $param['status'] !== '') {
  117. $where[] = ['approve', '=', $param['status']];
  118. }
  119. } else {
  120. $where[] = ['approve', '=', 1];
  121. }
  122. $data = TalkskillModel::field('id,title,content,label,use_count,addtime,approve,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2');
  123. //使用人群筛选
  124. if (!empty($param['user_cate'])) {
  125. $data = $data->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  126. }
  127. //标签筛选
  128. if (!empty($param['label'])) {
  129. $data = $data->whereRaw("FIND_IN_SET(" . $param['label'] . " , label)");
  130. }
  131. //一级场景
  132. if (!empty($param['cate1'])) {
  133. $where[] = ['cate1', '=',$param['cate1']];
  134. }
  135. //二级级分类
  136. if (!empty($param['cate2'])) {
  137. $id = TalkskillLabel::where([['root_id','=',$token['root_org']],['label_name','=',$param['cate2']]])->value('id');//迭代之前的数据
  138. $sql = $id ? "(FIND_IN_SET('" . $param['cate2'] . "' , cate2) or label=".$id.')' : "FIND_IN_SET('" . $param['cate2'] . "' , cate2)";
  139. $data = $data->whereRaw($sql);
  140. }
  141. //房屋类型
  142. if (!empty($param['house_type'])) {
  143. $data = $data->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  144. }
  145. //仅展示0回答
  146. if (isset($param['messy']) && $param['messy'] == 1) {
  147. $where[] = ['commentCount', '=', 0];
  148. $where[] = ['content', '=', ''];
  149. }
  150. $data = $data->with(['TalkskillComment' => function ($query) use ($token) {
  151. $query->where('root_id='.$token['root_org'].' and approve=1')->order('use desc,praise desc,id asc')
  152. ->field('taid,uid,comments');
  153. }])->field('id,title,content,label,use_count,addtime,approve,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2');
  154. //关键词筛选
  155. if (!empty(trim($param['keyword']))) {
  156. $hot = TalkskillHotKeyword::where(['root_id' => $token['root_org'], 'keyword' => trim($param['keyword'])])->find();
  157. if (!empty($hot)) {
  158. $hot->use_count = $hot->use_count + 1;
  159. $hot->save();
  160. } else {
  161. TalkskillHotKeyword::create(['root_id' => $token['root_org'], 'keyword' => trim($param['keyword'])]);
  162. }
  163. $where[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  164. }
  165. //热词搜索
  166. if (!empty(trim($param['hot_keyword']))) {
  167. TalkskillHotKeyword::where(['root_id' => $token['root_org'], 'keyword' => trim($param['hot_keyword'])])->inc('use_count')->update();
  168. $where[] = ['title', 'like', '%' . trim($param['hot_keyword']) . '%'];
  169. }
  170. $list = $data->where($where)->page($param['page'], $limit)->order($param['order'])->select();
  171. $count = $data->where($where)->count();
  172. foreach ($list as &$val) {
  173. $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
  174. }
  175. $list = $list->toArray();
  176. $column_talkskill_id = array_column($list, 'id');
  177. $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
  178. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  179. foreach ($list as &$val) {
  180. //话术回答展示顺序,官方回答,员工回答使用最多,点赞最多,先回答
  181. if (!$val['content'] && $val['TalkskillComment']) {
  182. $val['content'] = $val['TalkskillComment'][0]['comments'];
  183. }
  184. $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
  185. $val['status'] = $arr[$val['approve']];
  186. // $val['addtime'] = isset($comment[$val['id']]) ? $comment[$val['id']] : $val['addtime'];
  187. unset($e_where);
  188. $e_where[] = ['taid', '=', $val['id']];
  189. $e_where[] = ['root_id', '=', $token['root_org']];
  190. $comment_count = TalkskillComment::where($e_where)->where(function ($query) use ($token) {
  191. $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
  192. })->count();
  193. $val['comment_count'] = $comment_count;
  194. }
  195. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
  196. }
  197. /*
  198. * 话术详情
  199. */
  200. public function talkskill_xq($id)
  201. {
  202. $data = TalkskillModel::where('id', $id)->field('id,title,content,label,commentCount,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2')->find();
  203. // 浏览量
  204. TalkskillModel::where('id', $id)->inc('view_num')->update();
  205. //是否收藏
  206. $collect = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_id' => $id, 'content_type' => 'talkskill'])->count();
  207. $data['collect'] = $collect > 0 ? 1 : 0;
  208. // $imgs = $data->imgs ? explode(',',$data->imgs) : [];
  209. // $ali_oss_bindurl = config('app.ali_oss_bindurl');
  210. // foreach ($imgs as $k => &$v) {
  211. // $v = $ali_oss_bindurl.'/'.$v;
  212. // }
  213. // $data->imgs = $imgs;
  214. $this->addTalkskillViewLog('talkskill');
  215. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  216. }
  217. /*
  218. * 增加话术浏览记录
  219. */
  220. private function addTalkskillViewLog($type)
  221. {
  222. $token = $this->request->token;
  223. TalkskillViewLog::create([
  224. 'root_id' => $token['root_org'],
  225. 'org_id' => $token['org_id'],
  226. 'employee_id' => $token['employee_id'],
  227. 'type' => $type
  228. ]);
  229. return true;
  230. }
  231. /*
  232. * 复制话术
  233. * type->use_comment:复制用户的,use_admin:复制后台的
  234. */
  235. public function use_talkskill()
  236. {
  237. $param = Request::only(['type', 'id']);
  238. $token = $this->request->token;
  239. // $token['root_org'] = 1;
  240. // $token['employee_id'] = 58;
  241. Db::startTrans();
  242. try {
  243. $end = true;
  244. $type = $param['type'] == 'use_comment' ? 1 : 0;
  245. $where = [];
  246. $where[] = ['type', '=', $type];
  247. $where[] = ['talkskill_id', '=', $param['id']];
  248. $where[] = ['root_id', '=', $token['root_org']];
  249. $where[] = ['employee_id', '=', $token['employee_id']];
  250. $credits_log = CreditsLog::where($where)->select()->toArray();
  251. if (!$credits_log) {
  252. $save['talkskill_id'] = $param['id'];
  253. $save['root_id'] = $token['root_org'];
  254. $save['employee_id'] = $token['employee_id'];
  255. $save['type'] = $type;
  256. $save['addtime'] = date('Y-m-d H:i:s', time());
  257. CreditsLog::insert($save);
  258. //增加使用人次
  259. if ($param['type'] == 'use_comment') {
  260. TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->inc('use')->update();
  261. } else {
  262. TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->inc('use_count')->update();
  263. }
  264. } else {
  265. $end = false;
  266. // Db::commit();
  267. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  268. }
  269. if ($end) {
  270. //复制自己上传的话术或者后台添加的话术或者官方回答或非审核通过,不得积分
  271. if ($param['type'] != 'use_comment') {
  272. $info = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
  273. if ($info->isEmpty() || $info['employee_id'] == 0 || $info['type'] == 'admin' || $info['type'] == 'submit' || $info['employee_id'] == $token['employee_id'] || $info['approve'] != 1) {
  274. $end = false;
  275. // Db::commit();
  276. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  277. }
  278. } else { //复制自己添加的评论不得积分复制没有审核通过的评论不得积分
  279. $info = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
  280. $eid = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->findOrEmpty();
  281. if ($info->isEmpty() || $eid->isEmpty() || $info['uid'] == $eid['uid'] || $info['approve'] != 1) {
  282. $end = false;
  283. // Db::commit();
  284. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  285. }
  286. $info['employee_id'] = Employee::where([['uid', '=', $info['uid']], ['root_id', '=', $token['root_org']]])->value('id');
  287. }
  288. }
  289. if ($end) {
  290. //未计算积分的被使用次数
  291. $where[] = ['status', '=', 0];
  292. $credits_log = CreditsLog::where($where)->select()->toArray();
  293. $count = count($credits_log);
  294. if ($count == 0) {
  295. $end = false;
  296. // Db::commit();
  297. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  298. }
  299. }
  300. if ($end) {
  301. //查询规则
  302. if ($param['type'] == 'use_comment') {
  303. $rule = CreditsSetting::where([['root_id', '=', $token['root_org']], ['code', '=', 'talkskill_person2']])->value('value');
  304. $rule = $rule ? json_decode($rule, true) : ['person2' => 1, 'credits2' => 1];
  305. $rule = ['person' => $rule['person2'], 'credits' => $rule['credits2']];
  306. } else {
  307. $rule = CreditsSetting::where([['root_id', '=', $token['root_org']], ['code', '=', 'talkskill_person1']])->value('value');
  308. $rule = $rule ? json_decode($rule, true) : ['person1' => 1, 'credits1' => 1];
  309. $rule = ['person' => $rule['person1'], 'credits' => $rule['credits1']];
  310. }
  311. //计算积分
  312. if ($count >= $rule['person']) {
  313. $credits = new Credits();
  314. $save = [];
  315. $credits->employee_id = $info['employee_id'];
  316. $credits->root_id = $token['root_org'];
  317. $credits->credits = $rule['credits'];
  318. $credits->type = 0;
  319. $credits->json = $param['id'];
  320. $credits->remark = ($param['type'] == 'use_comment') ? '话术评论被使用' : '话术被使用';
  321. $credits->save();
  322. $id = $credits->id;
  323. $c_where[] = ['employee_id', '=', $info['employee_id']];
  324. $c_where[] = ['root_id', '=', $token['root_org']];
  325. $c_where[] = ['type', '=', 0];
  326. $sum = Credits::where($c_where)->sum('credits');
  327. Credits::where([['id', '=', $id]])->update(['sum' => $sum]);
  328. CreditsLog::where([['id', 'in', array_column($credits_log, 'id')]])->update(['status' => 1]);
  329. }
  330. }
  331. Db::commit();
  332. } catch (Exception $e) {
  333. // 回滚事务
  334. Db::rollback();
  335. return json(['code' => 1, 'msg' => $e->getMessage(), 'data' => $this->get_credits($param)]);
  336. }
  337. return json(['code' => 0, 'msg' => '复制成功', 'data' => $this->get_credits($param)]);
  338. }
  339. public function get_credits($param)
  340. {
  341. $token = $this->request->token;
  342. if ($param['type'] == 'use_comment') {
  343. $count = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->value('use');
  344. } else {
  345. $count = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->value('use_count');
  346. }
  347. return $count;
  348. }
  349. /*
  350. * 话术回答列表
  351. */
  352. public function commentList()
  353. {
  354. $param = Request::only(['taid', 'page']);
  355. $token = $this->request->token;
  356. $where[] = ['taid', '=', $param['taid']];
  357. $where[] = ['root_id', '=', $token['root_org']];
  358. $data = TalkskillComment::with(['user'])->where(function ($query) use ($token) {
  359. $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
  360. })->where($where)->page($param['page'], 10)->order(['approve' => 'asc', 'use' => 'desc', 'praise' => 'desc'])->select()->toArray();
  361. $column_talkskillcomment_id = array_column($data, 'id');
  362. $column_talkskillcomment_uid = array_column($data, 'uid');
  363. $praise = TalkskillPraise::where([['root_id', '=', $token['root_org']], ['content_id', 'in', $column_talkskillcomment_id], ['user_id', '=', $token['uid']]])->column('user_id', 'content_id');
  364. $employee = Employee::where([['uid', 'in', $column_talkskillcomment_uid], ['root_id', '=', $token['root_org']]])->column('name', 'uid');
  365. $state = Employee::where([['uid', 'in', $column_talkskillcomment_uid], ['root_id', '=', $token['root_org']]])->column('state', 'uid');
  366. foreach ($data as &$val) {
  367. $val['ispraise'] = isset($praise[$val['id']]) ? 1 : 0;
  368. $val['name'] = isset($employee[$val['uid']]) ? $employee[$val['uid']] : '暂无';
  369. $val['state'] = isset($state[$val['uid']]) ? $state[$val['uid']] : '离职';
  370. if (strpos($val['state'], "离职") !== false) {
  371. $val['name'] = '匿名用户';
  372. $val['headimgurl'] = '';
  373. }
  374. }
  375. $count = TalkskillComment::where($where)->where(function ($query) use ($token) {
  376. $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
  377. })->count();
  378. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  379. }
  380. /*
  381. * 话术评论
  382. */
  383. public function addComment()
  384. {
  385. $param = Request::only(['content', 'taid']);
  386. $data = [
  387. 'uid' => $this->request->token['uid'],
  388. 'taid' => $param['taid'],
  389. 'root_id' => $this->request->token['root_org'],
  390. 'comments' => $param['content']
  391. ];
  392. TalkskillComment::insert($data);
  393. return json(['code' => 0, 'msg' => '回答成功']);
  394. }
  395. /**
  396. * 收藏
  397. */
  398. public function collect($id)
  399. {
  400. $rs = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'talkskill', 'content_id' => $id])->count();
  401. if ($rs > 0) return json(['code' => 1, 'msg' => '您已收藏']);
  402. UserCollect::create([
  403. 'user_id' => $this->request->token['uid'],
  404. 'content_type' => 'talkskill',
  405. 'content_id' => $id
  406. ]);
  407. return json(['code' => 0, 'msg' => '收藏成功']);
  408. }
  409. /**
  410. * 取消收藏
  411. */
  412. public function collectCancel($id)
  413. {
  414. $rs = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'talkskill', 'content_id' => $id])->find();
  415. if (!$rs) return json(['code' => 1, 'msg' => '取消收藏失败']);
  416. $rs->delete();
  417. return json(['code' => 0, 'msg' => '取消收藏']);
  418. }
  419. /**
  420. * 点赞
  421. */
  422. public function praise($id)
  423. {
  424. $rs = TalkskillPraise::where(['user_id' => $this->request->token['uid'], 'root_id' => $this->request->token['root_org'], 'content_id' => $id])->count();
  425. if ($rs > 0) return json(['code' => 1, 'msg' => '您已点赞']);
  426. TalkskillPraise::create([
  427. 'user_id' => $this->request->token['uid'],
  428. 'root_id' => $this->request->token['root_org'],
  429. 'content_id' => $id
  430. ]);
  431. TalkskillComment::where('id', $id)->inc('praise')->update();
  432. return json(['code' => 0, 'msg' => '点赞成功']);
  433. }
  434. /**
  435. * 取消点赞
  436. */
  437. public function praiseCancel($id)
  438. {
  439. $rs = TalkskillPraise::where(['user_id' => $this->request->token['uid'], 'root_id' => $this->request->token['root_org'], 'content_id' => $id])->find();
  440. if (!$rs) return json(['code' => 1, 'msg' => '取消点赞失败']);
  441. $rs->delete();
  442. $praise = TalkskillComment::where('id', $id)->value('praise');
  443. if ($praise > 0) {
  444. TalkskillComment::where('id', $id)->dec('praise')->update();
  445. }
  446. return json(['code' => 0, 'msg' => '取消成功']);
  447. }
  448. /**
  449. * 话术场景列表
  450. */
  451. public function get_talkskill_label()
  452. {
  453. $param = Request::only(['page' => 1, 'limit' => 10, 'keyword','user_cate']);
  454. $token = $this->request->token;
  455. $count = 0;
  456. $data = TalkskillLabel::where('root_id', $token['root_org'])->order('id desc')->select()->toArray(); //所有分类
  457. if ($data) {
  458. $ids = array_column($data, 'id');
  459. $where1 = [];
  460. foreach ($ids as $v) {
  461. $where1[] = "FIND_IN_SET(" . $v . " , label)";
  462. }
  463. $where2[] = ['root_id', '=', $token['root_org']];
  464. $where2[] = ['del', '=', 0];
  465. $where2[] = ['approve', '=', 1];
  466. if (isset($param['keyword']) && $param['keyword']) {
  467. $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  468. }
  469. if (!empty($param['user_cate'])) {
  470. $where2[] = ['user_cate','=',$param['user_cate']];
  471. }
  472. $label_count = TalkskillModel::where($where2)->whereRaw(implode(' or ', $where1))->field('addtime,label label_id,id,use_count')->select()->toArray();
  473. $label_counts = $dates = $person_count = [];
  474. foreach ($label_count as $v) {
  475. $label_ids = explode(',', $v['label_id']);
  476. foreach ($label_ids as $v2) {
  477. //话术数量
  478. $label_counts[$v2] = isset($label_counts[$v2]) ? $label_counts[$v2] += 1 : 1;
  479. //最近话术上传时间
  480. $dates[$v2] = isset($dates[$v2]) ? ($dates[$v2] > $v['addtime'] ? $dates[$v2] : $v['addtime']) : $v['addtime'];
  481. //使用人次
  482. $person_count[$v2] = isset($person_count[$v2]) ? $person_count[$v2] + $v['use_count'] : $v['use_count'];
  483. }
  484. }
  485. foreach ($data as $k => $v) {
  486. $data[$k]['label_count'] = isset($label_counts[$v['id']]) ? $label_counts[$v['id']] : 0;
  487. $data[$k]['update_time'] = isset($dates[$v['id']]) ? $dates[$v['id']] : '';
  488. $data[$k]['person_count'] = isset($person_count[$v['id']]) ? $person_count[$v['id']] : 0;
  489. //来源
  490. if($v['from_type'] == 0)
  491. {
  492. $data[$k]['from'] = '店面发布';
  493. }elseif($v['from_type'] == 1){
  494. $data[$k]['from'] = '集团指派';
  495. }else{
  496. $company = Company::where('root_id',$v['from_root_id'])->value('company_name');
  497. $data[$k]['from'] = $company.'共享';
  498. }
  499. //清除场景下面话术为0的数据
  500. if ($data[$k]['label_count'] == 0) {
  501. unset($data[$k]);
  502. }
  503. }
  504. $count = isset($data) ? count($data) : 0;
  505. $sort = array_column($data , 'id');
  506. array_multisort($sort , SORT_DESC , $data);
  507. $page = ($param['page'] - 1) * $param['limit'];
  508. $data = array_slice($data , $page , $param['limit']);
  509. }
  510. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  511. }
  512. /**
  513. * 白问百搭列表/我的提问列表
  514. */
  515. public function get_answer_list()
  516. {
  517. $param = Request::only(['page' => 1, 'limit' => 10, 'keyword', 'messy', 'is_my']);
  518. $token = $this->request->token;
  519. // $token['root_org'] = 177;
  520. $where2[] = ['root_id', '=', $token['root_org']];
  521. $where2[] = ['del', '=', 0];
  522. $where2[] = ['type', '=', 'submit']; //admin后台增加话术,share小程序上传话术,submit小程序提交问题
  523. if (isset($param['is_my']) && $param['is_my'] == 1) {
  524. $where2[] = ['employee_id', '=', $token['employee_id']];
  525. }
  526. if (isset($param['keyword']) && $param['keyword']) {
  527. $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  528. }
  529. $data = TalkskillModel::withCount(['TalkskillComment' => function ($query) {
  530. $query->where('approve', 1); //回答审核通过
  531. }])->where($where2)->where('(content is null OR content<=0)')->field('addtime,label label_id,id,use_count,media_id,weixin_media')->select()->toArray();
  532. foreach ($data as $k => $v) {
  533. // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
  534. if (isset($param['messy']) && $param['messy'] == 1 && $v['talkskill_comment_count'] > 0) {
  535. unset($data[$k]);
  536. }
  537. }
  538. $count = count($data);
  539. //排序
  540. $last_names = array_column($data, 'addtime');
  541. array_multisort($last_names, SORT_DESC, $data);
  542. $page = ($param['page'] - 1) * $param['limit'];
  543. $data = array_slice($data, $page, $param['limit']);
  544. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  545. }
  546. /**
  547. * 我的回答列表
  548. */
  549. public function get_my_answer_list()
  550. {
  551. $param = Request::only(['page' => 1, 'limit' => 10]);
  552. $token = $this->request->token;
  553. // $token['root_org'] = 1;
  554. // $token['uid'] = 33;
  555. $where[] = ['uid', '=', $token['uid']];
  556. $where[] = ['root_id', '=', $token['root_org']];
  557. $data = TalkskillComment::with('talkskill')->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
  558. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  559. foreach ($data as $k => $v) {
  560. // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
  561. $data[$k]['status'] = $arr[$v['approve']];
  562. }
  563. $count = TalkskillComment::with('talkskill')->where($where)->count();
  564. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  565. }
  566. /**
  567. * 编辑话术
  568. */
  569. public function edit_talkskill()
  570. {
  571. $questions = $param = Request::only(['title', 'content', 'label', 'imgs', 'id','house_type'=>'','cate1'=>'','cate2'=>'','user_cate'=>'']);
  572. $token = $this->request->token;
  573. $info = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
  574. if ($info->isEmpty() || $info['approve'] == 1) return json(['code' => 1, 'msg' => '已审核无法修改']);
  575. $data = [
  576. 'title' => $questions['title'],
  577. 'content' => $questions['content'],
  578. 'label' => isset($questions['label']) && $questions['label'] ? $questions['label'] : '',
  579. 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
  580. 'approve' => 0,
  581. 'house_type' => $questions['house_type'],
  582. 'cate1' => $questions['cate1'],
  583. 'cate2' => $questions['cate2'],
  584. 'user_cate' => $questions['user_cate']
  585. ];
  586. TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->update($data);
  587. return json(['code' => 0, 'msg' => '修改成功', 'data' => '修改成功']);
  588. }
  589. /**
  590. * 编辑评论
  591. */
  592. public function edit_talkskill_comment()
  593. {
  594. $questions = $param = Request::only(['comments', 'id']);
  595. $token = $this->request->token;
  596. // $token['employee_id'] = 1;
  597. // $token['root_org'] = 1;
  598. $info = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
  599. if ($info->isEmpty() || $info['approve'] == 1) return json(['code' => 1, 'msg' => '已审核无法修改']);
  600. // $token['employee_id'] = 1;
  601. // $token['root_org'] = 1;
  602. $data = [
  603. 'comments' => $questions['comments'],
  604. 'approve' => 0
  605. ];
  606. TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->update($data);
  607. return json(['code' => 0, 'msg' => '修改成功', 'data' => '修改成功']);
  608. }
  609. /*
  610. * 贡献值积分详情
  611. */
  612. public function credits_info()
  613. {
  614. $param = Request::only(['page', 'limit', 'type']);
  615. $param['type'] = !isset($param['type']) || !$param['type'] ? 0 : 1;
  616. $token = $this->request->token;
  617. // $token['employee_id'] = 25;
  618. // $token['root_org'] = 23;
  619. $where[] = ['root_id', '=', $token['root_org']];
  620. $where[] = ['employee_id', '=', $token['employee_id']];
  621. $start_time = date('Y-m') . '-01 00:00:00';
  622. $end_time = date('Y-m-d H:i:s', strtotime('+1 months', strtotime($start_time)));
  623. //当前贡献值,当前积分
  624. $val = Credits::where($where)->where('type', $param['type'])
  625. // ->where([['addtime', 'between', [$start_time, $end_time]]])
  626. ->sum('credits');
  627. $res['my_value'] = $val;
  628. //贡献值排名
  629. $ranking = Credits::where([['root_id', '=', $token['root_org']], ['type', '=', $param['type']]])
  630. // ->where([['addtime', 'between', [$start_time, $end_time]]])
  631. ->group('employee_id')->order('sum desc,id desc')->column('sum(credits) sum', 'employee_id');
  632. if ($ranking) {
  633. $found_arr = array_keys($ranking);
  634. $key = array_search($token['employee_id'], $found_arr);
  635. $res['my_ranking'] = $key === false ? 0 : $key + 1;
  636. } else {
  637. $res['my_ranking'] = 0;
  638. }
  639. //详情0积分,1贡献值
  640. $credits = Credits::where($where)->where('type', $param['type'])->order('id desc')->page($param['page'], $param['limit'])->select()->toArray();
  641. foreach ($credits as $k => $v) {
  642. $val = [];
  643. if(!strstr($v['remark'],'上传案例')){
  644. if ($v['type'] == 0) {
  645. $credits[$k]['remark'] = '您的话术被使用积分+' . $v['credits'] . ',当前积分总数:' . $v['sum'];
  646. } elseif ($v['type'] == 1) {
  647. $credits[$k]['remark'] = '话术审批已通过,贡献值+' . $v['credits'] . ',当前贡献值总数:' . $v['sum'];
  648. }
  649. }else{
  650. if ($v['type'] == 0) {
  651. $credits[$k]['remark'] = $v['remark'].'积分+' . $v['credits'] . ',当前积分总数:' . $v['sum'];
  652. } elseif ($v['type'] == 1) {
  653. $credits[$k]['remark'] = $v['remark'].'贡献值+' . $v['credits'] . ',当前贡献值总数:' . $v['sum'];
  654. }
  655. }
  656. }
  657. $res['info'] = $credits;
  658. $res['info_count'] = Credits::where($where)->where('type', $param['type'])->count();
  659. return json(['code' => 0, 'msg' => '获取成功', 'data' => $res]);
  660. }
  661. /**
  662. * 数字化产出贡献
  663. * @return \think\response\Json
  664. */
  665. public function contribution(){
  666. $token = $this->request->token;
  667. $root_id = $token['root_org'];
  668. $employee_id = $token['employee_id'];
  669. $where[] = ['root_id', '=', $root_id];
  670. $where[] = ['employee_id', '=', $employee_id];
  671. //当前积分
  672. $val = Credits::where($where)->where('type', '=', 0)->sum('credits');
  673. $res['integral'] = $val;
  674. // 当前贡献值
  675. $val = Credits::where($where)->where('type', '=', 1)->sum('credits');
  676. $res['contribution'] = $val;
  677. //勋章数量
  678. $res['medal'] = EmployeeMedal::where($where)->count();
  679. //贡献值排名
  680. $ranking = Credits::where([['root_id', '=', $root_id], ['type', '=', 1]])->group('employee_id')->order('sum desc')->column('sum(credits) as sum');
  681. $credits_where[] = ['root_id', '=', $root_id];
  682. $credits_where[] = ['employee_id', '=', $employee_id];
  683. $credits_where[] = ['type', '=', 1];
  684. $credits_all = Credits::where($credits_where)->sum('credits');
  685. $res['credits_ranking'] = 0;
  686. if ($ranking && $credits_all) {
  687. $key = array_search($credits_all, $ranking);
  688. if ($key === false) {
  689. $res['credits_ranking'] = count($ranking) + 1;
  690. } else {
  691. $res['credits_ranking'] = $key + 1;
  692. }
  693. } else {
  694. $res['credits_ranking'] = count($ranking) + 1;
  695. }
  696. $res['help_people'] = TalkskillModel::where([['root_id', '=', $root_id], ['employee_id', '=', $employee_id]])->sum('use_count');
  697. return json(['code' => 0, 'msg' => '获取成功', 'data' => $res]);
  698. }
  699. /*
  700. * 话术审核记录列表
  701. */
  702. public function approve_talkskill()
  703. {
  704. $param = Request::only(['page' => 1, 'limit' => 10, 'status' => 0]);
  705. $token = $this->request->token;
  706. // $token['employee_id'] = 25;
  707. // $token['root_org'] = 1;
  708. $limit = 10;
  709. $where = [
  710. ['del', '=', 0],
  711. ['root_id', '=', $token['root_org']],
  712. ['aprove_employee_id', '=', $token['employee_id']],
  713. ['approve', '>', 0], //审批,0待审核,1审核通过,2不通过
  714. ];
  715. if ($param['status']) {
  716. $where[] = ['approve', '=', $param['status']];
  717. }
  718. $data = TalkskillModel::field('id,title,content,label,use_count,addtime,approve,imgs');
  719. $data = $data->with(['TalkskillComment' => function ($query) use ($token) {
  720. $query->where([['root_id', '=', $token['root_org']]])->field('taid,uid');
  721. }])->field('id,title,content,label,use_count,addtime,approve,imgs');
  722. $list = $data->where($where)->page($param['page'], $limit)->order('aprove_time desc')->select();
  723. $count = $data->where($where)->count();
  724. foreach ($list as &$val) {
  725. $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
  726. }
  727. $list = $list->toArray();
  728. $column_talkskill_id = array_column($list, 'id');
  729. $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
  730. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  731. foreach ($list as &$val) {
  732. $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
  733. $val['status'] = $arr[$val['approve']];
  734. $val['addtime'] = isset($comment[$val['id']]) ? $comment[$val['id']] : $val['addtime'];
  735. }
  736. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
  737. }
  738. /**
  739. * 回答审核记录列表
  740. */
  741. public function approve_talkskill_comment()
  742. {
  743. $param = Request::only(['page' => 1, 'limit' => 10, 'status' => 0]);
  744. $token = $this->request->token;
  745. // $token['root_org'] = 1;
  746. // $token['employee_id'] = 25;
  747. if ($param['status']) {
  748. $where[] = ['approve', '=', $param['status']];
  749. }
  750. $where[] = ['root_id', '=', $token['root_org']];
  751. $where[] = ['aprove_employee_id', '=', $token['employee_id']];
  752. $data = TalkskillComment::with('talkskill')->where($where)->page($param['page'], $param['limit'])->order('aprove_time desc')->select()->toArray();
  753. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  754. foreach ($data as $k => $v) {
  755. // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
  756. $data[$k]['status'] = $arr[$v['approve']];
  757. }
  758. $count = TalkskillComment::with('talkskill')->where($where)->count();
  759. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  760. }
  761. /*
  762. * 业务员上传的话术审核列表
  763. * 只能审核直接下级
  764. */
  765. public function approve_talkskill_list()
  766. {
  767. $param = Request::only(['page' => 1, 'limit' => 10]);
  768. $token = $this->request->token;
  769. // $token['root_org'] = 1;
  770. // $token['org_id'] = 22;
  771. // $token['employee_id'] = 37;
  772. $where = [
  773. ['root_id', '=', $token['root_org']],
  774. ['del', '=', 0],
  775. ['approve', '=', 0],
  776. ['type', '=', 'share'],
  777. ['employee_id', '>', 0]
  778. ];
  779. $employee = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->value('is_manager');
  780. if ($employee != 1) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
  781. $f = $employee == 1 ? '(org.pid=' . $token['org_id'] . ' or (org.id=' . $token['org_id'] . ' and employee.is_manager=0))' : [['org.pid', '=', $token['org_id']]];
  782. // echo $f;
  783. $eids = Employee::withJoin('org', 'inner')
  784. ->where([['employee.id', '<>', $token['employee_id']]])
  785. ->where($f)->column('employee.id');
  786. // var_dump($eids);die;
  787. if (!$eids) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
  788. $where[] = ['employee_id', 'in', $eids];
  789. $data = TalkskillModel::field('id,title,content,label,use_count,addtime,approve,imgs');
  790. $data = $data->with(['TalkskillComment' => function ($query) use ($token) {
  791. $query->where([['root_id', '=', $token['root_org']]])->field('taid,uid');
  792. }])->field('id,title,content,label,use_count,addtime,approve,imgs');
  793. $list = $data->where($where)->page($param['page'], $param['limit'])->order('aprove_time desc')->select();
  794. $count = $data->where($where)->count();
  795. foreach ($list as &$val) {
  796. $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
  797. }
  798. $list = $list->toArray();
  799. $column_talkskill_id = array_column($list, 'id');
  800. $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
  801. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  802. foreach ($list as &$val) {
  803. $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
  804. $val['status'] = $arr[$val['approve']];
  805. $val['addtime'] = isset($comment[$val['id']]) ? $comment[$val['id']] : $val['addtime'];
  806. }
  807. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  808. }
  809. /**
  810. * 回答审核列表
  811. */
  812. public function approve_talkskill_comment_list()
  813. {
  814. $param = Request::only(['page' => 1, 'limit' => 10]);
  815. $token = $this->request->token;
  816. // $token['root_org'] = 23;
  817. // $token['org_id'] = 43;
  818. // $token['employee_id'] = 58;
  819. $employee = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->value('is_manager');
  820. if ($employee != 1) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
  821. $f = '(org.pid=' . $token['org_id'] . ' or (org.id=' . $token['org_id'] . ' and employee.is_manager=0))';
  822. $eids = Employee::withJoin(['org'], 'inner')
  823. ->where([['employee.id', '<>', $token['employee_id']]])
  824. ->where($f)->column('employee.uid');
  825. // var_dump($eids);die;
  826. if (!$eids) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
  827. $where[] = ['root_id', '=', $token['root_org']];
  828. $where[] = ['uid', 'in', $eids];
  829. $where[] = ['approve', '=', 0];
  830. $data = TalkskillComment::with('talkskill')->where($where)->page($param['page'], $param['limit'])->order('aprove_time desc')->select()->toArray();
  831. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  832. foreach ($data as $k => $v) {
  833. // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
  834. $data[$k]['status'] = $arr[$v['approve']];
  835. }
  836. $count = TalkskillComment::with('talkskill')->where($where)->count();
  837. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  838. }
  839. /*
  840. * 批量审核话术
  841. */
  842. public function talkskill_approve()
  843. {
  844. $ids = explode(',', input('id'));
  845. $approve = input('status', 2);
  846. $where = [
  847. ['root_id', '=', $this->request->token['root_org']],
  848. ['id', 'in', $ids],
  849. ['approve', '=', 0]
  850. ];
  851. // 数据获取
  852. $list = TalkskillModel::where($where)->select();
  853. // 数据初始化
  854. $employeeCreditSum = [];
  855. $insertData = [];
  856. $updateTalkskillDataList = [];
  857. // 单次贡献值增加数值
  858. $contribution = 0;
  859. if ($approve == 1) {
  860. $contribution = CreditsSetting::where(['code' => 'talkskill_credits', 'root_id' => $this->request->token['root_org']])->value('value');
  861. !empty($contribution) ?: $contribution = 1;
  862. }
  863. $i = 0;
  864. // 数据整合
  865. foreach ($list as $v) {
  866. if (!isset($employeeCreditSum[$v['employee_id']]))
  867. $employeeCreditSum[$v['employee_id']] = Credits::where(['root_id' => $this->request->token['root_org'], 'employee_id' => $v['employee_id'], 'type' => 1])->sum('credits');
  868. if ($approve == 1) {
  869. $employeeCreditSum[$v['employee_id']] += $contribution;
  870. /*$insertData[] = [
  871. 'employee_id' => $v['employee_id'],
  872. 'root_id' => $this->request->token['root_org'],
  873. 'credits' => $contribution,
  874. 'sum' => $employeeCreditSum[$v['employee_id']],
  875. 'type' => 1,
  876. 'json' => $v['id'],
  877. 'remark' => '共享话术审核通过'
  878. ];*/
  879. Credits::create([
  880. 'employee_id' => $v['employee_id'],
  881. 'root_id' => $this->request->token['root_org'],
  882. 'credits' => $contribution,
  883. 'sum' => $employeeCreditSum[$v['employee_id']],
  884. 'type' => 1,
  885. 'json' => $v['id'],
  886. 'remark' => '共享话术审核通过'
  887. ]);
  888. }
  889. /*$updateTalkskillDataList[] = [
  890. 'id' => $v['id'],
  891. 'approve' => $approve,
  892. 'aprove_employee_id' => $this->request->token['employee_id'],
  893. 'aprove_time' => date('Y-m-d H:i:s')
  894. ];*/
  895. TalkskillModel::where('id',$v['id'])->update([
  896. 'approve' => $approve,
  897. 'aprove_employee_id' => $this->request->token['employee_id'],
  898. 'aprove_time' => date('Y-m-d H:i:s')
  899. ]);
  900. $s = $approve == 1 ? '已' : '未';
  901. if($approve == 1) $i+=1;
  902. event(new Msg($v['employee_id'], '您提交的共享话术“' . $v['title'] . '”' . $s . '审核通过', 'talkskillApprove'));
  903. }
  904. if($i) dataStatistics($this->request->token['root_org'],'talkskill_count',$i,'inc');//manage应用首页统计数据
  905. // 数据保存
  906. (new TalkskillModel())->saveAll($updateTalkskillDataList);
  907. (new Credits())->saveAll($insertData);
  908. return json(['code'=> 0, 'msg'=> '操作成功']);
  909. }
  910. /**
  911. * 批量审核回答
  912. */
  913. public function all_approve_comment()
  914. {
  915. $ids = explode(',', input('id'));
  916. $approve = input('status', 2);
  917. $where = [
  918. ['root_id', '=', $this->request->token['root_org']],
  919. ['id', 'in', $ids],
  920. ['approve', '=', 0]
  921. ];
  922. // 数据获取
  923. $list = TalkskillComment::where($where)->select();
  924. // 数据初始化
  925. $employeeCreditSum = [];
  926. $insertData = [];
  927. $updateTalkskillCommentDataList = [];
  928. $idList = [];
  929. // 单次贡献值增加数值
  930. $contribution = 0;
  931. if ($approve == 1) {
  932. $contribution = CreditsSetting::where(['code' => 'talkskill_contribution_value', 'root_id' => $this->request->token['root_org']])->value('value');
  933. !empty($contribution) ?: $contribution = 1;
  934. }
  935. // 数据整合
  936. foreach ($list as $v) {
  937. if (!isset($employeeCreditSum[$v['uid']])) {
  938. $employeeCreditSum[$v['uid']]['employee_id'] = Employee::where(['root_id' => $this->request->token['root_org'], 'uid' => $v['uid'], 'state' => '在职'])->value('id');
  939. $employeeCreditSum[$v['uid']]['creditsSum'] = Credits::where(['root_id' => $this->request->token['root_org'], 'employee_id' => $v['employee_id'], 'type' => 1])->sum('credits');
  940. }
  941. if ($approve == 1) {
  942. $employeeCreditSum[$v['uid']]['creditsSum'] += $contribution;
  943. $insertData[] = [
  944. 'employee_id' => $employeeCreditSum[$v['uid']]['employee_id'],
  945. 'root_id' => $this->request->token['root_org'],
  946. 'credits' => $contribution,
  947. 'sum' => $employeeCreditSum[$v['uid']]['creditsSum'],
  948. 'type' => 1,
  949. 'json' => $v['id'],
  950. 'remark' => '话术评论审核通过增加贡献值'
  951. ];
  952. }
  953. $updateTalkskillCommentDataList[] = [
  954. 'id' => $v['id'],
  955. 'approve' => $approve,
  956. 'aprove_employee_id' => $this->request->token['employee_id'],
  957. 'aprove_time' => date('Y-m-d H:i:s')
  958. ];
  959. $idList[] = $v['id'];
  960. $s = $approve == 1 ? '已' : '未';
  961. event(new Msg($employeeCreditSum[$v['uid']]['employee_id'], '您提交的话术评论“' . $v['comments'] . '”' . $s . '审核通过', 'talkskillCemApprove'));
  962. }
  963. // 数据保存
  964. (new TalkskillComment())->saveAll($updateTalkskillCommentDataList);
  965. (new Credits())->saveAll($insertData);
  966. TalkskillModel::where('id', 'in', $idList)->inc('commentCount')->update();
  967. return json(["code" => 0, 'msg' => '操作成功', 'data' => []]);
  968. }
  969. /** ------------------------ 企业微信智能话术 end ----------------------- */
  970. //微信1-JS-SDK使用权限签名算法
  971. public function ticket($url)
  972. {
  973. $appid = config('app.official_appid');
  974. $secret = config('app.official_secret');
  975. $accesstoken = Cache::get('zqxg_offi_access_token');
  976. if (!$accesstoken) {
  977. $accesstoken = (new Oplatform())->AccessToken($appid,$secret);
  978. $accesstoken = $accesstoken['access_token'];
  979. Cache::set('zqxg_offi_access_token',$accesstoken,7100);
  980. }
  981. $ticket = (new Oplatform())->getTicket($accesstoken);
  982. //随机字符
  983. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
  984. $randStr = str_shuffle($str);//打乱字符串
  985. $rands= substr($randStr,0,10);//substr(string,start,length);返回字符串的一部分
  986. $data['appid'] = $appid;
  987. $data['timestamp'] = time();
  988. $data['noncestr'] = $rands;
  989. $signParam['jsapi_ticket'] = $ticket['ticket'];
  990. $signParam['noncestr'] = $rands;
  991. $signParam['url'] = $url;
  992. $signParam['timestamp'] = $data['timestamp'];
  993. ksort($signParam);
  994. $scept = toUrlParams($signParam);
  995. $scept = sha1($scept);
  996. $data['sign'] = $scept;
  997. return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
  998. }
  999. public function system(){
  1000. $type = input('type','');
  1001. // 来自企业微信的请求
  1002. $from_wework = input('from_wework', '', 'trim');
  1003. if ($from_wework) {
  1004. $output = Console::call('download',[$type, 'wework', $this->request->token['root_org']]);
  1005. } else {
  1006. $output = Console::call('download',[$type]);
  1007. }
  1008. return $output->fetch();
  1009. }
  1010. /*
  1011. * 使用人群列表
  1012. */
  1013. public function get_person_list()
  1014. {
  1015. $token = $this->request->token;
  1016. $param = Request::only(['page' => 1, 'limit' => 10]);
  1017. $where = [
  1018. 'root_id' => $token['root_org']
  1019. ];
  1020. $list = TalkskillUsersCate::where($where)->order('id asc')->select();
  1021. return json(['code' => 0, 'data' => $list]);
  1022. }
  1023. /*
  1024. * 迭代,获取一级场景列表
  1025. */
  1026. public function get_cate_list()
  1027. {
  1028. $param = Request::only(['page' => 1, 'limit' => 10, 'keyword','cate1']);
  1029. $token = $this->request->token;
  1030. $count = 0;
  1031. //一级分类
  1032. $cw[] = ['root_id','=',$token['root_org']];
  1033. $cw[] = ['pid','=',0];
  1034. $cw[] = ['type','=','cate'];
  1035. $data = TalkskillCates::where($cw)->order('id asc')->select()->toArray(); //所有分类
  1036. if ($data) {
  1037. $where2[] = ['cate1','in',array_column($data, 'name')];
  1038. $where2[] = ['root_id', '=', $token['root_org']];
  1039. $where2[] = ['del', '=', 0];
  1040. $where2[] = ['approve', '=', 1];
  1041. if (isset($param['keyword']) && $param['keyword']) {
  1042. $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  1043. }
  1044. if (!empty($param['cate1'])) {
  1045. $where2[] = ['cate1','=',$param['cate1']];
  1046. }
  1047. $label_count = TalkskillModel::where($where2)->field('addtime,label label_id,id,use_count,cate1')->order('id asc')->select()->toArray();
  1048. $label_counts = $dates = $person_count = [];
  1049. foreach ($label_count as $v) {
  1050. //话术数量
  1051. $label_counts[$v['cate1']] = isset($label_counts[$v['cate1']]) ? $label_counts[$v['cate1']] += 1 : 1;
  1052. //最近话术上传时间
  1053. $dates[$v['cate1']] = $v['addtime'];
  1054. //使用人次
  1055. $person_count[$v['cate1']] = isset($person_count[$v['cate1']]) ? $person_count[$v['cate1']] + $v['use_count'] : $v['use_count'];
  1056. }
  1057. foreach ($data as $k => $v) {
  1058. $data[$k]['label_count'] = isset($label_counts[$v['name']]) ? $label_counts[$v['name']] : 0;
  1059. $data[$k]['update_time'] = isset($dates[$v['name']]) ? $dates[$v['name']] : '';
  1060. $data[$k]['person_count'] = isset($person_count[$v['name']]) ? $person_count[$v['name']] : 0;
  1061. //来源
  1062. if($v['from_type'] == 0)
  1063. {
  1064. $data[$k]['from'] = '店面发布';
  1065. }elseif($v['from_type'] == 1){
  1066. $data[$k]['from'] = '集团指派';
  1067. }else{
  1068. $company = Company::where('root_id',$v['from_root_id'])->value('company_name');
  1069. $data[$k]['from'] = $company.'共享';
  1070. }
  1071. //清除场景下面话术为0的数据
  1072. if ($data[$k]['label_count'] == 0) {
  1073. // unset($data[$k]);
  1074. }
  1075. }
  1076. $count = count($data);
  1077. $sort = array_column($data , 'id');
  1078. array_multisort($sort , SORT_ASC , $data);
  1079. $page = ($param['page'] - 1) * $param['limit'];
  1080. $data = array_slice($data , $page , $param['limit']);
  1081. }
  1082. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  1083. }
  1084. /*
  1085. * 搜索页面 获取二级分类,使用人群,房屋类型
  1086. */
  1087. public function get_cates()
  1088. {
  1089. $token = $this->request->token;
  1090. $param = Request::only(['id'=>0]);
  1091. //二级分类
  1092. $w[] = ['pid','=',$param['id']];
  1093. $w[] = ['type','=','cate'];
  1094. $w[] = ['root_id','=',$token['root_org']];
  1095. $cate2 = TalkskillCates::where($w)->field('name,id')->order('id asc')->select()->toArray();
  1096. $w33[] = ['root_id','=',$token['root_org']];
  1097. $w33[] = ['approve', '=', 1];
  1098. $w33[] = ['del', '=', 0];
  1099. $cate1 = TalkskillCates::where('id',$param['id'])->value('name');
  1100. $w33[] = ['cate1','=',$cate1];
  1101. $count = TalkskillModel::where($w33)->count();
  1102. foreach ($cate2 as $k => $v) {
  1103. $w3 = [];
  1104. $w3[] = ['root_id','=',$token['root_org']];
  1105. $w3[] = ['approve', '=', 1];
  1106. $w3[] = ['del', '=', 0];
  1107. $sql = "FIND_IN_SET('" . $v['name'] . "' , cate2)";
  1108. $cate2[$k]['count'] = TalkskillModel::where($w3)->whereRaw($sql)->count();
  1109. }
  1110. $data['cate2'] = $cate2;
  1111. //房屋类型
  1112. $w1[] = ['pid','=',0];
  1113. $w1[] = ['type','=','house_type'];
  1114. $w1[] = ['root_id','=',$token['root_org']];
  1115. $data['house_type'] = TalkskillCates::where($w1)->field('name,id')->order('id asc')->select()->toArray();
  1116. //适用人群列表
  1117. $UserCateWhere = ['root_id' => $token['root_org']];
  1118. $data['user'] = TalkskillUsersCate::where($UserCateWhere)->field('name,id')->select()->toarray();
  1119. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data,'count'=>$count]);
  1120. }
  1121. /*
  1122. * 新增页面 获取一级分类 ,二级分类,使用人群,房屋类型
  1123. * talkskill_type:精选话术chosen
  1124. */
  1125. public function get_cates_all()
  1126. {
  1127. $scene_type = Request::param('talkskill_type');
  1128. $token = $this->request->token;
  1129. //一级分类和二级分类
  1130. $cate = [
  1131. ['type','=','cate'],
  1132. ['root_id','=',$token['root_org']]
  1133. ];
  1134. $cate2 = TalkskillCates::where($cate)->field('pid,name,id,scene_type,show')->order('id asc')->select()->toarray();
  1135. //查询关联一级话术数量
  1136. $talkskillArr = TalkskillModel::where([['del', '=', 0], ['root_id', '=', $token['root_org']], ['approve', '=', 1]])->group('cate1')->column('count(id)','cate1');
  1137. //查询成功案例关联一级话术数量
  1138. $talkskillSuccessArr = TalkskillSuccess::where([['del', '=', 0], ['root_id', '=', $token['root_org']]])->group('cate1')->column('count(id)','cate1');
  1139. $catesNewArray = [];
  1140. foreach ($cate2 as $k => $v) {
  1141. if ($v['pid']==0) {
  1142. //排除非设置为精选的一级分类
  1143. if (isset($scene_type) && $scene_type == 'chosen') {
  1144. $v['scene_type'] == 'chosen' && $v['show'] != 1 ? $catesNewArray[] = $v : '';
  1145. }else{
  1146. $v['count'] = isset($talkskillArr[$v['name']]) ? $talkskillArr[$v['name']] : 0;
  1147. $v['successCount'] = isset($talkskillSuccessArr[$v['id']]) ? $talkskillSuccessArr[$v['id']] : 0;
  1148. $catesNewArray[] = $v;
  1149. }
  1150. }
  1151. }
  1152. foreach ($catesNewArray as $k2 => $v2) {
  1153. $catesNewArray[$k2]['child'] = [];
  1154. foreach ($cate2 as $k3 => $v3) {
  1155. //判断在精选案例处不展示“其它”分类
  1156. if($v2['id'] == $v3['pid']) {
  1157. if(isset($scene_type) && $scene_type == 'chosen') {
  1158. $v3['show'] != 1 ? $catesNewArray[$k2]['child'][] = $v3 : '';
  1159. }else{
  1160. $catesNewArray[$k2]['child'][] = $v3;
  1161. }
  1162. }
  1163. }
  1164. }
  1165. $data['cates'] = $catesNewArray;
  1166. //房屋类型
  1167. $houseTypeWhere = [
  1168. ['pid','=',0],
  1169. ['type','=','house_type'],
  1170. ['root_id','=',$token['root_org']]
  1171. ];
  1172. $data['house_type'] = TalkskillCates::where($houseTypeWhere)->field('name,id')->order('id asc')->select()->toarray();
  1173. //适用人群列表
  1174. $UserCateWhere = ['root_id' => $token['root_org']];
  1175. $data['user'] = TalkskillUsersCate::where($UserCateWhere)->field('name,id')->select()->toarray();
  1176. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  1177. }
  1178. /*
  1179. * 成功案例标题列表
  1180. */
  1181. public function success_list()
  1182. {
  1183. $param = Request::only(['page', 'limit', 'keyword', 'house_type', 'user_cate', 'order', 'cate1']);
  1184. $token = $this->request->token;
  1185. $where = [
  1186. ['root_id', '=', $token['root_org']],
  1187. ['del', '=', 0]
  1188. ];
  1189. if(!empty($param['keyword']))
  1190. {
  1191. $where[] = ['title', 'like', '%'.$param['keyword'].'%'];
  1192. }
  1193. //一级场景
  1194. if (!empty($param['cate1'])) {
  1195. $where[] = ['cate1', '=',$param['cate1']];
  1196. }
  1197. $data = TalkskillSuccess::where($where);
  1198. if(!empty($param['house_type']))
  1199. {
  1200. $data = $data->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  1201. }
  1202. if(!empty($param['user_cate']))
  1203. {
  1204. $data = $data->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  1205. }
  1206. $order = 'addtime desc';
  1207. if(!empty($param['order']))
  1208. {
  1209. $order = $param['order'];
  1210. }
  1211. $array = $data->field('id,title,use_count,view_times,addtime')->order($order)->page($param['page'], $param['limit'])->select();
  1212. return json(['code' => 0, 'msg' => '获取成功', 'data' => $array]);
  1213. }
  1214. /*
  1215. * 精选话术、成功案例的话术列表
  1216. */
  1217. public function chosen_and_success_list()
  1218. {
  1219. $param = Request::only(['id', 'page', 'limit', 'house_type', 'user_cate', 'order', 'talkskill_type', 'cate2']);
  1220. $token = $this->request->token;
  1221. $where = [
  1222. ['root_id', '=', $token['root_org']],
  1223. ['del', '=', 0]
  1224. ];
  1225. $order = 'addtime desc';
  1226. if($param['talkskill_type'] == 'chosen')
  1227. {
  1228. $data = TalkskillChosen::field('id,title,view_times,use_count');
  1229. $data = $data->whereRaw("FIND_IN_SET('" . $param['cate2'] . "' , cate2)");
  1230. }else{
  1231. $where[] = ['id', '=', $param['id']];
  1232. $data = TalkskillSuccess::field('id,title,view_times,use_count');
  1233. }
  1234. if(!empty($param['house_type']))
  1235. {
  1236. $data = $data->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
  1237. }
  1238. if(!empty($param['user_cate']))
  1239. {
  1240. $data = $data->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
  1241. }
  1242. if(!empty($param['order']))
  1243. {
  1244. $order = $param['order'];
  1245. }
  1246. $data = $data->where($where)->page($param['page'], $param['limit'])->order($order)->select()->toArray();
  1247. $column_id = array_column($data,'id');
  1248. $content = TalkskillContent::where([['root_id', '=', $token['root_org']], ['talkskill_id', 'in', $column_id], ['talkskill_type', '=', $param['talkskill_type']]])->field('talkskill_id,content,type,img')->order('addtime asc')->select()->toArray();
  1249. foreach($data as &$item)
  1250. {
  1251. $item['child'] = [];
  1252. foreach($content as $key=>$val)
  1253. {
  1254. if($val['talkskill_id'] == $item['id'])
  1255. {
  1256. $val['img'] = $val['img'] ? explode(',',$val['img']) : '';
  1257. if(!empty($val['img'])){
  1258. foreach($val['img'] as &$img){
  1259. $img = 'https://' . config('app.ali_oss_bindurl') . '/' . $img;
  1260. }
  1261. }
  1262. $item['child'][] = $val;
  1263. }
  1264. }
  1265. }
  1266. //使用次数增加
  1267. $condition = [
  1268. ['id', 'in', $column_id],
  1269. ['root_id', '=', $token['root_org']]
  1270. ];
  1271. $model = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where($condition) : TalkskillSuccess::where($condition);
  1272. $model->inc('view_times')->update();
  1273. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  1274. }
  1275. /*
  1276. * 精选话术、成功案例检索查询
  1277. */
  1278. public function keyword_list()
  1279. {
  1280. $param = Request::param();
  1281. $token = $this->request->token;
  1282. $where = [
  1283. ['root_id', '=', $token['root_org']],
  1284. ['del', '=', 0]
  1285. ];
  1286. if(!empty($param['keyword']))
  1287. {
  1288. $array_column = [];
  1289. $column_model = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where($where) : TalkskillSuccess::where($where);
  1290. $array_column = $column_model->where([['title', 'like', '%'.trim($param['keyword']).'%']])->column('id');
  1291. $new_array_column = TalkskillContent::where([
  1292. ['root_id', '=', $token['root_org']],
  1293. ['talkskill_type', '=', $param['talkskill_type']],
  1294. ['content', 'like', '%'.trim($param['keyword']).'%']
  1295. ])->group('talkskill_id')->column('talkskill_id');
  1296. $newArray = array_merge($new_array_column,$array_column);
  1297. $where[] = ['id', 'in', $newArray];
  1298. }
  1299. $data = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where($where) : TalkskillSuccess::where($where);
  1300. if($param['limit'] == '')
  1301. {
  1302. $data = $data->page(1,3);
  1303. }
  1304. $data = $data->field('id,title,use_count,view_times')->order('addtime desc')->select()->toArray();
  1305. //成功案例无需展示回答内容
  1306. if($param['talkskill_type'] == 'chosen')
  1307. {
  1308. $column_id = array_column($data,'id');
  1309. $content = TalkskillContent::where([['root_id', '=', $token['root_org']], ['talkskill_id', 'in', $column_id], ['talkskill_type', '=', $param['talkskill_type']]])->field('talkskill_id,content,type')->order('addtime asc')->select()->toArray();
  1310. foreach($data as &$item)
  1311. {
  1312. $item['child'] = [];
  1313. foreach($content as $key=>$val)
  1314. {
  1315. if($val['talkskill_id'] == $item['id'])
  1316. {
  1317. $item['child'][] = $val;
  1318. }
  1319. }
  1320. }
  1321. }
  1322. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  1323. }
  1324. }