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

class CitysConfig extends Model{

    protected $pk     = 'id';
    protected $json   = ['top'];
    protected $jsonAssoc = true;

    /**
     * 读取信息配置
     * @return void
     */
    public static function config(int $id){
        return self::where(['member_miniapp_id' => $id])->find();
    }

    /**
     * 配置
     * @return void
     */
    public static function edit(array $param,int $miniapp_id){
        $today = [];
        foreach ($param['today'] as $key => $value) {
            $today[$key]['day']   = $value;
            $today[$key]['money'] = $param['money'][$key] ?: 0;
        }
        $data['top']    = $today;
        $data['charge'] = $param['charge'];
        $data['vip']    = $param['vip'];
        $data['phone']  = $param['phone'];
        $data['mch_id'] = $param['mch_id'];
        $rel = self::where(['member_miniapp_id' => $miniapp_id])->find();
        if(empty($rel)){
            $data['member_miniapp_id'] = $miniapp_id;
            return self::create($data);
        }else{
            return self::where(['member_miniapp_id' => $miniapp_id])->update($data);
        }
    }
}