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

import me.zhengjie.modules.dm.foodPj.domain.DmFoodPj;
import me.zhengjie.modules.dm.order.domain.DmOrderItem;
import me.zhengjie.modules.dm.order.domain.DmOrderRecord;
import me.zhengjie.modules.dm.order.repository.DmOrderItemRepository;
import me.zhengjie.modules.dm.order.repository.DmOrderRecordRepository;
import me.zhengjie.modules.dm.order.service.DmOrderRecordService;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.foodPj.repository.DmFoodPjRepository;
import me.zhengjie.modules.dm.foodPj.service.DmFoodPjService;
import me.zhengjie.modules.dm.foodPj.service.dto.DmFoodPjDto;
import me.zhengjie.modules.dm.foodPj.service.dto.DmFoodPjQueryCriteria;
import me.zhengjie.modules.dm.foodPj.service.mapstruct.DmFoodPjMapper;
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.sql.Timestamp;
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 DmFoodPjServiceImpl implements DmFoodPjService {

    private final DmFoodPjRepository dmFoodPjRepository;
    private final DmFoodPjMapper dmFoodPjMapper;
    private final DmOrderItemRepository dmOrderItemRepository;

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

    @Override
    public List<DmFoodPjDto> queryAll(DmFoodPjQueryCriteria criteria){
        return dmFoodPjMapper.toDto(dmFoodPjRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmFoodPjDto findById(Long id) {
        DmFoodPj dmFoodPj = dmFoodPjRepository.findById(id).orElseGet(DmFoodPj::new);
        ValidationUtil.isNull(dmFoodPj.getId(),"DmFoodPj","id",id);
        return dmFoodPjMapper.toDto(dmFoodPj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmFoodPjDto create(DmFoodPj resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return dmFoodPjMapper.toDto(dmFoodPjRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmFoodPjDto createApp(DmFoodPj resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        DmOrderItem dmOrderItem = dmOrderItemRepository.findById(resources.getOid()).orElseGet(DmOrderItem::new);
        ValidationUtil.isNull( resources.getOid(),"DmOrderItem","id",resources.getOid());
//        dmOrderItem.copy(resources);
        dmOrderItem.setPjType("1");
        dmOrderItemRepository.save(dmOrderItem);

        return dmFoodPjMapper.toDto(dmFoodPjRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmFoodPjDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmFoodPjDto dmFoodPj : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("菜品名称", dmFoodPj.getFoodName());
            map.put("菜品编号", dmFoodPj.getFoodCode());
            map.put("菜品分类", dmFoodPj.getFoodType());
            map.put("用户工号", dmFoodPj.getUserCode());
            map.put("用户姓名", dmFoodPj.getUserName());
            map.put("下单时间", dmFoodPj.getOrderTime());
            map.put("订单编号", dmFoodPj.getOrderBh());
            map.put("评价时间", dmFoodPj.getPjTime());
            map.put("评价星级", dmFoodPj.getPjStar());
            map.put("评价标签", dmFoodPj.getPjTag());
            map.put("评价详情", dmFoodPj.getPjDesc());
            map.put("上传图片", dmFoodPj.getPjPic());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String,Object>> findByFoodCode(String code) {
        return dmFoodPjRepository.findByFoodCode(code);
    }
}