MyFaceDao.java 2.56 KB
package com.vion.dao;


import com.vion.entity.FaceEntity;
import com.vion.entity.OssClientEntity;
import com.vion.job.MyUpdateJob;
import com.vion.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @author 谢明辉
 * @createDate 2018-10-31
 * @description
 */
@Slf4j
public class MyFaceDao implements IDao {

    @Autowired(required = false)
    private JdbcTemplate jdbcTemplate;


    public static long DAY = 3600L * 24L * 1000L;
    public static final String FIND_ALL = "SELECT chanel_id,frr_arrival_face_picture1,frr_arrival_showbody_picture FROM r_face_recognition_record WHERE frr_count_date=?";


    @Override
    public void getAll(Date beginDate, Date endDate, OssClientEntity ossClientEntity, String facePath, String featurePath, ThreadPoolExecutor threadPool) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");

        // 按天循环
        for (long count = beginDate.getTime(); count <= endDate.getTime(); count += DAY) {
            log.info(count + "================================================");

            // 查询一天的数据
            List<FaceEntity> list = jdbcTemplate.query(FIND_ALL, new Object[]{new Date(count)}, (r, i) -> {
                FaceEntity entity = new FaceEntity();
                entity.setBodyPic(r.getString("frr_arrival_showbody_picture"));
                entity.setFacePic(r.getString("frr_arrival_face_picture1"));
                entity.setChannelSerialNum(r.getString("chanel_id"));
                return entity;
            });

            // 平均分成50份,每个线程执行其中的一份,共50个线程
            List<List<FaceEntity>> lists = FileUtils.averageAssign(list, 50);

            Integer pathDate = Integer.parseInt(format.format(new Date(count)));
            lists.forEach(faceEntities -> threadPool.execute(new MyUpdateJob(faceEntities, ossClientEntity, pathDate, facePath, featurePath)));

            // 等待线程池执行完成,执行下个循环
            while (threadPool.getActiveCount() != 0) {

            }

            log.info("==============================={}结束=========================================", new Date(count));

            System.gc();

            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}