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

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.util.StringUtils;

import com.ecep.contract.IEntityService;
import com.ecep.contract.ds.project.repository.CustomerSatisfactionSurveyRepository;
import com.ecep.contract.model.CustomerSatisfactionSurvey;

/**
 * 项目客户满意度调查服务
 */
@Lazy
@Service
public class CustomerSatisfactionSurveyService
        implements IEntityService<CustomerSatisfactionSurvey> {
    @Lazy
    @Autowired
    private CustomerSatisfactionSurveyRepository repository;

    /**
     * 根据ID查找客户满意度调查
     *
     * @param id ID
     * @return 客户满意度调查实体
     */
    public CustomerSatisfactionSurvey findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Specification<CustomerSatisfactionSurvey> getSpecification(String searchText) {
        if (!StringUtils.hasText(searchText)) {
            return null;
        }
        return (root, query, builder) -> {
            return builder.like(root.get("code"), "%" + searchText + "%");
        };
    }

    ;

    /**
     * 保存客户满意度调查
     *
     * @param survey 客户满意度调查实体
     * @return 保存后的实体
     */
    public CustomerSatisfactionSurvey save(CustomerSatisfactionSurvey survey) {
        return repository.save(survey);
    }

    /**
     * 删除客户满意度调查
     *
     * @param survey 客户满意度调查实体
     */
    public void delete(CustomerSatisfactionSurvey survey) {
        repository.delete(survey);
    }

    /**
     * 根据条件查找所有客户满意度调查
     *
     * @param spec     查询条件
     * @param pageable 分页参数
     * @return 客户满意度调查分页列表
     */
    public Page<CustomerSatisfactionSurvey> findAll(Specification<CustomerSatisfactionSurvey> spec, Pageable pageable) {
        return repository.findAll(spec, pageable);
    }

    /**
     * 根据条件查找所有客户满意度调查
     *
     * @param spec 查询条件
     * @return 客户满意度调查列表
     */
    public List<CustomerSatisfactionSurvey> findAll(Specification<CustomerSatisfactionSurvey> spec) {
        return repository.findAll(spec);
    }
}
