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

import me.zhengjie.modules.dm.roomDevice.domain.DmRoomDevice;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.roomDevice.repository.DmRoomDeviceRepository;
import me.zhengjie.modules.dm.roomDevice.service.DmRoomDeviceService;
import me.zhengjie.modules.dm.roomDevice.service.dto.DmRoomDeviceDto;
import me.zhengjie.modules.dm.roomDevice.service.dto.DmRoomDeviceQueryCriteria;
import me.zhengjie.modules.dm.roomDevice.service.mapstruct.DmRoomDeviceMapper;
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 Zheng Jie
* @date 2021-11-27
**/
@Service
@RequiredArgsConstructor
public class DmRoomDeviceServiceImpl implements DmRoomDeviceService {

    private final DmRoomDeviceRepository dmRoomDeviceRepository;
    private final DmRoomDeviceMapper dmRoomDeviceMapper;

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

    @Override
    public List<DmRoomDeviceDto> queryAll(DmRoomDeviceQueryCriteria criteria){
        return dmRoomDeviceMapper.toDto(dmRoomDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmRoomDeviceDto findById(Long id) {
        DmRoomDevice dmRoomDevice = dmRoomDeviceRepository.findById(id).orElseGet(DmRoomDevice::new);
        ValidationUtil.isNull(dmRoomDevice.getId(),"DmRoomDevice","id",id);
        return dmRoomDeviceMapper.toDto(dmRoomDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmRoomDeviceDto create(DmRoomDevice resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmRoomDeviceMapper.toDto(dmRoomDeviceRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmRoomDeviceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmRoomDeviceDto dmRoomDevice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备编号", dmRoomDevice.getCode());
            map.put("设备名称", dmRoomDevice.getName());
            map.put("设备安装位置", dmRoomDevice.getBuildAddress());
            map.put("设备状态(00-启用、01-禁用、02-待维修)", dmRoomDevice.getStatus());
            map.put("备注", dmRoomDevice.getRemark());
            map.put("创建者", dmRoomDevice.getCreateBy());
            map.put("更新者", dmRoomDevice.getUpdateBy());
            map.put("创建时间", dmRoomDevice.getCreateTime());
            map.put("更新时间", dmRoomDevice.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}