package com.qqchen.deploy.backend.workflow.service.impl;

import com.qqchen.deploy.backend.framework.exception.BusinessException;
import com.qqchen.deploy.backend.framework.service.impl.BaseServiceImpl;
import com.qqchen.deploy.backend.workflow.dto.FormDataDTO;
import com.qqchen.deploy.backend.workflow.dto.query.FormDataQuery;
import com.qqchen.deploy.backend.workflow.entity.FormData;
import com.qqchen.deploy.backend.workflow.entity.FormDefinition;
import com.qqchen.deploy.backend.workflow.enums.FormDataStatusEnums;
import com.qqchen.deploy.backend.workflow.repository.IFormDataRepository;
import com.qqchen.deploy.backend.workflow.repository.IFormDefinitionRepository;
import com.qqchen.deploy.backend.workflow.service.IFormDataService;
import com.qqchen.deploy.backend.workflow.converter.FormDataConverter;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 表单数据服务实现
 *
 * @author qqchen
 * @date 2025-10-24
 */
@Slf4j
@Service
public class FormDataServiceImpl extends BaseServiceImpl<FormData, FormDataDTO, FormDataQuery, Long> implements IFormDataService {

    @Resource
    private IFormDataRepository formDataRepository;

    @Resource
    private IFormDefinitionRepository formDefinitionRepository;

    @Resource
    private FormDataConverter formDataConverter;

    @Override
    @Transactional
    public FormDataDTO submit(FormDataDTO dto) {
        log.info("提交表单数据: formKey={}, submitter={}", dto.getFormKey(), dto.getSubmitter());

        // 1. 查询表单定义，获取schema快照
        FormDefinition formDefinition = formDefinitionRepository.findById(dto.getFormDefinitionId())
                .orElseThrow(() -> new BusinessException(com.qqchen.deploy.backend.framework.enums.ResponseCode.FORM_DEFINITION_NOT_FOUND));

        // 2. 创建表单数据
        FormData formData = formDataConverter.toEntity(dto);
        formData.setFormKey(formDefinition.getKey());
        formData.setFormVersion(formDefinition.getFormVersion());
        formData.setCategoryId(formDefinition.getCategoryId()); // 冗余存储分类ID
        formData.setSchemaSnapshot(formDefinition.getSchema()); // 保存schema快照
        formData.setSubmitTime(LocalDateTime.now());
        formData.setStatus(FormDataStatusEnums.SUBMITTED);

        // 3. 保存
        FormData savedFormData = formDataRepository.save(formData);
        log.info("表单数据提交成功: id={}, formKey={}", savedFormData.getId(), savedFormData.getFormKey());

        return formDataConverter.toDto(savedFormData);
    }

    @Override
    public FormDataDTO getByBusinessKey(String businessKey) {
        log.info("根据业务标识查询表单数据: businessKey={}", businessKey);

        FormData formData = formDataRepository.findByBusinessKeyAndDeletedFalse(businessKey)
                .orElseThrow(() -> new BusinessException(com.qqchen.deploy.backend.framework.enums.ResponseCode.FORM_DATA_NOT_FOUND));

        return formDataConverter.toDto(formData);
    }
}

