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

use app\admin\model\LifeBookCourse;

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

    /**
     * Notes:修改书院课程目录
     * @param string $course_name
     * @param int $course_id
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function updateBookCourse($course_id, $course_name, $course_time, $course_book_id, $course_price, $course_status, $admin_id)
    {
        LifeBookCourse::affairBegin();
        try {
            $where = [];
            $where['course_id'] = $course_id;
            $data = [];
            $data['course_name'] = $course_name;
            $data['course_time'] = strtotime($course_time);
            $data['course_book_id'] = $course_book_id;
            $data['course_price'] = $course_price;
            $data['course_status'] = $course_status;
            $data['course_update_time'] = time();
            $result = LifeBookCourse::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦书院课程目录-编号: ' . $course_id;
                plog('life-course-update', '悦活-万悦书院-修改书院课程目录', $msg);
                LifeBookCourse::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeBookCourse::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

    /**
     * Notes: 添加书院课程目录
     * @param string $course_name
     * @param array $course_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function insertBookCourse($course_name, $course_time, $course_book_id, $course_price, $course_status, $admin_id)
    {

        LifeBookCourse::affairBegin();
        try {
            $data = [];
            $data['course_name'] = $course_name;
            $data['course_time'] = strtotime($course_time);
            $data['course_book_id'] = $course_book_id;
            $data['course_price'] = $course_price;
            $data['course_status'] = $course_status;
            $data['course_create_time'] = time();
            $result =  LifeBookCourse::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦书院课程目录-编号: ' . $result;
                plog('life-course-create', '悦活-万悦书院-添加书院课程目录', $msg);
                LifeBookCourse::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeBookCourse::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询书院课程目录
     * @param int $course_id
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function courseInfo($course_id)
    {
        $where = [];
        $where['course_id'] = $course_id;
        $result = LifeBookCourse::where($where)
            ->leftJoin('life_book','life_book_id','=','life_book_course.course_book_id')
            ->first(['life_book_course.*','life_book.life_book_name']);
        if (!empty($result)){
            $result['course_time'] = date('Y-m-d H:i:s',$result['course_time']);
            $result['course_create_time'] = date('Y-m-d H:i:s',$result['course_create_time']);
            if (!empty($result['course_update_time'])){
                $result['course_update_time'] = date('Y-m-d H:i:s',$result['course_update_time']);
            }

        }
        return $result;
    }

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

}
