package com.ecep.contract.service;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.ecep.contract.util.ParamUtils;
import com.ecep.contract.vm.DeliverySignMethodViewModel;
import com.ecep.contract.vo.DeliverySignMethodVo;
import com.ecep.contract.vo.ProjectSaleTypeVo;

import java.util.List;

@Service
@CacheConfig(cacheNames = "delivery-sign-method")
public class DeliverySignMethodService extends QueryService<DeliverySignMethodVo, DeliverySignMethodViewModel> {

    @Cacheable(key = "#id")
    @Override
    public DeliverySignMethodVo findById(Integer id) {
        return super.findById(id);
    }

    /**
     * 根据销售类型和编码查询发货方式
     *
     * @param saleType
     * @param code
     * @return
     */
    public DeliverySignMethodVo findBySaleTypeAndCode(ProjectSaleTypeVo saleType, String code) {
        Page<DeliverySignMethodVo> page = findAll(ParamUtils.builder()
                .equals("saleType", saleType.getId()).build(), Pageable.unpaged());
        if (page.isEmpty()) {
            return null;
        }
        return page.stream().filter(v -> v.getCode().equals(code)).findFirst().orElse(null);
    }


    @Cacheable(key = "'all'")
    @Override
    public List<DeliverySignMethodVo> findAll() {
        return super.findAll();
    }

    @CacheEvict(allEntries = true)
    @Override
    public DeliverySignMethodVo save(DeliverySignMethodVo entity) {
        return super.save(entity);
    }

    @CacheEvict(allEntries = true)
    @Override
    public void delete(DeliverySignMethodVo entity) {
        super.delete(entity);
    }
}
