/*
*  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 com.youchain.basicdata.service.impl;

import com.youchain.basicdata.domain.ItemRelation;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.ItemRelationRepository;
import com.youchain.basicdata.service.ItemRelationService;
import com.youchain.basicdata.service.dto.ItemRelationDto;
import com.youchain.basicdata.service.dto.ItemRelationQueryCriteria;
import com.youchain.basicdata.service.mapstruct.ItemRelationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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://eladmin.vip
* @description 服务实现
* @author baobinglin
* @date 2024-01-04
**/
@Service
@RequiredArgsConstructor
public class ItemRelationServiceImpl implements ItemRelationService {

    private final ItemRelationRepository itemRelationRepository;
    private final ItemRelationMapper itemRelationMapper;

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

    @Override
    public List<ItemRelationDto> queryAll(ItemRelationQueryCriteria criteria){
        return itemRelationMapper.toDto(itemRelationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ItemRelationDto findById(Long id) {
        ItemRelation itemRelation = itemRelationRepository.findById(id).orElseGet(ItemRelation::new);
        ValidationUtil.isNull(itemRelation.getId(),"ItemRelation","id",id);
        return itemRelationMapper.toDto(itemRelation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemRelationDto create(ItemRelation resources) {
        return itemRelationMapper.toDto(itemRelationRepository.save(resources));
    }

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

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

    @Override
    public void download(List<ItemRelationDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list= ExcelDownUtils.CreateMap(all,"ItemRelation");
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (ItemRelationDto itemRelation : all) {
//            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("供应商", itemRelation.getBeSupplier());
//            map.put("key", itemRelation.getBarCode());
//            map.put("品番", itemRelation.getItem());
//            map.put("纳入场所", itemRelation.getCode());
//            map.put("门", itemRelation.getDoor());
//            map.put("箱种", itemRelation.getType());
//            map.put("收容数", itemRelation.getQuantity());
//            map.put("仓库ID", itemRelation.getDeptId());
//            map.put("描述", itemRelation.getDescription());
//            map.put("状态：1启用、0禁用", itemRelation.getEnabled());
//            map.put("创建人", itemRelation.getCreateBy());
//            map.put("修改人", itemRelation.getUpdateBy());
//            map.put("创建时间", itemRelation.getCreateTime());
//            map.put("修改时间", itemRelation.getUpdateTime());
//            list.add(map);
//        }
        FileUtil.downloadExcel(list, response);
    }
}