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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.gpsboot.common.exceptions.MeiCunExceptionDisplay;
import com.tofly.gpsboot.common.exceptions.ResponseStatusEnum;
import com.tofly.gpsboot.custom.service.CustomCarBaseinfoService;
import com.tofly.gpsboot.mapper.comm.MeiCunCarOilMapper;
import com.tofly.gpsboot.service.car.CarBaseinfoService;
import com.tofly.gpsboot.service.car.CarOilFileService;
import com.tofly.gpsboot.service.car.MeiCunCarOilService;
import com.tofly.pojo.car.CarOilFile;
import com.tofly.pojo.car.MeiCunCarOil;
import com.tofly.pojo.car.dto.CarOilSaveDTO;
import com.tofly.pojo.car.vo.CarBaseinfoDetailVO;
import com.tofly.pojo.car.vo.MeiCunCarOilDetailVO;
import com.tofly.pojo.car.vo.MeiCunCarOilVO;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author wujianjun
 * @date 2023/12/11 10:02
 */
@AllArgsConstructor
@Service("meiCunCarOilService")
public class MeiCunCarOilServiceImpl extends ServiceImpl<MeiCunCarOilMapper, MeiCunCarOil> implements MeiCunCarOilService {


    private final CarOilFileService carOilFileService;

    private final CustomCarBaseinfoService carBaseinfoService;

    @Override
    public Page<MeiCunCarOilVO> cutomPage(Page page) {
        return page.setRecords(this.baseMapper.customPage(page));
    }

    @Override
    @Transactional
    public int customSave(CarOilSaveDTO dto) {

        MeiCunCarOil meiCunCarOil;
        if (dto.getId() > 0) {
            meiCunCarOil = super.getById(dto.getId());
            if (null == meiCunCarOil) {
                MeiCunExceptionDisplay.display(ResponseStatusEnum.RECORD_NOT_EXIT);
            }
        } else {
            //雪花ID
            meiCunCarOil = new MeiCunCarOil();
            Snowflake snowflake = new Snowflake(12L, 18L);
            String sid = snowflake.nextIdStr();
            System.out.println("sid=" + sid);
            meiCunCarOil.setSnowflakeId(sid);
        }

        CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true);
        BeanUtil.copyProperties(dto, meiCunCarOil, copyOptions);

        if (CollectionUtils.isNotEmpty(dto.getFilePath())) {
            if (dto.getId() > 0) {
                List<CarOilFile> carOilFiles = carOilFileService.list(Wrappers.<CarOilFile>lambdaQuery().eq(CarOilFile::getPid, meiCunCarOil.getSnowflakeId()));
                if (CollectionUtils.isNotEmpty(carOilFiles)) {
                    List<Long> ids = carOilFiles.stream().map(CarOilFile::getId).collect(Collectors.toList());
                    carOilFileService.removeByIds(ids);
                }
            }
            for (String path : dto.getFilePath()) {
                CarOilFile carOilFile = new CarOilFile();
                carOilFile.setPid(meiCunCarOil.getSnowflakeId());
                carOilFile.setFilePath(path);
                carOilFileService.save(carOilFile);
            }
        }
        if (dto.getId() > 0) {
            return super.updateById(meiCunCarOil) ? 1 : 0;
        } else {
            return super.save(meiCunCarOil) ? 1 : 0;
        }
    }


    @Override
    public MeiCunCarOilDetailVO getDetailById(Long id) {

        MeiCunCarOil meiCunCarOil = super.getById(id);
        if (null == meiCunCarOil) {
            MeiCunExceptionDisplay.display(ResponseStatusEnum.RECORD_NOT_EXIT);
        }

        MeiCunCarOilDetailVO result = new MeiCunCarOilDetailVO();
        BeanUtils.copyProperties(meiCunCarOil, result);

        List<CarOilFile> carOilFiles = carOilFileService.list(Wrappers.<CarOilFile>lambdaQuery().eq(CarOilFile::getPid, meiCunCarOil.getSnowflakeId()));
        result.setFilePath(carOilFiles);

        CarBaseinfoDetailVO carBaseinfoDetailVO = carBaseinfoService.GetDetailById(meiCunCarOil.getVehicleId());
        result.setCarDetail(carBaseinfoDetailVO);
        return result;
    }
}
