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.ds.contract.repository.SalesOrderRepository;
import com.ecep.contract.model.Contract;
import com.ecep.contract.model.SalesOrder;

/**
 * 合同服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "contract-sale-order")
public class SaleOrdersService implements IEntityService<SalesOrder> {
    private static final Logger logger = LoggerFactory.getLogger(SaleOrdersService.class);

    @Lazy
    @Autowired
    private SalesOrderRepository salesOrderRepository;

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

    @Override
    public Specification<SalesOrder> 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 + "%"));
        };
    }

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

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

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

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

    public long count(Specification<SalesOrder> spec) {
        return salesOrderRepository.count(spec);
    }

    public Page<SalesOrder> findAll(Specification<SalesOrder> spec, Pageable pageable) {
        return salesOrderRepository.findAll(spec, pageable);
    }

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

    public List<SalesOrder> findAllByContract(Contract contract) {
        return salesOrderRepository.findAllByContract(contract);
    }

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