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

import com.liangjian.dataplatform.member.dao.secondary.PointGrowthRuleRepository;
import com.liangjian.dataplatform.member.entity.po.secondary.PointGrowthRule;
import com.liangjian.dataplatform.member.entity.vo.request.BirdGirdRequest;
import com.liangjian.dataplatform.member.entity.vo.response.BirdGirdTableResponse2;
import com.liangjian.dataplatform.member.service.BirdGirdQueryRequest;
import com.liangjian.dataplatform.member.service.IPointGrowthRuleService;
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 javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 黄子奇
 * @Date: 2019/11/18
 */
@Service
public class PointGrowthRuleService implements IPointGrowthRuleService, BirdGirdQueryRequest<PointGrowthRule> {

    @Autowired
    private PointGrowthRuleRepository pointGrowthRuleRepository;

    @Override
    public Page<PointGrowthRule> query(String terminalCode, String terminalName, String action, Boolean running, Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page,size, Sort.Direction.DESC,"crtDate");
        Specification<PointGrowthRule> specification =  (Specification<PointGrowthRule>) (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> list = new ArrayList<>();
            if(terminalCode != null) {
                list.add(criteriaBuilder.equal(root.get("terminal_code").as(String.class), terminalCode));
            }
            if(terminalName != null) {
                list.add(criteriaBuilder.like(root.get("terminal_name").as(String.class), "%"+terminalName+"%"));
            }
            if(action != null) {
                list.add(criteriaBuilder.like(root.get("action").as(String.class), "%"+action+"%"));
            }
            if(running != null) {
                list.add(criteriaBuilder.equal(root.get("running").as(Boolean.class), running));
            }
            return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
        };
        Page<PointGrowthRule> rules = pointGrowthRuleRepository.findAll(specification,pageable);
        return rules;
    }

    @Override
    public BirdGirdTableResponse2<PointGrowthRule> queryByCondition(BirdGirdRequest request) {
        Integer page = request.getPageIndex();
        Integer size = request.getPageSize();
        Pageable pageable = PageRequest.of(page,size, Sort.Direction.DESC,request.getSortField());

        Specification<PointGrowthRule> specification = (root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> where = buildConditionByFilters(request,root,criteriaBuilder);
            return criteriaBuilder.and(where.toArray(new Predicate[where.size()]));
        };
        Page<PointGrowthRule> paged = pointGrowthRuleRepository.findAll(specification,pageable);
        return new BirdGirdTableResponse2<>(paged.getContent(),paged.getTotalElements());
//        return null;
    }

    @Override
    public boolean addPointGrowthRule(PointGrowthRule pointGrowthRule) {
        if(pointGrowthRule.getId() != null) {
            return false;
        }

        try {
            pointGrowthRuleRepository.saveWithCycle(pointGrowthRule);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean savePointGrowthRule(PointGrowthRule pointGrowthRule) {
        if(pointGrowthRule.getId() == null) {
            return false;
        }

        try {
            pointGrowthRuleRepository.saveWithCycle(pointGrowthRule);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deletePointGrowthRule(String id) {
        try {
            pointGrowthRuleRepository.deleteById(id);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }


}