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

import me.zhengjie.modules.dm.modbus.group.domain.DmModbusGroup;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.modbus.group.repository.DmModbusGroupRepository;
import me.zhengjie.modules.dm.modbus.group.service.DmModbusGroupService;
import me.zhengjie.modules.dm.modbus.group.service.dto.DmModbusGroupDto;
import me.zhengjie.modules.dm.modbus.group.service.dto.DmModbusGroupQueryCriteria;
import me.zhengjie.modules.dm.modbus.group.service.mapstruct.DmModbusGroupMapper;
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 wld
* @date 2022-05-21
**/
@Service
@RequiredArgsConstructor
public class DmModbusGroupServiceImpl implements DmModbusGroupService {

    private final DmModbusGroupRepository dmModbusGroupRepository;
    private final DmModbusGroupMapper dmModbusGroupMapper;

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

    @Override
    public List<DmModbusGroupDto> queryAll(DmModbusGroupQueryCriteria criteria){
        return dmModbusGroupMapper.toDto(dmModbusGroupRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmModbusGroupDto findById(Long groupId) {
        DmModbusGroup dmModbusGroup = dmModbusGroupRepository.findById(groupId).orElseGet(DmModbusGroup::new);
        ValidationUtil.isNull(dmModbusGroup.getGroupId(),"DmModbusGroup","groupId",groupId);
        return dmModbusGroupMapper.toDto(dmModbusGroup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmModbusGroupDto create(DmModbusGroup resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setGroupId(snowflake.nextId()); 
        return dmModbusGroupMapper.toDto(dmModbusGroupRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmModbusGroup resources) {
        DmModbusGroup dmModbusGroup = dmModbusGroupRepository.findById(resources.getGroupId()).orElseGet(DmModbusGroup::new);
        ValidationUtil.isNull( dmModbusGroup.getGroupId(),"DmModbusGroup","id",resources.getGroupId());
        dmModbusGroup.copy(resources);
        dmModbusGroupRepository.save(dmModbusGroup);
    }

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

    @Override
    public void download(List<DmModbusGroupDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmModbusGroupDto dmModbusGroup : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("组名称", dmModbusGroup.getGroupName());
            map.put("关联设备id", dmModbusGroup.getDeviceId());
            map.put("创建人", dmModbusGroup.getCreateBy());
            map.put("更新人", dmModbusGroup.getUpdateBy());
            map.put("创建时间", dmModbusGroup.getCreateTime());
            map.put("更新时间", dmModbusGroup.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}