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

use app\admin\model\LifeBook;

class BookServer
{
    /**
     * Notes:获取书院课程列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function getBookList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeBook::getBookList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['life_book_create_time'] = date('Y-m-d H:i:s',$v['life_book_create_time']);
                if (!empty($v['life_book_update_time'])){
                    $list[$k]['life_book_update_time'] = date('Y-m-d H:i:s',$v['life_book_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改书院课程
     * @param string $life_book_name
     * @param int $life_book_id
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function updateBook($life_book_id, $life_book_name, $life_book_title, $life_book_time, $life_book_category_id, $life_book_teacher_id, $life_book_num, $life_book_status, $lift_book_img, $life_book_type, $life_book_long, $life_book_week, $admin_id)
    {
        LifeBook::affairBegin();
        try {
            $where = [];
            $where['life_book_id'] = $life_book_id;
            $data = [];
            $data['life_book_name'] = $life_book_name;
            $data['life_book_title'] = $life_book_title;
            $data['life_book_time'] = is_array($life_book_time) ? implode('~', $life_book_time) : $life_book_time;
            $data['life_book_category_id'] = $life_book_category_id;
            $data['life_book_teacher_id'] = $life_book_teacher_id;
            $data['life_book_num'] = $life_book_num;
            $data['life_book_long'] = $life_book_long;
            $data['life_book_type'] = $life_book_type;
            $data['life_book_img'] = $lift_book_img;
            $data['life_book_status'] = $life_book_status;
            $data['life_book_week'] = is_array($life_book_week) ? implode(',', $life_book_week) : $life_book_week;
            $data['life_book_update_time'] = time();
            $result = LifeBook::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦书院课程-编号: ' . $life_book_id;
                plog('life-life_book-update', '悦活-万悦书院-修改书院课程', $msg);
                LifeBook::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeBook::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

    /**
     * Notes: 添加书院课程
     * @param string $life_book_name
     * @param array $life_book_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function insertBook($life_book_name, $life_book_title, $life_book_time, $life_book_category_id, $life_book_teacher_id, $life_book_num, $life_book_status, $lift_book_img, $life_book_type, $life_book_long, $life_book_week, $admin_id)
    {

        LifeBook::affairBegin();
        try {
            $data = [];
            $data['life_book_name'] = $life_book_name;
            $data['life_book_title'] = $life_book_title;
            $data['life_book_time'] = is_array($life_book_time) ? implode('~', $life_book_time) : $life_book_time;
            $data['life_book_category_id'] = $life_book_category_id;
            $data['life_book_teacher_id'] = $life_book_teacher_id;
            $data['life_book_num'] = $life_book_num;
            $data['life_book_long'] = $life_book_long;
            $data['life_book_type'] = $life_book_type;
            $data['life_book_img'] = $lift_book_img;
            $data['life_book_week'] = is_array($life_book_week) ? implode(',', $life_book_week) : $life_book_week;
            $data['life_book_status'] = $life_book_status;
            $data['life_book_create_time'] = time();
            $result =  LifeBook::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦书院课程-编号: ' . $result;
                plog('life-life_book-create', '悦活-万悦书院-添加书院课程', $msg);
                LifeBook::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeBook::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询书院课程
     * @param int $life_book_id
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function bookInfo($life_book_id)
    {
        $where = [];
        $where['life_book_id'] = $life_book_id;
        $result = LifeBook::where($where)
            ->leftJoin('category','category_id','=','life_book.life_book_category_id')
            ->leftJoin('teacher','teacher_id','=','life_book.life_book_teacher_id')
            ->first(['life_book.*','category.category_name','teacher.teacher_name']);
        if (!empty($result)){
            $result['life_book_create_time'] = date('Y-m-d H:i:s',$result['life_book_create_time']);
            if (!empty($result['life_book_update_time'])){
                $result['life_book_update_time'] = date('Y-m-d H:i:s',$result['life_book_update_time']);
            }

        }
        return $result;
    }

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

}
