<?php
/**
 *
 * @file:OrdersController.php
 * @Date: 2023/5/23
 * @Time: 10:15
 */


namespace Modules\Channel\Http\Controllers;

use Catch\Base\CatchController;
use Illuminate\Http\Request;
use Modules\Channel\Models\UserHasMiniprograms;
use Modules\Channel\Services\Order\OrderService;
use Modules\User\Http\Controllers\UserTrait;

class OrdersController extends CatchController
{
    use UserTrait;

    /**
     *  订单列表
     * name: orderList
     * @param Request $request
     * date 2023/05/23 10:22
     */
    public function orderList(Request $request)
    {
        $param = $request->all();
        $userContext = $this->getUserContext(null);
        if (getProp($param, 'puser_id')) {
            $param['puser_id'] = $param['puser_id'];
        }
        if (getProp($param, 'user_id')) {
            $param['user_id'] = $param['user_id'];
        }
        // 公司账户处理
        if ($userContext['loginUserRoles']->contains('company')) {
            $param['puser_id'] = $userContext['operateUser']->id;
        }

        // 投手处理
        if ($userContext['loginUserRoles']->contains('optimizer')) {
            $param['user_id'] = $userContext['operateUser']->id;
        }

        $list = OrderService::getOrderList($param);
        if (!$list->isEmpty()) {
            foreach ($list as $val) {
                if ($userContext['loginUserRoles']->contains('company')) {
                    $val->total_count = $val->caompany_count;
                }
                if (getProp($param, 'user_id')) {
                    $val->total_count = $val->promotion_count;
                }
                unset($val->caompany_count, $val->promotion_count, $val->pay_product_id, $val->create_ip, $val->transaction_id);
                unset($val->third_orderid, $val->pay_merchant_sourcem, $val->video_id, $val->video_series_sequence, $val->updated_at);
            }
        }
        return $list;
    }

    /**
     *  订单小程选择列表
     * name: userUseList
     * @param Request $request
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection|UserHasMiniprograms[]
     * date 2023/05/23 17:35
     */
    public function userUseList(Request $request)
    {
        $uid = $request->input("user_id", 0);
        if (!$uid) {
            $uid = $this->getCurrentUser()->id;
        }
        $name = $request->input("name", "");
        $limit =  $request->input("limit", 20);
        return OrderService::userUseProgramsList($uid,$name,$limit);
    }

    /**
     *  订单优化师列选择项
     * name: promotionsUsers
     * @param Request $request
     * date 2023/05/26 09:12
     */
    public function promotionsUsers(Request $request){

        $name = $request->input('name');
        $pid = $request->input('user_id');
        $limit = $request->input('limit',20);
        $miniprogram_id = $request->input('miniprogram_id',0);
        $userContext = $this->getUserContext(null);
        // 公司账户处理
        if ($userContext['loginUserRoles']->contains('company')) {
            $pid = $userContext['operateUser']->id;
        }

        // 投手处理
        if ($userContext['loginUserRoles']->contains('optimizer')) {
            return [['user_id' => $userContext['operateUser']->id,'name' => $userContext['operateUser']->username]];
        }
        return  OrderService::promotionsUsers($pid,$name,$miniprogram_id,$limit);
    }

    /**
     *  订单列表推广选择项
     * name: promotionsOptions
     * @param Request $request
     * @return \Illuminate\Support\Collection
     * date 2023/05/26 15:42
     */
    public function promotionsOptions(Request $request)
    {
        $title = $request->input('name');
        $uid = $request->input("user_id", 0);
        $miniprogram_id = $request->input('miniprogram_id',0);
        if (!$uid) {
            $uid = $this->getCurrentUser()->id;
        }
        $limit =  $request->input("limit", 20);
        return OrderService::promotionsOptions($uid, $title,$miniprogram_id,$limit);
    }

    /**
     *  订单充值类型选择项
     * name: orderTypes
     * @return \string[][]
     * date 2023/05/26 15:43
     */
    public function orderTypes()
    {
        return OrderService::getOrderType();
    }

    /**
     *  订单支付状态现在项
     * name: orderStatus
     * @return \string[][]
     * date 2023/05/26 15:43
     */
    public function orderStatus()
    {
        return OrderService::getOrderPayType();
    }
}

