ExcelExportService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service;
  15. use app\common\enum\FileEnum;
  16. use app\common\model\export\Export;
  17. use app\common\model\file\File;
  18. use app\common\service\storage\Driver as StorageDriver;
  19. use excel\ExcelWriter;
  20. use Exception;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. class ExcelExportService
  24. {
  25. private $sheet;
  26. public function __construct()
  27. {
  28. $this->sheet = new ExcelWriter();
  29. }
  30. public function download($id)
  31. {
  32. try{
  33. Log::info("download-export:{$id}");
  34. $infoExport = Export::findOrEmpty($id);
  35. if(!$infoExport->isEmpty()){
  36. $download_fun = $infoExport['download_fun'];
  37. $filename = $infoExport['name'];
  38. $params = $infoExport['params']?:[];
  39. // 是否存在该函数
  40. if(!method_exists($this,$download_fun)){
  41. throw new Exception('下载函数不存在');
  42. }
  43. $this->$download_fun($infoExport,$filename,$params);
  44. }
  45. }catch (\Exception $e){
  46. Log::info("download-error:{$id}:".$e->getMessage());
  47. throw new Exception($e->getMessage());
  48. }
  49. }
  50. /**
  51. * @notes 工程师 -上月最后结算余额 导出
  52. */
  53. public function EngineerBillDownload($infoExport,$filename,$params)
  54. {
  55. try{
  56. if($params){ }
  57. $firstDay = date('Y-m-01 00:00:00', strtotime('first day of last month'));
  58. $lastDay = date('Y-m-t 23:59:59', strtotime('last day of last month'));
  59. $lists = Db::name('master_worker')->alias('a')->field([
  60. 'a.id','a.real_name','a.nickname','a.worker_number','e.account_holder','e.bank_name','e.opening_branch','e.account as bank_account',
  61. Db::raw("IFNULL(c.maxid,0) as maxid"),Db::raw("IFNULL(d.left_amount,0) as left_amount")
  62. ])
  63. ->leftJoin([Db::name('master_worker_account_log')
  64. ->where('create_time','between',[strtotime($firstDay),strtotime($lastDay)])
  65. ->field('worker_id as master_worker_id, max(id) as maxid')
  66. ->group('worker_id')
  67. ->buildSql() => 'c'], 'a.id = c.master_worker_id')
  68. ->leftJoin('master_worker_account_log d', 'c.maxid = d.id')
  69. ->leftJoin('bank_account e', 'a.id = e.worker_id')
  70. //->where('d.left_amount', '>', 0)
  71. ->select()->toArray();
  72. $filename = ($filename?:'').('-'.date('YmdHis').'-'.$infoExport->id);
  73. $this->sheet->generateExcelFile([
  74. '工程师ID', '工程师编号', '工程师姓名', '工程师银行卡号', '工程师开户行及支行信息','上月最后结算余额'
  75. ], $lists,$filename, ['id','worker_number','nickname','bank_account','bank_name','left_amount']);
  76. $infoExport->file_url = $this->sheet->fileUrl();
  77. $infoExport->generate_status = 1;
  78. $infoExport->updatetime = time();
  79. $infoExport->save();
  80. return $infoExport->file_url;
  81. }catch (\Exception $e){
  82. throw new Exception($e->getMessage());
  83. }
  84. }
  85. }