ApiTopController.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: guanxl
  5. * Date: 2018/3/31
  6. * Time: 15:56
  7. */
  8. namespace App\Http\Controllers;
  9. use App\Models\User;
  10. use App\Services\BrainstormingService;
  11. use App\Services\CmbcService;
  12. use App\Services\DepartmentService;
  13. use App\Services\TopService;
  14. use App\Services\UserService;
  15. use Illuminate\Support\Facades\Log;
  16. use Illuminate\Support\Facades\Redis;
  17. class ApiTopController extends Controller
  18. {
  19. public function joinRate()
  20. {
  21. try{
  22. $myDepartmentId = $this->user["department_id"];
  23. $data = TopService::getInstance()->departmentJoinRateTop($this->activityId);
  24. $myRateRank = TopService::getInstance()->departmentJoinRateRank($this->activityId,$myDepartmentId);
  25. $myAvgRank = TopService::getInstance()->departmentKmAvgRank($this->activityId,$myDepartmentId);
  26. $joinCounts = TopService::getInstance()->departmentJoinTop($this->activityId);
  27. $top = [];
  28. foreach ($data as $key => $value) {
  29. $joinCount = isset($joinCounts[$key]) ? $joinCounts[$key] : 0;
  30. $top[] = [
  31. "department_id" => $key,
  32. "department_name" => DepartmentService::getInstance()->getDepartmentName($key),
  33. "join_count" => $joinCount,
  34. "join_rate" => $this->roundRate($value)
  35. ];
  36. }
  37. if(count($top)>1){
  38. foreach ($top as $value){
  39. $arrJoinRate[] = $value["join_rate"];
  40. $arrJoinCount[] = $value["join_count"];
  41. }
  42. array_multisort($arrJoinRate,SORT_DESC,$arrJoinCount,SORT_DESC,$top);
  43. }
  44. $joinCount = isset($joinCounts[$myDepartmentId]) ? $joinCounts[$myDepartmentId] : 0;
  45. $my = [
  46. "index_rate" => $myRateRank["index"],
  47. "index_avg" => $myAvgRank["index"],
  48. "department_id" => $myDepartmentId,
  49. "department_name" => DepartmentService::getInstance()->getDepartmentName($myDepartmentId),
  50. "join_count" => $joinCount,
  51. "join_rate" => $this->roundRate($myRateRank["rate"]),
  52. "avg_knowledge_money" => round($myAvgRank["avg_knowledge_money"], 1)
  53. ];
  54. return response()->json(["error" => 0, "data" => ["top" => $top, "my" => $my]]);
  55. }catch (\Exception $exception){
  56. Log::info("joinRate:exception ".$exception->getMessage());
  57. return response()->json(["error"=>config("errorcode.server_error"),"message"=>config("errmsg.")]);
  58. }
  59. }
  60. public function departmentKmAvg()
  61. {
  62. $myDepartmentId = $this->user["department_id"];
  63. $data = TopService::getInstance()->departmentKmAvgTop($this->activityId);
  64. $myRateRank = TopService::getInstance()->departmentJoinRateRank($this->activityId,$myDepartmentId);
  65. $myAvgRank = TopService::getInstance()->departmentKmAvgRank($this->activityId,$myDepartmentId);
  66. $joinCounts = TopService::getInstance()->departmentJoinTop($this->activityId);
  67. $top = [];
  68. foreach ($data as $key => $value) {
  69. $joinCount = isset($joinCounts[$key]) ? $joinCounts[$key] : 0;
  70. $top[] = [
  71. "department_id" => $key,
  72. "department_name" => DepartmentService::getInstance()->getDepartmentName($key),
  73. "join_count" => $joinCount,
  74. "avg_knowledge_money" => round($value, 1)
  75. ];
  76. }
  77. if(count($top)>1){
  78. foreach ($top as $value){
  79. $arrAvgKm[] = $value["avg_knowledge_money"];
  80. $arrJoinCount[] = $value["join_count"];
  81. }
  82. array_multisort($arrAvgKm,SORT_DESC,$arrJoinCount,SORT_DESC,$top);
  83. }
  84. $joinCount = isset($joinCounts[$myDepartmentId]) ? $joinCounts[$myDepartmentId] : 0;
  85. $my = [
  86. "index_rate" => $myRateRank["index"],
  87. "index_avg" => $myAvgRank["index"],
  88. "department_id" => $myDepartmentId,
  89. "department_name" => DepartmentService::getInstance()->getDepartmentName($myDepartmentId),
  90. "join_count" => $joinCount,
  91. "join_rate" => $this->roundRate($myRateRank["rate"]),
  92. "avg_knowledge_money" => round($myAvgRank["avg_knowledge_money"], 1)
  93. ];
  94. return response()->json(["error" => 0, "data" => ["top" => $top, "my" => $my]]);
  95. }
  96. public function personKm()
  97. {
  98. $brainstorming = BrainstormingService::getInstance()->get($this->activityId);
  99. $data = TopService::getInstance()->personKmTop($this->activityId,100);
  100. $myRank = TopService::getInstance()->personKmRank($this->activityId,$this->userId);
  101. $top = [];
  102. foreach ($data as $key => $value) {
  103. $top[] = [
  104. "user_id" => $key,
  105. "name" => self::getName($brainstorming,$value["name"]),
  106. "headpic_url" => $value["avatar"],
  107. "department_id" => $value["department_id"],
  108. "department_name" => DepartmentService::getInstance()->getDepartmentName($value["department_id"]),
  109. "knowledge_money" => $value["knowledge_money"]
  110. ];
  111. }
  112. $my = [
  113. "index" => $myRank,
  114. "user_id" => $this->user["user_id"],
  115. "name" => $this->user["name"],
  116. "headpic_url" => $this->user["avatar"],
  117. "department_id" => $this->user["department_id"],
  118. "department_name" => $this->user["department_name"],
  119. "knowledge_money" => $this->user["knowledge_money"]
  120. ];
  121. $requirementIndex = isset($brainstorming["top_prize_num"])? intval($brainstorming["top_prize_num"]):1000;
  122. $prizeKnowledgeMoney = TopService::getInstance()->prizeKmRank($this->activityId,$requirementIndex);
  123. $prizeRequirement = [
  124. "index" => $requirementIndex,
  125. "knowledge_money" => $prizeKnowledgeMoney
  126. ];
  127. return response()->json(["error" => 0, "data" => ["top" => $top, "my" => $my,"prize_requirement" => $prizeRequirement]]);
  128. }
  129. public function personDepartmentKm()
  130. {
  131. $brainstorming = BrainstormingService::getInstance()->get($this->activityId);
  132. $myDepartmentId = $this->user["department_id"];
  133. $data = TopService::getInstance()->personKmDepartmentTop($this->activityId,$myDepartmentId, 100);
  134. $myRank = TopService::getInstance()->personKmDepartmentRank($this->activityId,$this->userId, $myDepartmentId);
  135. $departmentName = DepartmentService::getInstance()->getDepartmentName($myDepartmentId);
  136. $top = [];
  137. foreach ($data as $key => $value) {
  138. $top[] = [
  139. "user_id" => $key,
  140. "name" => self::getName($brainstorming,$value["name"]),
  141. "headpic_url" => $value["avatar"],
  142. "department_id" => $value["department_id"],
  143. "department_name" => $departmentName,
  144. "knowledge_money" => $value["knowledge_money"]
  145. ];
  146. }
  147. $my = [
  148. "index" => $myRank,
  149. "user_id" => $this->user["user_id"],
  150. "name" => $this->user["name"],
  151. "headpic_url" => $this->user["avatar"],
  152. "department_id" => $this->user["department_id"],
  153. "department_name" => $this->user["department_name"],
  154. "knowledge_money" => $this->user["knowledge_money"]
  155. ];
  156. return response()->json(["error" => 0, "data" => ["top" => $top, "my" => $my]]);
  157. }
  158. /**
  159. * 炫耀
  160. * @return \Illuminate\Http\JsonResponse
  161. */
  162. public function mark()
  163. {
  164. $myDepartmentId = $this->user["department_id"];
  165. $myRank = TopService::getInstance()->personKmRank($this->activityId,$this->userId);
  166. $myDepartmentRank = TopService::getInstance()->personKmDepartmentRank($this->activityId,$this->userId, $myDepartmentId);
  167. $userCount = User::where("activity_id",$this->activityId)
  168. ->where("status",1)->count();
  169. $departmentCount = User::where("activity_id",$this->activityId)
  170. ->where("department_id",$myDepartmentId)
  171. ->where("status",1)->count();
  172. $pkWin = intval($this->user["pk_win_count"]);
  173. $pkLost = intval($this->user["pk_lost_count"]);
  174. $pkDraw = intval($this->user["pk_draw_count"]);
  175. $pkCount = $pkWin + $pkLost + $pkDraw;
  176. $pkWinRate = 0;
  177. if ($pkWin > 0 && $pkCount > 0) {
  178. $pkWinRate = round(($pkWin/$pkCount)*100,2);
  179. }
  180. $topWinRate=0;
  181. $topWinDepartmentRate = 0;
  182. if($userCount>0){
  183. $topWinRate = $myRank==1? 100:$this->roundRate((1-$myRank/$userCount)*100);
  184. }
  185. if($departmentCount>0){
  186. $topWinDepartmentRate = $myDepartmentRank==1? 100:$this->roundRate((1-$myDepartmentRank/$departmentCount)*100);
  187. }
  188. $activityInfo = BrainstormingService::getInstance()->get($this->activityId);
  189. $levelStarConfig = json_decode($activityInfo['level_star_config'], true);
  190. $data = [
  191. "index_all" => $myRank,
  192. "user_id" => $this->user["user_id"],
  193. "name" => $this->user["name"],
  194. "headpic_url" => $this->user["name"],
  195. "department_id" => $this->user["department_id"],
  196. "department_name" => DepartmentService::getInstance()->getDepartmentName($myDepartmentId),
  197. "knowledge_money" => $this->user["knowledge_money"],
  198. "index_department" => $myDepartmentRank,
  199. "top_win_rate" => $topWinRate,
  200. "top_win_department_rate" => $topWinDepartmentRate,
  201. "pk_count" => $pkCount,
  202. "pk_win" => $pkWin,
  203. "pk_lost" => $pkLost,
  204. "pk_draw" => $pkDraw,
  205. "pk_win_rate" => $pkWinRate,
  206. "user_level" => intval($this->user['user_level']),
  207. "user_star" => intval($this->user['user_star']),
  208. "win_continuity" => intval($this->user['win_continuity']),
  209. "user_level_name" => $levelStarConfig[$this->user['user_level']]['name'],
  210. "level_need_star" => intval($levelStarConfig[$this->user['user_level']]['star']),
  211. "index" => intval(TopService::getInstance()->personPkRank($this->activityId, $this->userId)),
  212. ];
  213. return response()->json(["error" => 0, "data" => $data]);
  214. }
  215. protected function roundRate($rate)
  216. {
  217. if(is_int($rate)){
  218. return $rate;
  219. }else{
  220. if ($rate > 10) {
  221. return substr(sprintf("%.3f",$rate),0,-1);
  222. } else {
  223. return substr(sprintf("%.2f",$rate),0,-1);
  224. }
  225. }
  226. }
  227. /**
  228. * @param $activity
  229. * @param $name
  230. * @return string
  231. */
  232. function getName($activity,$name){
  233. if($activity["client_type"]=="yunzhijia"){
  234. return self::hidestr($name,1,1,1);
  235. }else{
  236. return $name;
  237. }
  238. }
  239. /**
  240. +----------------------------------------------------------
  241. * 将一个字符串部分字符用*替代隐藏
  242. +----------------------------------------------------------
  243. * @param string $string 待转换的字符串
  244. * @param int $bengin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度
  245. * @param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度
  246. * @param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串
  247. * @param string $glue 分割符
  248. +----------------------------------------------------------
  249. * @return string 处理后的字符串
  250. +----------------------------------------------------------
  251. */
  252. function hideStr($string, $bengin=0, $len = 4, $type = 0, $glue = "@") {
  253. if (empty($string))
  254. return false;
  255. $array = array();
  256. if ($type == 0 || $type == 1 || $type == 4) {
  257. $strlen = $length = mb_strlen($string);
  258. while ($strlen) {
  259. $array[] = mb_substr($string, 0, 1, "utf8");
  260. $string = mb_substr($string, 1, $strlen, "utf8");
  261. $strlen = mb_strlen($string);
  262. }
  263. }
  264. if ($type == 0) {
  265. for ($i = $bengin; $i < ($bengin + $len); $i++) {
  266. if (isset($array[$i]))
  267. $array[$i] = "*";
  268. }
  269. $string = implode("", $array);
  270. }else if ($type == 1) {
  271. $array = array_reverse($array);
  272. for ($i = $bengin; $i < ($bengin + $len); $i++) {
  273. if (isset($array[$i]))
  274. $array[$i] = "*";
  275. }
  276. $string = implode("", array_reverse($array));
  277. }else if ($type == 2) {
  278. $array = explode($glue, $string);
  279. $array[0] = self::hideStr($array[0], $bengin, $len, 1);
  280. $string = implode($glue, $array);
  281. } else if ($type == 3) {
  282. $array = explode($glue, $string);
  283. $array[1] = self::hideStr($array[1], $bengin, $len, 0);
  284. $string = implode($glue, $array);
  285. } else if ($type == 4) {
  286. $left = $bengin;
  287. $right = $len;
  288. $tem = array();
  289. for ($i = 0; $i < ($length - $right); $i++) {
  290. if (isset($array[$i]))
  291. $tem[] = $i >= $left ? "*" : $array[$i];
  292. }
  293. $array = array_chunk(array_reverse($array), $right);
  294. $array = array_reverse($array[0]);
  295. for ($i = 0; $i < $right; $i++) {
  296. $tem[] = $array[$i];
  297. }
  298. $string = implode("", $tem);
  299. }
  300. return $string;
  301. }
  302. }