<?php

namespace app\common\model;

use liuniu\BaseModel;
use think\Exception;
use think\Model;


class Lave extends BaseModel
{

    // 表名
    protected $name = 'lave';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_open_text',
        'is_ticket_text',
        'paid_text',
        'pay_type_text',
        'paytime_text',
        'type_text'
    ];


    public function getIsOpenList()
    {
        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
    }

    public function getIsTicketList()
    {
        return ['0' => __('Is_ticket 0'), '1' => __('Is_ticket 1')];
    }

    public function getPaidList()
    {
        return ['0' => __('Paid 0'), '1' => __('Paid 1')];
    }

    public function getPayTypeList()
    {
        return ['0' => __('Pay_type 0'), '1' => __('Pay_type 1'), '2' => __('Pay_type 2'), '3' => __('Pay_type 3'), '4' => __('Pay_type 4')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1')];
    }


    public function getIsOpenTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_open']) ? $data['is_open'] : '');
        $list = $this->getIsOpenList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsTicketTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_ticket']) ? $data['is_ticket'] : '');
        $list = $this->getIsTicketList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaidTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paid']) ? $data['paid'] : '');
        $list = $this->getPaidList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPaytimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public static function lst($where)
    {
        $model = new self;
        $xwhere = null;
        $order = "id desc";
        if (isset($where['cid']) && $where['cid'] > 0) $xwhere['cid'] = $where['cid'];
        if (isset($where['user_id']) && $where['user_id'] > 0) $xwhere['user_id'] = $where['user_id'];
        if (isset($where['category_id']) && $where['category_id'] > 0) $xwhere['category_id'] = $where['category_id'];
        if (isset($where['paid']) && $where['paid'] > -1) $xwhere['paid'] = $where['paid'];
        if (isset($where['help_id']) && $where['help_id'] > -1) $xwhere['help_id'] = $where['help_id'];
        if (isset($where['order']) && $where['order'] != '') $order = $where['order'];
        if (isset($where['key']) && $where['key'] != '') $xwhere['name|contact|tel'] = ['like', "%{$where['key']}%"];
        $data = $model->where($xwhere)->order($order)->page($where['page'], $where['limit'])->select();
        $count = $model->where($xwhere)->count();
        return compact('count', 'data');
    }

    public static function lave_create($where)
    {
        try {
            if ($where['cid'] == 0 || $where['user_id'] == 0 || $where['amount'] <= 0 || empty($where['name']) || empty($where['tel'])) return self::setErrorInfo('请核对信息');
            if ($where['type'] == 1 && $where['contact'] == '') return self::setErrorInfo('请核对信息');
            $where['order_id'] = self::getNewOrderId();
            $rs = self::create($where);
            return $rs;
        } catch (Exception $e) {
            return self::setErrorInfo($e->getMessage());
        }
    }

    public static function getNewOrderId()
    {
        do {
            list($msec, $sec) = explode(' ', microtime());
            $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
            $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
        } while (self::where(['order_id' => $orderId])->find());
        return $orderId;
    }

    public static function paySuccess($cid, $order_id)
    {
        $max_sn = self::where('cid', $cid)->order('SN DESC')->value('SN');
        if ($max_sn) {
            $no = intval(explode("_", $max_sn)[1]) + 1;
        } else {
            $no = 1;
        }
        $SN = $cid . "_" . str_pad($no, 8, '0', STR_PAD_LEFT);
        $res = self::where('order_id', $order_id)->where('cid', $cid)->update(['paid' => 1, 'paytime' => time(), "SN" => $SN]);//订单改为支付
        return false !== $res;
    }

}
