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

import com.liangjian.dataplatform.member.dao.primary.FeatureInfoRepository;
import com.liangjian.dataplatform.member.dao.primary.FeatureLogRepository;
import com.liangjian.dataplatform.member.entity.po.primary.FeatureInfo;
import com.liangjian.dataplatform.member.entity.po.primary.FeatureLog;
import com.liangjian.dataplatform.member.service.IFeatureInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
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.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/24 9:05
 */
@Service
public class FeatureInfoService implements IFeatureInfoService {
    @Autowired
    private FeatureInfoRepository featureInfoRepository;
    @Autowired
    private FeatureLogRepository featureLogRepository;
    @Value("${supersetReplace}")
    private String supersetReplace;
    @Value("${supersetReplaceAs}")
    private String supersetReplaceAs;
    @Autowired
    Environment environment;

    @Override
    public Page<FeatureInfo> queryByNameLike(String name,Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page,size, Sort.Direction.DESC,"lastUpdate");
        if(name == null || name.isEmpty()) {
            return featureInfoRepository.findAll(pageable);
        }
        return featureInfoRepository.queryByNameLike("%"+name+"%", pageable);
    }

    @Override
    public FeatureInfo queryById(String id) {
        return featureInfoRepository.queryById(id);
    }


    private void commonReplayUrl(FeatureInfo featureInfo) {
        String url = featureInfo.getUrl();
        String targetStr = supersetReplaceAs;
        List<String> supersetReplaces = Arrays.stream(supersetReplace.split(","))
                .map(i -> i.trim())
                .filter(i -> !i.isEmpty())
                .collect(Collectors.toList());
        for (String str : supersetReplaces) {
            url = url.replace(str, targetStr);
        }
        featureInfo.setUrl(url);
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean addFeaturesInfo(FeatureInfo featuresInfo) {
        if(featuresInfo.getId() != null){
            featuresInfo.setId(null);
        }
        commonReplayUrl(featuresInfo);
        featuresInfo.setCrtDate(new Date());
        featuresInfo.setLastUpdate(new Date());
        try{
            featureInfoRepository.saveWithCycle(featuresInfo);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean saveFeaturesInfo(FeatureInfo featuresInfo) {
        if(featuresInfo.getId() == null) {
            return false;
        }
        commonReplayUrl(featuresInfo);
        featuresInfo.setLastUpdate(new Date());
        try{
            featureInfoRepository.saveWithCycle(featuresInfo);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteFeaturesInfo(String id) {
        try{
            featureInfoRepository.deleteById(id);
            List<FeatureLog> logs = featureLogRepository.queryByInfoId(id);
            featureLogRepository.deleteInBatch(logs);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }
}
