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

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
//import com.rabbitmq.client.QueueingConsumer;
import me.zhengjie.modules.dm.daypc.domain.DmDayPc;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.daypc.repository.DmDayPcRepository;
import me.zhengjie.modules.dm.daypc.service.DmDayPcService;
import me.zhengjie.modules.dm.daypc.service.dto.DmDayPcDto;
import me.zhengjie.modules.dm.daypc.service.dto.DmDayPcQueryCriteria;
import me.zhengjie.modules.dm.daypc.service.mapstruct.DmDayPcMapper;
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 java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author sunmz
* @date 2021-09-22
**/
@Service
@RequiredArgsConstructor
public class DmDayPcServiceImpl implements DmDayPcService {
    private final DmDayPcRepository dmDayPcRepository;
    private final DmDayPcMapper dmDayPcMapper;

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

    @Override
    public List<DmDayPcDto> queryAll(DmDayPcQueryCriteria criteria){
        return dmDayPcMapper.toDto(dmDayPcRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmDayPcDto findById(Long id) {
        DmDayPc dmDayPc = dmDayPcRepository.findById(id).orElseGet(DmDayPc::new);
        ValidationUtil.isNull(dmDayPc.getId(),"DmDayPc","id",id);
        return dmDayPcMapper.toDto(dmDayPc);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmDayPcDto create(DmDayPc resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return dmDayPcMapper.toDto(dmDayPcRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmDayPcDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmDayPcDto dmDayPc : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜品id", dmDayPc.getPid());//.getPid()
//            map.put("餐盘id", dmDayPc.getRfidTableware().getId());//.getCpid()
            map.put("菜品名称", dmDayPc.getName());
            map.put("价格", dmDayPc.getPrice());
            map.put("餐别", dmDayPc.getTimes());
            map.put("菜品分类id", dmDayPc.getCate());
            map.put("菜品分类名称", dmDayPc.getCateName());
            map.put("档口id", dmDayPc.getWindow());
            map.put("档口名称", dmDayPc.getWindowName());
            map.put("菜品状态", dmDayPc.getPstate());
            map.put("流水号", dmDayPc.getSeq());
            map.put("日期", dmDayPc.getDate());
            map.put("餐厅名称", dmDayPc.getShopname());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String,Object>> foodRepository() {
        return dmDayPcRepository.foodRepository();
    }
}