package com.zd.exam.service.impl;

import java.util.Collection;
import java.util.List;

import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zd.exam.mapper.ElCourcesAccessRecordMapper;
import com.zd.exam.domain.ElCourcesAccessRecord;
import com.zd.exam.service.IElCourcesAccessRecordService;

/**
 * 学习资料访问Service业务层处理
 *
 * @author liubo
 * @date 2021-12-07
 */
@Service
public class ElCourcesAccessRecordServiceImpl implements IElCourcesAccessRecordService {
    @Autowired
    private ElCourcesAccessRecordMapper elCourcesAccessRecordMapper;

    /**
     * 查询学习资料访问
     *
     * @param id 学习资料访问主键
     * @return 学习资料访问
     */
    @Override
    public ElCourcesAccessRecord selectElCourcesAccessRecordById(Long id) {
        return elCourcesAccessRecordMapper.selectElCourcesAccessRecordById(id);
    }

    /**
     * 查询学习资料访问列表
     *
     * @param elCourcesAccessRecord 学习资料访问
     * @return 学习资料访问
     */
    @Override
    public List<ElCourcesAccessRecord> selectElCourcesAccessRecordList(ElCourcesAccessRecord elCourcesAccessRecord) {
        return elCourcesAccessRecordMapper.selectElCourcesAccessRecordList(elCourcesAccessRecord);
    }

    /**
     * 新增学习资料访问
     *
     * @param elCourcesAccessRecord 学习资料访问
     * @return 结果
     */
    @Override
    public int insertElCourcesAccessRecord(ElCourcesAccessRecord elCourcesAccessRecord) {

        List<ElCourcesAccessRecord> list = selectElCourcesAccessRecordList(elCourcesAccessRecord);
        if(CollectionUtils.isNotEmpty(list)){
            // 同一用户，同一课程只存一次
            return 0;
        }

        //设置其他公共字段
        SaveUtil.setCommonAttr(elCourcesAccessRecord);
        return elCourcesAccessRecordMapper.insertElCourcesAccessRecord(elCourcesAccessRecord);
    }

    /**
     * 修改学习资料访问
     *
     * @param elCourcesAccessRecord 学习资料访问
     * @return 结果
     */
    @Override
    public int updateElCourcesAccessRecord(ElCourcesAccessRecord elCourcesAccessRecord) {
        elCourcesAccessRecord.setUpdateTime(DateUtils.getNowDate());
        elCourcesAccessRecord.setUpdateBy(SecurityUtils.getUsername());
        return elCourcesAccessRecordMapper.updateElCourcesAccessRecord(elCourcesAccessRecord);
    }

    /**
     * 批量删除学习资料访问
     *
     * @param ids 需要删除的学习资料访问主键
     * @return 结果
     */
    @Override
    public int deleteElCourcesAccessRecordByIds(Long[] ids) {
        return elCourcesAccessRecordMapper.deleteElCourcesAccessRecordByIds(ids);
    }

    /**
     * 删除学习资料访问信息
     *
     * @param id 学习资料访问主键
     * @return 结果
     */
    @Override
    public int deleteElCourcesAccessRecordById(Long id) {
        return elCourcesAccessRecordMapper.deleteElCourcesAccessRecordById(id);
    }
}
