<?php
namespace Smi\Model;

use Think\Model;

header("Content-type:text/html;charset=utf-8");
class FundApplyModel extends Model
{
    /**
     * 企业付款成功
     * @param  int    $id         提现申请ID
     * @param  string $payment_no 提现申请ID
     * @return mix
     */
    public function paySuccess($id, $payment_no)
    {
        if (!$id) {
            die("无法获取提现订单");
        } else {
            $status = $this->where("`id`={$id}")->getField("status");
            if ($status == 2) {
                return false;
            } else {
                // 申请记录状态修改
                $data = array();
                $data['payment_no'] = $payment_no;
                $data['status'] = 2;
                $result = $this->where("`id`=$id")->save($data);
                return $result;
            }
        }
    }

    /**
     * 拒绝申请
     * @return 1/false
     */
    public function refuse()
    {
        $id = $_POST['id'];
        if ($id) {
            $data['reason'] = I("reason");
            $data['status'] = -1;
            $data['dealtime'] = $_SERVER['REQUEST_TIME'];
            return $this->where("`id`={$id}")->save($data);
        } else {
            return false;
        }
    }

    /**
     * 撤回拒绝
     * @param  int      $id 申请记录ID
     * @return 1/false
     */
    public function retract($id)
    {
        if ($id) {
            $data['status'] = 1;
            $data['dealtime'] = $_SERVER['REQUEST_TIME'];
            return $this->where("`id`={$id}")->save($data);
        } else {
            return false;
        }
    }

    /**
     * 生成改价订单号
     * @param  int $id 提现申请记录ID
     * @return string
     */
    public function changePartnerTradeNo($id)
    {
        // 用户ID
        if ($id) {
            $openid = $this->where("`id`={$id}")->getField("openid");
        }
        if ($openid) {
            $userId = M("users")->where("`openid`='$openid'")->getField("id");
        }
        if ($userId) {
            return "WX" . $userId . date("YmdHis") . "c" . session("admin_id");
        } else {
            return "WX" . date("YmdHis") . rand(0, 999);
        }
    }
}