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

import me.zhengjie.modules.dm.envmonitor.icdevice.domain.DmEnvIcDevice;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.envmonitor.icdevice.repository.DmEnvIcDeviceRepository;
import me.zhengjie.modules.dm.envmonitor.icdevice.service.DmEnvIcDeviceService;
import me.zhengjie.modules.dm.envmonitor.icdevice.service.dto.DmEnvIcDeviceDto;
import me.zhengjie.modules.dm.envmonitor.icdevice.service.dto.DmEnvIcDeviceQueryCriteria;
import me.zhengjie.modules.dm.envmonitor.icdevice.service.mapstruct.DmEnvIcDeviceMapper;
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 DmEnvIcDeviceServiceImpl implements DmEnvIcDeviceService {

    private final DmEnvIcDeviceRepository dmEnvIcDeviceRepository;
    private final DmEnvIcDeviceMapper dmEnvIcDeviceMapper;

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

    @Override
    public List<DmEnvIcDeviceDto> queryAll(DmEnvIcDeviceQueryCriteria criteria){
        return dmEnvIcDeviceMapper.toDto(dmEnvIcDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmEnvIcDeviceDto findById(Long id) {
        DmEnvIcDevice dmEnvIcDevice = dmEnvIcDeviceRepository.findById(id).orElseGet(DmEnvIcDevice::new);
        ValidationUtil.isNull(dmEnvIcDevice.getId(),"DmEnvIcDevice","id",id);
        return dmEnvIcDeviceMapper.toDto(dmEnvIcDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmEnvIcDeviceDto create(DmEnvIcDevice resources) {
        return dmEnvIcDeviceMapper.toDto(dmEnvIcDeviceRepository.save(resources));
    }

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

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

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