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

namespace admin\controllers\user;

use admin\util\AdminUtil;
use common\Controller\AdminController;
use admin\service\user\FxUserService;
use common\models\FxUserArea;
use common\models\FxUserQrcode;
use common\models\MainCorpUser;
use common\models\OrderMain;
use common\util\Util;
use Yii;
use yii\base\Exception;

class FxUserController extends AdminController
{
    /**
     * Des:获取
     * Name: service
     * @author 倪宗锋
     */
    public function service()
    {
        if ($this->service == '') {
            $this->service = new FxUserService();
        }
        return $this->service;
    }

    /**
     * Des:初始化查询条件
     * Name: initSearch
     * @return string
     * @author 倪宗锋
     */
    public function actionInitSearch()
    {
        $mainCorp = new MainCorpUser();
        $mainList = $mainCorp->getListForUserMainCorp();
        $return['mainList'] = $mainList;
        $userArea = new FxUserArea();
        $userAreaList = $userArea->getUserAreaList();
        $return['userAreaList'] = $userAreaList;
        return Util::returnJsSu('', $return);
    }

    /**
     * Function Description:获取分销账户列表
     * Function Name: actionFxUserList
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionFxUserList()
    {
        $param = [
            'wechat_name' => $this->_post('wechat_name'),
            'fx_phone' => $this->_post('fx_phone'),
            'start_reg_time' => $this->_post('start_reg_time'),
            'end_reg_time' => $this->_post('end_reg_time'),
            'current_page' => $this->_post('current_page', 1),
            'page_size' => $this->_post('page_size', 10),
            'main_user_id' => $this->_post('search_main', ''),//运营负责人
            'user_area' => $this->_post('user_area', '')
        ];
        $result = $this->service()->getUserList($param);
        return json_encode($result);
    }

    /**
     * Function Description:获取分销商二维码
     * Function Name: actionGetQrCode
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionGetQrCode()
    {
        $fx_uid = $this->_post('fx_uid');
        $result = $this->service()->getQrCode($fx_uid);
        return json_encode($result);

    }

    /*
     * 分销商账户状态变更
     */
    public function actionChangeFxUser()
    {
        $param = [
            'fx_uid' => $this->_post('fx_uid'),
            'fx_status_id' => $this->_post('fx_status_id'),
        ];
        $result = $this->service()->ChangeFxUser($param);
        return json_encode($result);
    }

    /*
     * 分销商审核列表
     */
    public function actionCheckFxUser()
    {
        $param = [
            'wechat_name' => $this->_post('wechat_name'),
            'fx_phone' => $this->_post('fx_phone'),
            'start_reg_time' => $this->_post('start_reg_time'),
            'end_reg_time' => $this->_post('end_reg_time'),
            'status' => $this->_post('status', ''),
            'page_size' => $this->_post('page_size', 20),
            'current_page' => $this->_post('current_page', 1),
            'main_user_id' => $this->_post('search_main', ''),//运营负责人
            'user_area' => $this->_post('user_area', '')
        ];
        $result = $this->service()->CheckFxUser($param);
        return json_encode($result);
    }

    /*
     * 审核分销商通过
     */
    public function actionThroughFxUser()
    {
        $fx_uid = $this->_post('fx_uid');
        $status = $this->_post('status', '');
        $result = $this->service()->ThroughFxUser($fx_uid, $status);
        return json_encode($result);
    }

    /**
     * Des:修改用户名称
     * Name: actionUpdateUserName
     * @return string
     * @author 倪宗锋
     */
    public function actionUpdateUserName()
    {
        $fx_uid = $this->_post('fx_uid', '');
        $user_name = trim($this->_post('user_name', ''));
        $result = $this->service()->updateUserName($fx_uid, $user_name);
        return json_encode($result);
    }

    /**
     * Des:修改用户名称
     * Name: actionUpdateUserName
     * @return string
     * @author 倪宗锋
     */
    public function actionGetAmountOrder()
    {
        $params = [
            'current_page' => $this->_post('current_page', 1),//显示页编号
            'page_size' => $this->_post('page_size', 20),//每页显示数量
            'fx_uid' => $this->_post('fx_uid', ''),//分销用户ID
        ];
        $result = $this->service()->getAmountOrder($params);
        return json_encode($result);
    }

