/*
 * Author: wangjian
 * 生成ServiceImpl
 */
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.service.impl.ServiceImpl;
import com.tofly.gpsboot.common.exceptions.MeiCunExceptionDisplay;
import com.tofly.gpsboot.common.exceptions.ResponseStatusEnum;
import com.tofly.gpsboot.mapper.car.CarBaseinfoMapper;
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.CarOilFile;
import com.tofly.pojo.car.dto.CarBaseinfoSaveDTO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 车辆基本信息管理
 *
 * @author Zhangyu
 * @date Mon Apr 27 00:00:00 GMT+08:00 2020
 */
@Service("carBaseinfoService")
@AllArgsConstructor
public class CarBaseinfoServiceImpl extends ServiceImpl<CarBaseinfoMapper, CarBaseinfo> implements CarBaseinfoService {

    private final CarOilFileService carOilFileService;

    @Override
    public List<String> getCommonCarInfo() {
        return this.baseMapper.getCommonCarInfo();
    }

    @Override
    public int customSave(CarBaseinfoSaveDTO dto) {

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

        CopyOptions copyOptions = CopyOptions.create().setIgnoreNullValue(true);
        BeanUtil.copyProperties(dto, carBaseinfo, copyOptions);
        carBaseinfo.setStateName(null);
        carBaseinfo.setCreateUserName(null);
        carBaseinfo.setDepartmentName(null);

        if (CollectionUtils.isNotEmpty(dto.getFilePath())) {
            if (dto.getId() > 0) {
                List<CarOilFile> carOilFiles = carOilFileService.list(Wrappers.<CarOilFile>lambdaQuery().eq(CarOilFile::getPid, carBaseinfo.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(carBaseinfo.getSnowflakeId());
                carOilFile.setFilePath(path);
                carOilFileService.save(carOilFile);
            }
        }

        if (dto.getId() > 0) {
            return super.updateById(carBaseinfo) ? 1 : 0;
        } else {
            carBaseinfo.setId(null);
            return super.save(carBaseinfo) ? 1 : 0;
        }
    }
}
