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

use app\admin\model\LifeTeacher;

class TeacherServer
{
    /**
     * Notes:获取教师列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function getTeacherList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeTeacher::getTeacherList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['teacher_create_time'] = date('Y-m-d H:i:s',$v['teacher_create_time']);
                if (!empty($v['teacher_update_time'])){
                    $list[$k]['teacher_update_time'] = date('Y-m-d H:i:s',$v['teacher_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取所有教师
     * @return array
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function getTeacherAll()
    {
        return LifeTeacher::getTeacherAll();
    }

    /**
     * Notes:修改教师
     * @param string $teacher_name
     * @param int $teacher_id
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function updateTeacher($teacher_id, $teacher_name, $teacher_img, $teacher_title, $teacher_positional, $admin_id)
    {
        LifeTeacher::affairBegin();
        try {
            $where = [];
            $where['teacher_id'] = $teacher_id;
            $data = [];
            $data['teacher_name'] = $teacher_name;
            $data['teacher_img'] = $teacher_img;
            $data['teacher_title'] = $teacher_title;
            $data['teacher_positional'] = $teacher_positional;
            $data['teacher_update_time'] = time();
            $result = LifeTeacher::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改教师-编号: ' . $teacher_id;
                plog('life-teacher-update', '悦活-教师-修改教师', $msg);
                LifeTeacher::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeTeacher::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除教师
     * @param int $teacher_id
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function delTeacher($teacher_id,$admin_id)
    {
        LifeTeacher::affairBegin();
        try {
            $where = [];
            $where['teacher_id'] = $teacher_id;
            $data['teacher_is_del'] = 1;
            $result = LifeTeacher::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除教师-编号: ' . $teacher_id;
                plog('life-teacher-delete', '悦活-教师-删除教师', $msg);
                LifeTeacher::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeTeacher::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加教师
     * @param string $teacher_name
     * @param array $teacher_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function insertTeacher($teacher_name, $teacher_img, $teacher_title, $teacher_positional, $admin_id)
    {

        LifeTeacher::affairBegin();
        try {
            $data = [];
            $data['teacher_name'] = $teacher_name;
            $data['teacher_img'] = $teacher_img;
            $data['teacher_title'] = $teacher_title;
            $data['teacher_create_time'] = time();
            $data['teacher_positional'] = $teacher_positional;
            $result =  LifeTeacher::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加教师-编号: ' . $result;
                plog('life-teacher-create', '悦活-教师-添加教师', $msg);
                LifeTeacher::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeTeacher::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询教师
     * @param int $teacher_id
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function teacherInfo($teacher_id)
    {
        $where = [];
        $where['teacher_id'] = $teacher_id;
        $result = LifeTeacher::where($where)->first();
        if (!empty($result)){
            $result['teacher_create_time'] = date('Y-m-d H:i:s',$result['teacher_create_time']);
            if (!empty($result['teacher_update_time'])){
                $result['teacher_update_time'] = date('Y-m-d H:i:s',$result['teacher_update_time']);
            }

        }
        return $result;
    }

    /**
     * Notes:修改教师状态
     * @param string $teacher_name
     * @param int $teacher_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($teacher_id, $teacher_status)
    {
        LifeTeacher::affairBegin();
        try {
            $where = [];
            $where['teacher_id'] = $teacher_id;
            $data = [];
            $data['teacher_status'] = $teacher_status;
            $result = LifeTeacher::where($where)->update($data);
            if ($result !== false){
                LifeTeacher::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeTeacher::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
