|
@@ -0,0 +1,90 @@
|
|
|
|
|
+/*
|
|
|
|
|
+ * @Author: 倚楼听风雨 18408246387@163.com
|
|
|
|
|
+ * @Date: 2022-09-22 14:50:54
|
|
|
|
|
+ * @LastEditors: 倚楼听风雨 18408246387@163.com
|
|
|
|
|
+ * @LastEditTime: 2022-09-22 18:01:13
|
|
|
|
|
+ * @FilePath: \cool-admin-3.x\cool-admin-midway-master\src\app\modules\task\service\rent_computer.ts
|
|
|
|
|
+ * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
|
|
|
|
+ */
|
|
|
|
|
+import { Inject, Logger, Provide } from '@midwayjs/decorator';
|
|
|
|
|
+import { BaseService } from 'midwayjs-cool-core';
|
|
|
|
|
+import { ILogger } from '@midwayjs/logger';
|
|
|
|
|
+import { Utils } from '../../../comm/utils';
|
|
|
|
|
+import * as _ from 'lodash';
|
|
|
|
|
+import { InjectEntityModel } from '@midwayjs/orm';
|
|
|
|
|
+import { Repository } from 'typeorm';
|
|
|
|
|
+// import { ShuyouUtilsService } from '../../shuyou/service/utils';
|
|
|
|
|
+import { ShuyouRentComputerEntity } from '../../shuyou/entity/rent_computer';
|
|
|
|
|
+import { ShuyouRentComputerService } from '../../shuyou/service/rent_computer';
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 租机定时任务
|
|
|
|
|
+ */
|
|
|
|
|
+@Provide()
|
|
|
|
|
+export class RentComputerTaskService extends BaseService {
|
|
|
|
|
+ @Logger()
|
|
|
|
|
+ logger: ILogger;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject()
|
|
|
|
|
+ utils: Utils;
|
|
|
|
|
+
|
|
|
|
|
+ @InjectEntityModel(ShuyouRentComputerEntity)
|
|
|
|
|
+ ShuyouRentComputerEntity: Repository<ShuyouRentComputerEntity>;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject()
|
|
|
|
|
+ ShuyouRentComputerService: ShuyouRentComputerService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 检查是否有电脑到期,修改租机状态
|
|
|
|
|
+ * @memberof RentComputerTaskService
|
|
|
|
|
+ */
|
|
|
|
|
+ async checkIsExpire() {
|
|
|
|
|
+ let updateTime = new Date();
|
|
|
|
|
+ this.logger.info('checkOccupyOverTime>>>' + updateTime);
|
|
|
|
|
+ const sql = `
|
|
|
|
|
+ SELECT pc_num,rent_start,rent_end,is_expire FROM shuyou_rent_computer
|
|
|
|
|
+ `;
|
|
|
|
|
+ // console.log(sql)
|
|
|
|
|
+ const accList = await this.nativeQuery(sql);
|
|
|
|
|
+ const overtime = 24 * 60 * 60 * 1000;
|
|
|
|
|
+ let is_expire = 0;
|
|
|
|
|
+ await ykhForeach(accList, async (item, index) => {
|
|
|
|
|
+ if (Date.parse(item.rent_end) < updateTime.getTime()) {
|
|
|
|
|
+ // console.log('已经到期');
|
|
|
|
|
+ //已经到期
|
|
|
|
|
+ is_expire = 1;
|
|
|
|
|
+ } else if (Date.parse(item.rent_end) - updateTime.getTime() <= overtime) {
|
|
|
|
|
+ //明日到期
|
|
|
|
|
+ is_expire = 2;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //在租期中
|
|
|
|
|
+ is_expire = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ const ShuyouRentComputerEntity =
|
|
|
|
|
+ await this.ShuyouRentComputerEntity.findOne({ pc_num: item.pc_num });
|
|
|
|
|
+ if (ShuyouRentComputerEntity) {
|
|
|
|
|
+ ShuyouRentComputerEntity.is_expire = is_expire;
|
|
|
|
|
+ await this.ShuyouRentComputerEntity.save(ShuyouRentComputerEntity);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return 'checkIsExpire SUCCESS>>>' + updateTime;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+//==========================================================================================
|
|
|
|
|
+
|
|
|
|
|
+// foreach加上async/await
|
|
|
|
|
+const ykhForeach = async (arr, callback) => {
|
|
|
|
|
+ const length = arr.length;
|
|
|
|
|
+ const O = Object(arr);
|
|
|
|
|
+ let k = 0;
|
|
|
|
|
+ while (k < length) {
|
|
|
|
|
+ if (k in O) {
|
|
|
|
|
+ // console.log('doing foreach...');
|
|
|
|
|
+ const kValue = O[k];
|
|
|
|
|
+ await callback(kValue, k, O);
|
|
|
|
|
+ }
|
|
|
|
|
+ k++;
|
|
|
|
|
+ }
|
|
|
|
|
+};
|