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

import com.alibaba.fastjson.JSONObject;
import me.zhengjie.modules.dm.dmPlateTraffic.domain.DmPlateTraffic;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.dmPlateTraffic.repository.DmPlateTrafficRepository;
import me.zhengjie.modules.dm.dmPlateTraffic.service.DmPlateTrafficService;
import me.zhengjie.modules.dm.dmPlateTraffic.service.dto.DmPlateTrafficDto;
import me.zhengjie.modules.dm.dmPlateTraffic.service.dto.DmPlateTrafficQueryCriteria;
import me.zhengjie.modules.dm.dmPlateTraffic.service.mapstruct.DmPlateTrafficMapper;
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.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author juan
* @date 2021-11-23
**/
@Service
@RequiredArgsConstructor
public class DmPlateTrafficServiceImpl implements DmPlateTrafficService {

    private final DmPlateTrafficRepository dmPlateTrafficRepository;
    private final DmPlateTrafficMapper dmPlateTrafficMapper;

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

    @Override
    public List<DmPlateTrafficDto> queryAll(DmPlateTrafficQueryCriteria criteria){
        return dmPlateTrafficMapper.toDto(dmPlateTrafficRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmPlateTrafficDto findById(Long id) {
        DmPlateTraffic dmPlateTraffic = dmPlateTrafficRepository.findById(id).orElseGet(DmPlateTraffic::new);
        ValidationUtil.isNull(dmPlateTraffic.getId(),"DmPlateTraffic","id",id);
        return dmPlateTrafficMapper.toDto(dmPlateTraffic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmPlateTrafficDto create(DmPlateTraffic resources) {
        return dmPlateTrafficMapper.toDto(dmPlateTrafficRepository.save(resources));
    }

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

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

    @Override
    public void download(List<DmPlateTrafficDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmPlateTrafficDto dmPlateTraffic : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户", dmPlateTraffic.getDmUser().getName());
            map.put("车牌", dmPlateTraffic.getPlateNumber());
            map.put("设备编号", dmPlateTraffic.getRoomDevice());
            map.put("出入(00-进入、01-离开)", dmPlateTraffic.getComeGoOut());
            map.put("设备安装位置", dmPlateTraffic.getBuildAddress());
            map.put("通行时间", dmPlateTraffic.getAccessTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> parkingSpot() {
        String url = "http://10.208.128.14:9999/api/park/getparkspacenum";
        String dataStr = BeanMapUtils.doPostForm(url,null);
        JSONObject dataJSON = JSONObject.parseObject(dataStr);
        JSONObject data = dataJSON.getJSONObject("data");
        Integer totalNum = data.getInteger("totalnum");//总车位数
        Integer surplusNum = data.getInteger("surplusnum");//剩余车位数

        Map<String,Object> map = new HashMap<>();
        map.put("getSumPlate",totalNum);
        map.put("getYiPlate",totalNum-surplusNum);//dmPlateTrafficRepository.getYiPlate()
        map.put("getShenPlate",surplusNum);//dmPlateTrafficRepository.getShenPlate()
        map.put("getCountPlate",dmPlateTrafficRepository.getCountPlate());
        return map;
    }

    @Override
    public List<Map<String, Object>> getParkingTrafficByUserNumber(String userNumber, Pageable pageable) {
        return dmPlateTrafficRepository.getParkingTrafficByUserNumber(userNumber,pageable);
    }
}