ExcelExportService.php 11 KB

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