<?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 CitysTpl extends Model{
    
    protected $pk    = 'id';
    protected $autoWriteTimestamp = true;
    
    /**
     * 保存数据
     * @param  string $array 传入的保存数据的参数
     * @return json
     */
    public static function edit(array $param){
        //处理规格参数
        $title       = $param['title'];
        $types       = $param['types'];
        $values      = $param['values'];
        $placeholder = $param['placeholder'];
        //判断新增还是修改
        foreach ($param['title'] as $key => $value) {
            $spec_value_data[$key]['title']       = $title[$key];
            $spec_value_data[$key]['types']       = $types[$key];
            $spec_value_data[$key]['placeholder'] = $placeholder[$key];
            if($types[$key] == 'selector'){
                if(empty($values[$key])){
                    $spec_value_data[$key]['values'] = [];
                }else{
                    $spec_value_data[$key]['values'] = explode('|',$values[$key]);
                }
            }else{
                $spec_value_data[$key]['values'] = $values[$key];
            }
        }
        $spec_data['fields']        = json_encode($spec_value_data);
        $spec_data['name']          = $param['name'];
        $spec_data['update_time']   = time();
        if(empty($param['id'])){
            $spec_data['member_miniapp_id'] = $param['member_miniapp_id'];
            $spec_data['users'] = '[]';
            $rel = self::insertGetId($spec_data);
        }else{
            $rel = self::where(['id' => $param['id']])->update($spec_data);
        }
        if(empty($rel)){
            return enjson(403);
        }
        return enjson(200);
    } 

   /**
     * 保存数据
     * @param  string $array 传入的保存数据的参数
     * @return json
     */
    public static function usersEdit(array $param){
        //处理规格参数
        $title       = $param['title'];
        $types       = $param['types'];
        $values      = $param['values'];
        $placeholder = $param['placeholder'];
        //判断新增还是修改
        foreach ($param['title'] as $key => $value) {
            $spec_value_data[$key]['title']       = $title[$key];
            $spec_value_data[$key]['types']       = $types[$key];
            $spec_value_data[$key]['placeholder'] = $placeholder[$key];
            if($types[$key] == 'selector'){
                if(empty($values[$key])){
                    $spec_value_data[$key]['values'] = [];
                }else{
                    $spec_value_data[$key]['values'] = explode('|',$values[$key]);
                }
            }else{
                $spec_value_data[$key]['values'] = $values[$key];
            }
        }
        $spec_data['users']         = json_encode($spec_value_data);
        $spec_data['button_name']   = $param['name'];
        $spec_data['tips']          = $param['tips'];
        $spec_data['is_shop']       = $param['is_shop'];
        $spec_data['update_time']   = time();
        if(empty($param['id'])){
            $spec_data['member_miniapp_id'] = $param['member_miniapp_id'];
            $spec_data['fields'] = '[]';
            $rel = self::insertGetId($spec_data);
        }else{
            $rel = self::where(['id' => $param['id']])->update($spec_data);
        }
        if(empty($rel)){
            return enjson(403);
        }
        return enjson(200);
    }
}