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

import java.util.List;

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.SalesBillVoucherItemRepository;
import com.ecep.contract.ds.customer.model.SalesBillVoucherItem;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.SalesBillVoucherItemVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "sales-bill-voucher-item")
public class SalesBillVoucherItemService
        implements IEntityService<SalesBillVoucherItem>, QueryService<SalesBillVoucherItemVo>,
        VoableService<SalesBillVoucherItem, SalesBillVoucherItemVo> {
    @Lazy
    @Autowired
    private SalesBillVoucherItemRepository repository;

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

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

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

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

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

    @Override
    public Page<SalesBillVoucherItemVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<SalesBillVoucherItem> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        return findAll(spec, pageable).map(SalesBillVoucherItem::toVo);
    }

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

    @Caching(evict = {
            @CacheEvict(key = "'item-'+#p0.id")
    })
    @Override
    public SalesBillVoucherItem save(SalesBillVoucherItem entity) {
        return repository.save(entity);
    }

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

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

    @Override
    public void updateByVo(SalesBillVoucherItem model, SalesBillVoucherItemVo vo) {
        model.setId(vo.getId());

        // 这里只设置基本属性，关联实体的设置需要在调用此方法的地方处理

        model.setQuantity(vo.getQuantity());
        model.setPrice(vo.getPrice());
        model.setDescription(vo.getDescription());
    }
}