package com.zd.exam.service.impl;

import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.service.TokenService;
import com.zd.exam.domain.ElRepo;
import com.zd.exam.domain.vo.ElRepoVO;
import com.zd.exam.mapper.ElQuRepoMapper;
import com.zd.exam.mapper.ElRepoMapper;
import com.zd.exam.service.ElRepoService;
import com.zd.system.api.domain.SysUser;
import com.zd.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 题库(ElRepo)表服务实现类
 *
 * @author liubo
 * @since 2021-08-30 14:45:55
 */
@Service("elRepoService")
public class ElRepoServiceImpl implements ElRepoService {

    @Resource
    private ElRepoMapper elRepoMapper;
    @Autowired
    private ElQuRepoMapper elQuRepoMapper;
    @Autowired
    private TokenService tokenService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ElRepo queryById(Long id) {
        return this.elRepoMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ElRepo> queryAllByLimit(int offset, int limit) {
        return this.elRepoMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param elRepo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(ElRepo elRepo) {
        SysUser user = tokenService.getLoginUser().getSysUser();

        elRepo.setCreateTime(new Date());
        elRepo.setUpdateTime(new Date());
        elRepo.setUserId(user.getUserId());
        elRepo.setCreateBy(user.getUserName());

        elRepo.setJudgeCount(0);
        elRepo.setRadioCount(0);
        elRepo.setMultiCount(0);

        if(user.getDept() != null){
            elRepo.setDeptId(user.getDeptId());
            elRepo.setDeptName(user.getDept().getDeptName());
        }

        return elRepoMapper.insert(elRepo);
    }

    /**
     * 修改数据
     *
     * @param elRepo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(ElRepo elRepo) {
        LoginUser loginUser = tokenService.getLoginUser();

        elRepo.setUpdateBy(loginUser.getSysUser().getUserName());
        elRepo.setUpdateTime(new Date());
        return elRepoMapper.update(elRepo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.elRepoMapper.deleteById(id) > 0;
    }

    /**
     * 获取题库列表
     */
    @Override
    @DataScope(deptAlias = "r", userAlias = "r", permi = PerPrefix.EXAM_REPO)
    public List<ElRepo> queryList(ElRepoVO repovo) {
        return elRepoMapper.queryAll(repovo);
    }

    @Override
    public int deleteByIds(List<Long> ids) {
        int num = elRepoMapper.deleteByIds(ids);

        for (Long id : ids) {
            elQuRepoMapper.deleteByRepoId(id);
        }

        return num;
    }

    /**
     * 更新数量数据
     *
     * @param repoId
     */
    @Override
    public void refreshStat(Long repoId) {
        elRepoMapper.refreshStat(repoId);
    }
}
