<?php
/**
 * Function Description:${CARET}
 * Function Name: FxUserService
 * ${PARAM_DOC}
 *
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 *
 * @author 娄梦宁
 */

namespace admin\service\user;

use admin\util\AdminUtil;
use common\models\CmsCategory;
use common\models\FxUser;
use common\models\FxUserAccount;
use common\models\FxUserAmountOrder;
use common\models\FxUserArea;
use common\models\OrderMain;
use common\models\ProdCategory;
use common\models\ProdMain;
use common\util\Util;
use common\util\WeChatService;


class FxUserService
{
    /**
     * Function Description:获取分销用户列表
     * Function Name: getUserList
     * @param $param
     *
     * @return array
     *
     * @author 娄梦宁
     */
    public function getUserList($param)
    {
        $fx_user = new FxUser();
        $user_info = $fx_user->getUserList($param);
        $user_list = $user_info['list'];
        $page_size = $param['page_size'];
        $total_count = $user_info['count'];
        $total_pages = ceil($total_count / $page_size);
        $result = [
            'user_list' => $user_list,
            'is_bind' => 0,
            'page' => [
                'current_page' => (string)$param['current_page'],
                'total_pages' => (string)$total_pages,
                'page_size' => (string)$page_size,
                'total_count' => (string)$total_count,
            ]
        ];
        $uid = AdminUtil::$uid;
        $siteConfig = Util::getSiteConfig();
        $bindFxUser = $siteConfig['bind_fx_user'];
        if (in_array($uid, $bindFxUser)) {
            $result['is_bind'] = 1;
        }
        return Util::returnArrSu('', $result);
    }

    /*
     * 获取分销二维码
     */
    public function getQrCode($fx_uid)
    {
        $qr_code = WeChatService::getFxQrCode($fx_uid);
        if ($qr_code['flag'] == true) {
            $result = ['qr_code' => $qr_code['data']['url']];
            $new_qr_code = WeChatService::getFxNewQrCode($fx_uid);
            if ($new_qr_code['flag'] == true) {
                $result['new_qr_code'] = $new_qr_code['data']['url'];
            }
            return Util::returnArrSu('', $result);
        }
        return Util::returnArrEr('');
    }

    /**
     * Function Description:分销商状态变更
     * Function Name: ChangeFxUser
     * @param $param
     *
     * @return array
     *
     * @author 娄梦宁
     */
    public function ChangeFxUser($param)
    {
        if (Util::checkPattern('intVal', $param['fx_uid'] == false || Util::checkPattern('', $param['fx_status_id'], '/1|2/') == false)) {
            return Util::returnArrEr('参数错误');
        }
        $fx_user_account = new FxUserAccount();
        $result = $fx_user_account->changeStatus($param);
        if ($result == 1) {
            return Util::returnArrSu();
        }
        return Util::returnArrEr('操作失败');
    }

    /*
     * 分销商审核列表
     */
    public function CheckFxUser($param)
    {
        $fx_user = new FxUser();
        $fxList = $fx_user->CheckFxUser($param);
        $fx_list = $fxList['list'];
        $page_size = $param['page_size'];
        $total_count = $fxList['count'];
        $total_pages = ceil($total_count / $page_size);
        $result = [
            'fx_list' => $fx_list,
            'page' => [
                'current_page' => (string)$param['current_page'],
                'total_pages' => (string)$total_pages,
                'page_size' => (string)$page_size,
                'total_count' => (string)$total_count,
            ]
        ];
        return Util::returnArrSu('', $result);
    }

    /*
     * 分销商审核通过
     */
    public function ThroughFxUser($fx_uid, $status)
    {
        if (!in_array($status, array(1, 2))) {
            return Util::returnArrEr('操作失败！[params error_01]');
        }
        if (!Util::checkPattern('intVal', $fx_uid)) {
            return Util::returnArrEr('操作失败！[params error_02]');
        }
        $fx_user = new FxUser();
        $result = $fx_user->ThroughFxUser($fx_uid, $status);
        if ($result == 1) {
            if ($status == 1) {
                $cache = \Yii::$app->cache;
                $tokenKey = 'FX_LOGIN_' . YII_ENV . '_' . $fx_uid;
                $cache->delete($tokenKey);//清除用户的登录token 让用户重新授权登录
            }
            return Util::returnArrSu();
        } else {
            return Util::returnArrEr('操作失败![repetition]');
        }
    }

    /**
     * Des:修改用户名称
     * Name: updateUserName
     * @param $fx_uid
     * @param $user_name
     * @return array
     * @author 倪宗锋
     */
    public function updateUserName($fx_uid, $user_name)
    {
        if (!Util::checkPattern('fx_user_name', $user_name)) {
            return Util::returnArrEr('操作失败！用户名应为100位以内，数字、字母、汉字或下划线！');
        }
        if (!Util::checkPattern('intVal', $fx_uid)) {
            return Util::returnArrEr('操作失败！参数错误！分销账户不明！');
        }
        $fx_user = new FxUser();
        $result = $fx_user->updateUserName($fx_uid, $user_name);
        if ($result == 1) {
            return Util::returnArrSu();
        } else {
            return Util::returnArrEr('操作失败!system error！');
        }
    }

