hide_game_task.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. import { Inject, Provide } from '@midwayjs/decorator';
  2. import { BaseService, CoolCommException } from 'midwayjs-cool-core';
  3. import { InjectEntityModel } from '@midwayjs/orm';
  4. import { Repository } from 'typeorm';
  5. import * as _ from 'lodash';
  6. import { ILogger } from '@midwayjs/logger';
  7. import { HideGameTaskEntity } from '../entity/hide_game_task';
  8. import { HideGameTargetEntity } from '../entity/hide_game_target';
  9. import { Utils } from '../../../comm/utils';
  10. import { HideGameCompleteEntity } from '../entity/hide_game_complete';
  11. import { HideGameAgentService } from './hide_game_agent';
  12. import { HideTaskGameRecordEntity } from '../entity/hide_task_game_record';
  13. import { HideLoModeService } from './hide_lo_mode';
  14. import { HideGameDirectorService } from './hide_game_director';
  15. import { createTableService } from './createTable';
  16. /**
  17. * 系统用户
  18. */
  19. @Provide()
  20. export class HideGameTaskService extends BaseService {
  21. @InjectEntityModel(HideGameTaskEntity)
  22. HideGameTaskEntity: Repository<HideGameTaskEntity>;
  23. @InjectEntityModel(HideGameTargetEntity)
  24. HideGameTargetEntity: Repository<HideGameTargetEntity>;
  25. @InjectEntityModel(HideGameCompleteEntity)
  26. HideGameCompleteEntity: Repository<HideGameCompleteEntity>;
  27. @InjectEntityModel(HideTaskGameRecordEntity)
  28. HideTaskGameRecordEntity: Repository<HideTaskGameRecordEntity>;
  29. @Inject()
  30. HideGameAgentService: HideGameAgentService;
  31. @Inject()
  32. HideLoModeService: HideLoModeService;
  33. @Inject()
  34. HideGameDirectorService: HideGameDirectorService;
  35. @Inject()
  36. logger: ILogger;
  37. @Inject()
  38. utils: Utils;
  39. @Inject()
  40. createTableService: createTableService;
  41. /**
  42. * 重载新增接口
  43. */
  44. async add(param) {
  45. const existsTask = await this.HideGameTaskEntity.findOne({
  46. id: param.id,
  47. });
  48. if (!_.isEmpty(existsTask)) {
  49. throw new CoolCommException('已存在相同任务id~');
  50. }
  51. param.createTime = new Date()
  52. param.updateTime = new Date()
  53. //======================为每个任务单独生成一张账号表,用于存储留存账号
  54. this.createTableService.createAccountTable(param.id)
  55. // let taskAccount = {
  56. // name: "hide_game_account_" + param.id,
  57. // columns: [
  58. // {
  59. // name: "id",
  60. // type: "int",
  61. // isPrimary: true,
  62. // length: "11"
  63. // },
  64. // {
  65. // name: "account_text",
  66. // type: "varchar",
  67. // length: "50",
  68. // comment: "账号"
  69. // },
  70. // {
  71. // name: "password_text",
  72. // type: "varchar",
  73. // length: "50",
  74. // comment: "密码"
  75. // },
  76. // {
  77. // name: "new_date",
  78. // type: "date",
  79. // comment: "日期"
  80. // },
  81. // {
  82. // name: "vpn_text",
  83. // type: "varchar",
  84. // length: "50",
  85. // comment: "vpn域"
  86. // }, {
  87. // name: "manufacturer",
  88. // type: "varchar",
  89. // length: "50",
  90. // comment: "厂商"
  91. // }
  92. // ]
  93. // }
  94. // const queryRunner = await getConnection().createQueryRunner();
  95. // await queryRunner.createTable(new Table(taskAccount), true);
  96. // await queryRunner.release();
  97. //======================
  98. // console.log(param)
  99. // 创建游戏目标start==========================
  100. const existsTarget = await this.HideGameTargetEntity.findOne({
  101. task_id: param.id,
  102. new_date: param.new_date
  103. });
  104. let target_new = 0
  105. let target_pay = 0
  106. let target_active = 0
  107. target_new = Math.floor(Math.random() * (parseInt(param.target_new_high) - parseInt(param.target_new_low) + 1) + parseInt(param.target_new_low))
  108. target_pay = Math.floor(Math.random() * (parseInt(param.target_pay_high) - parseInt(param.target_pay_low) + 1) + parseInt(param.target_pay_low))
  109. target_active = Math.floor(Math.random() * (parseInt(param.target_active_high) - parseInt(param.target_active_low) + 1) + parseInt(param.target_active_low))
  110. //
  111. if (!_.isEmpty(existsTarget)) {
  112. // 当天的任务目标已存在,执行更新目标
  113. let targetValue = {
  114. task_name: param.task_name,
  115. updateTime: param.updateTime,
  116. target_new: target_new,
  117. target_pay: target_pay,
  118. target_active: target_active,
  119. target_new_low: param.target_new_low,
  120. target_pay_low: param.target_pay_low,
  121. target_active_low: param.target_active_low
  122. }
  123. // this.logger.info(targetValue)
  124. await this.HideGameTargetEntity
  125. .createQueryBuilder()
  126. .update()
  127. .set(targetValue)
  128. .where('task_id = :task_id and new_date = :new_date', {
  129. task_id: param.id,
  130. new_date: param.new_date
  131. })
  132. .execute();
  133. // this.logger.info(updateResult) //UpdateResult { generatedMaps: [], raw: [], affected: 1 }
  134. // 执行更新目标
  135. } else {
  136. // 添加任务目标
  137. let targetValue = {
  138. task_id: param.id,
  139. task_name: param.task_name,
  140. directorId: param.directorId,
  141. new_date: param.new_date,
  142. createTime: param.createTime,
  143. updateTime: param.updateTime,
  144. target_new: target_new,
  145. target_pay: target_pay,
  146. target_active: target_active,
  147. target_new_low: param.target_new_low,
  148. target_pay_low: param.target_pay_low,
  149. target_active_low: param.target_active_low
  150. }
  151. // this.logger.info(targetValue)
  152. await this.HideGameTargetEntity.insert(targetValue)
  153. }
  154. // 创建游戏目标end==========================
  155. // 创建游戏完成game_complete===============================
  156. const existsComplete = await this.HideGameCompleteEntity.findOne({
  157. task_id: param.id,
  158. new_date: param.new_date
  159. })
  160. if (_.isEmpty(existsComplete)) {
  161. // 添加任务完成game_complete
  162. let completeObj = {
  163. task_id: param.id,
  164. task_name: param.task_name,
  165. new_date: param.new_date,
  166. createTime: param.createTime,
  167. updateTime: param.updateTime,
  168. complete_new: 0,
  169. complete_pay: 0,
  170. complete_active: 0
  171. }
  172. // this.logger.info(completeObj)
  173. await this.HideGameCompleteEntity.insert(completeObj)
  174. }
  175. // 创建游戏目标end==========================
  176. // 记录端口对应的游戏数量start
  177. await this.HideGameAgentService.recordGameNum(param.game_agentId)
  178. // this.logger.info(returnMsg)
  179. // 记录端口对应的游戏数量end
  180. await this.updateTaskGame(param);
  181. return super.add(param);
  182. }
  183. /**
  184. * 重写分页查询
  185. * @param query
  186. */
  187. async page(query) {
  188. const { id, task_name, is_run, login_modeId, directorId, game_agentId, account_typeId } = query;
  189. const sql = `
  190. SELECT
  191. a.id,a.task_name,a.new_date,a.createTime,a.updateTime,a.target_new_low,a.target_new_high,a.target_pay_low,a.target_pay_high,a.target_active_low,a.target_active_high,
  192. a.lc_two_ratio,a.lc_three_ratio,a.lc_four_ratio,a.lc_five_ratio,a.lc_six_ratio,a.lc_seven_ratio,a.lc_eight_ratio,a.lc_fifteen_ratio,a.lc_thirty_ratio,a.stopTime,a.stopRemark,
  193. a.remark,a.is_run,a.is_repeat,a.download,a.unit_price_low,a.unit_price_high,
  194. any_value (b.name) AS directorName,b.id AS directorId,
  195. any_value (c.name) AS login_modeId,
  196. any_value (d.name) AS game_agentId,
  197. any_value (e.name) AS account_typeId,
  198. GROUP_CONCAT(g.game_name) AS gameList
  199. FROM
  200. hide_game_task a
  201. LEFT JOIN hide_game_director b ON a.directorId = b.id
  202. LEFT JOIN hide_lo_mode c ON a.login_modeId = c.id
  203. LEFT JOIN hide_game_agent d ON a.game_agentId = d.id
  204. LEFT JOIN hide_account_type e ON a.account_typeId = e.id
  205. LEFT JOIN hide_task_game_record t ON a.id = t.task_id
  206. LEFT JOIN hide_game_list g ON t.game_id = g.id
  207. WHERE 1 = 1
  208. ${this.setSql(id, 'and a.id = ?', [id])}
  209. ${this.setSql(task_name, 'and a.task_name LIKE ?', [`%${task_name}%`])}
  210. ${this.setSql(is_run, 'and a.is_run = ?', [is_run])}
  211. ${this.setSql(directorId, 'and b.id = ?', [directorId])}
  212. ${this.setSql(game_agentId, 'and d.id = ?', [game_agentId])}
  213. ${this.setSql(account_typeId, 'and e.id = ?', [account_typeId])}
  214. ${this.setSql(login_modeId, 'and c.id = ?', [login_modeId])}
  215. GROUP BY a.id
  216. `;
  217. return this.sqlRenderPage(sql, query);
  218. }
  219. /**
  220. * 根据ID获得信息
  221. * @param id
  222. */
  223. public async info(id) {
  224. const info = await this.HideGameTaskEntity.findOne({ id });
  225. const gameIds = await this.nativeQuery(
  226. 'select a.game_id from hide_task_game_record a where a.task_id = ?',
  227. [id]
  228. );
  229. if (info) {
  230. // delete info.password;
  231. if (gameIds) {
  232. info.gameIdList = gameIds.map(e => {
  233. return parseInt(e.game_id);
  234. });
  235. }
  236. }
  237. return info;
  238. }
  239. /**
  240. * 修改
  241. * @param param 数据
  242. */
  243. async update(param) {
  244. // console.log(param)
  245. const new_game_agentId = param.game_agentId
  246. const new_login_modeId = param.login_modeId
  247. const new_directorId = param.directorId
  248. const new_task_name = param.task_name
  249. const taskInfo = await this.HideGameTaskEntity.findOne({ id: param.id });
  250. if (!taskInfo) {
  251. throw new CoolCommException('任务不存在');
  252. }
  253. const old_game_agentId = taskInfo.game_agentId
  254. const old_login_modeId = taskInfo.login_modeId
  255. const old_directorId = taskInfo.directorId
  256. const old_task_name = taskInfo.task_name
  257. await this.HideGameTaskEntity.save(param);
  258. await this.updateTaskGame(param);
  259. //检测负责人-厂商-端口,如有变化,则更新对应数量
  260. if (new_game_agentId != old_game_agentId) {
  261. await this.HideGameAgentService.recordGameNum(new_game_agentId)
  262. await this.HideGameAgentService.reduceGameNum(old_game_agentId)
  263. }
  264. if (new_login_modeId != old_login_modeId) {
  265. await this.HideLoModeService.recordGameNum(new_game_agentId)
  266. await this.HideLoModeService.reduceGameNum(old_game_agentId)
  267. }
  268. if (new_directorId != old_directorId) {
  269. await this.HideGameDirectorService.addGameNumById(new_game_agentId)
  270. await this.HideGameDirectorService.reduceGameNumById(old_game_agentId)
  271. }
  272. //修改当天任务表里的任务名称(当名称变化时)
  273. if (old_task_name != new_task_name) {
  274. const existsTarget = await this.HideGameTargetEntity.findOne({
  275. task_id: param.id,
  276. new_date: this.utils.formatDate(new Date())
  277. });
  278. if (!_.isEmpty(existsTarget)) {
  279. // 当天的任务目标已存在,执行更新目标
  280. let targetValue = {
  281. task_name: new_task_name
  282. }
  283. // this.logger.info(targetValue)
  284. await this.HideGameTargetEntity
  285. .createQueryBuilder()
  286. .update()
  287. .set(targetValue)
  288. .where('task_id = :task_id and new_date = :new_date', {
  289. task_id: param.id,
  290. new_date: this.utils.formatDate(new Date())
  291. })
  292. .execute();
  293. // this.logger.info(updateResult) //UpdateResult { generatedMaps: [], raw: [], affected: 1 }
  294. }
  295. }
  296. }
  297. /**
  298. * 更新任务-游戏关系
  299. * @param taskGame
  300. */
  301. async updateTaskGame(taskGame) {
  302. await this.HideTaskGameRecordEntity.delete({ task_id: taskGame.id });
  303. if (taskGame.gameIdList) {
  304. for (const game_id of taskGame.gameIdList) {
  305. await this.HideTaskGameRecordEntity.save({ task_id: taskGame.id, game_id });
  306. }
  307. }
  308. }
  309. /**
  310. * 删除
  311. * @param ids
  312. */
  313. async delete(ids) {
  314. let idArr;
  315. console.log(ids)
  316. if (ids instanceof Array) {
  317. idArr = ids;
  318. } else {
  319. idArr = ids.split(',');
  320. }
  321. for (const id of idArr) {
  322. //删除游戏时判断游戏是否在数优中,如在,则对应负责人,端口,厂商所属游戏数量-1
  323. const taskInfo = await this.HideGameTaskEntity.findOne({ id: id });
  324. if (taskInfo) {
  325. // console.log(taskInfo)
  326. if (taskInfo.is_run == 1) {
  327. await this.HideGameAgentService.reduceGameNum(taskInfo.game_agentId)
  328. await this.HideLoModeService.reduceGameNum(taskInfo.login_modeId)
  329. await this.HideGameDirectorService.reduceGameNumById(taskInfo.directorId)
  330. }
  331. }
  332. //===================================================================================
  333. await this.HideTaskGameRecordEntity.delete({ task_id: id });
  334. await this.HideGameTaskEntity.delete({ id: id });
  335. }
  336. }
  337. /**
  338. * 根据id删除任务
  339. * @param id
  340. */
  341. async deleteById(id) {
  342. //===================================================================================
  343. // console.log(id)
  344. // console.log(this.utils.formatDate(new Date()))
  345. await this.HideTaskGameRecordEntity.delete({ task_id: id });
  346. await this.HideGameTaskEntity.delete({ id: id });
  347. await this.HideGameTargetEntity.delete({ task_id: id, new_date: this.utils.formatDate(new Date()) })
  348. // await this.HideGameCompleteEntity.delete({ task_id: id, new_date: this.utils.formatDate(new Date()) })
  349. return 1000
  350. }
  351. //
  352. /**
  353. *
  354. *
  355. * @param {string} task_id
  356. * @param {Date} stopTime
  357. * @param {string} stopRemark
  358. * @return {*}
  359. * @memberof HideGameTaskService
  360. */
  361. async stopTask(task_id: string, stopTime: Date, stopRemark: string) {
  362. // const { id_name } = query;
  363. let obj = {
  364. id: task_id
  365. }
  366. const HideGameTaskEntity = await this.HideGameTaskEntity.findOne(obj);
  367. if (HideGameTaskEntity) {
  368. if (new Date(stopTime).getTime() > new Date().getTime()) {
  369. HideGameTaskEntity.is_run = 1;
  370. } else {
  371. HideGameTaskEntity.is_run = 0;
  372. }
  373. HideGameTaskEntity.stopTime = stopTime;
  374. HideGameTaskEntity.stopRemark = stopRemark;
  375. // this.logger.info(HideGameTaskEntity)
  376. await this.HideGameTaskEntity.save(HideGameTaskEntity);
  377. return 1000
  378. } else {
  379. return -1
  380. }
  381. }
  382. /**
  383. *
  384. *
  385. * @param {string} task_id
  386. * @return {*}
  387. * @memberof HideGameTaskService
  388. */
  389. async startTask(task_id: string) {
  390. // const { id_name } = query;
  391. let obj = {
  392. id: task_id
  393. }
  394. const HideGameTaskEntity = await this.HideGameTaskEntity.findOne(obj);
  395. if (HideGameTaskEntity) {
  396. HideGameTaskEntity.is_run = 1;
  397. HideGameTaskEntity.new_date = new Date(this.utils.formatDate(new Date()));
  398. HideGameTaskEntity.stopTime = new Date('2099-01-01');
  399. HideGameTaskEntity.stopRemark = '';
  400. // this.logger.info(HideGameTaskEntity)
  401. await this.HideGameTaskEntity.save(HideGameTaskEntity);
  402. return 1000
  403. } else {
  404. return -1
  405. }
  406. }
  407. /**
  408. * 获取新增账号
  409. * @param {*} task_id
  410. * @param {*} account_typeId
  411. * @return {*}
  412. * @memberof HideGameTaskService
  413. */
  414. async getAccountOne(task_id, account_typeId, account_state) {
  415. const sql = `
  416. SELECT
  417. A.id AS id,
  418. a.account_text,
  419. a.password_text,
  420. a.vpn_text,
  421. a.manufacturer,
  422. a.model,
  423. a.pnumber,
  424. a.imei,
  425. a.imsi,
  426. a.simserial,
  427. a.androidid,
  428. a.mac
  429. FROM
  430. hide_game_account AS A
  431. WHERE
  432. 1 = 1
  433. ${this.setSql(account_typeId, 'and type_id = ?', [account_typeId])}
  434. AND occupy_taskId = 0
  435. ${this.setSql(account_state, 'AND STATUS = ?', [account_state])}
  436. AND
  437. ( SELECT count( 1 ) AS num FROM hide_task_account_record AS T WHERE A.id = T.account_id
  438. ${this.setSql(task_id, 'AND T.task_id = ?', [task_id])}
  439. )= 0
  440. AND (
  441. SELECT
  442. count( 1 ) AS num
  443. FROM
  444. game_account_record AS G
  445. WHERE
  446. G.game_id IN ( SELECT game_id FROM hide_task_game_record r WHERE
  447. ${this.setSql(task_id, 'r.task_id = ?', [task_id])}
  448. )
  449. AND A.id = G.account_id
  450. )= 0
  451. ORDER BY
  452. A.id
  453. LIMIT 1
  454. `;
  455. return this.nativeQuery(sql);
  456. }
  457. /**
  458. * 获取留存账号
  459. * @param {*} task_id
  460. * @param {*} retained_day
  461. * @return {*}
  462. * @memberof HideGameTaskService
  463. */
  464. async getAccountRetainedOne(task_id, retained_date) {
  465. const sql = `
  466. SELECT
  467. A.id AS id,
  468. b.account_text AS account_text,
  469. b.password_text AS password_text,
  470. b.vpn_text AS vpn_text,
  471. b.manufacturer AS manufacturer,
  472. b.model AS model,
  473. b.pnumber AS pnumber,
  474. b.imei AS imei,
  475. b.imsi AS imsi,
  476. b.simserial AS simserial,
  477. b.androidid AS androidid,
  478. b.mac AS mac
  479. FROM
  480. hide_task_account_record AS a
  481. LEFT JOIN hide_game_account b ON a.account_id = b.id
  482. WHERE
  483. a.statusRetained = 0 AND a.ok_new = 1
  484. ${this.setSql(task_id, 'AND a.task_id = ?', [task_id])}
  485. ${this.setSql(retained_date, 'AND a.new_date = ?', [retained_date])}
  486. ORDER BY
  487. A.id
  488. LIMIT 1
  489. `;
  490. return this.nativeQuery(sql);
  491. }
  492. }