PersonGroupServiceImpl.java 9.58 KB
package com.viontech.keliu.service.impl;

import cn.hutool.core.date.DateUtil;
import com.viontech.keliu.dao.DMallPersonGroupDayCountDataDao;
import com.viontech.keliu.dao.DMallPersonGroupHourCountDataDao;
import com.viontech.keliu.dao.DataCountDao;
import com.viontech.keliu.entity.DMallPersonGroupDayCountData;
import com.viontech.keliu.entity.DMallPersonGroupHourCountData;
import com.viontech.keliu.entity.Person;
import com.viontech.keliu.entity.PersonGroupResult;
import com.viontech.keliu.service.PersonGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: zhuhai
 * Date: 2022-11-02
 * Time: 17:57
 */
@Service
@Slf4j
public class PersonGroupServiceImpl implements PersonGroupService {
    @Resource
    private DataCountDao dataCountDao;

    @Resource
    private DMallPersonGroupDayCountDataDao dMallPersonGroupDayCountDataDao;
    @Resource
    private DMallPersonGroupHourCountDataDao dMallPersonGroupHourCountDataDao;



    @Resource
    private TaskExecutor taskExecutor;


    @Override
    public void personToGroup(List<Long> mallIdList, Date countDate) {
        if (CollectionUtils.isEmpty(mallIdList)) {
            return;
        }
        CountDownLatch countDownLatch = new CountDownLatch(mallIdList.size());
        for (Long mallId : mallIdList) {
            log.info("mallId:{},开始执行人员分组统计", mallId);
            taskExecutor.execute(() -> {
                try {
                    List<Person> personList = dataCountDao.getPersonByMallId(mallId, countDate);
                    //按天统计
                    personGroupDayStat(personList, mallId, countDate);
                    //按小时统计
                    Map<Date, List<Person>> hourPersonList = personList.stream().collect(Collectors.groupingBy((p) -> DateUtil.beginOfHour(p.getCountTime())));
                    for (Map.Entry<Date, List<Person>> entry : hourPersonList.entrySet()) {
                        personGroupHourStat(entry.getValue(), mallId, countDate, entry.getKey());
                    }

                } catch (Exception e) {
                   log.error("mallId:{},人员分组异常", mallId, e);
                } finally {
                    countDownLatch.countDown();
                }
            });

        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            log.error("线程等待异常", e);
        }


    }


    /**
     * 顾客组数小时级统计表
     * @param personList
     * @param mallId
     * @param countDate
     * @param countTime
     */
    private void personGroupHourStat(List<Person> personList, Long mallId, Date countDate, Date countTime) {
        PersonGroupResult personGroupResult = personGroupCompute(personList);
        DMallPersonGroupHourCountData dMallPersonGroupHourCountData = new DMallPersonGroupHourCountData();
        dMallPersonGroupHourCountData.setMallId(mallId);
        dMallPersonGroupHourCountData.setCountDate(countDate);
        dMallPersonGroupHourCountData.setCountTime(countTime);
        dMallPersonGroupHourCountData.setHour(DateUtil.hour(countTime, true));
        dMallPersonGroupHourCountData.setTotalPersonNum(personList.size());
        dMallPersonGroupHourCountData.setStaffNum(personGroupResult.getStaffNum());
        dMallPersonGroupHourCountData.setCustomerNum(personGroupResult.getCustomerNum());
        dMallPersonGroupHourCountData.setCustomerAdultNum(personGroupResult.getCustomerAdultNum());
        dMallPersonGroupHourCountData.setCustomerChildNum(personGroupResult.getCustomerChildNum());
        dMallPersonGroupHourCountData.setCustomerAlone(personGroupResult.getCustomerAlone());
        dMallPersonGroupHourCountData.setCustomerGroup(personGroupResult.getCustomerGroup());
        //判断数据库中是否已经存在记录,如果不存在则插入记录,否则更新记录
        List<DMallPersonGroupHourCountData> countDataList = dMallPersonGroupHourCountDataDao.selectByMallIdAndCountDate(mallId, countDate, countTime);
        if (CollectionUtils.isEmpty(countDataList)) {
            //插入记录
            dMallPersonGroupHourCountDataDao.create(dMallPersonGroupHourCountData);
        } else {
            //更新记录
            DMallPersonGroupHourCountData countData = countDataList.get(0);
            dMallPersonGroupHourCountData.setId(countData.getId());
            dMallPersonGroupHourCountDataDao.update(dMallPersonGroupHourCountData);

        }
    }


