/*
*  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.LampLog;
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.LampLogRepository;
import com.youchain.basicdata.service.LampLogService;
import com.youchain.basicdata.service.dto.LampLogDto;
import com.youchain.basicdata.service.dto.LampLogQueryCriteria;
import com.youchain.basicdata.service.mapstruct.LampLogMapper;
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 2023-11-14
**/
@Service
@RequiredArgsConstructor
public class LampLogServiceImpl implements LampLogService {

    private final LampLogRepository lampLogRepository;
    private final LampLogMapper lampLogMapper;

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

    @Override
    public List<LampLogDto> queryAll(LampLogQueryCriteria criteria){
        return lampLogMapper.toDto(lampLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public LampLogDto findById(Long id) {
        LampLog lampLog = lampLogRepository.findById(id).orElseGet(LampLog::new);
        ValidationUtil.isNull(lampLog.getId(),"LampLog","id",id);
        return lampLogMapper.toDto(lampLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public LampLogDto create(LampLog resources) {
        return lampLogMapper.toDto(lampLogRepository.save(resources));
    }

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

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

    @Override
    public void download(List<LampLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (LampLogDto lampLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("ip", lampLog.getIp());
            map.put("所属按钮盒", lampLog.getButtonBox());
            map.put("按钮编码", lampLog.getButtonCode());
            map.put("操作类型", lampLog.getOperationType());
            map.put("请求内容", lampLog.getRequestContent());
            map.put("返回内容", lampLog.getReturnContent());
            map.put("返回状态", lampLog.getReturnState());
            map.put("创建人", lampLog.getCreateBy());
            map.put("修改人", lampLog.getUpdateBy());
            map.put("创建时间", lampLog.getCreateTime());
            map.put("修改时间", lampLog.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}