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.SalesBillVoucherItemRepository;
import com.ecep.contract.ds.contract.repository.SalesBillVoucherRepository;
import com.ecep.contract.model.SalesBillVoucher;
import com.ecep.contract.model.SalesBillVoucherItem;

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

    @Lazy
    @Autowired
    private SalesBillVoucherRepository salesBillVoucherRepository;
    @Lazy
    @Autowired
    private SalesBillVoucherItemRepository salesBillVoucherItemRepository;


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

    @Override
    public Specification<SalesBillVoucher> 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 SalesBillVoucher findByCode(String code) {
        return salesBillVoucherRepository.findByCode(code).orElse(null);
    }

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

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

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

    public long count(Specification<SalesBillVoucher> spec) {
        return salesBillVoucherRepository.count(spec);
    }

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

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

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


    @Cacheable(key = "'item-'+#p0")
    public SalesBillVoucherItem findItemById(Integer id) {
        return salesBillVoucherItemRepository.findById(id).orElse(null);
    }

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

    @Caching(
            evict = {
                    @CacheEvict(key = "'item-'+#p0.id")
            }
    )
    public void delete(SalesBillVoucherItem order) {
        salesBillVoucherItemRepository.delete(order);
    }

    public long countItems() {
        return salesBillVoucherItemRepository.count();
    }

    public long countItems(Specification<SalesBillVoucherItem> spec) {
        return salesBillVoucherItemRepository.count(spec);
    }

    public Page<SalesBillVoucherItem> findAllItems(Specification<SalesBillVoucherItem> spec, Pageable pageable) {
        return salesBillVoucherItemRepository.findAll(spec, pageable);
    }

    public List<SalesBillVoucherItem> findAllItems(Specification<SalesBillVoucherItem> spec, Sort sort) {
        return salesBillVoucherItemRepository.findAll(spec, sort);
    }
}
