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

import java.util.List;

import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.ContractVo;
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.PurchaseBillVoucherItemRepository;
import com.ecep.contract.ds.vendor.model.PurchaseBillVoucherItem;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.PurchaseBillVoucherItemVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "purchase-bill-voucher-item")
public class PurchaseBillVoucherItemService
        implements IEntityService<PurchaseBillVoucherItem>, QueryService<PurchaseBillVoucherItem>,
        VoableService<PurchaseBillVoucherItem, PurchaseBillVoucherItemVo> {
    @Lazy
    @Autowired
    private PurchaseBillVoucherItemRepository repository;

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

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

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

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

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

        spec = SpecificationUtils.andParam(spec, paramsNode, "voucher");
        return findAll(spec, pageable);
    }

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

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

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

    @Override
    public void updateByVo(PurchaseBillVoucherItem model, PurchaseBillVoucherItemVo vo) {
        model.setId(vo.getId());
        model.setRefId(vo.getRefId());

        // 关联实体ID设置
        // voucher, orderItem, invoice, inventory, contract等实体需要在service层进行设置
        // 这里只设置基本属性，关联实体的设置需要在调用此方法的地方处理

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

    public List<PurchaseBillVoucherItem> findAllByContract(ContractVo contract) {
        return repository.findAllByContractId(contract.getId());
    }
}
