DrugReportInfoController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Servers\DrugReportInfoService;
  4. use App\Models\DrugReportAss;
  5. use App\Servers\Aliyun\Oss;
  6. use Carbon\Carbon;
  7. use ZipArchive;
  8. use Illuminate\Http\JsonResponse;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\DB;
  11. class DrugReportInfoController extends Api
  12. {
  13. /**
  14. * 查询医药报告选项
  15. */
  16. public function get_list(DrugReportAss $DrugReportAss)
  17. {
  18. $start_time = request('start_time', 0);
  19. $end_time = request('end_time', 0);
  20. $produce_ent_id = request('produce_ent_id', 0);
  21. $from_ref_ent_id = request('from_ref_ent_id', 0);
  22. $bill_code = request('bill_code', 0);
  23. $physic_name = request('physic_name', 0);
  24. $batch_no = request('batch_no', 0);
  25. $drug_report_sign_status = request('drug_report_sign_status');
  26. $seal_status = request('seal_status');
  27. $limit = request('limit',10);
  28. $map = [];
  29. if ($start_time){
  30. $start_time = strtotime($start_time);
  31. $map[] = ['drug_report_ass.bill_time','>=', $start_time];
  32. }
  33. if ($end_time){
  34. $end_time = Carbon::parse($end_time)->endOfDay()->timestamp;
  35. $map[] = ['drug_report_ass.bill_time','<=', $end_time];
  36. }
  37. if ($produce_ent_id) $map[] = ['drug_report_ass.produce_ent_id','=', $produce_ent_id];
  38. if ($from_ref_ent_id) $map[] = ['drug_report_ass.from_ref_ent_id','=', $from_ref_ent_id];
  39. if ($bill_code) $map[] = ['drug_report_ass.bill_code','=', $bill_code];
  40. if ($physic_name) $map[] = ['drug_report_ass.physic_name','=', $physic_name];
  41. if ($batch_no) $map[] = ['drug_report_ass.batch_no','=', $batch_no];
  42. if (!is_null($seal_status)) $map[] = ['drug_report_ass.seal_status','=', $seal_status];
  43. $countMap = $map;
  44. if (!is_null($drug_report_sign_status)) $map[] = ['drug_report_ass.drug_report_sign_status','=', $drug_report_sign_status];
  45. $Paginator = $DrugReportAss->query()
  46. /*->leftJoin('drug_report_info', function ($join) {
  47. $join->on('drug_report_ass.batch_no', '=', 'drug_report_info.batch_no')
  48. ->on('drug_report_ass.drug_id', '=', 'drug_report_info.drug_id');
  49. })*/
  50. ->where($map)
  51. ->select( ['drug_report_ass.id','drug_report_ass.bill_code','drug_report_ass.batch_no','drug_report_ass.drug_id','drug_report_ass.crt_date',
  52. 'drug_report_ass.produce_ent_id','drug_report_ass.produce_ent_name','drug_report_ass.from_ref_ent_id',
  53. 'drug_report_ass.from_ent_name','drug_report_ass.drug_report_sign_status','drug_report_ass.seal_status',
  54. 'drug_report_ass.seal_report_url','drug_report_ass.report_name','drug_report_ass.physic_name','drug_report_ass.pkg_spec','drug_report_ass.prepn_spec','drug_report_ass.prepn_type_desc',
  55. DB::raw('(
  56. SELECT drug_report_info.seal_report_url
  57. FROM drug_report_info
  58. WHERE drug_report_info.batch_no = drug_report_ass.batch_no
  59. AND drug_report_info.drug_id = drug_report_ass.drug_id
  60. ORDER BY drug_report_info.id DESC LIMIT 1
  61. ) as info_seal_report_url'),
  62. DB::raw('(
  63. SELECT drug_report_info.is_seal
  64. FROM drug_report_info
  65. WHERE drug_report_info.batch_no = drug_report_ass.batch_no
  66. AND drug_report_info.drug_id = drug_report_ass.drug_id
  67. ORDER BY drug_report_info.id DESC LIMIT 1
  68. ) as is_seal')
  69. ])
  70. ->orderBy('id', 'desc')
  71. ->paginate($limit);
  72. foreach ($Paginator->items() as &$item){
  73. if (in_array($item['seal_status'],[5,6])){
  74. if ($item['info_seal_report_url']){
  75. $item['seal_report_url'] = $item['info_seal_report_url'];
  76. }
  77. }
  78. }
  79. $countData = $DrugReportAss->query()
  80. ->where($countMap)
  81. ->select(['drug_report_sign_status',DB::raw('count(*) as count')])
  82. ->groupBy('drug_report_sign_status')
  83. ->get();
  84. $count = $DrugReportAss->query()
  85. ->where($countMap)
  86. ->count();
  87. $data['count'] = $count;
  88. $data['pending_send_count'] = 0;
  89. $data['pending_signed_count'] = 0;
  90. $data['signed_count'] = 0;
  91. $data['refuse_count'] = 0;
  92. foreach ($countData as $value){
  93. switch ($value['drug_report_sign_status']){
  94. case 0:
  95. $data['pending_send_count'] = $value['count'];
  96. break;
  97. case 2:
  98. $data['pending_signed_count'] = $value['count'];
  99. break;
  100. case 3:
  101. $data['signed_count'] = $value['count'];
  102. break;
  103. case 4:
  104. $data['refuse_count'] = $value['count'];
  105. break;
  106. }
  107. }
  108. // 获取数据
  109. $data['total'] = $Paginator->total();
  110. $data['current_page'] = $Paginator->currentPage();
  111. $data['per_page'] = $Paginator->perPage();
  112. $data['last_page'] = $Paginator->lastPage();
  113. $data['data'] = $Paginator->items();
  114. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  115. }
  116. /**
  117. * 下载包
  118. *
  119. * */
  120. public function down_excel(DrugReportAss $DrugReportAss)
  121. {
  122. $ids = request('ids','');
  123. $list = $DrugReportAss->query();
  124. if($ids){
  125. $ids = explode(',',$ids);
  126. $list = $list->whereIn('drug_report_ass.id',$ids);
  127. }
  128. $list = $list->orderBy('drug_report_ass.id', 'desc')
  129. ->select( ['drug_report_ass.seal_status','drug_report_ass.bill_code','drug_report_ass.batch_no','drug_report_ass.drug_id','drug_report_ass.physic_name','drug_report_ass.crt_date','drug_report_ass.seal_report_url','drug_report_ass.report_name',
  130. DB::raw('(
  131. SELECT drug_report_info.seal_report_url
  132. FROM drug_report_info
  133. WHERE drug_report_info.batch_no = drug_report_ass.batch_no
  134. AND drug_report_info.drug_id = drug_report_ass.drug_id
  135. ORDER BY drug_report_info.id DESC LIMIT 1
  136. ) as info_seal_report_url')])
  137. ->get()
  138. ->toArray();
  139. if (!$list) return json_send(['code'=>'error','msg'=>'没有数据']);
  140. $localUrl = $this->down($list);
  141. $fileName = basename($localUrl);
  142. //上传oss
  143. $oss = new Oss();
  144. $url = $oss->uploadFile($fileName,$localUrl,'','zip');
  145. unlink($localUrl);
  146. return json_send(['code'=>'success','msg'=>'下载成功','data'=>['url'=>$url]]);
  147. }
  148. /**
  149. * 下载文件到本地
  150. *
  151. * */
  152. public function down($list){
  153. // 创建临时目录
  154. $tempDir = public_path('/uploads/' . uniqid('drug_'));
  155. if (!file_exists($tempDir)) {
  156. mkdir($tempDir, 0755, true);
  157. }
  158. // 下载图片到临时目录
  159. foreach ($list as $item) {
  160. if (in_array($item['seal_status'],[5,6]) && $item['info_seal_report_url']){
  161. $item['seal_report_url'] = $item['info_seal_report_url'];
  162. }
  163. if (!$item['seal_report_url']) continue;
  164. try {
  165. $content = file_get_contents($item['seal_report_url']);
  166. $localPath = $tempDir . '/' . $item['report_name'];
  167. file_put_contents($localPath, $content);
  168. } catch (\Exception $e) {
  169. return json_send(['code'=>'error','msg'=>'下载失败','data'=>$e->getMessage()]);
  170. }
  171. }
  172. // 创建 ZIP 文件
  173. $zipPath = public_path('/uploads/zip/' . uniqid('drug_')).'.zip';
  174. $zip = new ZipArchive();
  175. if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === TRUE) {
  176. $files = scandir($tempDir);
  177. foreach ($files as $file) {
  178. if ($file != "." && $file != "..") {
  179. $filePath = $tempDir . '/' . $file;
  180. if (file_exists($filePath)) {
  181. $zip->addFile($filePath, $file);
  182. }
  183. }
  184. }
  185. $zip->close();
  186. }
  187. // 清理临时目录
  188. $this->deleteDir($tempDir);
  189. return $zipPath;
  190. }
  191. /**
  192. * 删除目录及其内容
  193. */
  194. private function deleteDir(string $dirPath): void
  195. {
  196. if (!is_dir($dirPath)) {
  197. return;
  198. }
  199. $files = scandir($dirPath);
  200. foreach ($files as $file) {
  201. if ($file != "." && $file != "..") {
  202. $filePath = $dirPath . '/' . $file;
  203. if (is_dir($filePath)) {
  204. $this->deleteDir($filePath);
  205. } else {
  206. unlink($filePath);
  207. }
  208. }
  209. }
  210. rmdir($dirPath);
  211. }
  212. /**
  213. * 查询医药报告选项
  214. */
  215. public function option( DrugReportInfoService $service)
  216. {
  217. try {
  218. $data = [
  219. 'produce_ent' => $service->getProduceEntIdOption(),
  220. 'from_ref_ent' => $service->getFromRefEntIdOption(),
  221. ];
  222. return json_send(['code'=>'success','msg'=>'获取成功','data'=>$data]);
  223. } catch (\Exception $e) {
  224. return json_send(['code'=>'error','msg'=>'获取失败','data'=>$e->getMessage()]);
  225. }
  226. }
  227. }