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

import me.zhengjie.modules.dm.announce.domain.DmAnnounce;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.announce.repository.DmAnnounceRepository;
import me.zhengjie.modules.dm.announce.service.DmAnnounceService;
import me.zhengjie.modules.dm.announce.service.dto.DmAnnounceDto;
import me.zhengjie.modules.dm.announce.service.dto.DmAnnounceQueryCriteria;
import me.zhengjie.modules.dm.announce.service.mapstruct.DmAnnounceMapper;
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 Li Rui
* @date 2021-11-30
**/
@Service
@RequiredArgsConstructor
public class DmAnnounceServiceImpl implements DmAnnounceService {

    private final DmAnnounceRepository dmAnnounceRepository;
    private final DmAnnounceMapper dmAnnounceMapper;

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

    @Override
    public List<DmAnnounceDto> queryAll(DmAnnounceQueryCriteria criteria){
        return dmAnnounceMapper.toDto(dmAnnounceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmAnnounceDto findById(Integer id) {
        DmAnnounce dmAnnounce = dmAnnounceRepository.findById(id).orElseGet(DmAnnounce::new);
        ValidationUtil.isNull(dmAnnounce.getId(),"DmAnnounce","id",id);
        return dmAnnounceMapper.toDto(dmAnnounce);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmAnnounceDto create(DmAnnounce resources) {
        return dmAnnounceMapper.toDto(dmAnnounceRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmAnnounceDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmAnnounceDto dmAnnounce : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("公告标题", dmAnnounce.getAnnounceTitle());
            map.put("公告内容", dmAnnounce.getAnnounceContent());
            map.put("发布方式(0.立刻发布 1.定时发布)", dmAnnounce.getSendType());
            map.put("定时发布公告时间", dmAnnounce.getSendTime());
            map.put("是否取消定时发布(0.否 1.是)", dmAnnounce.getIsCancel());
            map.put("发布状态(0.未发布 1.已发布)", dmAnnounce.getSendState());
            map.put("创建者", dmAnnounce.getCreateBy());
            map.put("更新者", dmAnnounce.getUpdateBy());
            map.put("创建时间", dmAnnounce.getCreateTime());
            map.put("更新时间", dmAnnounce.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}