/*
*  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.report_data.service.impl;

import com.youchain.report_data.domain.Report;
import com.youchain.report_data.repository.ReportRepository;
import com.youchain.report_data.service.ReportService;
import com.youchain.report_data.service.dto.ReportDto;
import com.youchain.report_data.service.dto.ReportQueryCriteria;
import com.youchain.report_data.service.mapstruct.ReportMapper;
import com.youchain.utils.FileUtil;
import com.youchain.utils.PageUtil;
import com.youchain.utils.QueryHelp;
import com.youchain.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author liuxue
* @date 2023-11-24
**/
@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final ReportRepository reportRepository;
    private final ReportMapper reportMapper;

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

    @Override
    public List<ReportDto> queryAll(ReportQueryCriteria criteria){
        return reportMapper.toDto(reportRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReportDto findById(Integer id) {
        Report report = reportRepository.findById(id).orElseGet(Report::new);
        ValidationUtil.isNull(report.getId(),"Report","id",id);
        return reportMapper.toDto(report);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportDto create(Report resources) {
        return reportMapper.toDto(reportRepository.save(resources));
    }

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

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

    @Override
    public void download(List<ReportDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReportDto report : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", report.getName());
            map.put("缩略图", report.getThumbnail());
            map.put("内容", report.getContent());
            map.put("创建人", report.getCreateBy());
            map.put("更新人", report.getUpdateBy());
            map.put("创建时间", report.getCreateTime());
            map.put("更新时间", report.getUpdateTime());
            map.put("公司ID", report.getDeptId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}