package com.securitycontrol.background.service.impl;


import com.securitycontrol.background.mapper.VehicleMapper;
import com.securitycontrol.background.service.VehicleService;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.entity.background.dto.VehicleDto;
import com.securitycontrol.entity.background.vo.VehicleVo;

import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

import java.util.*;

/**
 *车辆管理
 */
@Service(value = "VehicleService")
@Slf4j
public class VehicleServiceImpl implements VehicleService {

    @Resource(name = "VehicleMapper")
    private VehicleMapper mapper;

    /**
     * 列表
     * @param dto
     * @return
     */
    @Override
    public List<VehicleVo> getVehicleLists(VehicleVo dto) {
        List<VehicleVo> list = new ArrayList<>();
        try {
            list = mapper.getVehicleLists(dto);
        } catch (Exception e) {
            log.error("获取车辆列表",e);
        }
        return list;
    }

    /**
     * 新增修改
     * @param vo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateVehicle(VehicleVo vo) {
        try {
            if (StringUtils.isEmpty(vo.getId())) {
                String ids = IdUtils.getUUId();
                vo.setId(ids);
                vo.setType(1);
            } else {
                vo.setType(2);
            }
            mapper.addOrUpdateVehicle(vo);
        } catch (Exception e) {
            log.error("车辆新增/修改", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    /**
     * 删除
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delVehicleById(VehicleVo dto) {
        try {
            mapper.delVehicleById(dto);
        } catch (Exception e) {
            log.error("车辆删除", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public List<VehicleVo> getLists(VehicleVo dto) {
        List<VehicleVo> list = new ArrayList<>();
        String date = dto.getDate();
        if (!StringUtils.isEmpty(date)) {
            String[] arr = date.split("@");
            dto.setStartDate(arr[0]);
            dto.setEndDate(arr[1]);
        }
        try {
            list = mapper.getLists(dto);
        } catch (Exception e) {
            log.error("获取进出场记录列表",e);
        }
        return list;
    }

    @Override
    public List<VehicleDto> getListsDto(VehicleDto dto) {
        List<VehicleDto> list = new ArrayList<>();
        String date = dto.getDate();
        if (!StringUtils.isEmpty(date)) {
            String[] arr = date.split("@");
            dto.setStartDate(arr[0]);
            dto.setEndDate(arr[1]);
        }
        try {
            list = mapper.getListsDto(dto);
        } catch (Exception e) {
            log.error("获取进出场记录列表",e);
        }
        return list;
    }

}
