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

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.Predicate;

import cn.com.qmth.examcloud.core.questions.base.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.com.qmth.examcloud.core.basic.api.CourseCloudService;
import cn.com.qmth.examcloud.core.basic.api.request.GetCourseReq;
import cn.com.qmth.examcloud.core.basic.api.response.GetCourseResp;
import cn.com.qmth.examcloud.core.questions.dao.CoursePropertyRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.CoursePropertyDto;
import cn.com.qmth.examcloud.core.questions.dao.entity.CourseProperty;
import cn.com.qmth.examcloud.core.questions.service.CoursePropertyService;

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

    @Autowired
    private CoursePropertyRepo coursePropertyRepo;

    @Autowired
    CourseCloudService courseCloudService;

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

    @Override
    public void saveCourseProperty(CoursePropertyDto coursePropertyDto) throws Exception {
        coursePropertyDto.setEnable("1");
        CourseProperty courseProperty = coursePropertyRepo.findByNameAndOrgId(coursePropertyDto.getName(), coursePropertyDto.getOrgId());
        if (courseProperty != null) {
            throw new Exception("课程属性名已经存在");
        }
        courseProperty = new CourseProperty(coursePropertyDto);
        coursePropertyRepo.save(courseProperty);
    }

    @Override
    public Page<CourseProperty> findAllByOrg(CoursePropertyDto coursePropertyDto, Pageable pageable) {
        Specification<CourseProperty> specification = getSpecification(coursePropertyDto);
        Page<CourseProperty> coursePropertyies = coursePropertyRepo.findAll(specification, pageable);
        List<CourseProperty> list = coursePropertyies.getContent();
        if (list != null && list.size() > 0) {
            for (CourseProperty courseProperty : list) {
                GetCourseReq req = new GetCourseReq();
                req.setId(courseProperty.getCourseId());
                req.setRootOrgId(Long.valueOf(courseProperty.getOrgId()));
                GetCourseResp resp = courseCloudService.getCourse(req);
                courseProperty.setCourseName(resp.getCourseBean().getName());
            }
        }
        return coursePropertyies;
    }

    private Specification<CourseProperty> getSpecification(CoursePropertyDto coursePropertyDto) {
        CourseProperty courseProperty = new CourseProperty(coursePropertyDto);
        Specification<CourseProperty> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!StringUtils.isEmpty(courseProperty.getOrgId())) {
                predicates.add(cb.equal(root.get("orgId"), courseProperty.getOrgId()));
            }
            if (!StringUtils.isEmpty(courseProperty.getName())) {
                predicates.add(cb.like(root.get("name"), "%" + courseProperty.getName() + "%"));
            }
            if (!StringUtils.isEmpty(courseProperty.getCourseId())) {
                predicates.add(cb.equal(root.get("courseId"), courseProperty.getCourseId()));
            }
            if (!StringUtils.isEmpty(courseProperty.getEnable())) {
                predicates.add(cb.equal(root.get("enable"), courseProperty.getEnable()));
            }
            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        return specification;
    }

    @Override
    public void openCourseProperty(Long id) {
        CourseProperty courseProperty = Model.of(coursePropertyRepo.findById(id));
        courseProperty.setEnable(true);
        coursePropertyRepo.save(courseProperty);
    }

    @Override
    public void closeCourseProperty(Long id) {
        CourseProperty courseProperty = Model.of(coursePropertyRepo.findById(id));
        courseProperty.setEnable(false);
        coursePropertyRepo.save(courseProperty);
    }

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

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

    @Override
    public List<CourseProperty> findAllEnable(String code, Boolean enable) {
        List<CourseProperty> courseProperties = coursePropertyRepo.findByCourseCodeAndEnable(code, true);
        return courseProperties;
    }

    @Override
    public void opensCourseProperty(List<String> idList) {
        List<Long> ids = new ArrayList<Long>();
        if (idList != null && idList.size() > 0) {
            for (String id : idList) {
                ids.add(Long.parseLong(id));
            }
        }
        List<CourseProperty> courseProperties = coursePropertyRepo.findByIdIn(ids);
        for (CourseProperty courseProperty : courseProperties) {
            courseProperty.setEnable(true);
        }
        coursePropertyRepo.saveAll(courseProperties);
    }

    @Override
    public void closesCourseProperty(List<String> idList) {
        List<Long> ids = new ArrayList<Long>();
        if (idList != null && idList.size() > 0) {
            for (String id : idList) {
                ids.add(Long.parseLong(id));
            }
        }
        List<CourseProperty> courseProperties = coursePropertyRepo.findByIdIn(ids);
        for (CourseProperty courseProperty : courseProperties) {
            courseProperty.setEnable(false);
        }
        coursePropertyRepo.saveAll(courseProperties);
    }

}
