package com.mq.mylibrary.db.team_group;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mq.mylibrary.db.BaseDao;
import com.mq.mylibrary.tool.HappySQL;
import com.mq.mylibrary.tool.LogTool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 创建人 mQ
 * 创建时间 2019/8/16 0016 10:19
 * 文件名称 TeamGroupDao
 * 说明:
 **/
public class TeamGroupDao extends BaseDao<TeamGroupEntity> {

    private TeamGroupDao() {
    }

    private static class SingleHelper {
        private static final TeamGroupDao INSTANCE = new TeamGroupDao();
    }

    public static TeamGroupDao get() {
        return SingleHelper.INSTANCE;
    }


    @Override
    public String getTableName() {
        return "worke_team_group";
    }


    public boolean insertByJSONArray(SQLiteDatabase db, JSONArray array) {
        if (array == null || array.length() == 0) {
            LogTool.i("初始化: 没有作业队组信息");
            return true;
        }
        try {
            for (int i = 0; i < array.length(); i++) {
                long flg = insertByJSONObject(db, array.getJSONObject(i));
                if (flg == -1)
                    return false;
            }
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    public long insertByJSONObject(SQLiteDatabase db, JSONObject object) {
        TeamGroupEntity entity = new TeamGroupEntity();
        entity.setReal_id(object.optString("zyzid"));
        entity.setWork_team(object.optString("sszyd"));
        entity.setWork_group(object.optString("zyz"));
        return insert(db, entity);
    }


    public List<TeamGroupEntity> getTeamList(SQLiteDatabase db) {
        Cursor cursor = db.rawQuery("select * from " + getTableName() + " group by work_team " +
                " order by work_team", null);
        return HappySQL.cursor2VOList(cursor, TeamGroupEntity.class);
    }

    public List<TeamGroupEntity> getGroupList(SQLiteDatabase db, String team) {
        Cursor cursor = db.rawQuery("select * from " + getTableName() +
                " where  work_team = '" + team + "'" +
                " order by work_group", null);
        return HappySQL.cursor2VOList(cursor, TeamGroupEntity.class);
    }

    public List<TeamGroupEntity> getGroupList(SQLiteDatabase db) {
        Cursor cursor = db.rawQuery("select * from " + getTableName() + " group by work_group " +
                " order by work_group", null);
        return HappySQL.cursor2VOList(cursor, TeamGroupEntity.class);
    }

}
