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

import me.zhengjie.modules.dm.device.domain.DmDevice;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.device.repository.DmDeviceRepository;
import me.zhengjie.modules.dm.device.service.DmDeviceService;
import me.zhengjie.modules.dm.device.service.dto.DmDeviceDto;
import me.zhengjie.modules.dm.device.service.dto.DmDeviceQueryCriteria;
import me.zhengjie.modules.dm.device.service.mapstruct.DmDeviceMapper;
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 xwx
* @date 2021-04-20
**/
@Service
@RequiredArgsConstructor
public class DmDeviceServiceImpl implements DmDeviceService {

    private final DmDeviceRepository dmDeviceRepository;
    private final DmDeviceMapper dmDeviceMapper;

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

    @Override
    public List<DmDeviceDto> queryAll(DmDeviceQueryCriteria criteria){
        return dmDeviceMapper.toDto(dmDeviceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmDeviceDto findById(Long id) {
        DmDevice dmDevice = dmDeviceRepository.findById(id).orElseGet(DmDevice::new);
        ValidationUtil.isNull(dmDevice.getId(),"DmDevice","id",id);
        return dmDeviceMapper.toDto(dmDevice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmDeviceDto create(DmDevice resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmDeviceMapper.toDto(dmDeviceRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmDevice resources) {
        DmDevice dmDevice = dmDeviceRepository.findById(resources.getId()).orElseGet(DmDevice::new);
        ValidationUtil.isNull( dmDevice.getId(),"DmDevice","id",resources.getId());
        dmDevice.copy(resources);
        dmDeviceRepository.save(dmDevice);
    }

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

    @Override
    public void download(List<DmDeviceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmDeviceDto dmDevice : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("设备编号", dmDevice.getCode());
            map.put("设备名称", dmDevice.getName());
            map.put("设备安装位置", dmDevice.getBuildAddress());
            map.put("安装时间", dmDevice.getBuildDate());
            if (dmDevice.getState()==0){
                map.put("设备状态", "启用");
            }else if (dmDevice.getState()==1){
                map.put("设备状态", "停用");
            }else if (dmDevice.getState()==2){
                map.put("设备状态", "待维修");
            }
            map.put("创建者", dmDevice.getCreateBy());
            map.put("更新者", dmDevice.getUpdateBy());
            map.put("创建日期", dmDevice.getCreateTime());
            map.put("更新时间", dmDevice.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int checkCode(String code,Long id){ return dmDeviceRepository.checkCode(code,id); }
}