/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.youchain.basicdata.service.impl;

import com.youchain.basicdata.domain.CarReg;
import com.youchain.utils.FileUtil;
import com.youchain.utils.PageUtil;
import com.youchain.utils.QueryHelp;
import com.youchain.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.CarRegRepository;
import com.youchain.basicdata.service.CarRegService;
import com.youchain.basicdata.service.dto.CarRegDto;
import com.youchain.basicdata.service.dto.CarRegQueryCriteria;
import com.youchain.basicdata.service.mapstruct.CarRegMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author liuxue
* @date 2023-08-09
**/
@Service
@RequiredArgsConstructor
public class CarRegServiceImpl implements CarRegService {

    private final CarRegRepository carRegRepository;
    private final CarRegMapper carRegMapper;

    @Override
    public Map<String,Object> queryAll(CarRegQueryCriteria criteria, Pageable pageable){
        Page<CarReg> page = carRegRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(carRegMapper::toDto));
    }

    @Override
    public List<CarRegDto> queryAll(CarRegQueryCriteria criteria){
        return carRegMapper.toDto(carRegRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CarRegDto findById(Long id) {
        CarReg carReg = carRegRepository.findById(id).orElseGet(CarReg::new);
        ValidationUtil.isNull(carReg.getId(),"CarReg","id",id);
        return carRegMapper.toDto(carReg);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CarRegDto create(CarReg resources) {
        return carRegMapper.toDto(carRegRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CarReg resources) {
        CarReg carReg = carRegRepository.findById(resources.getId()).orElseGet(CarReg::new);
        ValidationUtil.isNull( carReg.getId(),"CarReg","id",resources.getId());
        carReg.copy(resources);
        carRegRepository.save(carReg);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            carRegRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CarRegDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CarRegDto carReg : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("供应商", carReg.getSupplier());
            map.put("车牌", carReg.getCarNo());
            map.put("车型", carReg.getCarType());
            map.put("数量", carReg.getNumber());
            map.put("预约时间", carReg.getAppointmentTime());
            map.put("到达时间", carReg.getArrivalTime());
            map.put("单位", carReg.getItemUnit());
            map.put(" createBy",  carReg.getCreateBy());
            map.put(" updateBy",  carReg.getUpdateBy());
            map.put(" createTime",  carReg.getCreateTime());
            map.put(" updateTime",  carReg.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}