/**
  * Remove a website into help desk setting
  *
  * <b>Request Type: </b>PUT<br/>
  * <b>Request Endpoint: </b>http://{server-domain}/api/helpdesk/setting/remove-website<br/>
  * <b>Content-type: </b>Application/json<br/>
  * <b>Summary: </b>This api is for removing a website into help desk setting.<br/>
  *
  * <b>Request Example: </b>
  * <pre>
  *     {
  *          "settingId": '52d791327ae252f9149547cb',
  *          "websiteId": '52d791307ae252f9149547c9'
  *     }
  * </pre>
  */
 public function actionRemoveWebsite()
 {
     $settingId = $this->getParams('settingId');
     $websiteId = $this->getParams('websiteId');
     if (!empty($websiteId) && !empty($settingId)) {
         $settingId = new \MongoId($settingId);
         // Add a channel into help desk setting
         $result = HelpDeskSetting::updateAll(['$pull' => ['websites' => ['id' => $websiteId]]], ['_id' => $settingId]);
         if ($result) {
             return $websiteId . '';
         }
         throw new ServerErrorHttpException('remove website fail');
     }
     throw new BadRequestHttpException('parameters missing');
 }
 /**
  * Remove bound a channel account
  *
  * <b>Request Type</b>: DELETE<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/management/channel/{channelaccountId}<br/><br/>
  * <b>Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used for removing bound a channel account.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     channelAccount: string, the channel account id<br/>
  *     channelType: string, channel type("weibo" , "wechat", ...)<br/>
  *     <br/><br/>
  *
  * <b>Response Params:</b><br/>
  *     ack: integer, mark the delete result, 0 means query fail, 1 means delete successfully<br/>
  *     msg: string, if query fail, it contains the error message<br/>
  *     data: array, json array to deleted channel detail information<br/>
  *     <br/><br/>
  *
  * <b>Request Example:</b><br/>
  * <pre>
  * {
  *     "channelAccount": "gh_fdba39256c8e",
  *     "channelType": "wechat",
  * }
  * </pre>
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *    'data' : []
  * }
  * </pre>
  */
 public function actionDelete($id)
 {
     $accountId = $this->getAccountId();
     $params = $this->getParams();
     if (empty($params['type']) || !in_array($params['type'], [Channel::WEIBO, Channel::ALIPAY])) {
         throw new BadRequestHttpException(Yii::t('common', 'data_error'));
     }
     $channelType = $params['type'];
     // Recovery micro-blog authorized access token
     if ($channelType == Channel::WEIBO) {
         $token = $params['weiboToken'];
         Yii::$app->weiboConnect->revokeWeiboToken($token);
     }
     //refine to call wechat-connection system api(Delete One Account)
     $result = Yii::$app->weConnect->deleteAccount($id);
     if ($result) {
         if (!Channel::disableByChannelIds($accountId, [$id])) {
             throw new \yii\web\ServerErrorHttpException("delete account channel failed");
         }
         if (false === HelpDeskSetting::updateAll(['$pull' => ['channels' => ['id' => $id]]], ['accountId' => $accountId])) {
             throw new \yii\web\ServerErrorHttpException("delete helpdesk setting channel failed");
         }
     } else {
         throw new \yii\web\ServerErrorHttpException("delete channel failed");
     }
     return [];
 }
 /**
  * Create menu
  *
  * <b>Request Type</b>: POST<br/><br/>
  * <b>Request Endpoint</b>:http://{server-domain}/api/channel/menus<br/><br/>
  * <b>Response Content-type</b>: application/json<br/><br/>
  * <b>Summary</b>: This api is used to create/update menu.
  * <br/><br/>
  *
  * <b>Request Params</b>:<br/>
  *     channelId: string<br/>
  *     menu.name: string<br/>
  *     menu.keycode: string<br/>
  *     menu.type: string, VIEW or CLICK<br/>
  *     menu.subMenus.name: string<br/>
  *     menu.subMenus.type: string<br/>
  *     menu.subMenus.msgType: TEXT or NEWS<br/>
  *     menu.subMenus.content: string, if TEXT<br/>
  *     menu.subMenus.content.articles: array, if NEWS<br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Request Example</b>:<br/>
  * <pre>
  * {
  *  "channelId": "5473ffe7db7c7c2f0bee5c71",
  *  "menu": [
  *      {
  *          ...
  *      },
  *      {
  *          "name": "去踢球",
  *          "type": "CLICK",
  *          "subMenus": [
  *              {
  *                  "name": "menu1-sub1",
  *                  "type": "VIEW",
  *                  "msgType": "TEXT",
  *                  "content": "hello world"
  *              },
  *              {
  *                  "name": "menu1-sub2",
  *                  "type": "VIEW",
  *                  "msgType": "URL",
  *                  "content": "http://www.baidu.com"
  *              },
  *              {
  *                  "name": "menu1-sub3",
  *                  "type": "VIEW",
  *                  "msgType": "NEWS",
  *                  "content": {
  *                      "articles": [
  *                          {
  *                              "title": "新闻",
  *                              "description": "APEC会议举行第三天",
  *                              "sourceUrl": "http://www.baidu.com",
  *                              "picUrl": "http://www.baidu.com/image.jpg"
  *                          },
  *                          {
  *                              "title": "新闻",
  *                              "description": "APEC会议举行第三天",
  *                              "sourceUrl": "http://www.baidu.com",
  *                              "picUrl": "http://www.baidu.com/image.jpg"
  *                          }
  *                      ]
  *                  }
  *              }
  *          ]
  *      },
  *      {
  *          ...
  *      }
  *  ]
  * }
  *
  * <b>Response Params:</b><br/>
  *     msg: string, if query fail, it contains the error message<br/>
  *     <br/><br/>
  *
  * <br/><br/>
  *
  * <b>Response Example</b>:<br/>
  * <pre>
  * {
  *   "message": "OK"
  * }
  * </pre>
  */
 public function actionCreate()
 {
     $menu = $this->getParams();
     $channelId = $this->getChannelId();
     $accountId = $this->getAccountId();
     unset($menu['channelId']);
     $actions = Yii::$app->channelMenu->getMenuActions($channelId, $accountId, true);
     $result = Yii::$app->weConnect->createMenu($channelId, $menu['menu'], $actions);
     if ($result) {
         $conditon = ['accountId' => $accountId, 'channels.id' => $channelId];
         $helpDeskSetting = HelpDeskSetting::findOne($conditon);
         if (!empty($helpDeskSetting)) {
             $isSet = Yii::$app->weConnect->isSetHelpDesk($menu['menu']);
             $channelResult = HelpDeskSetting::updateAll(['$set' => ['channels.$.isSet' => $isSet]], $conditon);
             if (!$channelResult) {
                 throw new ServerErrorHttpException('Set menu channel status fail.');
             }
         }
         return ['message' => 'OK'];
     } else {
         throw new ServerErrorHttpException('Create menu fail.');
     }
 }