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

import me.zhengjie.modules.dm.plateNumber.domain.DmPlateNumber;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.plateNumber.repository.DmPlateNumberRepository;
import me.zhengjie.modules.dm.plateNumber.service.DmPlateNumberService;
import me.zhengjie.modules.dm.plateNumber.service.dto.DmPlateNumberDto;
import me.zhengjie.modules.dm.plateNumber.service.dto.DmPlateNumberQueryCriteria;
import me.zhengjie.modules.dm.plateNumber.service.mapstruct.DmPlateNumberMapper;
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 Zheng Jie
* @date 2021-11-27
**/
@Service
@RequiredArgsConstructor
public class DmPlateNumberServiceImpl implements DmPlateNumberService {

    private final DmPlateNumberRepository dmPlateNumberRepository;
    private final DmPlateNumberMapper dmPlateNumberMapper;

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

    @Override
    public List<DmPlateNumberDto> queryAll(DmPlateNumberQueryCriteria criteria){
        return dmPlateNumberMapper.toDto(dmPlateNumberRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmPlateNumberDto findById(Long id) {
        DmPlateNumber dmPlateNumber = dmPlateNumberRepository.findById(id).orElseGet(DmPlateNumber::new);
        ValidationUtil.isNull(dmPlateNumber.getId(),"DmPlateNumber","id",id);
        return dmPlateNumberMapper.toDto(dmPlateNumber);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmPlateNumberDto create(DmPlateNumber resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmPlateNumberMapper.toDto(dmPlateNumberRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmPlateNumberDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmPlateNumberDto dmPlateNumber : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户", dmPlateNumber.getUserNumber());
            map.put("车牌号", dmPlateNumber.getPlateNumber());
            map.put("有效开始时间", dmPlateNumber.getStartTime());
            map.put("有效结束时间", dmPlateNumber.getEndTime());
            map.put("效期状态(00-有效、01-过期)", dmPlateNumber.getEffectiveState());
            map.put("登记状态(00-待审核、01-已登记)", dmPlateNumber.getRegistrationStatus());
            map.put("创建者", dmPlateNumber.getCreateBy());
            map.put("更新者", dmPlateNumber.getUpdateBy());
            map.put("创建时间", dmPlateNumber.getCreateTime());
            map.put("更新时间", dmPlateNumber.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}