<?php

namespace app\admin\logic;

use app\admin\controller\Common;
use app\model\AdminModel;
use app\model\CardModel;
use app\model\CommonModel;
use think\exception\ValidateException;
use think\facade\Config;
use think\response\Json;

class AdminLogic extends BaseLogic
{
    //获取运营账号列表
    public static function list(array $data = []): Json
    {
        $db = AdminModel::alias('a')
            ->leftJoin('role b', 'b.id=a.role_id')
            ->where('a.is_del', CommonModel::$del_normal);

        if ($data['username'] != '') $db->whereLike('a.username', '%' . $data['username'] . '%');
        if ($data['status'] != '') $db->where('a.status', $data['status']);

        $count = $db->count('a.id');

        $list = $db->field('a.id,a.username,a.status,a.addtime,b.name as role_name,a.card_id')
            ->append(['card_name'])
            ->withAttr('card_name', function ($val, $da) {
                return CardModel::field('id,title,status')
                    ->whereIn('id', $da['card_id'])
                    ->where('is_del', CommonModel::$del_normal)
                    ->order(['id' => 'desc'])
                    ->select()
                    ->toArray();
            })
            ->order(['a.id' => 'desc'])
            ->page($data['page'], $data['size'])
            ->select()
            ->toArray();

        return json_show(CommonModel::$success, '获取运营账号列表成功', ['count' => $count, 'list' => $list]);
    }

    //添加运营账号
    public static function add(array $data = []): Json
    {
        $salt = randomkeys(6);
        $data = array_merge($data, [
            'salt' => $salt,
            'password' => get_password(Config::get('common.default_password'), $salt),
            'is_del' => CommonModel::$del_normal,
            'status' => CommonModel::$status_normal,
            'addtime' => date('Y-m-d H:i:s'),
            'updatetime' => date('Y-m-d H:i:s'),
            'card_id' => implode(',', $data['card_id'])
        ]);

        $rs = AdminModel::create($data)->save();

        return $rs ? json_show(CommonModel::$success, '添加运营账号成功') : json_show(CommonModel::$error_param, '添加运营账号失败');
    }

    //获取运营账号详情
    public static function read(int $id = 0): Json
    {
        $rs = AdminModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
            ->withoutField('password,salt')
            ->withAttr('card_id', function ($val) {
                return explode(',', $val);
            })
            ->findOrEmpty()
            ->toArray();

        return json_show(CommonModel::$success, '获取运营账号详情成功', $rs);
    }

    //编辑运营账号
    public static function edit(array $data = []): Json
    {
        $data = array_merge($data, [
            'card_id' => implode(',', $data['card_id']),
            'updatetime' => date('Y-m-d H:i:s'),
        ]);

        $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])->save($data);

        return $rs ? json_show(CommonModel::$success, '编辑运营账号成功') : json_show(CommonModel::$error_param, '编辑运营账号失败');
    }

    //删除运营账号
    public static function delete(int $id = 0): Json
    {
        if ($id == self::$uid) throw new ValidateException('不能删除当前账号');

        $rs = AdminModel::where(['id' => $id, 'is_del' => CommonModel::$del_normal])
            ->save([
                'is_del' => CommonModel::$del_deleted,
                'updatetime' => date('Y-m-d H:i:s'),
            ]);

        return $rs ? json_show(CommonModel::$success, '删除运营账号成功') : json_show(CommonModel::$error_param, '删除运营账号失败,该账号不存在或重复删除');

    }

    //启禁用运营账号
    public static function status(array $data = []): Json
    {
        if ($data['id'] == self::$uid) throw new ValidateException('不能对当前账号进行操作');

        $data = array_merge($data, ['updatetime' => date('Y-m-d H:i:s')]);

        $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
            ->where('status', '<>', $data['status'])
            ->save($data);

        return $rs ? json_show(CommonModel::$success, '操作成功') : json_show(CommonModel::$error_param, '操作失败');
    }

    //更改密码
    public static function changePasswod(array $data = []): Json
    {
        $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
            ->field('id,password,salt,status')
            ->findOrEmpty();

        if ($rs->isEmpty()) throw new ValidateException('该运营账号不存在');

        $salt = randomkeys(6);
        $password = get_password($data['new_password'], $salt);

        $da = [
            'salt' => $salt,
            'password' => $password,
            'updatetime' => date('Y-m-d H:i:s'),
        ];

        $rs = AdminModel::where(['id' => $data['id'], 'is_del' => CommonModel::$del_normal])
            ->save($da);

        return $rs ? json_show(CommonModel::$success, '更改密码成功') : json_show(CommonModel::$error_param, '更改密码失败');
    }


}