TicketJob.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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",date("Y-m-d",time())));
  97. $preliminary_end = date("Y-m-d H:i:s",strtotime("+5 days",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. $item = [
  104. 'realname' => $v['realname'],
  105. 'ex_number' => array_key_exists('ex_number',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_number'] : '',
  106. 'card' => $v['card'],
  107. 'post' => $post_data[$v['post_id']],
  108. 'ex_address' => array_key_exists('ex_address',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_address'] : '',
  109. 'ex_time' => array_key_exists('ex_time',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_time'] : '',
  110. 'ex_subject' => array_key_exists('ex_subject',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_subject'] : '',
  111. 'ex_room' => array_key_exists('ex_room',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_room'] : '',
  112. 'ex_seat' => array_key_exists('ex_seat',$ticket_data_list[$v['card']]) ? $ticket_data_list[$v['card']]['ex_seat'] : '',
  113. 'avatar' => $v['avatar']
  114. ];
  115. $ticket = [
  116. 'appoint_id' => $v['id'],
  117. 'ex_type' => 1,
  118. 'ex_number' => $item['ex_number'],
  119. 'ex_seat' => $item['ex_seat'],
  120. 'ex_room' => $item['ex_room'],
  121. 'ex_status' => 0,
  122. 'ex_start' => $preliminary_start,
  123. 'ex_end' => $preliminary_end
  124. ];
  125. RecruitTicket::create($ticket);
  126. $this->fetch($item,$recruit->pen_ticket_content, 'pen', $recruit);
  127. }
  128. $this->pdf('pen',$recruit);
  129. Recruit::where('id',$this->recruit_id)->update(['pen_ticket_status' => 2]);
  130. break;
  131. }
  132. break;
  133. case 'face':
  134. if(!$recruit->face_ticket_content){
  135. return;
  136. }
  137. switch ($this->data['type']){
  138. case '0'://不需要额外数据
  139. $list = RecruitAppointInfo::where('recruit_id',$this->recruit_id)->where('audit',3)->where('pen_audit',1)->where('face_audit',-1)->orderBy('post_id','asc')->orderBy('created_at','asc')->get()->toArray();
  140. $post_data = RecruitPost::where('recruit_id',$this->recruit_id)->where('status',1)->selectRaw('id, CONCAT(code," ",name) as post')->pluck('post', 'id');
  141. if($list){
  142. foreach ($list as $k => $v){
  143. $item = [
  144. 'appoint_id' => $v['id'],
  145. 'ex_type' => 3,
  146. 'realname' => $v['realname'],
  147. 'ex_title' => '',
  148. 'card' => $v['card'],
  149. 'ex_time' => '',
  150. 'ex_address' => '',
  151. 'post' => $post_data[$v['post_id']],
  152. ];
  153. $ticket = [
  154. 'appoint_id' => $v['id'],
  155. 'ex_type' => 3,
  156. 'ex_status' => 1,
  157. 'ex_start' => '2021-05-10 00:00:00',
  158. 'ex_end' => '2021-05-15 23:59:59'
  159. ];
  160. RecruitTicket::create($ticket);
  161. $this->fetch($item,$recruit->face_ticket_content, 'face', $recruit);
  162. }
  163. }
  164. break;
  165. }
  166. break;
  167. case 'pen_result':
  168. $list = $this->data;
  169. foreach ($list as $k => $v){
  170. if(!array_key_exists('id',$v) || intval($v['id']) <= 0){
  171. continue;
  172. }
  173. $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();
  174. if(!$appoint_info){
  175. continue;
  176. }
  177. if($appoint_info->realname != $v['realname'] || $appoint_info->card != $v['card'] || $appoint_info->ex_number != $v['ex_number']){
  178. //数据污染,路过处理
  179. continue;
  180. }
  181. if($v['next'] == '是'){
  182. $update['pen_audit'] = 1;
  183. $status = -1;
  184. }else{
  185. $update['pen_audit'] = 0;
  186. $status = 3;
  187. }
  188. if(!is_numeric($v['record']) && empty($v['record'])){
  189. $update['pen_audit'] = 2;
  190. $status = 3;
  191. }
  192. if(in_array(4,explode(',',$recruit->step))){
  193. $update['computer_audit'] = $status;
  194. }else{
  195. $update['face_audit'] = $status;
  196. }
  197. RecruitAppointInfo::where('id',$v['id'])->update($update);
  198. }
  199. break;
  200. }
  201. }
  202. protected function fetch($data, $template, $type, $recruit = null){
  203. $templateProcessor = new TemplateProcessor(base_path() . $template);
  204. foreach ($data as $k => $v){
  205. if($k == 'avatar'){
  206. $templateProcessor->setImageValue('avatar',['path' => 'http://www.jucai.gov.cn/'.$v, 'width' => 200, 'height' => 150]);
  207. }else{
  208. $templateProcessor->setValue($k, $v);
  209. }
  210. }
  211. if($recruit){
  212. $name = $recruit->name_en;
  213. $date = date("Y-m-d",strtotime($recruit->created_at));
  214. }else{
  215. $name = uniqid();
  216. $date = date("Y-m-d",time());
  217. }
  218. if(!is_dir(base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}")){
  219. Storage::makeDirectory("public/recruit/ticket/word/{$date}/{$name}/{$type}");
  220. }
  221. // if(!is_dir(base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}" )){
  222. // Storage::makeDirectory("public/recruit/ticket/pdf/{$date}/{$name}/{$type}");
  223. // }
  224. $filename = $date . '_' . $data['realname'] . '_' . $data['card'] . '_' . $data['post'];
  225. $word_url = base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}/" . $filename . '.docx';
  226. $templateProcessor->saveAs($word_url);//另存为
  227. // $pdf_url = base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}";
  228. // shell_exec('export DISPLAY=:0.0');
  229. // shell_exec('libreoffice --headless --convert-to pdf:writer_pdf_Export ' . $word_url . ' --outdir ' . $pdf_url);
  230. }
  231. protected function pdf($type, $recruit = null){
  232. if($recruit){
  233. $name = $recruit->name_en;
  234. $date = date("Y-m-d",strtotime($recruit->created_at));
  235. }else{
  236. $name = uniqid();
  237. $date = date("Y-m-d",time());
  238. }
  239. if(!is_dir(base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}" )){
  240. Storage::makeDirectory("public/recruit/ticket/pdf/{$date}/{$name}/{$type}");
  241. }
  242. $word_url = base_path() . "/storage/app/public/recruit/ticket/word/{$date}/{$name}/{$type}/*.docx";
  243. $pdf_url = base_path() . "/storage/app/public/recruit/ticket/pdf/{$date}/{$name}/{$type}";
  244. shell_exec('export DISPLAY=:0.0 && export HOME=/data/wwwroot &&libreoffice --headless --convert-to pdf:writer_pdf_Export ' . $word_url . ' --outdir ' . $pdf_url);
  245. }
  246. }