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

use app\admin\model\LifeFoodOrder;
use app\admin\model\LifeFoodOrderDetail;

class FoodOrderServer
{
    /**
     * Notes:获取订单列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/13
     */
    public static function getOrderList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeFoodOrder::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']);
                if (!empty($v['order_update_time'])){
                    $list[$k]['order_update_time'] = date('Y-m-d H:i:s',$v['order_update_time']);
                }
                if (!empty($v['detail'])){
                    foreach ($v['detail'] as $key => $value){
                        $list[$k]['detail'][$key]['detail_create_time'] = date('Y-m-d H:i:s',$value['detail_create_time']);
                        if (!empty($value['detail_update_time'])){
                            $list[$k]['detail'][$key]['detail_update_time'] = date('Y-m-d H:i:s',$value['detail_update_time']);
                        }
                    }
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取订单详情
     * @param int $order_id
     * @return int
     * User: ZQ
     * Date: 2022/10/13
     */
    public static function orderInfo($order_id)
    {
        $where = [];
        $where['order_id'] = $order_id;
        $result = LifeFoodOrder::with(['user','detail'])->where($where)
            ->leftJoin('merchant_shop','shop_id','=','life_healthy_order.order_shop_id')
            ->first(['life_healthy_order.*','merchant_shop.shop_name']);
        if (!empty($result)){
            if (!empty($result)){
                $result['order_create_time'] = date('Y-m-d H:i:s',$result['order_create_time']);
                if (!empty($v['order_update_time'])){
                    $result['order_update_time'] = date('Y-m-d H:i:s',$result['order_update_time']);
                }
                if (!empty($result['detail'])){
                    foreach ($result['detail'] as $key => $value){
                        $result['detail'][$key]['detail_create_time'] = date('Y-m-d H:i:s',$value['detail_create_time']);
                        if (!empty($value['detail_update_time'])){
                            $result['detail'][$key]['detail_update_time'] = date('Y-m-d H:i:s',$value['detail_update_time']);
                        }
                    }
                }

            }
            return $result;
        }else{
            return false;
        }
    }

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