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.QpTransportUserCredentialsMapper;
import com.zd.airbottle.domain.QpTransportUserCredentials;
import com.zd.airbottle.service.IQpTransportUserCredentialsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 运输人员证件Service业务层处理
 *
 * @author zd
 */
@Service
public class QpTransportUserCredentialsServiceImpl implements IQpTransportUserCredentialsService {
    @Autowired
    private QpTransportUserCredentialsMapper qpTransportUserCredentialsMapper;

    /**
     * 查询运输人员证件
     *
     * @param id 运输人员证件主键
     * @return 运输人员证件
     */
    @Override
    public QpTransportUserCredentials selectQpTransportUserCredentialsById(Long id) {
        return qpTransportUserCredentialsMapper.selectQpTransportUserCredentialsById(id);
    }

    /**
     * 查询运输人员证件列表
     *
     * @param qpTransportUserCredentials 运输人员证件
     * @return 运输人员证件
     */
    @Override
    public List<QpTransportUserCredentials> selectQpTransportUserCredentialsList(QpTransportUserCredentials qpTransportUserCredentials) {
        return qpTransportUserCredentialsMapper.selectQpTransportUserCredentialsList(qpTransportUserCredentials);
    }

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

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

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

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

    @Override
    public QpTransportUserCredentials getUserCredentialsByUserId(Long userId) {
        return qpTransportUserCredentialsMapper.getUserCredentialsByUserId(userId);
    }

    @Override
    public List<QpTransportUserCredentials> getUserCredentialsByUserIds(List<Long> userIds) {
        if (CollUtil.isEmpty(userIds))
            return new ArrayList<>();
        return qpTransportUserCredentialsMapper.getByUserIds(userIds);
    }
}
