package com.rf.psychological.scale.service.impl;

import com.rf.psychological.dao.model.DimensionEntity;
import com.rf.psychological.dao.repository.DimensionRepository;
import com.rf.psychological.scale.dao.model.SubjectEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.rf.psychological.scale.service.DimensionService;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zsy
 * @description:
 * @date 2021/5/14 10:51
 */
@Service
@Slf4j
public class DimensionServiceImpl implements DimensionService {
    @Autowired
    private DimensionRepository dimensionRepository;

    /**
     * 保存
     *
     * @param dimensionEntity
     */
    @Override
    public void saveDimension(DimensionEntity dimensionEntity) {
        //dimensionEntity.encrypt();
        this.dimensionRepository.save(dimensionEntity);
    }

    @Override
    public void saveBatch(List<DimensionEntity> dimensionEntitys) {
        this.dimensionRepository.saveAll(dimensionEntitys);
    }

    /**
     * 根据flag查询维度信息
     *
     * @param flag
     */
    @Override
    public List<DimensionEntity> getDimensionByFlag(String flag) {
        //解密
        //List<DimensionEntity> dimensionEntities = this.dimensionRepository.getDimensionByFlag(flag);
        //List<DimensionEntity> result = new ArrayList<>();
        //for (DimensionEntity dimensionEntity : dimensionEntities) {
        //    dimensionEntity.decrypt();
        //    result.add(dimensionEntity);
        //}

        return this.dimensionRepository.getDimensionByFlag(flag);
    }

    @Override
    public List<DimensionEntity> findAll() {
        return dimensionRepository.findAll();
    }

    @Override
    public void updateDimension(DimensionEntity dimensionEntity) {
        this.dimensionRepository.updateDimension(dimensionEntity.getName(),dimensionEntity.getQuestionNo(),dimensionEntity.getId());
    }

    @Override
    public void deleteDimensionByFlag(String flag) {
        this.dimensionRepository.deleteDimensionByFlag(flag);
    }

    @Override
    public List<String> findByIds(List<String> dimensionIds) {
        Specification<DimensionEntity> subspecification  = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicateList =  new ArrayList<>();
            if(dimensionIds != null && dimensionIds.size()>0){
                predicateList.add(root.get("id").in(dimensionIds));
            }
            criteriaQuery.where(criteriaBuilder.and(predicateList.toArray(new Predicate[0])));
            return criteriaQuery.getRestriction();
        };
        List<DimensionEntity> entities = this.dimensionRepository.findAll(subspecification);
        List<String> flags = new ArrayList<>();
        if(entities != null && entities.size()>0){
            entities.forEach(item -> flags.add(item.getFlag()));
        }


        return flags;
    }

    @Override
    public DimensionEntity findById(String dimensionId) {
        return this.dimensionRepository.findById(dimensionId).orElse(null);
    }
}
