PHP+Mysql+jQuery+fullcalendar实现的微型公司内部订餐系统

老古董 发布于 2010/06/10 10:55
阅读 4K+
收藏 4

需求相关: 公司20人左右, 每天中午, 下午两次订餐, 都是助理在群里说"开始订餐", 然后大家报菜名, 然后助理统计, 打电话. 今天, 助理说"开始订餐, 订什么密聊...", 突然就想做这么个东西....

耗时: 4.5小时

评估: 时间太短, 不能做到很好, 没有什么输入验证之类, 就自己公司内部一点人, 用用应该没啥问题.....bug估计很多...应该是没时间改bug的, 不过有不足之处请大家提出, 互相学习.

提醒: 由于我这边和服务器有个时差, 所以, 里面有代码在倒时差.....另外, 13点之前被认为是上午, 之后包括13点被认为是下午, 一天两次.

本文涉及到的插件: jQuery, jQuery.UI, fullcalendar

数据库创建:

CREATE TABLE USER
(
  ID INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(100)
);

CREATE TABLE ITEM
(
  ID INT PRIMARY KEY AUTO_INCREMENT,
  USER_ID INT,
  FOOD TINYBOLB,
  ORDER_TIME TIMESTAMP
);

ALTER TABLE ITEM ADD FOREIGN KEY(USER_ID) REFERENCES USER(ID) ON DELETE CASCADE;


一个实体的基类, 封装的不太好: Entity.php

PHP code
< Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ >
<?php
/**
* 实体基类
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @email lgg860911@yahoo.com.cn
*
*/
class Entity {
protected static $_mapping = array();
public static function getField($column_name) {
return self::$_mapping[$column_name] ? self::$_mapping[$column_name] : $column_name;
}
public function set($field, $value) {
$method = 'set'.ucwords($field);
return $this->$method($value);
}
public function get() {
$method = 'get'.ucwords($field);
return $this->$method();
}
}
?>




用户类(这个里面是没有权限, 登录的概念的, 就公司内部一个小东西): User.php

PHP code
< Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ >
<?php
require_once './Entity.php';

/**
* 用户实体类
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @email lgg860911@yahoo.com.cn
*
*/
class User extends Entity {
private $id;
private $name;

public function User($id = NULL, $name = NULL) {
$this->id = $id;
$this->name = $name;
}

public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
return $this;
}
}
?>




订餐订单项类: Item.php

PHP code
< Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ >
<?php
require_once './Entity.php';
/**
* 订餐订单
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @email lgg860911@yahoo.com.cn
*
*/
class Item extends Entity {
private $id;
private $user;
private $food;
private $orderTime;

public function Item($id = NULL, $food = NULL, $orderTime = NULL) {
$this->id = $id;
$this->food = $food;
$this->orderTime = $orderTime;
}

public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
return $this;
}
public function getUser() {
return $this->user;
}
public function setUser($user) {
$this->user = $user;
return $this;
}
public function getFood() {
return $this->food;
}
public function setFood($food) {
$this->food = $food;
return $this;
}
public function getOrderTime() {
return $this->orderTime;
}
public function setOrderTime($orderTime) {
$this->orderTime = $orderTime;
return $this;
}
}
?>




数据库工具类: mysql_util.php

PHP code
< Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ >
<?php
/**
* Mysql工具类
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @email lgg860911@yahoo.com.cn
*
*/
class Mysql {
private $host;
private $username;
private $password;
private $db_name;
private $character_set;

public function __construct(
$host = "192.168.2.10",
$username = "root",
$password = "jhx",
$db_name = "eat",
$character_set = "GBK") {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db_name = $db_name;
$this->character_set = $character_set;
}

public function getConn() {
$conn = mysql_connect($this->host, $this->username, $this->password) or die('数据库连接失败');
mysql_select_db($this->db_name, $conn);
mysql_set_charset(
$this->character_set, $conn);
return $conn;
}

/**
*
* @param $sql 查询用的sql
* @param $result 查询得到的结果集, 如果UPDATE, INSERT, DELETE 不同提供这个参数
* @return SELECT 返回查询得到的条数, UPDATE, INSERT, DELETE返回bool型, 标识是否执行成功.
*/
public function execute($sql) {
$conn = $this->getConn();
$resultset = mysql_query($sql, $conn) or die(mysql_error($conn));
if(!is_bool($resultset)) {
while($line = mysql_fetch_assoc($resultset)) {
$result[] = $line;
}
}
else {
$result = $resultset;
}

mysql_close($conn) or die(mysql_error($conn));

// echo "【".$sql."】 excute success!<br />";

return $result;
}

}
?>




业务逻辑(没时间了就写一个文件里了): bussiness.php

PHP code
< Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ >
<?php
require_once './mysql_util.php';
require_once './User.php';
require_once './Item.php';

/**
* 获取当前库中所有的用户
* @return unknown_type
*/
function getUsers() {
$util = new Mysql();
$users_data = $util->execute("SELECT * FROM USER");
foreach($users_data as $index => $data) {
$user = new User();
foreach($data as $column_name => $value) {
$user->set(User::getField($column_name), $value);
}
$users[] = $user;
}
return $users;
}

