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 com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.ds.contract.repository.PurchaseOrderRepository;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.PurchaseOrder;
import com.ecep.contract.util.SpecificationUtils;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 采购订单服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "contract-purchase-order")
public class PurchaseOrdersService implements IEntityService<PurchaseOrder>, QueryService<PurchaseOrder> {
    private static final Logger logger = LoggerFactory.getLogger(PurchaseOrdersService.class);

    @Lazy
    @Autowired
    private PurchaseOrderRepository purchaseOrderRepository;

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

    @Override
    public Specification<PurchaseOrder> getSpecification(String searchText) {
        return (root, query, builder) -> {
            return builder.or(
                    builder.like(root.get("code"), "%" + searchText + "%"),
                    builder.like(root.get("description"), "%" + searchText + "%"));
        };
    }

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

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

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

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

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

    public long count(Specification<PurchaseOrder> spec) {
        return purchaseOrderRepository.count(spec);
    }

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

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

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

    public List<PurchaseOrder> findAllByContract(Contract contract) {
        return purchaseOrderRepository.findAllByContract(contract);
    }

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