/*
*  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.businessdata.service.impl;

import com.youchain.businessdata.domain.XppPlan;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.businessdata.repository.XppPlanRepository;
import com.youchain.businessdata.service.XppPlanService;
import com.youchain.businessdata.service.dto.XppPlanDto;
import com.youchain.businessdata.service.dto.XppPlanQueryCriteria;
import com.youchain.businessdata.service.mapstruct.XppPlanMapper;
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 2026-02-11
**/
@Service
@RequiredArgsConstructor
public class XppPlanServiceImpl implements XppPlanService {

    private final XppPlanRepository xppPlanRepository;
    private final XppPlanMapper xppPlanMapper;

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

    @Override
    public List<XppPlanDto> queryAll(XppPlanQueryCriteria criteria){
        return xppPlanMapper.toDto(xppPlanRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public XppPlanDto findById(Long id) {
        XppPlan xppPlan = xppPlanRepository.findById(id).orElseGet(XppPlan::new);
        ValidationUtil.isNull(xppPlan.getId(),"XppPlan","id",id);
        return xppPlanMapper.toDto(xppPlan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public XppPlanDto create(XppPlan resources) {
        return xppPlanMapper.toDto(xppPlanRepository.save(resources));
    }

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

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

    @Override
    public void download(List<XppPlanDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list= ExcelDownUtils.CreateMap(all,"XppPlan");
        FileUtil.downloadExcel(list, response);
    }
}