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

import com.liangjian.dataplatform.member.dao.primary.JobInfoRepository;
import com.liangjian.dataplatform.member.dao.primary.TagRuleRepository;
import com.liangjian.dataplatform.member.entity.po.primary.TagRule;
import com.liangjian.dataplatform.member.entity.vo.TagRuleVo;
import com.liangjian.dataplatform.member.service.ITagRuleService;
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.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/9 15:21
 */
@Service
public class TagRuleService implements ITagRuleService {

    @Autowired
    private TagRuleRepository tagRuleRepository;
    @Autowired
    private JobInfoRepository jobInfoRepository;

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

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

            if(object != null) {
                list.add(criteriaBuilder.equal(root.get("object"),object));
            }
            if(name != null && !name.isEmpty()) {
                list.add(criteriaBuilder.like(root.get("name").as(String.class), "%"+name+"%"));
            }
            if(isJobActive != null) {
                List<String> jobIds = jobInfoRepository.queryByActive(isJobActive).stream()
                        .map(i -> i.getId()).collect(Collectors.toList());
                list.add(root.get("job_id").in(jobIds));
            }
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
        Page<TagRule> tagRules = tagRuleRepository.findAll(specification,pageable);
        Page<TagRuleVo> res = tagRules.map(i -> TagRuleVo.transfer(i));
        return res;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean addTagRule(TagRuleVo vo) {
        vo.setCrtDate(new Date());
        vo.setLastUpdate(new Date());
        TagRule po = TagRuleVo.reTanslate(vo);
        try{
            tagRuleRepository.saveWithCycle(po);
            return true;
        } catch (Exception ex){
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveTagRule(TagRuleVo vo) {
        vo.setLastUpdate(new Date());
        TagRule po = TagRuleVo.reTanslate(vo);
        try{
            tagRuleRepository.saveWithCycle(po);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

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


    @Override
    public Page<TagRule> queryByTagList( Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page,size, Sort.Direction.DESC,"id");
        Specification<TagRule> specification = (Specification<TagRule>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
        Page<TagRule> tagRules = tagRuleRepository.findAll(specification,pageable);
        return tagRules;
    }
}
