1
0

WechatActivity.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <?php
  2. namespace app\api\controller;
  3. use app\model\Employee;
  4. use app\model\Org;
  5. use app\model\WechatActivity as Model;
  6. use app\model\WechatActivityCourse;
  7. use app\model\WechatActivityIntegral;
  8. use app\model\User;
  9. use app\model\WechatActivityTeam;
  10. use app\model\CustomerVisitLog;
  11. /**
  12. * 微群爆破返回数据
  13. * @package app\api\controller
  14. */
  15. class WechatActivity {
  16. private $root_id = 0;
  17. private $activity_id = 0;
  18. public function __construct()
  19. {
  20. $ticket = input('ticket', '', 'trim');
  21. $activity = Model::where('ticket', '=', $ticket)->find();
  22. if (empty($activity)) {
  23. return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
  24. }
  25. $this->activity_id = $activity->id;
  26. $this->root_id = $activity->root_id;
  27. }
  28. /**
  29. * 获取展示模块
  30. */
  31. public function showModule() {
  32. $activity = Model::find($this->activity_id);
  33. $show = [
  34. ['name'=> '加微数据', 'value'=> 1, 'checked'=> true],
  35. ['name'=> '见面数据', 'value'=> 2, 'checked'=> true],
  36. ['name'=> '到店数据', 'value'=> 3, 'checked'=> true],
  37. ['name'=> '量房数据', 'value'=> 4, 'checked'=> true],
  38. ['name'=> '到场数据', 'value'=> 5, 'checked'=> true],
  39. ['name'=> '签单数据', 'value'=> 6, 'checked'=> true],
  40. ['name'=> '转单数据', 'value'=> 7, 'checked'=> true],
  41. ['name'=> '积分数据', 'value'=> 8, 'checked'=> true]
  42. ];
  43. if (!empty($activity['show'])) {
  44. $show_checked = explode(',', $activity['show']);
  45. foreach ($show as $k => $v) {
  46. if (!in_array($v['value'], $show_checked)) {
  47. $show[$k]['checked'] = false;
  48. }
  49. }
  50. }
  51. return json(['code'=> 0, 'data'=> $show]);
  52. }
  53. /**
  54. * 获取一些页面静态数据
  55. */
  56. public function defaultData(){
  57. $activity = Model::find($this->activity_id);
  58. $course_list = WechatActivityCourse::where('activity_id', '=', $this->activity_id)->order('start_time asc')->select()->toArray();
  59. if (empty($course_list)) {
  60. return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
  61. }
  62. $data['course'] = $course_list;
  63. $data['start_time'] = $activity['start_date'];
  64. $data['end_time'] = $activity['end_date'];
  65. $data['title'] = $activity['title'];
  66. return json(['code'=> 0, 'data'=> $data]);
  67. }
  68. /**
  69. * 首页数据
  70. */
  71. public function index(){
  72. $activity = Model::find($this->activity_id);
  73. $course_list = WechatActivityCourse::where('activity_id', '=', $this->activity_id)->order('start_time asc')->select()->toArray();
  74. if (empty($course_list)) {
  75. return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
  76. }
  77. $start_time = input('start_time', '', 'strtotime');
  78. $end_time = input('end_time', '', 'strtotime');
  79. $check_course = input('course', '', 'intval');
  80. $over = true; // 判断赛程是否已结束
  81. $over_time = 0; // 判断赛程是否已结束,如果结束展示最后一轮
  82. $now_course = []; // 当前轮次信息
  83. $over_course = []; // 结束时的轮次
  84. $now_course_key = 0;
  85. $over_course_key = 0;
  86. foreach ($course_list as $k => $v) {
  87. if (empty($check_course)) { // 没有选中轮次
  88. if (strtotime($v['start_time']) < time() && strtotime($v['end_time']) > time()) {
  89. $now_course = $v;
  90. $over = false;
  91. $now_course_key = $k + 1;
  92. }
  93. if (strtotime($v['end_time']) > $over_time) {
  94. $over_time = strtotime($v['end_time']);
  95. $over_course = $v;
  96. $over_course_key = $k + 1;
  97. }
  98. } else { // 手动选中轮次
  99. if ($v['id'] == $check_course) {
  100. $over = false;
  101. $now_course_key = $k + 1;
  102. $now_course = $v;
  103. }
  104. }
  105. }
  106. if ($over) {
  107. $course = $over_course;
  108. $course_key = $over_course_key;
  109. } else {
  110. $course = $now_course;
  111. $course_key = $now_course_key;
  112. }
  113. if (empty($course)) {
  114. return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
  115. }
  116. $data['course_key'] = $course_key; // 轮次
  117. // 赛程相关信息
  118. $countdown_time = $activity['countdown'];
  119. $data['course'] = [
  120. 'id'=> $course['id'],
  121. 'course_key'=> $course_key,
  122. 'start_time'=> $course['start_time'],
  123. 'end_time'=> $course['end_time'],
  124. 'countdown_time'=> $countdown_time
  125. ];
  126. $type = input('type', '', 'intval'); // (旧:1加微 2见面 3签单 4转单 5积分) 新:1加微,2见面,3到店,4量房,5到场,6签单,7转单,8积分
  127. if ($type == 8) {
  128. $where[] = ['activity_id', '=', $this->activity_id];
  129. $where[] = ['root_id', '=', $this->root_id];
  130. if ($start_time && $end_time) {
  131. $where[] = ['addtime', 'between', [date('Y-m-d 00:00:00', $start_time), date('Y-m-d 23:59:59', $end_time)]];
  132. } else {
  133. $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']))]];
  134. }
  135. $list = WechatActivityIntegral::where($where)->where('type', '<>', 6)->group('employee_id')->column('sum(integral)', 'employee_id');
  136. // 团队加分
  137. $where[] = ['type', '=', 6];
  138. $team_integral = WechatActivityIntegral::where($where)->group('org_id')->column('sum(integral)', 'org_id');
  139. } else {
  140. $where[] = ['activity_id', '=', $this->activity_id];
  141. $where[] = ['root_id', '=', $this->root_id];
  142. if ($start_time && $end_time) {
  143. $where[] = ['addtime', 'between', [date('Y-m-d 00:00:00', $start_time), date('Y-m-d 23:59:59', $end_time)]];
  144. } else {
  145. $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']))]];
  146. }
  147. $level_num = 0; // 任务量
  148. switch ($type){
  149. case 1: //加微
  150. $where[] = ['type', '=', 1];
  151. $level_num = $activity['schedule_jiav_num'];
  152. break;
  153. case 2: //见面
  154. $where[] = ['type', 'in', [2, 3, 4, 5, 7]];
  155. $level_num = $activity['schedule_meet_num'];
  156. break;
  157. case 3: //到店
  158. $where[] = ['type', '=', 2];
  159. break;
  160. case 4: //量房
  161. $where[] = ['type', '=', 3];
  162. break;
  163. case 5: //到场
  164. $where[] = ['type', '=', 7];
  165. break;
  166. case 6: //签单
  167. $where[] = ['type', '=', 4];
  168. $level_num = $activity['schedule_deposit_num'];
  169. break;
  170. case 7: //转单
  171. $where[] = ['type', '=', 5];
  172. $level_num = $activity['schedule_zd_num'];
  173. break;
  174. case 8: //积分
  175. $where[] = ['type', '=', 6];
  176. break;
  177. default:
  178. $where[] = ['type', '=', 9999];
  179. break;
  180. }
  181. if(in_array($type, [2, 3, 4, 5, 6, 7])){
  182. $type_data = WechatActivityIntegral::where($where)->group('employee_id,customer_id')->column('customer_id,employee_id');
  183. $new = [];
  184. foreach($type_data as $val){
  185. $new[$val['employee_id']][] = $val;
  186. }
  187. $list = [];
  188. foreach($new as $key=>$val){
  189. $list[$key] = !empty($val) ? count(array_column($val,'customer_id')) : 0;
  190. }
  191. }else{
  192. $list = WechatActivityIntegral::where($where)->group('employee_id')->column('count(id)', 'employee_id');
  193. }
  194. }
  195. // 部门数据
  196. $a_where[] = ['activity_id', '=', $this->activity_id];
  197. $a_where[] = ['root_id', '=', $this->root_id];
  198. $all_team = WechatActivityTeam::with(['employee'=> function($query) {
  199. return $query->with('user')->where([['state', '=', '在职'], ['uid', '>', 0]]);
  200. }])->where($a_where)->select()->toArray();
  201. $orgList = Org::where([['id', 'in', array_column($all_team, 'org_id')]])->column('info', 'id');
  202. foreach($all_team as &$yy){
  203. if(!isset($orgList[$yy['org_id']])){
  204. $yy['teamname'] = '';
  205. continue;
  206. }
  207. $teamname = $orgList[$yy['org_id']];
  208. $namepath = explode('/', $teamname);
  209. $lastTwo = implode('/', array_slice($namepath, -2));
  210. $yy['teamname'] = $lastTwo;
  211. }
  212. // 团队数据
  213. $team_data = [];
  214. $all_employee = []; // 所有参赛成员
  215. foreach ($all_team as $k => $v) {
  216. $one['team_id'] = $v['org_id'];
  217. $one['team_name'] = $v['teamname']; // 团队名称
  218. // 团队成员
  219. $one_employee = !empty($v['employee']) ? array_column($v['employee'], 'id') : [];
  220. foreach ($v['employee'] as &$v_emp){
  221. $v_emp['team_name'] = $v['teamname'];
  222. }
  223. $all_employee = array_merge($all_employee, $v['employee']);
  224. $one_count = 0;
  225. foreach ($one_employee as $kk => $vv) {
  226. if (isset($list[$vv])) {
  227. $one_count += $list[$vv];
  228. }
  229. }
  230. if (isset($team_integral[$v['org_id']])) {
  231. $one_count += $team_integral[$v['org_id']];
  232. }
  233. $one['count'] = $one_count;
  234. if (in_array($type, [1, 2, 6, 7])) {
  235. $level_total_num = $level_num * count($one_employee); // 总任务量
  236. $lv = $level_total_num > 0 ? ceil($one_count / $level_total_num * 100) : 0;
  237. $one['lv'] = $lv;
  238. }
  239. $team_data[] = $one;
  240. }
  241. $team_count = array_column($team_data, 'count');
  242. array_multisort($team_count, SORT_DESC, $team_data);
  243. $data['team_data'] = $team_data;
  244. // 战神榜 乌龟榜
  245. $employee = [];
  246. foreach ($all_employee as $k => $v) {
  247. $v_value = $list[$v['id']] ?? 0;
  248. $employee[] = [
  249. 'count'=> (int)$v_value,
  250. 'headimg'=> $v['user']['headimgurl'] ?? '',
  251. 'name'=> $v['name'],
  252. 'team_name'=> $v['team_name'],
  253. ];
  254. }
  255. $emp_count = array_column($employee, 'count');
  256. array_multisort($emp_count, SORT_DESC, $employee);
  257. $top_list = $employee;
  258. array_multisort($emp_count, SORT_ASC, $employee);
  259. $end_list = $employee;
  260. $top_data = array_slice($top_list, 0, 10);
  261. $end_data = array_slice($end_list, 0, 10);
  262. $data['top_data'] = $top_data;
  263. $data['end_data'] = $end_data;
  264. // 总数展示
  265. $total_count = array_sum($list);
  266. $data['total_count'] = $total_count;
  267. return json(['code'=> 0, 'data'=> $data, 'msg'=> 'success']);
  268. }
  269. /**
  270. * 部门数据
  271. */
  272. public function orgData(){
  273. $course_id = input('course', '', 'intval');
  274. $org_id = input('team_id', '', 'intval');
  275. $course = WechatActivityCourse::find($course_id);
  276. $type = input('type', '', 'intval'); // (旧:1加微 2见面 3签单 4转单 5积分) 新:1加微,2见面,3到店,4量房,5到场,6签单,7转单,8积分
  277. $start_time = input('start_time', '', 'trim');
  278. $end_time = input('end_time', '', 'trim');
  279. if (empty($course)) {
  280. return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
  281. }
  282. $where[] = ['activity_id', '=', $this->activity_id];
  283. $where[] = ['root_id', '=', $this->root_id];
  284. if ($start_time && $end_time) {
  285. $where[] = ['addtime', 'between', [date('Y-m-d 00:00:00', strtotime($start_time)), date('Y-m-d 23:59:59', strtotime($end_time))]];
  286. } else {
  287. $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']))]];
  288. }
  289. if ($type == 8) {
  290. $where[] = ['type', '<>', 6];
  291. $list = WechatActivityIntegral::where($where)->group('employee_id')->column('sum(integral)', 'employee_id');
  292. } else {
  293. switch ($type){
  294. case 1:
  295. $where[] = ['type', '=', 1];
  296. break;
  297. case 2:
  298. $where[] = ['type', 'in', [2, 3, 4, 5, 7]];
  299. break;
  300. case 3:
  301. $where[] = ['type', '=', 2];
  302. break;
  303. case 4:
  304. $where[] = ['type', '=', 3];
  305. break;
  306. case 5:
  307. $where[] = ['type', '=', 7];
  308. break;
  309. case 6:
  310. $where[] = ['type', '=', 4];
  311. break;
  312. case 7:
  313. $where[] = ['type', '=', 5];
  314. break;
  315. default:
  316. $where[] = ['type', '=', 9999];
  317. break;
  318. }
  319. if ($type == 2) {
  320. $type_data = WechatActivityIntegral::where($where)->group('employee_id,customer_id')->column('customer_id,employee_id');
  321. foreach ($type_data as $val) {
  322. $new[$val['employee_id']][] = $val;
  323. }
  324. foreach ($new as $key => $val) {
  325. $list[$key] = !empty($val) ? count(array_column($val, 'customer_id')) : 0;
  326. }
  327. } else {
  328. $list = WechatActivityIntegral::where($where)->group('employee_id')->column('count(id)', 'employee_id');
  329. }
  330. }
  331. // 部门数据
  332. $a_where[] = ['activity_id', '=', $this->activity_id];
  333. $a_where[] = ['root_id', '=', $this->root_id];
  334. $a_where[] = ['org_id', '=', $org_id];
  335. $team = WechatActivityTeam::with(['org' => function($query) {
  336. return $query->field('id,name');
  337. }, 'employee'=> function($query) {
  338. return $query->with('user')->where([['state', '=', '在职'], ['uid', '>', 0]]);
  339. }])->where($a_where)->findOrEmpty();
  340. if ($team->isEmpty()) {
  341. return json(['code'=> 0, 'msg'=> 'success', 'data'=> []]);
  342. }
  343. $employee = []; // 所有参赛成员
  344. // 团队成员
  345. $all_employee = $team['employee'] ?? [];
  346. foreach ($all_employee as $k => $v) {
  347. $one['name'] = $v['name'];
  348. $one['headimgurl'] = $v['user']['headimgurl'] ?? '';
  349. if (isset($list[$v['id']])) {
  350. $one['count'] = $list[$v['id']];
  351. } else {
  352. $one['count'] = 0;
  353. }
  354. $employee[] = $one;
  355. }
  356. $emp_count = array_column($employee, 'count');
  357. array_multisort($emp_count, SORT_DESC, $employee);
  358. return json(['code'=> 0, 'data'=> $employee, 'msg'=> 'success']);
  359. }
  360. }