package com.tofly.gpsboot.custom.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.tofly.common.ftp.config.FtpUtil;
import com.tofly.gpsboot.common.exceptions.MeiCunExceptionDisplay;
import com.tofly.gpsboot.common.exceptions.ResponseStatusEnum;
import com.tofly.gpsboot.custom.mapper.CustomCarBaseinfoMapper;
import com.tofly.gpsboot.custom.service.CustomCarBaseinfoService;
import com.tofly.gpsboot.service.car.CarBaseinfoService;
import com.tofly.gpsboot.service.car.CarOilFileService;
import com.tofly.pojo.car.CarBaseinfo;
import com.tofly.pojo.car.CarMochaitom;
import com.tofly.pojo.car.CarOilFile;
import com.tofly.pojo.car.cusom.CustomCarBaseinfo;
import com.tofly.pojo.car.vo.CarBaseinfoDetailVO;
import com.tofly.pojo.car.vo.CarMochaitomVo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
@Slf4j
public class CustomCarBaseinfoServiceImpl extends ServiceImpl<CustomCarBaseinfoMapper, CustomCarBaseinfo> implements CustomCarBaseinfoService {
    private CarBaseinfoService carBaseinfoService;

    private final CarOilFileService carOilFileService;

    @Override
    public Page<Map> pageByCarNumber(Page page, String number) {
        return page.setRecords(baseMapper.pageByCarNumber(page, number));
    }

    @Override
    @Transactional
    public boolean removeById(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        List<CarBaseinfo> list1 = carBaseinfoService.list(Wrappers.<CarBaseinfo>lambdaQuery()
                .in(CarBaseinfo::getId, list));
        list1.forEach(k -> {
            FtpUtil.deleteFile(k.getCarPic());
        });
        carBaseinfoService.update(Wrappers.<CarBaseinfo>lambdaUpdate().in(CarBaseinfo::getId, list)
                .set(CarBaseinfo::getDelTime, new Date()));
        carBaseinfoService.remove(Wrappers.<CarBaseinfo>lambdaQuery()
                .in(CarBaseinfo::getId, list));
        //uploadPhotoService.removeByIds(list);
        return true;
    }

    @Override
    public CarBaseinfoDetailVO GetDetailById(Long id) {
        CarBaseinfo carBaseinfo = carBaseinfoService.getById(id);
        if (carBaseinfo == null) {
            MeiCunExceptionDisplay.display(ResponseStatusEnum.RECORD_NOT_EXIT);
        }
        List<CarOilFile> carOilFiles = carOilFileService.list(Wrappers.<CarOilFile>lambdaQuery().eq(CarOilFile::getPid, carBaseinfo.getSnowflakeId()));
        CarBaseinfoDetailVO result = new CarBaseinfoDetailVO();
        BeanUtils.copyProperties(carBaseinfo, result);
        result.setFilePath(carOilFiles);
        return result;
    }

    @Override
    public List<CarMochaitomVo> getCarUseInfo(Integer statisticsType, String startDate, String endDate) {
        return this.baseMapper.getCarUseInfo(statisticsType, startDate, endDate);
    }
}
