PostFaceDao.java 2.48 KB
package com.vion.dao;


import com.vion.entity.FaceEntity;
import com.vion.entity.OssClientEntity;
import com.vion.job.PostUpdateJob;
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 PostFaceDao implements IDao {


    public static long DAY = 3600L * 24L * 1000L;
    public static final String FIND_ALL = "SELECT channel_serialnum,face_pic,body_pic FROM d_face_recognition WHERE countdate=?";

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @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("body_pic"));
                entity.setFacePic(r.getString("face_pic"));
                entity.setChannelSerialNum(r.getString("channel_serialnum"));
                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 PostUpdateJob(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();
            }
        }
    }
}