package com.fs.survey.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fs.survey.domain.SurveyFieldTypeMapping;
import com.fs.survey.mapper.SurveyFieldTypeMappingMapper;
import com.fs.survey.service.ISurveyFieldTypeMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 字段类型映射Service业务层处理
 *
 */
@Service
public class SurveyFieldTypeMappingServiceImpl implements ISurveyFieldTypeMappingService {
    @Autowired
    private SurveyFieldTypeMappingMapper surveyFieldTypeMappingMapper;

    /**
     * 查询字段类型映射
     *
     * @param id 字段类型映射主键
     * @return 字段类型映射
     */
    @Override
    public SurveyFieldTypeMapping selectSurveyFieldTypeMappingById(Long id) {
        return surveyFieldTypeMappingMapper.selectSurveyFieldTypeMappingById(id);
    }

    /**
     * 查询字段类型映射列表
     *
     * @param surveyFieldTypeMapping 字段类型映射
     * @return 字段类型映射
     */
    @Override
    public List<SurveyFieldTypeMapping> selectSurveyFieldTypeMappingList(SurveyFieldTypeMapping surveyFieldTypeMapping) {
        return surveyFieldTypeMappingMapper.selectSurveyFieldTypeMappingList(surveyFieldTypeMapping);
    }

    /**
     * 新增字段类型映射
     *
     * @param surveyFieldTypeMapping 字段类型映射
     * @return 结果
     */
    @Override
    public int insertSurveyFieldTypeMapping(SurveyFieldTypeMapping surveyFieldTypeMapping) {
        return surveyFieldTypeMappingMapper.insertSurveyFieldTypeMapping(surveyFieldTypeMapping);
    }

    /**
     * 修改字段类型映射
     *
     * @param surveyFieldTypeMapping 字段类型映射
     * @return 结果
     */
    @Override
    public int updateSurveyFieldTypeMapping(SurveyFieldTypeMapping surveyFieldTypeMapping) {
        return surveyFieldTypeMappingMapper.updateSurveyFieldTypeMapping(surveyFieldTypeMapping);
    }

    /**
     * 批量删除字段类型映射
     *
     * @param ids 需要删除的字段类型映射主键
     * @return 结果
     */
    @Override
    public int deleteSurveyFieldTypeMappingByIds(Long[] ids) {
        return surveyFieldTypeMappingMapper.deleteSurveyFieldTypeMappingByIds(ids);
    }

    /**
     * 删除字段类型映射信息
     *
     * @param id 字段类型映射主键
     * @return 结果
     */
    @Override
    public int deleteSurveyFieldTypeMappingById(Long id) {
        return surveyFieldTypeMappingMapper.deleteSurveyFieldTypeMappingById(id);
    }

    /**
     * 根据tag获取类型映射
     *
     * @param tagName 组件tag名称
     * @param tagIcon 组件图标
     * @return 类型映射信息
     */
    @Override
    public Map<String, Object> getTypeMappingByTag(String tagName, String tagIcon) {
        SurveyFieldTypeMapping mapping = surveyFieldTypeMappingMapper.selectByTagAndIcon(tagName, tagIcon);

        Map<String, Object> result = new HashMap<>();
        if (mapping != null) {
            result.put("dbType", mapping.getDbType());
            result.put("dbLength", mapping.getDbLength());
            result.put("searchType", mapping.getSearchType());
        } else {
            // 返回默认值
            result.put("dbType", getDefaultDbType(tagName));
            result.put("dbLength", getDefaultDbLength(tagName));
            result.put("searchType", "LIKE");
        }

        return result;
    }

    /**
     * 初始化默认类型映射
     */
    @Override
    public void initDefaultTypeMappings() {
        // 清空现有配置
        surveyFieldTypeMappingMapper.truncateTable();

        // 插入默认配置
        insertDefaultMapping("el-input", "icon-input", "VARCHAR", 255, "LIKE", "单行文本");
        insertDefaultMapping("el-textarea", "icon-textarea", "TEXT", null, "LIKE", "多行文本");
        insertDefaultMapping("el-input-number", "icon-number", "DECIMAL", 10, "EQ", "数字输入");
        insertDefaultMapping("el-radio-group", "icon-radio", "VARCHAR", 100, "EQ", "单选框组");
        insertDefaultMapping("el-checkbox-group", "icon-checkbox", "TEXT", null, "LIKE", "多选框组");
        insertDefaultMapping("el-select", "icon-select", "VARCHAR", 100, "EQ", "下拉选择");
        insertDefaultMapping("el-cascader", "icon-cascader", "TEXT", null, "LIKE", "级联选择");
        insertDefaultMapping("el-switch", "icon-switch", "TINYINT", 1, "EQ", "开关");
        insertDefaultMapping("el-slider", "icon-slider", "INT", 11, "BETWEEN", "滑块");
        insertDefaultMapping("el-time-picker", "icon-time", "TIME", null, "BETWEEN", "时间选择");
        insertDefaultMapping("el-date-picker", "icon-date", "DATETIME", null, "BETWEEN", "日期选择");
        insertDefaultMapping("el-rate", "icon-rate", "DECIMAL", 2, "EQ", "评分");
        insertDefaultMapping("el-color-picker", "icon-color", "VARCHAR", 20, "EQ", "颜色选择");
        insertDefaultMapping("el-upload", "icon-upload", "VARCHAR", 500, "LIKE", "文件上传");
        insertDefaultMapping("tinymce", "icon-rich-text", "LONGTEXT", null, "LIKE", "富文本编辑器");
    }

    /**
     * 插入默认映射
     */
    private void insertDefaultMapping(String tagName, String tagIcon, String dbType,
                                     Integer dbLength, String searchType, String description) {
        SurveyFieldTypeMapping mapping = new SurveyFieldTypeMapping();
        mapping.setTagName(tagName);
        mapping.setTagIcon(tagIcon);
        mapping.setDbType(dbType);
        mapping.setDbLength(dbLength);
        mapping.setSearchType(searchType);
        mapping.setDescription(description);
        mapping.setStatus(1);
        surveyFieldTypeMappingMapper.insertSurveyFieldTypeMapping(mapping);
    }

    /**
     * 获取默认数据库类型
     */
    private String getDefaultDbType(String tagName) {
        switch (tagName) {
            case "el-input-number":
                return "DECIMAL";
            case "el-textarea":
            case "el-checkbox-group":
            case "el-cascader":
                return "TEXT";
            case "tinymce":
                return "LONGTEXT";
            case "el-date-picker":
                return "DATETIME";
            case "el-time-picker":
                return "TIME";
            case "el-switch":
                return "TINYINT";
            default:
                return "VARCHAR";
        }
    }

    /**
     * 获取默认长度
     */
    private Integer getDefaultDbLength(String tagName) {
        switch (tagName) {
            case "el-input":
                return 255;
            case "el-select":
            case "el-radio-group":
                return 100;
            case "el-upload":
                return 500;
            case "el-switch":
                return 1;
            case "el-input-number":
            case "el-slider":
                return 11;
            case "el-color-picker":
                return 20;
            case "el-rate":
                return 2;
            default:
                return null;
        }
    }
}
