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

use app\admin\model\BookCourse;
use app\admin\model\LifeBook;
use app\admin\model\LifeBookCourse;

class BookCourseOrderServer
{
    /**
     * Notes:获取商品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getOrderList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  BookCourse::getOrderList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['order_create_time'] = date('Y-m-d H:i:s',$v['order_create_time']);
                $list[$k]['order_update_time'] = $v['order_update_time'] ? date('Y-m-d H:i:s',$v['order_update_time']) : '';
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }
    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: QJF
     * Date: 2022/10/14
     */
    public static function getOrderInfo($order_id)
    {
        BookCourse::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $result = BookCourse::with(['user','Detail'])->where($where)->first();
            if (!empty($result)){
                foreach ($result['detail'] as $k => $v){
                    $result['detail'][$k]['book_name'] = LifeBook::where(['life_book_id'=>$v['log_book_id']])->value('life_book_name');
                    $result['detail'][$k]['course_name'] = LifeBookCourse::where(['course_id'=>$v['log_course_id']])->value('course_name');
                }
                BookCourse::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            BookCourse::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    //删除订单
    public static function delOrder($order_id)
    {
        BookCourse::affairBegin();
        try {
            $where = [];
            $where['order_id']         = $order_id;
            $data['order_is_del']      = 1;
            $data['order_update_time'] = time();
            $result = BookCourse::where($where)->update($data);
            if (!empty($result)){
                BookCourse::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            BookCourse::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改订单支付状态
     * @param string $order_id
     * @param int $order_finish
     * @return int
     * User: YCP
     * Date: 2022/11/28
     */
    public static function updatePayStatus($order_id, $order_status)
    {
        BookCourse::affairBegin();
        try {
            $where = [];
            $where['order_id'] = $order_id;
            $data = [];
            $data['order_status'] = $order_status;
            if($order_status == 2){
                $data['order_pay_time'] = time();
            }else{
                $data['order_pay_time'] = "";
            }
            $result = BookCourse::where($where)->update($data);
            if ($result !== false){
                BookCourse::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            BookCourse::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
