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

import me.zhengjie.modules.dm.modbus.drive.domain.DmModbusDrive;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.modbus.drive.repository.DmModbusDriveRepository;
import me.zhengjie.modules.dm.modbus.drive.service.DmModbusDriveService;
import me.zhengjie.modules.dm.modbus.drive.service.dto.DmModbusDriveDto;
import me.zhengjie.modules.dm.modbus.drive.service.dto.DmModbusDriveQueryCriteria;
import me.zhengjie.modules.dm.modbus.drive.service.mapstruct.DmModbusDriveMapper;
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-20
**/
@Service
@RequiredArgsConstructor
public class DmModbusDriveServiceImpl implements DmModbusDriveService {

    private final DmModbusDriveRepository dmModbusDriveRepository;
    private final DmModbusDriveMapper dmModbusDriveMapper;

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

    @Override
    public List<DmModbusDriveDto> queryAll(DmModbusDriveQueryCriteria criteria){
        return dmModbusDriveMapper.toDto(dmModbusDriveRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmModbusDriveDto findById(Long driveId) {
        DmModbusDrive dmModbusDrive = dmModbusDriveRepository.findById(driveId).orElseGet(DmModbusDrive::new);
        ValidationUtil.isNull(dmModbusDrive.getDriveId(),"DmModbusDrive","driveId",driveId);
        return dmModbusDriveMapper.toDto(dmModbusDrive);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmModbusDriveDto create(DmModbusDrive resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setDriveId(snowflake.nextId()); 
        return dmModbusDriveMapper.toDto(dmModbusDriveRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmModbusDrive resources) {
        DmModbusDrive dmModbusDrive = dmModbusDriveRepository.findById(resources.getDriveId()).orElseGet(DmModbusDrive::new);
        ValidationUtil.isNull( dmModbusDrive.getDriveId(),"DmModbusDrive","id",resources.getDriveId());
        dmModbusDrive.copy(resources);
        dmModbusDriveRepository.save(dmModbusDrive);
    }

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

    @Override
    public void download(List<DmModbusDriveDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmModbusDriveDto dmModbusDrive : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("驱动名称", dmModbusDrive.getDriveName());
            map.put("名称", dmModbusDrive.getName());
            map.put("轮询时间(毫秒)", dmModbusDrive.getPollTime());
            map.put("创建人", dmModbusDrive.getCreateBy());
            map.put("更新人", dmModbusDrive.getUpdateBy());
            map.put("创建时间", dmModbusDrive.getCreateTime());
            map.put("更新时间", dmModbusDrive.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}