KfjkLogic.php 10 KB

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