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

import com.youchain.user.domain.ElTableConfig;
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.user.repository.ElTableConfigRepository;
import com.youchain.user.service.ElTableConfigService;
import com.youchain.user.service.dto.ElTableConfigDto;
import com.youchain.user.service.dto.ElTableConfigQueryCriteria;
import com.youchain.user.service.mapstruct.ElTableConfigMapper;
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 DengJiangKun
* @date 2024-01-25
**/
@Service
@RequiredArgsConstructor
public class ElTableConfigServiceImpl implements ElTableConfigService {

    private final ElTableConfigRepository elTableConfigRepository;
    private final ElTableConfigMapper elTableConfigMapper;

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

    @Override
    public List<ElTableConfigDto> queryAll(ElTableConfigQueryCriteria criteria){
        return elTableConfigMapper.toDto(elTableConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ElTableConfigDto findById(Long id) {
        ElTableConfig elTableConfig = elTableConfigRepository.findById(id).orElseGet(ElTableConfig::new);
        ValidationUtil.isNull(elTableConfig.getId(),"ElTableConfig","id",id);
        return elTableConfigMapper.toDto(elTableConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ElTableConfigDto create(ElTableConfig resources) {
        return elTableConfigMapper.toDto(elTableConfigRepository.save(resources));
    }

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

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

    @Override
    public void download(List<ElTableConfigDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ElTableConfigDto elTableConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("页面的url", elTableConfig.getVueUrl());
            map.put("tag的样式", elTableConfig.getTagStyle());
            map.put("文字的样式", elTableConfig.getVueStyle());
            map.put("页面存储路径", elTableConfig.getVuePath());
            map.put("排序", elTableConfig.getSort());
            map.put("没有数据时显示的内容", elTableConfig.getDefaultContent());
            map.put("字段名", elTableConfig.getPropName());
            map.put("字段展示名", elTableConfig.getLabelName());
            map.put("字段的存储类型", elTableConfig.getFieldType());
            map.put("ioc图片", elTableConfig.getIIcon());
            map.put("ioc图片是否展示", elTableConfig.getTfIcon());
            map.put("内容过度使用这个开关", elTableConfig.getTfShowOverflowTooltip());
            map.put("排序开关|自定义自己加", elTableConfig.getTfSortable());
            map.put("tag开关", elTableConfig.getTfElTag());
            map.put("tag属性", elTableConfig.getTagType());
            map.put("用户id", elTableConfig.getUserId());
            map.put("是否展示", elTableConfig.getTfShow());
            map.put("备注", elTableConfig.getRemarks());
            map.put("创建者", elTableConfig.getCreateBy());
            map.put("更新者", elTableConfig.getUpdateBy());
            map.put("创建日期", elTableConfig.getCreateTime());
            map.put("更新时间", elTableConfig.getUpdateTime());
            map.put("宽", elTableConfig.getWidth());
            map.put("页面号", elTableConfig.getPageNo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}