<?php
namespace app\common\model;

use think\Db;

class Security extends Base
{

    public function updates(){
        $data = request()->post();
        $data['org_id'] = cur_org_id();
        $result = validate('Security')->check($data,[],'');

        if(true !== $result){
            $this->error = validate('Security')->getError();
            return false;
        }
        $id = $data['id'];
        unset($data['id']);

        if($data['sn']){
            $deviceLog = Db::name('device_log')->where('sn',$data['sn'])->where('type',4)->where('del',0)->find();
            $txt = '添加';
            if($id > 0){
                $txt = '修改';
            }
            if(!$deviceLog){
                $this->error = '该设备无法'.$txt.',请联系管理员';
                return false;
            }

        }

        if($id > 0){
            $data['update_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data,['id'=>$id]);
        }else{
            $data['create_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data);
        }
        if(!$ret){
            $this->error = '操作失败';
            return false;
        }

        return true;
    }

}