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

import me.zhengjie.modules.dm.printingQueue.domain.DmPrintingQueue;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.printingQueue.repository.DmPrintingQueueRepository;
import me.zhengjie.modules.dm.printingQueue.service.DmPrintingQueueService;
import me.zhengjie.modules.dm.printingQueue.service.dto.DmPrintingQueueDto;
import me.zhengjie.modules.dm.printingQueue.service.dto.DmPrintingQueueQueryCriteria;
import me.zhengjie.modules.dm.printingQueue.service.mapstruct.DmPrintingQueueMapper;
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-03-17
**/
@Service
@RequiredArgsConstructor
public class DmPrintingQueueServiceImpl implements DmPrintingQueueService {

    private final DmPrintingQueueRepository dmPrintingQueueRepository;
    private final DmPrintingQueueMapper dmPrintingQueueMapper;

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

    @Override
    public List<DmPrintingQueueDto> queryAll(DmPrintingQueueQueryCriteria criteria){
        return dmPrintingQueueMapper.toDto(dmPrintingQueueRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmPrintingQueueDto findById(Long id) {
        DmPrintingQueue dmPrintingQueue = dmPrintingQueueRepository.findById(id).orElseGet(DmPrintingQueue::new);
        ValidationUtil.isNull(dmPrintingQueue.getId(),"DmPrintingQueue","id",id);
        return dmPrintingQueueMapper.toDto(dmPrintingQueue);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmPrintingQueueDto create(DmPrintingQueue resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmPrintingQueueMapper.toDto(dmPrintingQueueRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmPrintingQueueDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmPrintingQueueDto dmPrintingQueue : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("文印申请主键", dmPrintingQueue.getPrintingRequest().getId());
            map.put("进入队列时间", dmPrintingQueue.getStartTime());
            map.put("状态", dmPrintingQueue.getStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<DmPrintingQueue> findUnprint(Long deviceId){
        return  dmPrintingQueueRepository.findUnprint(deviceId);
    }

}