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

import me.zhengjie.modules.dmParkingTraffic.domain.DmParkingTraffic;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dmParkingTraffic.repository.DmParkingTrafficRepository;
import me.zhengjie.modules.dmParkingTraffic.service.DmParkingTrafficService;
import me.zhengjie.modules.dmParkingTraffic.service.dto.DmParkingTrafficDto;
import me.zhengjie.modules.dmParkingTraffic.service.dto.DmParkingTrafficQueryCriteria;
import me.zhengjie.modules.dmParkingTraffic.service.mapstruct.DmParkingTrafficMapper;
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 zhirong
* @date 2022-05-05
**/
@Service
@RequiredArgsConstructor
public class DmParkingTrafficServiceImpl implements DmParkingTrafficService {

    private final DmParkingTrafficRepository dmParkingTrafficRepository;
    private final DmParkingTrafficMapper dmParkingTrafficMapper;

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

    @Override
    public List<DmParkingTrafficDto> queryAll(DmParkingTrafficQueryCriteria criteria){
        return dmParkingTrafficMapper.toDto(dmParkingTrafficRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmParkingTrafficDto findById(Integer id) {
        DmParkingTraffic dmParkingTraffic = dmParkingTrafficRepository.findById(id).orElseGet(DmParkingTraffic::new);
        ValidationUtil.isNull(dmParkingTraffic.getId(),"DmParkingTraffic","id",id);
        return dmParkingTrafficMapper.toDto(dmParkingTraffic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmParkingTrafficDto create(DmParkingTraffic resources) {
        return dmParkingTrafficMapper.toDto(dmParkingTrafficRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmParkingTrafficDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmParkingTrafficDto dmParkingTraffic : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("停车场唯一标识", dmParkingTraffic.getParkCode());
            map.put("出入口唯一标识", dmParkingTraffic.getEntranceCode());
            map.put("出入口名称", dmParkingTraffic.getEntranceName());
            map.put("车道唯一标识 通道/出入口信息", dmParkingTraffic.getRoadwayCode());
            map.put("车道名称", dmParkingTraffic.getRoadwayName());
            map.put("车牌号码", dmParkingTraffic.getPlateNo());
            map.put("车牌颜色", dmParkingTraffic.getPlateColor());
            map.put("出入类型  0-进场，1-出场", dmParkingTraffic.getVehicleOut());
            map.put("车辆颜色", dmParkingTraffic.getVehicleColor());
            map.put("车辆类型       0：其他车  1：小型车  2：大型车  3：摩托车", dmParkingTraffic.getVehicleType());
            map.put("放行方式   10-未开闸   11-自动开闸   12-人工/人工开闸   13-遥控器开闸", dmParkingTraffic.getReleaseWay());
            map.put("放行结果  0-未放行 1-正常放行 2-离线放行", dmParkingTraffic.getReleaseResult());
            map.put("放行原因(备注)", dmParkingTraffic.getReleaseRemark());
            map.put("入园时间/出园时间", dmParkingTraffic.getCrossTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}