123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405 |
- <?php
- namespace app\api\controller;
- use app\model\Employee;
- use app\model\Org;
- use app\model\WechatActivity as Model;
- use app\model\WechatActivityCourse;
- use app\model\WechatActivityIntegral;
- use app\model\User;
- use app\model\WechatActivityTeam;
- use app\model\CustomerVisitLog;
- /**
- * 微群爆破返回数据
- * @package app\api\controller
- */
- class WechatActivity {
- private $root_id = 0;
- private $activity_id = 0;
- public function __construct()
- {
- $ticket = input('ticket', '', 'trim');
- $activity = Model::where('ticket', '=', $ticket)->find();
- if (empty($activity)) {
- return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
- }
- $this->activity_id = $activity->id;
- $this->root_id = $activity->root_id;
- }
- /**
- * 获取展示模块
- */
- public function showModule() {
- $activity = Model::find($this->activity_id);
- $show = [
- ['name'=> '加微数据', 'value'=> 1, 'checked'=> true],
- ['name'=> '见面数据', 'value'=> 2, 'checked'=> true],
- ['name'=> '到店数据', 'value'=> 3, 'checked'=> true],
- ['name'=> '量房数据', 'value'=> 4, 'checked'=> true],
- ['name'=> '到场数据', 'value'=> 5, 'checked'=> true],
- ['name'=> '签单数据', 'value'=> 6, 'checked'=> true],
- ['name'=> '转单数据', 'value'=> 7, 'checked'=> true],
- ['name'=> '积分数据', 'value'=> 8, 'checked'=> true]
- ];
- if (!empty($activity['show'])) {
- $show_checked = explode(',', $activity['show']);
- foreach ($show as $k => $v) {
- if (!in_array($v['value'], $show_checked)) {
- $show[$k]['checked'] = false;
- }
- }
- }
- return json(['code'=> 0, 'data'=> $show]);
- }
- /**
- * 获取一些页面静态数据
- */
- public function defaultData(){
- $activity = Model::find($this->activity_id);
- $course_list = WechatActivityCourse::where('activity_id', '=', $this->activity_id)->order('start_time asc')->select()->toArray();
- if (empty($course_list)) {
- return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
- }
- $data['course'] = $course_list;
- $data['start_time'] = $activity['start_date'];
- $data['end_time'] = $activity['end_date'];
- $data['title'] = $activity['title'];
- return json(['code'=> 0, 'data'=> $data]);
- }
- /**
- * 首页数据
- */
- public function index(){
- $activity = Model::find($this->activity_id);
- $course_list = WechatActivityCourse::where('activity_id', '=', $this->activity_id)->order('start_time asc')->select()->toArray();
- if (empty($course_list)) {
- return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
- }
- $start_time = input('start_time', '', 'strtotime');
- $end_time = input('end_time', '', 'strtotime');
- $check_course = input('course', '', 'intval');
- $over = true; // 判断赛程是否已结束
- $over_time = 0; // 判断赛程是否已结束,如果结束展示最后一轮
- $now_course = []; // 当前轮次信息
- $over_course = []; // 结束时的轮次
- $now_course_key = 0;
- $over_course_key = 0;
- foreach ($course_list as $k => $v) {
- if (empty($check_course)) { // 没有选中轮次
- if (strtotime($v['start_time']) < time() && strtotime($v['end_time']) > time()) {
- $now_course = $v;
- $over = false;
- $now_course_key = $k + 1;
- }
- if (strtotime($v['end_time']) > $over_time) {
- $over_time = strtotime($v['end_time']);
- $over_course = $v;
- $over_course_key = $k + 1;
- }
- } else { // 手动选中轮次
- if ($v['id'] == $check_course) {
- $over = false;
- $now_course_key = $k + 1;
- $now_course = $v;
- }
- }
- }
- if ($over) {
- $course = $over_course;
- $course_key = $over_course_key;
- } else {
- $course = $now_course;
- $course_key = $now_course_key;
- }
- if (empty($course)) {
- return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
- }
- $data['course_key'] = $course_key; // 轮次
- // 赛程相关信息
- $countdown_time = $activity['countdown'];
- $data['course'] = [
- 'id'=> $course['id'],
- 'course_key'=> $course_key,
- 'start_time'=> $course['start_time'],
- 'end_time'=> $course['end_time'],
- 'countdown_time'=> $countdown_time
- ];
- $type = input('type', '', 'intval'); // (旧:1加微 2见面 3签单 4转单 5积分) 新:1加微,2见面,3到店,4量房,5到场,6签单,7转单,8积分
- if ($type == 8) {
- $where[] = ['activity_id', '=', $this->activity_id];
- $where[] = ['root_id', '=', $this->root_id];
- if ($start_time && $end_time) {
- $where[] = ['addtime', 'between', [date('Y-m-d 00:00:00', $start_time), date('Y-m-d 23:59:59', $end_time)]];
- } else {
- $where[] = ['addtime', 'between', [date('Y-m-d H:i:s', strtotime($course['start_time'])), date('Y-m-d H:i:s', strtotime($course['end_time']))]];
- }
- $list = WechatActivityIntegral::where($where)->where('type', '<>', 6)->group('employee_id')->column('sum(integral)', 'employee_id');
- // 团队加分
- $where[] = ['type', '=', 6];
- $team_integral = WechatActivityIntegral::where($where)->group('org_id')->column('sum(integral)', 'org_id');
- } else {
- $where[] = ['activity_id', '=', $this->activity_id];
- $where[] = ['root_id', '=', $this->root_id];
- if ($start_time && $end_time) {
- $where[] = ['addtime', 'between', [date('Y-m-d 00:00:00', $start_time), date('Y-m-d 23:59:59', $end_time)]];
- } else {
- $where[] = ['addtime', 'between', [date('Y-m-d H:i:s', strtotime($course['start_time'])), date('Y-m-d H:i:s', strtotime($course['end_time']))]];
- }
- $level_num = 0; // 任务量
- switch ($type){
- case 1: //加微
- $where[] = ['type', '=', 1];
- $level_num = $activity['schedule_jiav_num'];
- break;
- case 2: //见面
- $where[] = ['type', 'in', [2, 3, 4, 5, 7]];
- $level_num = $activity['schedule_meet_num'];
- break;
- case 3: //到店
- $where[] = ['type', '=', 2];
- break;
- case 4: //量房
- $where[] = ['type', '=', 3];
- break;
- case 5: //到场
- $where[] = ['type', '=', 7];
- break;
- case 6: //签单
- $where[] = ['type', '=', 4];
- $level_num = $activity['schedule_deposit_num'];
- break;
- case 7: //转单
- $where[] = ['type', '=', 5];
- $level_num = $activity['schedule_zd_num'];
- break;
- case 8: //积分
- $where[] = ['type', '=', 6];
- break;
- default:
- $where[] = ['type', '=', 9999];
- break;
- }
- if(in_array($type, [2, 3, 4, 5, 6, 7])){
- $type_data = WechatActivityIntegral::where($where)->group('employee_id,customer_id')->column('customer_id,employee_id');
- $new = [];
- foreach($type_data as $val){
- $new[$val['employee_id']][] = $val;
- }
- $list = [];
- foreach($new as $key=>$val){
- $list[$key] = !empty($val) ? count(array_column($val,'customer_id')) : 0;
- }
- }else{
- $list = WechatActivityIntegral::where($where)->group('employee_id')->column('count(id)', 'employee_id');
- }
- }
- // 部门数据
- $a_where[] = ['activity_id', '=', $this->activity_id];
- $a_where[] = ['root_id', '=', $this->root_id];
- $all_team = WechatActivityTeam::with(['employee'=> function($query) {
- return $query->with('user')->where([['state', '=', '在职'], ['uid', '>', 0]]);
- }])->where($a_where)->select()->toArray();
- $orgList = Org::where([['id', 'in', array_column($all_team, 'org_id')]])->column('info', 'id');
- foreach($all_team as &$yy){
- if(!isset($orgList[$yy['org_id']])){
- $yy['teamname'] = '';
- continue;
- }
- $teamname = $orgList[$yy['org_id']];
- $namepath = explode('/', $teamname);
- $lastTwo = implode('/', array_slice($namepath, -2));
- $yy['teamname'] = $lastTwo;
- }
- // 团队数据
- $team_data = [];
- $all_employee = []; // 所有参赛成员
- foreach ($all_team as $k => $v) {
- $one['team_id'] = $v['org_id'];
- $one['team_name'] = $v['teamname']; // 团队名称
- // 团队成员
- $one_employee = !empty($v['employee']) ? array_column($v['employee'], 'id') : [];
- foreach ($v['employee'] as &$v_emp){
- $v_emp['team_name'] = $v['teamname'];
- }
- $all_employee = array_merge($all_employee, $v['employee']);
- $one_count = 0;
- foreach ($one_employee as $kk => $vv) {
- if (isset($list[$vv])) {
- $one_count += $list[$vv];
- }
- }
- if (isset($team_integral[$v['org_id']])) {
- $one_count += $team_integral[$v['org_id']];
- }
- $one['count'] = $one_count;
- if (in_array($type, [1, 2, 6, 7])) {
- $level_total_num = $level_num * count($one_employee); // 总任务量
- $lv = $level_total_num > 0 ? ceil($one_count / $level_total_num * 100) : 0;
- $one['lv'] = $lv;
- }
- $team_data[] = $one;
- }
- $team_count = array_column($team_data, 'count');
- array_multisort($team_count, SORT_DESC, $team_data);
- $data['team_data'] = $team_data;
- // 战神榜 乌龟榜
- $employee = [];
- foreach ($all_employee as $k => $v) {
- $v_value = $list[$v['id']] ?? 0;
- $employee[] = [
- 'count'=> (int)$v_value,
- 'headimg'=> $v['user']['headimgurl'] ?? '',
- 'name'=> $v['name'],
- 'team_name'=> $v['team_name'],
- ];
- }
- $emp_count = array_column($employee, 'count');
- array_multisort($emp_count, SORT_DESC, $employee);
- $top_list = $employee;
- array_multisort($emp_count, SORT_ASC, $employee);
- $end_list = $employee;
- $top_data = array_slice($top_list, 0, 10);
- $end_data = array_slice($end_list, 0, 10);
- $data['top_data'] = $top_data;
- $data['end_data'] = $end_data;
- // 总数展示
- $total_count = array_sum($list);
- $data['total_count'] = $total_count;
- return json(['code'=> 0, 'data'=> $data, 'msg'=> 'success']);
- }
- /**
- * 部门数据
- */
- public function orgData(){
- $course_id = input('course', '', 'intval');
- $org_id = input('team_id', '', 'intval');
- $course = WechatActivityCourse::find($course_id);
- $type = input('type', '', 'intval'); // (旧:1加微 2见面 3签单 4转单 5积分) 新:1加微,2见面,3到店,4量房,5到场,6签单,7转单,8积分
- $start_time = input('start_time', '', 'trim');
- $end_time = input('end_time', '', 'trim');
- if (empty($course)) {
- return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
- }
- $where[] = ['activity_id', '=', $this->activity_id];
- $where[] = ['root_id', '=', $this->root_id];
- if ($start_time && $end_time) {
- $where[] = ['addtime', 'between', [date('Y-m-d 00:00:00', strtotime($start_time)), date('Y-m-d 23:59:59', strtotime($end_time))]];
- } else {
- $where[] = ['addtime', 'between', [date('Y-m-d H:i:s', strtotime($course['start_time'])), date('Y-m-d H:i:s', strtotime($course['end_time']))]];
- }
- if ($type == 8) {
- $where[] = ['type', '<>', 6];
- $list = WechatActivityIntegral::where($where)->group('employee_id')->column('sum(integral)', 'employee_id');
- } else {
- switch ($type){
- case 1:
- $where[] = ['type', '=', 1];
- break;
- case 2:
- $where[] = ['type', 'in', [2, 3, 4, 5, 7]];
- break;
- case 3:
- $where[] = ['type', '=', 2];
- break;
- case 4:
- $where[] = ['type', '=', 3];
- break;
- case 5:
- $where[] = ['type', '=', 7];
- break;
- case 6:
- $where[] = ['type', '=', 4];
- break;
- case 7:
- $where[] = ['type', '=', 5];
- break;
- default:
- $where[] = ['type', '=', 9999];
- break;
- }
- if ($type == 2) {
- $type_data = WechatActivityIntegral::where($where)->group('employee_id,customer_id')->column('customer_id,employee_id');
- foreach ($type_data as $val) {
- $new[$val['employee_id']][] = $val;
- }
- foreach ($new as $key => $val) {
- $list[$key] = !empty($val) ? count(array_column($val, 'customer_id')) : 0;
- }
- } else {
- $list = WechatActivityIntegral::where($where)->group('employee_id')->column('count(id)', 'employee_id');
- }
- }
- // 部门数据
- $a_where[] = ['activity_id', '=', $this->activity_id];
- $a_where[] = ['root_id', '=', $this->root_id];
- $a_where[] = ['org_id', '=', $org_id];
- $team = WechatActivityTeam::with(['org' => function($query) {
- return $query->field('id,name');
- }, 'employee'=> function($query) {
- return $query->with('user')->where([['state', '=', '在职'], ['uid', '>', 0]]);
- }])->where($a_where)->findOrEmpty();
- if ($team->isEmpty()) {
- return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
- }
- $employee = []; // 所有参赛成员
- // 团队成员
- $all_employee = $team['employee'] ?? [];
- foreach ($all_employee as $k => $v) {
- $one['name'] = $v['name'];
- $one['headimgurl'] = $v['user']['headimgurl'] ?? '';
- if (isset($list[$v['id']])) {
- $one['count'] = $list[$v['id']];
- } else {
- $one['count'] = 0;
- }
- $employee[] = $one;
- }
- $emp_count = array_column($employee, 'count');
- array_multisort($emp_count, SORT_DESC, $employee);
- return json(['code'=> 0, 'data'=> $employee, 'msg'=> 'success']);
- }
- }
|