package cn.com.qmth.examcloud.core.questions.service.impl;

import cn.com.qmth.examcloud.commons.exception.StatusException;
import cn.com.qmth.examcloud.core.questions.base.Constants;
import cn.com.qmth.examcloud.core.questions.base.Model;
import cn.com.qmth.examcloud.core.questions.dao.CoursePropertyRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.CourseProperty;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyDto;
import cn.com.qmth.examcloud.core.questions.service.CoursePropertyService;
import cn.com.qmth.examcloud.support.cache.CacheHelper;
import cn.com.qmth.examcloud.support.cache.bean.CourseCacheBean;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author weiwenhai
 * @describle 课程属性
 * @date 2017.11.2
 */
@Service("coursePropertyService")
public class CoursePropertyServiceImpl implements CoursePropertyService {

    @Autowired
    private CoursePropertyRepo coursePropertyRepo;

    @Override
    public List<CourseProperty> findAllByOrgId(Long orgId) {
        return coursePropertyRepo.findByOrgId(orgId);
    }

    @Override
    public void saveCourseProperty(CoursePropertyDto dto) {
        if (StringUtils.isBlank(dto.getName())) {
            throw new StatusException(Constants.SYS_CODE_400, "属性名不能为空！");
        }

        if (dto.getOrgId() == null) {
            throw new StatusException(Constants.SYS_CODE_400, "所属机构不能为空！");
        }

        if (dto.getCourseId() == null || StringUtils.isBlank(dto.getCourseCode())) {
            throw new StatusException(Constants.SYS_CODE_400, "所属课程不能为空！");
        }

        CourseProperty courseProperty = coursePropertyRepo.findByOrgIdAndName(dto.getOrgId(), dto.getName());
        if (courseProperty != null) {
            throw new StatusException(Constants.SYS_CODE_500, "课程属性名已经存在！");
        }

        if (StringUtils.isBlank(dto.getId())) {
            dto.setId(null);//剔除空字符串ID(导致异常数据)
        }

        courseProperty = new CourseProperty();
        courseProperty.setId(dto.getId());
        courseProperty.setName(dto.getName());
        courseProperty.setOrgId(dto.getOrgId());
        courseProperty.setCourseId(dto.getCourseId());
        courseProperty.setCourseCode(dto.getCourseCode());
        courseProperty.setEnable(true);
        courseProperty.setUpdateTime(new Date());

        coursePropertyRepo.save(courseProperty);
    }

    @Override
    public Page<CourseProperty> findList(CoursePropertyDto dto, Pageable pageable) {
        CourseProperty params = new CourseProperty();

        if (dto.getOrgId() != null) {
            params.setOrgId(dto.getOrgId());
        }

        if (dto.getCourseId() != null) {
            params.setCourseId(dto.getCourseId());
        }

        if (dto.getEnable() != null) {
            params.setEnable(dto.getEnable());
        }

        Example<CourseProperty> query;
        if (!StringUtils.isEmpty(dto.getName())) {
            params.setName(dto.getName());

            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("name", match -> match.contains());

            query = Example.of(params, matcher);
        } else {
            query = Example.of(params);
        }

        Page<CourseProperty> page = coursePropertyRepo.findAll(query, pageable);

        List<CourseProperty> list = page.getContent();
        if (CollectionUtils.isEmpty(list)) {
            return page;
        }

        for (CourseProperty courseProperty : list) {
            CourseCacheBean course = CacheHelper.getCourse(courseProperty.getCourseId());
            if (course != null) {
                courseProperty.setCourseName(course.getName());
            }
        }

        return page;
    }

    @Override
    public List<CourseProperty> findAllByCourseId(Long courseId) {
        return coursePropertyRepo.findByCourseId(courseId);
    }

    @Override
    public List<CourseProperty> findAllByCourseCode(String courseCode) {
        return coursePropertyRepo.findByCourseCode(courseCode);
    }

    @Override
    public List<CourseProperty> findByEnable(String courseCode, Boolean enable) {
        return coursePropertyRepo.findByCourseCodeAndEnable(courseCode, enable);
    }

    @Override
    public void updateCoursePropertyStatus(List<String> coursePropertyIds, Boolean enable) {
        if (CollectionUtils.isEmpty(coursePropertyIds)) {
            return;
        }

        List<CourseProperty> courseProperties = coursePropertyRepo.findByIdIn(coursePropertyIds);
        if (CollectionUtils.isEmpty(courseProperties)) {
            return;
        }

        for (CourseProperty courseProperty : courseProperties) {
            courseProperty.setEnable(enable);
            courseProperty.setUpdateTime(new Date());
        }

        coursePropertyRepo.saveAll(courseProperties);
    }

    @Override
    public void updateCoursePropertyStatus(String coursePropertyId, Boolean enable) {
        CourseProperty courseProperty = Model.of(coursePropertyRepo.findById(coursePropertyId));
        if (courseProperty == null) {
            throw new StatusException(Constants.SYS_CODE_500, "课程属性已不存在！");
        }

        courseProperty.setEnable(enable);
        courseProperty.setUpdateTime(new Date());
        coursePropertyRepo.save(courseProperty);
    }

}