package com.bohuikeji.delivery.backstage.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bohuikeji.delivery.backstage.entity.TLocation;
import com.bohuikeji.delivery.backstage.mapper.TLocationMapper;
import com.bohuikeji.delivery.backstage.service.ITLocationService;
import com.bohuikeji.frame.module.admin.base.ServiceImplBase;
import com.bohuikeji.frame.module.admin.component.RedisUtil;
import com.bohuikeji.frame.module.admin.entity.vo.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 派送员位置记录服务实现类
 * </p>
 *
 * @author 10552
 * @since 2019-08-22
 */
@Slf4j
@Service
public class TLocationServiceImpl extends ServiceImplBase<TLocationMapper, TLocation> implements ITLocationService {


    @Override
    public boolean insertLocation(Integer courierId, AddressVo address, String type) {

        TLocation location=new TLocation();
        location.setCourierId(courierId);
        location.setCreateTime(LocalDateTime.now());
        location.setLocationAddress(address);
        location.setLocationXY(address.getLng()+","+address.getLat());
        location.setLocationType(type);
        return super.save(location);
    }

    @Override
    public JSONArray getLocationArray(Integer courierId) {
        List<TLocation> locationList=list(new QueryWrapper<TLocation>().eq("courier_id",courierId));
        JSONArray array=new JSONArray(locationList.size());
        locationList.forEach(location->{
            JSONArray xy=new JSONArray(2);
            xy.addAll(Arrays.asList(location.getLocationXY().split(",")));
            array.add(xy);
        });
        return array;
    }
}
