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

import me.zhengjie.modules.dm.dmAttendanceRecord.domain.DmAttendanceRecord;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmAttendanceRecord.repository.DmAttendanceRecordRepository;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.DmAttendanceRecordService;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.dto.DmAttendanceRecordDto;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.dto.DmAttendanceRecordQueryCriteria;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.mapstruct.DmAttendanceRecordMapper;
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 juan
* @date 2021-11-23
**/
@Service
@RequiredArgsConstructor
public class DmAttendanceRecordServiceImpl implements DmAttendanceRecordService {

    private final DmAttendanceRecordRepository dmAttendanceRecordRepository;
    private final DmAttendanceRecordMapper dmAttendanceRecordMapper;

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

    @Override
    public List<DmAttendanceRecordDto> queryAll(DmAttendanceRecordQueryCriteria criteria){
        return dmAttendanceRecordMapper.toDto(dmAttendanceRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmAttendanceRecordDto findById(Long id) {
        DmAttendanceRecord dmAttendanceRecord = dmAttendanceRecordRepository.findById(id).orElseGet(DmAttendanceRecord::new);
        ValidationUtil.isNull(dmAttendanceRecord.getId(),"DmAttendanceRecord","id",id);
        return dmAttendanceRecordMapper.toDto(dmAttendanceRecord);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmAttendanceRecordDto create(DmAttendanceRecord resources) {
        return dmAttendanceRecordMapper.toDto(dmAttendanceRecordRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmAttendanceRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmAttendanceRecordDto dmAttendanceRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户", dmAttendanceRecord.getDmUser().getName());
            map.put("卡号", dmAttendanceRecord.getCardNumber());
            map.put("门禁设备编号", dmAttendanceRecord.getRoomDevice());
            map.put("考勤属性", dmAttendanceRecord.getProperty());
            map.put("考勤时间", dmAttendanceRecord.getAttendanceTime());
            map.put("考勤照片", dmAttendanceRecord.getAttendancePhoto());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}