<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 会员卡管理
 */
namespace app\allwin\model;
use app\common\facade\Inform;
use think\Model;

class Card extends Model{

    protected $pk = 'id';
    protected $table = 'ai_allwin_card';

    /**
     * 所属店铺
     * @return void
     */
    public function store(){
        return $this->hasOne('AllwinStore','id','store_id');
    }

    /**
     * 所属优惠券
     * @return void
     */
    public function coupon(){
        return $this->hasOne('Coupon','id','coupon_id');
    } 

    /**
     * 编辑创客的关联的优惠券
     * @param integer $vip_id
     * @param [type] $coupon_ids
     * @return void
     */
    public static function editCoupon(int $id,array $coupon_ids){
        $info = self::where(['id' => $id])->find();
        if(empty($info->coupon_ids)){
            $info->coupon_ids = implode(',',$coupon_ids);
        }else{
            $coupon_ida = explode(',',$info->coupon_ids);
            $ida        = array_merge($coupon_ida,$coupon_ids);
            $info->coupon_ids = implode(',',$ida);
        }
        return $info->save();
    }

    /**
     * 锁定或取消
     * @param integer $id
     */
    public  static function isLock(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_lock,store_id')->find();
        $data['is_lock'] = $result['is_lock'] ? 0 : 1;
        if($data['is_lock'] == 0) {
            $store = AllwinStore::where('id', '=', $result->store_id)->find();
            if ($store) {
                //通知申请者到微信
                Inform::sms($store->manage_uid,$member_miniapp_id,['title' =>'业务进展通知','type' => '储值活动申请','content' =>'您的储值活动申请已经通过审核','state' => '成功']);
            }
        }
        return self::where('id',$id)->update($data);
    }
}