ExcelExportService.php 11 KB

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