ImportMessageJob.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. <?php
  2. /*
  3. * This file is part of the ZhMead/laravel-logger.
  4. *
  5. * (c) Mead <751066209@qql.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\Jobs\Manage;
  11. use App\Repositories\Enums\Manage\DealStatusEnum;
  12. use App\Repositories\Enums\Manage\ImportStatusEnum;
  13. use App\Repositories\Enums\ModelStatusEnum;
  14. use App\Repositories\Models\Base\Admin;
  15. use App\Repositories\Models\Base\Department;
  16. use App\Repositories\Models\Base\Dict;
  17. use App\Repositories\Models\Base\Resource;
  18. use App\Repositories\Models\Base\SourceMatters;
  19. use App\Repositories\Models\Manage\Category;
  20. use App\Repositories\Models\Manage\ImportRecord;
  21. use App\Repositories\Models\Manage\Message;
  22. use App\Repositories\Models\Manage\PreMessage;
  23. use Carbon\Carbon;
  24. use Fukuball\Jieba\Finalseg;
  25. use Fukuball\Jieba\Jieba;
  26. use Illuminate\Bus\Queueable;
  27. use Illuminate\Contracts\Queue\ShouldQueue;
  28. use Illuminate\Queue\InteractsWithQueue;
  29. use Illuminate\Support\Facades\DB;
  30. use Illuminate\Support\Facades\Log;
  31. use Illuminate\Support\Facades\Storage;
  32. use Illuminate\Support\Str;
  33. class ImportMessageJob implements ShouldQueue
  34. {
  35. use InteractsWithQueue;
  36. use Queueable;
  37. private $importRecordId;
  38. private $admin;
  39. /**
  40. * Create a new job instance.
  41. */
  42. public function __construct($importRecordId, $admin)
  43. {
  44. $this->importRecordId = $importRecordId;
  45. $this->admin = $admin;
  46. }
  47. /**
  48. * 确定任务应该超时的时间
  49. *
  50. * @return \DateTime
  51. */
  52. public function retryUntil()
  53. {
  54. return Carbon::now()->addHours(8);
  55. }
  56. public $tries = 1;
  57. public $timeout = 1440;
  58. /**
  59. * Execute the job.
  60. */
  61. public function handle()
  62. {
  63. $importRecord = ImportRecord::query()->find($this->importRecordId);
  64. // $this->importTemplate3($importRecord);
  65. // dd(1);
  66. switch ($importRecord->type) {
  67. case 1:
  68. $this->importTemplate1($importRecord);
  69. break;
  70. case 2:
  71. $this->importTemplate2($importRecord);
  72. break;
  73. case 3:
  74. $this->importTemplate3($importRecord);
  75. break;
  76. }
  77. }
  78. /**
  79. * 导入数据
  80. * @return false|void
  81. */
  82. public function importTemplate1(&$importRecord)
  83. {
  84. ini_set('memory_limit', '1024M');
  85. $admin = $this->admin;
  86. $import_department_id = $admin['department_id'];
  87. $importRecord->status = ImportStatusEnum::IMPORT_IN;
  88. $importRecord->save();
  89. $path = Storage::disk('public')->path(Resource::byIdGetPath($importRecord->resource_id));
  90. // $path = base_path('./1.xlsx');
  91. // $path = base_path('./2.xlsx');
  92. if (!file_exists($path)) {
  93. $importRecord->status = ImportStatusEnum::IMPORT_ERROR;
  94. $importRecord->message = '找不到文件';
  95. $importRecord->save();
  96. Log::error('*************找不到文件***************');
  97. Log::error($path);
  98. return false;
  99. }
  100. $departments = Department::getNames();
  101. //获取事项来源列表--id---and---相似名称
  102. $sourceMatters = SourceMatters::getNames();
  103. $sourceMatter_luoshan = SourceMatters::query()->where('name', '罗山县')->where('status', ModelStatusEnum::OK)->value('id');
  104. try {
  105. $config = ['path' => ''];
  106. $excel = new \Vtiful\Kernel\Excel($config);
  107. $excel->openFile($path)
  108. ->openSheet()
  109. ->setType([
  110. 3 => \Vtiful\Kernel\Excel::TYPE_STRING
  111. ])
  112. ->setSkipRows(2);
  113. //处理数据
  114. $data = [];
  115. Log::error('*************开始读取数据*****************');
  116. $total = 0;
  117. $r_total = 0;
  118. $w_total = 0;
  119. while (($row = $excel->nextRow()) !== NULL) {
  120. $nums = count($row);
  121. if ($nums >= 11) {
  122. $total++;
  123. list(
  124. $type_name,
  125. $category_string,
  126. $body,
  127. $complain_date,
  128. $name,
  129. $mobile,
  130. $deal_department_name,
  131. $deal_limit_day,
  132. $deal_status_name,
  133. $deal_idea,
  134. $deal_evaluation,
  135. ) = $row;
  136. if (empty($body)) continue;
  137. $category_arr = str2arr($category_string, '/');
  138. $data[] = [
  139. 'category' => $category_arr,
  140. 'body' => $body,
  141. 'type_name' => $type_name,
  142. 'name' => $name,
  143. 'mobile' => $mobile,
  144. 'complain_date' => Carbon::parse($complain_date)->toDateString(),
  145. 'deal_department_name' => $deal_department_name ?? '其他',
  146. 'deal_idea' => $deal_idea,
  147. 'deal_limit_day' => $deal_limit_day,
  148. 'deal_status_name' => $deal_status_name,
  149. 'deal_evaluation' => $deal_evaluation,
  150. ];
  151. }
  152. }
  153. Log::error("数据读取完成");
  154. $n = count($data);
  155. $r_total = $n;
  156. Log::error("开始写入数据:共{$n}条");
  157. //数据写入
  158. DB::beginTransaction();
  159. foreach ($data as $i => $item) {
  160. //教室
  161. $category_1 = 0;
  162. $category_2 = 0;
  163. $category_3 = 0;
  164. $category_4 = 0;
  165. $category_5 = 0;
  166. if (isset($item['category'][0])) {
  167. $category_1_model = Category::query()->firstOrCreate([
  168. 'name' => $item['category'][0],
  169. ]);
  170. $category_1 = $category_1_model->id;
  171. }
  172. if (isset($item['category'][1])) {
  173. $category_2_model = Category::query()->firstOrCreate([
  174. 'name' => $item['category'][1],
  175. 'parent_id' => $category_1,
  176. ]);
  177. $category_2 = $category_2_model->id;
  178. }
  179. if (isset($item['category'][2])) {
  180. $category_3_model = Category::query()->firstOrCreate([
  181. 'name' => $item['category'][2],
  182. 'parent_id' => $category_2,
  183. ]);
  184. $category_3 = $category_3_model->id;
  185. }
  186. if (isset($item['category'][3])) {
  187. $category_4_model = Category::query()->firstOrCreate([
  188. 'name' => $item['category'][3],
  189. 'parent_id' => $category_3,
  190. ]);
  191. $category_4 = $category_4_model->id;
  192. }
  193. if (isset($item['category'][4])) {
  194. $category_5_model = Category::query()->firstOrCreate([
  195. 'name' => $item['category'][4],
  196. 'parent_id' => $category_4,
  197. ]);
  198. $category_5 = $category_5_model->id;
  199. }
  200. Category::updateLevel();
  201. // $type = Dict::firstOrCreateItem('MESSAGE_FORM', $item['type_name']);
  202. // if (!$type) continue;
  203. //事项来源--获取id
  204. $type_id = 0;
  205. foreach ($sourceMatters as $sourceMatter) {
  206. if (in_array($item['type_name'], $sourceMatter['names'])) {
  207. $type_id = $sourceMatter['id'];
  208. }
  209. }
  210. if (!$type_id) $type_id = $sourceMatter_luoshan;
  211. //投诉时间--判断时间
  212. if (strtotime($item['complain_date']) < strtotime('2020-01-01')) $item['complain_date'] = Carbon::now()->toDateString();
  213. // $department = Department::query()->firstOrCreate([
  214. // 'name' => $item['deal_department_name']
  215. // ]);
  216. $department_id = 0;
  217. foreach ($departments as $department) {
  218. if (in_array($item['deal_department_name'], $department['names'])) {
  219. $department_id = $department['id'];
  220. }
  221. }
  222. if (!$department_id) continue;
  223. $department_admin_id = Admin::query()->where('department_id', $department_id)->where('status', ModelStatusEnum::OK)->value('id') ?? 0;
  224. $deal_status = DealStatusEnum::WAIT;
  225. switch ($item['deal_status_name']) {
  226. case '未办理':
  227. $deal_status = DealStatusEnum::WAIT;
  228. break;
  229. case '正在办理':
  230. $deal_status = DealStatusEnum::IN;
  231. break;
  232. case '已办结':
  233. $deal_status = DealStatusEnum::OK;
  234. break;
  235. }
  236. //提取字段关键字
  237. $address_id = 0;
  238. $address_name = '';
  239. $address_keys = Dict::byCodeGetDicts('ADDRESS_KEYS');
  240. foreach ($address_keys as $key) {
  241. if (strpos($item['body'], $key['name']) !== false) {
  242. $address_id = $key['id'];
  243. $address_name = $key['name'];
  244. break;
  245. }
  246. }
  247. if (!$address_id) {
  248. $address_id = 72;
  249. $address_name = '其他';
  250. }
  251. Jieba::init();
  252. Finalseg::init();
  253. $seg_list = Jieba::cut(remove_symbol($item['body']));
  254. $md5 = md5($item['body']);
  255. $id = Message::query()->where('body_md5', $md5)->where('import_department_id', $import_department_id)->value('id');
  256. $w_total++;
  257. if ($id) {
  258. $mark_red_day = null;
  259. if ($deal_status == DealStatusEnum::OK) $mark_red_day = date('Y-m-d');
  260. Message::query()->where('id', $id)->update([
  261. 'type_id' => $type_id,
  262. 'category_1_id' => $category_1,
  263. 'category_2_id' => $category_2,
  264. 'category_3_id' => $category_3,
  265. 'category_4_id' => $category_4,
  266. 'category_5_id' => $category_5,
  267. 'name' => $item['name'],
  268. 'mobile' => $item['mobile'],
  269. 'address_id' => $address_id,
  270. 'address_name' => $address_name,
  271. 'complain_date' => $item['complain_date'],
  272. 'warn_type_id' => 1,
  273. 'body' => $item['body'],
  274. 'body_md5' => $md5,
  275. 'keywords' => $seg_list,
  276. 'deal_department_id' => $department_id,
  277. 'deal_admin_id' => $department_admin_id,
  278. 'deal_evaluation' => $item['deal_evaluation'],
  279. 'deal_status' => $deal_status,
  280. 'deal_idea' => $item['deal_idea'],
  281. 'deal_time' => $item['deal_idea'] ? $item['complain_date'] : null,
  282. 'assign_deal_time' => $item['complain_date'],
  283. 'assign_admin_id' => $admin['id'],
  284. 'is_assign_status' => 1,
  285. 'deal_limit_day' => $item['deal_limit_day'],
  286. 'deal_limit_date' => Carbon::parse($item['complain_date'])->addDays($item['deal_limit_day'])->toDateTimeString(),
  287. 'import_record_id' => $importRecord->id,
  288. 'import_department_id' => $import_department_id,
  289. 'import_admin_id' => $admin['id'],
  290. 'status' => 1,
  291. 'mark_red_day' => $mark_red_day
  292. ]);
  293. //替换
  294. continue;
  295. }
  296. Message::query()->create([
  297. 'type_id' => $type_id,
  298. 'category_1_id' => $category_1,
  299. 'category_2_id' => $category_2,
  300. 'category_3_id' => $category_3,
  301. 'category_4_id' => $category_4,
  302. 'category_5_id' => $category_5,
  303. 'no' => Str::random(8),
  304. 'name' => $item['name'],
  305. 'mobile' => $item['mobile'],
  306. 'address_id' => $address_id,
  307. 'address_name' => $address_name,
  308. 'complain_date' => $item['complain_date'],
  309. 'warn_type_id' => 1,
  310. 'body' => $item['body'],
  311. 'body_md5' => $md5,
  312. 'keywords' => $seg_list,
  313. 'deal_department_id' => $department_id,
  314. 'deal_admin_id' => $department_admin_id,
  315. 'deal_evaluation' => $item['deal_evaluation'],
  316. 'deal_status' => $deal_status,
  317. 'deal_idea' => $item['deal_idea'],
  318. 'deal_time' => $item['deal_idea'] ? $item['complain_date'] : null,
  319. 'assign_deal_time' => $item['complain_date'],
  320. 'assign_admin_id' => $admin['id'],
  321. 'is_assign_status' => 1,
  322. 'deal_limit_day' => $item['deal_limit_day'],
  323. 'deal_limit_date' => Carbon::parse($item['complain_date'])->addDays($item['deal_limit_day'])->toDateTimeString(),
  324. 'import_record_id' => $importRecord->id,
  325. 'import_department_id' => $import_department_id,
  326. 'import_admin_id' => $admin['id'],
  327. 'status' => 1
  328. ]);
  329. Log::error("正在创建:第{$i}条数据已完成");
  330. }
  331. DB::commit();
  332. Category::updateLevel();
  333. Log::error("-----------完成---------------");
  334. $importRecord->message = "共{$total}条,读取{$r_total}条,导入{$w_total}条。";
  335. $importRecord->status = ImportStatusEnum::IMPORT_OK;
  336. $importRecord->save();
  337. } catch (\Exception $exception) {
  338. $importRecord->status = ImportStatusEnum::IMPORT_ERROR;
  339. $importRecord->message = $exception->getMessage();
  340. $importRecord->save();
  341. Log::error($exception);
  342. DB::rollBack();
  343. }
  344. }
  345. /**
  346. * 模版二
  347. * @param $importRecord
  348. * @return false|void
  349. */
  350. public function importTemplate2(&$importRecord)
  351. {
  352. ini_set('memory_limit', '1024M');
  353. $admin = $this->admin;
  354. $import_department_id = $admin['department_id'];
  355. $importRecord->status = ImportStatusEnum::IMPORT_IN;
  356. $importRecord->save();
  357. $path = Storage::disk('public')->path(Resource::byIdGetPath($importRecord->resource_id));
  358. if (!file_exists($path)) {
  359. $importRecord->status = ImportStatusEnum::IMPORT_ERROR;
  360. $importRecord->message = '找不到文件';
  361. $importRecord->save();
  362. Log::error('*************找不到文件***************');
  363. Log::error($path);
  364. return false;
  365. }
  366. // $path = base_path('2.xlsx');
  367. $departments = Department::getNames();
  368. //获取事项来源列表--id---and---相似名称
  369. $sourceMatters = SourceMatters::getNames();
  370. $sourceMatter_luoshan = SourceMatters::query()->where('name', '罗山县')->where('status', ModelStatusEnum::OK)->value('id');
  371. try {
  372. $config = ['path' => ''];
  373. $excel = new \Vtiful\Kernel\Excel($config);
  374. $excel->openFile($path)
  375. ->openSheet()
  376. ->setType([
  377. 14 => \Vtiful\Kernel\Excel::TYPE_STRING
  378. ])
  379. ->setSkipRows(2);
  380. //处理数据
  381. $data = [];
  382. Log::error('*************开始读取数据*****************');
  383. $total = 0;
  384. $r_total = 0;
  385. $w_total = 0;
  386. while (($row = $excel->nextRow()) !== NULL) {
  387. $nums = count($row);
  388. if ($nums == 44) {
  389. $total++;
  390. $type_name = '12345';
  391. $category_string = $row[13];
  392. $body = $row[11];
  393. $complain_date = $row[14];
  394. $name = $row[6];
  395. $mobile = $row[4];
  396. $deal_department_name = $row[19];
  397. $deal_limit_day = 0;
  398. $deal_idea = $row[26];
  399. $deal_evaluation = 100;
  400. $deal_status_name = empty($deal_idea) ? '正在办理' : '已办结';
  401. if (empty($body)) continue;
  402. $category_arr = str2arr($category_string, '/');
  403. $data[] = [
  404. 'category' => $category_arr,
  405. 'body' => $body,
  406. 'type_name' => $type_name,
  407. 'name' => $name,
  408. 'mobile' => $mobile,
  409. 'complain_date' => Carbon::parse($complain_date)->toDateString(),
  410. 'deal_department_name' => $deal_department_name ?? '其他',
  411. 'deal_idea' => $deal_idea,
  412. 'deal_limit_day' => $deal_limit_day,
  413. 'deal_status_name' => $deal_status_name,
  414. 'deal_evaluation' => $deal_evaluation,
  415. ];
  416. }
  417. }
  418. Log::error("数据读取完成");
  419. $n = count($data);
  420. Log::error("开始写入数据:共{$n}条");
  421. $r_total = count($data);
  422. //数据写入
  423. DB::beginTransaction();
  424. foreach ($data as $i => $item) {
  425. //教室
  426. $category_1 = 0;
  427. $category_2 = 0;
  428. $category_3 = 0;
  429. $category_4 = 0;
  430. $category_5 = 0;
  431. if (isset($item['category'][0])) {
  432. $category_1_model = Category::query()->firstOrCreate([
  433. 'name' => $item['category'][0],
  434. ]);
  435. $category_1 = $category_1_model->id;
  436. }
  437. if (isset($item['category'][1])) {
  438. $category_2_model = Category::query()->firstOrCreate([
  439. 'name' => $item['category'][1],
  440. 'parent_id' => $category_1,
  441. ]);
  442. $category_2 = $category_2_model->id;
  443. }
  444. if (isset($item['category'][2])) {
  445. $category_3_model = Category::query()->firstOrCreate([
  446. 'name' => $item['category'][2],
  447. 'parent_id' => $category_2,
  448. ]);
  449. $category_3 = $category_3_model->id;
  450. }
  451. if (isset($item['category'][3])) {
  452. $category_4_model = Category::query()->firstOrCreate([
  453. 'name' => $item['category'][3],
  454. 'parent_id' => $category_3,
  455. ]);
  456. $category_4 = $category_4_model->id;
  457. }
  458. if (isset($item['category'][4])) {
  459. $category_5_model = Category::query()->firstOrCreate([
  460. 'name' => $item['category'][4],
  461. 'parent_id' => $category_4,
  462. ]);
  463. $category_5 = $category_5_model->id;
  464. }
  465. Category::updateLevel();
  466. // $type = Dict::firstOrCreateItem('MESSAGE_FORM', $item['type_name']);
  467. // if (!$type) continue;
  468. //事项来源--获取id
  469. $type_id = 0;
  470. foreach ($sourceMatters as $sourceMatter) {
  471. if (in_array($item['type_name'], $sourceMatter['names'])) {
  472. $type_id = $sourceMatter['id'];
  473. }
  474. }
  475. if (!$type_id) $type_id = $sourceMatter_luoshan;
  476. //投诉时间--判断时间
  477. if (strtotime($item['complain_date']) < strtotime('2020-01-01')) $item['complain_date'] = Carbon::now()->toDateString();
  478. // $department = Department::query()->firstOrCreate([
  479. // 'name' => $item['deal_department_name']
  480. // ]);
  481. $department_id = 0;
  482. foreach ($departments as $department) {
  483. if (in_array($item['deal_department_name'], $department['names'])) {
  484. $department_id = $department['id'];
  485. }
  486. }
  487. if (!$department_id) continue;
  488. $department_admin_id = Admin::query()->where('department_id', $department_id)->where('status', ModelStatusEnum::OK)->value('id') ?? 0;
  489. $deal_status = DealStatusEnum::WAIT;
  490. switch ($item['deal_status_name']) {
  491. case '未办理':
  492. $deal_status = DealStatusEnum::WAIT;
  493. break;
  494. case '正在办理':
  495. $deal_status = DealStatusEnum::IN;
  496. break;
  497. case '已办结':
  498. $deal_status = DealStatusEnum::OK;
  499. break;
  500. }
  501. //提取字段关键字
  502. $address_id = 0;
  503. $address_name = '';
  504. $address_keys = Dict::byCodeGetDicts('ADDRESS_KEYS');
  505. foreach ($address_keys as $key) {
  506. if (strpos($item['body'], $key['name']) !== false) {
  507. $address_id = $key['id'];
  508. $address_name = $key['name'];
  509. break;
  510. }
  511. }
  512. Jieba::init();
  513. Finalseg::init();
  514. $seg_list = Jieba::cut(remove_symbol($item['body']));
  515. $md5 = md5($item['body']);
  516. $id = Message::query()->where('body_md5', $md5)->where('import_department_id', $import_department_id)->value('id');
  517. $w_total++;
  518. if ($id) {
  519. $mark_red_day = null;
  520. if ($deal_status == DealStatusEnum::OK) $mark_red_day = date('Y-m-d');
  521. Message::query()->where('id', $id)->update([
  522. 'type_id' => $type_id,
  523. 'category_1_id' => $category_1,
  524. 'category_2_id' => $category_2,
  525. 'category_3_id' => $category_3,
  526. 'category_4_id' => $category_4,
  527. 'category_5_id' => $category_5,
  528. 'name' => $item['name'],
  529. 'mobile' => $item['mobile'],
  530. 'address_id' => $address_id,
  531. 'address_name' => $address_name,
  532. 'complain_date' => $item['complain_date'],
  533. 'warn_type_id' => 1,
  534. 'body' => $item['body'],
  535. 'body_md5' => $md5,
  536. 'keywords' => $seg_list,
  537. 'deal_department_id' => $department_id,
  538. 'deal_admin_id' => $department_admin_id,
  539. 'deal_evaluation' => $item['deal_evaluation'],
  540. 'deal_status' => $deal_status,
  541. 'deal_idea' => $item['deal_idea'],
  542. 'deal_time' => $item['deal_idea'] ? $item['complain_date'] : null,
  543. 'assign_deal_time' => $item['complain_date'],
  544. 'assign_admin_id' => $admin['id'],
  545. 'is_assign_status' => 1,
  546. 'deal_limit_day' => $item['deal_limit_day'],
  547. 'deal_limit_date' => Carbon::parse($item['complain_date'])->addDays($item['deal_limit_day'])->toDateTimeString(),
  548. 'import_record_id' => $importRecord->id,
  549. 'import_department_id' => $import_department_id,
  550. 'import_admin_id' => $admin['id'],
  551. 'status' => 1,
  552. 'mark_red_day' => $mark_red_day
  553. ]);
  554. //替换
  555. continue;
  556. }
  557. Message::query()->create([
  558. 'type_id' => $type_id,
  559. 'category_1_id' => $category_1,
  560. 'category_2_id' => $category_2,
  561. 'category_3_id' => $category_3,
  562. 'category_4_id' => $category_4,
  563. 'category_5_id' => $category_5,
  564. 'no' => Str::random(8),
  565. 'name' => $item['name'],
  566. 'mobile' => $item['mobile'],
  567. 'address_id' => $address_id,
  568. 'address_name' => $address_name,
  569. 'complain_date' => $item['complain_date'],
  570. 'warn_type_id' => 1,
  571. 'body' => $item['body'],
  572. 'body_md5' => $md5,
  573. 'keywords' => $seg_list,
  574. 'deal_department_id' => $department_id,
  575. 'deal_admin_id' => $department_admin_id,
  576. 'deal_evaluation' => $item['deal_evaluation'],
  577. 'deal_status' => $deal_status,
  578. 'deal_idea' => $item['deal_idea'],
  579. 'deal_time' => $item['deal_idea'] ? $item['complain_date'] : null,
  580. 'assign_deal_time' => $item['complain_date'],
  581. 'assign_admin_id' => $admin['id'],
  582. 'is_assign_status' => 1,
  583. 'deal_limit_day' => $item['deal_limit_day'],
  584. 'deal_limit_date' => Carbon::parse($item['complain_date'])->addDays($item['deal_limit_day'])->toDateTimeString(),
  585. 'import_record_id' => $importRecord->id,
  586. 'import_department_id' => $import_department_id,
  587. 'import_admin_id' => $admin['id'],
  588. 'status' => 1
  589. ]);
  590. Log::error("正在创建:第{$i}条数据已完成");
  591. }
  592. DB::commit();
  593. Category::updateLevel();
  594. Log::error("-----------完成---------------");
  595. $importRecord->message = "共{$total}条,读取{$r_total}条,导入{$w_total}条。";
  596. $importRecord->status = ImportStatusEnum::IMPORT_OK;
  597. $importRecord->save();
  598. } catch (\Exception $exception) {
  599. $importRecord->status = ImportStatusEnum::IMPORT_ERROR;
  600. $importRecord->message = $exception->getMessage();
  601. $importRecord->save();
  602. Log::error($exception);
  603. DB::rollBack();
  604. }
  605. }
  606. /**
  607. * 模版二
  608. * @param $importRecord
  609. * @return false|void
  610. */
  611. public function importTemplate3(&$importRecord)
  612. {
  613. ini_set('memory_limit', '1024M');
  614. $admin = $this->admin;
  615. $import_department_id = $admin['department_id'];
  616. $importRecord->status = ImportStatusEnum::IMPORT_IN;
  617. $importRecord->save();
  618. $path = Storage::disk('public')->path(Resource::byIdGetPath($importRecord->resource_id));
  619. if (!file_exists($path)) {
  620. $importRecord->status = ImportStatusEnum::IMPORT_ERROR;
  621. $importRecord->message = '找不到文件';
  622. $importRecord->save();
  623. Log::error('*************找不到文件***************');
  624. Log::error($path);
  625. return false;
  626. }
  627. // $path = base_path('2.xlsx');
  628. $departments = Department::getNames();
  629. //获取事项来源列表--id---and---相似名称
  630. $sourceMatters = SourceMatters::getNames();
  631. $sourceMatter_luoshan = SourceMatters::query()->where('name', '罗山县')->where('status', ModelStatusEnum::OK)->value('id');
  632. try {
  633. $config = ['path' => ''];
  634. $excel = new \Vtiful\Kernel\Excel($config);
  635. $excel->openFile($path)
  636. ->openSheet()
  637. ->setType([
  638. 0 => \Vtiful\Kernel\Excel::TYPE_STRING
  639. ])
  640. ->setSkipRows(2);
  641. //处理数据
  642. $data = [];
  643. Log::error('*************开始读取数据*****************');
  644. $total = 0;
  645. $r_total = 0;
  646. $w_total = 0;
  647. while (($row = $excel->nextRow()) !== NULL) {
  648. $nums = count($row);
  649. if ($nums == 15) {
  650. $total++;
  651. $type_name = '信访办';
  652. $category_string = $row[7];
  653. $body = $row[14];
  654. $complain_date = $row[0];
  655. $name = $row[4];
  656. $mobile = '';
  657. $deal_department_name = $row[10];
  658. $deal_limit_day = 0;
  659. $deal_idea = null;
  660. $deal_evaluation = 100;
  661. $deal_status_name = empty($deal_idea) ? '正在办理' : '已办结';
  662. if (empty($body)) continue;
  663. $category_arr = str2arr($category_string, '/');
  664. $data[] = [
  665. 'category' => $category_arr,
  666. 'body' => $body,
  667. 'type_name' => $type_name,
  668. 'name' => $name,
  669. 'mobile' => $mobile,
  670. 'complain_date' => Carbon::parse($complain_date)->toDateString(),
  671. 'deal_department_name' => $deal_department_name ?? '其他',
  672. 'deal_idea' => $deal_idea,
  673. 'deal_limit_day' => $deal_limit_day,
  674. 'deal_status_name' => $deal_status_name,
  675. 'deal_evaluation' => $deal_evaluation,
  676. ];
  677. }
  678. }
  679. Log::error("数据读取完成");
  680. $n = count($data);
  681. $r_total = $n;
  682. Log::error("开始写入数据:共{$n}条");
  683. //数据写入
  684. DB::beginTransaction();
  685. foreach ($data as $i => $item) {
  686. //教室
  687. $category_1 = 0;
  688. $category_2 = 0;
  689. $category_3 = 0;
  690. $category_4 = 0;
  691. $category_5 = 0;
  692. if (isset($item['category'][0])) {
  693. $category_1_model = Category::query()->firstOrCreate([
  694. 'name' => $item['category'][0],
  695. ]);
  696. $category_1 = $category_1_model->id;
  697. }
  698. if (isset($item['category'][1])) {
  699. $category_2_model = Category::query()->firstOrCreate([
  700. 'name' => $item['category'][1],
  701. 'parent_id' => $category_1,
  702. ]);
  703. $category_2 = $category_2_model->id;
  704. }
  705. if (isset($item['category'][2])) {
  706. $category_3_model = Category::query()->firstOrCreate([
  707. 'name' => $item['category'][2],
  708. 'parent_id' => $category_2,
  709. ]);
  710. $category_3 = $category_3_model->id;
  711. }
  712. if (isset($item['category'][3])) {
  713. $category_4_model = Category::query()->firstOrCreate([
  714. 'name' => $item['category'][3],
  715. 'parent_id' => $category_3,
  716. ]);
  717. $category_4 = $category_4_model->id;
  718. }
  719. if (isset($item['category'][4])) {
  720. $category_5_model = Category::query()->firstOrCreate([
  721. 'name' => $item['category'][4],
  722. 'parent_id' => $category_4,
  723. ]);
  724. $category_5 = $category_5_model->id;
  725. }
  726. Category::updateLevel();
  727. // $type = Dict::firstOrCreateItem('MESSAGE_FORM', $item['type_name']);
  728. // if (!$type) continue;
  729. //事项来源--获取id
  730. $type_id = 0;
  731. foreach ($sourceMatters as $sourceMatter) {
  732. if (in_array($item['type_name'], $sourceMatter['names'])) {
  733. $type_id = $sourceMatter['id'];
  734. }
  735. }
  736. if (!$type_id) $type_id = $sourceMatter_luoshan;
  737. //投诉时间--判断时间
  738. if (strtotime($item['complain_date']) < strtotime('2020-01-01')) $item['complain_date'] = Carbon::now()->toDateString();
  739. // $department = Department::query()->firstOrCreate([
  740. // 'name' => $item['deal_department_name']
  741. // ]);
  742. $department_id = 0;
  743. foreach ($departments as $department) {
  744. if (in_array($item['deal_department_name'], $department['names'])) {
  745. $department_id = $department['id'];
  746. }
  747. }
  748. if (!$department_id) continue;
  749. $department_admin_id = Admin::query()->where('department_id', $department_id)->where('status', ModelStatusEnum::OK)->value('id') ?? 0;
  750. $deal_status = DealStatusEnum::WAIT;
  751. switch ($item['deal_status_name']) {
  752. case '未办理':
  753. $deal_status = DealStatusEnum::WAIT;
  754. break;
  755. case '正在办理':
  756. $deal_status = DealStatusEnum::IN;
  757. break;
  758. case '已办结':
  759. $deal_status = DealStatusEnum::OK;
  760. break;
  761. }
  762. //提取字段关键字
  763. $address_id = 0;
  764. $address_name = '';
  765. $address_keys = Dict::byCodeGetDicts('ADDRESS_KEYS');
  766. foreach ($address_keys as $key) {
  767. if (strpos($item['body'], $key['name']) !== false) {
  768. $address_id = $key['id'];
  769. $address_name = $key['name'];
  770. break;
  771. }
  772. }
  773. Jieba::init();
  774. Finalseg::init();
  775. $seg_list = Jieba::cut(remove_symbol($item['body']));
  776. $md5 = md5($item['body']);
  777. $id = Message::query()->where('body_md5', $md5)->where('import_department_id', $import_department_id)->value('id');
  778. $w_total++;
  779. if ($id) {
  780. $mark_red_day = null;
  781. if ($deal_status == DealStatusEnum::OK) $mark_red_day = date('Y-m-d');
  782. Message::query()->where('id', $id)->update([
  783. 'type_id' => $type_id,
  784. 'category_1_id' => $category_1,
  785. 'category_2_id' => $category_2,
  786. 'category_3_id' => $category_3,
  787. 'category_4_id' => $category_4,
  788. 'category_5_id' => $category_5,
  789. 'name' => $item['name'],
  790. 'mobile' => $item['mobile'],
  791. 'address_id' => $address_id,
  792. 'address_name' => $address_name,
  793. 'complain_date' => $item['complain_date'],
  794. 'warn_type_id' => 1,
  795. 'body' => $item['body'],
  796. 'body_md5' => $md5,
  797. 'keywords' => $seg_list,
  798. 'deal_department_id' => $department_id,
  799. 'deal_admin_id' => $department_admin_id,
  800. 'deal_evaluation' => $item['deal_evaluation'],
  801. 'deal_status' => $deal_status,
  802. 'deal_idea' => $item['deal_idea'],
  803. 'deal_time' => $item['deal_idea'] ? $item['complain_date'] : null,
  804. 'assign_deal_time' => $item['complain_date'],
  805. 'assign_admin_id' => $admin['id'],
  806. 'is_assign_status' => 1,
  807. 'deal_limit_day' => $item['deal_limit_day'],
  808. 'deal_limit_date' => Carbon::parse($item['complain_date'])->addDays($item['deal_limit_day'])->toDateTimeString(),
  809. 'import_record_id' => $importRecord->id,
  810. 'import_department_id' => $import_department_id,
  811. 'import_admin_id' => $admin['id'],
  812. 'status' => 1,
  813. 'mark_red_day' => $mark_red_day
  814. ]);
  815. //替换
  816. continue;
  817. }
  818. Message::query()->create([
  819. 'type_id' => $type_id,
  820. 'category_1_id' => $category_1,
  821. 'category_2_id' => $category_2,
  822. 'category_3_id' => $category_3,
  823. 'category_4_id' => $category_4,
  824. 'category_5_id' => $category_5,
  825. 'no' => Str::random(8),
  826. 'name' => $item['name'],
  827. 'mobile' => $item['mobile'],
  828. 'address_id' => $address_id,
  829. 'address_name' => $address_name,
  830. 'complain_date' => $item['complain_date'],
  831. 'warn_type_id' => 1,
  832. 'body' => $item['body'],
  833. 'body_md5' => $md5,
  834. 'keywords' => $seg_list,
  835. 'deal_department_id' => $department_id,
  836. 'deal_admin_id' => $department_admin_id,
  837. 'deal_evaluation' => $item['deal_evaluation'],
  838. 'deal_status' => $deal_status,
  839. 'deal_idea' => $item['deal_idea'],
  840. 'deal_time' => $item['deal_idea'] ? $item['complain_date'] : null,
  841. 'assign_deal_time' => $item['complain_date'],
  842. 'assign_admin_id' => $admin['id'],
  843. 'is_assign_status' => 1,
  844. 'deal_limit_day' => $item['deal_limit_day'],
  845. 'deal_limit_date' => Carbon::parse($item['complain_date'])->addDays($item['deal_limit_day'])->toDateTimeString(),
  846. 'import_record_id' => $importRecord->id,
  847. 'import_department_id' => $import_department_id,
  848. 'import_admin_id' => $admin['id'],
  849. 'status' => 1
  850. ]);
  851. Log::error("正在创建:第{$i}条数据已完成");
  852. }
  853. DB::commit();
  854. Category::updateLevel();
  855. Log::error("-----------完成---------------");
  856. $importRecord->status = ImportStatusEnum::IMPORT_OK;
  857. $importRecord->message = "共{$total}条,读取{$r_total}条,导入{$w_total}条。";
  858. $importRecord->save();
  859. } catch (\Exception $exception) {
  860. $importRecord->status = ImportStatusEnum::IMPORT_ERROR;
  861. $importRecord->message = $exception->getMessage();
  862. $importRecord->save();
  863. Log::error($exception);
  864. DB::rollBack();
  865. }
  866. }
  867. }