/*
 *  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.PackageCheck;
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.PackageCheckRepository;
import com.youchain.basicdata.service.PackageCheckService;
import com.youchain.basicdata.service.dto.PackageCheckDto;
import com.youchain.basicdata.service.dto.PackageCheckQueryCriteria;
import com.youchain.basicdata.service.mapstruct.PackageCheckMapper;
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;

/**
 * @author huojin
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-06-13
 **/
@Service
@RequiredArgsConstructor
public class PackageCheckServiceImpl implements PackageCheckService {

    private final PackageCheckRepository packageCheckRepository;
    private final PackageCheckMapper packageCheckMapper;

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

    @Override
    public List<PackageCheckDto> queryAll(PackageCheckQueryCriteria criteria) {
        return packageCheckMapper.toDto(packageCheckRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public PackageCheckDto findById(Long id) {
        PackageCheck packageCheck = packageCheckRepository.findById(id).orElseGet(PackageCheck::new);
        ValidationUtil.isNull(packageCheck.getId(), "PackageCheck", "id", id);
        return packageCheckMapper.toDto(packageCheck);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PackageCheckDto create(PackageCheck resources) {

        return packageCheckMapper.toDto(packageCheckRepository.save(resources));
    }

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

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

    @Override
    public void download(List<PackageCheckDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PackageCheckDto packageCheck : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("成套代码", packageCheck.getCode());
            map.put("物料", packageCheck.getItem().getCode());
            map.put("套数", packageCheck.getQuantity());
            map.put("创建人", packageCheck.getCreateBy());
            map.put("修改人", packageCheck.getUpdateBy());
            map.put("创建时间", packageCheck.getCreateTime());
            map.put("修改时间", packageCheck.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
