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

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.plan.dao.model.HisTestPlanEntry;
import com.rf.psychological.plan.dao.repository.HisTestPlanRepository;
import com.rf.psychological.plan.service.HisTestPlanService;
import com.rf.psychological.utils.Constant;
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;

/**
 * @Author:zzf
 * @Date:2022/6/28:16:55
 * @Description:历史测试计划
 */
@Service
public class HisTestPlanServiceImpl implements HisTestPlanService {

    @Autowired
    private HisTestPlanRepository repository;
    /**
     * 批量保存
     *
     * @param hisTestPlanEntryList 插入列表
     */
    @Override
    public void saveInBatch(List<HisTestPlanEntry> hisTestPlanEntryList) {
        this.repository.saveAll(hisTestPlanEntryList);
    }

    /**
     * 查询
     *
     * @param jsonObject
     * @return
     */
    @Override
    public Page find(JSONObject jsonObject) {
        int pageNum = 1;
        int pageSize = 20 ;
        if (jsonObject.containsKey("pageNum")&&jsonObject.getIntValue("pageNum") > 0)
            pageNum = jsonObject.getIntValue("pageNum");
        if (jsonObject.containsKey("pageSize")&&jsonObject.getIntValue("pageSize") > 0)
            pageSize = jsonObject.getIntValue("pageSize");
        Specification<HisTestPlanEntry> specification = (root, query, cb) -> {
            List<Predicate> predicateList = new ArrayList<>();
            if((jsonObject.containsKey("planName")&&jsonObject.get("planName") != null)){
                predicateList.add(cb.like(root.get("name"), "%"+jsonObject.getString("planName").trim()+"%"));
            }
            if(jsonObject.containsKey("testBeginTime")&&jsonObject.getDate("testBeginTime") != null){
                predicateList.add(cb.greaterThanOrEqualTo(root.get("testBeginTime"),jsonObject.getDate("testBeginTime")));
            }
            if(jsonObject.containsKey("testEndTime")&&jsonObject.getDate("testEndTime") != null){
                predicateList.add(cb.lessThanOrEqualTo(root.get("testEndTime"),jsonObject.getDate("testEndTime")));
            }
            if((jsonObject.containsKey("status")&&jsonObject.get("status") != null ) &&((jsonObject.getInteger("status") == Constant.TEST_PLAN_STATUS_END) ||(jsonObject.getInteger("status") == Constant.TEST_PLAN_STATUS_STOP))){
                predicateList.add(cb.equal(root.get("status"),jsonObject.getInteger("status")));
            }
            if((jsonObject.containsKey("institutionNo")&& StringUtils.isNotBlank(jsonObject.getString("institutionNo")))){
                predicateList.add(cb.equal(root.get("institutionNo"),jsonObject.getString("institutionNo")));
            }
            query.where(cb.and(predicateList.toArray(new Predicate[0])));
            query.orderBy(cb.desc(root.get("updateTime")));
            return query.getRestriction();
        };
        return  this.repository.findAll(specification, PageRequest.of(pageNum-1,pageSize));
    }

    /**
     * 保存
     *
     * @param hisTestPlanEntry
     */
    @Override
    public void save(HisTestPlanEntry hisTestPlanEntry) {
        this.repository.save(hisTestPlanEntry);
    }

    /**
     * find by id
     *
     * @param testPlanId
     * @return
     */
    @Override
    public HisTestPlanEntry findById(String testPlanId) {
        return this.repository.findByPlanId(testPlanId);
    }

    /**
     * 获取到历史测试记录
     * @param institutionNo 机构编号
     * @return
     */
    @Override
    public List<HisTestPlanEntry> findByInstitutionNo(String institutionNo) {
        return repository.findAllByInstitutionNo(institutionNo);
    }
}
