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

import me.zhengjie.modules.dm.order.domain.DmOrderItem;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.order.repository.DmOrderItemRepository;
import me.zhengjie.modules.dm.order.service.DmOrderItemService;
import me.zhengjie.modules.dm.order.service.dto.DmOrderItemDto;
import me.zhengjie.modules.dm.order.service.dto.DmOrderItemQueryCriteria;
import me.zhengjie.modules.dm.order.service.mapstruct.DmOrderItemMapper;
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 DmOrderItemServiceImpl implements DmOrderItemService {

    private final DmOrderItemRepository dmOrderItemRepository;
    private final DmOrderItemMapper dmOrderItemMapper;

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

    @Override
    public Map<String,Object> queryAll(DmOrderItemQueryCriteria criteria, Pageable pageable){
        Page<DmOrderItem> page = dmOrderItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        List<DmOrderItem> list = page.getContent();
        List<DmOrderItem> list1 = new ArrayList<DmOrderItem>();
        Integer sum = 0;
        for(DmOrderItem dmOrderItem: list) {
            list1.add(dmOrderItem);
            Double reseve = dmOrderItem.getAmt();
            sum += reseve.intValue();
        }
        DmOrderItem dmOrderItem = new DmOrderItem();
        dmOrderItem.setName("合计");
        dmOrderItem.setMoney(sum.toString());
        list1.add(dmOrderItem);
        return PageUtil.toPage(list1,page.getTotalElements());
//        return PageUtil.toPage(page.map(dmOrderItemMapper::toDto));
    }

    @Override
    public List<DmOrderItemDto> queryAll(DmOrderItemQueryCriteria criteria){
        return dmOrderItemMapper.toDto(dmOrderItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmOrderItemDto findById(Long id) {
        DmOrderItem dmOrderItem = dmOrderItemRepository.findById(id).orElseGet(DmOrderItem::new);
        ValidationUtil.isNull(dmOrderItem.getId(),"DmOrderItem","id",id);
        return dmOrderItemMapper.toDto(dmOrderItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmOrderItemDto create(DmOrderItem resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId());
        return dmOrderItemMapper.toDto(dmOrderItemRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmOrderItemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmOrderItemDto dmOrderItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("编号", dmOrderItem.getBh());
            map.put("商品编号", dmOrderItem.getCode());
            map.put("商品名称", dmOrderItem.getName());
            map.put("单价", dmOrderItem.getPrice());
            map.put("数量", dmOrderItem.getNum());
            map.put("金额小计", dmOrderItem.getMoney());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}