/*
 * @Author: YKH
 * @Date: 2021-12-22 13:59:30
 * @LastEditTime: 2021-12-30 10:12:58
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\task_account_record.ts
 */
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { ShuyouTaskAccountRecordEntity } from '../entity/task_account_record';
import * as _ from 'lodash';
import { Utils } from '../../../comm/utils';
import { ILogger } from '@midwayjs/logger';

/**
 * 
 */
@Provide()
export class ShuyouTaskAccountRecordService extends BaseService {
  @InjectEntityModel(ShuyouTaskAccountRecordEntity)
  ShuyouTaskAccountRecordEntity: Repository<ShuyouTaskAccountRecordEntity>;

  @Inject()
  utils: Utils;

  @Inject()
  logger: ILogger;

  /**
   *
   * 修改TaskAccountRecord账号状态
   * @param {number} record_id
   * @param {number} status 0.新增留存成功 1.冻结 2.密错 3.禁用 4.新增出现留存 5.人脸验证
   * @return {*} 
   * @memberof ShuyouTaskAccountRecordService
   */
  async updateAccount(record_id: number, status: number) {
    // status 0.新增留存成功 1.冻结 2.密错 3.禁用 4.新增出现留存 5.人脸验证
    const exists = await this.ShuyouTaskAccountRecordEntity.findOne({
      id: record_id
    });
    if (exists) {
      const new_date = exists.new_date;
      const today = this.utils.formatDate(new Date());
      const dayDiff = this.utils.dateDiff(today, new_date)
      let account_id = ''
      if (dayDiff == 0) {
        this.logger.warn(this.utils.timeDiff(new Date(), exists.getTimeNew))
        if (this.utils.timeDiff(new Date(), exists.getTimeNew) > 7200) {
          return { code: 2, message: '修改账号超时，只能修改2小时内取的账号' }
        }
        if (status == 0) {
          exists.ok_new = 1;
          exists.successTimeNew = new Date();
        } else if (status == 4) {
          exists.ok_new = 2;
        } else if (status == 1) {
          exists.ok_new = 0;
          exists.statusNew = 1;
        } else if (status == 2) {
          exists.ok_new = 0;
          exists.statusNew = 2;
        } else if (status == 3) {
          exists.ok_new = 0;
          exists.statusNew = 3;
        } else if (status == 5) {
          exists.ok_new = 0;
          exists.statusNew = 5;
        }
        const aa = await this.ShuyouTaskAccountRecordEntity.save(exists);
        account_id = aa.account_id
      } else {
        if (this.utils.timeDiff(new Date(), exists.getTimeRetained) > 7200) {
          return { code: 2, message: '修改账号超时，只能修改2小时内取的账号' }
        }
        if (status == 0) {
          exists.ok_retained = dayDiff + 1;
          exists.successTimeRetained = new Date();
        } else if (status == 1) {
          exists.ok_retained = 0;
          exists.statusRetained = 1;
        } else if (status == 2) {
          exists.ok_retained = 0;
          exists.statusRetained = 2;
        } else if (status == 3) {
          exists.ok_retained = 0;
          exists.statusRetained = 3;
        } else if (status == 5) {
          exists.ok_retained = 0;
          exists.statusRetained = 5;
        }
        const bb = await this.ShuyouTaskAccountRecordEntity.save(exists);
        account_id = bb.account_id
      }
      return { code: 1, account_id: account_id }
    } else {
      return { code: -1 }
    }
  }



}
