/*
*  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.BigItem;
import com.youchain.utils.FileUtil;
import com.youchain.utils.PageUtil;
import com.youchain.utils.QueryHelp;
import com.youchain.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.BigItemRepository;
import com.youchain.basicdata.service.BigItemService;
import com.youchain.basicdata.service.dto.BigItemDto;
import com.youchain.basicdata.service.dto.BigItemQueryCriteria;
import com.youchain.basicdata.service.mapstruct.BigItemMapper;
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 shenyinan
* @date 2024-02-21
**/
@Service
@RequiredArgsConstructor
public class BigItemServiceImpl implements BigItemService {

    private final BigItemRepository bigItemRepository;
    private final BigItemMapper bigItemMapper;

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

    @Override
    public List<BigItemDto> queryAll(BigItemQueryCriteria criteria){
        return bigItemMapper.toDto(bigItemRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BigItemDto findById(Long id) {
        BigItem bigItem = bigItemRepository.findById(id).orElseGet(BigItem::new);
        ValidationUtil.isNull(bigItem.getId(),"BigItem","id",id);
        return bigItemMapper.toDto(bigItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BigItemDto create(BigItem resources) {
        return bigItemMapper.toDto(bigItemRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BigItemDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BigItemDto bigItem : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("代码", bigItem.getCode());
            map.put("名称", bigItem.getName());
            map.put("类型", bigItem.getType());
            map.put("备注", bigItem.getRemark());
            map.put("仓库ID", bigItem.getDeptId());
            map.put("创建人", bigItem.getCreateBy());
            map.put("修改人", bigItem.getUpdateBy());
            map.put("创建时间", bigItem.getCreateTime());
            map.put("修改时间", bigItem.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}