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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.driver.entity.Driver;
import com.sqx.modules.driver.service.DriverService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.orders.service.OrdersService;
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.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private DriverService driverService;


    @Override
    public Result riderLocation(Long userId, Double lng, Double lat, String province, String city, String district) {
        UserEntity userEntity = userService.getById(userId);
        CommonInfo commonInfo = commonInfoService.findOne(261);
        if (userEntity != null && userEntity.getStatus() == 1 && userEntity.getCashDeposit().compareTo(new BigDecimal(commonInfo.getValue())) >= 0) {
            if (userId != null && lng != null && lat != null && StringUtils.isNotBlank(province) && StringUtils.isNotBlank(city) && StringUtils.isNotBlank(district)) {
                RiderLocation riderLocation = baseMapper.selectOne(new QueryWrapper<RiderLocation>().eq("user_id", userId));
                if (riderLocation == null) {
                    riderLocation = new RiderLocation();
                    riderLocation.setUserId(userId);
                    riderLocation.setLng(lng);
                    riderLocation.setLat(lat);
                    riderLocation.setProvince(province);
                    riderLocation.setCity(city);
                    riderLocation.setDistrict(district);
                    baseMapper.insert(riderLocation);
                    return Result.success();
                } else {
                    riderLocation.setLng(lng);
                    riderLocation.setLat(lat);
                    riderLocation.setProvince(province);
                    riderLocation.setCity(city);
                    riderLocation.setDistrict(district);
                    baseMapper.updateById(riderLocation);
                }
            }
        }
        return Result.success();
    }

    @Override
    public HashMap<String, Object> selectAllRiderLocation(String province, String city, String district, String nickName) {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<RiderLocation> riderLocations = baseMapper.selectAllRiderLocation(province, city, district, nickName);
        for (RiderLocation riderLocation : riderLocations) {
            riderLocation.setUserEntity(userService.getById(riderLocation.getUserId()));
            Driver driver = driverService.selectDriverByUserId(riderLocation.getUserId());
            if(driver!=null){
                riderLocation.setIndentCount(ordersService.count(new QueryWrapper<Orders>().eq("driver_id",driver.getDriverId()).in("status",3,4)));
                riderLocation.setDriverId(driver.getDriverId());
            }
        }
        hashMap.put("riderList", riderLocations);
        List<Orders> ordersList = ordersService.list(new QueryWrapper<Orders>().eq("status", 2));
        hashMap.put("ordersList", ordersList);
        return hashMap;
    }

    @Override
    public int getUserRiderCount(String city, Double lng, Double lat) {
        if (lng == null || lat == null) {
            return 0;
        }
        int kilometreCount = Integer.parseInt(commonInfoService.findOne(418).getValue());

        return baseMapper.getUserRiderCount(kilometreCount, city, lng, lat);

    }

    @Override
    public List<HashMap<String, Object>> getRiderCount(Long userId, String city, Double lng, Double lat) {
        int kilometreCount = Integer.parseInt(commonInfoService.findOne(418).getValue());
        return baseMapper.getRiderCount(kilometreCount, city, lng, lat);
    }


}
