<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/30
 * Time: 17:38
 */

namespace App\Http\Controllers\Manage\User;

use App\Http\Controllers\Controller;
use App\Http\Controllers\Manage\User\Transformers\UserTransformer;
use App\Modules\User\Services\UserService;
use Illuminate\Http\Request;

/**
 * Class UserDataController 读者数据
 * @package App\Http\Controllers\Manage\User
 */
class UserDataController extends Controller
{
    /**
     * @apiDefine user 用户
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 根据id 获取用户信息
     * @api {GET} user/getUserDataById 根据id 获取用户信息
     * @apiGroup user
     * @apiName getUserDataById
     * @apiParam{String}id  用户id
     * @apiSuccess {Number}  id 用户ID.
     * @apiSuccess {String}  openid 微信openid.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  unionid 微信unionid.
     * @apiSuccess {Number}  distribution_channel_id 分销渠道ID.
     * @apiSuccess {String}  province 省份.
     * @apiSuccess {String}  city 城市.
     * @apiSuccess {String}  country 国家.
     * @apiSuccess {String}  headimgurl 头像地址.
     * @apiSuccess {String}  register_time 创建时间.
     * @apiSuccess {String}  update_time 更新时间.
     * @apiSuccess {Number}  send_order_id 派单ID.
     * @apiSuccess {Number=0,1}  sex 性别.
     * @apiSuccess {String}  balance 书币余额.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *             "id": 56,
     *             "nickname": "昵称",
     *             "openid": "sdfs34ssdfdsf",
     *             "unionid": "SDFSD3343S",
     *             "distribution_channel_id": 1212,
     *             "province": "浙江省",
     *             "city": "杭州",
     *             "country": "中国",
     *             "headimgurl": "http://.."
     *             "send_order_id": 323
     *             "balance": 8956
     *             "register_time": "2017-12-12 12:12:12"
     *             "update_time": "2017-12-12 12:12:12"
     *         }
     *     }
     */
    function getUserDataById(Request $request)
    {
        $id = $request->has('id') ? $request->input('id') : '';
        try {
            $data = UserService::getUserDataDetailById($id);
            return response()->collection(new UserTransformer(), $data);
        } catch (Exception $e) {
            return response()->error('PARAM_ERROR');
        }
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取读者数据列表
     * @api {GET} user/getUsers 获取读者数据列表
     * @apiGroup user
     * @apiName getUsers
     * @apiParam{Number}pageSize  每页显示的条数(默认每页 15 条)
     * @apiParam{Number}careStatus 是否关注(0:所有，1：关注，2：未关注)
     * @apiSuccess {Number}  id 用户ID.
     * @apiSuccess {String}  openid 微信openid.
     * @apiSuccess {String}  nickname 昵称.
     * @apiSuccess {String}  unionid 微信unionid.
     * @apiSuccess {Number}  distribution_channel_id 分销渠道ID.
     * @apiSuccess {String}  province 省份.
     * @apiSuccess {String}  city 城市.
     * @apiSuccess {String}  country 国家.
     * @apiSuccess {String}  headimgurl 头像地址.
     * @apiSuccess {String}  register_time 创建时间.
     * @apiSuccess {String}  update_time 更新时间.
     * @apiSuccess {Number}  send_order_id 派单ID.
     * @apiSuccess {Number=0,1}  sex 性别.
     * @apiSuccess {String}  balance 书币余额.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *         "list": [
     *            {
     *             "id": 56,
     *             "nickname": "昵称",
     *             "openid": "sdfs34ssdfdsf",
     *             "unionid": "SDFSD3343S",
     *             "distribution_channel_id": 1212,
     *             "province": "浙江省",
     *             "city": "杭州",
     *             "country": "中国",
     *             "headimgurl": "http://.."
     *             "send_order_id": 323
     *             "balance": 8956
     *             "register_time": "2017-12-12 12:12:12"
     *             "update_time": "2017-12-12 12:12:12"
     *         },
     *         {
     *             "id": 56,
     *             "nickname": "昵称",
     *             "openid": "sdfs34ssdfdsf",
     *             "unionid": "SDFSD3343S",
     *             "distribution_channel_id": 1212,
     *             "province": "浙江省",
     *             "city": "杭州",
     *             "country": "中国",
     *             "headimgurl": "http://.."
     *             "send_order_id": 323
     *             "balance": 8956
     *             "register_time": "2017-12-12 12:12:12"
     *             "update_time": "2017-12-12 12:12:12"
     *         }
     *        ],
     *       "meta": {
     *         "total": 12,
     *         "per_page": 2,
     *         "current_page": 1,
     *         "last_page": 6,
     *          "next_page_url": "http://47.96.174.172:8091/api/Product/getUsers?pageCount=2",
     *          "prev_page_url": ""
     *        }
     *     }
     *  }
     */
    function getUsers(Request $request)
    {
        $pageSize = $request->has('pageSize') ? $request->input('pageSize') : '';
        $careStatus = $request->has('careStatus') ? $request->input('careStatus') : '';
        if (empty($pageSize)) {
            $pageSize = 15;
        }
        $data = UserService::getUsers($pageSize,$careStatus);
        return response()->pagination(new UserTransformer(), $data);
    }
}