MakeWord.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Support\Traits;
  11. use App\Repositories\Enums\ResponseCodeEnum;
  12. use App\Repositories\Models\Base\CategorySetting;
  13. use Carbon\Carbon;
  14. use Illuminate\Support\Facades\File;
  15. use Illuminate\Support\Facades\Storage;
  16. use PhpOffice\PhpWord\Element\Table;
  17. use PhpOffice\PhpWord\SimpleType\JcTable;
  18. use PhpOffice\PhpWord\TemplateProcessor;
  19. trait MakeWord
  20. {
  21. /**
  22. * 获取目录
  23. * @param $model
  24. * @return string
  25. */
  26. public static function getDirectory($model, $isAllPath = true)
  27. {
  28. $time = Carbon::parse($model->updated_at)->timestamp;
  29. if (!$isAllPath) return "jlb/{$model->id}/{$time}";
  30. $directory = Storage::disk('public')->path("jlb/{$model->id}/{$time}");
  31. return $directory;
  32. }
  33. /**
  34. * 是否存在
  35. * @param $model
  36. * @return bool
  37. */
  38. public static function isExists($model)
  39. {
  40. $time = Carbon::parse($model->updated_at)->timestamp;
  41. $directory = Storage::disk('public')->path("jlb/{$model->id}/{$time}");
  42. $filename = $directory . '.zip';
  43. return file_exists($filename);
  44. }
  45. /**
  46. * 是否存在
  47. * @param $model
  48. * @return bool
  49. */
  50. public static function isZipExists($directory)
  51. {
  52. $filename = Storage::disk('public')->path($directory . '.zip');
  53. return file_exists($filename);
  54. }
  55. /**
  56. * 下载文件
  57. * @param $model
  58. * @return false|void
  59. */
  60. public static function downZip($model)
  61. {
  62. $time = Carbon::parse($model->updated_at)->timestamp;
  63. $directory = Storage::disk('public')->path("jlb/{$model->id}/{$time}");
  64. $filename = $directory . '.zip';
  65. if (!file_exists($filename)) {
  66. //下载文件
  67. return false;
  68. }
  69. $name = "{$model->project_name}_{$time}.zip";
  70. // 设置头部以便下载
  71. header('Content-Type: application/zip');
  72. header('Content-Disposition: attachment; filename="' . $name . '"');
  73. header('Content-Length: ' . filesize($filename));
  74. // 读取 ZIP 文件并输出到浏览器
  75. readfile($filename);
  76. }
  77. /**
  78. * 删除文件夹
  79. * @param $model
  80. * @return true
  81. */
  82. public static function clearDirectory($model)
  83. {
  84. $time = Carbon::parse($model->updated_at)->timestamp;
  85. $storage_directory = "jlb/{$model->id}/{$time}";
  86. Storage::disk('public')->deleteDirectory($storage_directory);
  87. return true;
  88. }
  89. /**
  90. * 打包文件
  91. * @param $model
  92. * @return true
  93. */
  94. public static function packDirectory($model)
  95. {
  96. $time = Carbon::parse($model->updated_at)->timestamp;
  97. $zipname = Storage::disk('public')->path("jlb/{$model->id}/{$time}.zip");
  98. $storage_directory = "jlb/{$model->id}/{$time}";
  99. $zip = new \ZipArchive();
  100. $zip->open($zipname, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  101. // 如果你想要递归获取所有子目录中的文件,可以使用 directories 方法
  102. $files = Storage::disk('public')->allFiles($storage_directory);
  103. // 输出文件列表
  104. foreach ($files as $file) {
  105. $zip->addFile(Storage::disk('public')->path($file), str_replace($storage_directory . '/', '', $file)); // 使用 basename() 保留原始文件名
  106. }
  107. $zip->close();
  108. return true;
  109. }
  110. /**
  111. * 打包
  112. * @param $zipname
  113. * @param $storage_directory
  114. * @return true
  115. */
  116. public static function pack($zipname, $storage_directory)
  117. {
  118. $zip = new \ZipArchive();
  119. $zip->open($zipname, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
  120. // 如果你想要递归获取所有子目录中的文件,可以使用 directories 方法
  121. $files = Storage::disk('public')->allFiles($storage_directory);
  122. // 输出文件列表
  123. foreach ($files as $file) {
  124. $zip->addFile(Storage::disk('public')->path($file), str_replace($storage_directory . '/', '', $file)); // 使用 basename() 保留原始文件名
  125. }
  126. $zip->close();
  127. return true;
  128. }
  129. /**
  130. * 打包并下载文件
  131. * @param $zipname
  132. * @param $storage_directory
  133. * @return false|void
  134. */
  135. public static function packDownZip($zipname, $directory)
  136. {
  137. $filename = Storage::disk('public')->path($directory);
  138. if (!file_exists($filename)) {
  139. //下载文件
  140. return false;
  141. }
  142. $filename = $filename . '.zip';
  143. //是否存在 zip
  144. if (!file_exists($filename)) {
  145. self::pack($filename, $directory);
  146. }
  147. // // 设置头部以便下载
  148. header('Content-Type: application/zip');
  149. header('Content-Disposition: attachment; filename="' . $zipname . '"');
  150. header('Content-Length: ' . filesize($filename));
  151. // 读取 ZIP 文件并输出到浏览器
  152. readfile($filename);
  153. }
  154. /**
  155. * 生成 word
  156. * @param $model
  157. * @param $template_type
  158. * @return true|null|false
  159. */
  160. public static function makeWord($model, $template_type)
  161. {
  162. $time = Carbon::parse($model->updated_at)->timestamp;
  163. $directory = Storage::disk('public')->path("jlb/{$model->id}/{$time}");
  164. $path = base_path("public/template/ant/template_{$template_type}.docx");
  165. $is = file_exists($path);
  166. if (!$is) {
  167. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '模板文件找不到');
  168. return false;
  169. }
  170. $br = '<w:br />';
  171. try {
  172. $template = new TemplateProcessor($path);
  173. //1、基本信息
  174. $template->setValue('dan_name', $model->danger_cell);
  175. $template->setValue('start_date', Carbon::parse($model->day)->format("Y年 m月 d日"));
  176. $template->setValue('end_date', Carbon::parse($model->end_day)->format("Y年 m月 d日"));
  177. $template->setValue('project_name', $model->project_name);
  178. $template->setValue('project_level_name', $model->project_level_name);
  179. $types = [
  180. '土坝', '土石坝', '其它坝', '土堤', '土石堤', '其它堤'
  181. ];
  182. $projectArr = [];
  183. foreach ($types as $type) {
  184. if ($model->project_type_name == $type) {
  185. $projectArr[] = "☑{$type}";
  186. continue;
  187. }
  188. $projectArr[] = "□{$type}";
  189. }
  190. $template->setValue('project_type', arr2str($projectArr, ' '));
  191. //周边环境
  192. $environment = '';
  193. if ($model->is_environment && is_array($model->environment)) {
  194. $environment .= '(1)周围环境:';
  195. $es = [];
  196. foreach ($model->environment as $item) {
  197. $cs = [];
  198. if (!is_array($item['select'])) continue;
  199. foreach ($item['select'] as $val) {
  200. if ($val['checked']) {
  201. $cs[] = $val['name'];
  202. }
  203. }
  204. if (count($cs)) {
  205. $es[] = "{$item['name']}:" . arr2str($cs, '、');
  206. }
  207. }
  208. $environment .= arr2str($es, ';');
  209. $environment .= $br;
  210. } else {
  211. $environment .= '(1)周围环境:无';
  212. $environment .= $br;
  213. }
  214. if ($model->is_turang) {
  215. $environment .= "(2)土壤信息:";
  216. $ts = [];
  217. if ($model->turang_day) {
  218. $ts[] = '采集日期:' . Carbon::parse($model->turang_day)->format('Y-m-d');
  219. }
  220. if ($model->turang_buwei) {
  221. $ts[] = '采集部位:' . $model->turang_buwei;
  222. }
  223. if ($model->turang_ph) {
  224. $ts[] = 'ph值:' . $model->turang_ph;
  225. }
  226. if ($model->turang_wendu) {
  227. $ts[] = "温度:{$model->turang_wendu}°C";
  228. }
  229. if ($model->turang_shidu) {
  230. $ts[] = "湿度:{$model->turang_shidu}RH";
  231. }
  232. $environment .= arr2str($ts, ';');
  233. $environment .= $br;
  234. } else {
  235. $environment .= '(2)土壤信息:无';
  236. $environment .= $br;
  237. }
  238. if ($model->is_danger_plants && is_array($model->danger_plants)) {
  239. $ps = [];
  240. foreach ($model->danger_plants['select'] as $item) {
  241. if ($item['checked']) {
  242. $ps[] = $item['name'];
  243. }
  244. }
  245. if ($model->danger_plants['others']) $ps[] = $model->danger_plants['others'];
  246. $environment .= '(3)蚁患区主要植被:' . arr2str($ps, '、');
  247. $environment .= $br;
  248. } else {
  249. $environment .= '(3)蚁患区主要植被:无';
  250. $environment .= $br;
  251. }
  252. if ($model->is_area_plants && is_array($model->area_plants)) {
  253. $ps = [];
  254. foreach ($model->area_plants['select'] as $item) {
  255. if ($item['checked']) {
  256. $ps[] = $item['name'];
  257. }
  258. }
  259. if ($model->area_plants['others']) $ps[] = $model->area_plants['others'];
  260. $environment .= '(4)蚁源区主要植被:' . arr2str($ps, '、');
  261. $environment .= $br;
  262. } else {
  263. $environment .= '(4)蚁源区主要植被:无';
  264. $environment .= $br;
  265. }
  266. if ($model->turang_body) {
  267. $environment .= '(5)情况描述:' . $model->turang_body;
  268. } else {
  269. $environment .= '(5)情况描述:无';
  270. }
  271. $template->setValue('environment', $environment);
  272. //2、白蚁危害
  273. $danger_types = $model->danger_types;
  274. $dangerArr = [];
  275. // $isBaiyi = false;
  276. if (is_array($danger_types) && array_key_exists('select', $danger_types)) {
  277. foreach ($danger_types['select'] as $item) {
  278. if ($item['checked']) {
  279. $dangerArr[] = "☑{$item['name']}";
  280. // $isBaiyi = true;
  281. continue;
  282. }
  283. $dangerArr[] = "□{$item['name']}";
  284. }
  285. if ($danger_types['others']) $dangerArr[2] = $dangerArr[2] . ':' . $danger_types['others'];
  286. }
  287. // if ($isBaiyi) {
  288. // array_unshift($dangerArr, "☑无白蚁");
  289. // } else {
  290. // array_unshift($dangerArr, "□无白蚁");
  291. // }
  292. $template->setValue('danger_type', arr2str($dangerArr, ' '));
  293. $danger = '';
  294. $danger_area = '';
  295. $danger .= "是否有蚁患:" . ($model->is_ant_danger ? '是' : '否') . $br;
  296. if ($model->is_ant_danger) {
  297. if ($model->danger_area) {
  298. $danger .= "面积:{$model->danger_area}m²;{$br}";
  299. }
  300. if ($model->danger_feature && is_array($model->danger_feature_extend)) {
  301. $danger .= '外露特征:';
  302. $dangerArr = [];
  303. foreach ($model->danger_feature_extend as $item) {
  304. if ($item['nums']) {
  305. $dangerArr[] = "{$item['name']}{$item['nums']}处";
  306. }
  307. }
  308. $danger .= arr2str($dangerArr, '、');
  309. $danger .= $br;
  310. }
  311. if ($model->danger_hole) {
  312. $danger .= "分飞孔:{$model->danger_hole}个;";
  313. $danger .= $br;
  314. }
  315. }
  316. if ($model->danger_animal && is_array($model->danger_animal_extend)) {
  317. $danger .= '其他害堤动物外露特征:';
  318. $dangerArr = [];
  319. foreach ($model->danger_animal_extend as $item) {
  320. if ($item['nums']) {
  321. $dangerArr[] = "{$item['name']}{$item['nums']}处";
  322. }
  323. }
  324. $danger .= arr2str($dangerArr, '、');
  325. // $danger .= $br;
  326. } else {
  327. $danger .= '其他害堤动物外露特征:无';
  328. // $danger .= $br;
  329. }
  330. $danger_area .= "是否有蚁患:" . ($model->is_ant_area ? '是' : '否') . $br;
  331. if ($model->is_ant_area) {
  332. if ($model->area_area) {
  333. $danger_area .= "面积:{$model->area_area}m²;{$br}";
  334. }
  335. if ($model->area_feature && is_array($model->area_feature_extend)) {
  336. $danger_area .= '外露特征:';
  337. $dangerArr = [];
  338. foreach ($model->area_feature_extend as $item) {
  339. if ($item['nums']) {
  340. $dangerArr[] = "{$item['name']}{$item['nums']}处";
  341. }
  342. }
  343. $danger_area .= arr2str($dangerArr, '、');
  344. $danger_area .= $br;
  345. }
  346. if ($model->area_hole) {
  347. $danger_area .= "分飞孔:{$model->area_hole}个;";
  348. $danger_area .= $br;
  349. }
  350. if ($model->pd_sanjin) {
  351. $danger_area .= "散浸:{$model->pd_sanjin}处;";
  352. $danger_area .= $br;
  353. }
  354. if ($model->pd_shipo) {
  355. $danger_area .= "湿坡:{$model->pd_shipo}处;";
  356. $danger_area .= $br;
  357. }
  358. if ($model->pd_loudong) {
  359. $danger_area .= "漏洞:{$model->pd_loudong}处;";
  360. $danger_area .= $br;
  361. }
  362. if ($model->pd_diewo) {
  363. $danger_area .= "跌窝:{$model->pd_diewo}处;";
  364. $danger_area .= $br;
  365. }
  366. if ($model->pd_tuopo) {
  367. $danger_area .= "脱坡:{$model->pd_tuopo}处;";
  368. $danger_area .= $br;
  369. }
  370. $danger_area .= $br;
  371. }
  372. //其他区域
  373. $other = '';
  374. $settings = CategorySetting::settings();
  375. $yingxiang = '';
  376. foreach ($settings['data']['yingxiang'] as $setting) {
  377. if ($setting['id'] == $model->is_project_danger) $yingxiang = $setting['name'];
  378. }
  379. $other .= "对水工建筑物影响:{$yingxiang};" . $br;
  380. $other .= "是否有白蚁造成危害:" . ($model->is_ant_cause_danger ? '是' : '否') . ";" . $br;
  381. $other .= "是否有纷飞监测:" . ($model->is_fenfei ? '是' : '否') . ";";
  382. $template->setValue('other', $other);
  383. $danger = trim($danger, $br);
  384. $template->setValue('danger', $danger);
  385. $danger_area = trim($danger_area, $br);
  386. $template->setValue('danger_area', $danger_area);
  387. $template->setValue('danwei', $model->department);
  388. $template->setValue('admin_name', $model->admin ? $model->admin->name : '');
  389. $template->setValue('jianchashijian', Carbon::parse($model->created_at)->format('Y年 m月 d日'));
  390. $template->setValue('danger_level', $model->danger_level);
  391. $template->setValue('dan_cell', $model->danger_cell_code);
  392. switch ($template_type) {
  393. case 1:
  394. $filename = "3.白蚁危害检查表.docx";
  395. File::isDirectory($directory) or File::makeDirectory($directory, 0777, true, true);
  396. $file_path = "{$directory}/{$filename}";
  397. $template->saveAs($file_path);
  398. //附件
  399. $shiyitu = $model->shiyitu;
  400. if (is_array($shiyitu)) {
  401. $directory = "jlb/{$model->id}/{$time}";
  402. if (count($shiyitu) === 1) {
  403. foreach ($shiyitu as $k => $item) {
  404. $exception = File::extension($item['url']);
  405. $filename = $directory . "/4.白蚁危害分布图.{$exception}";
  406. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  407. }
  408. } else {
  409. foreach ($shiyitu as $k => $item) {
  410. $index = $k + 1;
  411. $exception = File::extension($item['url']);
  412. $filename = $directory . "/4.白蚁危害分布图/{$index}.{$exception}";
  413. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  414. }
  415. }
  416. }
  417. break;
  418. case 2:
  419. $filename = "2.白蚁危害等级评定表.docx";
  420. File::isDirectory($directory) or File::makeDirectory($directory, 0777, true, true);
  421. $file_path = "{$directory}/{$filename}";
  422. $template->saveAs($file_path);
  423. break;
  424. }
  425. } catch (\Exception $exception) {
  426. log_record('记录表生产出错', $exception);
  427. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '文件生成失败');
  428. }
  429. return true;
  430. }
  431. /**
  432. * 生成报告
  433. * @param $model
  434. * @return false|void
  435. */
  436. public static function makeBaoGao($model)
  437. {
  438. $time = Carbon::parse($model->updated_at)->timestamp;
  439. $directory = Storage::disk('public')->path("jlb/{$model->id}/{$time}");
  440. $template_type = 3;
  441. $path = base_path("public/template/ant/template_{$template_type}.docx");
  442. $is = file_exists($path);
  443. if (!$is) {
  444. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '模板文件找不到');
  445. return false;
  446. }
  447. $br = '<w:br />';
  448. try {
  449. $template = new TemplateProcessor($path);
  450. //1、基本信息
  451. $template->setValue('project_name', $model->project_name);
  452. //工程概况
  453. $gaikuangArr = [];
  454. $day = Carbon::parse($model->day)->format('Y年m月d日');
  455. $address = '';
  456. if ($model->province) $address .= $model->province;
  457. if ($model->city) $address .= $model->city;
  458. if ($model->area) $address .= $model->area;
  459. if ($model->zhen) $address .= $model->zhen;
  460. if ($model->cun) $address .= $model->cun;
  461. $dan_name = $model->danger_cell;
  462. $gaikuangArr[] = "{$day},我单位对{$dan_name}进行白蚁等害堤动物定期普查。该工程位于{$address},为{$model->project_level_name}{$model->project_type_name},坝型为{$model->project_dam_type_name}";
  463. if ($model->altitude) {
  464. $gaikuangArr[] = "工程所在地海拔{$model->altitude}m";
  465. }
  466. if ($model->rain) {
  467. $gaikuangArr[] = "年平均降雨量{$model->rain}mm";
  468. }
  469. if ($model->max_temperature) {
  470. $gaikuangArr[] = "年平均气温{$model->max_temperature}℃";
  471. }
  472. $cs = [];
  473. $is_qt = false;
  474. if ($model->is_environment && is_array($model->environment)) {
  475. foreach ($model->environment as $item) {
  476. foreach ($item['select'] as $val) {
  477. if ($val['checked']) {
  478. if ($val['name'] == '其它' || $val['name'] == '其他') {
  479. // $cs[] = $val['name'];
  480. $is_qt = true;
  481. } else {
  482. $cs[] = $val['name'];
  483. }
  484. }
  485. }
  486. }
  487. $cs = array_unique($cs);
  488. }
  489. if (count($cs)) {
  490. $gaikuangArr[] = '工程周边主要为' . arr2str($cs, '、') . '等地形';
  491. }
  492. //2024年6月3日,我单位对铜仁市沿河土家族自治县青杠溪水库进行白蚁等害堤动物定期普查。该工程位于贵州省铜仁市沿河土家族自治县甘溪镇关林村,为小(二)型土石坝,工程所在地海拔558m,年平均降雨量1140mm,年平均气温18.3℃,工程周边主要为旱地、灌木地,林地及其他地形。
  493. $gaikuang = arr2str($gaikuangArr, ',') . '。';
  494. $template->setValue('gongchenggaikuang', $gaikuang);
  495. //分飞监测
  496. $fenfei = '';
  497. $fenfei_baiyijianche = '';
  498. if ($model->is_fenfei) {
  499. $fday = Carbon::parse($model->fenfei_start_day)->format("Y年m月d日");
  500. $fenfei = "本工程实施了白蚁分飞监测,{$fday}监测到{$model->fenfei_nums}次分飞,监测到{$model->fenfei_body}种蚁种。";
  501. $fenfei_baiyijianche = "经对监测结果分析{$model->project_name}有白蚁危害。";
  502. } else {
  503. $fenfei = '本工程未进行白蚁危害监测。';
  504. $fenfei_baiyijianche = "未实施白蚁监测。";
  505. }
  506. $template->setValue('fenfei', $fenfei);
  507. //未实施白蚁监测(如有就填写“经对监测结果分析xx水库有白蚁危害”)。
  508. $template->setValue('fenfei_baiyijianche', $fenfei_baiyijianche);
  509. $baiyifenxiArr = [];
  510. $baiyifenxizongjie = '';
  511. if (!$model->is_ant_danger && !$model->is_ant_area) {
  512. $baiyifenxizongjie = "经对{$model->project_name}检查未发现白蚁外露特征及活动痕迹,未发现其他害堤动物活动痕迹。";
  513. } elseif ($model->is_ant_danger && !$model->is_ant_area) {
  514. $wltz = [];
  515. if (is_array($model->danger_feature_extend)) {
  516. foreach ($model->danger_feature_extend as $item) {
  517. $wltz[] = "{$item['name']}{$item['nums']}处";
  518. }
  519. }
  520. $wltz[] = "分飞孔{$model->danger_hole}个";
  521. $wltzs = arr2str($wltz, '、');
  522. $wltzs = "({$wltzs}。)";
  523. $baiyifenxiArr[] = "经对{$model->project_name}检查,蚁患区共发现有{$model->danger_feature}处外露特征{$wltzs}。";
  524. } elseif (!$model->is_ant_danger && $model->is_ant_area) {
  525. $wltz = [];
  526. if (is_array($model->area_feature_extend)) {
  527. foreach ($model->area_feature_extend as $item) {
  528. $wltz[] = "{$item['name']}{$item['nums']}处";
  529. }
  530. }
  531. $wltz[] = "分飞孔{$model->area_hole}个";
  532. $wltzs = arr2str($wltz, '、');
  533. $wltzs = "({$wltzs}。)";
  534. $baiyifenxiArr[] = "经对{$model->project_name}检查,蚁源区共发现有{$model->area_feature}处外露特征{$wltzs}。";
  535. } else {
  536. $wltz = [];
  537. if (is_array($model->danger_feature_extend)) {
  538. foreach ($model->danger_feature_extend as $item) {
  539. $wltz[] = "{$item['name']}{$item['nums']}处";
  540. }
  541. }
  542. $wltz[] = "分飞孔{$model->danger_hole}个";
  543. $wltzs = arr2str($wltz, '、');
  544. $wltzs = "({$wltzs}。)";
  545. $awltz = [];
  546. if (is_array($model->area_feature_extend)) {
  547. foreach ($model->area_feature_extend as $item) {
  548. $awltz[] = "{$item['name']}{$item['nums']}处";
  549. }
  550. }
  551. $awltz[] = "分飞孔{$model->area_hole}个";
  552. $awltz = arr2str($awltz, '、');
  553. $awltz = "({$awltz}。)";
  554. $baiyifenxiArr[] = "经对{$model->project_name}蚁患区{$model->danger_area}m²、蚁源区{$model->area_area}m²进行检查,蚁患区共有{$model->danger_feature}处外露特征{$wltzs}。蚁源区共发现有{$model->area_feature}处外露特征{$wltzs}。";
  555. }
  556. // $wltz = [];
  557. // if (is_array($model->danger_feature_extend)) {
  558. // foreach ($model->danger_feature_extend as $item) {
  559. // $wltz[] = "{$item['name']}{$item['nums']}处";
  560. // }
  561. // }
  562. // $wltz[] = "分飞孔{$model->danger_hole}个";
  563. // $wltzs = arr2str($wltz, '、');
  564. // $wltzs = "({$wltzs}。)";
  565. // $baiyifenxiArr[] = "经对{$model->project_name}蚁患区{$model->danger_area}m²、蚁源区{$model->area_area}m²进行检查,蚁患区共有{$model->danger_feature}处外露特征{$wltzs}。";
  566. if ($model->pd_sanjin || $model->pd_shipo || $model->pd_loudong || $model->pd_diewo || $model->pd_tuopo) {
  567. $byzc = "非白蚁造成危害";
  568. if ($model->is_ant_cause_danger) {
  569. $byzc = "为白蚁造成危害";
  570. }
  571. $baiyifenxiArr[] = "散浸{$model->pd_sanjin}处、湿坡{$model->shipo}处、漏洞{$model->loudong}处、跌窝{$model->pd_diewo}处、脱坡{$model->tuopo}处,{$byzc}";
  572. } else {
  573. $baiyifenxiArr[] = "无散浸、湿坡、漏洞、跌窝、脱坡等现象。";
  574. }
  575. $danger_types = $model->danger_types;
  576. $dangerArr = [];
  577. if (is_array($danger_types) && array_key_exists('select', $danger_types)) {
  578. foreach ($danger_types['select'] as $item) {
  579. if ($item['checked']) {
  580. $dangerArr[] = "{$item['name']}";
  581. }
  582. }
  583. if ($danger_types['others']) $dangerArr[] = $danger_types['others'];
  584. }
  585. $p3 = '';
  586. $isDanger = false;
  587. //检查到蚁种为黑翅土白蚁,检查未发现其他害堤动物(检查发现其他害堤动物外露特征 处,(獾 处、鼠 处、狐 处、其他动物 处))
  588. if (count($dangerArr)) {
  589. $isDanger = true;
  590. $p3 = "检查到蚁种为" . arr2str($dangerArr, '、');
  591. } else {
  592. $p3 = "检查未发现蚁种";
  593. }
  594. $dangerAnimalArr = [];
  595. if ($model->danger_animal) {
  596. $danger_animals = $model->danger_animal_extend;
  597. foreach ($danger_animals as $item) {
  598. $dangerAnimalArr[] = "{$item['name']}{$item['nums']}处";
  599. }
  600. }
  601. $p4 = '';
  602. $isDangerAnimal = false;
  603. //检查到蚁种为黑翅土白蚁,检查未发现其他害堤动物(检查发现其他害堤动物外露特征 处,(獾 处、鼠 处、狐 处、其他动物 处))
  604. if (count($dangerAnimalArr)) {
  605. $dws = arr2str($dangerAnimalArr, '、');
  606. $isDangerAnimal = true;
  607. $p4 = "检查发现其他害堤动物外露特征{$model->danger_animal}处({$dws})。";
  608. } else {
  609. $p4 = "检查未发现其他害堤动物。";
  610. }
  611. $baiyifenxiArr[] = "{$p3},{$p4}";
  612. $baiyifenxi = arr2str($baiyifenxiArr, '');
  613. //经对青杠溪水库蚁患区 m²、蚁源区 m²进行检查,蚁患区共有11处外露特征(泥被、泥线11处、真菌指示物0处、白蚁活动痕迹0处、蚁道0处、其他外露特征0处、分飞孔0处。)。蚁源区共有1处外露特征(泥被、泥线1处、真菌指示物0处、白蚁活动痕迹0处、蚁道0处、其他外露特征0处、分飞孔0处。)。无散浸、湿坡、漏洞、跌窝、脱坡等现象,(散浸 处、湿坡 处、漏洞 处、跌窝 处、脱坡 处,为白蚁造成危害{非白蚁造成危害})。检查到蚁种为黑翅土白蚁,检查未发现其他害堤动物(检查发现其他害堤动物外露特征 处,(獾 处、鼠 处、狐 处、其他动物 处))(括号为“如有的情况下”段落为固定格式)。
  614. $template->setValue('baiyifenxi', $baiyifenxi);
  615. if ($isDanger == false && $isDangerAnimal == false) {
  616. $baiyifenxizongjie = "根据检查结果分析,{$model->project_name}没有白蚁危害。";
  617. } else {
  618. $baiyifenxizongjieArr[] = '根据检查结果分析';
  619. if ($isDanger) {
  620. $my = arr2str($dangerArr, '、');
  621. $baiyifenxizongjieArr[] = "{$model->project_name}有{$my}危害";
  622. if ($isDangerAnimal) {
  623. $baiyifenxizongjieArr[] = "及其他害堤动物危害";
  624. }
  625. } else {
  626. if ($isDangerAnimal) {
  627. $baiyifenxizongjieArr[] = "{$model->project_name}有其他害堤动物危害";
  628. }
  629. }
  630. $baiyifenxizongjie = arr2str($baiyifenxizongjieArr, ',') . '。';
  631. }
  632. $template->setValue('baiyifenxizongjie', $baiyifenxizongjie);
  633. $template->setValue('danger_level', $model->danger_level . '。');
  634. $danger_jianyi = '';
  635. switch ($model->danger_level) {
  636. case '无危害':
  637. $danger_jianyi = "";
  638. break;
  639. case '一级危害':
  640. $danger_jianyi = "建议对危害物种定期进行监测,掌握危害发展趋势,适时开展白蚁等害堤动物危害综合治理。";
  641. break;
  642. case '二级危害':
  643. $danger_jianyi = "建议对危害物种进行适时监测,尽早开展白蚁等害堤动物危害综合治理。";
  644. break;
  645. case '三级危害':
  646. $danger_jianyi = "建议对危害物种进行密切监测,即时开展白蚁等害堤动物危害综合治理。";
  647. break;
  648. }
  649. $template->setValue('danger_jianyi', $danger_jianyi);
  650. $filename = "1.水利工程白蚁危害评定报告.docx";
  651. File::isDirectory($directory) or File::makeDirectory($directory, 0777, true, true);
  652. $file_path = "{$directory}/{$filename}";
  653. $template->saveAs($file_path);
  654. $base_directory = "jlb/{$model->id}/{$time}";
  655. //拷贝图片
  656. if ($model->danger_feature && is_array($model->danger_animal_extend)) {
  657. $directory = "{$base_directory}/5.白蚁危害评定检查现场及相关影像/蚁患/外露特征";
  658. foreach ($model->danger_feature_extend as $item) {
  659. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  660. foreach ($item['img_paths'] as $k => $img) {
  661. $index = $k + 1;
  662. $exception = File::extension($img['url']);
  663. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  664. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  665. }
  666. }
  667. }
  668. //分飞孔
  669. if ($model->danger_hole && is_array($model->danger_hole_paths)) {
  670. $directory = "{$base_directory}/5.白蚁危害评定检查现场及相关影像/蚁患/外露特征";
  671. foreach ($model->danger_hole_paths as $k => $img) {
  672. $index = $k + 1;
  673. $exception = File::extension($img['url']);
  674. $filename = $directory . "/分飞孔/{$index}.{$exception}";
  675. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  676. }
  677. }
  678. if ($model->danger_animal && is_array($model->danger_animal_extend)) {
  679. $directory = "{$base_directory}/5.白蚁危害评定检查现场及相关影像/蚁患/其他害堤动物外露特征";
  680. foreach ($model->danger_animal_extend as $item) {
  681. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  682. foreach ($item['img_paths'] as $k => $img) {
  683. $index = $k + 1;
  684. $exception = File::extension($img['url']);
  685. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  686. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  687. }
  688. }
  689. }
  690. //蚁源区
  691. if ($model->area_feature && is_array($model->area_feature_extend)) {
  692. $directory = "{$base_directory}/5.白蚁危害评定检查现场及相关影像/蚁源区/外露特征";
  693. foreach ($model->area_feature_extend as $item) {
  694. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  695. foreach ($item['img_paths'] as $k => $img) {
  696. $index = $k + 1;
  697. $exception = File::extension($img['url']);
  698. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  699. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  700. }
  701. }
  702. }
  703. //分飞孔
  704. if ($model->area_hole && is_array($model->area_hole_paths)) {
  705. $directory = "{$base_directory}/5.白蚁危害评定检查现场及相关影像/蚁源区/外露特征";
  706. foreach ($model->area_hole_paths as $k => $img) {
  707. $index = $k + 1;
  708. $exception = File::extension($img['url']);
  709. $filename = $directory . "/分飞孔/{$index}.{$exception}";
  710. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  711. }
  712. }
  713. } catch (\Exception $exception) {
  714. log_record('报告生产出错', $exception);
  715. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '文件生成失败');
  716. }
  717. }
  718. /**
  719. * 生成图片
  720. * @param $model
  721. * @return bool
  722. */
  723. public static function makeImgWord($model)
  724. {
  725. $time = Carbon::parse($model->updated_at)->timestamp;
  726. $directory = Storage::disk('public')->path("jlb/{$model->id}/{$time}");
  727. $template_type = 4;
  728. $path = base_path("public/template/ant/template_{$template_type}.docx");
  729. $is = file_exists($path);
  730. if (!$is) {
  731. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '模板文件找不到');
  732. return false;
  733. }
  734. try {
  735. $template = new TemplateProcessor($path);
  736. $table = new Table(['borderSize' => 0, 'borderColor' => 'ffffff', 'alignMent' => JcTable::CENTER, 'cellSpacing' => 50]);
  737. $table_imgs = [];
  738. $i = 1;
  739. if ($model->danger_feature && is_array($model->danger_animal_extend)) {
  740. foreach ($model->danger_feature_extend as $item) {
  741. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  742. $table->addRow();
  743. $table->addCell(5000, ['gridSpan' => 2])->addText('蚁患-' . $item['name']);
  744. foreach ($item['img_paths'] as $k => $img) {
  745. $filename = $img['url'];
  746. if ($k % 2 == 0) $table->addRow();
  747. $key = 'img_' . $i;
  748. $table->addCell(5000)->addText('${img_' . $i . '}');
  749. $i++;
  750. $table_imgs[] = ['key' => $key, 'url' => $filename];
  751. }
  752. }
  753. }
  754. //分飞孔
  755. if ($model->danger_hole && is_array($model->danger_hole_paths)) {
  756. $table->addRow();
  757. $table->addCell(5000, ['gridSpan' => 2])->addText('蚁患-分飞孔');
  758. foreach ($model->danger_hole_paths as $k => $img) {
  759. $filename = $img['url'];
  760. if ($k % 2 == 0) $table->addRow();
  761. $key = 'img_' . $i;
  762. $table->addCell(5000)->addText('${img_' . $i . '}');
  763. $i++;
  764. $table_imgs[] = ['key' => $key, 'url' => $filename];
  765. }
  766. }
  767. if ($model->danger_animal && is_array($model->danger_animal_extend)) {
  768. $table->addRow();
  769. $table->addCell(5000, ['gridSpan' => 2])->addText('蚁患-其他害堤动物外露特征');
  770. foreach ($model->danger_animal_extend as $item) {
  771. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  772. foreach ($item['img_paths'] as $k => $img) {
  773. $filename = $img['url'];
  774. if ($k % 2 == 0) $table->addRow();
  775. $key = 'img_' . $i;
  776. $table->addCell(5000)->addText('${img_' . $i . '}');
  777. $i++;
  778. $table_imgs[] = ['key' => $key, 'url' => $filename];
  779. }
  780. }
  781. }
  782. //蚁源区
  783. if ($model->area_feature && is_array($model->area_feature_extend)) {
  784. $table->addRow();
  785. $table->addCell(5000, ['gridSpan' => 2])->addText('蚁源区-外露特征');
  786. foreach ($model->area_feature_extend as $item) {
  787. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  788. foreach ($item['img_paths'] as $k => $img) {
  789. $filename = $img['url'];
  790. if ($k % 2 == 0) $table->addRow();
  791. $key = 'img_' . $i;
  792. $table->addCell(5000)->addText('${img_' . $i . '}');
  793. $i++;
  794. $table_imgs[] = ['key' => $key, 'url' => $filename];
  795. }
  796. }
  797. }
  798. //分飞孔
  799. if ($model->area_hole && is_array($model->area_hole_paths)) {
  800. $table->addRow();
  801. $table->addCell(5000, ['gridSpan' => 2])->addText('蚁源区-分飞孔');
  802. foreach ($model->area_hole_paths as $k => $img) {
  803. $filename = $img['url'];
  804. if ($k % 2 == 0) $table->addRow();
  805. $key = 'img_' . $i;
  806. $table->addCell(5000)->addText('${img_' . $i . '}');
  807. $i++;
  808. $table_imgs[] = ['key' => $key, 'url' => $filename];
  809. }
  810. }
  811. $template->setComplexBlock('block', $table);
  812. foreach ($table_imgs as $img) {
  813. $template->setImageValue($img['key'], ['path' => $img['url'], 'width' => 1000, 'height' => 200]);
  814. }
  815. $filename = "6.白蚁危害评定检查现场及相关影像.docx";
  816. File::isDirectory($directory) or File::makeDirectory($directory, 0777, true, true);
  817. $file_path = "{$directory}/{$filename}";
  818. $template->saveAs($file_path);
  819. } catch (\Exception $exception) {
  820. log_record('报告图片出错', $exception);
  821. abort(ResponseCodeEnum::SERVICE_OPERATION_ERROR, '文件生成失败');
  822. }
  823. return true;
  824. }
  825. public static function packImgs($model, $type)
  826. {
  827. $time = Carbon::parse($model->updated_at)->timestamp;
  828. $base_directory = "packImgs/{$model->id}/{$time}";
  829. $filename = '';
  830. switch ($type) {
  831. case 1:
  832. $directory = "{$base_directory}/周围环境";
  833. //周围环境
  834. if (!self::isZipExists($directory)) {
  835. if (is_array($model->environment)) {
  836. foreach ($model->environment as $item) {
  837. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  838. foreach ($item['img_paths'] as $k => $img) {
  839. $index = $k + 1;
  840. $exception = File::extension($img['url']);
  841. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  842. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  843. }
  844. }
  845. }
  846. $filename = "{$model->project_name}_周围环境.zip";
  847. }
  848. break;
  849. case 2:
  850. //蚁患
  851. $directory = "{$base_directory}/蚁患/外露特征";
  852. if (!self::isZipExists($directory)) {
  853. if (is_array($model->danger_feature_extend)) {
  854. foreach ($model->danger_feature_extend as $item) {
  855. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  856. foreach ($item['img_paths'] as $k => $img) {
  857. $index = $k + 1;
  858. $exception = File::extension($img['url']);
  859. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  860. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  861. }
  862. }
  863. }
  864. if ($model->danger_hole && is_array($model->danger_hole_paths)) {
  865. foreach ($model->danger_hole_paths as $k => $img) {
  866. $index = $k + 1;
  867. $exception = File::extension($img['url']);
  868. $filename = $directory . "/分飞孔/{$index}.{$exception}";
  869. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  870. }
  871. }
  872. $directory = "{$base_directory}/蚁患/其他害堤动物外露特征";
  873. if ($model->danger_animal && is_array($model->danger_animal_extend)) {
  874. foreach ($model->danger_animal_extend as $item) {
  875. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  876. foreach ($item['img_paths'] as $k => $img) {
  877. $index = $k + 1;
  878. $exception = File::extension($img['url']);
  879. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  880. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  881. }
  882. }
  883. }
  884. }
  885. $directory = "{$base_directory}/蚁患";
  886. $filename = "{$model->project_name}_蚁患.zip";
  887. break;
  888. case 3:
  889. //蚁源区
  890. $directory = "{$base_directory}/蚁源区/外露特征";
  891. if (!self::isZipExists($directory)) {
  892. if ($model->area_feature && is_array($model->area_feature_extend)) {
  893. $directory = "{$base_directory}/蚁源区/外露特征";
  894. foreach ($model->area_feature_extend as $item) {
  895. if (!isset($item['img_paths']) || !is_array($item['img_paths'])) continue;
  896. foreach ($item['img_paths'] as $k => $img) {
  897. $index = $k + 1;
  898. $exception = File::extension($img['url']);
  899. $filename = $directory . "/{$item['name']}/{$index}.{$exception}";
  900. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  901. }
  902. }
  903. }
  904. //分飞孔
  905. if ($model->area_hole && is_array($model->area_hole_paths)) {
  906. $directory = "{$base_directory}/蚁源区/外露特征";
  907. foreach ($model->area_hole_paths as $k => $img) {
  908. $index = $k + 1;
  909. $exception = File::extension($img['url']);
  910. $filename = $directory . "/分飞孔/{$index}.{$exception}";
  911. Storage::disk('public')->put($filename, file_get_contents($img['url']));
  912. }
  913. }
  914. }
  915. $directory = "{$base_directory}/蚁源区";
  916. $filename = "{$model->project_name}_蚁源区.zip";
  917. break;
  918. case 4:
  919. //工程影响
  920. $directory = "{$base_directory}/工程影响";
  921. if (!self::isZipExists($directory)) {
  922. if ($model->pd_sanjin && is_array($model->pd_sanjin_paths)) {
  923. foreach ($model->pd_sanjin_paths as $k => $item) {
  924. $index = $k + 1;
  925. $exception = File::extension($item['url']);
  926. $filename = $directory . "/散浸/{$index}.{$exception}";
  927. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  928. }
  929. }
  930. if ($model->pd_shipo && is_array($model->pd_shipo_paths)) {
  931. foreach ($model->pd_shipo_paths as $k => $item) {
  932. $index = $k + 1;
  933. $exception = File::extension($item['url']);
  934. $filename = $directory . "/湿坡/{$index}.{$exception}";
  935. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  936. }
  937. }
  938. if ($model->pd_loudong && is_array($model->pd_loudong_paths)) {
  939. foreach ($model->pd_loudong_paths as $k => $item) {
  940. $index = $k + 1;
  941. $exception = File::extension($item['url']);
  942. $filename = $directory . "/漏洞/{$index}.{$exception}";
  943. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  944. }
  945. }
  946. if ($model->pd_diewo && is_array($model->pd_diewo_paths)) {
  947. foreach ($model->pd_diewo_paths as $k => $item) {
  948. $index = $k + 1;
  949. $exception = File::extension($item['url']);
  950. $filename = $directory . "/跌窝/{$index}.{$exception}";
  951. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  952. }
  953. }
  954. if ($model->pd_tuopo && is_array($model->pd_tuopo_paths)) {
  955. foreach ($model->pd_tuopo_paths as $k => $item) {
  956. $index = $k + 1;
  957. $exception = File::extension($item['url']);
  958. $filename = $directory . "/脱坡/{$index}.{$exception}";
  959. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  960. }
  961. }
  962. }
  963. $directory = "{$base_directory}/工程影响";
  964. $filename = "{$model->project_name}_工程影响.zip";
  965. break;
  966. case 5:
  967. //示意图
  968. $directory = "{$base_directory}/示意图";
  969. if (!self::isZipExists($directory)) {
  970. $shiyitu = $model->shiyitu;
  971. if (is_array($shiyitu)) {
  972. foreach ($shiyitu as $k => $item) {
  973. $index = $k + 1;
  974. $exception = File::extension($item['url']);
  975. $filename = $directory . "/{$index}.{$exception}";
  976. Storage::disk('public')->put($filename, file_get_contents($item['url']));
  977. }
  978. }
  979. }
  980. $directory = "{$base_directory}/示意图";
  981. $filename = "{$model->project_name}_示意图.zip";
  982. break;
  983. }
  984. self::packDownZip($filename, $directory);
  985. }
  986. }