    /**
     * Des:二维码列表 - 推广使用
     * Name: actionQrCodes
     * @return string
     * @author 倪宗锋
     */
    public function actionQrCodes()
    {
        $fxUserQrCode = new  FxUserQrcode();
        $getList = $fxUserQrCode->getList();
        return $this->render('qr_codes', ['list' => $getList]);
    }

    /**
     * Des:生成二维码 - 推广使用
     * Name: actionAddQrCodes
     * @return array|string
     * @author 倪宗锋
     */
    public function actionAddQrCodes()
    {
        ini_set('max_execution_time', '0');
        $qrCodeId = $this->_post('qr_id', '');
//        if (Util::checkPattern('intVal', $qrCodeId) == false || $qrCodeId <= 20000 && $qrCodeId >= 25000) {
//            return Util::returnJsEr('参数需为2万到2万5千之间的正整型');
//        }
        for ($i = 0; $i < 10; $i++) {
            $qrCodeId += $i;
            $this->addCodes($qrCodeId);
        }
        return Util::returnJsSu();

    }

    public function addCodes($qrCodeId)
    {
        $fxUserQrCode = new  FxUserQrcode();
        //查询记录
        $getInfo = $fxUserQrCode->getInfoByQrId($qrCodeId);
        if (empty($getInfo['qr_id']) == false) {
            return Util::returnArrEr('二维码已经生成过');
        }
        $transaction = \Yii::$app->db->beginTransaction();
        try {
            //添加记录
            $addFlag = $fxUserQrCode->addNew($qrCodeId);
            if ($addFlag == false) {
                return Util::returnArrEr('插入记录失败！');
            }
            //生成二维码
            $result = $this->service()->getQrCode($qrCodeId);
            if ($result['flag'] == false) {
                $transaction->rollBack();
                return Util::returnArrEr('生成二维码失败！');
            }
            $transaction->commit();
            return Util::returnArrSu();
        } catch (Exception $e) {
            $transaction->rollBack();
            return Util::returnArrEr('生成二维码异常');
        }
    }

    /**
     * Des:获取运维人员列表
     * Name: actionGetMainCorp
     * @return string
     * @author 倪宗锋
     */
    public function actionGetMainCorp()
    {
        $mainCorp = new MainCorpUser();
        $list = $mainCorp->getListForUserMainCorp();
        return Util::returnJsSu('', ['list' => $list]);
    }

    /**
     * Des:绑定运维负责人
     * Name: actionBindMainCorp
     * @return string
     * @author 倪宗锋
     */
    public function actionBindMainCorp()
    {
        $uid = AdminUtil::$uid;
        $siteConfig = Util::getSiteConfig();
        $bindFxUser = $siteConfig['bind_fx_user'];
        if (in_array($uid, $bindFxUser) == false) {
            return Util::returnJsEr('您没有执行此操作的权限！');
        }
        $params = [
            'fx_uid' => $this->_post('fx_uid', ''),
            'main_user_id' => $this->_post('main_corp_id', '')
        ];
        $return = $this->service()->bindMainCorp($params);
        return json_encode($return);
    }

    /**
     * Function Description:修改用户改价权限
     * Function Name: actionUptPriceChange
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionUptPriceChange()
    {
        $params = [
            'fx_uid' => $this->_post('fx_uid', ''),
            'change_price_power' => $this->_post('change_price_power', 0),
            'type' => $this->_post('type', 1),//权限类型 1 巴士自由行改价权限  2、酒店改价权限
        ];
        $return = $this->service()->uptPriceChange($params);
        return json_encode($return);
    }

    /**
     * Des:获取用户所属区域选择列表
     * Name: actionGetUserAreaList
     * @return string
     * @author 倪宗锋
     */
    public function actionGetUserAreaList()
    {
        $fx_uid = $this->_post('fx_uid', '');//用户ID
        $return = $this->service()->getUserAreaList($fx_uid);
        return json_encode($return);
    }

    /**
     * Des:绑定用户所属区域
     * Name: actionBindUserArea
     * @return string
     * @author 倪宗锋
     */
    public function actionBindUserArea()
    {
        $fx_uid = $this->_post('fx_uid', '');//分销商ID
        $area_id = $this->_post('area_id', '');//区域ID
        $result = $this->service()->bindUserArea($fx_uid, $area_id);
        return json_encode($result);
    }
}