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

import me.zhengjie.modules.dm.floor.domain.DmFloor;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.floor.repository.DmFloorRepository;
import me.zhengjie.modules.dm.floor.service.DmFloorService;
import me.zhengjie.modules.dm.floor.service.dto.DmFloorDto;
import me.zhengjie.modules.dm.floor.service.dto.DmFloorQueryCriteria;
import me.zhengjie.modules.dm.floor.service.mapstruct.DmFloorMapper;
import org.springframework.data.domain.Sort;
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 shiguangji
* @date 2021-03-11
**/
@Service
@RequiredArgsConstructor
public class DmFloorServiceImpl implements DmFloorService {

    private final DmFloorRepository dmFloorRepository;
    private final DmFloorMapper dmFloorMapper;

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

    @Override
    public List<DmFloorDto> queryAll(DmFloorQueryCriteria criteria){
        return dmFloorMapper.toDto(dmFloorRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmFloorDto findById(Long floorId) {
        DmFloor dmFloor = dmFloorRepository.findById(floorId).orElseGet(DmFloor::new);
        ValidationUtil.isNull(dmFloor.getFloorId(),"DmFloor","floorId",floorId);
        return dmFloorMapper.toDto(dmFloor);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmFloorDto create(DmFloor resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setFloorId(snowflake.nextId()); 
        return dmFloorMapper.toDto(dmFloorRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmFloor resources) {
        DmFloor dmFloor = dmFloorRepository.findById(resources.getFloorId()).orElseGet(DmFloor::new);
        ValidationUtil.isNull( dmFloor.getFloorId(),"DmFloor","id",resources.getFloorId());
        dmFloor.copy(resources);
        dmFloorRepository.save(dmFloor);
    }

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

    @Override
    public void download(List<DmFloorDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmFloorDto dmFloor : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("楼层名称", dmFloor.getFloorName());
            map.put("楼层负责人", dmFloor.getFloorPic());
            map.put("联系方式", dmFloor.getContacts());
            map.put("排序", dmFloor.getSortIndex());
            map.put("创建者", dmFloor.getCreateBy());
            map.put("更新者", dmFloor.getUpdateBy());
            map.put("创建日期", dmFloor.getCreateTime());
            map.put("更新时间", dmFloor.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}