/*
*  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.AutomaticPlanDetail;
import com.youchain.basicdata.domain.AutomaticPlanning;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.AutomaticPlanDetailRepository;
import com.youchain.basicdata.service.AutomaticPlanDetailService;
import com.youchain.basicdata.service.dto.AutomaticPlanDetailDto;
import com.youchain.basicdata.service.dto.AutomaticPlanDetailQueryCriteria;
import com.youchain.basicdata.service.mapstruct.AutomaticPlanDetailMapper;
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 2024-04-07
**/
@Service
@RequiredArgsConstructor
public class AutomaticPlanDetailServiceImpl implements AutomaticPlanDetailService {

    private final AutomaticPlanDetailRepository automaticPlanDetailRepository;
    private final AutomaticPlanDetailMapper automaticPlanDetailMapper;

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

    @Override
    public List<AutomaticPlanDetailDto> queryAll(AutomaticPlanDetailQueryCriteria criteria){
        return automaticPlanDetailMapper.toDto(automaticPlanDetailRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AutomaticPlanDetailDto findById(Long id) {
        AutomaticPlanDetail automaticPlanDetail = automaticPlanDetailRepository.findById(id).orElseGet(AutomaticPlanDetail::new);
        ValidationUtil.isNull(automaticPlanDetail.getId(),"AutomaticPlanDetail","id",id);
        return automaticPlanDetailMapper.toDto(automaticPlanDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutomaticPlanDetailDto create(AutomaticPlanDetail resources) {
        return automaticPlanDetailMapper.toDto(automaticPlanDetailRepository.save(resources));
    }

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

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

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

    @Override
    public AutomaticPlanDetail storeAutomaticPlanDetail(AutomaticPlanning automaticPlanning) {
        AutomaticPlanDetail detail=new AutomaticPlanDetail();
        detail.setAutomaticPlanning(automaticPlanning);
        detail.setDept(automaticPlanning.getDept());
        detail.setShArea(automaticPlanning.getShArea());
        detail.setBatchNum(automaticPlanning.getBatchNum());
        detail.setDescription(automaticPlanning.getDescription());
        detail.setSxQuantity(automaticPlanning.getSxQuantity());
        detail.setFxQuantity(automaticPlanning.getFxQuantity());
        detail.setJhQuantity(automaticPlanning.getJhQuantity());
        detail.setStartNum(automaticPlanning.getStartNum());
        detail.setEndNum(automaticPlanning.getEndNum());
        detail.setJhDate(automaticPlanning.getJhDate());
        detail.setShHour(automaticPlanning.getShHour());
        this.create(detail);
        return detail;
    }
}