// 该类用于处理与 SQL 数据相关的操作，可能是在笔记应用中对数据的管理和同步操作
public class SqlData {
    // 日志标签，使用类的简单名称
    private static final String TAG = SqlData.class.getSimpleName();
    // 表示无效的 ID
    private static final int INVALID_ID = -99999;
    // 数据查询的投影，指定了需要查询的数据列
    public static final String[] PROJECTION_DATA = new String[] {
            DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
            DataColumns.DATA3
    };
    // 投影中数据 ID 列的索引
    public static final int DATA_ID_COLUMN = 0;
    // 投影中数据 MIME 类型列的索引
    public static final int DATA_MIME_TYPE_COLUMN = 1;
    // 投影中数据内容列的索引
    public static final int DATA_CONTENT_COLUMN = 2;
    // 投影中数据 DATA1 列的索引
    public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
    // 投影中数据 DATA3 列的索引
    public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
    // 用于解析和操作数据的内容解析器
    private ContentResolver mContentResolver;
    // 标记是否为创建操作
    private boolean mIsCreate;
    // 数据的 ID
    private long mDataId;
    // 数据的 MIME 类型
    private String mDataMimeType;
    // 数据的内容
    private String mDataContent;
    // 数据的 DATA1 内容
    private long mDataContentData1;
    // 数据的 DATA3 内容
    private String mDataContentData3;
    // 存储不同的数据值，可能用于更新操作
    private ContentValues mDiffDataValues;


    // 构造函数，用于创建新的数据对象，初始化成员变量
    public SqlData(Context context) {
        mContentResolver = context.getContentResolver();
        mIsCreate = true;
        mDataId = INVALID_ID;
        mDataMimeType = DataConstants.NOTE;
        mDataContent = "";
        mDataContentData1 = 0;
        mDataContentData3 = "";
        mDiffDataValues = new ContentValues();
    }


    // 构造函数，根据 Cursor 初始化数据对象，用于从数据库中加载数据
    public SqlData(Context context, Cursor c) {
        mContentResolver = context.getContentResolver();
        mIsCreate = false;
        loadFromCursor(c);
        mDiffDataValues = new ContentValues();
    }


    // 从 Cursor 中加载数据
    private void loadFromCursor(Cursor c) {
        mDataId = c.getLong(DATA_ID_COLUMN);
        mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
        mDataContent = c.getString(DATA_CONTENT_COLUMN);
        mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
        mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
    }


    // 根据 JSON 对象设置数据内容
    public void setContent(JSONObject js) throws JSONException {
        long dataId = js.has(DataColumns.ID)? js.getLong(DataColumns.ID) : INVALID_ID;
        if (mIsCreate || mDataId!= dataId) {
            mDiffDataValues.put(DataColumns.ID, dataId);
        }
        mDataId = dataId;


        String dataMimeType = js.has(DataColumns.MIME_TYPE)? js.getString(DataColumns.MIME_TYPE)
                : DataConstants.NOTE;
        if (mIsCreate ||!mDataMimeType.equals(dataMimeType)) {
            mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType);
        }
        mDataMimeType = dataMimeType;


        String dataContent = js.has(DataColumns.CONTENT)? js.getString(DataColumns.CONTENT) : "";
        if (mIsCreate ||!mDataContent.equals(dataContent)) {
            mDiffDataValues.put(DataColumns.CONTENT, dataContent);
        }
        mDataContent = dataContent;


        long dataContentData1 = js.has(DataColumns.DATA1)? js.getLong(DataColumns.DATA1) : 0;
        if (mIsCreate || mDataContentData1!= dataContentData1) {
            mDiffDataValues.put(DataColumns.DATA1, dataContentData1);
        }
        mDataContentData3 = dataContentData1;


        String dataContentData3 = js.has(DataColumns.DATA3)? js.getString(DataColumns.DATA3) : "";
        if (mIsCreate ||!mDataContentData3.equals(dataContentData3)) {
            mDiffDataValues.put(DataColumns.DATA3, dataContentData3);
        }
        mDataContentData3 = dataContentData3;
    }


    // 获取数据的 JSON 表示
    public JSONObject getContent() throws JSONException {
        if (mIsCreate) {
            Log.e(TAG, "it seems that we haven't created this in database yet");
            return null;
        }
        JSONObject js = new JSONObject();
        js.put(DataColumns.ID, mDataId);
        js.put(DataColumns.MIME_TYPE, mDataMimeType);
        js.put(DataColumns.CONTENT, mDataContent);
        js.put(DataColumns.DATA1, mDataContentData1);
        js.put(DataColumns.DATA3, mDataContentData3);
        return js;
    }


    // 提交数据更改，根据情况进行插入或更新操作
    public void commit(long noteId, boolean validateVersion, long version) {


        if (mIsCreate) {
            if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {
                mDiffDataValues.remove(DataColumns.ID);
            }


            mDiffDataValues.put(DataColumns.NOTE_ID, noteId);
            Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);
            try {
                mDataId = Long.valueOf(uri.getPathSegments().get(1));
            } catch (NumberFormatException e) {
                Log.e(TAG, "Get note id error :" + e.toString());
                throw new ActionFailureException("create note failed");
            }
        } else {
            if (mDiffDataValues.size() > 0) {
                int result = 0;
                if (!validateVersion) {
                    result = mContentResolver.update(ContentUris.withAppendedId(
                            Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);
                } else {
                    result = mContentResolver.update(ContentUris.withAppendedId(
                            Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
                            "? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
                                    + " WHERE " + NoteColumns.VERSION + "=?)", new String[] {
                                    String.valueOf(noteId), String.valueOf(version)
                            });
                }
                if (result == 0) {
                    Log.w(TAG, "there is no update. maybe user updates note when syncing");
                }
            }
        }


        mDiffDataValues.clear();
        mIsCreate = false;
    }


    // 获取数据的 ID
    public long getId() {
        return mDataId;
    }
}