/*
 * @Author: YKH
 * @Date: 2021-12-15 15:16:03
 * @LastEditTime: 2022-01-17 14:09:10
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\controller\app\send_content.ts
 */
import { ALL, Body, Get, Inject, Post, Provide, Query } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { ILogger } from '@midwayjs/logger';
import { ShuyouSendContentService } from '../../service/send_content';
import { ShuyouUtilsService } from '../../service/utils';
import { ShuyouGameDirectorService } from '../../service/game_director';

/**
 * 游戏负责人
 */
@Provide()
@CoolController()
export class ShuyouAppSendContentController extends BaseController {

  @Inject()
  logger: ILogger;

  @Inject()
  ShuyouUtilsService: ShuyouUtilsService;

  @Inject()
  ShuyouSendContentService: ShuyouSendContentService;

  @Inject()
  ShuyouGameDirectorService: ShuyouGameDirectorService;
  /**
   *
   * http://127.0.0.1:8001/app/shuyou/send_content/ding_robot?text=**异常:**&atMobiles=18408246387,13088022436
   * http://ykhvip.fun/api/app/shuyou/send_content/ding_robot?text=**%E5%BC%82%E5%B8%B8:**&atMobiles=18408246387,13088022436
   * @param {object} queryObject
   * @return {*} 
   * @memberof ShuyouAppSendContentController
   */
  @Get('/ding_robot')
  async dingRobot(@Query(ALL) queryObject) {
    const { text, atMobiles, atNames } = queryObject
    if (!text) {
      return this.fail('缺少必要参数[text=消息内容&atNames=被@人&atMobiles=被@人手机号,atMobiles可不填]', -1)
    }
    let atPhoneStr = '';
    if (atMobiles) {
      atPhoneStr = atMobiles + ','
    }
    if (atNames && atNames != '') {
      //查询@人对应手机号
      //
      let atNameStr;
      atNames.split(',').forEach((item, index) => {
        if (index == 0) {
          atNameStr = ` name = '` + item + `'`;
        } else {
          atNameStr = atNameStr + ` or name = '` + item + `'`;
        }
      });
      let phones = await this.ShuyouGameDirectorService.queryPhoneByNames(atNameStr);
      phones.forEach(e => {
        atPhoneStr = atPhoneStr + e.phone + ','
      });
    }
    let res;
    res = await this.ShuyouUtilsService.dingRobot(text, atPhoneStr);
    let success = 0
    if (res.errcode == 0) {
      success = 1
    }
    let insertObj = {
      msgtype: 'markdown',
      title: '数据监控报警',
      text: text,
      atMobiles: atMobiles,
      isSuccess: success
    }
    await this.ShuyouSendContentService.insert(insertObj)
    if (success == 1) {
      return this.ok(res);
    } else {
      return this.fail('发送信息失败', -1);
    }
  }
  /**
   * http://127.0.0.1:8001/app/shuyou/send_content/ding_robot
   *  {
   *  "text": "**异常:**",
   *  "atMobiles": [
   *    "18408246387",
   *    "13088022436"
   *  ]
   * }
   * @param {object} queryObject
   * @return {*} http://ykhvip.fun/api
   * @memberof ShuyouAppSendContentController
   */
  @Post('/ding_robot')
  async dingRobotPost(@Body(ALL) queryObject) {
    // this.logger.info("Post")
    const { text, atMobiles, atNames } = queryObject
    let resObj = {}
    if (!text) {
      resObj = {
        msg: '缺少必要参数↓↓↓↓',
        text: '消息内容,必填',
        atNames: '被@人',
        atMobiles: '被@人手机号, atMobiles可不填'
      }
      return this.fail(resObj + '', -1)
    }
    let atPhoneArr = []
    if (atMobiles && atMobiles.length != 0) {
      atPhoneArr = atMobiles
    }
    if (atNames && atNames.length != 0) {
      //查询@人对应手机号
      //
      let atNameStr = '';
      atNames.forEach((item, index) => {
        if (index == 0) {
          atNameStr = ` name = '` + item + `'`;
        } else {
          atNameStr = atNameStr + ` or name = '` + item + `'`;
        }
      });
      let phones = await this.ShuyouGameDirectorService.queryPhoneByNames(atNameStr);
      phones.forEach(e => {
        atPhoneArr.push(e.phone)
      });
    }
    let res;
    res = await this.ShuyouUtilsService.postRobot(text, atPhoneArr);
    let success = 0
    if (res.errcode == 0) {
      success = 1
    }
    let insertObj = {
      msgtype: 'markdown',
      title: '数据监控报警',
      text: text,
      atMobiles: `'` + atMobiles + `'`,
      atUserIds: `'` + atNames + `'`,
      isSuccess: success
    }
    await this.ShuyouSendContentService.insert(insertObj)
    if (success == 1) {
      return this.ok(res);
    } else {
      return this.fail('发送信息失败', -1);
    }
  }

}
