LearningRecords.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\merchant\model\special;
  12. use basic\ModelBasic;
  13. use traits\ModelTrait;
  14. use think\Db;
  15. use app\merchant\model\special\SpecialWatch;
  16. use app\merchant\model\special\Special;
  17. use service\PhpSpreadsheetService;
  18. /**浏览记录
  19. * Class LearningRecords
  20. * @package app\merchant\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. if (isset($where['mer_id']) && $where['mer_id']) $model = $model->where('s.mer_id', $where['mer_id']);
  44. $model = $model->group('l.uid');
  45. if ($where['data'] != '') {
  46. $model = self::getModelTime($where, $model, 'l.add_time');
  47. }
  48. $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');
  49. return $model;
  50. }
  51. /**学习记录
  52. * @param $where
  53. * @param $id
  54. */
  55. public static function specialLearningRecordsLists($where, $id)
  56. {
  57. $model = self::getOrderWhere($where, $id);
  58. if (isset($where['excel']) && $where['excel'] == 1) {
  59. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  60. } else {
  61. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  62. }
  63. foreach ($data as $key => &$value) {
  64. $value['last_study_time'] = date('Y-m-d', $value['add_time']);
  65. }
  66. if (isset($where['excel']) && $where['excel'] == 1) {
  67. self::SaveExcel($data);
  68. }
  69. $count = self::getOrderWhere($where, $id)->count();
  70. return compact('count', 'data');
  71. }
  72. /**
  73. * 保存并下载excel
  74. * $list array
  75. * return
  76. */
  77. public static function SaveExcel($list)
  78. {
  79. $export = [];
  80. foreach ($list as $index => $item) {
  81. $export[] = [
  82. $item['uid'],
  83. $item['nickname'],
  84. $item['phone'],
  85. $item['level'] == 1 ? '会员' : '非会员',
  86. $item['title'],
  87. $item['percentage'],
  88. $item['last_study_time']
  89. ];
  90. }
  91. $filename = '学习记录' . time() . '.xlsx';
  92. $head = ['UID', '昵称', '电话', '身份', '专题名称', '观看进度%', '最后学习时间'];
  93. PhpSpreadsheetService::outdata($filename, $export, $head);
  94. }
  95. }