<?php
/**
 * 微信小程序用户
 * @file:WechatMinprogramUserController.php
 * @Date: 2023/5/19
 * @Time: 10:24
 */


namespace Modules\Channel\Http\Controllers;

use Catch\Base\CatchController;
use Catch\Exceptions\FailedException;
use Illuminate\Http\Request;
use Modules\Channel\Services\WechatMinprogram\WechatMinprogramUserService;
use Modules\User\Http\Controllers\UserTrait;

class WechatMinprogramUserController extends CatchController
{
    use UserTrait;
    /***
     *  获取用户信息
     * name: userInfoDetail
     * @param $uid
     * @return array|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object
     * date 2023/05/19 14:13
     */
    public function userInfoDetail($uid)
    {

        if ($uid < 1) {
           $this->$this->errorMsg('缺少微信小程序用户id参数');
        }
        $userInfo = WechatMinprogramUserService::userInfoDetail($uid);
        if (!$userInfo) {
          $this->errorMsg('用户不存在');
        }
        return $userInfo;
    }

    /**
     *  订单记录
     * name: orderList
     * @param Request $request
     * date 2023/05/19 14:14
     */
    public function orderList(Request $request)
    {
        $param = $request->all();
        $uid = $request->input('uid');
        if ($uid <  1){
            $this->errorMsg("用户参数必填");
        }
        $userContext = $this->getUserContext(null);
        // 公司账户处理
        if ($userContext['loginUserRoles']->contains('company')) {
            $param['puser_id'] = $userContext['operateUser']->id;
        }

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

        return WechatMinprogramUserService::getUserOrderList($uid,$param);

    }

    /***
     *  观看记录
     * name: watchRecord
     * @param Request $request
     * date 2023/05/19 14:18
     */
    public  function watchRecord  (Request $request)
    {
        $uid = $request->input('uid');
        if ($uid <  1){
            $this->errorMsg("用户参数必填");
        }

        $list =  WechatMinprogramUserService::getUserWatchRecord($uid);
        return $this->pageWithArray($request,$list,$request->input('limit',15));
    }

    /**
     *  消费记录
     * name: consumeRecord
     * @param Request $request
     * date 2023/05/19 14:19
     */
    public  function consumeRecord (Request $request)
    {
        $uid = $request->input('uid');
        if ($uid <  1){
            $this->errorMsg("用户参数必填");
        }
        return  WechatMinprogramUserService::getUserConsumeRecord($uid,$request->input('limit',15));
    }

    protected function errorMsg($msg,$code = 10005){
        throw new FailedException($msg,$code );
    }

    /**
     *  数组分页
     * name: pageWithArray
     * @param $request
     * @param $array
     * @param int $limit
     * @return \Illuminate\Pagination\LengthAwarePaginator
     * date 2023/05/19 16:44
     */
   protected function pageWithArray($request,$array,$limit = 15){
        $currentPage = \Illuminate\Pagination\LengthAwarePaginator::resolveCurrentPage();
        $itemCollection = collect($array);
        $currentPageItems = $itemCollection->slice(($currentPage*$limit)-$limit,$limit)->all();
        return new \Illuminate\Pagination\LengthAwarePaginator($currentPageItems,count($itemCollection),$limit);
    }

}
