/*
*  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 me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.Reimburse;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.ReimburseRepository;
import me.zhengjie.modules.system.service.ReimburseService;
import me.zhengjie.modules.system.service.dto.ReimburseDto;
import me.zhengjie.modules.system.service.dto.ReimburseQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.ReimburseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

import java.sql.Timestamp;
import java.time.LocalDateTime;
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;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author author
* 
**/
@Service
@RequiredArgsConstructor
public class ReimburseServiceImpl implements ReimburseService {

    private final ReimburseRepository reimburseRepository;
    private final ReimburseMapper reimburseMapper;

    @Override
    public PageResult<ReimburseDto> queryAll(ReimburseQueryCriteria criteria, Pageable pageable){
        Page<Reimburse> page = reimburseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(reimburseMapper::toDto));
    }

    @Override
    public List<ReimburseDto> queryAll(ReimburseQueryCriteria criteria){
        return reimburseMapper.toDto(reimburseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ReimburseDto findById(Long reimburseId) {
        Reimburse reimburse = reimburseRepository.findById(reimburseId).orElseGet(Reimburse::new);
        ValidationUtil.isNull(reimburse.getReimburseId(),"Reimburse","reimburseId",reimburseId);
        return reimburseMapper.toDto(reimburse);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Reimburse resources) {
        resources.setApplyTime(Timestamp.valueOf(LocalDateTime.now()));
        reimburseRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Reimburse resources) {
        Reimburse reimburse = reimburseRepository.findById(resources.getReimburseId()).orElseGet(Reimburse::new);
        ValidationUtil.isNull( reimburse.getReimburseId(),"Reimburse","id",resources.getReimburseId());
        reimburse.copy(resources);
        reimburseRepository.save(reimburse);
    }

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

    @Override
    public void download(List<ReimburseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReimburseDto reimburse : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("申请人", reimburse.getUser().getNickName());
            map.put("申请事由", reimburse.getReason());
            map.put("申请金额", reimburse.getFee());
            map.put("报销类型", reimburse.getReimburseType());
            map.put("项目名称", reimburse.getProjectName());
            map.put("项目编号", reimburse.getProjectNo());
            map.put("项目预算", reimburse.getProjectFee());
            map.put("花费产生时间", reimburse.getReimburseTime());
            map.put("申请时间", reimburse.getApplyTime());
            map.put("备注", reimburse.getRemark());
            map.put("创建者", reimburse.getCreateBy());
            map.put("更新者", reimburse.getUpdateBy());
            map.put("创建日期", reimburse.getCreateTime());
            map.put("更新时间", reimburse.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}