<?php


namespace app\models\merchant;


use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\db\exception\DbException;

class MerchantMiniprogram extends BaseModel
{
    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

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

    use ModelTrait;

    public static function vaildWhere()
    {
        return self::where('is_del', 0);
    }

    /**
     * 删除小程序绑定记录
     * @param $mer_id
     * @return bool
     */
    public static function delMiniprogram($mer_id)
    {
        try {
            if (!self::vaildWhere()->where(['mer_id' => $mer_id])->find()) {
                return self::setErrorInfo('绑定记录未找到或已删除');
            }
        } catch (DbException $e) {
            return self::setErrorInfo($e->getMessage());
        }
        $res = self::where('mer_id', $mer_id)->update(['is_del' => 1, 'update' => time()]);
        if ($res) {
            return true;
        } else {
            return self::setErrorInfo('删除失败');
        }

    }

    public static function checkOne($app_id, $sign, $sign_time)
    {
        $app_secret = self::where(['appid' => $app_id])->value('boofly_app_secret');
        if (!$app_secret) {
            return false;
        }
        return $sign == self::sign($app_id, $app_secret, $sign_time);
    }

    public static function sign($app_id, $app_secret, $time)
    {
        return md5(md5($app_id . $time . $app_secret . $time . $app_id . $app_secret));
    }

}