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

import java.util.List;

import com.ecep.contract.vo.ProjectSaleTypeVo;
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.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.ecep.contract.ContractFileType;
import com.ecep.contract.IEntityService;
import com.ecep.contract.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.ds.project.repository.ProjectSaleTypeRequireFileTypeRepository;
import com.ecep.contract.ds.project.service.ProjectSaleTypeService;
import com.ecep.contract.model.ProjectSaleType;
import com.ecep.contract.model.ProjectSaleTypeRequireFileType;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.ProjectSaleTypeRequireFileTypeVo;
import com.fasterxml.jackson.databind.JsonNode;

@Lazy
@Service
@CacheConfig(cacheNames = "project-sale-type-require-file-type")
public class ProjectSaleTypeRequireFileTypeService
        implements IEntityService<ProjectSaleTypeRequireFileType>, QueryService<ProjectSaleTypeRequireFileTypeVo>,
        VoableService<ProjectSaleTypeRequireFileType, ProjectSaleTypeRequireFileTypeVo> {
    @Lazy
    @Autowired
    private ProjectSaleTypeRequireFileTypeRepository repository;

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

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

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

    @Cacheable(key = "'bySaleType-'+#p0")
    public List<ProjectSaleTypeRequireFileTypeVo> findBySaleType(ProjectSaleTypeVo saleType) {
        return repository.findBySaleTypeId(saleType.getId()).stream().map(ProjectSaleTypeRequireFileType::toVo).toList();
    }

    @Cacheable(key = "'by-sale-type-'+#p0.id")
    public List<ProjectSaleTypeRequireFileTypeVo> findBySaleType(ProjectSaleType saleType) {
        return repository.findBySaleType(saleType).stream().map(ProjectSaleTypeRequireFileType::toVo).toList();
    }

    // findAll
    @Cacheable(key = "'all'")
    public List<ProjectSaleTypeRequireFileType> findAll() {
        return repository.findAll();
    }

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

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

    // save
    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'bySaleType-'+#p0.saleType.id"),
            @CacheEvict(key = "'by-sale-type-'+#p0.saleType.id"),
            @CacheEvict(key = "'all'")
    })
    public ProjectSaleTypeRequireFileType save(ProjectSaleTypeRequireFileType type) {
        return repository.save(type);
    }

    /**
     * delete and evict cache
     */
    @Caching(evict = {
            @CacheEvict(key = "#p0.id"),
            @CacheEvict(key = "'all'")
    })
    public void delete(ProjectSaleTypeRequireFileType type) {
        repository.delete(type);
    }

    @Override
    public void updateByVo(ProjectSaleTypeRequireFileType model, ProjectSaleTypeRequireFileTypeVo vo) {
        if (model == null || vo == null) {
            return;
        }

        // 设置基本属性
        model.setFileType(vo.getFileType());

        // 转换frequency从String到ContractFileType.Frequency枚举
        if (vo.getFrequency() != null) {
            try {
                model.setFrequency(ContractFileType.Frequency.valueOf(vo.getFrequency()));
            } catch (IllegalArgumentException e) {
                // 如果枚举值不存在，保持原来的值
            }
        }

        // 处理关联实体
        if (vo.getSaleTypeId() == null) {
            model.setSaleType(null);
        } else {
            ProjectSaleTypeService projectSaleTypeService = SpringApp.getBean(ProjectSaleTypeService.class);
            if (model.getSaleType() == null || !model.getSaleType().getId().equals(vo.getSaleTypeId())) {
                model.setSaleType(projectSaleTypeService.getById(vo.getSaleTypeId()));
            }
        }
    }
}
