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

import me.zhengjie.modules.dm.camera.domain.DmCamera;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.camera.repository.DmCameraRepository;
import me.zhengjie.modules.dm.camera.service.DmCameraService;
import me.zhengjie.modules.dm.camera.service.dto.DmCameraDto;
import me.zhengjie.modules.dm.camera.service.dto.DmCameraQueryCriteria;
import me.zhengjie.modules.dm.camera.service.mapstruct.DmCameraMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
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 xwx
* @date 2021-04-20
**/
@Service
@RequiredArgsConstructor
public class DmCameraServiceImpl implements DmCameraService {

    private final DmCameraRepository dmCameraRepository;
    private final DmCameraMapper dmCameraMapper;

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

    @Override
    public List<DmCameraDto> queryAll(DmCameraQueryCriteria criteria){
        return dmCameraMapper.toDto(dmCameraRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmCameraDto findById(Long id) {
        DmCamera dmCamera = dmCameraRepository.findById(id).orElseGet(DmCamera::new);
        ValidationUtil.isNull(dmCamera.getId(),"DmCamera","id",id);
        return dmCameraMapper.toDto(dmCamera);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmCameraDto create(DmCamera resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmCameraMapper.toDto(dmCameraRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmCameraDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmCameraDto dmCamera : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("摄像头编号", dmCamera.getCode());
            map.put("摄像头名称", dmCamera.getName());
            map.put("摄像头参数(摄像头基本参数)", dmCamera.getCameraParam());
            map.put("摄像头位置(摄像头安装位置)", dmCamera.getAddress());
            map.put("所属餐厅(摄像头所属餐厅)", dmCamera.getRestaurant());
            map.put("创建者", dmCamera.getCreateBy());
            map.put("更新者", dmCamera.getUpdateBy());
            map.put("创建日期", dmCamera.getCreateTime());
            map.put("更新时间", dmCamera.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int checkCode(String code,Long id){ return dmCameraRepository.checkCode(code,id); }
}