import 'package:base_app/models/inspectionModel.dart';
import 'package:base_app/utils/util_index.dart';
import 'package:flustars/flustars.dart';
import 'package:sqflite/sqlite_api.dart';
import '../baseDbProvider.dart';

/// Description: 巡检记录数据库操作类

class InspectionDbProvider extends BaseDbProvider {
  ///表名
  final String name = 'InspectionList';

  final String columnId = "id";
  final String columnName = "name";
  final String columnStartTime = "startTime";
  final String columnEndTime = "endTime";
  final String columnDate = "date";
  final String columnStartLng = "startLng";
  final String columnStartLat = "startLat";
  final String columnEndLng = "endLng";
  final String columnEndLat = "endLat";
  final String columnRoutes = "routes";
  final String columnUser = "user";

  InspectionDbProvider();

  @override
  tableName() {
    return name;
  }

  @override
  createTableString() {
    return '''
        create table $name (
        $columnId TEXT primary key,
        $columnName TEXT not null,
        $columnStartTime DateTime not null,
        $columnEndTime DateTime not null,
        $columnDate TEXT not null,
        $columnStartLng REAL not null,
        $columnStartLat REAL not null,
        $columnEndLng REAL not null,
        $columnEndLat REAL not null,
        $columnRoutes text not null,
        $columnUser text not null
        )
      ''';
  }

  ///查询数据库
  Future _getInspectionProvider(Database db, String id) async {
    List<Map<String, dynamic>> maps =
        await db.rawQuery("select * from $name where $columnId = '$id'");
    return maps;
  }

  ///插入到数据库
  Future insert(InspectionModel model) async {
    Database db = await getDataBase();
    var inspectionProvider = await _getInspectionProvider(db, model.id);
    if (inspectionProvider != null) {
      ///删除数据
      await db.delete(name, where: "$columnId = ?", whereArgs: [model.id]);
    }
    double startLng = model.startLng;
    double startLat = model.startLat;
    double endLng = model.endLng;
    double endLat = model.endLat;
    String routes = "";

    String insertSql = '''
    INSERT INTO $name ($columnId,$columnName,$columnStartTime,$columnEndTime,
    $columnDate,$columnStartLng,$columnStartLat,$columnEndLng,$columnEndLat,
    $columnRoutes,$columnUser)
    VALUES('${model.id}','${model.name}','${model.startTime}','${model.endTime}','${model.date}',$startLng,$startLat,$endLng,$endLat,'$routes','${model.user}')
    ''';
    print(insertSql);
    // return await db.rawInsert(insertSql,[model.id,model.name,model.startTime,model.endTime,model.date, startLng, startLat, endLng, endLat,routes, model.user]);

    return await db.rawInsert(insertSql);
  }

  ///更新数据库
  Future<void> update(InspectionModel model) async {
    // Database database = await getDataBase();
    // await database.rawUpdate(
    //     "update $name set $columnMobile = ?,$columnHeadImage = ? where $columnId= ?",[model.mobile,model.headImage,model.id]);
  }

  ///获取事件数据_——详情
  Future<InspectionModel> getInspectionInfo(String id) async {
    Database db = await getDataBase();
    List<Map<String, dynamic>> maps = await _getInspectionProvider(db, id);
    List<InspectionModel> list = [];
    for (int i = 0; i < maps.length; i++) {
      final item = maps[i];
      String id = item[columnId];
      String name = item[columnName];
      String startTime = item[columnStartTime];
      String endTime = item[columnEndTime];
      double startLng = item[columnStartLng];
      double startLat = item[columnStartLat];
      double endLng = item[columnEndLng];
      double endLat = item[columnEndLat];
      String routesStr = item[columnRoutes];
      List route = [];
      String user = item[columnUser];
      InspectionModel m = new InspectionModel(id, name, startTime, endTime,
          startLng, startLat, endLng, endLat, route, [], user);
      list.add(m);
    }
    if (list.length > 0) {
      return list[0];
    } else {
      return null;
    }
  }

  ///查询数据库
  Future<List<InspectionModel>> getInspections(
      String startTime, String endTime, String keywords) async {
    Database db = await getDataBase();
    String querySql =
        "select * from $name where $columnName LIKE '%$keywords%'";
    if (startTime.length > 0) {
      querySql += " and $columnStartTime>='$startTime 00:00:00' ";
    }
    if (endTime.length > 0) {
      querySql += " and $columnStartTime<='$endTime 23:59:59' ";
    }
    LogUtil.e(querySql);
    print(querySql);
    List<Map<String, dynamic>> maps = await db.rawQuery(querySql);
    List<InspectionModel> list = [];
    for (int i = 0; i < maps.length; i++) {
      final item = maps[i];
      String id = item[columnId];
      String name = item[columnName];
      String startTime = item[columnStartTime];
      String endTime = item[columnEndTime];
      // String date = item[columnDate];
      double startLng = item[columnStartLng];
      double startLat = item[columnStartLat];
      double endLng = item[columnEndLng];
      double endLat = item[columnEndLat];
      String routesStr = item[columnRoutes];
      List route = [];
      String user = item[columnUser];
      InspectionModel m = new InspectionModel(id, name, startTime, endTime,
          startLng, startLat, endLng, endLat, route, [], user);
      list.add(m);
    }
    return list;
  }

  /// 清空数据库表
  Future<void> deleteAll() async {
    Database database = await getDataBase();
    await database.delete(name);
  }
}
