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

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.dao.repository.DimensionRepository;
import com.rf.psychological.scale.dao.dto.ResultDto;
import com.rf.psychological.scale.dao.model.DimensionOrgEntity;
import com.rf.psychological.scale.dao.model.WarnEntity;
import com.rf.psychological.scale.dao.repository.WarnRepository;
import com.rf.psychological.scale.service.WarnService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.user.dao.repository.UserRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
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;
import java.util.Map;

/**
 * @Author:zzf
 * @Date:2022/10/12:15:52
 * @Description:
 */
@Service
public class WarnServiceImpl implements WarnService {

    @Autowired
    private WarnRepository warnRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private DimensionRepository dimensionRepository;
    @Override
    public WarnEntity save(WarnEntity warnEntity) {
        return this.warnRepository.save(warnEntity);
    }

    @Override
    public Page<WarnEntity> find(JSONObject jsonObject) throws Exception {
        int pageNum = 1;
        int pageSize = 10;
        Specification<DimensionOrgEntity> specification = (root, query, criteriaBuilder) -> {

            List<javax.persistence.criteria.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("userId") && (jsonObject.getJSONArray("userId") != null) && jsonObject.getJSONArray("userId").size()>0){
                predicateList.add(root.get("userId").in(jsonObject.getJSONArray("userId").toJavaList(String.class)));
//                predicateList.add(criteriaBuilder.equal(root.get("userId"), jsonObject.getString("userId")));
            }
            if(jsonObject.containsKey("institutionNo") && StringUtils.isNotBlank(jsonObject.getString("institutionNo"))){
                predicateList.add(criteriaBuilder.equal(root.get("institutionNo"), jsonObject.getString("institutionNo")));
            }
            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")));
            }
            if(jsonObject.containsKey("resultId") && StringUtils.isNotBlank(jsonObject.getString("resultId"))){
                predicateList.add(criteriaBuilder.equal(root.get("resultId"), jsonObject.getString("resultId")));
            }
            query.where(criteriaBuilder.and(predicateList.toArray(new Predicate[0])));
//            query.groupBy(root.get("userId"));
            query.orderBy(criteriaBuilder.asc(root.get("flag")));
            query.orderBy(criteriaBuilder.asc(root.get("resultId")));
            query.orderBy(criteriaBuilder.asc(root.get("dimensionId")));
            query.orderBy(criteriaBuilder.asc(root.get("createTime")));
            return  query.getRestriction();
        };

        if(jsonObject.containsKey("pageNum") && jsonObject.getInteger("pageNum")>0){
            pageNum = jsonObject.getInteger("pageNum");
        }
        if(jsonObject.containsKey("pageSize") && jsonObject.getInteger("pageSize")>0){
            pageSize = jsonObject.getInteger("pageSize");
        }
        return this.warnRepository.findAll(specification, PageRequest.of((pageNum-1),pageSize));
    }

    @Override
    public void deleteByFlagAndInstitutionNo(String dimensionId, String institutionNo) {

        this.warnRepository.deleteByFlagAndInstitutionNo(dimensionId,institutionNo);
    }

    @Override
    public List<Map<String,Object>> findResultList(int pageNum, int pageSize,String flag, String institutionNo, String searckKey) {
        return this.warnRepository.findResultList((pageNum-1)*pageSize,pageSize,flag,institutionNo,searckKey);
    }

    @Override
    public List<WarnEntity> findByResultId(String resultId) {
        return this.warnRepository.findByResultId(resultId);
    }

    @Override
    public int findResultListCount(String flag, String institutionNo, String searckKey) {
        return this.warnRepository.findResultListCount(flag,institutionNo,searckKey);
    }

    @Override
    public int findByInstitutionNo(String institutionNo) {
        return this.warnRepository.findByInstitutionNo(institutionNo);
    }
}
