package com.ecep.contract.ds.other.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.service.VoableService;
import com.fasterxml.jackson.databind.JsonNode;
import com.ecep.contract.ds.other.repository.FunctionRepository;
import com.ecep.contract.model.Function;
import com.ecep.contract.vo.FunctionVo;

@Lazy
@Service
@CacheConfig(cacheNames = "function")
public class FunctionService implements IEntityService<Function>, QueryService<FunctionVo>, VoableService<Function, FunctionVo> {
    @Lazy
    @Autowired
    private FunctionRepository repository;

    @Cacheable(key = "#p0")
    public FunctionVo findById(Integer id) {
        return repository.findById(id).map(Function::toVo).orElse(null);
    }
    
    public Function getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id")
    })
    public Function save(Function role) {
        return repository.save(role);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id")
    })
    public void delete(Function entity) {
        repository.delete(entity);
    }

    @Override
    public Page<Function> findAll(Specification<Function> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    @Override
    public Page<FunctionVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<Function> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // 可以根据需要添加更多参数处理
        return findAll(spec, pageable).map(Function::toVo);
    }

    @Override
    public Specification<Function> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("key"), "%" + searchText + "%"),
                    builder.like(root.get("name"), "%" + searchText + "%"),
                    builder.like(root.get("controller"), "%" + searchText + "%"),
                    builder.like(root.get("icon"), "%" + searchText + "%")
            );
        };
    }

    @Override
    public void updateByVo(Function function, FunctionVo vo) {
        function.setName(vo.getName());
        function.setKey(vo.getKey());
        function.setActive(vo.isActive());
        function.setController(vo.getController());
        function.setIcon(vo.getIcon());
        function.setDescription(vo.getDescription());
        // parentId和order字段在实体类中不存在，暂不处理
    }
}
