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

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

import cn.com.qmth.examcloud.core.questions.base.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.qmth.examcloud.api.commons.security.bean.User;
import cn.com.qmth.examcloud.core.questions.service.bean.PropertyAssembler;
import cn.com.qmth.examcloud.core.questions.dao.PropertyRepo;
import cn.com.qmth.examcloud.core.questions.dao.entity.dto.PropertyDto;
import cn.com.qmth.examcloud.core.questions.dao.entity.Property;
import cn.com.qmth.examcloud.core.questions.service.PropertyService;

/**
 * @author weiwenhai
 * @describle 课程属性 coursePropertyServiceImpl
 * @date 2017.11.8
 */
@Service("propertyService")
public class PropertyServiceImpl implements PropertyService {

    @Autowired
    private PropertyRepo propertyRepo;

    @Override
    public void saveProperty(List<PropertyDto> propertyDtos, User user) {
        PropertyAssembler propertyAssembler = new PropertyAssembler();
        for (int i = 0; i < propertyDtos.size(); i++) {
            //取到父类节点对象
            Property property = propertyAssembler.toProperty(propertyDtos.get(i));
            property.setNumber(i + 1);
            property.setOrgId(user.getRootOrgId());
            property = propertyRepo.save(property);
            List<Property> properties = propertyAssembler.toPropertySon(propertyDtos.get(i));
            if (properties != null && properties.size() > 0) {
                for (int j = 0; j < properties.size(); j++) {
                    properties.get(j).setParentId(property.getId());
                    properties.get(j).setNumber(j + 1);
                }
                propertyRepo.saveAll(properties);
            }
        }
    }

    @Override
    public List<PropertyDto> propertyDtos(Long coursePropertyId, User user) {
        List<PropertyDto> propertyDtos = new ArrayList<PropertyDto>();
        //查询所有父节点对象
        List<Property> propertieParents = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(user.getRootOrgId(), coursePropertyId, 0l);
        if (propertieParents != null && propertieParents.size() > 0) {
            for (Property property : propertieParents) {
                PropertyDto propertyDtoParent = new PropertyDto(property);
                //查询所有子节点对象
                List<Property> propertiesSons = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(user.getRootOrgId(), coursePropertyId, property.getId());
                if (propertiesSons != null && propertiesSons.size() > 0) {
                    List<PropertyDto> propertyDtosSons = new ArrayList<PropertyDto>();
                    for (Property property2 : propertiesSons) {
                        PropertyDto propertyDto2 = new PropertyDto(property2);
                        propertyDtosSons.add(propertyDto2);
                    }
                    propertyDtoParent.setPropertyDtos(propertyDtosSons);
                }
                propertyDtos.add(propertyDtoParent);
            }
        }
        return propertyDtos;
    }

    @Override
    public void saveProperty(Property property, User user) throws Exception {
        //新增
        if (property.getId() == null) {
            //检查是否重名
            Property sameName = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdAndName(user.getRootOrgId(), property.getCoursePropertyId(), property.getParentId(), property.getName());
            if (sameName != null) {
                throw new Exception("属性名已经存在");
            }
            Integer number = 0;
            List<Property> propertieParents = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(user.getRootOrgId(), property.getCoursePropertyId(), property.getParentId());
            if (propertieParents != null && propertieParents.size() > 0) {
                number = propertieParents.get(propertieParents.size() - 1).getNumber();
            }
            property.setNumber(number + 1);
        } else {
            //得到原来的对象
            Property propertyOld = Model.of(propertyRepo.findById(property.getId()));
            if (!propertyOld.getName().equals(property.getName())) {
                //检查是否重名
                Property sameName = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdAndName(user.getRootOrgId(), property.getCoursePropertyId(), property.getParentId(), property.getName());
                if (sameName != null) {
                    throw new Exception("属性名已经存在");
                }
            }
        }
        property.setOrgId(user.getRootOrgId());
        propertyRepo.save(property);
    }

    @Override
    public void deleteProperty(Long propertyId, Long coursePropertyId, User user) {
        //查询子节点对象
        List<Property> properties = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(user.getRootOrgId(), coursePropertyId, propertyId);
        if (properties != null && properties.size() > 0) {
            for (Property property : properties) {
                propertyRepo.delete(property);
            }
        }
        propertyRepo.deleteById(propertyId);
    }

    @Override
    public List<Property> findAll(Long coursePropertyId, User user) {
        List<PropertyDto> propertyDtos = propertyDtos(coursePropertyId, user);
        List<Property> properties = new ArrayList<Property>();
        if (propertyDtos != null && propertyDtos.size() > 0) {
            for (PropertyDto propertyDto : propertyDtos) {
                Property property = new Property(propertyDto);
                properties.add(property);
                if (propertyDto.getPropertyDtos() != null && propertyDto.getPropertyDtos().size() > 0) {
                    for (PropertyDto propertyDtoSon : propertyDto.getPropertyDtos()) {
                        Property propertySon = new Property(propertyDtoSon);
                        properties.add(propertySon);
                    }
                }
            }
        }
        return properties;
    }

    @Override
    public List<Property> findPropertySons(Property property) {
        List<Property> properySonsList = propertyRepo.findByParentIdOrderByNumber(property.getId());
        return properySonsList;
    }

    @Override
    public List<Property> findPropertySons(Long propertyId) {
        List<Property> properySonsList = propertyRepo.findByParentIdOrderByNumber(propertyId);
        return properySonsList;
    }

    @Override
    public List<Property> findPropertyParents(Long coursePropertyId, Long orgId) {
        List<Property> properties = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(orgId, coursePropertyId, 0l);
        return properties;
    }

    @Override
    public void moveUp(Property property) throws Exception {
        //查询所有一级属性
        List<Property> properties = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(property.getOrgId(), property.getCoursePropertyId(), 0L);
        if (properties.size() < 2) {
            throw new Exception("无法上移");
        } else {
            for (int i = 0; i < properties.size(); i++) {
                if (property.getId().equals(properties.get(i).getId())) {
                    if (i == 0) {
                        throw new Exception("无法上移");
                    } else {
                        Integer number = property.getNumber();
                        property.setNumber(properties.get(i - 1).getNumber());
                        properties.get(i - 1).setNumber(number);
                        propertyRepo.save(property);
                        propertyRepo.save(properties.get(i - 1));
                    }
                }
            }
        }
    }

    @Override
    public void moveDown(Property property) throws Exception {
        //查询所有一级属性
        List<Property> properties = propertyRepo.findByOrgIdAndCoursePropertyIdAndParentIdOrderByNumber(property.getOrgId(), property.getCoursePropertyId(), 0l);
        if (properties.size() < 2) {
            throw new Exception("无法下移");
        } else {
            for (int i = 0; i < properties.size(); i++) {
                if (property.getId().equals(properties.get(i).getId())) {
                    if (i == (properties.size() - 1)) {
                        throw new Exception("无法下移");
                    } else {
                        Integer number = property.getNumber();
                        property.setNumber(properties.get(i + 1).getNumber());
                        properties.get(i + 1).setNumber(number);
                        propertyRepo.save(property);
                        propertyRepo.save(properties.get(i + 1));
                    }
                }
            }
        }
    }

}
