package org.jeecg.modules.subwayNetwork.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.dto.PageSearch;
import org.jeecg.modules.subwayNetwork.dto.SubwaySectionEditDTO;
import org.jeecg.modules.subwayNetwork.dto.SubwaySectionQueryDTO;
import org.jeecg.modules.subwayNetwork.entity.LightRail;
import org.jeecg.modules.subwayNetwork.entity.SectionStationMap;
import org.jeecg.modules.subwayNetwork.entity.SubwaySection;
import org.jeecg.modules.subwayNetwork.entity.TrainStation;
import org.jeecg.modules.subwayNetwork.mapper.LightRailMapper;
import org.jeecg.modules.subwayNetwork.mapper.SectionStationMapMapper;
import org.jeecg.modules.subwayNetwork.mapper.SubwaySectionMapper;
import org.jeecg.modules.subwayNetwork.mapper.TrainStationMapper;
import org.jeecg.modules.subwayNetwork.service.ISubwaySectionService;
import org.jeecg.modules.subwayNetwork.vo.SubwaySectionVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 线路车站-地铁区间
 * @Author: jeecg-boot
 * @Date: 2023-06-15
 * @Version: V1.0
 */
@Service
public class SubwaySectionServiceImpl extends ServiceImpl<SubwaySectionMapper, SubwaySection> implements ISubwaySectionService {


    @Resource
    private LightRailMapper lightRailMapper;

    @Resource
    private TrainStationMapper trainStationMapper;

    @Resource
    private SectionStationMapMapper sectionStationMapMapper;

    @Override
    public void edit(SubwaySectionEditDTO dto) {
        SubwaySection subwaySection = BeanUtil.copyProperties(dto, SubwaySection.class);
        LightRail rail = lightRailMapper.selectById(dto.getLightRailId());
        subwaySection.setLightRailName(rail.getRailLineName());
        if (ObjectUtil.isEmpty(dto.getId())) {
            this.save(subwaySection);
        } else {
            this.updateById(subwaySection);
        }

        // 先删除
        LambdaQueryWrapper<SectionStationMap> ssmQueryWrapper = new LambdaQueryWrapper<>();
        ssmQueryWrapper.eq(SectionStationMap::getSectionId, subwaySection.getId());
        sectionStationMapMapper.delete(ssmQueryWrapper);


        // 查询这个区间的车站
        TrainStation startTrainStation = trainStationMapper.selectById(subwaySection.getStartTrainStationId());
        TrainStation endTrainStation = trainStationMapper.selectById(subwaySection.getEndTrainStationId());
        QueryWrapper<TrainStation> tsQueryWrapper = new QueryWrapper<>();
        tsQueryWrapper.select("DISTINCT station_name station_name", "id");
        tsQueryWrapper.lambda().ge(TrainStation::getSeq, startTrainStation.getSeq())
                .le(TrainStation::getSeq, endTrainStation.getSeq())
                .eq(TrainStation::getLightRailId, subwaySection.getLightRailId());
        List<TrainStation> trainStations = trainStationMapper.selectList(tsQueryWrapper);
        LightRail lightRail = lightRailMapper.selectById(subwaySection.getLightRailId());


        // 后新增
        for (TrainStation trainStation : trainStations) {
            SectionStationMap sectionStationMap = new SectionStationMap();
            sectionStationMap.setLightRailId(lightRail.getId());// 车站id
            sectionStationMap.setLightRailName(lightRail.getRailLineName()); // 车站名称
            sectionStationMap.setSectionId(subwaySection.getId());
            sectionStationMap.setSectionName(subwaySection.getSectionName());
            sectionStationMap.setStationId(trainStation.getId());
            sectionStationMap.setStationName(trainStation.getStationName());
            sectionStationMapMapper.insert(sectionStationMap);
        }

    }

    @Override
    public IPage<SubwaySectionVO> queryPageList(PageSearch<SubwaySectionQueryDTO> dto) {
        // 分页查询
        IPage<SubwaySectionVO> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        page = this.baseMapper.queryPageList(page, dto.getQuery());

        for (SubwaySectionVO record : page.getRecords()) {
            // 计算区间里程
            TrainStation startTrainStation = trainStationMapper.selectById(record.getStartTrainStationId());
            TrainStation endTrainStation = trainStationMapper.selectById(record.getEndTrainStationId());
            BigDecimal sectionMileage = endTrainStation.getEndMileage().subtract(startTrainStation.getEndMileage());
            record.setSectionMileage(sectionMileage);


            // 车站数量
            int trainStationNum = endTrainStation.getSeq() - startTrainStation.getSeq() + 1;
            record.setTrainStationNum(trainStationNum);
        }

        return page;

    }

    @Override
    public void delete(String id) {
        LambdaQueryWrapper<SectionStationMap> ssmQueryWrapper = new LambdaQueryWrapper<>();
        ssmQueryWrapper.eq(SectionStationMap::getSectionId, id);
        sectionStationMapMapper.delete(ssmQueryWrapper);

        this.removeById(id);
    }
}
