/*
*  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.BillType;
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.BillTypeRepository;
import com.youchain.basicdata.service.BillTypeService;
import com.youchain.basicdata.service.dto.BillTypeDto;
import com.youchain.basicdata.service.dto.BillTypeQueryCriteria;
import com.youchain.basicdata.service.mapstruct.BillTypeMapper;
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 houjianlan
* @date 2023-08-07
**/
@Service
@RequiredArgsConstructor
public class BillTypeServiceImpl implements BillTypeService {

    private final BillTypeRepository billTypeRepository;
    private final BillTypeMapper billTypeMapper;

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

    @Override
    public List<BillTypeDto> queryAll(BillTypeQueryCriteria criteria){
        return billTypeMapper.toDto(billTypeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public BillTypeDto findById(Long id) {
        BillType billType = billTypeRepository.findById(id).orElseGet(BillType::new);
        ValidationUtil.isNull(billType.getId(),"BillType","id",id);
        return billTypeMapper.toDto(billType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BillTypeDto create(BillType resources) {
        return billTypeMapper.toDto(billTypeRepository.save(resources));
    }

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

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

    @Override
    public void download(List<BillTypeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BillTypeDto billType : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("代码", billType.getCode());
            map.put("名称", billType.getName());
            map.put("仓库ID", billType.getDept());
            map.put("类型", billType.getType());
            map.put("来源名称", billType.getSourceName());
            map.put("来源序号", billType.getSourceId());
            map.put("创建人", billType.getCreateBy());
            map.put("修改人", billType.getUpdateBy());
            map.put("创建时间", billType.getCreateTime());
            map.put("修改时间", billType.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}