package com.zd.airbottle.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.zd.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.airbottle.mapper.QpTransportCarCredentialsMapper;
import com.zd.airbottle.domain.QpTransportCarCredentials;
import com.zd.airbottle.service.IQpTransportCarCredentialsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运输车辆证件Service业务层处理
 *
 * @author zd
 */
@Service
public class QpTransportCarCredentialsServiceImpl implements IQpTransportCarCredentialsService {
    @Autowired
    private QpTransportCarCredentialsMapper qpTransportCarCredentialsMapper;

    /**
     * 查询运输车辆证件
     *
     * @param id 运输车辆证件主键
     * @return 运输车辆证件
     */
    @Override
    public QpTransportCarCredentials selectQpTransportCarCredentialsById(Long id) {
        return qpTransportCarCredentialsMapper.selectQpTransportCarCredentialsById(id);
    }

    /**
     * 查询运输车辆证件列表
     *
     * @param qpTransportCarCredentials 运输车辆证件
     * @return 运输车辆证件
     */
    @Override
    public List<QpTransportCarCredentials> selectQpTransportCarCredentialsList(QpTransportCarCredentials qpTransportCarCredentials) {
        return qpTransportCarCredentialsMapper.selectQpTransportCarCredentialsList(qpTransportCarCredentials);
    }

    /**
     * 新增运输车辆证件
     *
     * @param qpTransportCarCredentials 运输车辆证件
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertQpTransportCarCredentials(QpTransportCarCredentials qpTransportCarCredentials) {
        Date endTime = qpTransportCarCredentials.getEndTime();
        Date startTime = qpTransportCarCredentials.getStartTime();
        if (startTime!=null && endTime!=null && startTime.getTime()>endTime.getTime()){
            throw new ServiceException("开始时间不能大于使用时间");
        }
        return qpTransportCarCredentialsMapper.insertQpTransportCarCredentials(qpTransportCarCredentials);
    }

    /**
     * 修改运输车辆证件
     *
     * @param qpTransportCarCredentials 运输车辆证件
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateQpTransportCarCredentials(QpTransportCarCredentials qpTransportCarCredentials) {
        Date endTime = qpTransportCarCredentials.getEndTime();
        Date startTime = qpTransportCarCredentials.getStartTime();
        if (startTime!=null && endTime!=null && startTime.getTime()>endTime.getTime()){
            throw new ServiceException("开始时间不能大于使用时间");
        }
        return qpTransportCarCredentialsMapper.updateQpTransportCarCredentials(qpTransportCarCredentials);
    }

    /**
     * 批量删除运输车辆证件
     *
     * @param ids 需要删除的运输车辆证件主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQpTransportCarCredentialsByIds(Long[] ids) {
        return qpTransportCarCredentialsMapper.deleteQpTransportCarCredentialsByIds(ids);
    }

    /**
     * 删除运输车辆证件信息
     *
     * @param id 运输车辆证件主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteQpTransportCarCredentialsById(Long id) {
        return qpTransportCarCredentialsMapper.deleteQpTransportCarCredentialsById(id);
    }

    @Override
    public QpTransportCarCredentials getCarCredentialsByCarId(Long carId) {
        return qpTransportCarCredentialsMapper.getCarCredentialsByCarId(carId);
    }

    @Override
    public List<QpTransportCarCredentials> getByCarIds(List<Long> carIds) {
        if (CollUtil.isEmpty(carIds))
            return new ArrayList<>();
        return qpTransportCarCredentialsMapper.getByCarIds(carIds);
    }
}
