/*
*  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.AutomaticPlanning;
import com.youchain.utils.*;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.AutomaticPlanningRepository;
import com.youchain.basicdata.service.AutomaticPlanningService;
import com.youchain.basicdata.service.dto.AutomaticPlanningDto;
import com.youchain.basicdata.service.dto.AutomaticPlanningQueryCriteria;
import com.youchain.basicdata.service.mapstruct.AutomaticPlanningMapper;
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 JiangKun
* @date 2024-01-05
**/
@Service
@RequiredArgsConstructor
public class AutomaticPlanningServiceImpl implements AutomaticPlanningService {

    private final AutomaticPlanningRepository automaticPlanningRepository;
    private final AutomaticPlanningMapper automaticPlanningMapper;


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

    @Override
    public List<AutomaticPlanningDto> queryAll(AutomaticPlanningQueryCriteria criteria){
        return automaticPlanningMapper.toDto(automaticPlanningRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AutomaticPlanningDto findById(Long id) {
        AutomaticPlanning automaticPlanning = automaticPlanningRepository.findById(id).orElseGet(AutomaticPlanning::new);
        ValidationUtil.isNull(automaticPlanning.getId(),"AutomaticPlanning","id",id);
        return automaticPlanningMapper.toDto(automaticPlanning);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AutomaticPlanningDto create(AutomaticPlanning resources) {
        return automaticPlanningMapper.toDto(automaticPlanningRepository.save(resources));
    }

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

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

    @Override
    public void download(List<AutomaticPlanningDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>>  list= ExcelDownUtils.CreateMap(all,"AutomaticPlanning");
//        List<Map<String, Object>> list = new ArrayList<>();
//        for (AutomaticPlanningDto automaticPlanning : all) {
//            Map<String,Object> map = new LinkedHashMap<>();
//            map.put("收货仓库", automaticPlanning.getShArea());
//            map.put("辅线数量", automaticPlanning.getFxQuantity());
//            map.put("计划数量", automaticPlanning.getJhQuantity());
//            map.put("耗用数量", automaticPlanning.getHyQuantity());
//            map.put("仓库ID", automaticPlanning.getDeptId());
//            map.put("状态：1启用、0禁用", automaticPlanning.getEnabled());
//            map.put("备注", automaticPlanning.getDescription());
//            map.put("创建人", automaticPlanning.getCreateBy());
//            map.put("更新人", automaticPlanning.getUpdateBy());
//            map.put("创建时间", automaticPlanning.getCreateTime());
//            map.put("修改日期", automaticPlanning.getUpdateTime());
//            list.add(map);
//        }
        FileUtil.downloadExcel(list, response);
    }

}