<?php
namespace app\common\model;

use think\Db;

class AttendanceMachine extends Base{

    public function updates(){
        $data = request()->post();
        $data['org_id'] = cur_org_id();

        $result = validate('AttendanceMachine')->check($data,[],'');
        if(true !== $result){
            $this->error = validate('AttendanceMachine')->getError();
            return false;
        }
        $id = $data['id'];
        unset($data['id']);
        Db::startTrans();
        try{
            $mid = 0;
            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);
                $mid = $this->id;
            }
            if(!$ret){
                \exception('操作失败');
            }
            if($mid){
                // 导入超级管理员
                $admin = config('app.machine_admin');
                $ret = model('AttendanceMachineCmd')->updateAdminInfo($mid,$admin['id'],$admin['name'],$admin['card']);
                if(!$ret){
                    exception('操作失败');
                }
            }

            Db::commit();
            return true;
        }catch (Exception $e){
            $this->error = $e->getMessage();
            Db::rollback();
            return  false;
        }

    }

    public function info($id){
        $info = db('AttendanceMachine')->where('id',$id)->find();

        return $info;
    }

}