package com.vote.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vote.system.domain.VoteEventWorkSettings;
import com.vote.system.domain.VoteJudgesVisibleFields;
import com.vote.system.domain.VoteSubmissionForm;
import com.vote.system.mapper.VoteEventWorkSettingsMapper;
import com.vote.system.mapper.VoteSubmissionFormMapper;
import com.vote.system.service.VoteJudgesVisibleFieldsService;
import com.vote.system.mapper.VoteJudgesVisibleFieldsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author MacBook
 * @description 针对表【vote_judges_visible_fields(专家可见信息配置表)】的数据库操作Service实现
 * @createDate 2024-08-26 20:15:36
 */
@Service
public class VoteJudgesVisibleFieldsServiceImpl extends ServiceImpl<VoteJudgesVisibleFieldsMapper, VoteJudgesVisibleFields>
        implements VoteJudgesVisibleFieldsService {

    @Autowired
    private VoteJudgesVisibleFieldsMapper voteJudgesVisibleFieldsMapper;

    @Autowired
    private VoteSubmissionFormMapper voteSubmissionFormMapper;

    @Autowired
    private VoteEventWorkSettingsMapper voteEventWorkSettingsMapper;

    @Override
    public List<Map<String, String>> selectByWorkSettingsId(Long workSettingsId) {
        VoteEventWorkSettings settings = voteEventWorkSettingsMapper.selectById(workSettingsId);
        if (settings != null) {
            VoteSubmissionForm submissionForm = voteSubmissionFormMapper.selectById(settings.getSubmissionTemplateId());
            if (submissionForm != null) {
                String formData = submissionForm.getFormData();
                return extractFieldInfo(formData); // 提取 vModel 和 label
            }
        }
        return Collections.emptyList();
    }



    // 提取 vModel 和 label 字段信息的逻辑
    private List<Map<String, String>> extractFieldInfo(String formData) {
        ObjectMapper objectMapper = new ObjectMapper();
        List<Map<String, Object>> formDataList;
        List<Map<String, String>> result = new ArrayList<>();

        try {
            // 解析 JSON 数据
            formDataList = objectMapper.readValue(formData, new TypeReference<List<Map<String, Object>>>() {});

                // 遍历列表，提取 vModel 和 label 字段
            for (Map<String, Object> field : formDataList) {
                Map<String, String> fieldInfo = new HashMap<>();
                if (field.containsKey("vModel")) {
                    fieldInfo.put("vModel", (String) field.get("vModel"));
                }
                if (field.containsKey("label")) {
                    fieldInfo.put("label", (String) field.get("label"));
                }
                if (!fieldInfo.isEmpty()) {
                    result.add(fieldInfo); // 将包含 vModel 和/或 label 的字段信息添加到结果中
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("解析 formData 字段失败", e);
        }

        return result;
    }
}




