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

import me.zhengjie.modules.dm.serveEvaluate.domain.DmServeEvaluate;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.serveEvaluate.repository.DmServeEvaluateRepository;
import me.zhengjie.modules.dm.serveEvaluate.service.DmServeEvaluateService;
import me.zhengjie.modules.dm.serveEvaluate.service.dto.DmServeEvaluateDto;
import me.zhengjie.modules.dm.serveEvaluate.service.dto.DmServeEvaluateQueryCriteria;
import me.zhengjie.modules.dm.serveEvaluate.service.mapstruct.DmServeEvaluateMapper;
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 DmServeEvaluateServiceImpl implements DmServeEvaluateService {

    private final DmServeEvaluateRepository dmServeEvaluateRepository;
    private final DmServeEvaluateMapper dmServeEvaluateMapper;

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

    @Override
    public List<DmServeEvaluateDto> queryAll(DmServeEvaluateQueryCriteria criteria){
        return dmServeEvaluateMapper.toDto(dmServeEvaluateRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmServeEvaluateDto findById(Long id) {
        DmServeEvaluate dmServeEvaluate = dmServeEvaluateRepository.findById(id).orElseGet(DmServeEvaluate::new);
        ValidationUtil.isNull(dmServeEvaluate.getId(),"DmServeEvaluate","id",id);
        return dmServeEvaluateMapper.toDto(dmServeEvaluate);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmServeEvaluateDto create(DmServeEvaluate resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmServeEvaluateMapper.toDto(dmServeEvaluateRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmServeEvaluateDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmServeEvaluateDto dmServeEvaluate : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜品名称", dmServeEvaluate.getFoodName());
            map.put("评价详情(对菜品的评价内容)", dmServeEvaluate.getConcat());
            map.put("用户名称(评价的用户名称)", dmServeEvaluate.getRequestUser());
            map.put("点餐时间(用户点餐时间）", dmServeEvaluate.getOrderTime());
            map.put("评价时间(用户评价时间)", dmServeEvaluate.getEvaluateTime());
            map.put("创建者", dmServeEvaluate.getCreateBy());
            map.put("更新者", dmServeEvaluate.getUpdateBy());
            map.put("创建日期", dmServeEvaluate.getCreateTime());
            map.put("更新时间", dmServeEvaluate.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}