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

import me.zhengjie.modules.dm.appMenu.domain.DmAppMenu;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.appMenu.repository.DmAppMenuRepository;
import me.zhengjie.modules.dm.appMenu.service.DmAppMenuService;
import me.zhengjie.modules.dm.appMenu.service.dto.DmAppMenuDto;
import me.zhengjie.modules.dm.appMenu.service.dto.DmAppMenuQueryCriteria;
import me.zhengjie.modules.dm.appMenu.service.mapstruct.DmAppMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
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://el-admin.vip
* @description 服务实现
* @author Li Rui
* @date 2021-12-21
**/
@Service
@RequiredArgsConstructor
public class DmAppMenuServiceImpl implements DmAppMenuService {

    private final DmAppMenuRepository dmAppMenuRepository;
    private final DmAppMenuMapper dmAppMenuMapper;

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

    @Override
    public List<DmAppMenuDto> queryAll(DmAppMenuQueryCriteria criteria){
        return dmAppMenuMapper.toDto(dmAppMenuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmAppMenuDto findById(Integer id) {
        DmAppMenu dmAppMenu = dmAppMenuRepository.findById(id).orElseGet(DmAppMenu::new);
        ValidationUtil.isNull(dmAppMenu.getId(),"DmAppMenu","id",id);
        return dmAppMenuMapper.toDto(dmAppMenu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmAppMenuDto create(DmAppMenu resources) {
        return dmAppMenuMapper.toDto(dmAppMenuRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmAppMenuDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmAppMenuDto dmAppMenu : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜单名称", dmAppMenu.getName());
            map.put("菜单图标", dmAppMenu.getIocn());
            map.put("菜单路由", dmAppMenu.getRoute());
//            map.put("创建者", dmAppMenu.getCreateBy());
//            map.put("更新者", dmAppMenu.getUpdateBy());
//            map.put("创建日期", dmAppMenu.getCreateTime());
//            map.put("更新时间", dmAppMenu.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}