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

import me.zhengjie.modules.dm.modbus.channel.domain.DmModbusChannel;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.modbus.channel.repository.DmModbusChannelRepository;
import me.zhengjie.modules.dm.modbus.channel.service.DmModbusChannelService;
import me.zhengjie.modules.dm.modbus.channel.service.dto.DmModbusChannelDto;
import me.zhengjie.modules.dm.modbus.channel.service.dto.DmModbusChannelQueryCriteria;
import me.zhengjie.modules.dm.modbus.channel.service.mapstruct.DmModbusChannelMapper;
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 wanglongda
* @date 2022-05-20
**/
@Service
@RequiredArgsConstructor
public class DmModbusChannelServiceImpl implements DmModbusChannelService {

    private final DmModbusChannelRepository dmModbusChannelRepository;
    private final DmModbusChannelMapper dmModbusChannelMapper;

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

    @Override
    public List<DmModbusChannelDto> queryAll(DmModbusChannelQueryCriteria criteria){
        return dmModbusChannelMapper.toDto(dmModbusChannelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmModbusChannelDto findById(Long channelId) {
        DmModbusChannel dmModbusChannel = dmModbusChannelRepository.findById(channelId).orElseGet(DmModbusChannel::new);
        ValidationUtil.isNull(dmModbusChannel.getChannelId(),"DmModbusChannel","channelId",channelId);
        return dmModbusChannelMapper.toDto(dmModbusChannel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmModbusChannelDto create(DmModbusChannel resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setChannelId(snowflake.nextId()); 
        return dmModbusChannelMapper.toDto(dmModbusChannelRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmModbusChannel resources) {
        DmModbusChannel dmModbusChannel = dmModbusChannelRepository.findById(resources.getChannelId()).orElseGet(DmModbusChannel::new);
        ValidationUtil.isNull( dmModbusChannel.getChannelId(),"DmModbusChannel","id",resources.getChannelId());
        dmModbusChannel.copy(resources);
        dmModbusChannelRepository.save(dmModbusChannel);
    }

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

    @Override
    public void download(List<DmModbusChannelDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmModbusChannelDto dmModbusChannel : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("通道名称", dmModbusChannel.getChannelName());
            map.put("串口参数", dmModbusChannel.getSerialPort());
            map.put("波特率", dmModbusChannel.getBaudRate());
            map.put("数据位", dmModbusChannel.getDataBits());
            map.put("停止位", dmModbusChannel.getStopBits());
            map.put("校验位", dmModbusChannel.getVerifyBits());
            map.put("流控制", dmModbusChannel.getStreamControl());
            map.put("超时时间", dmModbusChannel.getTimeout());
            map.put("关联驱动id", dmModbusChannel.getDriveId());
            map.put("创建人", dmModbusChannel.getCreateBy());
            map.put("更新人", dmModbusChannel.getUpdateBy());
            map.put("创建时间", dmModbusChannel.getCreateTime());
            map.put("更新时间", dmModbusChannel.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}