/*
*  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.TableConfig;
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.TableConfigRepository;
import com.youchain.basicdata.service.TableConfigService;
import com.youchain.basicdata.service.dto.TableConfigDto;
import com.youchain.basicdata.service.dto.TableConfigQueryCriteria;
import com.youchain.basicdata.service.mapstruct.TableConfigMapper;
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 LiuXue
* @date 2023-08-28
**/
@Service
@RequiredArgsConstructor
public class TableConfigServiceImpl implements TableConfigService {

    private final TableConfigRepository tableConfigRepository;
    private final TableConfigMapper tableConfigMapper;

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

    @Override
    public List<TableConfigDto> queryAll(TableConfigQueryCriteria criteria){
        return tableConfigMapper.toDto(tableConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public TableConfigDto findById(Integer id) {
        TableConfig tableConfig = tableConfigRepository.findById(id).orElseGet(TableConfig::new);
        ValidationUtil.isNull(tableConfig.getId(),"TableConfig","id",id);
        return tableConfigMapper.toDto(tableConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TableConfigDto create(TableConfig resources) {
        TableConfigQueryCriteria criteria =new TableConfigQueryCriteria();
        if(resources.getTableType().length()<=0){
            return null;
        }
        criteria.setTableType(resources.getTableType());
        criteria.setCreateBy(resources.getCreateBy());
        List<TableConfig> list=tableConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        if(list.size()>0){
            TableConfig tableConfig=list.get(0);
            ValidationUtil.isNull( tableConfig.getId(),"TableConfig","id",resources.getId());
            tableConfig.copy(resources);
            tableConfigRepository.save(tableConfig);
            return tableConfigMapper.toDto(tableConfig);
        }else{
            return tableConfigMapper.toDto(tableConfigRepository.save(resources));
        }


    }

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

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

    @Override
    public void download(List<TableConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TableConfigDto tableConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" tableType",  tableConfig.getTableType());
            map.put(" value",  tableConfig.getValue());
            map.put(" createBy",  tableConfig.getCreateBy());
            map.put(" updateBy",  tableConfig.getUpdateBy());
            map.put(" createTime",  tableConfig.getCreateTime());
            map.put(" updateTime",  tableConfig.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}