/**
* 根据名字增加一个用户
* @param $name
* @return unknown_type
*/
function addUser($name) {
$util = new Mysql();
$name = iconv("UTF-8", "gbk" , $name);
return $util->execute("INSERT INTO USER VALUES(NULL, '$name')");
}

/**
* 根据id删除一个用户
* @param $id
* @return unknown_type
*/
function deleteUser($id) {
$util = new Mysql();
return $util->execute("DELETE FROM USER WHERE ID = $id");
}

/**
* 根据id修改姓名
* @param $id
* @param $name
* @return unknown_type
*/
function updateUser($id, $name) {
$util = new Mysql();
return $util->execute("UPDATE USER SET NAME = '$name' WHERE ID = $id");
}

/**
* 插入一条订餐记录
* @param $user_id
* @param $food
* @return unknown_type
*/
function addItem($user_id, $food, $ordertime) {
$util = new Mysql();
$year = date('Y', $ordertime);
$month = date('n', $ordertime);
$day = date('j', $ordertime);
$hour = (date('G', $ordertime) + 8) % 24;
$flag = $hour < 13;
$item = getItemByTimeAndUser($year, $month, $day, $flag, $user_id);
$ordertime = "$year-$month-$day $hour:00:00";
$item && ($item = $item[0]) && ($flag = $flag ? ' < ' : ' >= ');
return $item
? $util->execute("UPDATE ITEM SET FOOD = '$food' WHERE USER_ID = $user_id AND YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13")
: $util->execute("INSERT INTO ITEM(USER_ID, FOOD, ORDER_TIME) VALUES($user_id, '$food', '$ordertime')");
}

/**
* 获取某天上午或下午的订餐
* @param $year
* @param $month
* @param $day
* @param $flag 上午true下午false
* @return unknown_type
*/
function getItemsByTime($year, $month, $day, $flag) {
$util = new Mysql();
$flag = $flag ? ' < ' : ' >= ';
$item_datas = $util->execute("SELECT ITEM.*, USER.NAME FROM ITEM JOIN USER ON ITEM.USER_ID = USER.ID WHERE YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13");
if(empty($item_datas)) return array();
foreach($item_datas as $index => $data) {
$item = new Item();
$items[$data['USER_ID']] = $item->setId($data['ID'])
->setUser(new User($data['USER_ID'], $data['NAME']))
->setFood($data['FOOD'])
->setOrderTime(date($data['ORDER_TIME']));
}
return $items;
}

/**
* 根据用户id, 时间, 获取其订饭记录
* @param $year
* @param $month
* @param $day
* @param $flag
* @param $user_id
* @return unknown_type
*/
function getItemByTimeAndUser($year, $month, $day, $flag, $user_id) {
$util = new Mysql();
$flag = $flag ? ' < ' : ' >= ';
$item_datas = $util->execute("SELECT ITEM.*, USER.NAME FROM ITEM JOIN USER ON ITEM.USER_ID = USER.ID WHERE YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13 AND ITEM.USER_ID = $user_id");
echo "SELECT ITEM.*, USER.NAME FROM ITEM JOIN USER ON ITEM.USER_ID = USER.ID WHERE YEAR(ORDER_TIME) = $year AND MONTH(ORDER_TIME) = $month AND DAY(ORDER_TIME) = $day AND HOUR(ORDER_TIME) $flag 13 AND ITEM.USER_ID = $user_id";
if(empty($item_datas)) return array();
foreach($item_datas as $index => $data) {
$item = new Item();
$items[] = $item->setId($data['ID'])
->setUser(new User($data['USER_ID'], $data['NAME']))
->setFood($data['FOOD'])
->setOrderTime(date($data['ORDER_TIME']));
}
return $items;
}
?>




单元测试: test.unit.php

PHP code
< Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ >
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php
require_once './mysql_util.php';
require_once './bussiness.php';

/**
* 本程序业务逻辑之下部分的单元测试
* @author selfimpr
* @blog http://blog.csdn.net/lgg201
* @email lgg860911@yahoo.com.cn
*/


/**
* 测试用例1: 数据库连接
*/
//$util = new Mysql();
//$res = $util->execute('SHOW TABLES');
//print_r($res);
//echo '<br>==========数据库连接成功===========<br>';


/**
* 测试用例2: 获取所有用户
*/
//var_dump(getUsers());

/**
* 测试用例3: 增加用户
*/
//var_dump(addUser("张三"));

/**
* 测试用例4: 删除用户
*/
//var_dump(deleteUser(4));

/**
* 测试用例5: 更新用户
*/
//var_dump(updateUser(5, '李四'));

/**
* 测试用例6: 插入订餐项
*/
//var_dump(addItem(1, '辣子鸡丁'));

/**
* 测试用例7: 获取某天上午或下午订餐
*/
//var_dump(getItemsByTime(2010, 6, 4, false));

/**
* 测试用例8: 获取某人某天上午或下午订餐
*/
//var_dump(getItemByTimeAndUser(date('Y'), date('m'), date('d'), date('H') + 8 < 13, 1));
?>

加载中
0
haol666
haol666
给个下载链接吧
0
小马-过河
小马-过河
java 的风格
返回顶部
顶部