package com.unis.model.login_log;

import android.os.AsyncTask;

import com.unis.model.room.MyDB;
import com.xyxsbj.mylibrary.tool.DateTool;
import com.xyxsbj.mylibrary.tool.LogTool;

/**
 * 保存登录记录
 * 创建人: mQ
 * 时间: 2021/2/7 16:17
 */
public class LoginLogSaveTask extends AsyncTask<LoginLog, Void, Void> {


    public LoginLogSaveTask() {
    }

    @Override
    protected Void doInBackground(LoginLog... logs) {
        LogTool.i("保存登录记录");
        if (logs == null || logs.length == 0) return null;
        LoginDao dao = MyDB.get().loginDao();

        LoginLog from = logs[0];
        // 查询数据库是否已有此用户登录
        LoginLog loginLog = dao.getByName(from.name);
        if (loginLog == null) {
            loginLog = new LoginLog();
            loginLog.name = from.name;
            loginLog.pwd = from.pwd;
        }
        loginLog.time = DateTool.getCurrentTimeStamp();

        if (loginLog.lid == 0) {
            dao.insert(loginLog);
        } else {
            dao.update(loginLog);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        super.onPostExecute(v);
    }

}
