SendFaceBatchTask253.java 3.27 KB
package com.sincere.haikangface.async;

import com.sincere.haikangface.bean.StudentBean;
import com.sincere.haikangface.enums.EnumSzBusinessType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 多线程执行卡下发
 * @author xuquan
 * @date 2021/01/12 15:56
 */
@Slf4j
public class SendFaceBatchTask253 implements Runnable {

    private String schoolId;
    private String userType;
    private List<StudentBean> students;
    private String sno;
    private SendUserAsync sendUserAsync;
    CountDownLatch begin;
    CountDownLatch end;

    public SendFaceBatchTask253(String schoolId, String userType,String sno,List<StudentBean> students,
                                SendUserAsync sendUserAsync, CountDownLatch begin, CountDownLatch end){
        this.userType=userType;
        this.schoolId=schoolId;
        this.students=students;
        this.sno=sno;
        this.sendUserAsync=sendUserAsync;
        this.begin=begin;
        this.end=end;
    }

    @Override
    public void run() {
        try {
            begin.await();
            String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.YEAR, 10);
            String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
            //下发标识:详情见枚举 EnumHkOperateType
            int validTimeEnabled = EnumSzBusinessType.EnumHkOperateType.ADD.code;
            for(StudentBean studentBean : students){
                try{
                    String photo= studentBean.getPhoto();
                    if (StringUtils.isBlank(photo)){
                        continue;
                    }
                    String userName= studentBean.getName();
                    String cardNum ="";
                    String typeName="";
                    if(userType.equals("1")){
                        cardNum = studentBean.getTeacher_num();
                        typeName= "Teacher";
                    }else {
                        cardNum = studentBean.getStudent_num();
                        typeName= "Student";
                    }
                    String path = "E:\\wwwhtdocs\\SmartCampus\\face17e50\\School" + schoolId + "\\" + typeName;
                    //以学籍号为名的文件名
                    String fileName = photo.split(typeName+"/")[1];
                    //100服务器上人脸照绝对路径
                    String filePath = path+ "\\" + fileName;
                    Thread.sleep(1000);
                    //下发253服务器
                    sendUserAsync.uploadImgs(filePath, cardNum, userName, sno, startTime, endTime, validTimeEnabled, userType,Integer.parseInt(schoolId));
                }catch (Exception e){
                    log.error("人脸下发失败");
                    e.printStackTrace();
                    continue;
                }
            }
        }catch (Exception e){
            log.error("下发人脸异常,信息:");
            e.printStackTrace();
        }finally {
            end.countDown();
        }
    }

}