WechatActivity.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace app\sys\controller;
  3. use app\model\ContractLog;
  4. use app\model\DepositLog;
  5. use app\model\Employee;
  6. use app\model\FansLog;
  7. use app\model\Team;
  8. use app\model\TeamMember;
  9. use app\model\WechatActivity as ModelWechatActivity;
  10. use think\facade\View;
  11. use think\helper\Str;
  12. use app\model\WechatActivityCourse;
  13. use app\model\WechatActivityIntegral;
  14. use app\model\WechatActivityTeam;
  15. use app\model\Org;
  16. /**
  17. * 数据统计
  18. */
  19. class WechatActivity extends Base
  20. {
  21. /**
  22. * 周期
  23. *
  24. * @param [type] $id
  25. * @return void
  26. */
  27. public function schedule($id)
  28. {
  29. $schedule = WechatActivityCourse::where(['activity_id' => $id])->field('start_time,end_time')->select()->toArray();
  30. foreach($schedule as &$val){
  31. $val['startDate'] = $val['start_time'];
  32. $val['endDate'] = $val['end_time'];
  33. }
  34. return json(['code' => 0, 'data' => $schedule, 'count' => 0]);
  35. }
  36. /**
  37. * 活动列表
  38. */
  39. public function index()
  40. {
  41. if (!request()->isAjax()) return View::fetch();
  42. $param = request()->param(['page' => 1, 'limit' => 10, 'date' => '', 'title' => '']);
  43. $root_id = request()->employee->root_id;
  44. $condition = [
  45. ['root_id', '=', $root_id],
  46. ['delete_time', '=', 0]
  47. ];
  48. if (!empty($param['title'])) {
  49. $condition[] = ['title', 'like', '%' . $param['title'] . '%'];
  50. }
  51. if (!empty($param['date'])) {
  52. $date = explode(' - ', $param['date']);
  53. $condition[] = ['start_date', '>=', $date[0]];
  54. $condition[] = ['end_date', '<=', $date[1]];
  55. }
  56. $data = ModelWechatActivity::field('id, title, start_date, end_date')->where($condition)->order('addtime desc')->page($param['page'], $param['limit'])->select();
  57. $count = ModelWechatActivity::where($condition)->count();
  58. foreach ($data as &$item) {
  59. $list = WechatActivityIntegral::where([['root_id','=',$root_id],['activity_id','=',$item['id']]])->field('count(id) as count,type')->group('type')->select()->toArray();
  60. $jv = $dd = $lf = $qd = $zd = $dc = 0;
  61. foreach($list as $k=>$v){
  62. if($v['type'] == 1) $jv = $v['count'];
  63. if($v['type'] == 2) $dd = $v['count'];
  64. if($v['type'] == 3) $lf = $v['count'];
  65. if($v['type'] == 4) $qd = $v['count'];
  66. if($v['type'] == 5) $zd = $v['count'];
  67. if($v['type'] == 7) $dc = $v['count'];
  68. }
  69. $item['jv'] = $jv;
  70. $item['dd'] = WechatActivityIntegral::where([['root_id','=',$root_id],['activity_id','=',$item['id']],['type','in',[2, 3, 4, 5, 7]]])->group('customer_id')->count();
  71. $item['qd'] = $qd;
  72. $item['zd'] = $zd;
  73. }
  74. return ['code' => 0, 'data' => $data, 'count' => $count];
  75. }
  76. /**
  77. * 活动添加
  78. */
  79. public function add()
  80. {
  81. return View::fetch();
  82. }
  83. /**
  84. * 活动编辑
  85. */
  86. public function edit()
  87. {
  88. $id = request()->param('id');
  89. $data = ModelWechatActivity::where(['root_id' => request()->employee->root_id, 'id' => $id])->find();
  90. View::assign('data',$data);
  91. return View::fetch();
  92. }
  93. /**
  94. * 活动保存
  95. */
  96. public function save()
  97. {
  98. $param = request()->param(['start_date', 'title', 'end_date']);
  99. $id = request()->param('id');
  100. $root_id = request()->employee->root_id;
  101. $param['root_id'] = $root_id;
  102. if (isset($id) && !empty($id)){
  103. $activity = ModelWechatActivity::where([['root_id','=',$root_id],['id','=',$id]])->find();
  104. }else{
  105. $param['ticket'] = str_rand(16);
  106. $activity = new ModelWechatActivity();
  107. }
  108. $activity->save($param);
  109. return ['code' => 0, 'msg' => '保存成功'];
  110. }
  111. /**
  112. * 团队设置
  113. */
  114. public function team()
  115. {
  116. $request = request();
  117. $param = $request->only(['id']);
  118. $root_id = $request->employee->root_id;
  119. $list = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->select()->toArray();
  120. View::assign('list',$list);
  121. $activity = ModelWechatActivity::with(['course'])->where([['root_id','=',$root_id],['id','=',$param['id']]])->find();
  122. //var_dump($activity->toArray());exit;
  123. View::assign('activity',$activity);
  124. $domain = request()->domain();
  125. $url = $domain.'/largescreen.html#/?ticket='.$activity['ticket'];
  126. View::assign('url',$url);
  127. return View::fetch();
  128. }
  129. /**
  130. * 面板设置
  131. */
  132. public function panel_setting()
  133. {
  134. $request = request();
  135. $param = $request->only(['id','countdown']);
  136. $root_id = $request->employee->root_id;
  137. $activity = ModelWechatActivity::with(['course'])->where([['root_id','=',$root_id],['id','=',$param['id']]])->find();
  138. if(empty($activity)) return json(['code'=>1,'msg'=>'活动不存在']);
  139. if (!$request->isAjax()) {
  140. $teamlist = WechatActivityTeam::with(['org'])->where([['root_id','=',$root_id],['activity_id','=',$activity['id']]])->select()->toArray();
  141. View::assign('teamlist',$teamlist);
  142. View::assign('activity',$activity);
  143. $domain = request()->domain();
  144. $url = $domain.'/largescreen.html#/?ticket='.$activity['ticket'];
  145. View::assign('url',$url);
  146. return View::fetch();
  147. }
  148. $activity->countdown = $param['countdown'];
  149. $activity->save();
  150. return json(['code'=>0,'msg'=>'设置成功']);
  151. }
  152. /**
  153. * 给团队添加积分
  154. */
  155. public function team_addintegral()
  156. {
  157. $request = request();
  158. $param = $request->only(['aid','integral','tid','addtime']);
  159. $root_id = $request->employee->root_id;
  160. $course = WechatActivityCourse::where([['root_id','=',$root_id],['activity_id','=',$param['aid']]])->select()->toArray();
  161. $check = true;
  162. foreach($course as $key=>$val){
  163. if($param['addtime'] >= $val['start_time'] && $param['addtime'] <= $val['end_time']){
  164. $check = false;
  165. continue;
  166. }
  167. }
  168. if($check) return json(['code'=>1,'msg'=>'没有对应的赛程时间段']);
  169. $add = ['root_id'=>$root_id,'activity_id'=>$param['aid'],'org_id'=>$param['tid'],'employee_id'=>$request->employee->id,'integral'=>$param['integral'],'type'=>6];
  170. $ms = WechatActivityIntegral::create($add);
  171. if($ms){
  172. return json(['code'=>0,'msg'=>'添加成功']);
  173. }else{
  174. return json(['code'=>1,'msg'=>'添加失败']);
  175. }
  176. }
  177. /**
  178. * 活动删除
  179. */
  180. public function delweact()
  181. {
  182. $request = request();
  183. $param = $request->only(['id']);
  184. $root_id = $request->employee->root_id;
  185. $activity = ModelWechatActivity::where([['root_id','=',request()->employee->root_id],['id','=',$param['id']]])->find();
  186. if(empty($activity)) return json(['code'=>1,'msg'=>'活动不存在']);
  187. $ms = $activity->delete();
  188. WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->delete();
  189. WechatActivityCourse::where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->delete();
  190. return json(['code' => 0, 'msg' => '删除成功']);
  191. }
  192. /**
  193. * 规则页面
  194. */
  195. public function setting()
  196. {
  197. $request = request();
  198. $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']);
  199. $root_id = request()->employee->root_id;
  200. $activity = ModelWechatActivity::with(['course'])->where([['root_id','=',request()->employee->root_id],['id','=',$param['id']]])->find();
  201. if(empty($activity)) return json(['code'=>1,'msg'=>'活动不存在']);
  202. if (!$request->isAjax()) {
  203. $id = request()->param('id');
  204. View::assign('activity',$activity);
  205. $domain = request()->domain();
  206. $url = $domain.'/largescreen.html#/?ticket='.$activity['ticket'];
  207. View::assign('url',$url);
  208. // 展示模块
  209. $show = [
  210. ['name'=> '加微数据', 'value'=> 1, 'checked'=> true],
  211. ['name'=> '见面数据', 'value'=> 2, 'checked'=> true],
  212. ['name'=> '到店数据', 'value'=> 3, 'checked'=> true],
  213. ['name'=> '量房数据', 'value'=> 4, 'checked'=> true],
  214. ['name'=> '到场数据', 'value'=> 5, 'checked'=> true],
  215. ['name'=> '签单数据', 'value'=> 6, 'checked'=> true],
  216. ['name'=> '转单数据', 'value'=> 7, 'checked'=> true],
  217. ['name'=> '积分数据', 'value'=> 8, 'checked'=> true]
  218. ];
  219. if (!empty($activity['show'])) {
  220. $show_checked = explode(',', $activity['show']);
  221. foreach ($show as $k => $v) {
  222. if (!in_array($v['value'], $show_checked)) {
  223. $show[$k]['checked'] = false;
  224. }
  225. }
  226. }
  227. View::assign('show', $show);
  228. return View::fetch();
  229. }
  230. $data = [
  231. 'one_jv_integral' => $param['one_jv_integral'],
  232. 'one_dd_integral' => $param['one_dd_integral'],
  233. 'one_dc_integral' => $param['one_dc_integral'],
  234. 'one_lf_integral' => $param['one_lf_integral'],
  235. 'one_qd_integral' => $param['one_qd_integral'],
  236. 'one_zd_integral' => $param['one_zd_integral'],
  237. 'schedule_jiav_num' => $param['schedule_jiav_num'],
  238. 'schedule_meet_num' => $param['schedule_meet_num'],
  239. 'schedule_zd_num' => $param['schedule_zd_num'],
  240. 'schedule_deposit_num' => $param['schedule_deposit_num'],
  241. 'show'=> $param['show']
  242. ];
  243. $activity->save($data);
  244. $schedule = [];
  245. // 更新赛程
  246. if (isset($param['schedule_start_date']) && isset($param['schedule_end_date'])) {
  247. foreach ($param['schedule_start_date'] as $k => $startDateTime)
  248. $schedule[] = [
  249. 'activity_id' => $activity->id,
  250. 'start_time' => strtotime($startDateTime),
  251. 'end_time' => strtotime($param['schedule_end_date'][$k]),
  252. 'root_id' => $root_id
  253. ];
  254. WechatActivityCourse::where(['activity_id' => $activity->id])->delete();
  255. (new WechatActivityCourse())->saveAll($schedule);
  256. }
  257. if (empty($param['schedule_start_date']) && empty($param['schedule_end_date'])) {
  258. WechatActivityCourse::where(['activity_id' => $activity->id])->delete();
  259. }
  260. return ['code' => 0, 'msg' => '保存成功'];
  261. }
  262. /**
  263. * 添加团队
  264. */
  265. public function add_team()
  266. {
  267. $request = request();
  268. $param = $request->only(['team','aid']);
  269. $root_id = $request->employee->root_id;
  270. $list = $param['team'];
  271. $yids = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['aid']]])->column('org_id');
  272. foreach($list as $key=>$val){
  273. if(!in_array($val['id'],$yids)){
  274. $add[] = ['activity_id'=>$param['aid'],'org_id'=>$val['id'],'team_size'=>$val['count'],'root_id'=>$root_id];
  275. }
  276. }
  277. $teamod = new WechatActivityTeam;
  278. if(!empty($add)) $teamod->saveAll($add);
  279. return json(['code'=>0,'msg'=>'设置成功']);
  280. }
  281. /**
  282. * 删除团队
  283. */
  284. public function del_team()
  285. {
  286. $request = request();
  287. $param = $request->only(['tid','aid']);
  288. $root_id = $request->employee->root_id;
  289. $team = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$param['aid']],['org_id','=',$param['tid']]])->find();
  290. if(empty($team)) return json(['code'=>1,'msg'=>'团队不存在']);
  291. $team->delete();
  292. return json(['code'=>0,'msg'=>'删除成功']);
  293. }
  294. /**
  295. * 获取部门树
  296. * 分配弹框页面接口
  297. */
  298. public function get_orgs()
  299. {
  300. $keyword = input('keyword', '');
  301. $aid = input('aid',0);
  302. $root_id = request()->employee->root_id;
  303. $ids = WechatActivityTeam::where([['root_id','=',$root_id],['activity_id','=',$aid]])->column('org_id');
  304. //获取本人部门及子部门
  305. $where = [
  306. ['path', 'like', $root_id.'-%'],
  307. ['status', '=', 1]
  308. ];
  309. $count = Employee::where([['root_id', '=', $root_id],['state', 'like', '%在职%'], ['uid', '>', 0]])->group('org_id')->column('count(*) count', 'org_id');
  310. $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toArray();
  311. foreach ($allnodes as $k => $v) {
  312. $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0; //本部门人数
  313. $allnodes[$k]['all_count'] = 0; //包含子部门总数
  314. $allnodes[$k]['org_class'] = 'org_' . $v['id'];
  315. $allnodes[$k]['per_class'] = 'per_org_' . $v['id'];
  316. $allnodes[$k]['type'] = 'org';
  317. foreach ($allnodes as $k2 => $v2) {
  318. if (strpos($v2['path'], $v['path']) !== false) {
  319. $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']] + $allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
  320. }
  321. }
  322. $allnodes[$k]['title'] = $allnodes[$k]['title'] . ' (' . $allnodes[$k]['all_count'] . ')';
  323. }
  324. $tree = $this->tree($allnodes,$root_id,$ids);
  325. return json(['code' => 0, 'data' => $tree, 'checkOrg' => '']);
  326. }
  327. public function tree($data, $pid = 0,$ids)
  328. {
  329. $new_arr = [];
  330. foreach ($data as $k => $v) {
  331. if ($v['pid'] == $pid) {
  332. $v['select'] = false;
  333. $children = $this->tree($data, $v['id'],$ids);
  334. $v['children'] = $children;
  335. if (empty($v['children'])) $v['disabled'] = true;
  336. if(in_array($v['id'],$ids)) $v['select'] = true;
  337. $new_arr[] = $v;
  338. }
  339. }
  340. return $new_arr;
  341. }
  342. /**
  343. * 团队数据统计
  344. */
  345. public function teamStatistics()
  346. {
  347. $request = request();
  348. $param = $request->only(['id','addtime','org_id','keyname']);
  349. $root_id = $request->employee->root_id;
  350. if (!$request->isAjax()) {
  351. View::assign('id',$param['id']);
  352. $tmlist = WechatActivityTeam::with(['org'])->where([['root_id','=',$root_id],['activity_id','=',$param['id']]])->select()->toArray();
  353. View::assign('tmlist',$tmlist);
  354. return View::fetch();
  355. }
  356. $where[] = ['root_id','=',$root_id];
  357. $where[] = ['activity_id', '=', $param['id']];
  358. if(!empty($param['org_id'])) $where[] = ['org_id','=',$param['org_id']];
  359. if(!empty($param['keyname'])){
  360. $orgids = Org::where([['name','like','%'.$param['keyname'].'%'],['path','like',$root_id.'-%']])->column('id');
  361. $where[] = ['org_id','in',$orgids];
  362. }
  363. $inw = [['id','>',0],['activity_id','=',$param['id']]];
  364. if(!empty($param['addtime'])){
  365. $time = explode(' - ',$param['addtime']);
  366. $inw = [['activity_id','=',$param['id']],['addtime','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']]];
  367. }
  368. $teamlist = WechatActivityTeam::with(['org'])->where($where)
  369. ->withSum(['integral'=>function($query) use ($inw){
  370. $query->where([$inw]);
  371. }],'integral')
  372. ->select()->toArray();
  373. foreach ($teamlist as &$item) {
  374. $twhere[] = ['root_id','=',$root_id];
  375. $twhere[] = ['org_id','=',$item['org_id']];
  376. $twhere[] = ['activity_id','=',$item['activity_id']];
  377. if(!empty($param['addtime'])){
  378. $time = explode(' - ',$param['addtime']);
  379. $twhere[] = ['addtime','between',[$time[0].' 00:00:00',$time[1].' 23:59:59']];
  380. }
  381. $list = WechatActivityIntegral::where($twhere)->field('count(id) as count,type')->group('type')->select()->toArray();
  382. $jv = $dd = $lf = $qd = $zd = $dc = 0;
  383. foreach($list as $k=>$v){
  384. if($v['type'] == 1) $jv = $v['count'];
  385. if($v['type'] == 2) $dd = $v['count'];
  386. if($v['type'] == 3) $lf = $v['count'];
  387. if($v['type'] == 4) $qd = $v['count'];
  388. if($v['type'] == 5) $zd = $v['count'];
  389. if($v['type'] == 7) $dc = $v['count'];
  390. }
  391. $item['jv'] = $jv;
  392. $item['dd'] = $dd + $lf + $dc;
  393. $item['qd'] = $qd;
  394. $item['zd'] = $zd;
  395. unset($twhere);
  396. }
  397. return ['code' => 0, 'data' => $teamlist];
  398. }
  399. }