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

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.constant.ServiceConstant;
import com.ecep.contract.ds.vendor.repository.VendorClassRepository;
import com.ecep.contract.model.VendorCatalog;
import com.ecep.contract.service.ServiceException;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.VendorCatalogVo;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 供应商目录服务类
 * 提供对供应商目录的查询、创建、更新和删除操作
 */
@Lazy
@Service
@CacheConfig(cacheNames = "vendor-catalog")
public class VendorCatalogService implements IEntityService<VendorCatalog>, QueryService<VendorCatalogVo>,
        VoableService<VendorCatalog, VendorCatalogVo> {
    @Lazy
    @Autowired
    private VendorClassRepository repository;

    @Override
    public Page<VendorCatalogVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<VendorCatalog> spec = null;
        if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) {
            spec = getSpecification(paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText());
        }

        // field
        spec = SpecificationUtils.andFieldEqualParam(spec, paramsNode, "name", "code", "parentId");
        return findAll(spec, pageable).map(VendorCatalog::toVo);
    }

    @Override
    public VendorCatalog getById(Integer id) {
        return repository.findById(id).orElse(null);
    }

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

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

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

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

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

    @Override
    public void updateByVo(VendorCatalog model, VendorCatalogVo vo) {
        if (model == null) {
            throw new ServiceException("VendorCatalog cannot be null");
        }
        if (vo == null) {
            throw new ServiceException("VendorCatalogVo cannot be null");
        }
        model.setName(vo.getName());
        model.setCode(vo.getCode());
        model.setType(vo.getType());
    }
}
