<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/9/15
 * Time: 下午6:03
 */

define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));

require_once(BASE_ROOT_PATH . '/fooder.php');

class TestGoods extends PHPUnit_Framework_TestCase
{
    public static function setUpBeforeClass()
    {
        Base::run_util();
    }

    public function testAddSku()
    {
        $path = BASE_DATA_PATH . "/sales/sku.csv";
        $lines = explode("\r",$this->read_file($path));

        $mod_price = Model('');
        foreach($lines as $line)
        {
            $row = explode(';',$line);

            $omsid = intval($row[0]);
            $market_price = intval($row[1] * 100 + 0.5) / 100;
            $purchase_price = intval($row[2] * 100 + 0.5) / 100;
            $sku_price = intval($row[3] * 100 + 0.5) / 100;

            if($omsid <= 0) continue;

            $val = ['omsid' => $omsid,'purchase_price' => $purchase_price,'market_price' => $market_price,'sku_price' => $sku_price];
            $ret = $mod_price->table('goods_orgprice')->insert($val);
            if($ret == false) {
                Log::record("cannot insert row",Log::DEBUG);
            }
        }
    }

    private function read_file($path)
    {
        $file = fopen($path, "r");
        $datas = '';
        while (!feof($file)) {
            $datas .= fgets($file);
        }
        return $datas;
    }

    public function testEvaluate()
    {
        $mod = Model('goods');
        $i = 0;
        while (true)
        {
            $start = $i * 1000;
            $items = Model()->table('evaluate_goods')->field('geval_commonid,geval_goodsid')->order('geval_id asc')->limit("{$start},1000")->select();
            if(empty($items)) {
                return;
            }
            $i++;

            foreach ($items as $item)
            {
                $cid = intval($item['geval_commonid']);
                if($cid <= 0) continue;
                $mod->editGoodsCommon(['comments' => array('exp', "comments+1")],['goods_commonid' => $cid]);
            }
        }
    }
}
