TicketJob.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\RecruitAppointInfo;
  4. use App\Models\RecruitPost;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Queue\SerializesModels;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use App\Models\Recruit;
  11. use App\Models\RecruitTicket;
  12. use PhpOffice\PhpWord\TemplateProcessor;
  13. use Illuminate\Support\Facades\Storage;
  14. class TicketJob implements ShouldQueue
  15. {
  16. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  17. private $recruit_id;
  18. private $type;
  19. private $data;
  20. /**
  21. * Create a new job instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct($recruit_id, $type, $data)
  26. {
  27. $this->recruit_id = $recruit_id;
  28. $this->type = $type;
  29. $this->data = $data;
  30. }
  31. /**
  32. * Execute the job.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. $recruit = Recruit::where('id',$this->recruit_id)->first();
  39. if(!$recruit->status){
  40. return ;
  41. }
  42. switch ($this->type){
  43. case 'pen':
  44. if(!$recruit->pen_ticket_content){
  45. return;
  46. }
  47. switch ($this->data['type']){
  48. case '1':
  49. //自动生成
  50. $list = RecruitAppointInfo::where('recruit_id',$this->recruit_id)->where('audit',3)->where('pen_audit',-1)->orderBy('post_id','asc')->orderBy('created_at','asc')->get()->toArray();
  51. $post_data = RecruitPost::where('recruit_id',$this->recruit_id)->where('status',1)->selectRaw('id, CONCAT(code," ",name) as post')->pluck('post', 'id');
  52. $list = array_chunk($list,$this->data['auto_each_number'],true);
  53. if(!empty($recruit->preliminary_start)){
  54. $preliminary_start = date("Y-m-d H:i:s",strtotime("+1 days",date("Y-m-d",time())));
  55. $preliminary_end = date("Y-m-d H:i:s",strtotime("+5 days",date("Y-m-d",time())));
  56. }else{
  57. $preliminary_start = date("Y-m-d H:i:s",strtotime("-5 days",strtotime($recruit->preliminary_start)));
  58. $preliminary_end = date("Y-m-d H:i:s",strtotime("+1 days",strtotime($recruit->preliminary_start)));
  59. }
  60. foreach ($list as $room => $room_list){
  61. if(is_array($room_list)){
  62. foreach ($room_list as $k => $v){
  63. $seat = $k + 1;
  64. $item = [
  65. 'realname' => $v['realname'],
  66. 'ex_number' => date("Y",time()) . '9' . sprintf("%03d",$v['recruit_id']) . sprintf("%04d",$seat),
  67. 'card' => $v['card'],
  68. 'post' => $post_data[$v['post_id']],
  69. 'ex_room' => sprintf("%02d",($room+1)),
  70. 'ex_seat' => sprintf("%03d",$seat),
  71. 'avatar' => $v['avatar']
  72. ];
  73. $ticket = [
  74. 'appoint_id' => $v['id'],
  75. 'ex_type' => 1,
  76. 'ex_number' => $item['ex_number'],
  77. 'ex_seat' => $item['ex_seat'],
  78. 'ex_room' => $item['ex_room'],
  79. 'ex_status' => 0,
  80. 'ex_start' => $preliminary_start,
  81. 'ex_end' => $preliminary_end
  82. ];
  83. RecruitTicket::create($ticket);
  84. $this->fetch($item,$recruit->pen_ticket_content, 'pen', $recruit);
  85. }
  86. }
  87. }
  88. $this->pdf('pen',$recruit);
  89. Recruit::where('id',$this->recruit_id)->update(['pen_ticket_status' => 2]);
  90. break;
  91. case '2':
  92. $ticket_data_list = $this->data['list'];
  93. $list = RecruitAppointInfo::where('recruit_id',$this->recruit_id)->where('audit',3)->where('pen_audit',-1)->orderBy('post_id','asc')->orderBy('created_at','asc')->get()->toArray();
  94. $post_data = RecruitPost::where('recruit_id',$this->recruit_id)->where('status',1)->selectRaw('id, CONCAT(code," ",name) as post')->pluck('post', 'id');
  95. if(empty($recruit->preliminary_start)){
  96. $preliminary_start = date("Y-m-d H:i:s",strtotime("+1 days",strtotime(date("Y-m-d",time()))));
  97. $preliminary_end = date("Y-m-d H:i:s",strtotime("+5 days",strtotime(date("Y-m-d",time()))));
  98. }else{
  99. $preliminary_start = date("Y-m-d H:i:s",strtotime("-5 days",strtotime($recruit->preliminary_start)));
  100. $preliminary_end = date("Y-m-d H:i:s",strtotime("+1 days",strtotime($recruit->preliminary_start)));
  101. }
  102. foreach ($list as $k => $v){
  103. if(!array_key_exists($v['card'],$ticket_data_list)){
  104. echo "{$v['realname']}_{$v['card']}_{$post_data[$v['post_id']]} 不在准考名单内\r\n";
  105. continue;
  106. }
  107. if(!$this->word_exist($recruit,'pen', ['realname' => $v['realname'],'card' => $v['card'],'post' => $post_data[$v['post_id']]])){
  108. $item = [
  109. 'realname' => $v['realname'],
  110. 'ex_number' => array_key_exists('ex_number',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_number'] : '',
  111. 'card' => $v['card'],
  112. 'post' => $post_data[$v['post_id']],
  113. 'ex_address' => array_key_exists('ex_address',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_address'] : '',
  114. 'ex_time' => array_key_exists('ex_time',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_time'] : '',
  115. 'ex_subject' => array_key_exists('ex_subject',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_subject'] : '',
  116. 'ex_room' => array_key_exists('ex_room',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_room'] : '',
  117. 'ex_seat' => array_key_exists('ex_seat',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_seat'] : '',
  118. 'avatar' => $v['avatar']
  119. ];
  120. $ticket = [
  121. 'appoint_id' => $v['id'],
  122. 'ex_type' => 1,
  123. 'ex_number' => $item['ex_number'],
  124. 'ex_address' => array_key_exists('ex_address',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_address'] : '',
  125. 'ex_time' => array_key_exists('ex_time',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_time'] : '',
  126. 'ex_seat' => $item['ex_seat'],
  127. 'ex_room' => $item['ex_room'],
  128. 'ex_subject' => array_key_exists('ex_subject',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_subject'] : '',
  129. 'ex_status' => 0,
  130. 'ex_start' => $preliminary_start,
  131. 'ex_end' => $preliminary_end
  132. ];
  133. RecruitTicket::create($ticket);
  134. $this->fetch($item,$recruit->pen_ticket_content, 'pen', $recruit);
  135. }
  136. }
  137. //$this->pdf('pen',$recruit);
  138. Recruit::where('id',$this->recruit_id)->update(['pen_ticket_status' => 2]);
  139. break;
  140. }
  141. break;
  142. case 'face':
  143. if(!$recruit->face_ticket_content){
  144. return;
  145. }
  146. if(empty($recruit->interview_start)){
  147. $interview_start = date("Y-m-d H:i:s",strtotime("+1 days",strtotime(date("Y-m-d",time()))));
  148. $interview_end = date("Y-m-d H:i:s",strtotime("+5 days",strtotime(date("Y-m-d",time()))));
  149. }else{
  150. $interview_start = date("Y-m-d H:i:s",strtotime("-5 days",strtotime($recruit->interview_start)));
  151. $interview_end = date("Y-m-d H:i:s",strtotime("+1 days",strtotime($recruit->interview_end)));
  152. }
  153. switch ($this->data['type']){
  154. case '0'://不需要额外数据
  155. $list = RecruitAppointInfo::where('recruit_id',$this->recruit_id)->where('audit',3)->where('face_audit',-1)->where(function($query){
  156. $query->where('pen_audit',1)
  157. ->orWhere('pen_audit',3);
  158. })->orderBy('post_id','asc')->orderBy('created_at','asc')->get()->toArray();
  159. $post_data = RecruitPost::where('recruit_id',$this->recruit_id)->where('status',1)->selectRaw('id, CONCAT(code," ",name) as post')->pluck('post', 'id');
  160. if($list){
  161. foreach ($list as $k => $v){
  162. $item = [
  163. 'appoint_id' => $v['id'],
  164. 'ex_type' => 3,
  165. 'realname' => $v['realname'],
  166. 'ex_title' => '',
  167. 'card' => $v['card'],
  168. 'ex_time' => '',
  169. 'ex_address' => '',
  170. 'post' => $post_data[$v['post_id']],
  171. ];
  172. $ticket = [
  173. 'appoint_id' => $v['id'],
  174. 'ex_type' => 3,
  175. 'ex_status' => 1,
  176. 'ex_start' => $interview_start,
  177. 'ex_end' => $interview_end
  178. ];
  179. RecruitTicket::create($ticket);
  180. $this->fetch($item,$recruit->face_ticket_content, 'face', $recruit);
  181. }
  182. }
  183. break;
  184. }
  185. break;
  186. case 'pen_result':
  187. $list = $this->data;
  188. foreach ($list as $k => $v){
  189. if(!array_key_exists('id',$v) || intval($v['id']) <= 0){
  190. continue;
  191. }
  192. $appoint_info = RecruitAppointInfo::where('recruit_appoint_info.id',$v['id'])->where('recruit_appoint_info.recruit_id',$this->recruit_id)->join('recruit_ticket', 'recruit_appoint_info.id', '=', 'recruit_ticket.appoint_id')->select('recruit_appoint_info.*','recruit_ticket.ex_number')->first();
  193. if(!$appoint_info){
  194. continue;
  195. }
  196. if($appoint_info->realname != $v['realname'] || $appoint_info->card != $v['card'] || $appoint_info->ex_number != $v['ex_number']){
  197. //数据污染,路过处理
  198. continue;
  199. }
  200. if($v['next'] == '是'){
  201. $update['pen_audit'] = 1;
  202. $status = -1;
  203. }else{
  204. $update['pen_audit'] = 0;
  205. $status = 3;
  206. }
  207. if(!is_numeric($v['record']) && empty($v['record'])){
  208. $update['pen_audit'] = 2;
  209. $status = 3;
  210. }
  211. if(in_array(4,explode(',',$recruit->step))){
  212. $update['computer_audit'] = $status;
  213. }else{
  214. $update['face_audit'] = $status;
  215. }
  216. RecruitAppointInfo::where('id',$v['id'])->update($update);
  217. }
  218. break;
  219. }
  220. }
  221. protected function fetch($data, $template, $type, $recruit = null){
  222. $templateProcessor = new TemplateProcessor(base_path() . $template);
  223. foreach ($data as $k => $v){
  224. if($k == 'avatar'){
  225. $templateProcessor->setImageValue('avatar',['path' => 'http://www.jucai.gov.cn/'.$v, 'width' => 200, 'height' => 150]);
  226. }else{
  227. $templateProcessor->setValue($k, $v);
  228. }
  229. }
  230. if($recruit){
  231. $name = $recruit->name_en;
  232. $date = date("Y-m-d",strtotime($recruit->created_at));
  233. }else{
  234. $name = uniqid();
  235. $date = date("Y-m-d",time());
  236. }
  237. if(!is_dir(base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}")){
  238. Storage::makeDirectory("public/recruit/ticket/word/{$date}/{$name}/{$type}");
  239. }
  240. // if(!is_dir(base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}" )){
  241. // Storage::makeDirectory("public/recruit/ticket/pdf/{$date}/{$name}/{$type}");
  242. // }
  243. $filename = $date . '_' . $data['realname'] . '_' . $data['card'] . '_' . $data['post'];
  244. $word_url = base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}/" . $filename . '.docx';
  245. $templateProcessor->saveAs($word_url);//另存为
  246. // $pdf_url = base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}";
  247. // shell_exec('export DISPLAY=:0.0');
  248. // shell_exec('libreoffice --headless --convert-to pdf:writer_pdf_Export ' . $word_url . ' --outdir ' . $pdf_url);
  249. }
  250. protected function word_exist($recruit,$type,$data)
  251. {
  252. if($recruit){
  253. $name = $recruit->name_en;
  254. $date = date("Y-m-d",strtotime($recruit->created_at));
  255. }else{
  256. $name = uniqid();
  257. $date = date("Y-m-d",time());
  258. }
  259. $filename = $date . '_' . $data['realname'] . '_' . $data['card'] . '_' . $data['post'];
  260. if(file_exists(base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}/" . $filename . '.docx')){
  261. echo $filename . "跳过\r\n";
  262. return true;
  263. }else{
  264. echo $filename . "生成\r\n";
  265. return false;
  266. }
  267. }
  268. protected function pdf($type, $recruit = null){
  269. if($recruit){
  270. $name = $recruit->name_en;
  271. $date = date("Y-m-d",strtotime($recruit->created_at));
  272. }else{
  273. $name = uniqid();
  274. $date = date("Y-m-d",time());
  275. }
  276. if(!is_dir(base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}" )){
  277. Storage::makeDirectory("public/recruit/ticket/pdf/{$date}/{$name}/{$type}");
  278. }
  279. $word_url = base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}/*.docx";
  280. $pdf_url = base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}";
  281. shell_exec('export DISPLAY=:0.0 && export HOME=/data/wwwroot &&libreoffice --headless --convert-to pdf:writer_pdf_Export ' . $word_url . ' --outdir ' . $pdf_url);
  282. }
  283. }