<?php
declare (strict_types = 1);

namespace app\model\system;

use library\basic\BaseModel;
use library\traits\JwtAuthModelTrait;
use library\traits\ModelTrait;
use think\Model;

/**
 * @mixin \think\Model
 */
class Admin extends BaseModel
{
    use ModelTrait;
    use JwtAuthModelTrait;




    /**
     * 管理注册写入
     * @param $post
     */
    public static function saveAdmin($post) {
        $save = ['name'     => $post['name'],
                 'role_id'  => $post['role_id'],
                 'avatar'   => $post['avatar'],
                 'status'   => $post['status'],
                'sassid'    => $post['sassId']
            ];
        //是否密码写入
        if(!empty($post['password'])) {
            $save['password'] = password_hash($post['password'],PASSWORD_BCRYPT);
        }
        if($post['id']) {
            //
            $info = self::where("username",$post['username'])->where('sassid',$save['sassid'])->find();
            if(!empty($info) && $info['id'] != $post['id'] ) {
                return self::setErrorInfo('登录的账号已存在');
            }
            //判断密码
            if(empty($save['password'])) {
                unset($post['password']);
            }
            self::where('id',$post['id'])->save($save);
        } else {
            $save['time'] = time();
            $save['last_time'] = time();
            $save['username'] = $post['username'];
            $count = self::where("username",$post['username'])->where('sassid',$save['sassid'])->count();
            if($count > 0) {
                return self::setErrorInfo('登录的账号已存在');
            }

            if(empty($save['password'])) {
                return self::setErrorInfo('请输入登录密码');
            }
            self::insert($save);
        }

        return true;
    }
}
