package com.ozs.service.impl;

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.ozs.common.utils.AppendUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.entity.BaseRailwayManagement;
import com.ozs.entity.BaseTerminal;
import com.ozs.entity.VehiclePosition;
import com.ozs.entity.vo.VehiclePositionResp;
import com.ozs.mapper.BaseRailwayManagementMapper;
import com.ozs.mapper.BaseTerminalMapper;
import com.ozs.mapper.VehiclePositionMapper;
import com.ozs.service.VehiclePositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机车行驶表 服务实现类
 * </p>
 *
 * @author ozs
 * @since 2023-04-11
 */
@Service
public class VehiclePositionServiceImpl extends ServiceImpl<VehiclePositionMapper, VehiclePosition> implements VehiclePositionService {

    @Autowired
    private VehiclePositionMapper vehiclePositionMapper;
    @Autowired
    private BaseTerminalMapper baseTerminalMapper;
    @Autowired
    private BaseRailwayManagementMapper baseRailwayManagementMapper;

    @Override
    public List<VehiclePosition> selectVehiclePositionList(VehiclePositionResp vehiclePositionResp) {
        List<VehiclePosition> list = vehiclePositionMapper.list(vehiclePositionResp.getVehicleCode(), vehiclePositionResp.getRailwayCode(),
                vehiclePositionResp.getLineDir(), vehiclePositionResp.getStatus());
        return list;
    }

    @Override
    public IPage listToPage(List list, int pageNum, int pageSize) {
        List pageList = new ArrayList<>();
        int curIdx = pageNum > 1 ? (pageNum - 1) * pageSize : 0;
        for (int i = 0; i < pageSize && curIdx + i < list.size(); i++) {
            pageList.add(list.get(curIdx + i));
        }
        IPage page = new Page<>(pageNum, pageSize);
        page.setRecords(pageList);
        page.setTotal(list.size());
        return page;
    }

    @Override
    public List<VehiclePosition> vehiclePositionPage(VehiclePositionResp vehiclePositionResp) {
        List<VehiclePosition> vehiclePositions = baseMapper.vehiclePositionPage(vehiclePositionResp.getTerminalCode(),vehiclePositionResp.getTerminalName(),vehiclePositionResp.getStatus());
        for (VehiclePosition vehiclePosition : vehiclePositions) {
            // 查询线路
            LambdaQueryWrapper<BaseRailwayManagement> lwBaseRailwayManagement = new LambdaQueryWrapper<>();
            if (!StringUtils.isEmptySunhh(vehiclePosition.getRailwayCode())) {
                lwBaseRailwayManagement.eq(BaseRailwayManagement::getRailwayCode, vehiclePosition.getRailwayCode());
            }
            BaseRailwayManagement baseRailwayManagement = baseRailwayManagementMapper.selectOne(lwBaseRailwayManagement);
            if (!ObjectUtils.isEmpty(baseRailwayManagement)) {
                vehiclePosition.setRailwayName(baseRailwayManagement.getRailwayName());
            }
            if (!ObjectUtils.isEmpty(vehiclePosition.getTravelMile())) {
                vehiclePosition.setTravelMileFormat(AppendUtils.stringAppend(vehiclePosition.getTravelMile()));
            }
            if (!ObjectUtils.isEmpty(vehiclePosition.getLineDir())) {
                vehiclePosition.setLineDirName(vehiclePosition.getLineDir() == 1 ? "上行" : "下行");
            }
            if (!ObjectUtils.isEmpty(vehiclePosition.getSta())) {
                vehiclePosition.setStatus(vehiclePosition.getSta() == 1 ? "在线" : "离线");
            }
        }
        return vehiclePositions;
    }
}
