<?php
/**
 * Created by PhpStorm.
 * User: qiuzijian
 * Date: 2021-04-16
 * Time: 10:52
 */

namespace Modules\Admin\Services;

use App\User;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Modules\Admin\Enum\AdminEnum;
use Modules\Admin\Enum\UserEnum;

class UsersService{

    //添加用户
    public static function addAdminUser($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        //判断该用户编码是否已存在
        $query = User::where('staff_num', $params['staff_num'])->first();
        if ($query) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::USER_ALREADY_EXIST;
            return $result;
        }

        //入库动作
        $res = User::insert(
            [
                'name'       => $params['name'],
                'mobile'     => $params['mobile'],
                'staff_num'  => $params['staff_num'],
                'password'   => bcrypt(UserEnum::ADMIN_USER_DETAULT_PASSWORD),
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //编辑用户
    public static function editAdminUser($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        $query = User::where('staff_num', $params['staff_num'])->first();

        if (!$query) {
            $result['status'] = true;
            $result['msg']    = AdminEnum::USER_NOT_EXIST;
            return $result;
        }

        $res = User::where('id', $query->id)->update(
            [
                'name'       => $params['name'],
                'mobile'     => $params['mobile'],
                'staff_num'  => $params['staff_num'],
                'updated_at' => date('Y-m-d H:i:s'),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //删除用户
    public static function delAdminUser($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        $query = User::where('staff_num', $params['staff_num'])->first();

        if (!$query) {
            $result['status'] = true;
            $result['msg']    = AdminEnum::USER_NOT_EXIST;
            return $result;
        }

        $res = User::where('id', $query->id)->delete();

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }

    //重置用户密码
    public static function resetPassword($params)
    {
        $result['status'] = true;
        $result['msg']    = AdminEnum::RETURN_SUCCESS;

        $query = User::where('staff_num', $params['staff_num'])->first();

        if (!$query) {
            $result['status'] = true;
            $result['msg']    = AdminEnum::USER_NOT_EXIST;
            return $result;
        }

        $res = User::where('id', $query->id)->update(
            [
                'password' => bcrypt(UserEnum::ADMIN_USER_DETAULT_PASSWORD),
            ]
        );

        if (!$res) {
            $result['status'] = false;
            $result['msg']    = AdminEnum::DATABASE_ERROR;
        }

        return $result;
    }
}