ExcelExportService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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\model\works\ServiceWorkAllocateWorkerLog;
  25. use app\common\model\group_activity\GroupUserOrder;
  26. use app\common\service\storage\Driver as StorageDriver;
  27. use excel\ExcelWriter;
  28. use Exception;
  29. use think\facade\App;
  30. use think\facade\Db;
  31. use think\facade\Log;
  32. class ExcelExportService
  33. {
  34. private ExcelWriter $sheet;
  35. protected array $params = [];
  36. protected Export $infoExport;
  37. public array $download_type = [];
  38. public function __construct()
  39. {
  40. $this->download_type = config('export.download_type');
  41. $this->sheet = new ExcelWriter();
  42. }
  43. public function download($id,$isCmd = false)
  44. {
  45. try{
  46. $infoExport = Export::findOrEmpty($id);
  47. if(!$infoExport->isEmpty()){
  48. $download_fun = $infoExport['download_fun'];
  49. $this->infoExport = $infoExport;
  50. switch ($infoExport['download_type']){
  51. case 1:
  52. //case 2:
  53. case 3:
  54. if(!method_exists($this,$download_fun)){
  55. throw new Exception('下载不存在-1001');
  56. }
  57. $this->$download_fun($infoExport);
  58. break;
  59. default:
  60. $use_file = explode('.',$download_fun);
  61. $this->sheet->savePath = ($isCmd?'./public/exports/':'');
  62. $this->getDownloadFileUrl(invoke(str_replace('.', '\\', App::getNamespace() . ($isCmd?'\\adminapi':'') . '\\lists\\' . $use_file[0] . '\\'. ucwords($use_file[1]))));
  63. break;
  64. }
  65. }
  66. }catch (\Exception $e){
  67. Log::info("download-error:{$id}:".$e->getMessage());
  68. $this->infoExport->generate_status = 2;
  69. $this->infoExport->updatetime = time();
  70. $this->infoExport->save();
  71. throw new Exception($e->getMessage());
  72. }
  73. }
  74. // 获取下载文件路径
  75. public function getDownloadFileUrl($downloadObj){
  76. $lists = $downloadObj->excelExportList($this->infoExport['params']?:[]);
  77. $filename = $downloadObj->setFileName()?:($this->infoExport['name']?:'');
  78. $fields = $downloadObj->setExcelComplexFields();
  79. if(!$lists || !$fields){
  80. throw new Exception('lists或fields不存在');
  81. }
  82. $this->sheet->generateExcelFile($fields['zh_cn_fields'], $lists,$filename.'-'.$this->infoExport->id, $fields['data_fields']);
  83. $this->infoExport->file_url = $this->sheet->fileUrl();
  84. $this->infoExport->generate_status = 1;
  85. $this->infoExport->updatetime = time();
  86. $this->infoExport->save();
  87. return $this->infoExport->file_url;
  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):'');
  117. $this->sheet->generateExcelFile([
  118. '工程师ID', '工程师编号', '工程师姓名', '工程师银行卡号', '所属银行', '工程师开户行及支行信息','导出时账号余额',
  119. '批次编号',
  120. '应发金额', '扣款金额', '扣款说明', '实发金额', '打款时间','备注'
  121. ], $lists,$filename.'-'.$infoExport->id, [
  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['is_recruiting_behalf']) && !empty($params['is_recruiting_behalf'])) {
  159. if ($params['is_recruiting_behalf'] == 1)
  160. $where[] = ['a.recruiting_behalf','<>' ,""];
  161. else
  162. $where[] = ['a.recruiting_behalf','=' ,""];
  163. }
  164. if (isset($params['mobile']) && !empty($params['mobile'])) {
  165. $where[] = ['a.mobile','=' ,$params['mobile']];
  166. }
  167. if (isset($params['cooperation']) && !empty($params['cooperation'])) {
  168. $where[] = ['a.cooperation','=' ,$params['cooperation']];
  169. }
  170. if (!empty($params['start_time'])) {
  171. $sqlJoin .= ' AND b.update_time >= '.strtotime($params['start_time']);
  172. }
  173. if (!empty($params['end_time'])) {
  174. $sqlJoin .= ' AND b.update_time <= '.strtotime($params['end_time'])+86400;
  175. }
  176. }
  177. $lists = Db::name('master_worker')->alias('a')->field([
  178. 'a.id','a.real_name','a.nickname','a.worker_number','a.recruiting_behalf','a.mobile','a.cooperation','a.category_ids','a.labels','a.remark',
  179. Db::raw("COUNT(b.id) AS all_count"),
  180. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN 1 ELSE 0 END) AS success_count"),
  181. Db::raw("SUM(CASE WHEN b.service_status = 4 OR b.service_status = 5 THEN 1 ELSE 0 END) AS fail_count"),
  182. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN b.work_total ELSE 0 END) work_total"),
  183. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN b.worker_price ELSE 0 END) worker_price"),
  184. ])
  185. ->leftJoin('service_work b', 'a.id = b.master_worker_id'.$sqlJoin)
  186. ->where($where)
  187. ->group('a.id')
  188. ->order('a.id desc')
  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. foreach ($lists as &$item) {
  195. $item['labels'] = $item['labels']?array_map(function ($item) { return intval($item); },explode(',',$item['labels'])):'';
  196. $item['category_name'] = $item['category_ids']?implode('、',array_column(get_parent_info($categoryData,explode(',',$item['category_ids'])),'name')):'';
  197. $item['allocate_num'] = ServiceWorkAllocateWorkerLog::where(['master_worker_id'=>$item['id']])->count();
  198. }
  199. $this->sheet->generateExcelFile([
  200. '服务类别', '工程师编号', '工程师姓名', '工程师手机', '代招人姓名', '工程师接单数', '工程师成功单','工程师失败单','工程师成交金额','工程师提成金额'
  201. ], $lists,$filename.'-'.$infoExport->id, ['category_name','worker_number','real_name','mobile','recruiting_behalf','all_count','success_count','fail_count','work_total','worker_price']);
  202. $infoExport->file_url = $this->sheet->fileUrl();
  203. $infoExport->generate_status = 1;
  204. $infoExport->updatetime = time();
  205. $infoExport->save();
  206. return $infoExport->file_url;
  207. }catch (\Exception $e){
  208. throw new Exception($e->getMessage());
  209. }
  210. }*/
  211. // 权益卡编码导出
  212. public function UserEquityDownload($infoExport){
  213. try{
  214. $filename = $infoExport['name'];
  215. $params = $infoExport['params']?:[];
  216. $where = [];
  217. if($params){
  218. if (isset($params['select_ids']) && !empty($params['select_ids'])) {
  219. $where[] = ['id','in' ,$params['select_ids']];
  220. }
  221. if (isset($params['equity_id']) && !empty($params['equity_id'])) {
  222. $where[] = ['equity_id','=' ,$params['equity_id']];
  223. }
  224. if (isset($params['mobile']) && !empty($params['mobile'])) {
  225. $userId = User::where('mobile',$params['mobile'])->value('id')??-1;
  226. $where[] = ['user_id','=' ,$userId];
  227. }
  228. if (isset($params['is_binding']) && !empty($params['is_binding'])) {
  229. if((int)$params['is_binding'] === 1){
  230. $where[] = ['user_id','>',0];
  231. }else{
  232. $where[] = ['user_id','=',0];
  233. }
  234. }
  235. }
  236. $lists = UserEquity::with(['equityConfig', 'goods','user'])
  237. ->where($where)
  238. ->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'])
  239. ->select()
  240. ->toArray();
  241. $this->sheet->generateExcelFile([
  242. '用户','编码', '权益卡名称','商品','预售价','权益总次数','剩余次数','权益截止时间'
  243. ], $lists,$filename.'-'.$infoExport->id, ['user.nickname','code','equityConfig.equity_name','goods.goods_name','equityConfig.price','equityConfig.number','number','end_time']);
  244. $infoExport->file_url = $this->sheet->fileUrl();
  245. $infoExport->generate_status = 1;
  246. $infoExport->updatetime = time();
  247. $infoExport->save();
  248. return $infoExport->file_url;
  249. }catch (\Exception $e){
  250. throw new Exception($e->getMessage());
  251. }
  252. }
  253. }