    /**
     * 顾客组数天级统计表
     * @param personList
     * @param mallId
     * @param countDate
     */
    private void personGroupDayStat(List<Person> personList, Long mallId, Date countDate) {
        DMallPersonGroupDayCountData dMallPersonGroupDayCountData = new DMallPersonGroupDayCountData();
        if (CollectionUtils.isEmpty(personList)) {
            dMallPersonGroupDayCountData.setMallId(mallId);
            dMallPersonGroupDayCountData.setCountDate(countDate);
            dMallPersonGroupDayCountData.setTotalPersonNum(0);
            dMallPersonGroupDayCountData.setStaffNum(0);
            dMallPersonGroupDayCountData.setCustomerNum(0);
            dMallPersonGroupDayCountData.setCustomerAdultNum(0);
            dMallPersonGroupDayCountData.setCustomerChildNum(0);
            dMallPersonGroupDayCountData.setCustomerAlone(0);
            dMallPersonGroupDayCountData.setCustomerGroup(0);
        } else {
            PersonGroupResult personGroupResult = personGroupCompute(personList);
            dMallPersonGroupDayCountData.setMallId(mallId);
            dMallPersonGroupDayCountData.setCountDate(countDate);
            dMallPersonGroupDayCountData.setTotalPersonNum(personList.size());
            dMallPersonGroupDayCountData.setStaffNum(personGroupResult.getStaffNum());
            dMallPersonGroupDayCountData.setCustomerNum(personGroupResult.getCustomerNum());
            dMallPersonGroupDayCountData.setCustomerAdultNum(personGroupResult.getCustomerAdultNum());
            dMallPersonGroupDayCountData.setCustomerChildNum(personGroupResult.getCustomerChildNum());
            dMallPersonGroupDayCountData.setCustomerAlone(personGroupResult.getCustomerAlone());
            dMallPersonGroupDayCountData.setCustomerGroup(personGroupResult.getCustomerGroup());
        }
        //判断数据库中是否已经存在记录,如果不存在则插入记录,否则更新记录
        List<DMallPersonGroupDayCountData> countDataList = dMallPersonGroupDayCountDataDao.selectByMallIdAndCountDate(mallId, countDate);
        if (CollectionUtils.isEmpty(countDataList)) {
            //插入记录
            dMallPersonGroupDayCountDataDao.create(dMallPersonGroupDayCountData);
        } else {
            //更新记录
            DMallPersonGroupDayCountData countData = countDataList.get(0);
            dMallPersonGroupDayCountData.setId(countData.getId());
            dMallPersonGroupDayCountDataDao.update(dMallPersonGroupDayCountData);

        }
    }


    /**
     * 分组统计逻辑
     * @param personList
     * @return
     */
    private PersonGroupResult personGroupCompute(List<Person> personList) {
        int staffNum = (personList.stream().filter((t) -> t.getPersonType().equals(1)).collect(Collectors.toList())).size();
        List<Person> customerList = (personList.stream().filter((t) -> t.getPersonType().equals(0)).collect(Collectors.toList())).stream().sorted(Comparator.comparing(Person::getCountTime)).collect(Collectors.toList());
        int customerNum = customerList.size();
        int customerAdultNum = 0;
        int customerChildNum = 0;
        for (Person person : customerList) {
            Integer mood = person.getMood();
            if (mood == null) {
                continue;
            }
            if (mood.equals(108)) {
                customerAdultNum += 1;
            } else if (mood.equals(107)) {
                customerChildNum += 1;
            }
        }
        int customerAlone = 0;
        int customerGroup = 0;
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < customerList.size() - 1; ++i) {
            Person pvo1 = customerList.get(i);
            Person pvo2 = customerList.get(i + 1);
            Long time1 = pvo1.getCountTime().getTime();
            Long time2 = pvo2.getCountTime().getTime();
            int seconds = (int) ((time2 - time1) / 1000L);
            if (i == 0) {
                str.append(pvo1.getMood());
            }
            if (seconds <= 6) {
                str.append(",").append(pvo2.getMood());
            } else {
                str.append("#").append(pvo2.getMood());
            }
        }
        String[] group = str.toString().split("#");
        for (String s : group) {
            String[] split = s.split(",");
            //单独的加一
            if (split.length == 1) {
                customerAlone += 1;
            } else {
                //分组加一
                customerGroup += 1;
            }
        }

        PersonGroupResult result = new PersonGroupResult();
        result.setStaffNum(staffNum);
        result.setCustomerNum(customerNum);
        result.setCustomerAdultNum(customerAdultNum);
        result.setCustomerChildNum(customerChildNum);
        result.setCustomerAlone(customerAlone);
        result.setCustomerGroup(customerGroup);

        return result;

    }


}