forked from wosenbo/happyfactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
warehouse.php
154 lines (135 loc) · 4.61 KB
/
warehouse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
function get_categories() {
global $db;
static $categories = null;
if (is_null($categories)) {
$categories = array();
$query = $db->query("SELECT * FROM game_productcategory");
while ($row = $db->fetch_array($query)) {
$categories[] = $row;
}
}
return $categories;
}
function get_warehouse_price($uid) {
global $db;
$row = $db->fetch_first("SELECT SUM(w.count * p.price) AS price FROM game_warehouses w INNER JOIN game_products p ON p.pid = w.pid WHERE w.uid = '{$uid}'");
return $row ? $row['price'] : 0;
}
require_once './include/api.php';
require_once './include/warehouse.php';
switch(gp('action')) {
case 'get_product_count':
$pid = isset($_POST['pid']) ? (int) $_POST['pid'] : 0;
$count = $db->result_first("SELECT COUNT(*) FROM #__warehouse WHERE uid = '$uid'
AND pid = '$pid'");
return $count;
break;
case 'list':
$cid = isset($_POST['cid']) ? (int) $_POST['cid'] : 0;
if($cid)
$sqladd = "AND p.cateid = '$cid' ";
$query = $db->query("SELECT w.*, p.productname, p.pic, p.price FROM #__warehouse w
LEFT JOIN #__products p ON w.pid = p.pid WHERE w.uid = '$uid' {$sqladd}");
while($row = $db->fetch_array($query)) {
$warehouse_list[] = $row;
}
$smarty->assign('warehouse_list', $warehouse_list);
$smarty->display('warehouse_productlist.html');
break;
case 'input':
$pid = $_POST['pid'];
$sql = "SELECT w.*,p.pid,p.productname,p.pic,p.price FROM game_warehouses w ".
"LEFT JOIN game_products p ON w.pid=p.pid ".
"WHERE w.uid=$uid AND w.pid=$pid";
$warehouse = $db->fetch_first($sql);
if($warehouse == FALSE)
{
echo '您刚刚已经把这个商品卖给商店了!';
}
else
{
$smarty->assign('productname', $warehouse['productname']);
$smarty->assign('count', $warehouse['count']);
$smarty->assign('pic', $warehouse['pic']);
$smarty->assign('price', $warehouse['price']);
$smarty->display('warehouse_form.html');
}
break;
case 'sell':
$pid = gp('pid', 0);
$amount = intval(gp('amount', 0));
if($amount < 1){
json_error('出售数量不能小于1');
}
$warehouse = get_warehouse($uid, $pid);
if ($amount > $warehouse['count']) {
json_error('库存数量不足');
}
if($amount >= $warehouse['count']){
$db->query("DELETE FROM game_warehouses WHERE uid = '{$uid}' AND pid = '{$pid}'");
}else{
$db->query("UPDATE game_warehouses SET count = count - {$amount} WHERE uid = '{$uid}' AND pid = '{$pid}'");
}
$db->query("UPDATE game_userproperty SET money = money + {$warehouse['total_price']} WHERE uid = '{$uid}'");
json_success();
// $pid = gp('pid');
// $count = intval(gp('count', 0));
// if($count <= 0)
// {
// echo '请输入正确的出售数量!';
// }
// else
// {
// // $sql = "SELECT id,uid,pid,count FROM game_warehouses WHERE uid=$uid AND pid=$pid";
// $sql = "SELECT w.*,p.productname,p.price FROM game_warehouses w LEFT JOIN game_products p ".
// "ON w.pid=p.pid WHERE w.uid=$uid AND w.pid=$pid";
// $warehouse = $db->fetch_first($sql);
// if($count > $warehouse['count'])
// {
// echo '您没有足够的物品出售,请重新输入要出售的物品数量!';
// }
// else
// {
// $totalPrice = $count * $warehouse['price'];
// if($count == $warehouse['count'])
// {
// $db->query("DELETE FROM game_warehouses WHERE uid=$uid AND pid=$pid");
// }
// else
// {
// $db->query("UPDATE game_warehouses SET count=count-$count WHERE uid=$uid AND pid=$pid");
// }
// $db->query("UPDATE game_userproperty SET money=money+$totalPrice WHERE uid=$uid");
// echo "成功出售了 $count 个 ". $warehouse['productname'];
// }
// }
break;
case 'stat':
json_result(['total_price' => get_warehouse_price($uid)]);
break;
default:
$cate_id = gp('cateid', 0);
$ajax = gp('ajax', 0);
if($ajax){
json_result(['products' => get_warehouses($uid, $cate_id)]);
}
// $cid = gp('cid', 0);
// $sql = "SELECT id,category FROM game_productcategory";
// $query = $db->query($sql);
// $categorys = array();
// while($row = $db->fetch_array($query)) {
// $categorys[] = $row;
// }
// $query = $db->query("SELECT * FROM game_warehouses WHERE uid = '$uid'");
// $list = array();
// while($row = $db->fetch_array($query)) {
// $list[] = $row;
// }
// $smarty->assign('categorys', get_categories());
// $smarty->assign('warehouse_list', get_warehouses($uid));
// $smarty->assign('warehouse', $warehouse);
$smarty->display('warehouse.html');
break;
}
?>