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

import me.zhengjie.modules.dm.envmonitor.icdevicenew.domain.DmEnvIcDeciveNew;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.envmonitor.icdevicenew.repository.DmEnvIcDeciveNewRepository;
import me.zhengjie.modules.dm.envmonitor.icdevicenew.service.DmEnvIcDeciveNewService;
import me.zhengjie.modules.dm.envmonitor.icdevicenew.service.dto.DmEnvIcDeciveNewDto;
import me.zhengjie.modules.dm.envmonitor.icdevicenew.service.dto.DmEnvIcDeciveNewQueryCriteria;
import me.zhengjie.modules.dm.envmonitor.icdevicenew.service.mapstruct.DmEnvIcDeciveNewMapper;
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.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 wanglongda
* @date 2022-05-12
**/
@Service
@RequiredArgsConstructor
public class DmEnvIcDeciveNewServiceImpl implements DmEnvIcDeciveNewService {

    private final DmEnvIcDeciveNewRepository dmEnvIcDeciveNewRepository;
    private final DmEnvIcDeciveNewMapper dmEnvIcDeciveNewMapper;

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

    @Override
    public List<DmEnvIcDeciveNewDto> queryAll(DmEnvIcDeciveNewQueryCriteria criteria){
        return dmEnvIcDeciveNewMapper.toDto(dmEnvIcDeciveNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmEnvIcDeciveNewDto findById(Long id) {
        DmEnvIcDeciveNew dmEnvIcDeciveNew = dmEnvIcDeciveNewRepository.findById(id).orElseGet(DmEnvIcDeciveNew::new);
        ValidationUtil.isNull(dmEnvIcDeciveNew.getId(),"DmEnvIcDeciveNew","id",id);
        return dmEnvIcDeciveNewMapper.toDto(dmEnvIcDeciveNew);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmEnvIcDeciveNewDto create(DmEnvIcDeciveNew resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmEnvIcDeciveNewMapper.toDto(dmEnvIcDeciveNewRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmEnvIcDeciveNewDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmEnvIcDeciveNewDto dmEnvIcDeciveNew : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备编码", dmEnvIcDeciveNew.getDeviceIdentifier());
            map.put("设备名称", dmEnvIcDeciveNew.getDeviceName());
            map.put("状态", dmEnvIcDeciveNew.getStatus());
            map.put("注册时间", dmEnvIcDeciveNew.getCreateTime());
            map.put("更新时间", dmEnvIcDeciveNew.getUpdateTime());
            map.put("创建人", dmEnvIcDeciveNew.getCreateBy());
            map.put("更新人", dmEnvIcDeciveNew.getUpdateBy());
            map.put("设备端口", dmEnvIcDeciveNew.getDevicePort());
            map.put("设备队列地址", dmEnvIcDeciveNew.getDeviceTopic());
            map.put("设备ip", dmEnvIcDeciveNew.getDeviceIp());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}