LearningRecords.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\special;
  12. use basic\ModelBasic;
  13. use traits\ModelTrait;
  14. use think\Db;
  15. use app\admin\model\special\SpecialWatch;
  16. use app\admin\model\special\Special;
  17. use service\PhpSpreadsheetService;
  18. /**浏览记录
  19. * Class LearningRecords
  20. * @package app\admin\model\special
  21. */
  22. class LearningRecords extends ModelBasic
  23. {
  24. use ModelTrait;
  25. /**最后学习时间
  26. * @param $id
  27. * @param $uid
  28. * @return mixed
  29. */
  30. public static function lastStudyTime($id, $uid)
  31. {
  32. return self::where(['special_id' => $id, 'uid' => $uid])->value('add_time');
  33. }
  34. /**条件处理
  35. * @param $where
  36. */
  37. public static function getOrderWhere($where, $id)
  38. {
  39. $model = self::alias('l')->join('User u', 'l.uid=u.uid')
  40. ->join('Special s', 'l.special_id=s.id', 'left');
  41. if ($id) $model = $model->where('l.special_id', $id);
  42. if (isset($where['uid']) && $where['uid']) $model = $model->where('l.uid', $where['uid']);
  43. $model = $model->group('l.uid');
  44. if ($where['data'] != '') {
  45. $model = self::getModelTime($where, $model, 'l.add_time');
  46. }
  47. $model = $model->order('l.add_time desc')->field('l.uid,l.special_id,l.add_time,s.id,s.type,s.title,s.is_light,u.nickname,u.phone,u.level');
  48. return $model;
  49. }
  50. /**学习记录
  51. * @param $where
  52. * @param $id
  53. */
  54. public static function specialLearningRecordsLists($where, $id)
  55. {
  56. $model = self::getOrderWhere($where, $id);
  57. if (isset($where['excel']) && $where['excel'] == 1) {
  58. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  59. } else {
  60. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  61. }
  62. foreach ($data as $key => &$value) {
  63. $value['last_study_time'] = date('Y-m-d', $value['add_time']);
  64. }
  65. if (isset($where['excel']) && $where['excel'] == 1) {
  66. self::SaveExcel($data);
  67. }
  68. $count = self::getOrderWhere($where, $id)->count();
  69. return compact('count', 'data');
  70. }
  71. /**
  72. * 保存并下载excel
  73. * $list array
  74. * return
  75. */
  76. public static function SaveExcel($list)
  77. {
  78. $export = [];
  79. foreach ($list as $index => $item) {
  80. $export[] = [
  81. $item['uid'],
  82. $item['nickname'],
  83. $item['phone'],
  84. $item['level'] == 1 ? '会员' : '非会员',
  85. $item['title'],
  86. $item['percentage'],
  87. $item['last_study_time']
  88. ];
  89. }
  90. $filename = '学习记录' . time() . '.xlsx';
  91. $head = ['UID', '昵称', '电话', '身份', '专题名称', '观看进度%', '最后学习时间'];
  92. PhpSpreadsheetService::outdata($filename, $export, $head);
  93. }
  94. }