123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418 |
- <?php
- namespace app\sys\controller;
- use app\model\ContractLog;
- use app\model\DepositLog;
- use app\model\Employee;
- use app\model\FansLog;
- use app\model\Team;
- use app\model\TeamMember;
- use app\model\WechatActivity as ModelWechatActivity;
- use think\facade\View;
- use think\helper\Str;
- use app\model\WechatActivityCourse;
- use app\model\WechatActivityIntegral;
- use app\model\WechatActivityTeam;
- use app\model\Org;
- /**
- * 数据统计
- */
- class WechatActivity extends Base
- {
- /**
- * 周期
- *
- * @param [type] $id
- * @return void
- */
- public function schedule($id)
- {
- $schedule = WechatActivityCourse::where(['activity_id' => $id])->field('start_time,end_time')->select()->toArray();
- foreach($schedule as &$val){
- $val['startDate'] = $val['start_time'];
- $val['endDate'] = $val['end_time'];
- }
- return json(['code' => 0, 'data' => $schedule, 'count' => 0]);
- }
- /**
- * 活动列表
- */
- public function index()
- {
- if (!request()->isAjax()) return View::fetch();
- $param = request()->param(['page' => 1, 'limit' => 10, 'date' => '', 'title' => '']);
- $root_id = request()->employee->root_id;
- $condition = [
- ['root_id', '=', $root_id],
- ['delete_time', '=', 0]
- ];
- if (!empty($param['title'])) {
- $condition[] = ['title', 'like', '%' . $param['title'] . '%'];
- }
- if (!empty($param['date'])) {
- $date = explode(' - ', $param['date']);
- $condition[] = ['start_date', '>=', $date[0]];
- $condition[] = ['end_date', '<=', $date[1]];
- }
- $data = ModelWechatActivity::field('id, title, start_date, end_date')->where($condition)->order('addtime desc')->page($param['page'], $param['limit'])->select();
- $count = ModelWechatActivity::where($condition)->count();
- foreach ($data as &$item) {
- $list = WechatActivityIntegral::where([['root_id','=',$root_id],['activity_id','=',$item['id']]])->field('count(id) as count,type')->group('type')->select()->toArray();
- $jv = $dd = $lf = $qd = $zd = $dc = 0;
- foreach($list as $k=>$v){
- if($v['type'] == 1) $jv = $v['count'];
- if($v['type'] == 2) $dd = $v['count'];
- if($v['type'] == 3) $lf = $v['count'];
- if($v['type'] == 4) $qd = $v['count'];
- if($v['type'] == 5) $zd = $v['count'];
- if($v['type'] == 7) $dc = $v['count'];
- }
- $item['jv'] = $jv;
- $item['dd'] = WechatActivityIntegral::where([['root_id','=',$root_id],['activity_id','=',$item['id']],['type','in',[2, 3, 4, 5, 7]]])->group('customer_id')->count();
- $item['qd'] = $qd;
- $item['zd'] = $zd;
- }
- return ['code' => 0, 'data' => $data, 'count' => $count];
- }
- /**
- * 活动添加
- */
- public function add()
- {
- return View::fetch();
- }
- /**
- * 活动编辑
- */
- public function edit()
- {
- $id = request()->param('id');
- $data = ModelWechatActivity::where(['root_id' => request()->employee->root_id, 'id' => $id])->find();
- View::assign('data',$data);
- return View::fetch();
- }
- /**
- * 活动保存
- */
- public function save()
- {
- $param = request()->param(['start_date', 'title', 'end_date']);
- $id = request()->param('id');
- $root_id = request()->employee->root_id;
- $param['root_id'] = $root_id;
- if (isset($id) && !empty($id)){
- $activity = ModelWechatActivity::where([['root_id','=',$root_id],['id','=',$id]])->find();
- }else{
- $param['ticket'] = str_rand(16);
- $activity = new ModelWechatActivity();
- }
- $activity->save($param);
- return ['code' => 0, 'msg' => '保存成功'];
- }
- /**
- * 团队设置
- */
- public function team()
- {
- $request = request();
- $param = $request->only(['id']);
- $root_id = $request->employee->root_id;
- $list = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->select()->toArray();
- View::assign('list',$list);
- $activity = ModelWechatActivity::with(['course'])->where([['root_id','=',$root_id],['id','=',$param['id']]])->find();
- //var_dump($activity->toArray());exit;
- View::assign('activity',$activity);
- $domain = request()->domain();
- $url = $domain.'/largescreen.html#/?ticket='.$activity['ticket'];
- View::assign('url',$url);
- return View::fetch();
- }
- /**
- * 面板设置
- */
- public function panel_setting()
- {
- $request = request();
- $param = $request->only(['id','countdown']);
- $root_id = $request->employee->root_id;
- $activity = ModelWechatActivity::with(['course'])->where([['root_id','=',$root_id],['id','=',$param['id']]])->find();
- if(empty($activity)) return json(['code'=>1,'msg'=>'活动不存在']);
- if (!$request->isAjax()) {
- $teamlist = WechatActivityTeam::with(['org'])->where([['root_id','=',$root_id],['activity_id','=',$activity['id']]])->select()->toArray();
- View::assign('teamlist',$teamlist);
- View::assign('activity',$activity);
- $domain = request()->domain();
- $url = $domain.'/largescreen.html#/?ticket='.$activity['ticket'];
- View::assign('url',$url);
- return View::fetch();
- }
- $activity->countdown = $param['countdown'];
- $activity->save();
- return json(['code'=>0,'msg'=>'设置成功']);
- }
- /**
- * 给团队添加积分
- */
- public function team_addintegral()
- {
- $request = request();
- $param = $request->only(['aid','integral','tid','addtime']);
- $root_id = $request->employee->root_id;
- $course = WechatActivityCourse::where([['root_id','=',$root_id],['activity_id','=',$param['aid']]])->select()->toArray();
- $check = true;
- foreach($course as $key=>$val){
- if($param['addtime'] >= $val['start_time'] && $param['addtime'] <= $val['end_time']){
- $check = false;
- continue;
- }
- }
- if($check) return json(['code'=>1,'msg'=>'没有对应的赛程时间段']);
- $add = ['root_id'=>$root_id,'activity_id'=>$param['aid'],'org_id'=>$param['tid'],'employee_id'=>$request->employee->id,'integral'=>$param['integral'],'type'=>6];
- $ms = WechatActivityIntegral::create($add);
- if($ms){
- return json(['code'=>0,'msg'=>'添加成功']);
- }else{
- return json(['code'=>1,'msg'=>'添加失败']);
- }
- }
- /**
- * 活动删除
- */
- public function delweact()
- {
- $request = request();
- $param = $request->only(['id']);
- $root_id = $request->employee->root_id;
- $activity = ModelWechatActivity::where([['root_id','=',request()->employee->root_id],['id','=',$param['id']]])->find();
- if(empty($activity)) return json(['code'=>1,'msg'=>'活动不存在']);
- $ms = $activity->delete();
- WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->delete();
- WechatActivityCourse::where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->delete();
- return json(['code' => 0, 'msg' => '删除成功']);
- }
- /**
- * 规则页面
- */
- public function setting()
- {
- $request = request();
- $param = $request->only(['id','one_jv_integral','one_dd_integral'=>0,'one_lf_integral'=>0,'one_qd_integral'=>0,'one_zd_integral'=>0,'schedule_jiav_num'=>0,'schedule_meet_num'=>0,'schedule_zd_num'=>0,'schedule_deposit_num'=>0,'one_dc_integral'=>0,'schedule_start_date','schedule_end_date','show']);
- $root_id = request()->employee->root_id;
- $activity = ModelWechatActivity::with(['course'])->where([['root_id','=',request()->employee->root_id],['id','=',$param['id']]])->find();
- if(empty($activity)) return json(['code'=>1,'msg'=>'活动不存在']);
- if (!$request->isAjax()) {
- $id = request()->param('id');
- View::assign('activity',$activity);
- $domain = request()->domain();
- $url = $domain.'/largescreen.html#/?ticket='.$activity['ticket'];
- View::assign('url',$url);
- // 展示模块
- $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;
- }
- }
- }
- View::assign('show', $show);
- return View::fetch();
- }
- $data = [
- 'one_jv_integral' => $param['one_jv_integral'],
- 'one_dd_integral' => $param['one_dd_integral'],
- 'one_dc_integral' => $param['one_dc_integral'],
- 'one_lf_integral' => $param['one_lf_integral'],
- 'one_qd_integral' => $param['one_qd_integral'],
- 'one_zd_integral' => $param['one_zd_integral'],
- 'schedule_jiav_num' => $param['schedule_jiav_num'],
- 'schedule_meet_num' => $param['schedule_meet_num'],
- 'schedule_zd_num' => $param['schedule_zd_num'],
- 'schedule_deposit_num' => $param['schedule_deposit_num'],
- 'show'=> $param['show']
- ];
- $activity->save($data);
- $schedule = [];
- // 更新赛程
- if (isset($param['schedule_start_date']) && isset($param['schedule_end_date'])) {
- foreach ($param['schedule_start_date'] as $k => $startDateTime)
- $schedule[] = [
- 'activity_id' => $activity->id,
- 'start_time' => strtotime($startDateTime),
- 'end_time' => strtotime($param['schedule_end_date'][$k]),
- 'root_id' => $root_id
- ];
- WechatActivityCourse::where(['activity_id' => $activity->id])->delete();
- (new WechatActivityCourse())->saveAll($schedule);
- }
- if (empty($param['schedule_start_date']) && empty($param['schedule_end_date'])) {
- WechatActivityCourse::where(['activity_id' => $activity->id])->delete();
- }
- return ['code' => 0, 'msg' => '保存成功'];
- }
- /**
- * 添加团队
- */
- public function add_team()
- {
- $request = request();
- $param = $request->only(['team','aid']);
- $root_id = $request->employee->root_id;
- $list = $param['team'];
- $yids = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['aid']]])->column('org_id');
- foreach($list as $key=>$val){
- if(!in_array($val['id'],$yids)){
- $add[] = ['activity_id'=>$param['aid'],'org_id'=>$val['id'],'team_size'=>$val['count'],'root_id'=>$root_id];
- }
- }
- $teamod = new WechatActivityTeam;
- if(!empty($add)) $teamod->saveAll($add);
- return json(['code'=>0,'msg'=>'设置成功']);
- }
- /**
- * 删除团队
- */
- public function del_team()
- {
- $request = request();
- $param = $request->only(['tid','aid']);
- $root_id = $request->employee->root_id;
- $team = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['aid']],['org_id','=',$param['tid']]])->find();
- if(empty($team)) return json(['code'=>1,'msg'=>'团队不存在']);
- $team->delete();
- return json(['code'=>0,'msg'=>'删除成功']);
- }
- /**
- * 获取部门树
- * 分配弹框页面接口
- */
- public function get_orgs()
- {
- $keyword = input('keyword', '');
- $aid = input('aid',0);
- $root_id = request()->employee->root_id;
- $ids = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$aid]])->column('org_id');
- //获取本人部门及子部门
- $where = [
- ['path', 'like', $root_id.'-%'],
- ['status', '=', 1]
- ];
- $count = Employee::where([['root_id', '=', $root_id],['state', 'like', '%在职%'], ['uid', '>', 0]])->group('org_id')->column('count(*) count', 'org_id');
- $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toArray();
- foreach ($allnodes as $k => $v) {
- $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0; //本部门人数
- $allnodes[$k]['all_count'] = 0; //包含子部门总数
- $allnodes[$k]['org_class'] = 'org_' . $v['id'];
- $allnodes[$k]['per_class'] = 'per_org_' . $v['id'];
- $allnodes[$k]['type'] = 'org';
- foreach ($allnodes as $k2 => $v2) {
- if (strpos($v2['path'], $v['path']) !== false) {
- $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']] + $allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
- }
- }
- $allnodes[$k]['title'] = $allnodes[$k]['title'] . ' (' . $allnodes[$k]['all_count'] . ')';
- }
- $tree = $this->tree($allnodes,$root_id,$ids);
- return json(['code' => 0, 'data' => $tree, 'checkOrg' => '']);
- }
- public function tree($data, $pid = 0,$ids)
- {
- $new_arr = [];
- foreach ($data as $k => $v) {
- if ($v['pid'] == $pid) {
- $v['select'] = false;
- $children = $this->tree($data, $v['id'],$ids);
- $v['children'] = $children;
- if (empty($v['children'])) $v['disabled'] = true;
- if(in_array($v['id'],$ids)) $v['select'] = true;
- $new_arr[] = $v;
- }
- }
- return $new_arr;
- }
- /**
- * 团队数据统计
- */
- public function teamStatistics()
- {
- $request = request();
- $param = $request->only(['id','addtime','org_id','keyname']);
- $root_id = $request->employee->root_id;
- if (!$request->isAjax()) {
- View::assign('id',$param['id']);
- $tmlist = WechatActivityTeam::with(['org'])->where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->select()->toArray();
- View::assign('tmlist',$tmlist);
- return View::fetch();
- }
- $where[] = ['root_id','=',$root_id];
- $where[] = ['activity_id', '=', $param['id']];
- if(!empty($param['org_id'])) $where[] = ['org_id','=',$param['org_id']];
- if(!empty($param['keyname'])){
- $orgids = Org::where([['name','like','%'.$param['keyname'].'%'],['path','like',$root_id.'-%']])->column('id');
- $where[] = ['org_id','in',$orgids];
- }
- $inw = [['id','>',0],['activity_id','=',$param['id']]];
- if(!empty($param['addtime'])){
- $time = explode(' - ',$param['addtime']);
- $inw = [['activity_id','=',$param['id']],['addtime','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']]];
- }
- $teamlist = WechatActivityTeam::with(['org'])->where($where)
- ->withSum(['integral'=>function($query) use ($inw){
- $query->where([$inw]);
- }],'integral')
- ->select()->toArray();
- foreach ($teamlist as &$item) {
- $twhere[] = ['root_id','=',$root_id];
- $twhere[] = ['org_id','=',$item['org_id']];
- $twhere[] = ['activity_id','=',$item['activity_id']];
- if(!empty($param['addtime'])){
- $time = explode(' - ',$param['addtime']);
- $twhere[] = ['addtime','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']];
- }
- $list = WechatActivityIntegral::where($twhere)->field('count(id) as count,type')->group('type')->select()->toArray();
- $jv = $dd = $lf = $qd = $zd = $dc = 0;
- foreach($list as $k=>$v){
- if($v['type'] == 1) $jv = $v['count'];
- if($v['type'] == 2) $dd = $v['count'];
- if($v['type'] == 3) $lf = $v['count'];
- if($v['type'] == 4) $qd = $v['count'];
- if($v['type'] == 5) $zd = $v['count'];
- if($v['type'] == 7) $dc = $v['count'];
- }
- $item['jv'] = $jv;
- $item['dd'] = $dd + $lf + $dc;
- $item['qd'] = $qd;
- $item['zd'] = $zd;
- unset($twhere);
- }
- return ['code' => 0, 'data' => $teamlist];
- }
- }
|