Newer
Older
huludao / src / main / java / com / newfiber / api / pc / service / impl / PeopleLocationServiceImpl.java
package com.newfiber.api.pc.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.newfiber.api.core.commons.CustomException;
import com.newfiber.api.core.commons.ResultCode;
import com.newfiber.api.pc.dao.PeopleLocationMapper;
import com.newfiber.api.pc.dao.PeopleMapper;
import com.newfiber.api.pc.model.meet.People;
import com.newfiber.api.pc.model.meet.PeopleLocation;
import com.newfiber.api.pc.model.vo.OnlineUserVo;
import com.newfiber.api.pc.model.vo.PeopleOnlineVo;
import com.newfiber.api.pc.service.PeopleLocationService;
import com.newfiber.api.pc.service.PeopleSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 记录当前登录手机APP用户最新经纬度的业务层接口实现类
 * @author: 张鸿志
 * @date: 2021/1/6 19:51
 * @version: v1.0
 */
@Service
public class PeopleLocationServiceImpl extends ServiceImpl<PeopleLocationMapper, PeopleLocation> implements PeopleLocationService {

    private static final Integer  INTERVAL = 3;

    @Autowired
    private PeopleMapper peopleMapper;

    @Override
    public void updateCurrentLocationByUserNo(PeopleLocation peopleLocation) {
        if(!StringUtils.isEmpty(peopleLocation)){
            if(!(StringUtils.isEmpty(peopleLocation.getUserNo()) && StringUtils.isEmpty(peopleLocation.getX()) && StringUtils.isEmpty(peopleLocation.getY()) )){
                //判断当前用户是否已经在表中存在了,如果存在则更新,不存在则修改
                EntityWrapper<PeopleLocation> wrapper = new EntityWrapper<>();
                wrapper.eq("user_no",peopleLocation.getUserNo());
                PeopleLocation location = this.selectOne(wrapper);
                if(!StringUtils.isEmpty(location)){
                    location.setX(peopleLocation.getX());
                    location.setY(peopleLocation.getY());
                    location.setUpdateTime(new Date());
                    this.updateById(location);
                }else{
                    peopleLocation.setUpdateTime(new Date());
                    this.insert(peopleLocation);
                }
            }
        }
        throw new CustomException(ResultCode.PARAM_NULL);
    }


    @Override
    public PeopleLocation selectByUserNo(String userNo) {
        if(StringUtils.isEmpty(userNo)){
            throw new CustomException(ResultCode.PARAM_NULL);
        }
        EntityWrapper<PeopleLocation> wrapper = new EntityWrapper<>();
        wrapper.eq("user_no",userNo);
        PeopleLocation location = this.selectOne(wrapper);
        if(StringUtils.isEmpty(location)){
            PeopleLocation pl = new PeopleLocation();
            pl.setState(0);
            return pl;
        }
        long currentTime = System.currentTimeMillis();
        long oldTime = location.getUpdateTime().getTime();
        //计算相隔的分钟数
        long minutes = (currentTime - oldTime) / ( 1000 * 60 );
        if(minutes > INTERVAL){
            location.setState(0);
        }
        location.setState(1);
        return location;
    }


    @Override
    public List<PeopleOnlineVo> selectOnlineUser() {
        List<PeopleOnlineVo> peopleOnlineVos = new ArrayList<>();
        List<OnlineUserVo> onlineUserVos = peopleMapper.selectOnlineUser();
        List<String> collect = onlineUserVos.stream().map(OnlineUserVo::getPeopleNo).distinct().collect(Collectors.toList());
        long currentTime = System.currentTimeMillis();
        for(String userNo : collect){
            PeopleOnlineVo vo = new PeopleOnlineVo();
            vo.setPeopleNo(userNo);
            ArrayList<String> jobs = new ArrayList<>();
            ArrayList<String> areaNames = new ArrayList<>();
            for(OnlineUserVo onlineVo : onlineUserVos){
                if(userNo.equalsIgnoreCase(onlineVo.getPeopleNo())){
                    vo.setPeopleName(onlineVo.getPeopleName());
                    areaNames.add(onlineVo.getAreaName());
                    vo.setPhone(onlineVo.getPhone());
                    jobs.add(onlineVo.getJob());
                }
                if(StringUtils.isEmpty(onlineVo.getUpdateTime())){
                    vo.setOnlineState(0);
                }else{
                    long oldTime = onlineVo.getUpdateTime().getTime();
                    //计算相隔的分钟数
                    long minutes = (currentTime - oldTime) / ( 1000 * 60 );
                    if(minutes > INTERVAL){
                        vo.setOnlineState(0);
                    }else{
                        vo.setOnlineState(1);
                    }
                }
            }
            //设置集合列表
            vo.setJob(jobs);
            vo.setAreaName(areaNames);
            peopleOnlineVos.add(vo);
        }

        return peopleOnlineVos.stream().sorted().collect(Collectors.toList());
    }
}