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

import me.zhengjie.modules.dm.envmonitor.peptdevicenew.domain.DmEnvPeptDeciveNew;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.envmonitor.peptdevicenew.repository.DmEnvPeptDeciveNewRepository;
import me.zhengjie.modules.dm.envmonitor.peptdevicenew.service.DmEnvPeptDeciveNewService;
import me.zhengjie.modules.dm.envmonitor.peptdevicenew.service.dto.DmEnvPeptDeciveNewDto;
import me.zhengjie.modules.dm.envmonitor.peptdevicenew.service.dto.DmEnvPeptDeciveNewQueryCriteria;
import me.zhengjie.modules.dm.envmonitor.peptdevicenew.service.mapstruct.DmEnvPeptDeciveNewMapper;
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-13
**/
@Service
@RequiredArgsConstructor
public class DmEnvPeptDeciveNewServiceImpl implements DmEnvPeptDeciveNewService {

    private final DmEnvPeptDeciveNewRepository dmEnvPeptDeciveNewRepository;
    private final DmEnvPeptDeciveNewMapper dmEnvPeptDeciveNewMapper;

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

    @Override
    public List<DmEnvPeptDeciveNewDto> queryAll(DmEnvPeptDeciveNewQueryCriteria criteria){
        return dmEnvPeptDeciveNewMapper.toDto(dmEnvPeptDeciveNewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmEnvPeptDeciveNewDto findById(Long id) {
        DmEnvPeptDeciveNew dmEnvPeptDeciveNew = dmEnvPeptDeciveNewRepository.findById(id).orElseGet(DmEnvPeptDeciveNew::new);
        ValidationUtil.isNull(dmEnvPeptDeciveNew.getId(),"DmEnvPeptDeciveNew","id",id);
        return dmEnvPeptDeciveNewMapper.toDto(dmEnvPeptDeciveNew);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmEnvPeptDeciveNewDto create(DmEnvPeptDeciveNew resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmEnvPeptDeciveNewMapper.toDto(dmEnvPeptDeciveNewRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmEnvPeptDeciveNewDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmEnvPeptDeciveNewDto dmEnvPeptDeciveNew : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备编码", dmEnvPeptDeciveNew.getDeviceIdentifier());
            map.put("设备名称", dmEnvPeptDeciveNew.getDeviceName());
            map.put("状态", dmEnvPeptDeciveNew.getStatus());
            map.put("注册时间", dmEnvPeptDeciveNew.getCreateTime());
            map.put("更新时间", dmEnvPeptDeciveNew.getUpdateTime());
            map.put("创建人", dmEnvPeptDeciveNew.getCreateBy());
            map.put("更新人", dmEnvPeptDeciveNew.getUpdateBy());
            map.put("所在楼层", dmEnvPeptDeciveNew.getDeviceFloor());
            map.put("所在位置", dmEnvPeptDeciveNew.getDeviceAddress());
            map.put("关联通信设备", dmEnvPeptDeciveNew.getDmEnvIcDeciveNew());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}