package bus.service.impl;

import bus.model.SnowflakeUtil;
import bus.model.dto.*;
import bus.model.vo.BUserCourseVo;
import bus.model.po.BUserCoursePo;
import bus.mapper.BUserCourseMapper;
import bus.service.BUserCourseService;
import bus.service.BCourseInfoService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orcas.common.sso.model.ExtInfo;
import com.orcas.iso.config.common.user.CurrentUserHolder;
import com.qzwisdom.qzframework.core.tool.exception.BusinessException;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.ArrayList;

/**
 * @Program: bus
 * @Description: 用户路线服务实现类
 * @Author: zy
 * @Date: 2025-03-11 18:22:22
 **/
@Service
public class BUserCourseServiceImpl extends ServiceImpl<BUserCourseMapper,BUserCoursePo> implements BUserCourseService {

    @Autowired
    private BUserCourseMapper bUserCourseMapper;

    @Resource
    private SnowflakeUtil snowflakeUtil;

    @Resource
    private BCourseInfoService bCourseInfoService;

    /**
     * 收藏路线
     * @param dto 路线信息
     * @return
     */
    @Override
    public void save(BUserCourseDto dto){
        ExtInfo extInfo = CurrentUserHolder.get();

        QueryWrapper<BUserCoursePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", dto.getCourseId());
        queryWrapper.eq("user_id", extInfo.getUserId());
        List<BUserCoursePo> bUserCoursePos = bUserCourseMapper.selectList(queryWrapper);
        if(CollectionUtil.isNotEmpty(bUserCoursePos)){
            //已经收藏，执行取消收藏
            delete(dto.getCourseId());
            return;
        }
        BUserCoursePo po = new BUserCoursePo();
        po.setUserId(extInfo.getUserId());
        po.setCourseId(dto.getCourseId());
        po.setTop("0");
        po.setId(snowflakeUtil.snowflakeId());
        this.bUserCourseMapper.insert(po);
    }

    /**
     * 顶置路线
     * @param courseId 路线ID
     */
    @Override
    public void topCourse(String courseId) {
        // 获取当前用户信息
        ExtInfo extInfo = CurrentUserHolder.get();
        String userId = extInfo.getUserId();
        // 先将所有路线设置为非顶置
        UpdateWrapper<BUserCoursePo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_id", userId);
        updateWrapper.eq("top","0");
        updateWrapper.set("top","0");
        bUserCourseMapper.update(null,updateWrapper);

        // 将指定路线设置为顶置
        UpdateWrapper<BUserCoursePo> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.eq("user_id", userId);
        updateWrapper1.eq("course_id", courseId);
        updateWrapper1.set("top","1");
        bUserCourseMapper.update(null,updateWrapper1);
    }

    /**
     * 获取我的路线列表
     * @return 路线列表
     */
    @Override
    public List<WBusTrackInfoDto> getMyCourse() {
        // 获取当前用户信息
        ExtInfo extInfo = CurrentUserHolder.get();
        String userId = extInfo.getUserId();
        // 查询用户收藏的路线
        QueryWrapper<BUserCoursePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("top");
        List<BUserCoursePo> bUserCoursePos = bUserCourseMapper.selectList(queryWrapper);
        if(CollectionUtil.isEmpty(bUserCoursePos)) {
            return new ArrayList<>();
        }
        // 获取路线ID列表
        List<String> courseIdList = bUserCoursePos.stream().map(BUserCoursePo::getCourseId).collect(Collectors.toList());
        
        // 获取每个路线的详细信息
        List<WBusTrackInfoDto> result = new ArrayList<>();
        for(String courseId : courseIdList) {
            BCourseInfoDto courseInfo = bCourseInfoService.getDetailById(courseId);
            if(courseInfo != null) {
                // 构建路线追踪信息
                WBusTrackInfoDto trackInfo = new WBusTrackInfoDto();
                trackInfo.setCourseId(courseInfo.getId());
                trackInfo.setCourseName(courseInfo.getCourseName());
                trackInfo.setFirstStationName(courseInfo.getFirstStationName());
                trackInfo.setLastStationName(courseInfo.getLastStationName());
                trackInfo.setFirstBusSpace(courseInfo.getFirstBusSpace());
                trackInfo.setTicketAmount(courseInfo.getTicketAmount());
                // 处理路线站点信息
                List<BCoursePointDto> stationInfoList = courseInfo.getMapPointList();
                trackInfo.setClist(stationInfoList);

                //车辆经纬度
                List<BCourseBusDto> bCourseBusList = courseInfo.getBCourseBusList();
                List<BCoursePointDto> bStationInfoList = new ArrayList<>();
                for(BCourseBusDto bCourseBus : bCourseBusList) {
                    BCoursePointDto bCoursePointDto = new BCoursePointDto();
                    bCoursePointDto.setLongitude(bCourseBus.getLocalLongitude());
                    bCoursePointDto.setLatitude(bCourseBus.getLocalLatitude());
                    bStationInfoList.add(bCoursePointDto);
                }
                trackInfo.setBlist(bStationInfoList);
                result.add(trackInfo);
            }
        }
        return result;
    }

    /**
     * 取消收藏路线
     * @param courseId 路线ID
     * @return
     */
    @Override
    public void delete(String courseId){
        ExtInfo extInfo = CurrentUserHolder.get();
        String userId = extInfo.getUserId();
        QueryWrapper<BUserCoursePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId);
        queryWrapper.eq("user_id", userId);
        bUserCourseMapper.delete(queryWrapper);
    }
}
