package com.ym.mec.biz.service.impl;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.StudentCourseHomeworkDao;
import com.ym.mec.biz.dal.dto.CourseHomeworkStudentDetailDto;
import com.ym.mec.biz.dal.entity.CourseHomework;
import com.ym.mec.biz.dal.entity.StudentCourseHomework;
import com.ym.mec.biz.dal.page.CourseHomeworkQueryInfo;
import com.ym.mec.biz.service.CourseHomeworkService;
import com.ym.mec.biz.service.StudentCourseHomeworkService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.QueryInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Service
public class StudentCourseHomeworkServiceImpl extends BaseServiceImpl<Long, StudentCourseHomework>  implements StudentCourseHomeworkService {
	
	@Autowired
	private StudentCourseHomeworkDao studentCourseHomeworkDao;
	@Autowired
	private CourseHomeworkService courseHomeworkService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	private String url = "http://auth-server/queryUserInfo";

	@Override
	public BaseDAO<Long, StudentCourseHomework> getDAO() {
		return studentCourseHomeworkDao;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public long insert(StudentCourseHomework bean) {
		SysUser user = sysUserFeignService.queryUserInfo();
		bean.setUserId(Long.valueOf(user.getId()));
		long insert = super.insert(bean);

		CourseHomework courseHomework=new CourseHomework();
		courseHomework.setId(bean.getCourseHomeworkId());
		courseHomework.setCompletedNum(studentCourseHomeworkDao.countCompletedStudentNum(bean.getCourseHomeworkId()));
		courseHomeworkService.update(courseHomework);
		return insert;
	}

	@Override
	public CourseHomeworkStudentDetailDto findCourseHomeworkStudentDetail(Long courseScheduleID) throws IOException {
		SysUser user = sysUserFeignService.queryUserInfo();
		return studentCourseHomeworkDao.findCourseHomeworkStudentDetail(courseScheduleID,Long.valueOf(user.getId()));
	}

	@Override
	public PageInfo<StudentCourseHomework> queryPage(QueryInfo queryInfo) {
		SysUser user = sysUserFeignService.queryUserInfo();
		CourseHomeworkQueryInfo courseHomeworkQueryInfo= (CourseHomeworkQueryInfo) queryInfo;
		courseHomeworkQueryInfo.setUserId(Long.valueOf(user.getId()));
		return super.queryPage(queryInfo);
	}
}