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

import me.zhengjie.modules.dm.food.domain.DmFood;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.food.repository.DmFoodRepository;
import me.zhengjie.modules.dm.food.service.DmFoodService;
import me.zhengjie.modules.dm.food.service.dto.DmFoodDto;
import me.zhengjie.modules.dm.food.service.dto.DmFoodQueryCriteria;
import me.zhengjie.modules.dm.food.service.mapstruct.DmFoodMapper;
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 DmFoodServiceImpl implements DmFoodService {

    private final DmFoodRepository dmFoodRepository;
    private final DmFoodMapper dmFoodMapper;

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

    @Override
    public List<DmFoodDto> queryAll(DmFoodQueryCriteria criteria){
        return dmFoodMapper.toDto(dmFoodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmFoodDto findById(Long id) {
        DmFood dmFood = dmFoodRepository.findById(id).orElseGet(DmFood::new);
        ValidationUtil.isNull(dmFood.getId(),"DmFood","id",id);
        return dmFoodMapper.toDto(dmFood);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmFoodDto create(DmFood resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmFoodMapper.toDto(dmFoodRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmFoodDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmFoodDto dmFood : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编号", dmFood.getCode());
            map.put("名称", dmFood.getName());
            map.put("单价", dmFood.getPrice());
            map.put("图片", dmFood.getPicture());
            map.put("创建者", dmFood.getCreateBy());
            map.put("更新者", dmFood.getUpdatedBy());
            map.put("创建日期", dmFood.getCreateTime());
            map.put("更新时间", dmFood.getUpdateTime());
            map.put("描述", dmFood.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}