function settlement()
{
    global $gPrices;
    $products = getProducts();
    $deals = getDeals();
    $mysqli = getMysqli();
    $mysqli->autocommit(false);
    // 开始事务
    // 插入操作
    foreach ($deals as $value) {
        $productId = $value['productId'];
        //$deal->getProductId();
        $product = $products[$productId];
        $contract = $product->getContract();
        // 合同名
        $breakeven = $product->getBreakeven();
        // 产品盈亏金额
        $weight = $product->getWeight();
        // 产品重量
        $price = $product->getPrice();
        // 产品价格
        $orderId = $value['orderId'];
        // 交易编号
        $wid = $value['wid'];
        // 微信号	$deal->getWid();
        $deposit = $value['rmoney'];
        // 保证金	$deal->getDeposit();
        $buy = $value['buyprice'];
        // 进仓价   $deal->getBuy();
        $sell = $value['sellprice'];
        // 平仓价 	$deal->getSell();
        $amount = $value['sl'];
        // 数量		$deal->getAmount();
        $type = $value['type'];
        // 买卖类型 1:跌,2:涨	$deal->getType();
        $profit = $value['zhuanqu'];
        // 收益		$deal->getProfit();
        $time = $value['addtime'];
        // 买入时间	$deal->getDate();
        if ($sell == 0) {
            $sell = $gPrices[$contract];
            // 按产品名取报价,设为平仓价
            if ($type == 1) {
                // 买跌:(进仓价-平仓价或11点的价格)* 手数 * 盈亏金额
                $profit = ($buy - $sell) * $amount * $breakeven;
            } else {
                // 买涨:(平仓价或11点的价格-进仓价)* 手数 * 盈亏金额
                $profit = ($sell - $buy) * $amount * $breakeven;
            }
        }
        $sql = "INSERT INTO settlement (orderId, wid, deposit, buyprice, sellprice, profit, type, amount, addtime, pId, pBreakeven, pWeight, pPrice, pContract) \r\n\t\t\tVALUES('{$orderId}', '{$wid}', '{$deposit}', '{$buy}', '{$sell}', '{$profit}', '{$type}', '{$amount}', '{$time}', '{$productId}', '{$breakeven}', '{$weight}', '{$price}', '{$contract}')";
        $mysqli->query($sql);
    }
    if (!$mysqli->errno) {
        $mysqli->commit();
    } else {
        $mysqli->rollback();
    }
    mysqli_close($mysqli);
}
Exemplo n.º 2
0
function crawl($tname, $year, $monthArr)
{
    $dealType = $GLOBALS['dealType'];
    $stateArr = $GLOBALS['stateArr'];
    if ($monthArr == null) {
        die("monthArr must be set!");
    }
    $period = intval(($monthArr[0] - 1) / 3) + 1;
    echo "Working on {$year} ({$period}) " . implode(" ", $monthArr) . "\n";
    $db = new mysqli("p:localhost", "trend", "only!trend!", "rtrend");
    // Check connection
    if ($db->connect_error) {
        die("Connection failed: " . $db->connect_error);
    }
    // Set utf8
    //$db->set_charset("utf8");
    if (!isset($dealType[$tname])) {
        die("No args for {$tname}\n");
    }
    // get deal type and table name
    $args = $dealType[$tname];
    foreach ($stateArr as $state => $stateCode) {
        echo "Working on {$year} ({$period}) on {$state} for {$tname}\n";
        $cities = getCities($year, $period, $stateCode, $args);
        $cityArr = json_decode($cities, true);
        foreach ($cityArr['jsonList'] as $city) {
            $counties = getCounties($year, $period, $stateCode, $city['CODE'], $args);
            $countyArr = json_decode($counties, true);
            foreach ($countyArr['jsonList'] as $county) {
                $deals = getDeals($year, $period, $stateCode, $city['CODE'], $county['CODE'], $args);
                $dealArr = json_decode($deals, true);
                foreach ($monthArr as $month) {
                    // echo("Working on $year/$month ($period) on $state " . $city['NAME'] . " " . $county['NAME'] . "\n");
                    $infoArr = ['year' => $year, 'month' => $month, 'state' => $state, 'city' => $city['NAME'], 'county' => $county['NAME']];
                    // update
                    // function update($db, $tname, $metaArr, $json) {
                    update($db, $tname, $infoArr, $deals);
                }
            }
        }
    }
    // make aggregation
    foreach ($monthArr as $month) {
        echo "Make agg {$tname} on {$year}/{$month}";
        mkagg($db, $tname, $year, $month);
    }
    $db->close();
}