KfjkLogic.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/7/6
  6. * Time: 16:59
  7. */
  8. namespace App\common;
  9. use app\admin\model\Kfonline as KfonlineModel;
  10. use app\admin\model\Users;
  11. use think\Db;
  12. /**
  13. * [
  14. * 用户名:
  15. * 会话量:今日客服累计的的会话量 hyl
  16. * 接待量:当前客服正在接待的会话数 jdl
  17. * 平均会话时长:今日客服的平均会话时长,即访客进入到会话结束时时间间隔的平均值 pjhusc
  18. * 参评率:今日客服的累计参评率,即参加评价的数量和接入会话数量的比值 cpl
  19. * 满意度率:今日客服的累计满意度率,即满意的数量和评价会话数量的比值 mydl
  20. * 平均响应时长:今日客服的平均响应时长,即访客访问和客服回复时间间隔的平均值。 pjxysc
  21. * 今日休息时长:今日客服的休息时长的累计值 jrxxsc
  22. * ]
  23. */
  24. class KfjkLogic
  25. {
  26. public function getAllData($kfuidarray)
  27. {
  28. if (empty($kfuidarray)) {
  29. return false;
  30. }
  31. $todayall = $this->getTodayServiceData($kfuidarray);
  32. if (!$todayall) {
  33. return false;
  34. }
  35. $kfuidarray_index = kftoKey($kfuidarray, 1);
  36. $uids = $kfuidarray;
  37. $uidsname = $this->uidName($kfuidarray);
  38. $hyl = $this->hyl($kfuidarray, $todayall);
  39. $jdl = $this->jdl($kfuidarray, $todayall);
  40. $pjhysc = $this->pjhusc($kfuidarray, $todayall);
  41. $cpl = $this->cpl($kfuidarray, $todayall);
  42. $mydl = $this->mydl($kfuidarray, $todayall);
  43. $pjxysc = $this->pjxysc($kfuidarray, $todayall);
  44. $jrxxsc = $this->jrxxsc($kfuidarray, $todayall);
  45. foreach ($kfuidarray_index as $uid => $val) {
  46. $kfuidarray_index[$uid]['uid'] = $uids[$uid];
  47. $kfuidarray_index[$uid]['uidname'] = $uidsname[$uid];
  48. $kfuidarray_index[$uid]['hhl'] = $hyl[$uid];
  49. $kfuidarray_index[$uid]['jdl'] = $jdl[$uid];
  50. $kfuidarray_index[$uid]['pjhysc'] = $pjhysc[$uid];
  51. $kfuidarray_index[$uid]['cpl'] = $cpl[$uid];
  52. $kfuidarray_index[$uid]['mydl'] = $mydl[$uid];
  53. $kfuidarray_index[$uid]['pjxysc'] = $pjxysc[$uid];
  54. $kfuidarray_index[$uid]['jrxxsc'] = $jrxxsc[$uid];
  55. $kfuidarray_index[$uid]['fxx'] = $this->makeBtn($uid);
  56. }
  57. $last = [];
  58. foreach ($kfuidarray_index as $val) {
  59. $last[] = $val;
  60. }
  61. return $last;
  62. }
  63. // 生成按钮
  64. private function makeBtn($id)
  65. {
  66. $operate = '<a href="javascript:msg(' . $id . ')"><button type="button" class="btn btn-info btn-sm">';
  67. $operate .= '发消息</button></a> ';
  68. return $operate;
  69. }
  70. //按条件获取用户数组 组ID, 在线状态 0为全部
  71. public function getValiKF($groupid, $onlinestatus)
  72. {
  73. $groupusers = (new Users)->getByGroup($groupid);
  74. $groupuserArray = [];
  75. if ($groupusers) {
  76. foreach ($groupusers as $val) {
  77. $groupuserArray[$val['id']] = $val['id'];
  78. }
  79. }
  80. $statusArr = (new KfonlineModel())->getOnlineAll($onlinestatus);
  81. $return = array_intersect($groupuserArray, $statusArr);
  82. return $return;
  83. }
  84. public function uidName($uidArray){
  85. $users = (new Users)->where(['id'=>['IN',$uidArray]])->select();
  86. $return = kftoKey($uidArray);
  87. foreach ($return as $uid=>$ttttt){
  88. foreach ($users as $nowuser){
  89. if ($uid == $nowuser['id']){
  90. $return[$uid] = $nowuser['user_name'];
  91. }
  92. }
  93. }
  94. return $return ;
  95. }
  96. //会话量 统计
  97. public function hyl($uidArray, $alllogs)
  98. {
  99. $rets = kftoKey($uidArray);
  100. foreach ($rets as $uid => $aaaa) {
  101. foreach ($alllogs as $log) {
  102. if ($uid == $log['kf_id'] && ($log['status'] == 1 || $log['status'] == 3)) {
  103. $rets[$uid]++;
  104. }
  105. }
  106. }
  107. return $rets;
  108. }
  109. //接待量 统计
  110. public function jdl($uidArray, $alllogs)
  111. {
  112. $rets = kftoKey($uidArray);
  113. foreach ($rets as $uid => $aaa) {
  114. foreach ($alllogs as $log) {
  115. if ($uid == $log['kf_id']) {
  116. $rets[$uid]++;
  117. }
  118. }
  119. }
  120. return $rets;
  121. }
  122. //平均会话时长 统计
  123. public function pjhusc($uidArray, $alllogs)
  124. {
  125. $rets = kftoKey($uidArray, 1);
  126. foreach ($rets as $uid => $aaa) {
  127. foreach ($alllogs as $log) {
  128. if ($uid == $log['kf_id']) {
  129. (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
  130. (!isset($rets[$uid]['times'])) ? ($rets[$uid]['times'] = 0) : ($rets[$uid]['times'] += (($log['end_time'] ? $log['end_time'] : $log['start_time']) - $log['start_time']));
  131. }
  132. }
  133. }
  134. $return = [];
  135. foreach ($rets as $uid => $val) {
  136. if (isset($rets[$uid]['count'])) {
  137. $return[$uid] = intval($rets[$uid]['times'] / $rets[$uid]['count']);
  138. } else {
  139. $return[$uid] = 0;
  140. }
  141. }
  142. return $return;
  143. }
  144. //参评率
  145. public function cpl($uidArray, $alllogs)
  146. {
  147. $rets = kftoKey($uidArray, 1);
  148. foreach ($rets as $uid => $aaa) {
  149. foreach ($alllogs as $log) {
  150. if ($uid == $log['kf_id']) {
  151. !(isset($rets[$uid]['count'])) ? 1 : $rets[$uid]['count']++;
  152. isset($rets[$uid]['pl']) ? ($rets[$uid]['pl'] = ($log['evaluate_id'] > 0 ? 1 : 0)) : 0;
  153. }
  154. }
  155. }
  156. $return = [];
  157. foreach ($rets as $uid => $val) {
  158. if (isset($rets[$uid]['count'])) {
  159. $return[$uid] = $return[$uid]['pl'] / $return[$uid]['cocountunt'];
  160. } else {
  161. $return[$uid] = 0;
  162. }
  163. }
  164. return $return;
  165. }
  166. //满意度率
  167. public function mydl($uidArray, $alllogs)
  168. {
  169. $rets = kftoKey($uidArray, 1);
  170. foreach ($rets as $uid => $aaa) {
  171. foreach ($alllogs as $log) {
  172. if ($uid == $log['kf_id'] && $log['evaluate_id'] > 0) {
  173. (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
  174. (!isset($rets[$uid]['times'])) ? (($log['evaluate_id'] == 1) ? 1 : 0) : (($log['evaluate_id'] == 1) ? $rets[$uid]['times']++ : '');
  175. }
  176. }
  177. }
  178. $return = [];
  179. foreach ($rets as $uid => $val) {
  180. if (isset($rets[$uid]['count']) && isset($rets[$uid]['times'])) {
  181. $return[$uid] = $rets[$uid]['times'] / $rets[$uid]['count'];
  182. } else {
  183. $return[$uid] = 0;
  184. }
  185. }
  186. return $return;
  187. }
  188. //平均响应时长
  189. public function pjxysc($uidArray, $alllogs)
  190. {
  191. $rets = kftoKey($uidArray);
  192. if (empty($alllogs)){
  193. return $rets ;
  194. }
  195. $serids =[] ;
  196. foreach ($alllogs as $val){
  197. $serids[] = $val['servicelog_id'];
  198. }
  199. $times = Db::name('alarm')->field('alarm_corresponding,servicelog_id')->where(['alarm_respond' => 2,'servicelog_id'=>['IN',$serids]])->select();
  200. if ($times){
  201. foreach ($alllogs as $key=>$val){
  202. foreach ($times as $sval){
  203. if ($val['servicelog_id'] == $sval['servicelog_id']){
  204. $alllogs[$key]['corresponding'] = $sval['alarm_corresponding'];
  205. }
  206. }
  207. }
  208. }
  209. $rets = kftoKey($uidArray, 1);
  210. foreach ($rets as $uid => $aaa) {
  211. foreach ($alllogs as $log) {
  212. if ($uid == $log['kf_id'] && isset($log['corresponding'])) {
  213. (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
  214. (!isset($rets[$uid]['times'])) ? ($rets[$uid]['times'] = intval($log['corresponding'])) : ($rets[$uid]['times'] += intval($log['corresponding']));
  215. }
  216. }
  217. }
  218. $return = [];
  219. foreach ($rets as $uid => $val) {
  220. if (isset($rets[$uid]['count']) && isset($rets[$uid]['times'])) {
  221. $return[$uid] = intval($rets[$uid]['times'] / $rets[$uid]['count']);
  222. } else {
  223. $return[$uid] = 0;
  224. }
  225. }
  226. return $return;
  227. }
  228. //今日休息时长统计
  229. public function jrxxsc($uidArray, $alllogs)
  230. {
  231. $kfuidarray = array_map(function($i){return 'KF'.$i; },$uidArray);
  232. $return = kftoKey($uidArray);
  233. $rets = kftoKey($uidArray, 1);
  234. $today =date("Y-m-d");
  235. $ret = Db::name('kfstatetimes')->field('kfuid,stime')->where(['sday'=>$today,'kfuid'=>['IN',$kfuidarray],'kstatus'=>3])->select();
  236. if (!$ret){
  237. return $return;
  238. }
  239. foreach ($return as $uid=>$ttttt){
  240. foreach ($ret as $val){
  241. $ruid = trim($val['kfuid'],'KF');
  242. if ($uid == $ruid){
  243. $return[$uid] = $val['stime'];
  244. }
  245. }
  246. }
  247. return $return ;
  248. }
  249. public function getTodayServiceData($kfuidArray = [])
  250. {
  251. $today_begin = strtotime(date("Y-m-d"));
  252. $today_end = $today_begin + 86400;
  253. $filds = "servicelog_id,group_id,kf_id,user_id,status,start_time,intime,end_time,evaluate_id";
  254. if ($kfuidArray) {
  255. $ret = Db::name('service_log')->field($filds)->where(['start_time' => ['>=', $today_begin], 'kf_id' => ['IN', $kfuidArray]])->where(['start_time' => ['<', $today_end]])->select();
  256. } else {
  257. $ret = Db::name('service_log')->field($filds)->where(['start_time' => ['>=', $today_begin]])->where(['start_time' => ['<', $today_end]])->select();
  258. }
  259. return $ret;
  260. }
  261. }