package cn.iocoder.yudao.module.rescue.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.module.rescue.domain.RescueDriverPosition;
import cn.iocoder.yudao.module.rescue.domain.RescueInfoDetail;
import cn.iocoder.yudao.module.rescue.service.IRescueDriverPositionService;
import cn.iocoder.yudao.module.rescue.service.IRescueInfoDetailService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO;
import cn.iocoder.yudao.module.rescue.mapper.RescueDriverPositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

import static cn.iocoder.yudao.module.rescue.utils.DistanceUtil.getDistanceMeter;

/**
 * 救援司机位置记录Service业务层处理
 * 
 * @author zcy
 * @date 2023-09-19
 */
@Service
public class RescueDriverPositionServiceImpl extends ServiceImpl<RescueDriverPositionMapper, RescueDriverPosition> implements IRescueDriverPositionService
{

    @Autowired
    private IRescueInfoDetailService detailService;
    /**
     * 查询救援司机位置记录
     * 
     * @param id 救援司机位置记录主键
     * @return 救援司机位置记录
     */
    @Override
    public RescueDriverPosition selectRescueDriverPositionById(Long id)
    {
        return baseMapper.selectRescueDriverPositionById(id);
    }

    /**
     * 查询救援司机位置记录列表
     * 
     * @param rescueDriverPosition 救援司机位置记录
     * @return 救援司机位置记录
     */
    @Override
    public IPage<RescueDriverPosition> selectRescueDriverPositionList(Page<RescueDriverPosition> page, RescueDriverPosition rescueDriverPosition)
    {
        return baseMapper.selectRescueDriverPositionList(page,rescueDriverPosition);
    }

    /**
     * 新增救援司机位置记录
     * 
     * @param rescueDriverPosition 救援司机位置记录
     * @return 结果
     */
    @Override
    public int insertRescueDriverPosition(RescueDriverPosition rescueDriverPosition)
    {
        return baseMapper.insert(rescueDriverPosition);
    }

    /**
     * 修改救援司机位置记录
     * 
     * @param rescueDriverPosition 救援司机位置记录
     * @return 结果
     */
    @Override
    public int updateRescueDriverPosition(RescueDriverPosition rescueDriverPosition)
    {
        return baseMapper.updateById(rescueDriverPosition);
    }

    /**
     * 批量删除救援司机位置记录
     * 
     * @param ids 需要删除的救援司机位置记录主键
     * @return 结果
     */
    @Override
    public int deleteRescueDriverPositionByIds(Long[] ids)
    {
        return baseMapper.deleteBatchIds(CollectionUtil.toList(ids));
    }

    /**
     * 删除救援司机位置记录信息
     * 
     * @param id 救援司机位置记录主键
     * @return 结果
     */
    @Override
    public int deleteRescueDriverPositionById(Long id)
    {
        return baseMapper.deleteById(id);
    }

    @Override
    public IPage<RescueDriverPosition> listByInfoId(Page<RescueDriverPosition> page, RescueDriverPosition rescueDriverPosition) {
        IPage<RescueDriverPosition> rescueDriverPositions = baseMapper.selectRescueDriverPositionList(page,rescueDriverPosition);
        RescueInfoDetail rescueInfoDetail =new RescueInfoDetail();
        rescueInfoDetail.setRescueInfoId(rescueDriverPosition.getRescueInfoId());
        LambdaQueryWrapper<RescueInfoDetail> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(RescueInfoDetail::getRescueInfoId,rescueDriverPosition.getRescueInfoId()).isNotNull(RescueInfoDetail::getPositionId).orderByAsc(TenantBaseDO::getCreateTime);
        List<RescueInfoDetail> details = detailService.list(queryWrapper);
        List<Long> collect = details.stream().map(RescueInfoDetail::getPositionId).collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(details)){
            for (RescueDriverPosition driverPosition : rescueDriverPositions.getRecords()) {
                if (collect.contains(driverPosition.getId())){
                    driverPosition.setMark("中转点");
                }
            }
        }
        return rescueDriverPositions;
    }

    @Override
    public  IPage<JSONObject> routeInfo(Page<RescueDriverPosition> page, RescueDriverPosition rescueDriverPosition) {
        IPage<RescueDriverPosition> rescueDriverPositions = baseMapper.selectRescueDriverPositionList(page,rescueDriverPosition);
        RescueInfoDetail rescueInfoDetail =new RescueInfoDetail();
        rescueInfoDetail.setRescueInfoId(rescueDriverPosition.getRescueInfoId());
        LambdaQueryWrapper<RescueInfoDetail> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(RescueInfoDetail::getRescueInfoId,rescueDriverPosition.getRescueInfoId()).isNotNull(RescueInfoDetail::getPositionId).orderByAsc(TenantBaseDO::getCreateTime);
        List<RescueInfoDetail> details = detailService.list(queryWrapper);
        List<Long> collect = details.stream().map(RescueInfoDetail::getPositionId).collect(Collectors.toList());
        Long distanceMeter = 0L;
        List<JSONObject> res =new ArrayList<>();
        if (CollectionUtil.isNotEmpty(details)){
            for (int i = 0; i < rescueDriverPositions.getRecords().size()-1; i++) {
                double longitude = Double.parseDouble(rescueDriverPositions.getRecords().get(i+1).getLongitude());
                //纬度
                double latitude = Double.parseDouble(rescueDriverPositions.getRecords().get(i+1).getLatitude());

                double longitude2 = Double.parseDouble(rescueDriverPositions.getRecords().get(i).getLongitude());
                //纬度
                double latitude2 = Double.parseDouble(rescueDriverPositions.getRecords().get(i).getLatitude());
                distanceMeter = distanceMeter + getDistanceMeter(longitude2,latitude2, longitude, latitude);

                if (collect.contains(rescueDriverPositions.getRecords().get(i+1).getId())){
                    LambdaQueryWrapper<RescueInfoDetail> queryWrapper1 =new LambdaQueryWrapper<>();
                    queryWrapper1.eq(RescueInfoDetail::getRescueInfoId,rescueDriverPosition.getRescueInfoId()).eq(RescueInfoDetail::getPositionId,rescueDriverPositions.getRecords().get(i+1).getId());
                    RescueInfoDetail detail = detailService.getOne(queryWrapper1);
                    JSONObject jsonObject =new JSONObject();
                    jsonObject.put("title",detail.getTitle());
                    jsonObject.put("distanceMeter",distanceMeter);
                    res.add(jsonObject);
                    distanceMeter =0L;
                }
            }
        }
        IPage<JSONObject> resultPage = new Page<>(page.getCurrent(), page.getSize());
        resultPage.setRecords(res); // 设置记录
        resultPage.setTotal(rescueDriverPositions.getTotal()); // 设置总数
        return resultPage;
    }


}
