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

import me.zhengjie.modules.dmConsumeDevices.domain.DmConsumeDevices;
import me.zhengjie.modules.dmConsumeDevices.service.DmConsumeDevicesService;
import me.zhengjie.modules.dmConsumeDevices.service.dto.DmConsumeDevicesDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dmConsumeDevices.repository.DmConsumeDevicesRepository;
import me.zhengjie.modules.dmConsumeDevices.service.dto.DmConsumeDevicesQueryCriteria;
import me.zhengjie.modules.dmConsumeDevices.service.mapstruct.DmConsumeDevicesMapper;
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 zhirong
* @date 2021-11-19
**/
@Service
@RequiredArgsConstructor
public class DmConsumeDevicesServiceImpl implements DmConsumeDevicesService {

    private final DmConsumeDevicesRepository dmConsumeRepository;
    private final DmConsumeDevicesMapper dmConsumeMapper;

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

    @Override
    public List<DmConsumeDevicesDto> queryAll(DmConsumeDevicesQueryCriteria criteria){
        return dmConsumeMapper.toDto(dmConsumeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmConsumeDevicesDto findById(Long consumeId) {
        DmConsumeDevices dmConsume = dmConsumeRepository.findById(consumeId).orElseGet(DmConsumeDevices::new);
        ValidationUtil.isNull(dmConsume.getId(),"DmConsume","consumeId",consumeId);
        return dmConsumeMapper.toDto(dmConsume);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmConsumeDevicesDto create(DmConsumeDevices resources) {
        return dmConsumeMapper.toDto(dmConsumeRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmConsumeDevicesDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmConsumeDevicesDto dmConsume : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备编号", dmConsume.getCode());
            map.put("设备名称", dmConsume.getName());
            map.put("安装位置", dmConsume.getBuildAddress());
            map.put("登记时间", dmConsume.getCreateTime());
            map.put("备注", dmConsume.getRemark());
            map.put("创建者", dmConsume.getCreateBy());
            map.put("更新者", dmConsume.getUpdateBy());
            map.put("创建时间", dmConsume.getCreateTime());
            map.put("更新时间", dmConsume.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}