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

import me.zhengjie.modules.dm.envmonitor.peptdevice.domain.DmEnvPeptDevice;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.envmonitor.peptdevice.repository.DmEnvPeptDeviceRepository;
import me.zhengjie.modules.dm.envmonitor.peptdevice.service.DmEnvPeptDeviceService;
import me.zhengjie.modules.dm.envmonitor.peptdevice.service.dto.DmEnvPeptDeviceDto;
import me.zhengjie.modules.dm.envmonitor.peptdevice.service.dto.DmEnvPeptDeviceQueryCriteria;
import me.zhengjie.modules.dm.envmonitor.peptdevice.service.mapstruct.DmEnvPeptDeviceMapper;
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 shuai.huang
* @date 2021-03-30
**/
@Service
@RequiredArgsConstructor
public class DmEnvPeptDeviceServiceImpl implements DmEnvPeptDeviceService {

    private final DmEnvPeptDeviceRepository dmEnvPeptDeviceRepository;
    private final DmEnvPeptDeviceMapper dmEnvPeptDeviceMapper;

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

    @Override
    public List<DmEnvPeptDeviceDto> queryAll(DmEnvPeptDeviceQueryCriteria criteria){
        return dmEnvPeptDeviceMapper.toDto(dmEnvPeptDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmEnvPeptDeviceDto findById(Long id) {
        DmEnvPeptDevice dmEnvPeptDevice = dmEnvPeptDeviceRepository.findById(id).orElseGet(DmEnvPeptDevice::new);
        ValidationUtil.isNull(dmEnvPeptDevice.getId(),"DmEnvPeptDevice","id",id);
        return dmEnvPeptDeviceMapper.toDto(dmEnvPeptDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmEnvPeptDeviceDto create(DmEnvPeptDevice resources) {
        return dmEnvPeptDeviceMapper.toDto(dmEnvPeptDeviceRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmEnvPeptDeviceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmEnvPeptDeviceDto dmEnvPeptDevice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备编号", dmEnvPeptDevice.getDeviceIdentifier());
            map.put("设备名称", dmEnvPeptDevice.getDeviceName());
            map.put("参数名称", dmEnvPeptDevice.getParmName());
            map.put("参数值", dmEnvPeptDevice.getParmValue());
            map.put("设备状态", dmEnvPeptDevice.getStatue());
            map.put("电流", dmEnvPeptDevice.getCurrent());
            map.put("电压", dmEnvPeptDevice.getVoltage());
            map.put("创建人", dmEnvPeptDevice.getCreateBy());
            map.put("修改人", dmEnvPeptDevice.getUpdateBy());
            map.put("创建时间", dmEnvPeptDevice.getCreateTime());
            map.put("修改时间", dmEnvPeptDevice.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}