import { Inject, Provide } from '@midwayjs/decorator'; import { BaseService, CoolCommException } from 'midwayjs-cool-core'; import { InjectEntityModel } from '@midwayjs/orm'; import { Repository } from 'typeorm'; import * as _ from 'lodash'; import { ILogger } from '@midwayjs/logger'; import { HideGameTaskEntity } from '../entity/hide_game_task'; import { HideGameTargetEntity } from '../entity/hide_game_target'; import { Utils } from '../../../comm/utils'; import { HideGameCompleteEntity } from '../entity/hide_game_complete'; import { HideGameAgentService } from './hide_game_agent'; import { HideTaskGameRecordEntity } from '../entity/hide_task_game_record'; import { HideLoModeService } from './hide_lo_mode'; import { HideGameDirectorService } from './hide_game_director'; import { createTableService } from './createTable'; /** * 系统用户 */ @Provide() export class HideGameTaskService extends BaseService { @InjectEntityModel(HideGameTaskEntity) HideGameTaskEntity: Repository; @InjectEntityModel(HideGameTargetEntity) HideGameTargetEntity: Repository; @InjectEntityModel(HideGameCompleteEntity) HideGameCompleteEntity: Repository; @InjectEntityModel(HideTaskGameRecordEntity) HideTaskGameRecordEntity: Repository; @Inject() HideGameAgentService: HideGameAgentService; @Inject() HideLoModeService: HideLoModeService; @Inject() HideGameDirectorService: HideGameDirectorService; @Inject() logger: ILogger; @Inject() utils: Utils; @Inject() createTableService: createTableService; /** * 重载新增接口 */ async add(param) { const existsTask = await this.HideGameTaskEntity.findOne({ id: param.id, }); if (!_.isEmpty(existsTask)) { throw new CoolCommException('已存在相同任务id~'); } param.createTime = new Date() param.updateTime = new Date() //======================为每个任务单独生成一张账号表,用于存储留存账号 this.createTableService.createAccountTable(param.id) // let taskAccount = { // name: "hide_game_account_" + param.id, // columns: [ // { // name: "id", // type: "int", // isPrimary: true, // length: "11" // }, // { // name: "account_text", // type: "varchar", // length: "50", // comment: "账号" // }, // { // name: "password_text", // type: "varchar", // length: "50", // comment: "密码" // }, // { // name: "new_date", // type: "date", // comment: "日期" // }, // { // name: "vpn_text", // type: "varchar", // length: "50", // comment: "vpn域" // }, { // name: "manufacturer", // type: "varchar", // length: "50", // comment: "厂商" // } // ] // } // const queryRunner = await getConnection().createQueryRunner(); // await queryRunner.createTable(new Table(taskAccount), true); // await queryRunner.release(); //====================== // console.log(param) // 创建游戏目标start========================== const existsTarget = await this.HideGameTargetEntity.findOne({ task_id: param.id, new_date: param.new_date }); let target_new = 0 let target_pay = 0 let target_active = 0 target_new = Math.floor(Math.random() * (parseInt(param.target_new_high) - parseInt(param.target_new_low) + 1) + parseInt(param.target_new_low)) target_pay = Math.floor(Math.random() * (parseInt(param.target_pay_high) - parseInt(param.target_pay_low) + 1) + parseInt(param.target_pay_low)) target_active = Math.floor(Math.random() * (parseInt(param.target_active_high) - parseInt(param.target_active_low) + 1) + parseInt(param.target_active_low)) // if (!_.isEmpty(existsTarget)) { // 当天的任务目标已存在,执行更新目标 let targetValue = { task_name: param.task_name, updateTime: param.updateTime, target_new: target_new, target_pay: target_pay, target_active: target_active, target_new_low: param.target_new_low, target_pay_low: param.target_pay_low, target_active_low: param.target_active_low } // this.logger.info(targetValue) await this.HideGameTargetEntity .createQueryBuilder() .update() .set(targetValue) .where('task_id = :task_id and new_date = :new_date', { task_id: param.id, new_date: param.new_date }) .execute(); // this.logger.info(updateResult) //UpdateResult { generatedMaps: [], raw: [], affected: 1 } // 执行更新目标 } else { // 添加任务目标 let targetValue = { task_id: param.id, task_name: param.task_name, directorId: param.directorId, new_date: param.new_date, createTime: param.createTime, updateTime: param.updateTime, target_new: target_new, target_pay: target_pay, target_active: target_active, target_new_low: param.target_new_low, target_pay_low: param.target_pay_low, target_active_low: param.target_active_low } // this.logger.info(targetValue) await this.HideGameTargetEntity.insert(targetValue) } // 创建游戏目标end========================== // 创建游戏完成game_complete=============================== const existsComplete = await this.HideGameCompleteEntity.findOne({ task_id: param.id, new_date: param.new_date }) if (_.isEmpty(existsComplete)) { // 添加任务完成game_complete let completeObj = { task_id: param.id, task_name: param.task_name, new_date: param.new_date, createTime: param.createTime, updateTime: param.updateTime, complete_new: 0, complete_pay: 0, complete_active: 0 } // this.logger.info(completeObj) await this.HideGameCompleteEntity.insert(completeObj) } // 创建游戏目标end========================== // 记录端口对应的游戏数量start await this.HideGameAgentService.recordGameNum(param.game_agentId) // this.logger.info(returnMsg) // 记录端口对应的游戏数量end await this.updateTaskGame(param); return super.add(param); } /** * 重写分页查询 * @param query */ async page(query) { const { id, task_name, is_run, login_modeId, directorId, game_agentId, account_typeId } = query; const sql = ` SELECT 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, 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, a.remark,a.is_run,a.is_repeat,a.download,a.unit_price_low,a.unit_price_high, any_value (b.name) AS directorName,b.id AS directorId, any_value (c.name) AS login_modeId, any_value (d.name) AS game_agentId, any_value (e.name) AS account_typeId, GROUP_CONCAT(g.game_name) AS gameList FROM hide_game_task a LEFT JOIN hide_game_director b ON a.directorId = b.id LEFT JOIN hide_lo_mode c ON a.login_modeId = c.id LEFT JOIN hide_game_agent d ON a.game_agentId = d.id LEFT JOIN hide_account_type e ON a.account_typeId = e.id LEFT JOIN hide_task_game_record t ON a.id = t.task_id LEFT JOIN hide_game_list g ON t.game_id = g.id WHERE 1 = 1 ${this.setSql(id, 'and a.id = ?', [id])} ${this.setSql(task_name, 'and a.task_name LIKE ?', [`%${task_name}%`])} ${this.setSql(is_run, 'and a.is_run = ?', [is_run])} ${this.setSql(directorId, 'and b.id = ?', [directorId])} ${this.setSql(game_agentId, 'and d.id = ?', [game_agentId])} ${this.setSql(account_typeId, 'and e.id = ?', [account_typeId])} ${this.setSql(login_modeId, 'and c.id = ?', [login_modeId])} GROUP BY a.id `; return this.sqlRenderPage(sql, query); } /** * 根据ID获得信息 * @param id */ public async info(id) { const info = await this.HideGameTaskEntity.findOne({ id }); const gameIds = await this.nativeQuery( 'select a.game_id from hide_task_game_record a where a.task_id = ?', [id] ); if (info) { // delete info.password; if (gameIds) { info.gameIdList = gameIds.map(e => { return parseInt(e.game_id); }); } } return info; } /** * 修改 * @param param 数据 */ async update(param) { // console.log(param) const new_game_agentId = param.game_agentId const new_login_modeId = param.login_modeId const new_directorId = param.directorId const new_task_name = param.task_name const taskInfo = await this.HideGameTaskEntity.findOne({ id: param.id }); if (!taskInfo) { throw new CoolCommException('任务不存在'); } const old_game_agentId = taskInfo.game_agentId const old_login_modeId = taskInfo.login_modeId const old_directorId = taskInfo.directorId const old_task_name = taskInfo.task_name await this.HideGameTaskEntity.save(param); await this.updateTaskGame(param); //检测负责人-厂商-端口,如有变化,则更新对应数量 if (new_game_agentId != old_game_agentId) { await this.HideGameAgentService.recordGameNum(new_game_agentId) await this.HideGameAgentService.reduceGameNum(old_game_agentId) } if (new_login_modeId != old_login_modeId) { await this.HideLoModeService.recordGameNum(new_game_agentId) await this.HideLoModeService.reduceGameNum(old_game_agentId) } if (new_directorId != old_directorId) { await this.HideGameDirectorService.addGameNumById(new_game_agentId) await this.HideGameDirectorService.reduceGameNumById(old_game_agentId) } //修改当天任务表里的任务名称(当名称变化时) if (old_task_name != new_task_name) { const existsTarget = await this.HideGameTargetEntity.findOne({ task_id: param.id, new_date: this.utils.formatDate(new Date()) }); if (!_.isEmpty(existsTarget)) { // 当天的任务目标已存在,执行更新目标 let targetValue = { task_name: new_task_name } // this.logger.info(targetValue) await this.HideGameTargetEntity .createQueryBuilder() .update() .set(targetValue) .where('task_id = :task_id and new_date = :new_date', { task_id: param.id, new_date: this.utils.formatDate(new Date()) }) .execute(); // this.logger.info(updateResult) //UpdateResult { generatedMaps: [], raw: [], affected: 1 } } } } /** * 更新任务-游戏关系 * @param taskGame */ async updateTaskGame(taskGame) { await this.HideTaskGameRecordEntity.delete({ task_id: taskGame.id }); if (taskGame.gameIdList) { for (const game_id of taskGame.gameIdList) { await this.HideTaskGameRecordEntity.save({ task_id: taskGame.id, game_id }); } } } /** * 删除 * @param ids */ async delete(ids) { let idArr; console.log(ids) if (ids instanceof Array) { idArr = ids; } else { idArr = ids.split(','); } for (const id of idArr) { //删除游戏时判断游戏是否在数优中,如在,则对应负责人,端口,厂商所属游戏数量-1 const taskInfo = await this.HideGameTaskEntity.findOne({ id: id }); if (taskInfo) { // console.log(taskInfo) if (taskInfo.is_run == 1) { await this.HideGameAgentService.reduceGameNum(taskInfo.game_agentId) await this.HideLoModeService.reduceGameNum(taskInfo.login_modeId) await this.HideGameDirectorService.reduceGameNumById(taskInfo.directorId) } } //=================================================================================== await this.HideTaskGameRecordEntity.delete({ task_id: id }); await this.HideGameTaskEntity.delete({ id: id }); } } /** * 根据id删除任务 * @param id */ async deleteById(id) { //=================================================================================== // console.log(id) // console.log(this.utils.formatDate(new Date())) await this.HideTaskGameRecordEntity.delete({ task_id: id }); await this.HideGameTaskEntity.delete({ id: id }); await this.HideGameTargetEntity.delete({ task_id: id, new_date: this.utils.formatDate(new Date()) }) // await this.HideGameCompleteEntity.delete({ task_id: id, new_date: this.utils.formatDate(new Date()) }) return 1000 } // /** * * * @param {string} task_id * @param {Date} stopTime * @param {string} stopRemark * @return {*} * @memberof HideGameTaskService */ async stopTask(task_id: string, stopTime: Date, stopRemark: string) { // const { id_name } = query; let obj = { id: task_id } const HideGameTaskEntity = await this.HideGameTaskEntity.findOne(obj); if (HideGameTaskEntity) { if (new Date(stopTime).getTime() > new Date().getTime()) { HideGameTaskEntity.is_run = 1; } else { HideGameTaskEntity.is_run = 0; } HideGameTaskEntity.stopTime = stopTime; HideGameTaskEntity.stopRemark = stopRemark; // this.logger.info(HideGameTaskEntity) await this.HideGameTaskEntity.save(HideGameTaskEntity); return 1000 } else { return -1 } } /** * * * @param {string} task_id * @return {*} * @memberof HideGameTaskService */ async startTask(task_id: string) { // const { id_name } = query; let obj = { id: task_id } const HideGameTaskEntity = await this.HideGameTaskEntity.findOne(obj); if (HideGameTaskEntity) { HideGameTaskEntity.is_run = 1; HideGameTaskEntity.new_date = new Date(this.utils.formatDate(new Date())); HideGameTaskEntity.stopTime = new Date('2099-01-01'); HideGameTaskEntity.stopRemark = ''; // this.logger.info(HideGameTaskEntity) await this.HideGameTaskEntity.save(HideGameTaskEntity); return 1000 } else { return -1 } } /** * 获取新增账号 * @param {*} task_id * @param {*} account_typeId * @return {*} * @memberof HideGameTaskService */ async getAccountOne(task_id, account_typeId, account_state) { const sql = ` SELECT A.id AS id, a.account_text, a.password_text, a.vpn_text, a.manufacturer, a.model, a.pnumber, a.imei, a.imsi, a.simserial, a.androidid, a.mac FROM hide_game_account AS A WHERE 1 = 1 ${this.setSql(account_typeId, 'and type_id = ?', [account_typeId])} AND occupy_taskId = 0 ${this.setSql(account_state, 'AND STATUS = ?', [account_state])} AND ( SELECT count( 1 ) AS num FROM hide_task_account_record AS T WHERE A.id = T.account_id ${this.setSql(task_id, 'AND T.task_id = ?', [task_id])} )= 0 AND ( SELECT count( 1 ) AS num FROM game_account_record AS G WHERE G.game_id IN ( SELECT game_id FROM hide_task_game_record r WHERE ${this.setSql(task_id, 'r.task_id = ?', [task_id])} ) AND A.id = G.account_id )= 0 ORDER BY A.id LIMIT 1 `; return this.nativeQuery(sql); } /** * 获取留存账号 * @param {*} task_id * @param {*} retained_day * @return {*} * @memberof HideGameTaskService */ async getAccountRetainedOne(task_id, retained_date) { const sql = ` SELECT A.id AS id, b.account_text AS account_text, b.password_text AS password_text, b.vpn_text AS vpn_text, b.manufacturer AS manufacturer, b.model AS model, b.pnumber AS pnumber, b.imei AS imei, b.imsi AS imsi, b.simserial AS simserial, b.androidid AS androidid, b.mac AS mac FROM hide_task_account_record AS a LEFT JOIN hide_game_account b ON a.account_id = b.id WHERE a.statusRetained = 0 AND a.ok_new = 1 ${this.setSql(task_id, 'AND a.task_id = ?', [task_id])} ${this.setSql(retained_date, 'AND a.new_date = ?', [retained_date])} ORDER BY A.id LIMIT 1 `; return this.nativeQuery(sql); } }