package com.keao.edu.user.service.impl;

import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.exception.BizException;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.page.QueryInfo;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.dao.ExamRegistrationDao;
import com.keao.edu.user.dao.ExamSongDao;
import com.keao.edu.user.entity.ExamSong;
import com.keao.edu.user.page.ExamSongQueryInfo;
import com.keao.edu.user.service.ExamSongService;
import com.keao.edu.util.collection.MapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ExamSongServiceImpl extends BaseServiceImpl<Integer, ExamSong> implements ExamSongService {
	
	@Autowired
	private ExamSongDao examSongDao;
	@Autowired
	private ExamRegistrationDao examRegistrationDao;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@Override
	public BaseDAO<Integer, ExamSong> getDAO() {
		return examSongDao;
	}

    @Override
    public long insert(ExamSong examSong) {
        if(StringUtils.isBlank(examSong.getSubjectList())){
            throw new BizException("请选择专业");
        }
	    if(Objects.isNull(examSong.getType())){
	        throw new BizException("请选择曲库类别");
        }
//      List<ExamSong> existsExamSongs = examSongDao.getWithLevelAndType(examSong.getTenantId(), examSong.getLevelList(), examSong.getType());
//	    if(!CollectionUtils.isEmpty(existsExamSongs)){
//	        throw new BizException("{}级别的{}已存在",examSong.getLevelList(), examSong.getType().getMsg());
//        }
        return super.insert(examSong);
    }

    @Override
    public PageInfo<ExamSong> queryPage(QueryInfo queryInfo) {
        PageInfo<ExamSong> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);

        List<ExamSong> dataList = new ArrayList<>();
        int count = this.findCount(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = this.getDAO().queryPage(params);
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }

    @Override
    public List<ExamSong> getWithSubject(Integer subjectId) {
	    if(Objects.isNull(subjectId)){
	        throw new BizException("请选择专业");
        }
        return examSongDao.getWithSubject(subjectId);
    }

    @Override
    public PageInfo<ExamSong> querySongPage(ExamSongQueryInfo queryInfo) {
        PageInfo<ExamSong> pageInfo = new PageInfo<>(queryInfo.getPage(), queryInfo.getRows());
	    //获取学员所在机构
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        List<Integer> tenantIds = examRegistrationDao.queryStudentTenantId(sysUser.getId());
        if(tenantIds == null || tenantIds.size() == 0){
            return pageInfo;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        MapUtil.populateMap(params, queryInfo);
//        params.put("tenantIds",tenantIds);

        List<ExamSong> dataList = new ArrayList<>();
        int count = examSongDao.countSongPage(params);
        if (count > 0) {
            pageInfo.setTotal(count);
            params.put("offset", pageInfo.getOffset());
            dataList = examSongDao.querySongPage(params);
        }
        pageInfo.setRows(dataList);
        return pageInfo;
    }
}
