//
// UserManager.m
// ChiHuo
//
// Created by administrator on 12-8-28.
// Copyright (c) 2012年 lxn. All rights reserved.
//
#import "UserManager.h"
#import "AppDelegate.h"
#import "User.h"
@implementation UserManager
@synthesize db = _db;
-(void)showAlert:(NSString *)msg
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
-(BOOL)openDB
{
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/chihuo.sqlite"];
self.db = [FMDatabase databaseWithPath:dataPath];
if(![_db open])
{
[self showAlert:@"数据库打开失败"];
return NO;
}
return YES;
}
-(void)registerWithName:(NSString *)name Password:(NSString *)pwd Phone:(NSString *)phone Address:(NSString *)address Email:(NSString *)email
{
if([self openDB])
{
if([_db executeUpdate:[NSString stringWithFormat:@"insert into member (name,password,phone,address,email) values ('%@','%@','%@' )",name,pwd,email]])
[self showAlert:@"注册成功"];
else
[self showAlert:@"注册失败"];
}
}
-(User *)loginUserByName:(NSString *)name AndPassword:(NSString *)pwd
{
User *resUser = [[[User alloc] init] autorelease];
resUser.userID = 0;
if([self openDB])
{
FMResultSet *fResult = [_db executeQuery:[NSString stringWithFormat:@"select * from member where name = '%@' and password = '%@'",pwd ]];
if([fResult next])
{
resUser.userID = [fResult intForColumn:@"memberID"];
resUser.name = [ fResult stringForColumn:@"name"];
resUser.pwd = [fResult stringForColumn:@"password"];
resUser.phone = [fResult stringForColumn:@"phone"];
resUser.address = [fResult stringForColumn:@"address"];
resUser.email = [fResult stringForColumn:@"email"];
}
[fResult close];
}
[_db close];
return resUser;
}
-(User *)getUserByID:(int)uid
{
User *resUser = [[[User alloc] init] autorelease];
resUser.userID = 0;
if([self openDB])
{
FMResultSet *fResult = [_db executeQuery:[NSString stringWithFormat:@"select * from member where memberID = %d",uid ]];
if([fResult next])
{
resUser.userID = [fResult intForColumn:@"memberID"];
resUser.name = [ fResult stringForColumn:@"name"];
resUser.pwd = [fResult stringForColumn:@"password"];
resUser.phone = [fResult stringForColumn:@"phone"];
resUser.address = [fResult stringForColumn:@"address"];
resUser.email = [fResult stringForColumn:@"email"];
}
[fResult close];
}
[_db close];
return resUser;
}
-(void)leaveMessage:(NSString *)message andMemberID:(int)mid
{
if([self openDB])
{
if([_db executeUpdate:[NSString stringWithFormat:@"insert into message (message,memberID) values ('%@',%d)",message,mid]])
[self showAlert:@"吐槽成功"];
else
[self showAlert:@"吐槽失败"];
}
[_db close];
}
-(NSMutableArray *)returnAllMessage;
{
NSMutableArray *msgArray = [[[NSMutableArray alloc] init] autorelease];
if([self openDB])
{
FMResultSet *fResult = [_db executeQuery:@"select * from message"];
while([fResult next])
{
//根据留言者ID获取留言人信息
int mid = [fResult intForColumn:@"memberID"];
User * msgUser = [self getUserByID:mid];
NSMutableDictionary *msgDic = [NSMutableDictionary dictionary];
[msgDic setObject:[fResult stringForColumn:@"message"] forKey:@"message"];
[msgDic setObject:msgUser.name forKey:@"name"];
[msgArray addObject:msgDic];
//
// [msgDic release];
}
[fResult close];
}
[_db close];
return msgArray;
}
-(BOOL)modifyPwdByUserID:(int)mid withNewPwd:(NSString *)pwd
{
if([self openDB])
{
if([_db executeUpdate:[NSString stringWithFormat:@"update member set password = '%@' where memberID = %d",mid]])
[self showAlert:@"密码修改成功"];
else
{
[self showAlert:@"密码修改失败"];
return NO;
}
}
[_db close];
return YES;
}
-(NSMutableArray *)returnAllCar
{
NSMutableArray *carArray = [[[NSMutableArray alloc] init] autorelease];
if([self openDB])
{
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//从AppDelegate中的carDic中依次取出所有的key和value的值
//获取字典中的所有的Key的值:keyEnumerator;Value值:objectEnumerator
NSEnumerator *keyEnum = [appDlg.carDic keyEnumerator];
id obj; //定义一个不确定类型的对象
//遍历字典输出所有的key
while (obj = [keyEnum nextObject])
{
NSLog(@"键值为:%@",obj);
//定义一个字典用于向数组中放入数据
NSMutableDictionary *car = [NSMutableDictionary dictionary];
//根据根据键值获取其对应的value(表示此菜的数目) 的值
id objValue = [appDlg.carDic objectForKey:obj];
[car setObject:obj forKey:@"foodID"]; //存入食物的ID
[car setObject:objValue forKey:@"count"];//存入食物的数量count
//根据食物的ID获取食物的信息
FMResultSet *foodResult = [_db executeQuery:[NSString stringWithFormat:@"select * from food where foodID = %@",obj]];
if([foodResult next])
{
//存入食物的名称name
//存入食物价格price
//存入食物的图片image
[car setObject:[foodResult stringForColumn:@"name"] forKey:@"name"];
[car setObject:[foodResult stringForColumn:@"price"] forKey:@"price"];
[car setObject:[foodResult stringForColumn:@"image"] forKey:@"image"];
}
[foodResult close];
[carArray addObject:car];
}
}
[_db close];
return carArray;
}
-(float)returnMoney
{
float money = 0;
if([self openDB])
{
AppDelegate *appDlg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//从AppDelegate中的carDic中依次取出所有的key和value的值
//获取字典中的所有的Key的值:keyEnumerator;Value值:objectEnumerator
NSEnumerator *keyEnum = [appDlg.carDic keyEnumerator];
id obj; //定义一个不确定类型的对象
//遍历字典输出所有的key
while (obj = [keyEnum nextObject])
{
int foodID = [obj integerValue]; //存入食物的ID
int count = [[appDlg.carDic objectForKey:obj] integerValue]; //存入食物的数量
int memberID = appDlg.user.userID;
NSLog(@"键值为:%@",obj);
//将数据存入数据库中
if([_db executeUpdate:[NSString stringWithFormat:@"insert into car (foodID,count,memberID) values (%d,%d,foodID,memberID]])
{
//根据食物的ID获取食物的信息
FMResultSet *foodResult = [_db executeQuery:[NSString stringWithFormat:@"select * from food where foodID = %@",obj]];
if([foodResult next])
{
int oldNum = [[foodResult stringForColumn:@"sale"] integerValue];
//计算所有总订单的价钱
money = money + [[foodResult stringForColumn:@"price"] floatValue] * count;
[_db executeUpdate:[NSString stringWithFormat:@"update food set sale = %d where foodID = %d",count + oldNum,foodID]];
}
[foodResult close];
}
else
[self showAlert:@"订单提交失败"];
//根据食物的ID获取食物的信息
FMResultSet *foodResult = [_db executeQuery:[NSString stringWithFormat:@"select * from food where foodID = %@",obj]];
if([foodResult next])
{
//计算所有总订单的价钱
money = money + [[foodResult stringForColumn:@"price"] floatValue] * count;
}
[foodResult close];
}
}
[_db close];
[self showAlert:@"订单提交成功"];
return money;
}
@end
原文链接:https://www.f2er.com/sqlite/201625.html