Talkskill.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\mobile\controller;
  4. use app\model\Employee;
  5. use app\model\User;
  6. use think\facade\Request;
  7. use app\model\TalkskillCates;
  8. use app\model\TalkskillSuccess;
  9. use app\model\TalkskillUsersCate;
  10. use app\model\Talkskill as TalkskillModel;
  11. use app\model\TalkskillHotKeyword;
  12. use app\model\TalkskillComment;
  13. use app\model\TalkskillChosen;
  14. use app\model\TalkskillContent;
  15. use app\model\TalkskillPraise;
  16. use app\model\TalkskillViewLog;
  17. use app\model\UserCollect;
  18. use app\model\CreditsLog;
  19. use app\model\CreditsSetting;
  20. use Exception;
  21. use think\facade\Db;
  22. use app\model\Credits;
  23. class Talkskill extends Base
  24. {
  25. /**
  26. * 精选话术分类
  27. */
  28. public function getChosenCate()
  29. {
  30. $where = [
  31. // ['scene_type','=','chosen'],//不唯一 都是精选话术,一份分类有chosen,二级分类没有
  32. ['root_id','=',$this->rootId],
  33. ['show','<>',1],
  34. ['type','=','cate']
  35. ];
  36. $list = TalkskillCates::where($where)->field('pid,name,id,scene_type')->select()->toArray();
  37. $res = [];
  38. foreach ($list as $key => $value) {
  39. if($value['pid']==0 && $value['scene_type']=='chosen'){
  40. $value['child'] = [];
  41. foreach ($list as $item) {
  42. if ($item['pid'] && $value['id'] == $item['pid']) $value['child'][] = $item;
  43. }
  44. $res[] = $value;
  45. }
  46. }
  47. return json(['code'=>0,'data'=>$res,'msg'=>'']);
  48. }
  49. /*
  50. * 成功案例列表
  51. */
  52. public function successList()
  53. {
  54. $param = Request::only(['page'=>1, 'limit'=>10, 'keyword'=>'', 'house_type'=>0, 'user_cate'=>0, 'order'=>'', 'cate1'=>0]);
  55. $where = [
  56. ['root_id', '=', $this->rootId],
  57. ['del', '=', 0]
  58. ];
  59. if($param['keyword']) $where[] = ['title', 'like', '%'.$param['keyword'].'%'];
  60. if ($param['cate1']) $where[] = ['cate1', '=',$param['cate1']];
  61. if($param['house_type']) $where[] = ['house_type','find in set',$param['house_type']];
  62. if($param['user_cate']) $where[] = ['user_cate','find in set',$param['user_cate']];
  63. $order = $param['order'] ?: 'addtime desc';
  64. $array = TalkskillSuccess::where($where)->field('id,title,use_count,view_times,addtime,cate1')->order($order)->page((int)$param['page'], (int)$param['limit'])->select()->toArray();
  65. $cates = array_column($array,'cate1');
  66. $cate = TalkskillCates::where([['id','in',$cates],['root_id', '=', $this->rootId]])->column('name','id');
  67. foreach ($array as $key => $value) {
  68. $array[$key]['addtime'] = date('Y-m-d',strtotime($value['addtime']));
  69. $array[$key]['cate1_name'] = isset($cate[$value['cate1']]) ? $cate[$value['cate1']] : '';
  70. }
  71. return json(['code' => 0, 'msg' => '获取成功', 'data' => $array]);
  72. }
  73. /*
  74. * 话术列表
  75. */
  76. public function lists()
  77. {
  78. $param = Request::only(['page'=>10,'limit'=>10, 'user_cate'=>0,'keyword'=>'','cate1'=>'','house_type'=>'','order'=>'id desc', 'messy'=>0]);
  79. $token['root_org'] = $this->rootId;
  80. $token['employee_id'] = $this->employeeId;
  81. $token['uid'] = $this->uid;
  82. $where = [
  83. ['del', '=', 0],
  84. ['root_id', '=', $token['root_org']],
  85. ['approve', '=', 1]
  86. ];
  87. if ($param['cate1']) $where[] = ['cate1', '=',$param['cate1']];
  88. if ($param['user_cate']) $where[] = ['user_cate','find in set',$param['user_cate']];
  89. if (!empty($param['house_type'])) $where[] = ['house_type','find in set',$param['house_type']];
  90. $order = $param['order'] ?: 'id desc';
  91. //关键词筛选
  92. if (!empty(trim($param['keyword']))) {
  93. $hot = TalkskillHotKeyword::where(['root_id' => $token['root_org'], 'keyword' => trim($param['keyword'])])->find();
  94. if (!empty($hot)) {
  95. $hot->use_count = $hot->use_count + 1;
  96. $hot->save();
  97. } else {
  98. TalkskillHotKeyword::create(['root_id' => $token['root_org'], 'keyword' => trim($param['keyword'])]);
  99. }
  100. $where[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  101. }
  102. //仅展示0回答
  103. if (isset($param['messy']) && $param['messy'] == 1) {
  104. $where[] = ['commentCount', '=', 0];
  105. $where[] = ['content', '=', ''];
  106. }
  107. $data = TalkskillModel::where($where)->with(['TalkskillComment' => function ($query) use ($token) {
  108. $query->where('root_id='.$token['root_org'].' and approve=1')->order('use desc,praise desc,id asc')
  109. ->field('taid,uid,comments');
  110. }])->field('id,title,content,label,use_count,addtime,approve,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2');
  111. $list = $data->where($where)->page((int)$param['page'],(int)$param['limit'])->order($order)->select();
  112. $count = $data->where($where)->count();
  113. foreach ($list as &$val) {
  114. $val['y_imgs'] = $val['imgs'] ? $val->getData('imgs') : '';
  115. }
  116. $list = $list->toArray();
  117. $column_talkskill_id = array_column($list, 'id');
  118. $comment = TalkskillComment::where([['taid', 'in', $column_talkskill_id], ['root_id', '=', $token['root_org']]])->field('taid,addtime')->group('taid')->order('addtime desc')->column('addtime', 'taid');
  119. $arr = [0 => '待审核', 1 => '审核通过', 2 => '被驳回'];
  120. foreach ($list as &$val) {
  121. //话术回答展示顺序,官方回答,员工回答使用最多,点赞最多,先回答
  122. if (!$val['content'] && $val['TalkskillComment']) {
  123. $val['content'] = $val['TalkskillComment'][0]['comments'];
  124. }
  125. $val['TalkskillComment'] = $val['TalkskillComment'] ? count(array_unique(array_column($val['TalkskillComment'], 'uid'))) : 0;
  126. $val['status'] = $arr[$val['approve']];
  127. unset($e_where);
  128. $e_where[] = ['taid', '=', $val['id']];
  129. $e_where[] = ['root_id', '=', $token['root_org']];
  130. $comment_count = TalkskillComment::where($e_where)->where(function ($query) use ($token) {
  131. $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
  132. })->count();
  133. $val['comment_count'] = $comment_count;
  134. }
  135. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
  136. }
  137. /*
  138. * 话术搜索条件下拉
  139. */
  140. public function searchSelect()
  141. {
  142. //房屋类型
  143. $houseTypeWhere = [
  144. ['pid','=',0],
  145. ['type','=','house_type'],
  146. ['root_id','=',$this->rootId]
  147. ];
  148. $data['house_type'] = TalkskillCates::where($houseTypeWhere)->field('name,id')->order('id asc')->select()->toarray();
  149. //适用人群列表
  150. $UserCateWhere = [
  151. ['root_id' ,'=', $this->rootId]
  152. ];
  153. $data['user'] = TalkskillUsersCate::where($UserCateWhere)->field('name,id')->select()->toarray();
  154. //精选话术场景,普通话术场景
  155. //查询关联一级话术数量
  156. $talkskillArr = TalkskillModel::where([['del', '=', 0], ['root_id', '=', $this->rootId], ['approve', '=', 1]])->group('cate1')->column('count(id)','cate1');
  157. //查询成功案例关联一级话术数量
  158. $talkskillSuccessArr = TalkskillSuccess::where([['del', '=', 0], ['root_id', '=', $this->rootId]])->group('cate1')->column('count(id)','cate1');
  159. $cate = [
  160. ['type','=','cate'],
  161. ['root_id','=',$this->rootId],
  162. ['pid','=',0]
  163. ];
  164. $data['scene'] = $data['success_scene'] = [];
  165. $cate = TalkskillCates::where($cate)->field('pid,name,id,scene_type,show')->order('id asc')->select()->toarray();
  166. foreach ($cate as $key => $value) {
  167. if ($value['scene_type']=='chosen' && $value['show']!=1) {
  168. $value['count'] = isset($talkskillSuccessArr[$value['id']]) ? $talkskillSuccessArr[$value['id']] : 0;
  169. $data['success_scene'][] = $value;
  170. }elseif ($value['scene_type']!='chosen') {
  171. $value['count'] = isset($talkskillArr[$value['id']]) ? $talkskillArr[$value['id']] : 0;
  172. $data['scene'][] = $value;
  173. }
  174. }
  175. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  176. }
  177. /*
  178. * 匿名提问
  179. */
  180. public function submitQuestions()
  181. {
  182. $questions = Request::only(['questions'=>'', 'imgs'=>'']);
  183. $data = [
  184. 'title' => trim($questions['questions']),
  185. 'root_id' => $this->rootId,
  186. 'type' => 'submit',
  187. 'employee_id' => $this->employeeId,
  188. 'content' => '',
  189. 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : ''
  190. ];
  191. TalkskillModel::create($data);
  192. return json(['code' => 0, 'msg' => '提交成功']);
  193. }
  194. /*
  195. * 上传话术下拉选框
  196. */
  197. public function addTalkskillSelect()
  198. {
  199. //房屋类型
  200. $houseTypeWhere = [
  201. ['pid','=',0],
  202. ['type','=','house_type'],
  203. ['root_id','=',$this->rootId]
  204. ];
  205. $data['house_type'] = TalkskillCates::where($houseTypeWhere)->field('name,id')->order('id asc')->select()->toarray();
  206. //适用人群列表
  207. $UserCateWhere = [
  208. ['root_id' ,'=', $this->rootId]
  209. ];
  210. $data['user'] = TalkskillUsersCate::where($UserCateWhere)->field('name,id')->select()->toarray();
  211. //普通话术场景
  212. $cate = [
  213. ['type','=','cate'],
  214. ['root_id','=',$this->rootId],
  215. ['pid','=',0],
  216. ['scene_type','=','']
  217. ];
  218. $cate1 = TalkskillCates::where($cate)->order('id desc')->column('id,name');
  219. $cate2 = TalkskillCates::where([['pid','in',array_column($cate1,'id')]])->column('id,pid,name');
  220. $cate3 = [];
  221. foreach ($cate2 as $key => $value) {
  222. $cate3[$value['pid']][] = $value;
  223. }
  224. foreach ($cate1 as $key2 => $value2) {
  225. $cate1[$key2]['child'] = isset($cate3[$value2['id']]) ? $cate3[$value2['id']] : [];
  226. }
  227. $data['scene'] = $cate1;
  228. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  229. }
  230. /*
  231. * 上传话术
  232. */
  233. public function submitTalkskill()
  234. {
  235. $questions = Request::only(['user_cate'=>'','title'=>'', 'content'=>'', 'imgs'=>'','house_type'=>'','cate1'=>'','cate2'=>'']);
  236. $data = [
  237. 'title' => $questions['title'],
  238. 'root_id' => $this->rootId,
  239. 'type' => 'share',
  240. 'employee_id' => $this->employeeId,
  241. 'content' => $questions['content'],
  242. 'imgs' => isset($questions['imgs']) && $questions['imgs'] ? $questions['imgs'] : '',
  243. 'approve' => 0,
  244. 'user_cate' => $questions['user_cate'],
  245. 'house_type' => $questions['house_type'],
  246. 'cate1' => $questions['cate1'],
  247. 'cate2' => $questions['cate2'],
  248. 'imgs' => $questions['imgs']
  249. ];
  250. TalkskillModel::create($data);
  251. return json(['code' => 0, 'msg' => '提交成功']);
  252. }
  253. /*
  254. * 精选话术、成功案例的话术列表
  255. */
  256. public function chosenAndSuccessList()
  257. {
  258. $param = Request::only(['id'=>0, 'page'=>1, 'limit'=>10, 'house_type'=>0, 'user_cate'=>0, 'talkskill_type', 'cate2'=>0,'keyword'=>'']);
  259. if($param['talkskill_type'] == 'chosen'){
  260. if(!$param['cate2']) return json(['code' => 0, 'msg' => '获取成功', 'data' => []]);
  261. }else{
  262. if(!$param['id']) return json(['code' => 0, 'msg' => '获取成功', 'data' => []]);
  263. }
  264. $where = [
  265. ['root_id', '=', $this->rootId],
  266. ['del', '=', 0]
  267. ];
  268. if ($param['keyword']) {
  269. $where1 = array_merge($where,[['title','like','%'.$param['keyword'].'%']]);
  270. $ids1 = ($param['talkskill_type']=='chosen') ? TalkskillChosen::where($where1) : TalkskillSuccess::where($where1);
  271. $ids1 = $ids1->column('id');
  272. $ids2 = TalkskillContent::where([
  273. ['root_id', '=', $this->rootId],
  274. ['talkskill_type', '=', $param['talkskill_type']],
  275. ['content', 'like', '%'.trim($param['keyword']).'%']
  276. ])->group('talkskill_id')->column('talkskill_id');
  277. if(empty($ids1) && empty($ids2)) return json(['code' => 0, 'msg' => '获取成功', 'data' => []]);
  278. $where[] = ['id','in',array_merge($ids1,$ids2)];
  279. }
  280. $where[] = ($param['talkskill_type']=='chosen') ? ['cate2','find in set',$param['cate2']] : ['id', '=', $param['id']];
  281. if(!empty($param['house_type'])) $where[] = ['house_type','find in set',$param['house_type']];
  282. if(!empty($param['user_cate'])) $where[] = ['user_cate','find in set',$param['user_cate']];
  283. $data = ($param['talkskill_type']=='chosen') ? TalkskillChosen::where($where) : TalkskillSuccess::where($where);
  284. $data = $data->field('id,title,view_times,use_count')->page((int)$param['page'], (int)$param['limit'])->order('id desc')->select()->toArray();
  285. $column_id = array_column($data,'id');
  286. $content = TalkskillContent::where([['talkskill_id', 'in', $column_id], ['talkskill_type', '=', $param['talkskill_type']]])->field('talkskill_id,content,type,img')->order('addtime asc')->select()->toArray();
  287. foreach($data as &$item)
  288. {
  289. $item['child'] = [];
  290. foreach($content as $key=>$val)
  291. {
  292. if($val['talkskill_id'] == $item['id'])
  293. {
  294. $val['img'] = $val['img'] ? explode(',',$val['img']) : '';
  295. if(!empty($val['img'])){
  296. foreach($val['img'] as &$img){
  297. $img = 'https://' . config('app.ali_oss_bindurl') . '/' . $img;
  298. }
  299. }
  300. $item['child'][] = $val;
  301. }
  302. }
  303. }
  304. //使用次数增加
  305. $model = $param['talkskill_type'] == 'chosen' ? TalkskillChosen::where([['id', 'in', $column_id]]) : TalkskillSuccess::where([['id', 'in', $column_id]]);
  306. $model->inc('view_times')->update();
  307. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  308. }
  309. /*
  310. * 话术评论
  311. */
  312. public function addComment()
  313. {
  314. $param = Request::only(['content', 'taid']);
  315. $check = TalkskillModel::where([['root_id','=',$this->rootId],['id','=',$param['taid']]])->field('id')->findOrEmpty();
  316. if($check->isEmpty()) return json(['code' => 1, 'msg' => '回答失败','data'=>'回答失败']);
  317. $data = [
  318. 'uid' => $this->uid,
  319. 'taid' => $param['taid'],
  320. 'root_id' => $this->rootId,
  321. 'comments' => $param['content']
  322. ];
  323. TalkskillComment::insert($data);
  324. return json(['code' => 0, 'msg' => '回答成功']);
  325. }
  326. /*
  327. * 话术回答列表
  328. */
  329. public function commentList()
  330. {
  331. $param = Request::only(['taid', 'page']);
  332. $token['root_org'] = $this->rootId;
  333. $token['employee_id'] = $this->employeeId;
  334. $token['uid'] = $this->uid;
  335. $where[] = ['taid', '=', $param['taid']];
  336. $where[] = ['root_id', '=', $token['root_org']];
  337. $data = TalkskillComment::with(['user'])->where(function ($query) use ($token) {
  338. $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
  339. })->where($where)->page((int)$param['page'], 10)->order(['approve' => 'asc', 'use' => 'desc', 'praise' => 'desc'])->select()->toArray();
  340. $column_talkskillcomment_id = array_column($data, 'id');
  341. $column_talkskillcomment_uid = array_column($data, 'uid');
  342. $praise = TalkskillPraise::where([['root_id', '=', $token['root_org']], ['content_id', 'in', $column_talkskillcomment_id], ['user_id', '=', $token['uid']]])->column('user_id', 'content_id');
  343. $employee = Employee::where([['uid', 'in', $column_talkskillcomment_uid], ['root_id', '=', $token['root_org']]])->column('name', 'uid');
  344. $state = Employee::where([['uid', 'in', $column_talkskillcomment_uid], ['root_id', '=', $token['root_org']]])->column('state', 'uid');
  345. foreach ($data as &$val) {
  346. $val['ispraise'] = isset($praise[$val['id']]) ? 1 : 0;
  347. $val['name'] = isset($employee[$val['uid']]) ? $employee[$val['uid']] : '暂无';
  348. $val['state'] = isset($state[$val['uid']]) ? $state[$val['uid']] : '离职';
  349. if (strpos($val['state'], "离职") !== false) {
  350. $val['name'] = '匿名用户';
  351. $val['headimgurl'] = '';
  352. }
  353. }
  354. $count = TalkskillComment::where($where)->where(function ($query) use ($token) {
  355. $query->where('approve=1 or (uid=' . $token['uid'] . ' and approve=0)');
  356. })->count();
  357. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  358. }
  359. /*
  360. * 百问百答详情
  361. */
  362. public function talkskillRead($id)
  363. {
  364. $data = TalkskillModel::where('id', $id)->field('use_count,id,title,content,label,commentCount,imgs,media_id,weixin_media,user_cate,house_type,cate1,cate2')->find();
  365. //是否收藏
  366. $collect = UserCollect::where(['user_id' => $this->uid, 'content_id' => $id, 'content_type' => 'talkskill'])->value('id');
  367. $data['collect'] = $collect ? 1 : 0;
  368. $this->addTalkskillViewLog('talkskill');
  369. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  370. }
  371. /*
  372. * 增加话术浏览记录
  373. */
  374. private function addTalkskillViewLog($type)
  375. {
  376. TalkskillViewLog::create([
  377. 'root_id' => $this->rootId,
  378. 'org_id' => Employee::where('id',$this->employeeId)->value('org_id'),
  379. 'employee_id' => $this->employeeId,
  380. 'type' => $type
  381. ]);
  382. return true;
  383. }
  384. /**
  385. * 点赞
  386. */
  387. public function praise($id)
  388. {
  389. $rs = TalkskillPraise::where(['user_id' => $this->uid, 'root_id' => $this->rootId, 'content_id' => $id])->count();
  390. if ($rs > 0) return json(['code' => 1, 'msg' => '您已点赞']);
  391. TalkskillPraise::create([
  392. 'user_id' => $this->uid,
  393. 'root_id' => $this->rootId,
  394. 'content_id' => $id
  395. ]);
  396. TalkskillComment::where('id', $id)->inc('praise')->update();
  397. return json(['code' => 0, 'msg' => '点赞成功']);
  398. }
  399. /**
  400. * 取消点赞
  401. */
  402. public function praiseCancel($id)
  403. {
  404. $rs = TalkskillPraise::where(['user_id' => $this->uid, 'root_id' => $this->rootId, 'content_id' => $id])->find();
  405. if (!$rs) return json(['code' => 1, 'msg' => '取消点赞失败']);
  406. $rs->delete();
  407. $praise = TalkskillComment::where('id', $id)->value('praise');
  408. if ($praise > 0) {
  409. TalkskillComment::where('id', $id)->dec('praise')->update();
  410. }
  411. return json(['code' => 0, 'msg' => '取消成功']);
  412. }
  413. /*
  414. * 复制话术
  415. * type->use_comment:复制用户的,use_admin:复制后台的
  416. */
  417. public function useTalkskill()
  418. {
  419. $param = Request::only(['type', 'id']);
  420. $token['root_org'] = $this->rootId;
  421. $token['employee_id'] = $this->employeeId;
  422. Db::startTrans();
  423. try {
  424. $end = true;
  425. $type = $param['type'] == 'use_comment' ? 1 : 0;
  426. $where = [];
  427. $where[] = ['type', '=', $type];
  428. $where[] = ['talkskill_id', '=', $param['id']];
  429. $where[] = ['root_id', '=', $token['root_org']];
  430. $where[] = ['employee_id', '=', $token['employee_id']];
  431. $credits_log = CreditsLog::where($where)->select()->toArray();
  432. if (!$credits_log) {
  433. $save['talkskill_id'] = $param['id'];
  434. $save['root_id'] = $token['root_org'];
  435. $save['employee_id'] = $token['employee_id'];
  436. $save['type'] = $type;
  437. $save['addtime'] = date('Y-m-d H:i:s', time());
  438. CreditsLog::insert($save);
  439. //增加使用人次
  440. if ($param['type'] == 'use_comment') {
  441. TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->inc('use')->update();
  442. } else {
  443. TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->inc('use_count')->update();
  444. }
  445. } else {
  446. $end = false;
  447. // Db::commit();
  448. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  449. }
  450. if ($end) {
  451. //复制自己上传的话术或者后台添加的话术或者官方回答或非审核通过,不得积分
  452. if ($param['type'] != 'use_comment') {
  453. $info = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
  454. if ($info->isEmpty() || $info['employee_id'] == 0 || $info['type'] == 'admin' || $info['type'] == 'submit' || $info['employee_id'] == $token['employee_id'] || $info['approve'] != 1) {
  455. $end = false;
  456. // Db::commit();
  457. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  458. }
  459. } else { //复制自己添加的评论不得积分复制没有审核通过的评论不得积分
  460. $info = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->findOrEmpty();
  461. $eid = Employee::where([['id', '=', $token['employee_id']], ['root_id', '=', $token['root_org']]])->findOrEmpty();
  462. if ($info->isEmpty() || $eid->isEmpty() || $info['uid'] == $eid['uid'] || $info['approve'] != 1) {
  463. $end = false;
  464. // Db::commit();
  465. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  466. }
  467. $info['employee_id'] = Employee::where([['uid', '=', $info['uid']], ['root_id', '=', $token['root_org']]])->value('id');
  468. }
  469. }
  470. if ($end) {
  471. //未计算积分的被使用次数
  472. $where[] = ['status', '=', 0];
  473. $credits_log = CreditsLog::where($where)->select()->toArray();
  474. $count = count($credits_log);
  475. if ($count == 0) {
  476. $end = false;
  477. // Db::commit();
  478. // return json(['code' => 0, 'msg' => '复制成功','data'=>$this->get_credits($param)]);
  479. }
  480. }
  481. if ($end) {
  482. //查询规则
  483. if ($param['type'] == 'use_comment') {
  484. $rule = CreditsSetting::where([['root_id', '=', $token['root_org']], ['code', '=', 'talkskill_person2']])->value('value');
  485. $rule = $rule ? json_decode($rule, true) : ['person2' => 1, 'credits2' => 1];
  486. $rule = ['person' => $rule['person2'], 'credits' => $rule['credits2']];
  487. } else {
  488. $rule = CreditsSetting::where([['root_id', '=', $token['root_org']], ['code', '=', 'talkskill_person1']])->value('value');
  489. $rule = $rule ? json_decode($rule, true) : ['person1' => 1, 'credits1' => 1];
  490. $rule = ['person' => $rule['person1'], 'credits' => $rule['credits1']];
  491. }
  492. //计算积分
  493. if ($count >= $rule['person']) {
  494. $credits = new Credits();
  495. $save = [];
  496. $credits->employee_id = $info['employee_id'];
  497. $credits->root_id = $token['root_org'];
  498. $credits->credits = $rule['credits'];
  499. $credits->type = 0;
  500. $credits->json = $param['id'];
  501. $credits->remark = ($param['type'] == 'use_comment') ? '话术评论被使用' : '话术被使用';
  502. $credits->save();
  503. $id = $credits->id;
  504. $c_where[] = ['employee_id', '=', $info['employee_id']];
  505. $c_where[] = ['root_id', '=', $token['root_org']];
  506. $c_where[] = ['type', '=', 0];
  507. $sum = Credits::where($c_where)->sum('credits');
  508. Credits::where([['id', '=', $id]])->update(['sum' => $sum]);
  509. CreditsLog::where([['id', 'in', array_column($credits_log, 'id')]])->update(['status' => 1]);
  510. }
  511. }
  512. Db::commit();
  513. } catch (Exception $e) {
  514. // 回滚事务
  515. Db::rollback();
  516. return json(['code' => 1, 'msg' => $e->getMessage(), 'data' => $this->get_credits($param)]);
  517. }
  518. return json(['code' => 0, 'msg' => '复制成功', 'data' => $this->get_credits($param)]);
  519. }
  520. public function get_credits($param)
  521. {
  522. $token['root_org'] = $this->rootId;
  523. if ($param['type'] == 'use_comment') {
  524. $count = TalkskillComment::where(['id' => $param['id'], 'root_id' => $token['root_org']])->value('use');
  525. } else {
  526. $count = TalkskillModel::where(['id' => $param['id'], 'root_id' => $token['root_org']])->value('use_count');
  527. }
  528. return $count;
  529. }
  530. }