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

use app\admin\model\LifeBookScheduling;
use app\admin\model\LifeBook;

class BookSchedulingServer
{
    /**
     * Notes:获取书院课程排期列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function getBookSchedulingList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeBookScheduling::getBookSchedulingList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['scheduling_start_time'] = date('Y-m-d H:i:s',$v['scheduling_start_time']);
                $list[$k]['scheduling_end_time'] = date('Y-m-d H:i:s',$v['scheduling_end_time']);
                $list[$k]['scheduling_create_time'] = date('Y-m-d H:i:s',$v['scheduling_create_time']);
                if (!empty($v['scheduling_update_time'])){
                    $list[$k]['scheduling_update_time'] = date('Y-m-d H:i:s',$v['scheduling_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改书院课程排期
     * @param string $scheduling_name
     * @param int $scheduling_id
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function updateBookScheduling($scheduling_id, $scheduling_book_id, $scheduling_start_time, $scheduling_end_time, $admin_id)
    {
        LifeBookScheduling::affairBegin();
        try {
            $where = [];
            $where['scheduling_id'] = $scheduling_id;
            $data = [];
            $data['scheduling_start_time'] = $scheduling_start_time;
            $data['scheduling_end_time'] = $scheduling_end_time;
            $data['scheduling_book_id'] = $scheduling_book_id;
            $data['scheduling_category_id'] = LifeBook::where(['life_book_id'=>$scheduling_book_id])->value('life_book_category_id');
            $data['scheduling_update_time'] = time();
            $result = LifeBookScheduling::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦书院课程排期-编号: ' . $scheduling_id;
                plog('life-scheduling-update', '悦活-万悦书院-修改书院课程排期', $msg);
                LifeBookScheduling::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeBookScheduling::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

    /**
     * Notes: 添加书院课程排期
     * @param string $scheduling_name
     * @param array $scheduling_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function insertBookScheduling($scheduling_book_id, $scheduling_start_time, $scheduling_end_time, $admin_id)
    {

        LifeBookScheduling::affairBegin();
        try {
            $data = [];
            $data['scheduling_start_time'] = $scheduling_start_time;
            $data['scheduling_end_time'] = $scheduling_end_time;
            $data['scheduling_book_id'] = $scheduling_book_id;
            $data['scheduling_category_id'] = LifeBook::where(['life_book_id'=>$scheduling_book_id])->value('life_book_category_id');
            $data['scheduling_create_time'] = time();
            $result =  LifeBookScheduling::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦书院课程排期-编号: ' . $result;
                plog('life-scheduling-create', '悦活-万悦书院-添加书院课程排期', $msg);
                LifeBookScheduling::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeBookScheduling::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询书院课程排期
     * @param int $scheduling_id
     * @return int
     * User: ZQ
     * Date: 2022/10/25
     */
    public static function schedulingInfo($scheduling_id)
    {
        $where = [];
        $where['scheduling_id'] = $scheduling_id;
        $result = LifeBookScheduling::where($where)
            ->leftJoin('life_book','life_book_id','=','life_book_scheduling.scheduling_book_id')
            ->leftJoin('category','category_id','=','life_book_scheduling.scheduling_category_id')
            ->first(['life_book_scheduling.*','life_book.life_book_name','category.category_name']);
        if (!empty($result)){
            $result['scheduling_start_time'] = date('Y-m-d H:i:s',$result['scheduling_start_time']);
            $result['scheduling_end_time'] = date('Y-m-d H:i:s',$result['scheduling_end_time']);
            $result['scheduling_create_time'] = date('Y-m-d H:i:s',$result['scheduling_create_time']);
            if (!empty($result['scheduling_update_time'])){
                $result['scheduling_update_time'] = date('Y-m-d H:i:s',$result['scheduling_update_time']);
            }

        }
        return $result;
    }

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

}
