Commit 956f2471 authored by 周尚's avatar 周尚

修复卡券批处理因时间格式导致的错误

暂时关闭门店日统计的批处理检查以规避mysql连接错误问题
parent df4029a7
......@@ -18,6 +18,7 @@ http {
include mime.types;
default_type application/octet-stream;
log_format main
#log_format main "'$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
......
......@@ -39,7 +39,7 @@ local function card_sql(db_st, total_time)
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_card WHERE (card_status != 0 AND card_status !=1) or now() > card_exp]]
local sql_update = [[UPDATE node_card SET card_status = 8 WHERE now() > card_exp]]
local sql_update = string.format([[UPDATE node_card SET card_status = 8 WHERE %d > card_exp]], os.date("%Y%m%d"))
local res, err, errcode, sqlstate = db:query(sql_update)
ngx.log(ngx.NOTICE, sql_update)
if not res then
......
......@@ -36,7 +36,7 @@ local function ticket_sql(db_st, total_time)
-- DELETE invalid rows
-- local sql_delete = [[DELETE FROM node_ticket WHERE (ticket_status != 0 AND ticket_status !=1) or now() > ticket_exp]]
local sql_update = [[UPDATE node_ticket SET ticket_status = 8 WHERE now() > ticket_exp]]
local sql_update = string.format([[UPDATE node_ticket SET ticket_status = 8 WHERE %d > ticket_exp]], os.date("%Y%m%d"))
ngx.log(ngx.NOTICE, sql_update)
local res, err, errcode, sqlstate = db:query(sql_update)
if not res then
......
local SETTLE = {}
-- Settle modules
local zx_base = loadmod('zx_base')
-- local node_asb = loadmod('node_asb')
......@@ -26,7 +27,7 @@ local tk_sync = loadmod('tk_sync')
local delay = 5 -- delaytime
local interval_time = '0100' -- time to run, here is 12:00 for each day
local interval_time = '0030' -- time to run, here is 12:00 for each day
-- local settledate = "2018-08-16"
local settledate = os.date('%Y-%m-%d', os.time())
local settletime = zx_base:date2time(settledate)
......@@ -58,10 +59,17 @@ handler = function(premature, interval_time, settletime)
-- node_smsvip: run()
-- node_vipacct: run()
tk_sync: run()
node_card: run(settletime)
node_ticket: run(settletime)
tk_sdt: run(settletime)
-- tk_sync: run()
-- node_card: run(settletime)
-- node_ticket: run(settletime)
-- tk_sdt: run(settletime)
local handle = io.popen("curl localhost:8080/settle")
if handle then
handle:close()
end
-- reset interval_time
interval_time = tostring(tonumber(interval_time) - 1)
......@@ -85,7 +93,6 @@ if ngx.worker.id() == 1 then
end
end
local SETTLE = {}
function SETTLE.run()
local args = ngx.req.get_uri_args()
local t
......
......@@ -22,7 +22,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
-- 新建mysql连接
local db_biz, err = mysql:new()
if not db_biz then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
zx_base:log("S", "failed to instantiate mysql: ", err)
return false
end
......@@ -40,14 +40,14 @@ local function sdt_sql(tk_biz, tk_control, total_time)
charset = 'utf8'
}
if not ok then
ngx.log(ngx.ERR, json.encode(tk_biz), ":", err, errcode, sqlstate)
zx_base:log("S", json.encode(tk_biz), ":", err, errcode, sqlstate)
return
end
-- 新建mysql连接
local db_control, err = mysql:new()
if not db_control then
ngx.log(ngx.ERR, "failed to instantiate mysql: ", err)
zx_base:log("S", "failed to instantiate mysql: ", err)
return false
end
......@@ -65,18 +65,19 @@ local function sdt_sql(tk_biz, tk_control, total_time)
charset = 'utf8'
}
if not ok then
ngx.log(ngx.ERR, json.encode(tk_control), ":", err, errcode, sqlstate)
zx_base:log("S", json.encode(tk_control), ":", err, errcode, sqlstate)
return
end
-- GET shop_map_id list
local shops
local sql_select = 'SELECT DISTINCT shop_map_id FROM node_saleorder UNION SELECT DISTINCT shop_map_id FROM node_rchg;'
-- local sql_select = 'SELECT DISTINCT shop_map_id FROM node_saleorder UNION SELECT DISTINCT shop_map_id FROM node_rchg;'
local sql_select = 'SELECT mer_map_id, shop_map_id FROM tk_shop'
shops, err, errcode, sqlstate = db_biz:query(sql_select)
ngx.log(ngx.NOTICE, sql_select)
if not shops then
ngx.log(ngx.ERR, "failed to query:", json.encode(tk_biz), ":", err, ":", errcode, ":", sqlstate, ".")
zx_base:log("S", "failed to query:", json.encode(tk_biz), ":", err, ":", errcode, ":", sqlstate, ".")
return
end
......@@ -89,9 +90,26 @@ local function sdt_sql(tk_biz, tk_control, total_time)
shop = shops[i]
-- -- CHECK if exists
-- local sql_do = string.format(
-- [[SELECT * FROM tk_sdt WHERE DATE(total_date) = DATE('%s') ]]..
-- [[AND shop_map_id = %s]],
-- os.date('%Y%m%d',os.time()),
-- tostring(shop.shop_map_id))
-- res, err, errcode, sqlstate = db_biz:query(sql_do)
-- if not res then
-- zx_base:log("S", "failed to connect:", json.encode(tk_biz), ":", err, ":", errcode, ":", sqlstate, ".")
-- zx_base:log("S", sql_do)
-- return
-- end
-- if #res ~= 0 then
-- ngx.log(ngx.NOTICE, shop['shop_map_id'], " finished already")
-- break
-- end
-- GET summary
table.insert(sql, string.format(
[[SELECT op_map_id, mer_map_id, shop_map_id, city_county_id, ]]..
[[SELECT op_map_id, city_county_id, ]]..
[[IFNULL(COUNT(*), 0) t_order_cnt, ]]..
[[IFNULL(SUM(amt_pay), 0) t_order_amt, ]]..
[[IFNULL(SUM(amt_profit), 0) t_order_profit, ]]..
......@@ -107,6 +125,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[1])
-- GET online records
table.insert(sql, string.format(
......@@ -119,6 +138,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[2])
-- GET VIP/NOVIP records
table.insert(sql, string.format(
......@@ -131,6 +151,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[3])
-- GET MS records
table.insert(sql, string.format(
......@@ -143,120 +164,135 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[4])
-- GET 0000-0900 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_0009, IFNULL(SUM(amt_pay), 0) time_amt_0009 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d000000', total_time),
os.date('%Y%m%d090000', total_time))
os.date('%Y%m%d000000', total_time-24*60*60),
os.date('%Y%m%d090000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[5])
-- GET 0900-1100 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_0911, IFNULL(SUM(amt_pay), 0) time_amt_0911 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d090000', total_time),
os.date('%Y%m%d110000', total_time))
os.date('%Y%m%d090000', total_time-24*60*60),
os.date('%Y%m%d110000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[6])
-- GET 1100-1300 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_1113, IFNULL(SUM(amt_pay), 0) time_amt_1113 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d110000', total_time),
os.date('%Y%m%d130000', total_time))
os.date('%Y%m%d110000', total_time-24*60*60),
os.date('%Y%m%d130000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[7])
-- GET 1300-1500 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_1315, IFNULL(SUM(amt_pay), 0) time_amt_1315 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d130000', total_time),
os.date('%Y%m%d150000', total_time))
os.date('%Y%m%d130000', total_time-24*60*60),
os.date('%Y%m%d150000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[8])
-- GET 1500-1700 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_1517, IFNULL(SUM(amt_pay), 0) time_amt_1517 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d150000', total_time),
os.date('%Y%m%d170000', total_time))
os.date('%Y%m%d150000', total_time-24*60*60),
os.date('%Y%m%d170000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[9])
-- GET 1700-1900 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_1719, IFNULL(SUM(amt_pay), 0) time_amt_1719 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d170000', total_time),
os.date('%Y%m%d190000', total_time))
os.date('%Y%m%d170000', total_time-24*60*60),
os.date('%Y%m%d190000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[10])
-- GET 1900-2100 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_1921, IFNULL(SUM(amt_pay), 0) time_amt_1921 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d190000', total_time),
os.date('%Y%m%d210000', total_time))
os.date('%Y%m%d190000', total_time-24*60*60),
os.date('%Y%m%d210000', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[11])
-- GET 2100-2400 order
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(*), 0) time_cnt_2124, IFNULL(SUM(amt_pay), 0) time_amt_2124 ]]..
[[FROM node_saleorder ]]..
[[WHERE shop_map_id = %s ]]..
[[AND order_status = 0 ]]..
[[AND `create_time` > '%s' AND `create_time` < '%s'; ]],
tostring(shop.shop_map_id),
os.date('%Y%m%d210000', total_time),
os.date('%Y%m%d235959', total_time))
os.date('%Y%m%d210000', total_time-24*60*60),
os.date('%Y%m%d235959', total_time-24*60*60))
)
-- ngx.log(ngx.NOTICE, sql[12])
-- get VIP cnt
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(vip_map_id), 0) t_vip_cnt ]]..
[[SELECT IFNULL(COUNT(DISTINCT(vip_map_id)), 0) t_vip_cnt ]]..
[[FROM node_vipacct ]]..
[[WHERE shop_map_id IN ]]..
[[(SELECT shop_map_id FROM tk_shop ]]..
[[WHERE shop_grp_id IN ]]..
[[(SELECT shop_grp_id FROM node_shopgrp ]]..
[[WHERE mer_map_id = (SELECT mer_map_id FROM tk_shop WHERE shop_map_id = %s)));]],
[[WHERE DATE(create_time) <= %s ]]..
[[AND mer_map_id = ]]..
[[(SELECT mer_map_id FROM tk_shop WHERE shop_map_id = %s);]],
os.date('%Y%m%d', total_time-24*60*60),
tostring(shop['shop_map_id']))
)
-- ngx.log(ngx.NOTICE, sql[13])
-- GET VIP new cnt
table.insert(sql, string.format(
[[SELECT IFNULL(COUNT(vip_map_id), 0) t_vip_newcnt ]]..
[[SELECT IFNULL(COUNT(DISTINCT(vip_map_id)), 0) t_vip_newcnt ]]..
[[FROM node_vipacct ]]..
[[WHERE DATE(create_time) = DATE('%s') ]]..
[[AND shop_map_id IN ]]..
[[(SELECT shop_map_id FROM tk_shop ]]..
[[WHERE shop_grp_id IN ]]..
[[(SELECT shop_grp_id FROM node_shopgrp ]]..
[[WHERE mer_map_id = (SELECT mer_map_id FROM tk_shop WHERE shop_map_id = %s)));]],
[[AND mer_map_id = ]]..
[[(SELECT mer_map_id FROM tk_shop WHERE shop_map_id = %s);]],
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop['shop_map_id']))
)
-- ngx.log(ngx.NOTICE, sql[14])
-- GET charge summary
table.insert(sql, string.format(
......@@ -267,6 +303,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[15])
-- GET cash summary
table.insert(sql, string.format(
......@@ -278,6 +315,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[16])
-- GET WeiXin summary
table.insert(sql, string.format(
......@@ -289,6 +327,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[17])
-- GET AliPay summary
table.insert(sql, string.format(
......@@ -300,6 +339,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[18])
-- GET bank summary
table.insert(sql, string.format(
......@@ -311,6 +351,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[19])
-- GET vipcard summary
table.insert(sql, string.format(
......@@ -322,6 +363,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[20])
-- GET othpay summary
table.insert(sql, string.format(
......@@ -333,11 +375,11 @@ local function sdt_sql(tk_biz, tk_control, total_time)
os.date("%Y%m%d", total_time-24*60*60),
tostring(shop.shop_map_id))
)
-- ngx.log(ngx.NOTICE, sql[21])
ngx.log(ngx.NOTICE, sql[1])
res, err, errcode, sqlstate = db_biz:query(table.concat(sql))
if not res then
ngx.log(ngx.ERR, "failed to query:", json.encode(tk_biz), ":", err, ":", errcode, ":", sqlstate, ".")
zx_base:log("S", "failed to query:", json.encode(tk_biz), ":", err, ":", errcode, ":", sqlstate, ".")
return
end
......@@ -345,31 +387,48 @@ local function sdt_sql(tk_biz, tk_control, total_time)
for k, v in pairs(res[1]) do
shop[k] = v
end
-- else
-- break
end
ngx.log(ngx.NOTICE,
string.format("================ TK_SDT [%s - %s] ==========",
tostring(shop['shop_map_id']),
os.date("%Y%m%d", total_time)))
for k, v in pairs(shop) do
ngx.log(ngx.NOTICE, string.format("%-32s : %s", k, tostring(v)))
end
local j = 2
while err == "again" do
ngx.log(ngx.NOTICE, sql[j])
-- repeat
res, err, errcode, sqlstate = db_biz:read_result()
if not res then
ngx.log(ngx.ERR, "bad result #", j, ": ", err, ": ", errcode, ": ", sqlstate, ".")
zx_base:log("S", "bad sql: ", sql[j])
zx_base:log("S", "bad result #", j, ": ", err, ": ", errcode, ": ", sqlstate, ".")
return ngx.exit(500)
end
ngx.log(ngx.NOTICE,
string.format("================ TK_SDT [%s - %s] ==========",
tostring(shop['shop_map_id']),
os.date("%Y%m%d", total_time)))
for k, v in pairs(shop) do
ngx.log(ngx.NOTICE, string.format("%-32s : %s", k, tostring(v)))
end
if #res ~= 0 then
for k, v in pairs(res[1]) do
shop[k] = v
end
j = j + 1
end
-- until(err ~= "again")
end
-- SET total_date
shop['total_date'] = zx_base:tonumber(os.date('%Y%m%d', total_time))
-- INSERT INTO tk_sdt
local str = zx_base:sql_concate('duplicate', 'tk_sdt', shop)
-- Convert type
shop['op_map_id'] = zx_base:tonumber(shop['op_map_id'])
shop['mer_map_id'] = zx_base:tonumber(shop['mer_map_id'])
......@@ -391,6 +450,7 @@ local function sdt_sql(tk_biz, tk_control, total_time)
shop['t_order_profit'] = zx_base:tonumber(shop['t_order_profit'])
shop['t_order_profit_per'] = zx_base:tonumber(shop['t_order_profit_per'])
shop['t_vip_cnt'] = zx_base:tonumber(shop['t_vip_cnt'])
-- shop['vip_cnt'] = zx_base:tonumber(shop['vip_cnt'])
shop['t_vip_newcnt'] = zx_base:tonumber(shop['t_vip_newcnt'])
shop['t_charge_cnt'] = zx_base:tonumber(shop['t_charge_cnt'])
shop['t_charge_amt'] = zx_base:tonumber(shop['t_charge_amt'])
......@@ -466,31 +526,35 @@ local function sdt_sql(tk_biz, tk_control, total_time)
shop['t_order_avg_price'] = 0
shop['t_order_profit_per'] = 0
else
shop['t_order_avg_price'] = shop['t_order_amt'] / shop['t_order_cnt']
shop['t_order_profit_per'] = shop['t_order_profit'] / shop['t_order_amt'] * 10000
-- shop['t_order_avg_price'] = shop['t_order_amt'] / (shop['vip_cnt'] + shop['t_order_novip_cnt'] - 1)
shop['t_order_avg_price'] = math.floor(shop['t_order_amt'] / shop['t_order_cnt'])
shop['t_order_profit_per'] = math.floor(shop['t_order_profit'] / shop['t_order_amt'] * 10000)
end
if shop['t_order_cnt'] == 0 then
break
end
-- if shop['t_order_cnt'] == 0 and shop['t_vip_newcnt'] == 0 and shop['t_vip_cnt'] == 0 then
-- break
-- end
-- INSERT INTO tk_sdt
local str = zx_base:sql_concate('duplicate', 'tk_sdt', shop)
-- INSERT INTO tk_biz database
res, err, errcode, sqlstate = db_biz:query(str)
ngx.log(ngx.NOTICE, str)
if not res then
ngx.log(ngx.ERR, "failed to query:", json.encode(tk_biz),
zx_base:log("S", "failed to query:", json.encode(tk_biz),
":", err, ":", errcode, ":", sqlstate, ".")
return
end
-- INSERT INTO tk_control database
res, err, errcode, sqlstate = db_control:query(str)
ngx.log(ngx.NOTICE, str)
if not res then
ngx.log(ngx.ERR, "failed to query:", json.encode(tk_control),
":", err, ":", errcode, ":", sqlstate, ".")
return
end
-- -- INSERT INTO tk_control database
-- res, err, errcode, sqlstate = db_control:query(str)
-- ngx.log(ngx.NOTICE, str)
-- if not res then
-- zx_base:log("S", "failed to query:", json.encode(tk_control),
-- ":", err, ":", errcode, ":", sqlstate, ".")
-- -- return
-- end
-- table.insert(shops, shop)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment