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.Cacheable;
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.QueryService;
import com.ecep.contract.SpringApp;
import com.ecep.contract.constant.ServiceConstant;
import com.ecep.contract.ds.other.service.EmployeeService;
import com.ecep.contract.ds.project.repository.CustomerSatisfactionSurveyRepository;
import com.ecep.contract.ds.customer.model.CustomerSatisfactionSurvey;
import com.ecep.contract.model.Employee;
import com.ecep.contract.ds.project.model.Project;
import com.ecep.contract.service.VoableService;
import com.ecep.contract.util.SpecificationUtils;
import com.ecep.contract.vo.CustomerSatisfactionSurveyVo;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * 项目客户满意度调查服务
 */
@Lazy
@Service
@CacheConfig(cacheNames = "customer-satisfaction-survey")
public class CustomerSatisfactionSurveyService
        implements IEntityService<CustomerSatisfactionSurvey>, QueryService<CustomerSatisfactionSurveyVo>,
        VoableService<CustomerSatisfactionSurvey, CustomerSatisfactionSurveyVo> {
    @Lazy
    @Autowired
    private CustomerSatisfactionSurveyRepository repository;

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

    /**
     * 根据ID查找客户满意度调查
     *
     * @param id ID
     * @return 客户满意度调查实体
     */
    @Cacheable(key = "#p0")
    @Override
    public CustomerSatisfactionSurveyVo findById(Integer id) {
        CustomerSatisfactionSurvey survey = getById(id);
        if (survey != null) {
            return survey.toVo();
        }
        return 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<CustomerSatisfactionSurveyVo> findAll(Specification<CustomerSatisfactionSurvey> spec) {
        List<CustomerSatisfactionSurvey> entities = repository.findAll(spec);
        return entities.stream().map(CustomerSatisfactionSurvey::toVo).toList();
    }

    @Override
    public Page<CustomerSatisfactionSurveyVo> findAll(JsonNode paramsNode, Pageable pageable) {
        Specification<CustomerSatisfactionSurvey> spec = null;
        if (paramsNode.has(ServiceConstant.KEY_SEARCH_TEXT)) {
            spec = getSpecification(paramsNode.get(ServiceConstant.KEY_SEARCH_TEXT).asText());
        }

        if (paramsNode.has("project.customer")) {
            int customerId = paramsNode.get("project.customer").asInt();
            spec = SpecificationUtils.and(spec, (root, query, builder) -> {
                return builder.equal(root.get("project").get("customer").get("id"), customerId);
            });
        }
        // field
        spec = SpecificationUtils.andParam(spec, paramsNode, "project", "applicant");
        Page<CustomerSatisfactionSurvey> page = findAll(spec, pageable);
        return page.map(CustomerSatisfactionSurvey::toVo);
    }

    @Override
    public void updateByVo(CustomerSatisfactionSurvey model, CustomerSatisfactionSurveyVo vo) {
        model.setCode(vo.getCode());
        model.setDate(vo.getDate());
        model.setTotalScore(vo.getTotalScore());
        model.setData(vo.getData());
        model.setApplyTime(vo.getApplyTime());
        model.setDescription(vo.getDescription());

        // 处理关联对象
        if (vo.getProject() != null) {
            Project project = SpringApp.getBean(ProjectService.class).getById(vo.getProject());
            model.setProject(project);
        }

        if (vo.getApplicantId() != null) {
            Employee employee = SpringApp.getBean(EmployeeService.class).getById(vo.getApplicantId());
            model.setApplicant(employee);
        }

        // active属性在实体中不存在，忽略
    }
}
