package com.liangjian.dataplatform.member.service.impl;

import com.liangjian.dataplatform.member.dao.primary.JobInfoRepository;
import com.liangjian.dataplatform.member.entity.po.primary.JobInfo;
import com.liangjian.dataplatform.member.service.IJobInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Author: 黄子奇
 * @Date: 2019/9/9 16:42
 */
@Service
public class JobInfoService implements IJobInfoService {

    @Autowired
    private JobInfoRepository jobInfoRepository;

    @Override
    public Page<JobInfo> queryByCondition(String name, Integer scheduleType, Boolean isActive, Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page,size, Sort.Direction.DESC,"id");

        Specification<JobInfo> specification = (Specification<JobInfo>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();

            if(name != null && !name.isEmpty()) {
                list.add(criteriaBuilder.like(root.get("name").as(String.class), "%"+name+"%"));
            }
            if(scheduleType != null) {
                list.add(criteriaBuilder.equal(root.get("repeat_type"), scheduleType));
            }
            if(isActive != null) {
                list.add(criteriaBuilder.equal(root.get("active"),isActive));
            }
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
        Page<JobInfo> res = jobInfoRepository.findAll(specification,pageable);
        return res;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean addJobInfo(JobInfo jobInfo) {
        jobInfo.setCrtDate(new Date());
        jobInfo.setLastUpdate(new Date());
        try{
            jobInfoRepository.saveWithCycle(jobInfo);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveJobInfo(JobInfo jobInfo) {
        jobInfo.setLastUpdate(new Date());
        try{
            jobInfoRepository.saveWithCycle(jobInfo);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean delete(String id) {
        try{
            jobInfoRepository.deleteById(id);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public JobInfo queryById(String id) {
        return jobInfoRepository.queryById(id);
    }
}
