package com.ecep.contract.ds.project.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.SpringApp;
import com.ecep.contract.ds.project.repository.ProductDeliverySignMethodRepository;
import com.ecep.contract.model.DeliverySignMethod;
import com.ecep.contract.model.ProjectSaleType;
import com.ecep.contract.service.ServiceException;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.vo.DeliverySignMethodVo;
import com.ecep.contract.vo.ProjectSaleTypeVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "project-sign-method")
public class DeliverySignMethodService
        implements IEntityService<DeliverySignMethod>, QueryService<DeliverySignMethodVo>,
        VoableService<DeliverySignMethod, DeliverySignMethodVo> {
    @Lazy
    @Autowired
    private ProductDeliverySignMethodRepository deliverySignMethodRepository;

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

    @Cacheable(key = "#p0")
    @Override
    public DeliverySignMethodVo findById(Integer id) {
        DeliverySignMethod method = getById(id);
        if (method != null) {
            return method.toVo();
        }
        return null;
    }

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

    @Override
    public Page<DeliverySignMethodVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<DeliverySignMethod> spec = null;
        if (paramsNode.has("searchText")) {
            spec = getSpecification(paramsNode.get("searchText").asText());
        }
        // field
        // spec = SpecificationUtils.andParam(spec, paramsNode, "company");
        Page<DeliverySignMethod> page = findAll(spec, pageable);
        return page.map(DeliverySignMethod::toVo);
    }

    public DeliverySignMethod findBySaleTypeAndName(ProjectSaleType saleType, String name) {
        return deliverySignMethodRepository.findBySaleTypeAndName(saleType, name).orElse(null);
    }

    @Cacheable(key = "'type-'+#p0.id+'_code-'+#p1")
    public DeliverySignMethodVo findBySaleTypeAndCode(ProjectSaleType saleType, String code) {
        return deliverySignMethodRepository.findBySaleTypeAndCode(saleType, code).map(DeliverySignMethod::toVo).orElse(null);
    }

    @Cacheable(key = "'type-'+#p0.id+'_code-'+#p1")
    public DeliverySignMethodVo findBySaleTypeAndCode(ProjectSaleTypeVo saleType, String code) {
        return deliverySignMethodRepository.findBySaleTypeIdAndCode(saleType.getId(), code).map(DeliverySignMethod::toVo).orElse(null);
    }

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

    @Cacheable(key = "'all'")
    public List<DeliverySignMethodVo> findAll() {
        return deliverySignMethodRepository.findAll().stream().map(DeliverySignMethod::toVo).toList();
    }

    public List<DeliverySignMethod> findAllBySaleTypeId(Integer saleTypeId) {
        return deliverySignMethodRepository.findAllBySaleTypeId(saleTypeId);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            // #p0.saleType.id 可能未载入
            @CacheEvict(key = "'type-'+#p0.saleType.id+'_code-'+#p0.code"),
            @CacheEvict(key = "'all'"),
    })
    public DeliverySignMethod save(DeliverySignMethod usage) {
        return deliverySignMethodRepository.save(usage);
    }

    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            // #p0.saleType.id 可能未载入
            @CacheEvict(key = "'type-'+#p0.saleType.id+'_code-'+#p0.code"),
            @CacheEvict(key = "'all'"),
    })
    public void delete(DeliverySignMethod method) {
        deliverySignMethodRepository.delete(method);
    }

    @Override
    public Specification<DeliverySignMethod> getSpecification(String searchText) {
        return null;
    }

    @Override
    public void updateByVo(DeliverySignMethod entity, DeliverySignMethodVo vo) {
        if (entity == null) {
            throw new ServiceException("DeliverySignMethod is null");
        }
        if (vo == null) {
            throw new ServiceException("DeliverySignMethodVo is null");
        }

        entity.setName(vo.getName());
        entity.setCode(vo.getCode());
        entity.setDescription(vo.getDescription());

        if (vo.getSaleTypeId() != null) {
            ProjectSaleTypeService saleTypeService = SpringApp.getBean(ProjectSaleTypeService.class);
            entity.setSaleType(saleTypeService.getById(vo.getSaleTypeId()));
        } else {
            entity.setSaleType(null);
        }
    }

}
