<?php
namespace Home\Model;

use Think\Model;

header("Content-Type:text/html;CharSet=UTF-8");
class OrderModel extends Model
{
    /**
     * 通过订单号 完成订单支付
     * @param  string $sn 订单号
     * @return boolean
     */
    public function payBySn($sn, $transaction_id)
    {
        if ($sn) {
            // 修改订单状态
            $data = array();
            $data['status'] = 2;
            $data['transaction_id'] = $transaction_id;
            $data['pay_time'] = $_SERVER['REQUEST_TIME'];
            $result = $this->where("`order_sn`='{$sn}'")->save($data);
        }
        if ($result) {
            $row = $this->where("`order_sn`='{$sn}'")->find();    // 订单记录
            $order_id = $row['id'];
            $village_id = $row['village_id'];
            $type = $row['type'];
            // 分订单状态同步修改
            M("orderSmi")->where("`order_id`={$order_id}")->setField("status", 2);
            // 增加对应商品的真实销量
            $orderGoodList = M("orderGood")->where("`order_id`={$order_id}")->select();    // 商品副表数组
            $len = count($orderGoodList);
            if (is_array($orderGoodList) && $len > 0) {
                foreach ($orderGoodList as $key => $value) {
                    $good_id = $value['good_id'];
                    $num = $value['num'];
                    M("goods")->where("`id`={$good_id}")->lock()->setInc("sales_volume", $num);
                    if ($type == 1) {
                        // 生成 补货订单
                        $data = array(
                            "order_id" => $order_id,
                            "good_id" => $good_id,
                            "supply_sn" => "BH" . $order_id . $good_id . date("YmdHis") . rand(10, 99),
                            "num" => $num,
                            "village_id" => $village_id,
                            "create_time" => $_SERVER['REQUEST_TIME']
                        );
                        M("orderSupply")->add($data);
                        // 减该村分仓库库存 补货订单送达时库存补回来
                        M("goodsStorage")->where("`village_id`={$village_id} AND `good_id`={$good_id}")->lock()->setDec("storage", $num);
                        // 生成分仓库出库记录
                        $data = array(
                            "choice" => 1,
                            "type" => 1,
                            "order_id" => $order_id,
                            "dinner_id" => 0,
                            "good_id" => $good_id,
                            "change_num" => $num,
                            "create_time" => $_SERVER['REQUEST_TIME']
                        );
                        M("storageVillage")->add($data);
                    }
                    // 减少总仓库库存
                    M("goodsStorage")->where("`village_id`=0 AND `good_id`={$good_id}")->lock()->setDec("storage", $num);
                    // 生成总仓库出库记录
                    $data = array(
                        "choice" => 1,
                        "type" => 1,
                        "order_id" => $order_id,
                        "dinner_id" => 0,
                        "good_id" => $good_id,
                        "change_num" => $num,
                        "create_time" => $_SERVER['REQUEST_TIME']
                    );
                    M("storageTotal")->add($data);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 订单支付金额判断
     * @param  string $sn  订单号
     * @param  string $fee 支付金额
     * @return boolean
     */
    public function payFeeCheck($sn = "", $fee = "")
    {
        if ($sn && $fee) {
            // 读取订单金额
            $realFee = $this->where("`order_sn`='{$sn}'")->getField("totalfee");
            $feeInt = $fee/100;
        }
        if ($realFee > 0 && ($feeInt == $realFee)) {
            return true;
        } else {
            return false;
        }
    }
}