/*
*  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 me.zhengjie.modules.dm.userInformation.service.impl;

import me.zhengjie.modules.dm.userInformation.domain.DmUserInformation;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.userInformation.repository.DmUserInformationRepository;
import me.zhengjie.modules.dm.userInformation.service.DmUserInformationService;
import me.zhengjie.modules.dm.userInformation.service.dto.DmUserInformationDto;
import me.zhengjie.modules.dm.userInformation.service.dto.DmUserInformationQueryCriteria;
import me.zhengjie.modules.dm.userInformation.service.mapstruct.DmUserInformationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
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://el-admin.vip
* @description 服务实现
* @author lr
* @date 2022-06-06
**/
@Service
@RequiredArgsConstructor
public class DmUserInformationServiceImpl implements DmUserInformationService {

    private final DmUserInformationRepository dmUserInformationRepository;
    private final DmUserInformationMapper dmUserInformationMapper;

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

    @Override
    public List<DmUserInformationDto> queryAll(DmUserInformationQueryCriteria criteria){
        return dmUserInformationMapper.toDto(dmUserInformationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmUserInformationDto findById(Long id) {
        DmUserInformation dmUserInformation = dmUserInformationRepository.findById(id).orElseGet(DmUserInformation::new);
        ValidationUtil.isNull(dmUserInformation.getId(),"DmUserInformation","id",id);
        return dmUserInformationMapper.toDto(dmUserInformation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmUserInformationDto create(DmUserInformation resources) {
        return dmUserInformationMapper.toDto(dmUserInformationRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmUserInformationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmUserInformationDto dmUserInformation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", dmUserInformation.getUserId());
            map.put("发布id", dmUserInformation.getInformationId());
            map.put("创建时间", dmUserInformation.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}