<?php

namespace app\models\store;

use crmeb\services\UtilService;
use crmeb\traits\ModelTrait;
use crmeb\basic\BaseModel;

/**
 * TODO 充值卡Model
 * Class StoreRechargeCard
 * @package app\models\store
 */
class StoreRechargeCard extends BaseModel
{
	/**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'store_recharge_card';

    use ModelTrait;

    public static function merSet($mer_id, $alias = '')
    {
        return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
    }

    protected function getAddTimeAttr($value)
    {
        if ($value) return date('Y-m-d H:i:s', $value);
        return '';
    }

    /**
     * 充值卡列表
     * @param $where
     * @return array
     */
    public static function systemPage($where)
    {
        $model = self::setWhere($where);
        $count = $model->count();
        $list = $model->page((int)$where['page'], (int)$where['limit'])
            ->select()
            ->each(function ($item) {
                $item['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $item['id']])->order('role asc, level asc')->select();
            });
        return compact('count', 'list');
    }

    /**
     * 设置充值卡 where 条件
     * @param $where
     * @param null $model
     * @return mixed
     */
    public static function setWhere($where, $model = null)
    {
        $model = $model === null ? new self() : $model;
        $model = $model->where('mer_id', $where['mer_id']);
        if ($where['is_brokerage'] != '') $model = $model->where('is_brokerage', $where['is_brokerage']);
        if (isset($where['status']) && $where['status'] != '') $model = $model->where('status', $where['status']);
        if (isset($where['card_name']) && $where['card_name'] != '') $model = $model->where('id|title', 'LIKE', "%$where[card_name]%");
        return $model->order('id desc')->where('is_del', 0);
    }

    /**
     * 详情
     */
    public static function getOne($id)
    {
        $info = self::where('is_del', 0)->find($id);
        if ($info) {
            if ($info['start_time'])
                $start_time = date('Y-m-d H:i:s', $info['start_time']);

            if ($info['end_time'])
                $end_time = date('Y-m-d H:i:s', $info['end_time']);
            if (isset($start_time) && isset($end_time))
                $info['section_time'] = [$start_time, $end_time];
            else
                $info['section_time'] = [];
            unset($info['start_time'], $info['end_time']);
        }
        if ($info['poster'])
            $info['poster'] = json_decode($info['poster'], true);
        else
            $info['poster'] = [];
        $info['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $id])->order('role asc, level asc')->select();
        return $info;
    }

    /**
     * 获取充值卡数据
     * @param int $page
     * @param int $limit
     * @return mixed
     */
    public static function getAll($mer_id = '')
    {
        $model = self::merSet($mer_id);
        $model = $model->where('status', 1);
        $model = $model->where('is_del', 0);
        $model = $model->where('start_time', '<', time());
        $model = $model->where('end_time', '>', time());
        return $model->select()->each(function ($item) {
            $item['self_brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $item['id'], 'role' => 1])->find();
            $item['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $item['id'], 'role' => 2])->order('level asc')->select();
        });
    }

    /**
     * 获取一条充值卡数据
     * @param $id
     * @return mixed
     */
    public static function getRechargeCardOne($id)
    {
        $model = new self();
        $model = $model->where('id', $id);
        $model = $model->where('status', 1);
        $model = $model->where('is_del', 0);
        $model = $model->where('start_time', '<', time());
        $model = $model->where('end_time', '>', time());
        $info = $model->find();
        if ($info['poster'])
            $info['poster'] = json_decode($info['poster'], true);
        else
            $info['poster'] = [];
        if (!empty($info['price'])) $info['price'] = floatval($info['price']);
        if (!empty($info['give_money'])) $info['give_money'] = floatval($info['give_money']);
        $info['brokerage'] = StoreRechargeCardBrokerage::where(['cid' => $id])->order('role asc, level asc')->select();
        if (isset($info['id'])) {
            return $info;
        } else {
            return [];
        }
    }
}