package cn.axzo.workflow.core.service.impl;

import cn.axzo.workflow.core.repository.entity.ExtAxProperty;
import cn.axzo.workflow.core.repository.mapper.ExtAxPropertyMapper;
import cn.axzo.workflow.core.service.ExtAxPropertyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * 引擎服务持久配置信息表操作 Service 实现
 *
 * @author wangli
 * @since 2024/2/6 16:20
 */
@Service
@Slf4j
public class ExtAxPropertyServiceImpl implements ExtAxPropertyService {
    @Resource
    private ExtAxPropertyMapper mapper;

    @CacheEvict(value = "property", key = "#property.name")
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ExtAxProperty add(ExtAxProperty property) {
        mapper.insert(property);
        return property;
    }

    @CachePut(value = "property", key = "#property.name")
    @Override
    public ExtAxProperty update(ExtAxProperty property) {
        mapper.updateById(property);
        return property;
    }

    @Cacheable(value = "property", key = "#name")
    @Override
    public Optional<ExtAxProperty> getByName(String name) {
        if (!StringUtils.hasText(name)) {
            return Optional.empty();
        }
        QueryWrapper<ExtAxProperty> queryWrapper = new QueryWrapper<ExtAxProperty>()
                .eq("name", name);
        Optional<ExtAxProperty> extAxProperty = Optional.ofNullable(mapper.selectOne(queryWrapper));
        return extAxProperty;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int delete(String name, String value) {
        if (!StringUtils.hasText(name)) {
            return 0;
        }
        Map<String, Object> deleteMap = new HashMap<>();
        deleteMap.put("name", name);
        if (StringUtils.hasText(value)) {
            deleteMap.put("value", value);
        }
        return mapper.deleteByMap(deleteMap);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int deleteByNameWithDuration(String name, Long timeOut, TimeUnit timeUnit) {
        if (!StringUtils.hasText(name) || timeOut == null || timeUnit == null) {
            log.warn("argument not valid,name:{},timeOut:{},timeUnit:{}", name, timeOut, timeUnit);
            throw new IllegalArgumentException("argument not valid");
        }
        LocalDateTime startTime = LocalDateTime.now().minus(timeUnit.toMillis(timeOut), ChronoUnit.MILLIS);
        QueryWrapper<ExtAxProperty> queryWrapper = new QueryWrapper<ExtAxProperty>()
                .eq("name", name)
                .le("create_at", startTime);
        return mapper.delete(queryWrapper);
    }
}
