<?php
namespace app\admin\server\housekeeper;

use app\admin\model\Housekeeper;

class HousekeeperServer
{
    /**
     * Notes:管家列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: yym
     * Date: 2022/10/28
     */
    public static function getHousekeeperList(string $keywords, int $page, int $limit)
    {
        [$list, $count] = Housekeeper::getHousekeeperList($keywords, $page, $limit);
        if(!empty($list))
        {
            foreach ($list as &$item)
            {
                $item['housekeeper_create_time'] = date("Y-m-d H:i:s", $item['housekeeper_create_time']);
                $item['housekeeper_update_time'] = date("Y-m-d H:i:s", $item['housekeeper_update_time']);
            }
            unset($item);
        }
        return compact('list', 'count');
    }

    /**
     * Notes:添加管家
     * @param $admin_id
     * @param $housekeeper_name
     * @param $housekeeper_pwd
     * @param $housekeeper_real_name
     * @param $housekeeper_head
     * @param $housekeeper_phone
     * @param $housekeeper_position
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/10/28
     */
    public static function addHousekeeper($admin_id, $housekeeper_name, $housekeeper_pwd, $housekeeper_real_name, $housekeeper_head, $housekeeper_phone, $housekeeper_position)
    {
        Housekeeper::affairBegin();
        try {
            //检测管家账号是否存在
            if(Housekeeper::checkAccount($housekeeper_name))
            {
                throw new \Exception('该账号已存在！');
            }
            $salf = getRandomString(6);
            $insert = array(
                'housekeeper_name' => $housekeeper_name,
                'housekeeper_pwd'  => md5(sha1($housekeeper_pwd) . $salf),
                'housekeeper_salf' => $salf,
                'housekeeper_real_name' => $housekeeper_real_name,
                'housekeeper_head' => $housekeeper_head,
                'housekeeper_phone' => $housekeeper_phone,
                'housekeeper_position' => $housekeeper_position,
                'housekeeper_create_time' => time()
            );
            $message = '管理员：' . $admin_id . '在' . date("Y-m-d H:i:s", time()) . '添加管家：' . $housekeeper_name;
            $result = Housekeeper::insertData($insert);
            if($result)
            {
                plog('housekeeper-add', '管家-添加管家', $message);
                Housekeeper::affairCommit();
                return true;
            }
            throw new \Exception('添加失败！');
        }catch (\Exception $exception){
            Housekeeper::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:管家编辑
     * @param $admin_id
     * @param $housekeeper_id
     * @param $housekeeper_name
     * @param $housekeeper_pwd
     * @param $housekeeper_real_name
     * @param $housekeeper_head
     * @param $housekeeper_phone
     * @param $housekeeper_position
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/10/28
     */
    public static function updateHousekeeper($admin_id, $housekeeper_id, $housekeeper_name, $housekeeper_pwd, $housekeeper_real_name, $housekeeper_head, $housekeeper_phone, $housekeeper_position)
    {
        Housekeeper::affairBegin();
        try {
            //检测管家账号是否存在
            if(Housekeeper::checkAccountNo($housekeeper_name, $housekeeper_id))
            {
                throw new \Exception('该账号已存在！');
            }

            $update = array(
                'housekeeper_name' => $housekeeper_name,
                'housekeeper_real_name' => $housekeeper_real_name,
                'housekeeper_head' => $housekeeper_head,
                'housekeeper_phone' => $housekeeper_phone,
                'housekeeper_position' => $housekeeper_position
            );
            if(!empty($housekeeper_pwd))
            {
                $salf = getRandomString(6);
                $update['housekeeper_pwd'] = md5(sha1($housekeeper_pwd) . $salf);
                $update['housekeeper_salf'] = $salf;
            }
            $message = '管理员：' . $admin_id . '在' . date("Y-m-d H:i:s", time()) . '编辑管家：' . $housekeeper_id;
            $result = Housekeeper::updateData($update, $housekeeper_id);
            if($result)
            {
                plog('housekeeper-update', '管家-编辑管家', $message);
                Housekeeper::affairCommit();
                return true;
            }
            throw new \Exception('编辑失败！');
        }catch (\Exception $exception){
            Housekeeper::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:获取管家详情
     * @param int $housekeeper_id
     * @return array
     * @throws \Exception
     * User: yym
     * Date: 2022/10/28
     */
    public static function getInfo(int $housekeeper_id)
    {
        try {
            $info = Housekeeper::getInfo($housekeeper_id);
            if(empty($info))
            {
                throw new \Exception('管家账号不存在或已删除！');
            }
            $info = $info->toArray();
            $info['housekeeper_pwd'] = '';
            return $info;
        }catch (\Exception $exception){
            throw new \Exception($exception->getMessage(), '500');
        }
    }

    /**
     * Notes:删除管家
     * @param int $admin_id
     * @param int $housekeeper_id
     * @return bool
     * @throws \Exception
     * User: yym
     * Date: 2022/10/28
     */
    public static function delHousekeeper(int $admin_id, int $housekeeper_id)
    {
        Housekeeper::affairBegin();
        try {
            $info = Housekeeper::getInfo($housekeeper_id);
            if(empty($info))
            {
                throw new \Exception('管家账号不存在或已删除！');
            }
            $update = array(
                'housekeeper_del' => Housekeeper::DEL_YES
            );
            $message = '管理员：' . $admin_id . '在' . date("Y-m-d H:i:s", time()) . '删除管家：' . $housekeeper_id;
            $result = Housekeeper::updateData($update, $housekeeper_id);
            if($result)
            {
                plog('housekeeper-delete', '管家-删除管家', $message);
                Housekeeper::affairCommit();
                return true;
            }
            throw new \Exception('删除失败！');
        }catch (\Exception $exception){
            Housekeeper::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
