<?php
/**
 *
 */
include_once(SERVER_ROOT."/model/mBase.php");


class mOrder extends mBase {
    private $obj;
    private $order;

    public function __construct() {
        $this->obj = new dOrder();
        $this->order = 'order_list';
    }

    public function addOrder($order_sn, $order_status, $refund_status, $pay_amount, $pay_time, $shop_id, $goods_id, $sku_id, $uid) {
        $data = array();
        $data['order_status'] = $order_status;
        $data['refund_status'] = $refund_status;
        $data['pay_amount'] = $pay_amount;
        $data['pay_time'] = $pay_time;
        $data['shop_id'] = $shop_id;
        $data['goods_id'] = $goods_id;
        $data['sku_id'] = $sku_id;
        $data['uid'] = $uid;

        $info = $this->getOrderBySn($order_sn);
        if ($info) {
            $res = $this->updateOrderBySn($order_sn, $data);
            if (!$res) {
                $this->setError('更新订单信息失败');
                return false;
            }
            return $info['id'];
        }

        $data['order_sn'] = $order_sn;

        return $this->obj->insert($this->order, $data);
    }

    public function getOrderBySn($order_sn) {
        return $this->obj->select($this->order, array('sql'=>'`order_sn`=?', 'vals'=>array($order_sn)));
    }

    public function updateOrderBySn($order_sn, $data) {
        return $this->obj->update($this->order, $data, array('sql'=>'`order_sn`=?', 'vals'=>array($order_sn)));
    }

    public function getOrdersCount($uid, $shop_id, $order_status=0, $refund_status=0) {
        $where = array('sql'=>'1=1');

        if ($uid) {
            $where['sql'] .= ' and `uid`=?';
            $where['vals'][] = $uid;
        }

        if ($shop_id) {
            $where['sql'] .= ' and `shop_id`=?';
            $where['vals'][] = $shop_id;
        }

        if ($order_status) {
            $where['sql'] .= ' and `order_status`=?';
            $where['vals'][] = $order_status;
        }

        if ($refund_status) {
            $where['sql'] .= ' and `refund_status`=?';
            $where['vals'][] = $refund_status;
        }

        return $this->obj->count($this->order, $where);
    }

    public function getOrderList($uid, $shop_id, $order_status=0, $refund_status=0, $page=1, $pagesize=100) {
        $where = array('sql'=>'1=1');

        if ($uid) {
            $where['sql'] .= ' and `uid`=?';
            $where['vals'][] = $uid;
        }

        if ($shop_id) {
            $where['sql'] .= ' and `shop_id`=?';
            $where['vals'][] = $shop_id;
        }

        if ($order_status) {
            $where['sql'] .= ' and `order_status`=?';
            $where['vals'][] = $order_status;
        }

        if ($refund_status) {
            $where['sql'] .= ' and `refund_status`=?';
            $where['vals'][] = $refund_status;
        }

        $start = ($page - 1) * $pagesize;
        return $this->obj->selectAll($this->order, $where, 'pay_time desc', array($start, $pagesize));
    }

    public function getNotFinishedOrders() {
        return $this->obj->selectAll($this->order, array('sql'=>'`order_status`!=? and `refund_status`!=?', 'vals'=>array(ORDER_STATUS_SIGNED_FOR, REFUND_STATUS_SUCC)));
    }

    public function getLastThirtyDaysOrderNum($uid, $shop_id) {
        $start_date = date("Y-m-d", strtotime("-30 day"));

        $sql = "select DATE_FORMAT(`pay_time`,'%Y-%m-%d') sale_day, count(*) as count from {$this->order} where `uid`={$uid} and `shop_id`={$shop_id} and `pay_time`>='{$start_date}' group by sale_day order by sale_day asc";
        $list = $this->obj->execute($sql, true, true);
        if (!$list) return array();

        $date2count = array();
        foreach ($list as $info) {
            $date2count[$info['sale_day']] = $info['count'];
        }

        unset($list);

        return $date2count;
    }

    public function addOrders($order_list, $shop_id, $uid) {
        $goods_list = array();

        foreach ($order_list as $order) {
            $this->addOrder($order['order_sn'], $order['order_status'], $order['refund_status'], $order['pay_amount'], $order['pay_time'], $shop_id, $order['item_list'][0]['goods_id'], $order['item_list'][0]['sku_id'], $uid);

            $g = array();
            $g['goods_id'] = $order['item_list'][0]['goods_id'];
            $g['goods_name'] = $order['item_list'][0]['goods_name'];
            $g['goods_price'] = $order['item_list'][0]['goods_price'];
            $g['sku_id'] = $order['item_list'][0]['sku_id'];
            $g['sku_name'] = $order['item_list'][0]['goods_spec'];
            $g['shop_id'] = $shop_id;
            $g['uid'] = $uid;

            $goods_list[] = $g;
        }

        $gobj = new mGoods();
        foreach ($goods_list as $goods) {
            $gobj->addGoods($goods['goods_id'], $goods['goods_name'], $goods['goods_price'], $goods['sku_id'], $goods['sku_name'], $goods['shop_id'], $goods['uid']);
        }

        return true;
    }
}