/*
 * @Author: YKH
 * @Date: 2021-12-11 21:36:52
 * @LastEditTime: 2022-10-08 14:10:16
 * @Description:
 * @FilePath: \cool-admin-3.x\cool-admin-midway-master\src\app\modules\shuyou\service\utils.ts
 */
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import * as _ from 'lodash';
import { Utils } from '../../../comm/utils';

@Provide()
export class ShuyouUtilsService extends BaseService {
  @Inject()
  utils: Utils;

  /***
   * 获取天气信息
   */
  async getWeatherCD() {
    let cityIds = '101270101'; //成都
    let resWe;
    const data = await getWeather(cityIds);
    let data6 = JSON.parse(data + '');
    if (data6.code == 200) {
      resWe = data6;
    }
    return resWe;
  }

  /**
   *
   *调用钉钉机器人发送消息GET
   * @return {*}
   * @memberof ShuyouUtilsService
   */
  async dingRobot(text: string, atMobiles: string) {
    const data = await dingRobot(text, atMobiles);
    return data;
  }
  /**
   *
   *调用钉钉机器人发送消息Post
   * @return {*}
   * @memberof ShuyouUtilsService
   */
  async postRobot(text: string, atMobiles) {
    const data = await postRobot(text, atMobiles);
    return data;
  }

  /**
   *调用企业微信机器人发送消息Post:数优-加币/未完成播报群
   * @return {*}
   * @memberof ShuyouUtilsService
   */
  async postRobotEnterpriseWeChat(text: string) {
    const time = this.utils.formatDateTime(new Date());
    const data = await postRobotEnterpriseWeChat(text, time);
    return data;
  }

  /**
   *调用企业微信机器人发送消息Post到:数据优化-深圳项目对接群
   * @return {*}
   * @memberof ShuyouUtilsService
   */
  async postRobotEnterpriseWeChat2(text: string) {
    const time = this.utils.formatDateTime(new Date());
    const data = await postRobotEnterpriseWeChat2(text, time);
    return data;
  }
  /**
   *调用企业微信机器人发送消息Post到:数优资源报警群
   * @return {*}
   * @memberof ShuyouUtilsService
   */
  async postRobotEnterpriseWeChat3(text: string) {
    const time = this.utils.formatDateTime(new Date());
    const data = await postRobotEnterpriseWeChat3(text, time);
    return data;
  }
}

//=====================
// 获取天气信息
let getWeather = function (cityIds) {
  var apiUrl =
    'http://aider.meizu.com/app/weather/listWeather?cityIds=' + cityIds;
  var http = require('http');
  var pm = new Promise(function (resolve, reject) {
    http
      .get(apiUrl, function (res) {
        var html = '';
        res.on('data', function (d) {
          html += d.toString();
        });
        res.on('end', function () {
          resolve(html);
        });
      })
      .on('error', function (e) {
        reject(e);
      });
  });
  return pm;
};
// 钉钉机器人发送消息get
let dingRobot = function (text: string, atMobiles: string) {
  var access_token =
    '9513466adf38271afe11d02406497db43e4a8db7dbbe3d0bed2a5919efc3ac2f';
  var apiUrl =
    'https://oapi.dingtalk.com/robot/send?access_token=' + access_token;
  let content = ``;
  let atArr;
  let atString = '';
  // console.log(atMobiles)
  if (atMobiles != '') {
    atArr = atMobiles.split(',');
    atArr.forEach(e => {
      atString = atString + '@' + e;
    });
  }
  // console.log(atArr);
  content =
    atString +
    '**监控报警** \n' +
    text +
    '\n > ##### [点击查看详细数据](http://ykhvip.fun) \n';
  // console.log(content)
  // "#### @18408246387监控报警 \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
  const axios = require('axios');
  var pm = new Promise(function (resolve, reject) {
    axios
      .post(apiUrl, {
        msgtype: 'markdown',
        markdown: {
          title: '数据监控报警',
          text: content,
        },
        at: {
          atMobiles: atArr,
          // 		"atUserIds": [
          // 			"ykh001"
          // 		],
          isAtAll: false,
        },
      })
      .then(res => {
        // console.log(`statusCode: ${res.statusCode}`)
        // console.log(res.data)
        resolve(res.data);
      })
      .catch(error => {
        // console.error(error)
        reject(error);
      });
  });
  return pm;
};
// 钉钉机器人发送消息post
let postRobot = function (text: string, atMobiles: []) {
  var access_token =
    '9513466adf38271afe11d02406497db43e4a8db7dbbe3d0bed2a5919efc3ac2f';
  var apiUrl =
    'https://oapi.dingtalk.com/robot/send?access_token=' + access_token;
  let content = ``;
  let atString = '';
  // console.log(atArr);
  if (atMobiles) {
    atMobiles.forEach(e => {
      atString = atString + '@' + e;
    });
  }
  content =
    atString +
    '**监控报警** \n' +
    text +
    '\n > ##### [点击查看详细数据](http://ykhvip.fun) \n';
  // console.log(content)
  // "#### @18408246387监控报警 \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
  const axios = require('axios');
  var pm = new Promise(function (resolve, reject) {
    axios
      .post(apiUrl, {
        msgtype: 'markdown',
        markdown: {
          title: '数据监控报警',
          text: content,
        },
        at: {
          atMobiles: atMobiles,
          // 		"atUserIds": [
          // 			"ykh001"
          // 		],
          isAtAll: false,
        },
      })
      .then(res => {
        // console.log(`statusCode: ${res.statusCode}`)
        // console.log(res.data)
        resolve(res.data);
      })
      .catch(error => {
        // console.error(error)
        reject(error);
      });
  });
  return pm;
};

