public function syn_data() { set_time_limit(0); es_session::close(); //同步,supplier_location表, deal表, youhui表, event表 , supplier 表 //总数 $page = intval($_REQUEST['p']) == 0 ? 1 : intval($_REQUEST['p']); if ($page == 1) { syn_dealing(); } $page_size = 5; $location_total = M("SupplierLocation")->count(); $deal_total = M("Deal")->count(); $youhui_total = M("Youhui")->count(); $event_total = M("Event")->count(); $supplier_total = M("Supplier")->count(); $count = max(array($location_total, $deal_total, $youhui_total, $event_total, $supplier_total)); $limit = ($page - 1) * $page_size . "," . $page_size; $location_list = M("SupplierLocation")->limit($limit)->findAll(); foreach ($location_list as $v) { recount_supplier_data_count($v['id'], "tuan"); recount_supplier_data_count($v['id'], "youhui"); recount_supplier_data_count($v['id'], "daijin"); recount_supplier_data_count($v['id'], "event"); recount_supplier_data_count($v['id'], "shop"); syn_supplier_location_match($v['id']); } $supplier_list = M("Supplier")->limit($limit)->findAll(); foreach ($supplier_list as $v) { syn_supplier_match($v['id']); } $deal_list = M("Deal")->limit($limit)->findAll(); foreach ($deal_list as $v) { syn_deal_match($v['id']); } $youhui_list = M("Youhui")->limit($limit)->findAll(); foreach ($youhui_list as $v) { syn_youhui_match($v['id']); } $event_list = M("Event")->limit($limit)->findAll(); foreach ($youhui_list as $v) { syn_event_match($v['id']); } if ($page * $page_size >= $count) { $this->assign("jumpUrl", U("Cache/index")); $ajax = intval($_REQUEST['ajax']); $data['status'] = 1; $data['info'] = "<div style='line-height:50px; text-align:center; color:#f30;'>同步成功</div><div style='text-align:center;'><input type='button' onclick='\$.weeboxs.close();' class='button' value='关闭' /></div>"; header("Content-Type:text/html; charset=utf-8"); exit(json_encode($data)); } else { $total_page = ceil($count / $page_size); $data['status'] = 0; $data['info'] = "共" . $total_page . "页,当前第" . $page . "页,等待更新下一页记录"; $data['url'] = U("Cache/syn_data", array("p" => $page + 1)); header("Content-Type:text/html; charset=utf-8"); exit(json_encode($data)); } }
function change_deal_status() { //$sql = "select id from ".DB_PREFIX."deal where is_effect = 1 and deal_status = 1 and is_delete = 0 AND load_money/borrow_amount < 1 AND (start_time + enddate*24*3600 - ".TIME_UTC.") <=0 "; /*$sql = "select id from ".DB_PREFIX."deal where is_effect = 1 and deal_status = 1 and is_delete = 0 AND load_money/borrow_amount <= 1 "; $deal_ids = $GLOBALS['db']->getAll($sql); foreach($deal_ids as $k=>$v) { syn_deal_status($v['id']); }*/ syn_dealing(); }
function clear_cache() { //系统后台缓存 syn_dealing(); syn_rebiding(); clear_dir_file(get_real_path() . "public/runtime/admin/Cache/"); clear_dir_file(get_real_path() . "public/runtime/admin/Data/_fields/"); clear_dir_file(get_real_path() . "public/runtime/admin/Temp/"); clear_dir_file(get_real_path() . "public/runtime/admin/Logs/"); @unlink(get_real_path() . "public/runtime/admin/~app.php"); @unlink(get_real_path() . "public/runtime/admin/~runtime.php"); @unlink(get_real_path() . "public/runtime/admin/lang.js"); @unlink(get_real_path() . "public/runtime/app/config_cache.php"); //数据缓存 clear_dir_file(get_real_path() . "public/runtime/app/data_caches/"); clear_dir_file(get_real_path() . "public/runtime/app/db_caches/"); $GLOBALS['cache']->clear(); clear_dir_file(get_real_path() . "public/runtime/data/"); //模板页面缓存 clear_dir_file(get_real_path() . "public/runtime/app/tpl_caches/"); clear_dir_file(get_real_path() . "public/runtime/app/tpl_compiled/"); @unlink(get_real_path() . "public/runtime/app/lang.js"); //脚本缓存 clear_dir_file(get_real_path() . "public/runtime/statics/"); }
function order_paid_done($order_id) { // 处理支付成功后的操作 /** * 1. * 发货 * 2. 超量发货的存到会员中心 * 3. 发券 * 4. 发放抽奖 */ require_once APP_ROOT_PATH . "system/libs/deal.php"; $order_id = intval($order_id); $stock_status = true; // 团购状态 $order_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal_order where id = " . $order_id); if ($order_info['type'] == 0) { // 首先验证所有的规格库存 $order_goods_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_id); foreach ($order_goods_list as $k => $v) { if ($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "attr_stock where deal_id = " . $v['deal_id'] . " and locate(attr_str,'" . $v['attr_str'] . "') > 0")) { $sql = "update " . DB_PREFIX . "attr_stock set buy_count = buy_count + " . $v['number'] . " where deal_id = " . $v['deal_id'] . " and ((buy_count + " . $v['number'] . " <= stock_cfg) or stock_cfg = 0 )" . " and locate(attr_str,'" . $v['attr_str'] . "') > 0 "; $GLOBALS['db']->query($sql); // 增加商品的发货量 $rs = $GLOBALS['db']->affected_rows(); if ($rs) { $affect_attr_list[] = $v; } else { $stock_status = false; break; } } } if ($stock_status) { $goods_list = $GLOBALS['db']->getAll("select deal_id,sum(number) as num from " . DB_PREFIX . "deal_order_item where order_id = " . $order_id . " group by deal_id"); foreach ($goods_list as $k => $v) { $sql = "update " . DB_PREFIX . "deal set buy_count = buy_count + " . $v['num'] . ",user_count = user_count + 1 where id=" . $v['deal_id'] . " and ((buy_count + " . $v['num'] . "<= max_bought) or max_bought = 0) " . " and time_status = 1 and buy_status <> 2"; $GLOBALS['db']->query($sql); // 增加商品的发货量 $rs = $GLOBALS['db']->affected_rows(); if ($rs) { $affect_list[] = $v; // 记录下更新成功的团购商品,用于回滚 } else { // 失败成功,即过期支付,超量支付 $stock_status = false; break; } } } if ($stock_status) { // 发货成功,发券 foreach ($goods_list as $k => $v) { // 为相应团购发券 $deal_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "deal where id = " . intval($v['deal_id'])); if ($deal_info['is_coupon'] == 1) { if ($deal_info['deal_type'] == 1) { $deal_order_item_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id'] . " and deal_id = " . $v['deal_id']); foreach ($deal_order_item_list as $item) { // for($i=0;$i<$item['number'];$i++) //按单 // { // 需要发券 /** * 1. * 先从已有团购券中发送 * 2. 无有未发送的券,自动发送 * 3. 发送状态的is_valid 都是 0, 该状态的激活在syn_deal_status中处理 */ $sql = "update " . DB_PREFIX . "deal_coupon set user_id=" . $order_info['user_id'] . ",order_id = " . $order_info['id'] . ",order_deal_id = " . $item['id'] . " where deal_id = " . $v['deal_id'] . " and user_id = 0 " . " and is_delete = 0"; $GLOBALS['db']->query($sql); $exist_coupon = $GLOBALS['db']->affected_rows(); if (!$exist_coupon) { // 未发送成功,即无可发放的预设团购券 add_coupon($v['deal_id'], $order_info['user_id'], 0, '', '', 0, 0, $item['id'], $order_info['id']); } // } } } else { $deal_order_item_list = $GLOBALS['db']->getAll("select * from " . DB_PREFIX . "deal_order_item where order_id = " . $order_info['id'] . " and deal_id = " . $v['deal_id']); foreach ($deal_order_item_list as $item) { for ($i = 0; $i < $item['number']; $i++) { // 需要发券 /** * 1. * 先从已有团购券中发送 * 2. 无有未发送的券,自动发送 * 3. 发送状态的is_valid 都是 0, * 该状态的激活在syn_deal_status中处理 */ $sql = "update " . DB_PREFIX . "deal_coupon set user_id=" . $order_info['user_id'] . ",order_id = " . $order_info['id'] . ",order_deal_id = " . $item['id'] . " where deal_id = " . $v['deal_id'] . " and user_id = 0 " . " and is_delete = 0 limit 1"; $GLOBALS['db']->query($sql); $exist_coupon = $GLOBALS['db']->affected_rows(); if (!$exist_coupon) { // 未发送成功,即无可发放的预设团购券 add_coupon($v['deal_id'], $order_info['user_id'], 0, '', '', 0, 0, $item['id'], $order_info['id']); } } } } } // 发券结束 } // 开始处理返还的积分或现金 require_once APP_ROOT_PATH . "system/libs/user.php"; if ($order_info['return_total_money'] != 0) { $msg = sprintf($GLOBALS['lang']['ORDER_RETURN_MONEY'], $order_info['order_sn']); modify_account(array('money' => $order_info['return_total_money'], 'score' => 0), $order_info['user_id'], $msg); } if ($order_info['return_total_score'] != 0) { $msg = sprintf($GLOBALS['lang']['ORDER_RETURN_SCORE'], $order_info['order_sn']); modify_account(array('money' => 0, 'score' => $order_info['return_total_score']), $order_info['user_id'], $msg); } // 开始处理返利,只创建返利, 发放将与msg_list的自动运行一起执行 $user_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . $order_info['user_id']); // 开始查询所购买的列表中支不支持促销 $is_referrals = 1; // 默认为返利 foreach ($goods_list as $k => $v) { $is_referrals = $GLOBALS['db']->getOne("select is_referral from " . DB_PREFIX . "deal where id = " . $v['deal_id']); if ($is_referrals == 0) { break; } } if ($user_info['referral_count'] < app_conf("REFERRAL_LIMIT") && $is_referrals == 1) { // 开始返利给推荐人 $parent_info = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . $user_info['pid']); if ($parent_info) { if (app_conf("REFERRAL_IP_LIMIT") == 1 && $parent_info['login_ip'] != get_client_ip() || app_conf("REFERRAL_IP_LIMIT") == 0) { if (app_conf("INVITE_REFERRALS_TYPE") == 0) { $referral_data['user_id'] = $parent_info['id']; // 初返利的会员ID $referral_data['rel_user_id'] = $user_info['id']; // 被推荐且发生购买的会员ID $referral_data['create_time'] = TIME_UTC; $referral_data['money'] = app_conf("INVITE_REFERRALS"); $referral_data['order_id'] = $order_info['id']; $GLOBALS['db']->autoExecute(DB_PREFIX . "referrals", $referral_data); // 插入 } else { $referral_data['user_id'] = $parent_info['id']; // 初返利的会员ID $referral_data['rel_user_id'] = $user_info['id']; // 被推荐且发生购买的会员ID $referral_data['create_time'] = TIME_UTC; $referral_data['score'] = app_conf("INVITE_REFERRALS"); $referral_data['order_id'] = $order_info['id']; $GLOBALS['db']->autoExecute(DB_PREFIX . "referrals", $referral_data); // 插入 } $GLOBALS['db']->query("update " . DB_PREFIX . "user set referral_count = referral_count + 1 where id = " . $user_info['id']); } } } // 超出充值 if ($order_info['pay_amount'] > $order_info['total_price']) { require_once APP_ROOT_PATH . "system/libs/user.php"; if ($order_info['total_price'] < 0) { $msg = sprintf($GLOBALS['lang']['MONEYORDER_INCHARGE'], $order_info['order_sn']); } else { $msg = sprintf($GLOBALS['lang']['OUTOFMONEY_INCHARGE'], $order_info['order_sn']); } $refund_money = $order_info['pay_amount'] - $order_info['total_price']; if ($order_info['account_money'] > $refund_money) { $account_money_now = $order_info['account_money'] - $refund_money; } else { $account_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set account_money = " . $account_money_now . " where id = " . $order_info['id']); if ($order_info['ecv_money'] > $refund_money) { $ecv_money_now = $order_info['ecv_money'] - $refund_money; } else { $ecv_money_now = 0; } $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set ecv_money = " . $ecv_money_now . " where id = " . $order_info['id']); modify_account(array('money' => $order_info['pay_amount'] - $order_info['total_price'], 'score' => 0), $order_info['user_id'], $msg); } // 生成抽奖 $lottery_list = $GLOBALS['db']->getAll("select d.id as did,doi.number from " . DB_PREFIX . "deal_order_item as doi left join " . DB_PREFIX . "deal_order as do on doi.order_id = do.id left join " . DB_PREFIX . "deal as d on doi.deal_id = d.id where d.is_lottery = 1 and do.id = " . $order_info['id']); $lottery_user = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . intval($order_info['user_id'])); // 如为首次抽奖,先为推荐人生成抽奖号 $lottery_count = $GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where user_id = " . intval($order_info['user_id'])); if ($lottery_count == 0 && $lottery_user['pid'] != 0) { $lottery_puser = $GLOBALS['db']->getRow("select * from " . DB_PREFIX . "user where id = " . intval($lottery_user['pid'])); foreach ($lottery_list as $lottery) { $k = 0; do { if ($k > 10) { break; } $buy_count = $GLOBALS['db']->getOne("select buy_count from " . DB_PREFIX . "deal where id = " . $lottery['did']); $max_sn = $buy_count - $lottery['number'] + intval($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where deal_id = " . intval($lottery['did']) . " and buyer_id <> 0 ")); // $max_sn = intval($GLOBALS['db']->getOne("select // lottery_sn from ".DB_PREFIX."lottery where deal_id = // '".$lottery['did']."' order by lottery_sn desc limit // 1")); $sn = $max_sn + 1; $sn = str_pad($sn, "6", "0", STR_PAD_LEFT); $sql = "insert into " . DB_PREFIX . "lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '" . $sn . "','" . $lottery['did'] . "'," . $lottery_puser['id'] . ",'" . $lottery_puser['lottery_mobile'] . "'," . TIME_UTC . "," . $order_info['user_id'] . " from dual where not exists( select * from " . DB_PREFIX . "lottery where deal_id = " . $lottery['did'] . " and lottery_sn = '" . $sn . "')"; $GLOBALS['db']->query($sql); send_lottery_sms(intval($GLOBALS['db']->insert_id())); $k++; } while (intval($GLOBALS['db']->insert_id()) == 0); } } foreach ($lottery_list as $lottery) { for ($i = 0; $i < $lottery['number']; $i++) { $k = 0; do { if ($k > 10) { break; } $buy_count = $GLOBALS['db']->getOne("select buy_count from " . DB_PREFIX . "deal where id = " . $lottery['did']); $max_sn = $buy_count - $lottery['number'] + intval($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "lottery where deal_id = " . intval($lottery['did']) . " and buyer_id <> 0 ")); // $max_sn = intval($GLOBALS['db']->getOne("select // lottery_sn from ".DB_PREFIX."lottery where deal_id = // '".$lottery['did']."' order by lottery_sn desc limit // 1")); $sn = $max_sn + $i + 1; $sn = str_pad($sn, "6", "0", STR_PAD_LEFT); $sql = "insert into " . DB_PREFIX . "lottery (`lottery_sn`,`deal_id`,`user_id`,`mobile`,`create_time`,`buyer_id`) select '" . $sn . "','" . $lottery['did'] . "'," . $order_info['user_id'] . "," . $lottery_user['lottery_mobile'] . "," . TIME_UTC . ",0 from dual where not exists( select * from " . DB_PREFIX . "lottery where deal_id = " . $lottery['did'] . " and lottery_sn = '" . $sn . "')"; $GLOBALS['db']->query($sql); send_lottery_sms(intval($GLOBALS['db']->insert_id())); $k++; } while (intval($GLOBALS['db']->insert_id()) == 0); } } } else { // 开始模拟事务回滚 foreach ($affect_attr_list as $k => $v) { $sql = "update " . DB_PREFIX . "attr_stock set buy_count = buy_count - " . $v['number'] . " where deal_id = " . $v['deal_id'] . " and locate(attr_str,'" . $v['attr_str'] . "') > 0 "; $GLOBALS['db']->query($sql); // 回滚已发的货量 } foreach ($affect_list as $k => $v) { $sql = "update " . DB_PREFIX . "deal set buy_count = buy_count - " . $v['num'] . ",user_count = user_count - 1 where id=" . $v['deal_id']; $GLOBALS['db']->query($sql); // 回滚已发的货量 } // 超出充值 require_once APP_ROOT_PATH . "system/libs/user.php"; $msg = sprintf($GLOBALS['lang']['OUTOFSTOCK_INCHARGE'], $order_info['order_sn']); modify_account(array('money' => $order_info['total_price'], 'score' => 0), $order_info['user_id'], $msg); // 将订单的extra_status 状态更新为2,并自动退款,结单 $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set extra_status = 2, after_sale = 1, refund_money = pay_amount, order_status = 1 where id = " . intval($order_info['id'])); // 记录退款的订单日志 $log['log_info'] = $msg; $log['log_time'] = TIME_UTC; $log['order_id'] = intval($order_info['id']); $GLOBALS['db']->autoExecute(DB_PREFIX . "deal_order_log", $log); } // 同步所有未过期的团购状态 syn_dealing(); } else { // 订单充值 $GLOBALS['db']->query("update " . DB_PREFIX . "deal_order set order_status = 1 where id = " . $order_info['id']); // 充值单自动结单 require_once APP_ROOT_PATH . "system/libs/user.php"; $msg = sprintf($GLOBALS['lang']['USER_INCHARGE_DONE'], $order_info['order_sn']); modify_account(array('money' => $order_info['total_price'] - $order_info['payment_fee'], 'score' => 0), $order_info['user_id'], $msg); } }