12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451 |
- <?php
- namespace app\api\controller;
- use app\event\Msg;
- use app\model\UserCollect;
- use app\model\Talkskill as TalkskillModel;
- use app\model\TalkskillLabel;
- use app\model\TalkskillComment;
- use app\model\TalkskillHotKeyword;
- use app\model\TalkskillPraise;
- use app\model\Employee;
- use think\facade\Request;
- use app\model\CreditsLog;
- use app\model\Credits;
- use app\model\CreditsSetting;
- use app\model\Company;
- use think\facade\Db;
- use Exception;
- use wx\Oplatform;
- use think\facade\Cache;
- use think\facade\Console;
- use app\model\EmployeeMedal;
- use app\model\TalkskillUsersCate;
- use app\model\TalkskillCates;
- use app\model\TalkskillChosen;
- use app\model\TalkskillSuccess;
- use app\model\TalkskillContent;
- use app\model\TalkskillViewLog;
- class Talkskill extends Base
- {
- /*
- * 热搜词记录
- */
- public function hot_keyword()
- {
- $list = TalkskillHotKeyword::where('root_id', $this->request->token['root_org'])->order('use_count desc')->limit(15)->select();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $list]);
- }
- /*
- * 话术标签列表
- */
- public function label_list()
- {
- $token = $this->request->token;
- // $token['root_org'] = 23;
- $data = TalkskillLabel::where('root_id', $token['root_org'])->select();
- $config = CreditsSetting::where('root_id', $token['root_org'])->column('value', 'code');
- $config['talkskill_credits'] = isset($config['talkskill_credits']) ? $config['talkskill_credits'] : '1';
- $config['talkskill_contribution_value'] = isset($config['talkskill_contribution_value']) ? $config['talkskill_contribution_value'] : '1';
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'config' => $config]);
- }
- /*
- * 匿名提问
- */
- public function submit_questions()
- {
- $token = $this->request->token;
- // $token['employee_id'] = 1;
- // $token['root_org'] = 1;
- $questions = Request::only(['questions', 'imgs','media_id'=>'','weixin_media'=>'']);
- $data = [
- 'title' => trim($questions['questions']),
- 'root_id' => $token['root_org'],
- 'type' => 'submit',
- 'employee_id' => $token['employee_id'],
- 'content' => '',
- 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
- 'media_id'=>$questions['media_id'],
- 'weixin_media'=>$questions['weixin_media']
- ];
- TalkskillModel::create($data);
- return json(['code' => 0, 'msg' => '提交成功']);
- }
- /*
- * 上传话术
- */
- public function submit_talkskill()
- {
- $token = $this->request->token;
- $questions = $param = Request::only(['user_cate','title', 'content', 'label', 'imgs','media_id'=>'','weixin_media'=>'','house_type'=>'','cate1'=>'','cate2'=>'']);
- // $token['employee_id'] = 1;
- // $token['root_org'] = 1;
- $data = [
- 'title' => $questions['title'],
- 'root_id' => $token['root_org'],
- 'type' => 'share',
- 'employee_id' => $token['employee_id'],
- 'content' => $questions['content'],
- 'label' => $questions['label'],
- 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
- 'approve' => 0,
- 'media_id'=>$questions['media_id'],
- 'weixin_media'=>$questions['weixin_media'],
- 'user_cate' => $questions['user_cate'],
- 'house_type' => $questions['house_type'],
- 'cate1' => $questions['cate1'],
- 'cate2' => $questions['cate2']
- ];
- TalkskillModel::create($data);
- return json(['code' => 0, 'msg' => '提交成功']);
- }
- /*
- * 话术列表
- */
- public function lists()
- {
- $param = Request::only(['label', 'user_cate','keyword', 'hot_keyword', 'page', 'is_my', 'status','cate2'=>'','cate1'=>'','house_type'=>'','order'=>'use_count desc', 'messy']);
- $token = $this->request->token;
- $limit = 10;
- $where = [
- ['del', '=', 0],
- ['root_id', '=', $token['root_org']]
- ];
- if (isset($param['is_my']) && $param['is_my'] == 1) { //我共享的
- $where[] = ['type', '=', 'share'];
- $where[] = ['employee_id', '=', $token['employee_id']];
- if (isset($param['status']) && $param['status'] !== '') {
- $where[] = ['approve', '=', $param['status']];
- }
- } else {
- $where[] = ['approve', '=', 1];
- }
- $data = TalkskillModel::field('id,title,content,label,use_count,addtime,approve,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2');
- //使用人群筛选
- if (!empty($param['user_cate'])) {
- $data = $data->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
- }
- //标签筛选
- if (!empty($param['label'])) {
- $data = $data->whereRaw("FIND_IN_SET(" . $param['label'] . " , label)");
- }
- //一级场景
- if (!empty($param['cate1'])) {
- $where[] = ['cate1', '=',$param['cate1']];
- }
- //二级级分类
- if (!empty($param['cate2'])) {
- $id = TalkskillLabel::where([['root_id','=',$token['root_org']],['label_name','=',$param['cate2']]])->value('id');//迭代之前的数据
- $sql = $id ? "(FIND_IN_SET('" . $param['cate2'] . "' , cate2) or label=".$id.')' : "FIND_IN_SET('" . $param['cate2'] . "' , cate2)";
- $data = $data->whereRaw($sql);
- }
- //房屋类型
- if (!empty($param['house_type'])) {
- $data = $data->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
- }
- //仅展示0回答
- if (isset($param['messy']) && $param['messy'] == 1) {
- $where[] = ['commentCount', '=', 0];
- $where[] = ['content', '=', ''];
- }
- $data = $data->with(['TalkskillComment' => function ($query) use ($token) {
- $query->where('root_id='.$token['root_org'].' and approve=1')->order('use desc,praise desc,id asc')
- ->field('taid,uid,comments');
- }])->field('id,title,content,label,use_count,addtime,approve,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2');
- //关键词筛选
- if (!empty(trim($param['keyword']))) {
- $hot = TalkskillHotKeyword::where(['root_id' => $token['root_org'], 'keyword' => trim($param['keyword'])])->find();
- if (!empty($hot)) {
- $hot->use_count = $hot->use_count + 1;
- $hot->save();
- } else {
- TalkskillHotKeyword::create(['root_id' => $token['root_org'], 'keyword' => trim($param['keyword'])]);
- }
- $where[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
- }
- //热词搜索
- if (!empty(trim($param['hot_keyword']))) {
- TalkskillHotKeyword::where(['root_id' => $token['root_org'], 'keyword' => trim($param['hot_keyword'])])->inc('use_count')->update();
- $where[] = ['title', 'like', '%' . trim($param['hot_keyword']) . '%'];
- }
- $list = $data->where($where)->page($param['page'], $limit)->order($param['order'])->select();
- $count = $data->where($where)->count();
- foreach ($list as &$val) {
- $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
- }
- $list = $list->toArray();
- $column_talkskill_id = array_column($list, 'id');
- $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
- $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
- foreach ($list as &$val) {
- //话术回答展示顺序,官方回答,员工回答使用最多,点赞最多,先回答
- if (!$val['content'] && $val['TalkskillComment']) {
- $val['content'] = $val['TalkskillComment'][0]['comments'];
- }
- $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
- $val['status'] = $arr[$val['approve']];
- // $val['addtime'] = isset($comment[$val['id']]) ? $comment[$val['id']] : $val['addtime'];
- unset($e_where);
- $e_where[] = ['taid', '=', $val['id']];
- $e_where[] = ['root_id', '=', $token['root_org']];
- $comment_count = TalkskillComment::where($e_where)->where(function ($query) use ($token) {
- $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
- })->count();
- $val['comment_count'] = $comment_count;
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
- }
- /*
- * 话术详情
- */
- public function talkskill_xq($id)
- {
- $data = TalkskillModel::where('id', $id)->field('id,title,content,label,commentCount,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2')->find();
- // 浏览量
- TalkskillModel::where('id', $id)->inc('view_num')->update();
- //是否收藏
- $collect = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_id' => $id, 'content_type' => 'talkskill'])->count();
- $data['collect'] = $collect > 0 ? 1 : 0;
- // $imgs = $data->imgs ? explode(',',$data->imgs) : [];
- // $ali_oss_bindurl = config('app.ali_oss_bindurl');
- // foreach ($imgs as $k => &$v) {
- // $v = $ali_oss_bindurl.'/'.$v;
- // }
- // $data->imgs = $imgs;
- $this->addTalkskillViewLog('talkskill');
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
- }
- /*
- * 增加话术浏览记录
- */
- private function addTalkskillViewLog($type)
- {
- $token = $this->request->token;
- TalkskillViewLog::create([
- 'root_id' => $token['root_org'],
- 'org_id' => $token['org_id'],
- 'employee_id' => $token['employee_id'],
- 'type' => $type
- ]);
- return true;
- }
- /*
- * 复制话术
- * type->use_comment:复制用户的,use_admin:复制后台的
- */
- public function use_talkskill()
- {
- $param = Request::only(['type', 'id']);
- $token = $this->request->token;
- // $token['root_org'] = 1;
- // $token['employee_id'] = 58;
- Db::startTrans();
- try {
- $end = true;
- $type = $param['type'] == 'use_comment' ? 1 : 0;
- $where = [];
- $where[] = ['type', '=', $type];
- $where[] = ['talkskill_id', '=', $param['id']];
- $where[] = ['root_id', '=', $token['root_org']];
- $where[] = ['employee_id', '=', $token['employee_id']];
- $credits_log = CreditsLog::where($where)->select()->toArray();
- if (!$credits_log) {
- $save['talkskill_id'] = $param['id'];
- $save['root_id'] = $token['root_org'];
- $save['employee_id'] = $token['employee_id'];
- $save['type'] = $type;
- $save['addtime'] = date('Y-m-d H:i:s', time());
- CreditsLog::insert($save);
- //增加使用人次
- if ($param['type'] == 'use_comment') {
- TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->inc('use')->update();
- } else {
- TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->inc('use_count')->update();
- }
- } else {
- $end = false;
- // Db::commit();
- // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
- }
- if ($end) {
- //复制自己上传的话术或者后台添加的话术或者官方回答或非审核通过,不得积分
- if ($param['type'] != 'use_comment') {
- $info = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
- if ($info->isEmpty() || $info['employee_id'] == 0 || $info['type'] == 'admin' || $info['type'] == 'submit' || $info['employee_id'] == $token['employee_id'] || $info['approve'] != 1) {
- $end = false;
- // Db::commit();
- // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
- }
- } else { //复制自己添加的评论不得积分复制没有审核通过的评论不得积分
- $info = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
- $eid = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->findOrEmpty();
- if ($info->isEmpty() || $eid->isEmpty() || $info['uid'] == $eid['uid'] || $info['approve'] != 1) {
- $end = false;
- // Db::commit();
- // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
- }
- $info['employee_id'] = Employee::where([['uid', '=', $info['uid']], ['root_id', '=', $token['root_org']]])->value('id');
- }
- }
- if ($end) {
- //未计算积分的被使用次数
- $where[] = ['status', '=', 0];
- $credits_log = CreditsLog::where($where)->select()->toArray();
- $count = count($credits_log);
- if ($count == 0) {
- $end = false;
- // Db::commit();
- // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
- }
- }
- if ($end) {
- //查询规则
- if ($param['type'] == 'use_comment') {
- $rule = CreditsSetting::where([['root_id', '=', $token['root_org']], ['code', '=', 'talkskill_person2']])->value('value');
- $rule = $rule ? json_decode($rule, true) : ['person2' => 1, 'credits2' => 1];
- $rule = ['person' => $rule['person2'], 'credits' => $rule['credits2']];
- } else {
- $rule = CreditsSetting::where([['root_id', '=', $token['root_org']], ['code', '=', 'talkskill_person1']])->value('value');
- $rule = $rule ? json_decode($rule, true) : ['person1' => 1, 'credits1' => 1];
- $rule = ['person' => $rule['person1'], 'credits' => $rule['credits1']];
- }
- //计算积分
- if ($count >= $rule['person']) {
- $credits = new Credits();
- $save = [];
- $credits->employee_id = $info['employee_id'];
- $credits->root_id = $token['root_org'];
- $credits->credits = $rule['credits'];
- $credits->type = 0;
- $credits->json = $param['id'];
- $credits->remark = ($param['type'] == 'use_comment') ? '话术评论被使用' : '话术被使用';
- $credits->save();
- $id = $credits->id;
- $c_where[] = ['employee_id', '=', $info['employee_id']];
- $c_where[] = ['root_id', '=', $token['root_org']];
- $c_where[] = ['type', '=', 0];
- $sum = Credits::where($c_where)->sum('credits');
- Credits::where([['id', '=', $id]])->update(['sum' => $sum]);
- CreditsLog::where([['id', 'in', array_column($credits_log, 'id')]])->update(['status' => 1]);
- }
- }
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- return json(['code' => 1, 'msg' => $e->getMessage(), 'data' => $this->get_credits($param)]);
- }
- return json(['code' => 0, 'msg' => '复制成功', 'data' => $this->get_credits($param)]);
- }
- public function get_credits($param)
- {
- $token = $this->request->token;
- if ($param['type'] == 'use_comment') {
- $count = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->value('use');
- } else {
- $count = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->value('use_count');
- }
- return $count;
- }
- /*
- * 话术回答列表
- */
- public function commentList()
- {
- $param = Request::only(['taid', 'page']);
- $token = $this->request->token;
- $where[] = ['taid', '=', $param['taid']];
- $where[] = ['root_id', '=', $token['root_org']];
- $data = TalkskillComment::with(['user'])->where(function ($query) use ($token) {
- $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
- })->where($where)->page($param['page'], 10)->order(['approve' => 'asc', 'use' => 'desc', 'praise' => 'desc'])->select()->toArray();
- $column_talkskillcomment_id = array_column($data, 'id');
- $column_talkskillcomment_uid = array_column($data, 'uid');
- $praise = TalkskillPraise::where([['root_id', '=', $token['root_org']], ['content_id', 'in', $column_talkskillcomment_id], ['user_id', '=', $token['uid']]])->column('user_id', 'content_id');
- $employee = Employee::where([['uid', 'in', $column_talkskillcomment_uid], ['root_id', '=', $token['root_org']]])->column('name', 'uid');
- $state = Employee::where([['uid', 'in', $column_talkskillcomment_uid], ['root_id', '=', $token['root_org']]])->column('state', 'uid');
- foreach ($data as &$val) {
- $val['ispraise'] = isset($praise[$val['id']]) ? 1 : 0;
- $val['name'] = isset($employee[$val['uid']]) ? $employee[$val['uid']] : '暂无';
- $val['state'] = isset($state[$val['uid']]) ? $state[$val['uid']] : '离职';
- if (strpos($val['state'], "离职") !== false) {
- $val['name'] = '匿名用户';
- $val['headimgurl'] = '';
- }
- }
- $count = TalkskillComment::where($where)->where(function ($query) use ($token) {
- $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
- })->count();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /*
- * 话术评论
- */
- public function addComment()
- {
- $param = Request::only(['content', 'taid']);
- $data = [
- 'uid' => $this->request->token['uid'],
- 'taid' => $param['taid'],
- 'root_id' => $this->request->token['root_org'],
- 'comments' => $param['content']
- ];
- TalkskillComment::insert($data);
- return json(['code' => 0, 'msg' => '回答成功']);
- }
- /**
- * 收藏
- */
- public function collect($id)
- {
- $rs = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'talkskill', 'content_id' => $id])->count();
- if ($rs > 0) return json(['code' => 1, 'msg' => '您已收藏']);
- UserCollect::create([
- 'user_id' => $this->request->token['uid'],
- 'content_type' => 'talkskill',
- 'content_id' => $id
- ]);
- return json(['code' => 0, 'msg' => '收藏成功']);
- }
- /**
- * 取消收藏
- */
- public function collectCancel($id)
- {
- $rs = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'talkskill', 'content_id' => $id])->find();
- if (!$rs) return json(['code' => 1, 'msg' => '取消收藏失败']);
- $rs->delete();
- return json(['code' => 0, 'msg' => '取消收藏']);
- }
- /**
- * 点赞
- */
- public function praise($id)
- {
- $rs = TalkskillPraise::where(['user_id' => $this->request->token['uid'], 'root_id' => $this->request->token['root_org'], 'content_id' => $id])->count();
- if ($rs > 0) return json(['code' => 1, 'msg' => '您已点赞']);
- TalkskillPraise::create([
- 'user_id' => $this->request->token['uid'],
- 'root_id' => $this->request->token['root_org'],
- 'content_id' => $id
- ]);
- TalkskillComment::where('id', $id)->inc('praise')->update();
- return json(['code' => 0, 'msg' => '点赞成功']);
- }
- /**
- * 取消点赞
- */
- public function praiseCancel($id)
- {
- $rs = TalkskillPraise::where(['user_id' => $this->request->token['uid'], 'root_id' => $this->request->token['root_org'], 'content_id' => $id])->find();
- if (!$rs) return json(['code' => 1, 'msg' => '取消点赞失败']);
- $rs->delete();
- $praise = TalkskillComment::where('id', $id)->value('praise');
- if ($praise > 0) {
- TalkskillComment::where('id', $id)->dec('praise')->update();
- }
- return json(['code' => 0, 'msg' => '取消成功']);
- }
- /**
- * 话术场景列表
- */
- public function get_talkskill_label()
- {
- $param = Request::only(['page' => 1, 'limit' => 10, 'keyword','user_cate']);
- $token = $this->request->token;
- $count = 0;
- $data = TalkskillLabel::where('root_id', $token['root_org'])->order('id desc')->select()->toArray(); //所有分类
- if ($data) {
- $ids = array_column($data, 'id');
- $where1 = [];
- foreach ($ids as $v) {
- $where1[] = "FIND_IN_SET(" . $v . " , label)";
- }
- $where2[] = ['root_id', '=', $token['root_org']];
- $where2[] = ['del', '=', 0];
- $where2[] = ['approve', '=', 1];
- if (isset($param['keyword']) && $param['keyword']) {
- $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
- }
- if (!empty($param['user_cate'])) {
- $where2[] = ['user_cate','=',$param['user_cate']];
- }
- $label_count = TalkskillModel::where($where2)->whereRaw(implode(' or ', $where1))->field('addtime,label label_id,id,use_count')->select()->toArray();
- $label_counts = $dates = $person_count = [];
- foreach ($label_count as $v) {
- $label_ids = explode(',', $v['label_id']);
- foreach ($label_ids as $v2) {
- //话术数量
- $label_counts[$v2] = isset($label_counts[$v2]) ? $label_counts[$v2] += 1 : 1;
- //最近话术上传时间
- $dates[$v2] = isset($dates[$v2]) ? ($dates[$v2] > $v['addtime'] ? $dates[$v2] : $v['addtime']) : $v['addtime'];
- //使用人次
- $person_count[$v2] = isset($person_count[$v2]) ? $person_count[$v2] + $v['use_count'] : $v['use_count'];
- }
- }
- foreach ($data as $k => $v) {
- $data[$k]['label_count'] = isset($label_counts[$v['id']]) ? $label_counts[$v['id']] : 0;
- $data[$k]['update_time'] = isset($dates[$v['id']]) ? $dates[$v['id']] : '';
- $data[$k]['person_count'] = isset($person_count[$v['id']]) ? $person_count[$v['id']] : 0;
- //来源
- if($v['from_type'] == 0)
- {
- $data[$k]['from'] = '店面发布';
- }elseif($v['from_type'] == 1){
- $data[$k]['from'] = '集团指派';
- }else{
- $company = Company::where('root_id',$v['from_root_id'])->value('company_name');
- $data[$k]['from'] = $company.'共享';
- }
- //清除场景下面话术为0的数据
- if ($data[$k]['label_count'] == 0) {
- unset($data[$k]);
- }
- }
- $count = isset($data) ? count($data) : 0;
- $sort = array_column($data , 'id');
- array_multisort($sort , SORT_DESC , $data);
- $page = ($param['page'] - 1) * $param['limit'];
- $data = array_slice($data , $page , $param['limit']);
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /**
- * 白问百搭列表/我的提问列表
- */
- public function get_answer_list()
- {
- $param = Request::only(['page' => 1, 'limit' => 10, 'keyword', 'messy', 'is_my']);
- $token = $this->request->token;
- // $token['root_org'] = 177;
- $where2[] = ['root_id', '=', $token['root_org']];
- $where2[] = ['del', '=', 0];
- $where2[] = ['type', '=', 'submit']; //admin后台增加话术,share小程序上传话术,submit小程序提交问题
- if (isset($param['is_my']) && $param['is_my'] == 1) {
- $where2[] = ['employee_id', '=', $token['employee_id']];
- }
- if (isset($param['keyword']) && $param['keyword']) {
- $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
- }
- $data = TalkskillModel::withCount(['TalkskillComment' => function ($query) {
- $query->where('approve', 1); //回答审核通过
- }])->where($where2)->where('(content is null OR content<=0)')->field('addtime,label label_id,id,use_count,media_id,weixin_media')->select()->toArray();
- foreach ($data as $k => $v) {
- // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
- if (isset($param['messy']) && $param['messy'] == 1 && $v['talkskill_comment_count'] > 0) {
- unset($data[$k]);
- }
- }
- $count = count($data);
- //排序
- $last_names = array_column($data, 'addtime');
- array_multisort($last_names, SORT_DESC, $data);
- $page = ($param['page'] - 1) * $param['limit'];
- $data = array_slice($data, $page, $param['limit']);
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /**
- * 我的回答列表
- */
- public function get_my_answer_list()
- {
- $param = Request::only(['page' => 1, 'limit' => 10]);
- $token = $this->request->token;
- // $token['root_org'] = 1;
- // $token['uid'] = 33;
- $where[] = ['uid', '=', $token['uid']];
- $where[] = ['root_id', '=', $token['root_org']];
- $data = TalkskillComment::with('talkskill')->where($where)->page($param['page'], $param['limit'])->order('addtime desc')->select()->toArray();
- $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
- foreach ($data as $k => $v) {
- // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
- $data[$k]['status'] = $arr[$v['approve']];
- }
- $count = TalkskillComment::with('talkskill')->where($where)->count();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /**
- * 编辑话术
- */
- public function edit_talkskill()
- {
- $questions = $param = Request::only(['title', 'content', 'label', 'imgs', 'id','house_type'=>'','cate1'=>'','cate2'=>'','user_cate'=>'']);
- $token = $this->request->token;
- $info = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
- if ($info->isEmpty() || $info['approve'] == 1) return json(['code' => 1, 'msg' => '已审核无法修改']);
- $data = [
- 'title' => $questions['title'],
- 'content' => $questions['content'],
- 'label' => isset($questions['label']) && $questions['label'] ? $questions['label'] : '',
- 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
- 'approve' => 0,
- 'house_type' => $questions['house_type'],
- 'cate1' => $questions['cate1'],
- 'cate2' => $questions['cate2'],
- 'user_cate' => $questions['user_cate']
- ];
- TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->update($data);
- return json(['code' => 0, 'msg' => '修改成功', 'data' => '修改成功']);
- }
- /**
- * 编辑评论
- */
- public function edit_talkskill_comment()
- {
- $questions = $param = Request::only(['comments', 'id']);
- $token = $this->request->token;
- // $token['employee_id'] = 1;
- // $token['root_org'] = 1;
- $info = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
- if ($info->isEmpty() || $info['approve'] == 1) return json(['code' => 1, 'msg' => '已审核无法修改']);
- // $token['employee_id'] = 1;
- // $token['root_org'] = 1;
- $data = [
- 'comments' => $questions['comments'],
- 'approve' => 0
- ];
- TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->update($data);
- return json(['code' => 0, 'msg' => '修改成功', 'data' => '修改成功']);
- }
- /*
- * 贡献值积分详情
- */
- public function credits_info()
- {
- $param = Request::only(['page', 'limit', 'type']);
- $param['type'] = !isset($param['type']) || !$param['type'] ? 0 : 1;
- $token = $this->request->token;
- // $token['employee_id'] = 25;
- // $token['root_org'] = 23;
- $where[] = ['root_id', '=', $token['root_org']];
- $where[] = ['employee_id', '=', $token['employee_id']];
- $start_time = date('Y-m') . '-01 00:00:00';
- $end_time = date('Y-m-d H:i:s', strtotime('+1 months', strtotime($start_time)));
- //当前贡献值,当前积分
- $val = Credits::where($where)->where('type', $param['type'])
- // ->where([['addtime', 'between', [$start_time, $end_time]]])
- ->sum('credits');
- $res['my_value'] = $val;
- //贡献值排名
- $ranking = Credits::where([['root_id', '=', $token['root_org']], ['type', '=', $param['type']]])
- // ->where([['addtime', 'between', [$start_time, $end_time]]])
- ->group('employee_id')->order('sum desc,id desc')->column('sum(credits) sum', 'employee_id');
- if ($ranking) {
- $found_arr = array_keys($ranking);
- $key = array_search($token['employee_id'], $found_arr);
- $res['my_ranking'] = $key === false ? 0 : $key + 1;
- } else {
- $res['my_ranking'] = 0;
- }
- //详情0积分,1贡献值
- $credits = Credits::where($where)->where('type', $param['type'])->order('id desc')->page($param['page'], $param['limit'])->select()->toArray();
- foreach ($credits as $k => $v) {
- $val = [];
- if(!strstr($v['remark'],'上传案例')){
- if ($v['type'] == 0) {
- $credits[$k]['remark'] = '您的话术被使用积分+' . $v['credits'] . ',当前积分总数:' . $v['sum'];
- } elseif ($v['type'] == 1) {
- $credits[$k]['remark'] = '话术审批已通过,贡献值+' . $v['credits'] . ',当前贡献值总数:' . $v['sum'];
- }
- }else{
- if ($v['type'] == 0) {
- $credits[$k]['remark'] = $v['remark'].'积分+' . $v['credits'] . ',当前积分总数:' . $v['sum'];
- } elseif ($v['type'] == 1) {
- $credits[$k]['remark'] = $v['remark'].'贡献值+' . $v['credits'] . ',当前贡献值总数:' . $v['sum'];
- }
- }
- }
- $res['info'] = $credits;
- $res['info_count'] = Credits::where($where)->where('type', $param['type'])->count();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $res]);
- }
- /**
- * 数字化产出贡献
- * @return \think\response\Json
- */
- public function contribution(){
- $token = $this->request->token;
- $root_id = $token['root_org'];
- $employee_id = $token['employee_id'];
- $where[] = ['root_id', '=', $root_id];
- $where[] = ['employee_id', '=', $employee_id];
- //当前积分
- $val = Credits::where($where)->where('type', '=', 0)->sum('credits');
- $res['integral'] = $val;
- // 当前贡献值
- $val = Credits::where($where)->where('type', '=', 1)->sum('credits');
- $res['contribution'] = $val;
- //勋章数量
- $res['medal'] = EmployeeMedal::where($where)->count();
- //贡献值排名
- $ranking = Credits::where([['root_id', '=', $root_id], ['type', '=', 1]])->group('employee_id')->order('sum desc')->column('sum(credits) as sum');
- $credits_where[] = ['root_id', '=', $root_id];
- $credits_where[] = ['employee_id', '=', $employee_id];
- $credits_where[] = ['type', '=', 1];
- $credits_all = Credits::where($credits_where)->sum('credits');
- $res['credits_ranking'] = 0;
- if ($ranking && $credits_all) {
- $key = array_search($credits_all, $ranking);
- if ($key === false) {
- $res['credits_ranking'] = count($ranking) + 1;
- } else {
- $res['credits_ranking'] = $key + 1;
- }
- } else {
- $res['credits_ranking'] = count($ranking) + 1;
- }
- $res['help_people'] = TalkskillModel::where([['root_id', '=', $root_id], ['employee_id', '=', $employee_id]])->sum('use_count');
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $res]);
- }
- /*
- * 话术审核记录列表
- */
- public function approve_talkskill()
- {
- $param = Request::only(['page' => 1, 'limit' => 10, 'status' => 0]);
- $token = $this->request->token;
- // $token['employee_id'] = 25;
- // $token['root_org'] = 1;
- $limit = 10;
- $where = [
- ['del', '=', 0],
- ['root_id', '=', $token['root_org']],
- ['aprove_employee_id', '=', $token['employee_id']],
- ['approve', '>', 0], //审批,0待审核,1审核通过,2不通过
- ];
- if ($param['status']) {
- $where[] = ['approve', '=', $param['status']];
- }
- $data = TalkskillModel::field('id,title,content,label,use_count,addtime,approve,imgs');
- $data = $data->with(['TalkskillComment' => function ($query) use ($token) {
- $query->where([['root_id', '=', $token['root_org']]])->field('taid,uid');
- }])->field('id,title,content,label,use_count,addtime,approve,imgs');
- $list = $data->where($where)->page($param['page'], $limit)->order('aprove_time desc')->select();
- $count = $data->where($where)->count();
- foreach ($list as &$val) {
- $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
- }
- $list = $list->toArray();
- $column_talkskill_id = array_column($list, 'id');
- $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
- $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
- foreach ($list as &$val) {
- $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
- $val['status'] = $arr[$val['approve']];
- $val['addtime'] = isset($comment[$val['id']]) ? $comment[$val['id']] : $val['addtime'];
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
- }
- /**
- * 回答审核记录列表
- */
- public function approve_talkskill_comment()
- {
- $param = Request::only(['page' => 1, 'limit' => 10, 'status' => 0]);
- $token = $this->request->token;
- // $token['root_org'] = 1;
- // $token['employee_id'] = 25;
- if ($param['status']) {
- $where[] = ['approve', '=', $param['status']];
- }
- $where[] = ['root_id', '=', $token['root_org']];
- $where[] = ['aprove_employee_id', '=', $token['employee_id']];
- $data = TalkskillComment::with('talkskill')->where($where)->page($param['page'], $param['limit'])->order('aprove_time desc')->select()->toArray();
- $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
- foreach ($data as $k => $v) {
- // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
- $data[$k]['status'] = $arr[$v['approve']];
- }
- $count = TalkskillComment::with('talkskill')->where($where)->count();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /*
- * 业务员上传的话术审核列表
- * 只能审核直接下级
- */
- public function approve_talkskill_list()
- {
- $param = Request::only(['page' => 1, 'limit' => 10]);
- $token = $this->request->token;
- // $token['root_org'] = 1;
- // $token['org_id'] = 22;
- // $token['employee_id'] = 37;
- $where = [
- ['root_id', '=', $token['root_org']],
- ['del', '=', 0],
- ['approve', '=', 0],
- ['type', '=', 'share'],
- ['employee_id', '>', 0]
- ];
- $employee = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->value('is_manager');
- if ($employee != 1) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
- $f = $employee == 1 ? '(org.pid=' . $token['org_id'] . ' or (org.id=' . $token['org_id'] . ' and employee.is_manager=0))' : [['org.pid', '=', $token['org_id']]];
- // echo $f;
- $eids = Employee::withJoin('org', 'inner')
- ->where([['employee.id', '<>', $token['employee_id']]])
- ->where($f)->column('employee.id');
- // var_dump($eids);die;
- if (!$eids) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
- $where[] = ['employee_id', 'in', $eids];
- $data = TalkskillModel::field('id,title,content,label,use_count,addtime,approve,imgs');
- $data = $data->with(['TalkskillComment' => function ($query) use ($token) {
- $query->where([['root_id', '=', $token['root_org']]])->field('taid,uid');
- }])->field('id,title,content,label,use_count,addtime,approve,imgs');
- $list = $data->where($where)->page($param['page'], $param['limit'])->order('aprove_time desc')->select();
- $count = $data->where($where)->count();
- foreach ($list as &$val) {
- $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
- }
- $list = $list->toArray();
- $column_talkskill_id = array_column($list, 'id');
- $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
- $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
- foreach ($list as &$val) {
- $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
- $val['status'] = $arr[$val['approve']];
- $val['addtime'] = isset($comment[$val['id']]) ? $comment[$val['id']] : $val['addtime'];
- }
- return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
- }
- /**
- * 回答审核列表
- */
- public function approve_talkskill_comment_list()
- {
- $param = Request::only(['page' => 1, 'limit' => 10]);
- $token = $this->request->token;
- // $token['root_org'] = 23;
- // $token['org_id'] = 43;
- // $token['employee_id'] = 58;
- $employee = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->value('is_manager');
- if ($employee != 1) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
- $f = '(org.pid=' . $token['org_id'] . ' or (org.id=' . $token['org_id'] . ' and employee.is_manager=0))';
- $eids = Employee::withJoin(['org'], 'inner')
- ->where([['employee.id', '<>', $token['employee_id']]])
- ->where($f)->column('employee.uid');
- // var_dump($eids);die;
- if (!$eids) return json(['code' => 0, 'data' => [], 'count' => 0, 'msg' => '获取成功']);
- $where[] = ['root_id', '=', $token['root_org']];
- $where[] = ['uid', 'in', $eids];
- $where[] = ['approve', '=', 0];
- $data = TalkskillComment::with('talkskill')->where($where)->page($param['page'], $param['limit'])->order('aprove_time desc')->select()->toArray();
- $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
- foreach ($data as $k => $v) {
- // $data[$k]['imgs'] = $v['imgs'] ? explode(',',$v['imgs']) : [];
- $data[$k]['status'] = $arr[$v['approve']];
- }
- $count = TalkskillComment::with('talkskill')->where($where)->count();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /*
- * 批量审核话术
- */
- public function talkskill_approve()
- {
- $ids = explode(',', input('id'));
- $approve = input('status', 2);
- $where = [
- ['root_id', '=', $this->request->token['root_org']],
- ['id', 'in', $ids],
- ['approve', '=', 0]
- ];
- // 数据获取
- $list = TalkskillModel::where($where)->select();
- // 数据初始化
- $employeeCreditSum = [];
- $insertData = [];
- $updateTalkskillDataList = [];
- // 单次贡献值增加数值
- $contribution = 0;
- if ($approve == 1) {
- $contribution = CreditsSetting::where(['code' => 'talkskill_credits', 'root_id' => $this->request->token['root_org']])->value('value');
- !empty($contribution) ?: $contribution = 1;
- }
- $i = 0;
- // 数据整合
- foreach ($list as $v) {
- if (!isset($employeeCreditSum[$v['employee_id']]))
- $employeeCreditSum[$v['employee_id']] = Credits::where(['root_id' => $this->request->token['root_org'], 'employee_id' => $v['employee_id'], 'type' => 1])->sum('credits');
- if ($approve == 1) {
- $employeeCreditSum[$v['employee_id']] += $contribution;
- /*$insertData[] = [
- 'employee_id' => $v['employee_id'],
- 'root_id' => $this->request->token['root_org'],
- 'credits' => $contribution,
- 'sum' => $employeeCreditSum[$v['employee_id']],
- 'type' => 1,
- 'json' => $v['id'],
- 'remark' => '共享话术审核通过'
- ];*/
- Credits::create([
- 'employee_id' => $v['employee_id'],
- 'root_id' => $this->request->token['root_org'],
- 'credits' => $contribution,
- 'sum' => $employeeCreditSum[$v['employee_id']],
- 'type' => 1,
- 'json' => $v['id'],
- 'remark' => '共享话术审核通过'
- ]);
- }
- /*$updateTalkskillDataList[] = [
- 'id' => $v['id'],
- 'approve' => $approve,
- 'aprove_employee_id' => $this->request->token['employee_id'],
- 'aprove_time' => date('Y-m-d H:i:s')
- ];*/
- TalkskillModel::where('id',$v['id'])->update([
- 'approve' => $approve,
- 'aprove_employee_id' => $this->request->token['employee_id'],
- 'aprove_time' => date('Y-m-d H:i:s')
- ]);
- $s = $approve == 1 ? '已' : '未';
- if($approve == 1) $i+=1;
- event(new Msg($v['employee_id'], '您提交的共享话术“' . $v['title'] . '”' . $s . '审核通过', 'talkskillApprove'));
- }
- if($i) dataStatistics($this->request->token['root_org'],'talkskill_count',$i,'inc');//manage应用首页统计数据
- // 数据保存
- (new TalkskillModel())->saveAll($updateTalkskillDataList);
- (new Credits())->saveAll($insertData);
- return json(['code'=> 0, 'msg'=> '操作成功']);
- }
- /**
- * 批量审核回答
- */
- public function all_approve_comment()
- {
- $ids = explode(',', input('id'));
- $approve = input('status', 2);
- $where = [
- ['root_id', '=', $this->request->token['root_org']],
- ['id', 'in', $ids],
- ['approve', '=', 0]
- ];
- // 数据获取
- $list = TalkskillComment::where($where)->select();
-
- // 数据初始化
- $employeeCreditSum = [];
- $insertData = [];
- $updateTalkskillCommentDataList = [];
- $idList = [];
- // 单次贡献值增加数值
- $contribution = 0;
- if ($approve == 1) {
- $contribution = CreditsSetting::where(['code' => 'talkskill_contribution_value', 'root_id' => $this->request->token['root_org']])->value('value');
- !empty($contribution) ?: $contribution = 1;
- }
- // 数据整合
- foreach ($list as $v) {
- if (!isset($employeeCreditSum[$v['uid']])) {
- $employeeCreditSum[$v['uid']]['employee_id'] = Employee::where(['root_id' => $this->request->token['root_org'], 'uid' => $v['uid'], 'state' => '在职'])->value('id');
- $employeeCreditSum[$v['uid']]['creditsSum'] = Credits::where(['root_id' => $this->request->token['root_org'], 'employee_id' => $v['employee_id'], 'type' => 1])->sum('credits');
- }
- if ($approve == 1) {
- $employeeCreditSum[$v['uid']]['creditsSum'] += $contribution;
- $insertData[] = [
- 'employee_id' => $employeeCreditSum[$v['uid']]['employee_id'],
- 'root_id' => $this->request->token['root_org'],
- 'credits' => $contribution,
- 'sum' => $employeeCreditSum[$v['uid']]['creditsSum'],
- 'type' => 1,
- 'json' => $v['id'],
- 'remark' => '话术评论审核通过增加贡献值'
- ];
- }
- $updateTalkskillCommentDataList[] = [
- 'id' => $v['id'],
- 'approve' => $approve,
- 'aprove_employee_id' => $this->request->token['employee_id'],
- 'aprove_time' => date('Y-m-d H:i:s')
- ];
- $idList[] = $v['id'];
- $s = $approve == 1 ? '已' : '未';
- event(new Msg($employeeCreditSum[$v['uid']]['employee_id'], '您提交的话术评论“' . $v['comments'] . '”' . $s . '审核通过', 'talkskillCemApprove'));
- }
- // 数据保存
- (new TalkskillComment())->saveAll($updateTalkskillCommentDataList);
- (new Credits())->saveAll($insertData);
- TalkskillModel::where('id', 'in', $idList)->inc('commentCount')->update();
- return json(["code" => 0, 'msg' => '操作成功', 'data' => []]);
- }
- /** ------------------------ 企业微信智能话术 end ----------------------- */
- //微信1-JS-SDK使用权限签名算法
- public function ticket($url)
- {
- $appid = config('app.official_appid');
- $secret = config('app.official_secret');
- $accesstoken = Cache::get('zqxg_offi_access_token');
- if (!$accesstoken) {
- $accesstoken = (new Oplatform())->AccessToken($appid,$secret);
- $accesstoken = $accesstoken['access_token'];
- Cache::set('zqxg_offi_access_token',$accesstoken,7100);
- }
- $ticket = (new Oplatform())->getTicket($accesstoken);
- //随机字符
- $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
- $randStr = str_shuffle($str);//打乱字符串
- $rands= substr($randStr,0,10);//substr(string,start,length);返回字符串的一部分
- $data['appid'] = $appid;
- $data['timestamp'] = time();
- $data['noncestr'] = $rands;
- $signParam['jsapi_ticket'] = $ticket['ticket'];
- $signParam['noncestr'] = $rands;
- $signParam['url'] = $url;
- $signParam['timestamp'] = $data['timestamp'];
- ksort($signParam);
- $scept = toUrlParams($signParam);
- $scept = sha1($scept);
- $data['sign'] = $scept;
- return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
- }
- public function system(){
- $type = input('type','');
- // 来自企业微信的请求
- $from_wework = input('from_wework', '', 'trim');
- if ($from_wework) {
- $output = Console::call('download',[$type, 'wework', $this->request->token['root_org']]);
- } else {
- $output = Console::call('download',[$type]);
- }
- return $output->fetch();
- }
- /*
- * 使用人群列表
- */
- public function get_person_list()
- {
- $token = $this->request->token;
- $param = Request::only(['page' => 1, 'limit' => 10]);
- $where = [
- 'root_id' => $token['root_org']
- ];
- $list = TalkskillUsersCate::where($where)->order('id asc')->select();
- return json(['code' => 0, 'data' => $list]);
- }
- /*
- * 迭代,获取一级场景列表
- */
- public function get_cate_list()
- {
- $param = Request::only(['page' => 1, 'limit' => 10, 'keyword','cate1']);
-
- $token = $this->request->token;
- $count = 0;
- //一级分类
- $cw[] = ['root_id','=',$token['root_org']];
- $cw[] = ['pid','=',0];
- $cw[] = ['type','=','cate'];
- $data = TalkskillCates::where($cw)->order('id asc')->select()->toArray(); //所有分类
- if ($data) {
- $where2[] = ['cate1','in',array_column($data, 'name')];
- $where2[] = ['root_id', '=', $token['root_org']];
- $where2[] = ['del', '=', 0];
- $where2[] = ['approve', '=', 1];
- if (isset($param['keyword']) && $param['keyword']) {
- $where2[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
- }
- if (!empty($param['cate1'])) {
- $where2[] = ['cate1','=',$param['cate1']];
- }
- $label_count = TalkskillModel::where($where2)->field('addtime,label label_id,id,use_count,cate1')->order('id asc')->select()->toArray();
- $label_counts = $dates = $person_count = [];
- foreach ($label_count as $v) {
- //话术数量
- $label_counts[$v['cate1']] = isset($label_counts[$v['cate1']]) ? $label_counts[$v['cate1']] += 1 : 1;
- //最近话术上传时间
- $dates[$v['cate1']] = $v['addtime'];
- //使用人次
- $person_count[$v['cate1']] = isset($person_count[$v['cate1']]) ? $person_count[$v['cate1']] + $v['use_count'] : $v['use_count'];
- }
- foreach ($data as $k => $v) {
- $data[$k]['label_count'] = isset($label_counts[$v['name']]) ? $label_counts[$v['name']] : 0;
- $data[$k]['update_time'] = isset($dates[$v['name']]) ? $dates[$v['name']] : '';
- $data[$k]['person_count'] = isset($person_count[$v['name']]) ? $person_count[$v['name']] : 0;
- //来源
- if($v['from_type'] == 0)
- {
- $data[$k]['from'] = '店面发布';
- }elseif($v['from_type'] == 1){
- $data[$k]['from'] = '集团指派';
- }else{
- $company = Company::where('root_id',$v['from_root_id'])->value('company_name');
- $data[$k]['from'] = $company.'共享';
- }
- //清除场景下面话术为0的数据
- if ($data[$k]['label_count'] == 0) {
- // unset($data[$k]);
- }
- }
- $count = count($data);
- $sort = array_column($data , 'id');
- array_multisort($sort , SORT_ASC , $data);
- $page = ($param['page'] - 1) * $param['limit'];
- $data = array_slice($data , $page , $param['limit']);
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
- }
- /*
- * 搜索页面 获取二级分类,使用人群,房屋类型
- */
- public function get_cates()
- {
- $token = $this->request->token;
- $param = Request::only(['id'=>0]);
- //二级分类
- $w[] = ['pid','=',$param['id']];
- $w[] = ['type','=','cate'];
- $w[] = ['root_id','=',$token['root_org']];
- $cate2 = TalkskillCates::where($w)->field('name,id')->order('id asc')->select()->toArray();
- $w33[] = ['root_id','=',$token['root_org']];
- $w33[] = ['approve', '=', 1];
- $w33[] = ['del', '=', 0];
- $cate1 = TalkskillCates::where('id',$param['id'])->value('name');
- $w33[] = ['cate1','=',$cate1];
- $count = TalkskillModel::where($w33)->count();
- foreach ($cate2 as $k => $v) {
- $w3 = [];
- $w3[] = ['root_id','=',$token['root_org']];
- $w3[] = ['approve', '=', 1];
- $w3[] = ['del', '=', 0];
- $sql = "FIND_IN_SET('" . $v['name'] . "' , cate2)";
- $cate2[$k]['count'] = TalkskillModel::where($w3)->whereRaw($sql)->count();
- }
- $data['cate2'] = $cate2;
- //房屋类型
- $w1[] = ['pid','=',0];
- $w1[] = ['type','=','house_type'];
- $w1[] = ['root_id','=',$token['root_org']];
- $data['house_type'] = TalkskillCates::where($w1)->field('name,id')->order('id asc')->select()->toArray();
- //适用人群列表
- $UserCateWhere = ['root_id' => $token['root_org']];
- $data['user'] = TalkskillUsersCate::where($UserCateWhere)->field('name,id')->select()->toarray();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data,'count'=>$count]);
- }
- /*
- * 新增页面 获取一级分类 ,二级分类,使用人群,房屋类型
- * talkskill_type:精选话术chosen
- */
- public function get_cates_all()
- {
- $scene_type = Request::param('talkskill_type');
- $token = $this->request->token;
- //一级分类和二级分类
- $cate = [
- ['type','=','cate'],
- ['root_id','=',$token['root_org']]
- ];
- $cate2 = TalkskillCates::where($cate)->field('pid,name,id,scene_type,show')->order('id asc')->select()->toarray();
- //查询关联一级话术数量
- $talkskillArr = TalkskillModel::where([['del', '=', 0], ['root_id', '=', $token['root_org']], ['approve', '=', 1]])->group('cate1')->column('count(id)','cate1');
- //查询成功案例关联一级话术数量
- $talkskillSuccessArr = TalkskillSuccess::where([['del', '=', 0], ['root_id', '=', $token['root_org']]])->group('cate1')->column('count(id)','cate1');
- $catesNewArray = [];
- foreach ($cate2 as $k => $v) {
- if ($v['pid']==0) {
- //排除非设置为精选的一级分类
- if (isset($scene_type) && $scene_type == 'chosen') {
- $v['scene_type'] == 'chosen' && $v['show'] != 1 ? $catesNewArray[] = $v : '';
- }else{
- $v['count'] = isset($talkskillArr[$v['name']]) ? $talkskillArr[$v['name']] : 0;
- $v['successCount'] = isset($talkskillSuccessArr[$v['id']]) ? $talkskillSuccessArr[$v['id']] : 0;
- $catesNewArray[] = $v;
- }
- }
- }
- foreach ($catesNewArray as $k2 => $v2) {
- $catesNewArray[$k2]['child'] = [];
- foreach ($cate2 as $k3 => $v3) {
- //判断在精选案例处不展示“其它”分类
- if($v2['id'] == $v3['pid']) {
- if(isset($scene_type) && $scene_type == 'chosen') {
- $v3['show'] != 1 ? $catesNewArray[$k2]['child'][] = $v3 : '';
- }else{
- $catesNewArray[$k2]['child'][] = $v3;
- }
- }
- }
- }
- $data['cates'] = $catesNewArray;
- //房屋类型
- $houseTypeWhere = [
- ['pid','=',0],
- ['type','=','house_type'],
- ['root_id','=',$token['root_org']]
- ];
- $data['house_type'] = TalkskillCates::where($houseTypeWhere)->field('name,id')->order('id asc')->select()->toarray();
- //适用人群列表
- $UserCateWhere = ['root_id' => $token['root_org']];
- $data['user'] = TalkskillUsersCate::where($UserCateWhere)->field('name,id')->select()->toarray();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
- }
- /*
- * 成功案例标题列表
- */
- public function success_list()
- {
- $param = Request::only(['page', 'limit', 'keyword', 'house_type', 'user_cate', 'order', 'cate1']);
- $token = $this->request->token;
- $where = [
- ['root_id', '=', $token['root_org']],
- ['del', '=', 0]
- ];
- if(!empty($param['keyword']))
- {
- $where[] = ['title', 'like', '%'.$param['keyword'].'%'];
- }
- //一级场景
- if (!empty($param['cate1'])) {
- $where[] = ['cate1', '=',$param['cate1']];
- }
- $data = TalkskillSuccess::where($where);
- if(!empty($param['house_type']))
- {
- $data = $data->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
- }
- if(!empty($param['user_cate']))
- {
- $data = $data->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
- }
- $order = 'addtime desc';
- if(!empty($param['order']))
- {
- $order = $param['order'];
- }
- $array = $data->field('id,title,use_count,view_times,addtime')->order($order)->page($param['page'], $param['limit'])->select();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $array]);
- }
- /*
- * 精选话术、成功案例的话术列表
- */
- public function chosen_and_success_list()
- {
- $param = Request::only(['id', 'page', 'limit', 'house_type', 'user_cate', 'order', 'talkskill_type', 'cate2']);
- $token = $this->request->token;
- $where = [
- ['root_id', '=', $token['root_org']],
- ['del', '=', 0]
- ];
- $order = 'addtime desc';
- if($param['talkskill_type'] == 'chosen')
- {
- $data = TalkskillChosen::field('id,title,view_times,use_count');
- $data = $data->whereRaw("FIND_IN_SET('" . $param['cate2'] . "' , cate2)");
- }else{
- $where[] = ['id', '=', $param['id']];
- $data = TalkskillSuccess::field('id,title,view_times,use_count');
- }
- if(!empty($param['house_type']))
- {
- $data = $data->whereRaw("FIND_IN_SET('" . $param['house_type'] . "' , house_type)");
- }
- if(!empty($param['user_cate']))
- {
- $data = $data->whereRaw("FIND_IN_SET('" . $param['user_cate'] . "' , user_cate)");
- }
- if(!empty($param['order']))
- {
- $order = $param['order'];
- }
- $data = $data->where($where)->page($param['page'], $param['limit'])->order($order)->select()->toArray();
- $column_id = array_column($data,'id');
- $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();
- foreach($data as &$item)
- {
- $item['child'] = [];
- foreach($content as $key=>$val)
- {
- if($val['talkskill_id'] == $item['id'])
- {
- $val['img'] = $val['img'] ? explode(',',$val['img']) : '';
- if(!empty($val['img'])){
- foreach($val['img'] as &$img){
- $img = 'https://' . config('app.ali_oss_bindurl') . '/' . $img;
- }
- }
- $item['child'][] = $val;
- }
- }
- }
- //使用次数增加
- $condition = [
- ['id', 'in', $column_id],
- ['root_id', '=', $token['root_org']]
- ];
- $model = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where($condition) : TalkskillSuccess::where($condition);
- $model->inc('view_times')->update();
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
- }
- /*
- * 精选话术、成功案例检索查询
- */
- public function keyword_list()
- {
- $param = Request::param();
- $token = $this->request->token;
- $where = [
- ['root_id', '=', $token['root_org']],
- ['del', '=', 0]
- ];
- if(!empty($param['keyword']))
- {
- $array_column = [];
- $column_model = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where($where) : TalkskillSuccess::where($where);
- $array_column = $column_model->where([['title', 'like', '%'.trim($param['keyword']).'%']])->column('id');
- $new_array_column = TalkskillContent::where([
- ['root_id', '=', $token['root_org']],
- ['talkskill_type', '=', $param['talkskill_type']],
- ['content', 'like', '%'.trim($param['keyword']).'%']
- ])->group('talkskill_id')->column('talkskill_id');
- $newArray = array_merge($new_array_column,$array_column);
- $where[] = ['id', 'in', $newArray];
- }
- $data = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where($where) : TalkskillSuccess::where($where);
- if($param['limit'] == '')
- {
- $data = $data->page(1,3);
- }
- $data = $data->field('id,title,use_count,view_times')->order('addtime desc')->select()->toArray();
- //成功案例无需展示回答内容
- if($param['talkskill_type'] == 'chosen')
- {
- $column_id = array_column($data,'id');
- $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();
- foreach($data as &$item)
- {
- $item['child'] = [];
- foreach($content as $key=>$val)
- {
- if($val['talkskill_id'] == $item['id'])
- {
- $item['child'][] = $val;
- }
- }
- }
- }
- return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
- }
- }
|