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

import me.zhengjie.modules.dm.envmonitor.monitorobject.domain.DmEnvMonitorObject;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.envmonitor.monitorobject.repository.DmEnvMonitorObjectRepository;
import me.zhengjie.modules.dm.envmonitor.monitorobject.service.DmEnvMonitorObjectService;
import me.zhengjie.modules.dm.envmonitor.monitorobject.service.dto.DmEnvMonitorObjectDto;
import me.zhengjie.modules.dm.envmonitor.monitorobject.service.dto.DmEnvMonitorObjectQueryCriteria;
import me.zhengjie.modules.dm.envmonitor.monitorobject.service.mapstruct.DmEnvMonitorObjectMapper;
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 lr
* @date 2022-05-19
**/
@Service
@RequiredArgsConstructor
public class DmEnvMonitorObjectServiceImpl implements DmEnvMonitorObjectService {

    private final DmEnvMonitorObjectRepository dmEnvMonitorObjectRepository;
    private final DmEnvMonitorObjectMapper dmEnvMonitorObjectMapper;

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

    @Override
    public List<DmEnvMonitorObjectDto> queryAll(DmEnvMonitorObjectQueryCriteria criteria){
        return dmEnvMonitorObjectMapper.toDto(dmEnvMonitorObjectRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmEnvMonitorObjectDto findById(Long id) {
        DmEnvMonitorObject dmEnvMonitorObject = dmEnvMonitorObjectRepository.findById(id).orElseGet(DmEnvMonitorObject::new);
        ValidationUtil.isNull(dmEnvMonitorObject.getId(),"DmEnvMonitorObject","id",id);
        return dmEnvMonitorObjectMapper.toDto(dmEnvMonitorObject);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmEnvMonitorObjectDto create(DmEnvMonitorObject resources) {
        return dmEnvMonitorObjectMapper.toDto(dmEnvMonitorObjectRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmEnvMonitorObjectDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmEnvMonitorObjectDto dmEnvMonitorObject : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("建筑名称", dmEnvMonitorObject.getConstructName());
            map.put("创建者", dmEnvMonitorObject.getCreateBy());
            map.put("更新者", dmEnvMonitorObject.getUpdateBy());
            map.put("创建时间", dmEnvMonitorObject.getCreateTime());
            map.put("更新时间", dmEnvMonitorObject.getUpdateTime());
            map.put("设备编号", dmEnvMonitorObject.getDeviceCode());
            map.put("对象名称", dmEnvMonitorObject.getObjectName());
            map.put("GIS坐标", dmEnvMonitorObject.getGis());
            map.put("所属楼层", dmEnvMonitorObject.getDmFloor().getFloorName());
            map.put("位置", dmEnvMonitorObject.getPosition());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}