package com.easygbs.easygbd.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import androidx.annotation.NonNull;

import com.easygbs.easygbd.common.Constant;
import com.easygbs.easygbd.dao.bean.Chan;

import java.util.ArrayList;
import java.util.List;

public class ChanOp {
    public String TAG = ChanOp.class.getSimpleName();
    public static ChanOp _instance = null;
    public DOpe mDataOperate;
    public SQLiteDatabase mSQLiteDatabase;

    public static ChanOp Instance(DOpe mDataOperate, SQLiteDatabase mSQLiteDatabase) {
        if (_instance == null) {
            if (_instance == null) {
                _instance = new ChanOp(mDataOperate, mSQLiteDatabase);
            }
        }
        return _instance;
    }

    private ChanOp(DOpe mDataOperate, SQLiteDatabase mSQLiteDatabase) {
        this.mDataOperate = mDataOperate;
        this.mSQLiteDatabase = mSQLiteDatabase;
    }

    public boolean Chanjudinsert(Chan mChan) {
        boolean value = false;
        Cursor mCursor = mSQLiteDatabase.query("chan", new String[]{"ec", "uid", "cid", "na", "sta"}, "uid = ?  and sta = 1", new String[]{"" + mChan.getUid()}, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
            if (mCursor.getCount() == 1) {
                value = true;
            }
            mCursor.close();
        }

        return !value;
    }

    public boolean MametInsert(Chan mChan) {
        if (Constant.ChanF == 0) {
            mChan.setEc(Constant.ChanID);
            ContentValues mContentValues = new ContentValues();
            mContentValues.put("ec", mChan.getEc());
            mContentValues.put("uid", mChan.getUid());
            mContentValues.put("cid", mChan.getCid());
            mContentValues.put("na", mChan.getNa());
            mContentValues.put("sta", mChan.getSta());
            long insertRe = mSQLiteDatabase.insert("chan", null, mContentValues);
            return insertRe != -1;
        } else {
            mChan.setEc(Constant.ChanID);
            ContentValues mContentValues = new ContentValues();
            mContentValues.put("uid", mChan.getUid());
            mContentValues.put("cid", mChan.getCid());
            mContentValues.put("na", mChan.getNa());
            mContentValues.put("sta", mChan.getSta());
            final String[] parms2 = new String[]{"" + mChan.getEc()};
            int updateRe = mSQLiteDatabase.update("chan", mContentValues, "ec = ?", parms2);
            return updateRe != 0;
        }
    }

    public long Chanqueryct() {
        long num = 0;
        Cursor cur = mSQLiteDatabase.rawQuery("select count(*)  from  chan", null);
        if (cur != null) {
            cur.moveToFirst();
            num = cur.getLong(0);
            cur.close();
        }
        return num;
    }

    public long getCount() {
        long count = 0;
        Cursor cur = mSQLiteDatabase.rawQuery("select count(*)  from  chan where sta!=0", null);
        if (cur.moveToFirst()) {
            count = cur.getInt(0);
        }
        return count;
    }

    public int ChanDelete(Chan mChan) {
        // 删除符合条件的记录
        String deleteQuery = "DELETE FROM chan WHERE uid = ?";
        SQLiteStatement statement = mSQLiteDatabase.compileStatement(deleteQuery);
        statement.bindLong(1, mChan.getUid());

        try {
            // 执行删除操作
            statement.executeUpdateDelete();
        } finally {
            // 确保关闭 statement
            statement.close();
        }
        return 0;
    }

    public Chan Chanqueryrow(int ec) {
        Chan mChan = null;
        Cursor mCursor = mSQLiteDatabase.query("chan", new String[]{"ec", "uid", "cid", "na", "sta"}, "ec = ?", new String[]{"" + ec}, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
            if (mCursor.getCount() == 1) {
                mChan = new Chan();
                mChan.setEc(mCursor.getInt(0));
                mChan.setUid(mCursor.getInt(1));
                mChan.setCid(mCursor.getString(2));
                mChan.setNa(mCursor.getString(3));
                mChan.setSta(mCursor.getInt(4));

                mCursor.moveToNext();
            }
            mCursor.close();
        }
        return mChan;
    }

    public List<Chan> Chanqueryeffect() {
        List<Chan> ChanList = new ArrayList<Chan>();
        Chan mChan = null;
        Cursor mCursor = mSQLiteDatabase.query("chan", new String[]{"ec", "uid", "cid", "na", "sta"}, "sta = ?", new String[]{"1"}, null, null, "uid  ASC");
        if (mCursor != null) {
            mCursor.moveToFirst();
            if (mCursor.getCount() >= 1) {
                for (int i = 0; i < mCursor.getCount(); i++) {
                    mChan = new Chan();
                    mChan.setEc(mCursor.getInt(0));
                    mChan.setUid(mCursor.getInt(1));
                    mChan.setCid(mCursor.getString(2));
                    mChan.setNa(mCursor.getString(3));
                    mChan.setSta(mCursor.getInt(4));

                    ChanList.add(mChan);
                    mCursor.moveToNext();
                }
            }
            mCursor.close();
        }
        return ChanList;
    }

    public Chan Chanquerymaxuid() {
        Chan mChan = null;
        Cursor mCursor = mSQLiteDatabase.query("chan", new String[]{"ec", "uid", "cid", "na", "sta"}, "", null, null, null, "ec  DESC  limit 0,1");
        if (mCursor != null) {
            mCursor.moveToFirst();
            if (mCursor.getCount() == 1) {
                mChan = new Chan();
                mChan.setEc(mCursor.getInt(0));
                mChan.setUid(mCursor.getInt(1));
                mChan.setCid(mCursor.getString(2));
                mChan.setNa(mCursor.getString(3));
                mChan.setSta(mCursor.getInt(4));
                mCursor.moveToNext();
            }
            mCursor.close();
        }
        return mChan;
    }

    public boolean Chanupdatedependuid(Chan mChan) {
        ContentValues cv = new ContentValues();
        cv.put("cid", mChan.getCid());
        cv.put("na", mChan.getNa());
        cv.put("sta", mChan.getSta());
        final String[] parmstrarr = new String[]{"" + mChan.getUid()};
        int updateRe = mSQLiteDatabase.update("chan", cv, "uid = ?", parmstrarr);
        return updateRe != 0;
    }
}