package com.example.memosystem.dao;

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

import com.example.memosystem.bean.Record;
import com.example.memosystem.dao.impl.AdminDaoImpl;
import com.example.memosystem.util.DBUtil;

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

public class AdminDao implements AdminDaoImpl {

    SQLiteDatabase db= DBUtil.db;
    @Override
    public int registerAdmin(String ...da) {
        try {
            db.execSQL("INSERT INTO d_admin VALUES(?,?,?,?,?,?)",da);
            return 1;
        }catch (Exception e){
            return -1;
        }

    }

    @Override
    public int loginAdmin(String... da) {

        int a=0;
        Cursor res = db.rawQuery("select * from d_admin where s_id=? and s_pwd=?", da);
        if(res.moveToNext()){
            a++;
        }
        return a;




    }

    /**
     * 修改密码 账号   密码
     * @param da
     * @return
     */
    @Override
    public int changePassword(String... da) {

        ContentValues values=new ContentValues();
        values.put("s_pwd", da[1]);

        String das[]={da[0]};
        try{
            return db.update("d_admin",values,"s_id=?",das);
        }catch (Exception e){
            return -1;
        }


    }

    public int changeMes(String ...da){
        ContentValues values=new ContentValues();
        values.put("s_name", da[1]);
        values.put("s_sex", da[2]);
        values.put("s_phone", da[3]);
        values.put("s_age", da[4]);
        String das[]={da[0]};
        try{
            return db.update("d_admin",values,"s_id=?",das);
        }catch (Exception e){
            return -1;
        }
    }


    public Map<String,String> showAccountMes(String account){

        Map<String,String> map=new HashMap<>();
        String da[]={account};
        Cursor result = db.rawQuery("select s_name,s_sex,s_phone,s_age from d_admin where s_id=?", da);
        while(result.moveToNext()){
            //姓名

            //姓名
            //手机号
            //年纪
            String name = result.getString(0);
            String sex= result.getString(1);
            String phone= result.getString(2);
            String age= result.getString(3);


            map.put("name",name);
            map.put("sex",sex);
            map.put("phone",phone);
            map.put("age",age);
        }
        return map;
    }

    /**
     * 删除信息
     * @param da
     * @return
     */
    @Override
    public int delRecord(String... da) {
        try {
            db.execSQL("delete from d_record where s_id=? and s_uid=?",da);
            return 1;
        }catch (Exception e){
            return -1;
        }


    }


    public int saveRecord(String ...da){//id 标题 内容 用户ID 时间
        try {
            db.execSQL("INSERT INTO d_record VALUES(?,?,?,?,?)",da);
            return 1;
        }catch (Exception e){

            String daa[]={da[1],da[2],da[4],da[0],da[3]};
            try {
                db.execSQL("update  d_record set s_title=? ,s_con=?,s_time=? where s_id=? and s_uid=?",daa);
                return 1;
            }catch (Exception ee){
                e.printStackTrace();
                ee.printStackTrace();
                return -1;
            }
        }
    }

    public List<Record> showRecord(String id){
        String da[]={id};
        List<Record> re=new ArrayList<>();//存储所有备忘录的
        Cursor res = db.rawQuery("select * from d_record where s_uid=?",da);
        while(res.moveToNext()){
            Record record=new Record(res.getString(0),res.getString(1),res.getString(2),res.getString(3),res.getString(4));
            re.add(record);
        }

        return re;

    }
}
