<?php
/**
 * 退款控制器
 * @file:RefundOrderController.php
 * @Date: 2023/5/26
 * @Time: 18:33
 */


namespace Modules\OrderRefund\Http\Controllers;

use Catch\Base\CatchController;
use Catch\Exceptions\FailedException;
use Illuminate\Http\Request;
use Modules\OrderRefund\Http\Requests\OrderRefundRequest;
use Modules\OrderRefund\Http\Requests\RefundVerIiyRequest;
use Modules\OrderRefund\Services\RefundOrderService;
use Modules\User\Http\Controllers\UserTrait;

class RefundOrderController extends CatchController
{

    use  UserTrait;

    /**
     *  退款列表
     * name: orderList
     * @param Request $request
     * date 2023/05/29 14:09
     */
    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;
        }

        return RefundOrderService::orderList($param);
    }

    /**
     *  申请退款
     * name: applyRefund
     * @param OrderRefundRequest $request
     * @return string
     * date 2023/05/29 14:09
     */
    public function applyRefund(OrderRefundRequest $request)
    {
        $param = $request->all();
        $param['create_user_id'] =  $this->getCurrentUser()->id;
        return RefundOrderService::applyRefund($param);
    }

    /**
     * 平台审核
     * name: platformVerify
     * @param RefundVerIiyRequest $request
     * @return string
     * date 2023/05/29 16:38
     */
    public function platformVerify(RefundVerIiyRequest $request){
        $userContext = $this->getUserContext(null);
        if (!($userContext['loginUserRoles']->contains('administrator')  || $userContext['loginUserRoles']->contains('financer'))) {
            throw  new  FailedException("没有操作权限");
        }
        $param = [
            'pt_verify_status' => $request->input('status'),
            'pt_verifty_user' => [
               "id"=> $userContext['loginUser']->id,
               "user_name"=> $userContext['loginUser']->username,
               "email"=> $userContext['loginUser']->email,
            ],
            'pt_verify_at' => get_date(),
            'pt_verify_remark' => $request->input('remark',"")
        ];
       return RefundOrderService::updateRefundVerify($request->input('refund_id'),$param,1);
    }

    /**
     * 商户审核
     * name: companyVerify
     * @param RefundVerIiyRequest $request
     * @return string
     * date 2023/05/29 16:39
     */
    public function  companyVerify(RefundVerIiyRequest $request){
        $userContext = $this->getUserContext(null);
        if (!($userContext['loginUserRoles']->contains('optimizer') || $userContext['loginUserRoles']->contains('company'))) {
            throw  new  FailedException("没有操作权限");
        }
        $param = [
            'sh_verify_status' => $request->input('status'),
            'sh_verifty_user' => [
                "id"=> $userContext['loginUser']->id,
                "user_name"=> $userContext['loginUser']->username,
                "email"=> $userContext['loginUser']->email,
            ],
            'sh_verify_at' => get_date(),
            'sh_verify_remark' => $request->input('remark',"")
        ];
        return RefundOrderService::updateRefundVerify($request->input('refund_id'),$param,0);
    }

    /**
     *  操作退款
     * name: payToUser
     * @param Request $request
     * @return string|void
     * date 2023/05/29 17:32
     */
    public function payToUser(Request $request){
        $refundId = $request->input('refund_id',0);
        if($refundId <  1){
            throw  new FailedException("退款申请单号不能为空");
        }
        $userContext = $this->getUserContext(null);
        if (!($userContext['loginUserRoles']->contains('administrator')  || $userContext['loginUserRoles']->contains('financer'))) {
            throw  new  FailedException("没有操作权限");
        }
        $option = [
            "id"=> $userContext['loginUser']->id,
            "user_name"=> $userContext['loginUser']->username,
            "email"=> $userContext['loginUser']->email,
        ];
        return RefundOrderService::payToUser($refundId,$option);
    }
}
