PersonTrackDetailServiceImpl.java 3.85 KB
package com.viontech.keliu.service.impl;

import com.viontech.keliu.dao.DPersonTrackDetailDao;
import com.viontech.keliu.dao.FaceRecognitionDao;
import com.viontech.keliu.entity.DPersonTrackDetail;
import com.viontech.keliu.entity.Person;
import com.viontech.keliu.service.PersonTrackDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: zhuhai
 * Date: 2023-12-21
 * Time: 16:53
 */
@Service
public class PersonTrackDetailServiceImpl implements PersonTrackDetailService {


    @Resource
    private FaceRecognitionDao faceRecognitionDao;

    @Resource
    private DPersonTrackDetailDao dPersonTrackDetailDao;


    @Override
    public void handlePersonTrackDetail(Long mallId, Date countDate) {

        List<Person> personList = faceRecognitionDao.queryGateFaceRecognitionList(mallId, countDate);
        if (CollectionUtils.isEmpty(personList)) {
            return;
        }
        //按照gateId分组
        Map<Long, List<Person>> gatePersonMap = personList.stream().collect(Collectors.groupingBy(Person::getGateId));
        gatePersonMap.forEach((key, value) -> createPersonTrack(value));
    }


    /**
     * 按照区域进行统计
     * @param personList
     */
    private void createPersonTrack(List<Person> personList) {
        if (personList.size() < 2) {
            return;
        }
        //按personUnid分组
        Map<String, List<Person>> personUnidMap = personList.stream().collect(Collectors.groupingBy(Person::getPersonUnid));
        personUnidMap.forEach((key, value) -> {
            List<DPersonTrackDetail> personTrackDetailList = getPersonTrackDetail(value);
            if (CollectionUtils.isNotEmpty(personTrackDetailList)) {
                //保存进数据库
                dPersonTrackDetailDao.createDPersonTrackDetailBatch(personTrackDetailList);
            }

        });

    }

    /**
     * 按personUnid进行区域进出匹配
     * @param personList
     * @return
     */
    private List<DPersonTrackDetail> getPersonTrackDetail(List<Person> personList) {
        if (personList.size() < 2) {
            return null;
        }
        List<DPersonTrackDetail> dPersonTrackDetailList = new ArrayList<>();
        Person arrivePerson = null;
        int isEngage = 0;
        for (Person person : personList) {
            //进
            if (person.getDirection() == 1) {
                arrivePerson = person;
                continue;
            }
            //试乘
            if (person.getDirection() == 6) {
                isEngage = 1;
                continue;
            }
            //进出匹配的
            if (person.getDirection() == -1 && arrivePerson != null) {
                DPersonTrackDetail personTrackDetail = new DPersonTrackDetail();
                personTrackDetail.setAccountId(person.getAccountId());
                personTrackDetail.setMallId(person.getMallId());
                personTrackDetail.setGateId(person.getGateId());
                personTrackDetail.setPersonUnid(person.getPersonUnid());
                personTrackDetail.setArriveTime(arrivePerson.getCountTime());
                personTrackDetail.setLeaveTime(person.getCountTime());
                long milli = personTrackDetail.getLeaveTime().getTime() - personTrackDetail.getArriveTime().getTime();
                personTrackDetail.setVisitDuration(milli);
                personTrackDetail.setIsEngage(isEngage);
                arrivePerson = null;
                dPersonTrackDetailList.add(personTrackDetail);
            }
            isEngage = 0;

        }
        return dPersonTrackDetailList;
    }
}