    /**
     * 获取充值订单列表
     * @param $params array
     * @return array
     */
    public function getAmountOrder($params)
    {
        $fxUserAmountOrder = new FxUserAmountOrder();
        $count = $fxUserAmountOrder->getList($params, $type = 1);
        $getList = $fxUserAmountOrder->getList($params);
        $return = [
            'order_list' => $getList,
            'page' => [
                'current_page' => $params['current_page'],
                'total_pages' => ceil($count / $params['page_size']),
                'total_count' => $count,
                'page_size' => $params['page_size']
            ]
        ];
        return Util::returnArrSu('', $return);
    }

    /**
     * Des:绑定运营负责人
     * Name: bindMainCorp
     * @param $params
     * @return array
     * @author 倪宗锋
     */
    public function bindMainCorp($params)
    {
        $fxUser = new FxUser();
        //判断用户是否存在
        $getInfo = $fxUser->getUserInfo($params['fx_uid']);
        if (empty($getInfo['uid'])) {
            return Util::returnArrEr('用户不存在！');
        }
        //判断是否有操作权限
        $cookies = $_COOKIE;
        if (empty($cookies['user_main_corp']) == false && $cookies['user_main_corp'] != 1) {
            if (empty($getInfo['main_corp_id']) == false && $cookies['user_main_corp'] != $getInfo['main_corp_id']) {
                return Util::returnArrEr('您没有权限操作该用户！');
            }
        }
        //如果当前运营负责人和将设置的运营负责人相同则返回成功
        if ($getInfo['main_user_id'] == $params['main_user_id']) {
            return Util::returnArrSu('成功');
        }
        //设置用户的运营负责人
        $bindFlag = $fxUser->bindMainCorp($params['fx_uid'], $params['main_user_id']);
        if (empty($bindFlag) == false) {
            //第一次绑定 则将订单也绑定到该运营负责人下面
            if ($getInfo['main_user_id'] == 0) {
                $orderMain = new OrderMain();
                $orderMain->bindMainCorp($params['fx_uid'], $params['main_user_id']);
            }
            return Util::returnArrSu();
        } else {
            return Util::returnArrEr('设置运维负责人失败！');
        }
    }

    /**
     * Function Description:修改用户改价权限
     * Function Name: uptPriceChange
     * @param $params
     *
     * @return array
     *
     * @author 娄梦宁
     */
    public function uptPriceChange($params)
    {
        $fxUser = new FxUser();
        //判断用户是否存在
        $getInfo = $fxUser->getUserInfo($params['fx_uid']);
        if (empty($getInfo['uid'])) {
            return Util::returnArrEr('用户不存在！');
        }
        $change_price_power = $params['change_price_power'] == 0 ? 1 : 0;
        $bindFlag = $fxUser->uptChangePricePower($params['fx_uid'], $change_price_power, $params['type']);
        if (!$bindFlag) {
            return Util::returnArrEr('修改用户权限失败！');
        }
        return Util::returnArrSu('修改用户权限成功！');
    }

    /**
     * Des:获取用户所属区域列表
     * Name: getUserAreaList
     * @param $fx_uid
     * @return array
     * @author 倪宗锋
     */
    public function getUserAreaList($fx_uid)
    {
        $fxUser = new FxUser();
        //判断用户是否存在
        $getInfo = $fxUser->getUserInfo($fx_uid);
        if (empty($getInfo['uid'])) {
            return Util::returnArrEr('用户不存在！');
        }
        $userArea = $getInfo['user_area'];
        //获取区域列表
        $fxUserArea = new FxUserArea();
        $getList = $fxUserArea->getUserAreaList($userArea);
        return Util::returnArrSu('', $getList);
    }

    /**
     * Des:修改用户所属区域
     * Name: bindUserArea
     * @param $fx_uid
     * @param $area_id
     * @return array
     * @author 倪宗锋
     */
    public function bindUserArea($fx_uid, $area_id)
    {
        $fxUser = new FxUser();
        if (Util::checkPattern('intVal', $area_id) == false) {
            return Util::returnArrEr('参数错误！');
        }
        //判断用户是否存在
        $getInfo = $fxUser->getUserInfo($fx_uid);
        if (empty($getInfo['uid'])) {
            return Util::returnArrEr('用户不存在！');
        }
        $userArea = explode(',', $getInfo['user_area']);
        if (in_array($area_id, $userArea)) {
            return Util::returnArrSu('');
        }
//        array_push($userArea, $area_id);
//        $fxUser->updateUserArea($fx_uid, implode(',', $userArea));
        $fxUser->updateUserArea($fx_uid, $area_id);
        return Util::returnArrSu();
    }

}