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

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.domain.Sort;
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.contract.repository.PurchaseBillVoucherRepository;
import com.ecep.contract.model.PurchaseBillVoucher;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 采购账单凭证服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "purchase-bill-voucher")
public class PurchaseBillVoucherService
        implements IEntityService<PurchaseBillVoucher>, QueryService<PurchaseBillVoucher> {
    private static final Logger logger = LoggerFactory.getLogger(PurchaseBillVoucherService.class);

    @Lazy
    @Autowired
    private PurchaseBillVoucherRepository repository;

    @Override
    @Cacheable(key = "#p0")
    public PurchaseBillVoucher findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Specification<PurchaseBillVoucher> getSpecification(String searchText) {
        // if null
        if (!StringUtils.hasText(searchText)) {
            return null;
        }

        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%"));
        };
    }

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

    @Cacheable(key = "'refId-'+#p0")
    public PurchaseBillVoucher findByRefId(Integer refId) {
        return repository.findByRefId(refId).orElse(null);
    }

    /**
     * 保存实体对象，异步方法
     *
     * @param voucher 要保存的实体对象
     * @return 返回异步调用
     */
    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'code-'+#p0.code"),
            @CacheEvict(key = "'refId-'+#p0.refId")
    })
    public PurchaseBillVoucher save(PurchaseBillVoucher voucher) {
        return repository.save(voucher);
    }

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

    public long count() {
        return repository.count();
    }

    public long count(Specification<PurchaseBillVoucher> spec) {
        return repository.count(spec);
    }

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

    @Override
    public Page<PurchaseBillVoucher> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<PurchaseBillVoucher> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "company", "inventory");
        return findAll(spec, pageable);
    }

    public List<PurchaseBillVoucher> findAll(Specification<PurchaseBillVoucher> spec, Sort sort) {
        return repository.findAll(spec, sort);
    }

    public List<PurchaseBillVoucher> search(String searchText) {
        return repository.findAll(getSpecification(searchText), Pageable.ofSize(10)).getContent();
    }
}
