ExcelExportService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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\adminapi\logic\financial\MasterSettlementDetailsLogic;
  16. use app\common\enum\FileEnum;
  17. use app\common\model\export\Export;
  18. use app\common\model\file\File;
  19. use app\common\model\financial\FinancialPaymentRecords;
  20. use app\common\service\storage\Driver as StorageDriver;
  21. use excel\ExcelWriter;
  22. use Exception;
  23. use think\facade\Db;
  24. use think\facade\Log;
  25. class ExcelExportService
  26. {
  27. private $sheet;
  28. public $download_type = [
  29. '1' => 'EngineerBillDownload',
  30. '2' => 'MasterWorkerServiceOrderDownload',
  31. ];
  32. public function __construct()
  33. {
  34. $this->sheet = new ExcelWriter();
  35. }
  36. public function download($id)
  37. {
  38. try{
  39. $infoExport = Export::findOrEmpty($id);
  40. if(!$infoExport->isEmpty()){
  41. $download_fun = $infoExport['download_fun'];
  42. if(!method_exists($this,$download_fun)){
  43. throw new Exception('下载不存在-1001');
  44. }
  45. $this->$download_fun($infoExport);
  46. }
  47. }catch (\Exception $e){
  48. Log::info("download-error:{$id}:".$e->getMessage());
  49. throw new Exception($e->getMessage());
  50. }
  51. }
  52. /**
  53. * @notes 工程师 -上月最后结算余额 导出
  54. */
  55. public function EngineerBillDownload_Bak($infoExport)
  56. {
  57. try{
  58. $filename = $infoExport['name'];
  59. $params = $infoExport['params']?:[];
  60. if($params){ }
  61. $firstDay = date('Y-m-01 00:00:00', strtotime('first day of last month'));
  62. $lastDay = date('Y-m-t 23:59:59', strtotime('last day of last month'));
  63. $lists = Db::name('master_worker')->alias('a')->field([
  64. 'a.id','a.real_name','a.nickname','a.worker_number','e.account_holder','e.bank_name','e.opening_branch','e.account as bank_account',
  65. Db::raw("IFNULL(c.maxid,0) as maxid"),Db::raw("IFNULL(d.left_amount,0) as left_amount")
  66. ])
  67. ->leftJoin([Db::name('master_worker_account_log')
  68. ->where('create_time','between',[strtotime($firstDay),strtotime($lastDay)])
  69. ->field('worker_id as master_worker_id, max(id) as maxid')
  70. ->group('worker_id')
  71. ->buildSql() => 'c'], 'a.id = c.master_worker_id')
  72. ->leftJoin('master_worker_account_log d', 'c.maxid = d.id')
  73. ->leftJoin('bank_account e', 'a.id = e.worker_id')
  74. //->where('d.left_amount', '>', 0)
  75. ->select()->toArray();
  76. $filename = ($filename?:'').('-'.date('YmdHis').'-'.$infoExport->id);
  77. $this->sheet->generateExcelFile([
  78. '工程师ID', '工程师编号', '工程师姓名', '工程师银行卡号', '工程师开户行及支行信息','上月最后结算余额'
  79. ], $lists,$filename, ['id','worker_number','real_name','bank_account','bank_name','left_amount']);
  80. $infoExport->file_url = $this->sheet->fileUrl();
  81. $infoExport->generate_status = 1;
  82. $infoExport->updatetime = time();
  83. $infoExport->save();
  84. return $infoExport->file_url;
  85. }catch (\Exception $e){
  86. throw new Exception($e->getMessage());
  87. }
  88. }
  89. /**
  90. * @notes 工程师 -截止某日最后结算余额 导出
  91. */
  92. public function EngineerBillDownload($infoExport)
  93. {
  94. Db::startTrans();
  95. try{
  96. $filename = $infoExport['name'];
  97. $params = $infoExport['params']?:[];
  98. if($params){ }
  99. // 导出逻辑
  100. /*添加财务打款记录表: 生成批次编号、状态待上传 已取消 打款状态: 未打款、部分打款、全部打款
  101. 生成数据表格:工程师信息、银行卡信息、批次编号信息、应发金额、导出时账号余额、导入时账号余额、扣款金额、扣款说明、实发金额、打款状态(已打、下次打款)、打款时间、备注、是否已导入
  102. (账号当前余额 !=0) < (应发金额 !=0) = 下次打款
  103. 更新表 la_master_settlement_details 批次编号、状态-待算
  104. */
  105. $batch_number = 'bn' . date('YmdHis') . rand(1000, 9999);
  106. // 添加财务打款记录表 -- 批次编号、状态待上传
  107. $params['infoExport_id'] = $infoExport->id;
  108. FinancialPaymentRecords::create([
  109. 'export_conditions' => $params,
  110. 'batch_number' => $batch_number,
  111. 'upload_status' => 1,
  112. 'payment_status' => 1,
  113. ]);
  114. //生成数据表格 & 更新表 -- 批次编号、状态-待算
  115. $lists = MasterSettlementDetailsLogic::upBatchStatus($params,$batch_number);
  116. $filename = ($filename?($filename.'-批号'.$batch_number):'').('-'.date('YmdHis').'-'.$infoExport->id);
  117. $this->sheet->generateExcelFile([
  118. '工程师ID', '工程师编号', '工程师姓名', '工程师银行卡号', '所属银行', '工程师开户行及支行信息','导出时账号余额',
  119. '批次编号',
  120. '应发金额', '扣款金额', '扣款说明', '实发金额', '打款时间','备注'
  121. ], $lists,$filename, [
  122. 'master_worker_id','worker_number','engineer_name','bank_account','bank_name','opening_branch','original_balance',
  123. 'batch_number',
  124. 'total_settlement_amount','','','','',''
  125. ]);
  126. $infoExport->name = $filename;
  127. $infoExport->file_url = $this->sheet->fileUrl();
  128. $infoExport->generate_status = 1;
  129. $infoExport->updatetime = time();
  130. $infoExport->save();
  131. Db::commit();
  132. return $infoExport->file_url;
  133. } catch (\Exception $e) {
  134. Db::rollback();
  135. throw new Exception($e->getMessage());
  136. }
  137. }
  138. /**
  139. * @notes 工程师 -工单统计 导出
  140. */
  141. public function MasterWorkerServiceOrderDownload($infoExport)
  142. {
  143. try{
  144. $filename = $infoExport['name'];
  145. $params = $infoExport['params']?:[];
  146. $where = [];
  147. $sqlJoin = '';
  148. if($params){
  149. if (isset($params['real_name']) && !empty($params['real_name'])) {
  150. $where[] = ['a.real_name','like' ,"%".$params['real_name']."%"];
  151. }
  152. if (isset($params['worker_number']) && !empty($params['worker_number'])) {
  153. $where[] = ['a.worker_number','like' ,"%".$params['worker_number']."%"];
  154. }
  155. if (isset($params['recruiting_behalf']) && !empty($params['recruiting_behalf'])) {
  156. $where[] = ['a.recruiting_behalf','like' ,"%".$params['recruiting_behalf']."%"];
  157. }
  158. if (isset($params['mobile']) && !empty($params['mobile'])) {
  159. $where[] = ['a.mobile','=' ,$params['mobile']];
  160. }
  161. if (isset($params['cooperation']) && !empty($params['cooperation'])) {
  162. $where[] = ['a.cooperation','=' ,$params['cooperation']];
  163. }
  164. if (!empty($params['start_time'])) {
  165. $sqlJoin .= ' AND b.update_time >= '.strtotime($params['start_time']);
  166. }
  167. if (!empty($params['end_time'])) {
  168. $sqlJoin .= ' AND b.update_time <= '.strtotime($params['end_time'])+86400;
  169. }
  170. }
  171. $lists = Db::name('master_worker')->alias('a')->field([
  172. 'a.id','a.real_name','a.nickname','a.worker_number','a.recruiting_behalf','a.mobile','a.cooperation',
  173. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN 1 ELSE 0 END) AS success_count"),
  174. Db::raw("SUM(CASE WHEN b.service_status = 4 OR b.service_status = 5 THEN 1 ELSE 0 END) AS fail_count"),
  175. Db::raw("SUM(b.work_total) work_total"),
  176. Db::raw("SUM(b.worker_price) worker_price"),
  177. ])
  178. ->leftJoin('service_work b', 'a.id = b.master_worker_id'.$sqlJoin)
  179. ->where($where)
  180. ->group('a.id')
  181. ->select()->toArray();
  182. $filename = ($filename?:'').('-'.date('YmdHis').'-'.$infoExport->id);
  183. $this->sheet->generateExcelFile([
  184. '工程师ID', '工程师编号', '工程师姓名', '工程师手机', '代招人姓名', '工程师成功单','工程师失败单','工程师成交金额','工程师提成金额'
  185. ], $lists,$filename, ['id','worker_number','real_name','mobile','recruiting_behalf','success_count','fail_count','work_total','worker_price']);
  186. $infoExport->file_url = $this->sheet->fileUrl();
  187. $infoExport->generate_status = 1;
  188. $infoExport->updatetime = time();
  189. $infoExport->save();
  190. return $infoExport->file_url;
  191. }catch (\Exception $e){
  192. throw new Exception($e->getMessage());
  193. }
  194. }
  195. }