KfjkLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. $kfip = $this->getKfinline();
  46. foreach ($kfuidarray_index as $uid => $val) {
  47. $kfuidarray_index[$uid]['ip'] = $kfip['KF' . $uid]['ip'];
  48. $kfuidarray_index[$uid]['status'] = $this->getStatusName($kfip['KF' . $uid]['status']);
  49. $kfuidarray_index[$uid]['uid'] = $uids[$uid];
  50. $kfuidarray_index[$uid]['uidname'] = $uidsname[$uid];
  51. $kfuidarray_index[$uid]['hhl'] = $hyl[$uid];
  52. $kfuidarray_index[$uid]['jdl'] = $jdl[$uid];
  53. $kfuidarray_index[$uid]['pjhysc'] = $pjhysc[$uid];
  54. $kfuidarray_index[$uid]['cpl'] = $cpl[$uid];
  55. $kfuidarray_index[$uid]['mydl'] = $mydl[$uid];
  56. $kfuidarray_index[$uid]['pjxysc'] = $pjxysc[$uid];
  57. $kfuidarray_index[$uid]['jrxxsc'] = $jrxxsc[$uid];
  58. $kfuidarray_index[$uid]['fxx'] = $this->makeBtn($uid);
  59. }
  60. $last = [];
  61. foreach ($kfuidarray_index as $val) {
  62. $last[] = $val;
  63. }
  64. return $last;
  65. }
  66. public function getStatusName($status)
  67. {
  68. $r = ['0' => '离线', '1' => '在线', '2' => '隐身', '3' => '休息'];
  69. return isset($r[$status]) ? $r[$status] : 'Error';
  70. }
  71. // 生成按钮
  72. private function makeBtn($id)
  73. {
  74. $operate = '<a href="javascript:msg(' . $id . ')"><button type="button" class="btn btn-info btn-sm">';
  75. $operate .= '发消息</button></a> ';
  76. return $operate;
  77. }
  78. //按条件获取用户数组 组ID, 在线状态 0为全部
  79. public function getValiKF($groupid, $onlinestatus)
  80. {
  81. $groupusers = (new Users)->getByGroup($groupid);
  82. $groupuserArray = [];
  83. if ($groupusers) {
  84. foreach ($groupusers as $val) {
  85. $groupuserArray[$val['id']] = $val['id'];
  86. }
  87. }
  88. $statusArr = (new KfonlineModel())->getOnlineAll($onlinestatus);
  89. $return = array_intersect($groupuserArray, $statusArr);
  90. return $return;
  91. }
  92. public function uidName($uidArray)
  93. {
  94. $users = (new Users)->where(['id' => ['IN', $uidArray]])->select();
  95. $return = kftoKey($uidArray);
  96. foreach ($return as $uid => $ttttt) {
  97. foreach ($users as $nowuser) {
  98. if ($uid == $nowuser['id']) {
  99. $return[$uid] = $nowuser['user_name'];
  100. }
  101. }
  102. }
  103. return $return;
  104. }
  105. //会话量 统计
  106. public function hyl($uidArray, $alllogs)
  107. {
  108. $rets = kftoKey($uidArray);
  109. foreach ($rets as $uid => $aaaa) {
  110. foreach ($alllogs as $log) {
  111. if ($uid == $log['kf_id'] && ($log['status'] == 1 || $log['status'] == 3)) {
  112. $rets[$uid]++;
  113. }
  114. }
  115. }
  116. return $rets;
  117. }
  118. //接待量 统计
  119. public function jdl($uidArray, $alllogs)
  120. {
  121. $rets = kftoKey($uidArray);
  122. foreach ($rets as $uid => $aaa) {
  123. foreach ($alllogs as $log) {
  124. if ($uid == $log['kf_id']) {
  125. $rets[$uid]++;
  126. }
  127. }
  128. }
  129. return $rets;
  130. }
  131. //平均会话时长 统计
  132. public function pjhusc($uidArray, $alllogs)
  133. {
  134. $rets = kftoKey($uidArray, 1);
  135. foreach ($rets as $uid => $aaa) {
  136. foreach ($alllogs as $log) {
  137. if ($uid == $log['kf_id']) {
  138. (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
  139. (!isset($rets[$uid]['times'])) ? ($rets[$uid]['times'] = 0) : ($rets[$uid]['times'] += (($log['end_time'] ? $log['end_time'] : $log['start_time']) - $log['start_time']));
  140. }
  141. }
  142. }
  143. $return = [];
  144. foreach ($rets as $uid => $val) {
  145. if (isset($rets[$uid]['count'])) {
  146. $return[$uid] = intval($rets[$uid]['times'] / $rets[$uid]['count']);
  147. } else {
  148. $return[$uid] = 0;
  149. }
  150. }
  151. return $return;
  152. }
  153. //参评率
  154. public function cpl($uidArray, $alllogs)
  155. {
  156. $rets = kftoKey($uidArray, 1);
  157. foreach ($rets as $uid => $aaa) {
  158. foreach ($alllogs as $log) {
  159. if ($uid == $log['kf_id']) {
  160. !(isset($rets[$uid]['count'])) ? 1 : $rets[$uid]['count']++;
  161. isset($rets[$uid]['pl']) ? ($rets[$uid]['pl'] = ($log['evaluate_id'] > 0 ? 1 : 0)) : 0;
  162. }
  163. }
  164. }
  165. $return = [];
  166. foreach ($rets as $uid => $val) {
  167. if (isset($rets[$uid]['count'])) {
  168. $return[$uid] = $return[$uid]['pl'] / $return[$uid]['cocountunt'];
  169. } else {
  170. $return[$uid] = 0;
  171. }
  172. }
  173. return $return;
  174. }
  175. //满意度率
  176. public function mydl($uidArray, $alllogs)
  177. {
  178. $rets = kftoKey($uidArray, 1);
  179. foreach ($rets as $uid => $aaa) {
  180. foreach ($alllogs as $log) {
  181. if ($uid == $log['kf_id'] && $log['evaluate_id'] > 0) {
  182. (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
  183. (!isset($rets[$uid]['times'])) ? (($log['evaluate_id'] == 1) ? 1 : 0) : (($log['evaluate_id'] == 1) ? $rets[$uid]['times']++ : '');
  184. }
  185. }
  186. }
  187. $return = [];
  188. foreach ($rets as $uid => $val) {
  189. if (isset($rets[$uid]['count']) && isset($rets[$uid]['times'])) {
  190. $return[$uid] = $rets[$uid]['times'] / $rets[$uid]['count'];
  191. } else {
  192. $return[$uid] = 0;
  193. }
  194. }
  195. return $return;
  196. }
  197. //平均响应时长
  198. public function pjxysc($uidArray, $alllogs)
  199. {
  200. $rets = kftoKey($uidArray);
  201. if (empty($alllogs)) {
  202. return $rets;
  203. }
  204. $serids = [];
  205. foreach ($alllogs as $val) {
  206. $serids[] = $val['servicelog_id'];
  207. }
  208. $times = Db::name('alarm')->field('alarm_corresponding,servicelog_id')->where(['alarm_respond' => 2, 'servicelog_id' => ['IN', $serids]])->select();
  209. if ($times) {
  210. foreach ($alllogs as $key => $val) {
  211. foreach ($times as $sval) {
  212. if ($val['servicelog_id'] == $sval['servicelog_id']) {
  213. $alllogs[$key]['corresponding'] = $sval['alarm_corresponding'];
  214. }
  215. }
  216. }
  217. }
  218. $rets = kftoKey($uidArray, 1);
  219. foreach ($rets as $uid => $aaa) {
  220. foreach ($alllogs as $log) {
  221. if ($uid == $log['kf_id'] && isset($log['corresponding'])) {
  222. (!isset($rets[$uid]['count'])) ? ($rets[$uid]['count'] = 1) : $rets[$uid]['count']++;
  223. (!isset($rets[$uid]['times'])) ? ($rets[$uid]['times'] = intval($log['corresponding'])) : ($rets[$uid]['times'] += intval($log['corresponding']));
  224. }
  225. }
  226. }
  227. $return = [];
  228. foreach ($rets as $uid => $val) {
  229. if (isset($rets[$uid]['count']) && isset($rets[$uid]['times'])) {
  230. $return[$uid] = intval($rets[$uid]['times'] / $rets[$uid]['count']);
  231. } else {
  232. $return[$uid] = 0;
  233. }
  234. }
  235. return $return;
  236. }
  237. //今日休息时长统计
  238. public function jrxxsc($uidArray, $alllogs)
  239. {
  240. $kfuidarray = array_map(function ($i) {
  241. return 'KF' . $i;
  242. }, $uidArray);
  243. $return = kftoKey($uidArray);
  244. $rets = kftoKey($uidArray, 1);
  245. $today = date("Y-m-d");
  246. $ret = Db::name('kfstatetimes')->field('kfuid,stime')->where(['sday' => $today, 'kfuid' => ['IN', $kfuidarray], 'kstatus' => 3])->select();
  247. if (!$ret) {
  248. return $return;
  249. }
  250. foreach ($return as $uid => $ttttt) {
  251. foreach ($ret as $val) {
  252. $ruid = trim($val['kfuid'], 'KF');
  253. if ($uid == $ruid) {
  254. $return[$uid] = $val['stime'];
  255. }
  256. }
  257. }
  258. return $return;
  259. }
  260. public function getTodayServiceData($kfuidArray = [])
  261. {
  262. $today_begin = strtotime(date("Y-m-d"));
  263. $today_end = $today_begin + 86400;
  264. $filds = "servicelog_id,group_id,kf_id,user_id,status,start_time,intime,end_time,evaluate_id";
  265. if ($kfuidArray) {
  266. $ret = Db::name('service_log')->field($filds)->where(['start_time' => ['>=', $today_begin], 'kf_id' => ['IN', $kfuidArray]])->where(['start_time' => ['<', $today_end]])->select();
  267. } else {
  268. $ret = Db::name('service_log')->field($filds)->where(['start_time' => ['>=', $today_begin]])->where(['start_time' => ['<', $today_end]])->select();
  269. }
  270. return $ret;
  271. }
  272. //获取在线客服ip地址
  273. public function getKfinline()
  274. {
  275. $ret = Db::name('kfonline')->select();
  276. $return = [];
  277. if ($ret) {
  278. foreach ($ret as $item) {
  279. //'KF22'=>'192.168.1.1';
  280. $return[$item['uid']] = $item;
  281. }
  282. }
  283. return $return;
  284. }
  285. }