ExcelRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\store;
  12. use app\common\dao\store\ExcelDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\system\admin\AdminRepository;
  15. use app\common\repositories\system\merchant\MerchantAdminRepository;
  16. use crmeb\services\ExcelService;
  17. use think\facade\Db;
  18. use think\facade\Queue;
  19. use crmeb\jobs\SpreadsheetExcelJob;
  20. class ExcelRepository extends BaseRepository
  21. {
  22. /**
  23. * @var ExcelDao
  24. */
  25. protected $dao;
  26. /**
  27. * StoreAttrTemplateRepository constructor.
  28. * @param ExcelDao $dao
  29. */
  30. public function __construct(ExcelDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 创建Excel并推送处理任务到队列
  36. *
  37. * 本函数用于根据提供的条件创建一个Excel文件,并将相应的处理任务推送到队列中去。
  38. * 这样做的目的是为了异步处理Excel相关的工作,提高系统的响应速度和处理能力。
  39. *
  40. * @param array $where 创建Excel的条件数组
  41. * @param int $admin_id 创建Excel的管理员ID
  42. * @param string $type Excel的类型,用于确定具体的处理逻辑
  43. * @param int $merId 商户ID,用于标识Excel的归属
  44. */
  45. public function create(array $where ,int $admin_id, string $type,int $merId)
  46. {
  47. // 通过DAO层创建Excel,指定商户ID、管理员ID和Excel类型
  48. $excel = $this->dao->create([
  49. 'mer_id' => $merId,
  50. 'admin_id' => $admin_id,
  51. 'type' => $type
  52. ]);
  53. // 构造任务数据数组,包含条件、类型和新创建的Excel的ID
  54. $data = ['where' => $where,'type' => $type,'excel_id' => $excel->excel_id];
  55. // 将Excel处理任务推送到队列中
  56. Queue::push(SpreadsheetExcelJob::class,$data);
  57. // 注释掉的代码可能是用于调试或备用的,保留注释可以提供一些历史信息或备选方案
  58. // app()->make(ExcelService::class)->$type($where,1);
  59. }
  60. /**
  61. * 根据条件获取分页列表。
  62. *
  63. * 该方法用于根据给定的条件从数据库中检索分页数据列表。它涉及到两个不同的管理员仓库,
  64. * 用于处理商家管理员和系统管理员的数据。通过对查询结果进行遍历,将管理员的实名
  65. * 填充到结果中,以便返回的数据更加完整。
  66. *
  67. * @param array $where 查询条件数组。
  68. * @param int $page 当前页码。
  69. * @param int $limit 每页数据条数。
  70. * @return array 返回包含总数和列表数据的数组。
  71. */
  72. public function getList(array $where,int $page, int $limit)
  73. {
  74. // 创建商家管理员仓库实例
  75. $mer_make = app()->make(MerchantAdminRepository::class);
  76. // 创建系统管理员仓库实例
  77. $sys_make = app()->make(AdminRepository::class);
  78. // 根据条件构造查询
  79. $query = $this->dao->search($where);
  80. // 计算总条数
  81. $count = $query->count();
  82. // 进行分页查询,并对结果进行处理
  83. $list = $query->page($page,$limit)->select()
  84. ->each(function($item) use ($mer_make,$sys_make){
  85. // 检查是否存在管理员ID,并且不为空
  86. if (isset($item['admin_id']) && $item['admin_id']) {
  87. // 根据商家ID判断是商家管理员还是系统管理员
  88. if($item['mer_id']){
  89. $admin = $mer_make->get($item['admin_id']);
  90. }else{
  91. $admin = $sys_make->get($item['admin_id']);
  92. }
  93. // 将管理员实名填充到结果中
  94. return $item['admin_id'] = $admin['real_name'] ??"";
  95. }
  96. });
  97. // 返回分页结果
  98. return compact('count','list');
  99. }
  100. /**
  101. * 删除文件
  102. * @param int $id
  103. * @param string $path
  104. * @author Qinii
  105. * @day 2020-08-15
  106. */
  107. public function del(int $id,?string $path)
  108. {
  109. Db::transaction(function()use($id,$path){
  110. $this->dao->delete($id);
  111. if(!is_null($path)){
  112. $path = app()->getRootPath().'public'.$path;
  113. if(file_exists($path))unlink($path);
  114. }
  115. });
  116. }
  117. }