DataDownloadRecords.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\download;
  12. use basic\ModelBasic;
  13. use traits\ModelTrait;
  14. use think\Db;
  15. use app\admin\model\download\DataDownload;
  16. use service\PhpSpreadsheetService;
  17. /**资料下载
  18. * Class DataDownloadRecords
  19. * @package app\admin\model\download
  20. */
  21. class DataDownloadRecords extends ModelBasic
  22. {
  23. use ModelTrait;
  24. /**条件处理
  25. * @param $where
  26. */
  27. public static function getOrderWhere($where, $id)
  28. {
  29. $model = self::alias('r')->join('User u', 'r.uid=u.uid', 'left')
  30. ->join('DataDownload d', 'r.data_id=d.id', 'left')
  31. ->where('r.data_id', $id);
  32. if ($where['data'] != '') {
  33. $model = self::getModelTime($where, $model, 'r.add_time');
  34. }
  35. $model = $model->order('r.add_time desc')->field('r.uid,r.data_id,r.add_time,r.update_time,r.number,d.id,d.title,d.money,d.member_money,u.nickname,u.phone,u.level');
  36. return $model;
  37. }
  38. /**下载记录
  39. * @param $where
  40. * @param $id
  41. */
  42. public static function specialLearningRecordsLists($where, $id)
  43. {
  44. $model = self::getOrderWhere($where, $id);
  45. if (isset($where['excel']) && $where['excel'] == 1) {
  46. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  47. } else {
  48. $data = ($data = $model->page((int)$where['page'], (int)$where['limit'])->select()) && count($data) ? $data->toArray() : [];
  49. }
  50. foreach ($data as $key => &$value) {
  51. $value['last_study_time'] = $value['update_time'];
  52. $value['price'] = $value['level'] > 0 ? $value['member_money'] : $value['money'];
  53. }
  54. if (isset($where['excel']) && $where['excel'] == 1) {
  55. self::SaveExcel($data);
  56. }
  57. $count = self::getOrderWhere($where, $id)->count();
  58. return compact('count', 'data');
  59. }
  60. /**
  61. * 保存并下载excel
  62. * $list array
  63. * return
  64. */
  65. public static function SaveExcel($list)
  66. {
  67. $export = [];
  68. foreach ($list as $index => $item) {
  69. $export[] = [
  70. $item['uid'],
  71. $item['nickname'],
  72. $item['phone'],
  73. $item['level'] == 1 ? '会员' : '非会员',
  74. $item['title'],
  75. $item['number'],
  76. $item['price'],
  77. $item['last_study_time']
  78. ];
  79. }
  80. $filename = '下载记录' . time() . '.xlsx';
  81. $head = ['UID', '昵称', '电话', '身份', '资料名称', '下载次数', '价格', '最后学习时间'];
  82. PhpSpreadsheetService::outdata($filename, $export, $head);
  83. }
  84. }