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

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.scale.dao.model.DimensionOrgEntity;
import com.rf.psychological.scale.dao.repository.DimensionOrgRepository;
import com.rf.psychological.scale.dao.repository.SubjectRepository;
import com.rf.psychological.scale.service.DimensionOrgService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

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

/**
 * @Author:zzf
 * @Date:2022/10/10:11:16
 * @Description:
 */
@Service
public class DimensionOrgServiceImpl implements DimensionOrgService {

    @Autowired
    private DimensionOrgRepository repository;


    @Override
    public DimensionOrgEntity save(DimensionOrgEntity dimensionOrgEntity) {
        return repository.save(dimensionOrgEntity);
    }

    @Override
    public List<DimensionOrgEntity> find(JSONObject jsonObject) {
        Specification<DimensionOrgEntity> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if(jsonObject.containsKey("id") && StringUtils.isNotBlank(jsonObject.getString("id"))){
                predicateList.add(criteriaBuilder.equal(root.get("id"),jsonObject.getString("id")));
            }
//            if(jsonObject.containsKey("flag") && StringUtils.isNotBlank(jsonObject.getString("flag"))){
//                predicateList.add(criteriaBuilder.equal(root.get("flag"),jsonObject.getString("flag")));
//            }
            if(jsonObject.containsKey("dimensionId") && StringUtils.isNotBlank(jsonObject.getString("dimensionId"))){
                predicateList.add(criteriaBuilder.equal(root.get("dimensionId"),jsonObject.getString("dimensionId")));
            }
//            predicateList.add(criteriaBuilder.equal(root.get("flag"), jsonObject.getString("flag")));
            predicateList.add(criteriaBuilder.equal(root.get("institutionNo"),jsonObject.getString("institutionNo")));
            query.where(criteriaBuilder.and(predicateList.toArray(new Predicate[0])));
//            query.groupBy(root.get("flag"));
            query.orderBy(criteriaBuilder.asc(root.get("dimensionId")));
            return  query.getRestriction();
        };

        return this.repository.findAll(specification);
    }


    @Override
    public DimensionOrgEntity findByInstitutionNoAndDimensionId(String institutionNo, String id) {
        return this.repository.findByInstitutionNoAndDimensionId( institutionNo,  id) ;
    }

    @Override
    public void deleteByFlagAndInstitutionNo(String dimensionId, String institutionNo) {
        this.repository.deleteByFlagAndInstitutionNo(dimensionId,institutionNo);
    }

    @Override
    public List<DimensionOrgEntity> findByInstitutionNoAndFlags(String institutionNo, List<String> flags) {
        Specification<DimensionOrgEntity> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();
            predicateList.add(criteriaBuilder.equal(root.get("institutionNo"), institutionNo));
            predicateList.add(root.get("flag").in(flags));
            query.where(criteriaBuilder.and(predicateList.toArray(new Predicate[0])));
            return  query.getRestriction();
        };

        return this.repository.findAll(specification);
    }
}
