ExcelExportService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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)
  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->getDownloadFileUrl(invoke(str_replace('.', '\\', App::getNamespace() . '\\lists\\' . $use_file[0] . '\\'. ucwords($use_file[1]))));
  62. break;
  63. }
  64. }
  65. }catch (\Exception $e){
  66. Log::info("download-error:{$id}:".$e->getMessage());
  67. throw new Exception($e->getMessage());
  68. }
  69. }
  70. // 获取下载文件路径
  71. public function getDownloadFileUrl($downloadObj){
  72. $lists = $downloadObj->excelExportList($this->infoExport['params']?:[]);
  73. $filename = $downloadObj->setFileName()?:($this->infoExport['name']?:'');
  74. $fields = $downloadObj->setExcelComplexFields();
  75. if(!$lists || !$fields){
  76. throw new Exception('lists或fields不存在');
  77. }
  78. $this->sheet->generateExcelFile($fields['zh_cn_fields'], $lists,$filename.'-'.$this->infoExport->id, $fields['data_fields']);
  79. $this->infoExport->file_url = $this->sheet->fileUrl();
  80. $this->infoExport->generate_status = 1;
  81. $this->infoExport->updatetime = time();
  82. $this->infoExport->save();
  83. return $this->infoExport->file_url;
  84. }
  85. /**
  86. * @notes 工程师 -截止某日最后结算余额 导出
  87. */
  88. public function EngineerBillDownload($infoExport)
  89. {
  90. Db::startTrans();
  91. try{
  92. $filename = $infoExport['name'];
  93. $params = $infoExport['params']?:[];
  94. if($params){ }
  95. // 导出逻辑
  96. /*添加财务打款记录表: 生成批次编号、状态待上传 已取消 打款状态: 未打款、部分打款、全部打款
  97. 生成数据表格:工程师信息、银行卡信息、批次编号信息、应发金额、导出时账号余额、导入时账号余额、扣款金额、扣款说明、实发金额、打款状态(已打、下次打款)、打款时间、备注、是否已导入
  98. (账号当前余额 !=0) < (应发金额 !=0) = 下次打款
  99. 更新表 la_master_settlement_details 批次编号、状态-待算
  100. */
  101. $batch_number = 'bn' . date('YmdHis') . rand(1000, 9999);
  102. // 添加财务打款记录表 -- 批次编号、状态待上传
  103. $params['infoExport_id'] = $infoExport->id;
  104. FinancialPaymentRecords::create([
  105. 'export_conditions' => $params,
  106. 'batch_number' => $batch_number,
  107. 'upload_status' => 1,
  108. 'payment_status' => 1,
  109. ]);
  110. //生成数据表格 & 更新表 -- 批次编号、状态-待算
  111. $lists = MasterSettlementDetailsLogic::upBatchStatus($params,$batch_number);
  112. $filename = ($filename?($filename.'-批号'.$batch_number):'');
  113. $this->sheet->generateExcelFile([
  114. '工程师ID', '工程师编号', '工程师姓名', '工程师银行卡号', '所属银行', '工程师开户行及支行信息','导出时账号余额',
  115. '批次编号',
  116. '应发金额', '扣款金额', '扣款说明', '实发金额', '打款时间','备注'
  117. ], $lists,$filename.'-'.$infoExport->id, [
  118. 'master_worker_id','worker_number','engineer_name','bank_account','bank_name','opening_branch','original_balance',
  119. 'batch_number',
  120. 'total_settlement_amount','','','','',''
  121. ]);
  122. $infoExport->name = $filename;
  123. $infoExport->file_url = $this->sheet->fileUrl();
  124. $infoExport->generate_status = 1;
  125. $infoExport->updatetime = time();
  126. $infoExport->save();
  127. Db::commit();
  128. return $infoExport->file_url;
  129. } catch (\Exception $e) {
  130. Db::rollback();
  131. throw new Exception($e->getMessage());
  132. }
  133. }
  134. /**
  135. * @notes 工程师 -工单统计 导出
  136. */
  137. /*public function MasterWorkerServiceOrderDownload($infoExport)
  138. {
  139. try{
  140. $filename = $infoExport['name'];
  141. $params = $infoExport['params']?:[];
  142. $where = [];
  143. $sqlJoin = '';
  144. if($params){
  145. if (isset($params['real_name']) && !empty($params['real_name'])) {
  146. $where[] = ['a.real_name','like' ,"%".$params['real_name']."%"];
  147. }
  148. if (isset($params['worker_number']) && !empty($params['worker_number'])) {
  149. $where[] = ['a.worker_number','like' ,"%".$params['worker_number']."%"];
  150. }
  151. if (isset($params['recruiting_behalf']) && !empty($params['recruiting_behalf'])) {
  152. $where[] = ['a.recruiting_behalf','like' ,"%".$params['recruiting_behalf']."%"];
  153. }
  154. if (isset($params['is_recruiting_behalf']) && !empty($params['is_recruiting_behalf'])) {
  155. if ($params['is_recruiting_behalf'] == 1)
  156. $where[] = ['a.recruiting_behalf','<>' ,""];
  157. else
  158. $where[] = ['a.recruiting_behalf','=' ,""];
  159. }
  160. if (isset($params['mobile']) && !empty($params['mobile'])) {
  161. $where[] = ['a.mobile','=' ,$params['mobile']];
  162. }
  163. if (isset($params['cooperation']) && !empty($params['cooperation'])) {
  164. $where[] = ['a.cooperation','=' ,$params['cooperation']];
  165. }
  166. if (!empty($params['start_time'])) {
  167. $sqlJoin .= ' AND b.update_time >= '.strtotime($params['start_time']);
  168. }
  169. if (!empty($params['end_time'])) {
  170. $sqlJoin .= ' AND b.update_time <= '.strtotime($params['end_time'])+86400;
  171. }
  172. }
  173. $lists = Db::name('master_worker')->alias('a')->field([
  174. 'a.id','a.real_name','a.nickname','a.worker_number','a.recruiting_behalf','a.mobile','a.cooperation','a.category_ids','a.labels','a.remark',
  175. Db::raw("COUNT(b.id) AS all_count"),
  176. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN 1 ELSE 0 END) AS success_count"),
  177. Db::raw("SUM(CASE WHEN b.service_status = 4 OR b.service_status = 5 THEN 1 ELSE 0 END) AS fail_count"),
  178. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN b.work_total ELSE 0 END) work_total"),
  179. Db::raw("SUM(CASE WHEN b.service_status = 3 THEN b.worker_price ELSE 0 END) worker_price"),
  180. ])
  181. ->leftJoin('service_work b', 'a.id = b.master_worker_id'.$sqlJoin)
  182. ->where($where)
  183. ->group('a.id')
  184. ->order('a.id desc')
  185. ->select()->toArray();
  186. $categoryData = GoodsCategory::select()->toArray();
  187. // foreach ($lists as &$item) {
  188. // $item['category_name'] = $item['category_ids']?implode('、',array_column(get_parent_info($categoryData,explode(',',$item['category_ids'])),'name')):'';
  189. // }
  190. foreach ($lists as &$item) {
  191. $item['labels'] = $item['labels']?array_map(function ($item) { return intval($item); },explode(',',$item['labels'])):'';
  192. $item['category_name'] = $item['category_ids']?implode('、',array_column(get_parent_info($categoryData,explode(',',$item['category_ids'])),'name')):'';
  193. $item['allocate_num'] = ServiceWorkAllocateWorkerLog::where(['master_worker_id'=>$item['id']])->count();
  194. }
  195. $this->sheet->generateExcelFile([
  196. '服务类别', '工程师编号', '工程师姓名', '工程师手机', '代招人姓名', '工程师接单数', '工程师成功单','工程师失败单','工程师成交金额','工程师提成金额'
  197. ], $lists,$filename.'-'.$infoExport->id, ['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. // 权益卡编码导出
  208. public function UserEquityDownload($infoExport){
  209. try{
  210. $filename = $infoExport['name'];
  211. $params = $infoExport['params']?:[];
  212. $where = [];
  213. if($params){
  214. if (isset($params['select_ids']) && !empty($params['select_ids'])) {
  215. $where[] = ['id','in' ,$params['select_ids']];
  216. }
  217. if (isset($params['equity_id']) && !empty($params['equity_id'])) {
  218. $where[] = ['equity_id','=' ,$params['equity_id']];
  219. }
  220. if (isset($params['mobile']) && !empty($params['mobile'])) {
  221. $userId = User::where('mobile',$params['mobile'])->value('id')??-1;
  222. $where[] = ['user_id','=' ,$userId];
  223. }
  224. if (isset($params['is_binding']) && !empty($params['is_binding'])) {
  225. if((int)$params['is_binding'] === 1){
  226. $where[] = ['user_id','>',0];
  227. }else{
  228. $where[] = ['user_id','=',0];
  229. }
  230. }
  231. }
  232. $lists = UserEquity::with(['equityConfig', 'goods','user'])
  233. ->where($where)
  234. ->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'])
  235. ->select()
  236. ->toArray();
  237. $this->sheet->generateExcelFile([
  238. '用户','编码', '权益卡名称','商品','预售价','权益总次数','剩余次数','权益截止时间'
  239. ], $lists,$filename.'-'.$infoExport->id, ['user.nickname','code','equityConfig.equity_name','goods.goods_name','equityConfig.price','equityConfig.number','number','end_time']);
  240. $infoExport->file_url = $this->sheet->fileUrl();
  241. $infoExport->generate_status = 1;
  242. $infoExport->updatetime = time();
  243. $infoExport->save();
  244. return $infoExport->file_url;
  245. }catch (\Exception $e){
  246. throw new Exception($e->getMessage());
  247. }
  248. }
  249. }