1
0

Company.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\Customer;
  4. use app\model\CustomerClue;
  5. use app\model\Employee;
  6. use app\model\Org;
  7. use app\model\User;
  8. class Company extends Base
  9. {
  10. /*
  11. * 公司员工列表
  12. * screened:company企业,team团队
  13. */
  14. public function employee()
  15. {
  16. $param = $this->request->only(['date', 'type', 'page', 'screened']);
  17. $token = $this->request->token;
  18. //选取时间月初和月末时间
  19. $start_time = $param['date'] . '-01 00:00:00';
  20. $end_time = date('Y-m-d H:i:s', strtotime('+1 months', strtotime($start_time)));
  21. $where = [['state', '=', '在职'], ['uid', '<>', 0]];
  22. if ($param['screened'] == 'company') {
  23. $where[] = ['root_id', '=', $token['root_org']];
  24. } else {
  25. $where[] = ['root_id', '=', $token['root_org']];
  26. $org_id = orgSubIds($token['org_id']);
  27. $where[] = ['org_id', 'in', $org_id];
  28. }
  29. $searchObj = Employee::with(['user' => function ($query) {
  30. $query->field('id, headimgurl');
  31. }])->field('id,uid,name')->where($where);
  32. if ($param['type'] == 'ding') {
  33. $chaos = Customer::changeState('签单', 'chaos');
  34. $searchObj->withCount([
  35. 'customer' => function ($query) use ($start_time, $end_time, $chaos) {
  36. $query->where([['addtime', 'between', [$start_time, $end_time]], ['state', 'in', $chaos]]);
  37. }
  38. ]);
  39. $field = 'customer_count';
  40. $gdp = 'userdingGdp';
  41. $totalType = 'total_customer';
  42. } elseif ($param['type'] == 'ke') {
  43. $searchObj->withCount([
  44. 'clue' => function ($query) use ($start_time, $end_time) {
  45. $query->where([['addtime', 'between', [$start_time, $end_time]]]);
  46. }
  47. ]);
  48. $field = 'clue_count';
  49. $gdp = 'userFanGdp';
  50. $totalType = 'total_clue';
  51. } elseif ($param['type'] == 'xuefen') {
  52. $searchObj->withSum(['studycredit' => function ($query) use ($start_time, $end_time) {
  53. $query->where([['addtime', 'between', [$start_time, $end_time]]]);
  54. }], 'credits');
  55. $field = 'studycredit_sum';
  56. $gdp = 'usersdyctGdp';
  57. $totalType = 'total_studycredit';
  58. }else {
  59. $searchObj->withSum(['credits' => function ($query) use ($start_time, $end_time) {
  60. $query->where([['addtime', 'between', [$start_time, $end_time]], ['type', '=', 1]]);
  61. }], 'credits');
  62. $field = 'credits_sum';
  63. $gdp = 'userdcreditsGdp';
  64. $totalType = 'total_credits';
  65. }
  66. $data = $searchObj->cache(60)->select()->toArray();
  67. array_multisort(array_column($data, $field), SORT_DESC, $data);
  68. // 统计总数
  69. $count = array_sum(array_column($data, $field));
  70. // 获取我的数据与排名
  71. $found_arr = array_column($data, 'id');
  72. $key = array_search($token['employee_id'], $found_arr);
  73. $myRanking = $data[$key];
  74. $myRanking['ranking'] = $key + 1;
  75. // 获取我的占比
  76. $myRanking[$gdp] = $count == 0 ? 0 : floor($myRanking[$field] / $count * 100);
  77. // 总人数
  78. $myRanking['count'] = count($data);
  79. $myRanking[$totalType] = $count;
  80. $data = array_slice($data, ($param['page'] - 1) * 10, 10);
  81. if ($param['type'] !== 'ke' && $param['type'] !== 'ding') {
  82. if($param['type']== 'xuefen'){
  83. foreach ($data as $k => $v) {
  84. $data[$k]['studycredit_max'] = $data[$k]['studycredit_sum'];
  85. }
  86. }else{
  87. foreach ($data as $k => $v) {
  88. $data[$k]['credits_max'] = $data[$k]['credits_sum'];
  89. }
  90. }
  91. }
  92. $companyName = Org::where(['id' => $token['root_org']])->value('name');
  93. return json(['code' => self::success, 'msg' => '获取成功', 'company_ranking_list' => $data, 'my_ranking' => $myRanking, 'company' => $companyName]);
  94. }
  95. }