/*
*  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 shuai.huang
* @date 2021-03-30
**/
@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("GIS坐标", dmEnvMonitorObject.getGisCoordinate());
            map.put("建筑结构类型", dmEnvMonitorObject.getConstructStructureType());
            map.put("使用期限", dmEnvMonitorObject.getConstructPeriod());
            map.put("建筑年代", dmEnvMonitorObject.getConstructTime());
            map.put("建筑类别", dmEnvMonitorObject.getConstructType());
            map.put("建筑层数", dmEnvMonitorObject.getConstructStorey());
            map.put("建筑高度", dmEnvMonitorObject.getConstructHeight());
            map.put("基底面积", dmEnvMonitorObject.getBaseArea());
            map.put("总建筑面积", dmEnvMonitorObject.getConstructArea());
            map.put("主要用途", dmEnvMonitorObject.getRemark());
            map.put("创建者", dmEnvMonitorObject.getCreateBy());
            map.put("更新者", dmEnvMonitorObject.getUpdateBy());
            map.put("创建时间", dmEnvMonitorObject.getCreateTime());
            map.put("更新时间", dmEnvMonitorObject.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    public List<DmEnvMonitorObject> findMonitorList() {
        return dmEnvMonitorObjectRepository.findMonitorList();
    }
}