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

import java.util.List;
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.ds.other.repository.InventoryCatalogRepository;
import com.ecep.contract.model.InventoryCatalog;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.InventoryCatalogVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "inventory-catalog")
public class InventoryCatalogService implements IEntityService<InventoryCatalog>, QueryService<InventoryCatalogVo>,
        VoableService<InventoryCatalog, InventoryCatalogVo> {
    @Lazy
    @Autowired
    private InventoryCatalogRepository repository;

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

    @Cacheable(key = "'code-'+#p0")
    public InventoryCatalog findByCode(String code) {
        return repository.findByCode(code).orElse(null);
    }

    @Cacheable(key = "'name-'+#p0")
    public InventoryCatalog findByName(String name) {
        return repository.findByName(name).orElse(null);
    }

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

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

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

    public List<InventoryCatalog> search(String searchText) {
        Specification<InventoryCatalog> spec = (root, query, builder) -> {
            return builder.or(builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("name"), "%" + searchText + "%"));
        };
        return repository.findAll(spec, Pageable.ofSize(10)).getContent();
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'code-'+#p0.code"),
            @CacheEvict(key = "'name-'+#p0.code")
    })
    public InventoryCatalog save(InventoryCatalog entity) {
        return repository.save(entity);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'code-'+#p0.code"),
            @CacheEvict(key = "'name-'+#p0.code")
    })
    @Override
    public void delete(InventoryCatalog entity) {
        repository.delete(entity);
    }

    @Override
    public void updateByVo(InventoryCatalog model, InventoryCatalogVo vo) {
        model.setCode(vo.getCode());
        model.setName(vo.getName());
        // 其他属性根据实际需要添加
    }
}
