/*
*  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.BigBom;
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.BigBomRepository;
import com.youchain.basicdata.service.BigBomService;
import com.youchain.basicdata.service.dto.BigBomDto;
import com.youchain.basicdata.service.dto.BigBomQueryCriteria;
import com.youchain.basicdata.service.mapstruct.BigBomMapper;
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 BigBomServiceImpl implements BigBomService {

    private final BigBomRepository bigBomRepository;
    private final BigBomMapper bigBomMapper;

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

    @Override
    public List<BigBomDto> queryAll(BigBomQueryCriteria criteria){
        return bigBomMapper.toDto(bigBomRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BigBomDto findById(Long id) {
        BigBom bigBom = bigBomRepository.findById(id).orElseGet(BigBom::new);
        ValidationUtil.isNull(bigBom.getId(),"BigBom","id",id);
        return bigBomMapper.toDto(bigBom);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BigBomDto create(BigBom resources) {
        return bigBomMapper.toDto(bigBomRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BigBomDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BigBomDto bigBom : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("bigitemID", bigBom.getBigItem());
            map.put("itemID", bigBom.getItem());
            map.put("数量", bigBom.getQuantity());
            map.put("仓库ID", bigBom.getDeptId());
            map.put("创建人", bigBom.getCreateBy());
            map.put("修改人", bigBom.getUpdateBy());
            map.put("创建时间", bigBom.getCreateTime());
            map.put("修改时间", bigBom.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}