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

import me.zhengjie.modules.dm.rfidTableware.domain.DmRfidTableware;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.rfidTableware.repository.DmRfidTablewareRepository;
import me.zhengjie.modules.dm.rfidTableware.service.DmRfidTablewareService;
import me.zhengjie.modules.dm.rfidTableware.service.dto.DmRfidTablewareDto;
import me.zhengjie.modules.dm.rfidTableware.service.dto.DmRfidTablewareQueryCriteria;
import me.zhengjie.modules.dm.rfidTableware.service.mapstruct.DmRfidTablewareMapper;
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 sunmz
* @date 2021-09-15
**/
@Service
@RequiredArgsConstructor
public class DmRfidTablewareServiceImpl implements DmRfidTablewareService {

    private final DmRfidTablewareRepository dmRfidTablewareRepository;
    private final DmRfidTablewareMapper dmRfidTablewareMapper;

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

    @Override
    public List<DmRfidTablewareDto> queryAll(DmRfidTablewareQueryCriteria criteria){
        return dmRfidTablewareMapper.toDto(dmRfidTablewareRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmRfidTablewareDto findById(Long id) {
        DmRfidTableware dmRfidTableware = dmRfidTablewareRepository.findById(id).orElseGet(DmRfidTableware::new);
        ValidationUtil.isNull(dmRfidTableware.getId(),"DmRfidTableware","id",id);
        return dmRfidTablewareMapper.toDto(dmRfidTableware);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmRfidTablewareDto create(DmRfidTableware resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmRfidTablewareMapper.toDto(dmRfidTablewareRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmRfidTablewareDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmRfidTablewareDto dmRfidTableware : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("芯片编号(餐具内RFID编号)", dmRfidTableware.getCode());
            map.put("餐具名称(餐具名称)", dmRfidTableware.getName());
            map.put("所属餐厅(餐具所属餐厅)", dmRfidTableware.getRestaurant());
            map.put("创建者", dmRfidTableware.getCreateBy());
            map.put("更新者", dmRfidTableware.getUpdateBy());
            map.put("创建日期", dmRfidTableware.getCreateTime());
            map.put("更新时间", dmRfidTableware.getUpdateTime());
            map.put("使用日期", dmRfidTableware.getUserTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}