package com.sqx.modules.timedtask.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.Result;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.timedtask.dao.RiderLocationDao;
import com.sqx.modules.timedtask.entity.RiderLocation;
import com.sqx.modules.timedtask.service.RiderLocationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

@Service
public class RiderLocationServiceImpl extends ServiceImpl<RiderLocationDao, RiderLocation> implements RiderLocationService {

    @Autowired
    private RiderLocationDao riderLocationDao;
    @Autowired
    private CommonInfoService commonInfoService;

    @Transactional
    @Override
    public Result riderLocation(Long userId, Double lng, Double lat, String province, String city, String district) {
        RiderLocation riderLocation1 = baseMapper.selectOne(new QueryWrapper<RiderLocation>().eq("user_id", userId));
        if(riderLocation1==null){
            RiderLocation riderLocation2 = new RiderLocation();
            riderLocation2.setUserId(userId);
            riderLocation2.setLng(lng);
            riderLocation2.setLat(lat);
            riderLocation2.setProvince(province);
            riderLocation2.setCity(city);
            riderLocation2.setDistrict(district);
            baseMapper.insert(riderLocation2);
            return Result.success();
        }
        riderLocation1.setLng(lng);
        riderLocation1.setLat(lat);
        riderLocation1.setProvince(province);
        riderLocation1.setCity(city);
        riderLocation1.setDistrict(district);
        baseMapper.updateById(riderLocation1);
        return Result.success();
    }

    @Override
    public Result selectRiderLocation(Long riderUserId, Double lng, Double lat) {
        HashMap hashMap = new HashMap();
        //查看骑手即时位置
        RiderLocation riderLocation = baseMapper.selectOne(new QueryWrapper<RiderLocation>().eq("user_id", riderUserId));
        if (ObjectUtil.isNull(riderLocation)) {
            throw new SqxException("无效的用户id");
        }
        //计算骑手到用户的距离
        Double aDouble = riderLocationDao.calculateDistance(lng, lat, riderLocation.getLng(), riderLocation.getLat());
        //计算预计送达时间
        CommonInfo one = commonInfoService.findOne(301);
        Double a = aDouble/Double.valueOf(one.getValue());
        Integer integer = Integer.valueOf(a.intValue());

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MINUTE, +integer);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String mDateTime=formatter.format(c.getTime());

        hashMap.put("riderLocation", riderLocation);
        hashMap.put("aDouble", aDouble);
        hashMap.put("mDateTime", mDateTime);
        return Result.success().put("data", hashMap);
    }

    @Override
    public Result selectAllRiderLocation(String province, String city, String district) {
        List<RiderLocation> riderLocations = baseMapper.selectList(new QueryWrapper<RiderLocation>()
                .eq(StringUtils.isNotEmpty(province),"province", province)
                .eq(StringUtils.isNotEmpty(city), "city", city)
                .eq(StringUtils.isNotEmpty(district), "district", district));
        return Result.success().put("data", riderLocations);
    }


}
