<?php
namespace Smi\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class OrderModel extends Model
{

    /**
     * 订单改价
     * @param  int       $id 订单ID
     * @return boolean
     */
    public function changePrice($id, $newPrice)
    {
        if ($id) {
            $newSn = $this->createSnByID($id);
            $data = array("ordersn" => $newSn, "totalfee" => $newPrice);
            $result = $this->where("`id` = $id")->save($data);
            if ($result) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 生成订单号
     * @param  int     $id 订单ID
     * @return string
     */
    public function createSnByID($id)
    {
        if ($id) {
            $row = M("order")
            ->join("o LEFT JOIN `xcx_users` u ON o.`openid` = u.`openid`")
            ->where("o.`id` = '$id'")
            ->field("u.`id`")
            ->find();
            $userId = $row['id'];
            return "SN" . $userId . date("YmdHis") . "c" . session("admin_id");
        } else {
            return "SN" . date("YmdHis") . rand(0, 999);
        }
    }
}