// 企业微信机器人发送消息post（数优-加币/未完成播报群）
let postRobotEnterpriseWeChat = function (text: string, time: string) {
  var access_token = '7d095d5b-8240-45fd-a68c-baff3628d83b';
  var apiUrl =
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + access_token;
  let content = ``;
  content =
    '**监控报警' +
    time +
    '** \n' +
    text +
    '\n > ##### [点击进入查看详细数据](http://ykhvip.fun) \n';
  // console.log(content)
  // "#### @18408246387监控报警 \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
  const axios = require('axios');
  var pm = new Promise(function (resolve, reject) {
    axios
      .post(apiUrl, {
        msgtype: 'markdown',
        markdown: {
          content: content,
        },
      })
      .then(res => {
        // console.log(`statusCode: ${res.statusCode}`)
        // console.log(res.data)
        resolve(res.data);
      })
      .catch(error => {
        // console.error(error)
        resolve(error);
      });
  });
  return pm;
};
// 企业微信机器人发送消息post（数据优化-深圳项目对接群）
let postRobotEnterpriseWeChat2 = function (text: string, time: string) {
  var access_token = '5ccfb180-c062-48b5-ae18-0c96f7c19f0b';
  var apiUrl =
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + access_token;
  let content = ``;
  content =
    '**监控报警' +
    time +
    '** \n' +
    text +
    '\n > ##### [点击进入查看详细数据](http://ykhvip.fun) \n';
  // console.log(content)
  // "#### @18408246387监控报警 \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
  const axios = require('axios');
  var pm = new Promise(function (resolve, reject) {
    axios
      .post(apiUrl, {
        msgtype: 'markdown',
        markdown: {
          content: content,
        },
      })
      .then(res => {
        // console.log(`statusCode: ${res.statusCode}`)
        // console.log(res.data)
        resolve(res.data);
      })
      .catch(error => {
        // console.error(error)
        resolve(error);
      });
  });
  return pm;
};
// 企业微信机器人发送消息post（数优资源报警群）
let postRobotEnterpriseWeChat3 = function (text: string, time: string) {
  var access_token = '34cbfa5a-c31d-464f-baf8-8363d6f4ac6a';
  var apiUrl =
    'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + access_token;
  let content = ``;
  content = '**资源不足报警' + time + '** \n' + text + '\n';
  // console.log(content)
  // "#### @18408246387监控报警 \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
  const axios = require('axios');
  var pm = new Promise(function (resolve, reject) {
    axios
      .post(apiUrl, {
        msgtype: 'markdown',
        markdown: {
          content: content,
        },
      })
      .then(res => {
        // console.log(`statusCode: ${res.statusCode}`)
        // console.log(res.data)
        resolve(res.data);
      })
      .catch(error => {
        // console.error(error)
        resolve(error);
      });
  });
  return pm;
};
