<?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\ais\model;
use think\Model;

class AisConfig extends Model{

    protected $pk = 'id';

    //配置表
    public static function getConfig(int $miniapp_id){
        return self::where(['member_miniapp_id' => $miniapp_id])->find();
    }

   //获取所有配置
    public static function getAllConfig(){
        return self::select();
    }
    
    //编辑
    public static function configs(array $param,int $miniapp_id){
        $rel = self::where(['member_miniapp_id' => $miniapp_id])->find();
        if(empty($rel)){
            $param['member_miniapp_id'] = $miniapp_id;
            return self::insert($param);
        }else{
            return self::where(['member_miniapp_id' => $miniapp_id])->update($param);
        }
    }

    /**
     * 城市号配置
     * @return void
     */
    public static function info(array $param,int $miniapp_id){
        $today = [];
        foreach ($param['info'] as $key => $value) {
           switch ($key) {
                case 1:
                    $today[$key]['day'] = 7;
                    break;
                case 2:
                    $today[$key]['day'] = 15;
                    break;
                case 3:
                    $today[$key]['day'] = 30;
                    break; 
               default:
                    $today[$key]['day'] = 2;
                    break; 
           }
           $today[$key]['money'] = $value;
        }
        return self::configs(['info' => json_encode($today)],$miniapp_id);
    }
}