/*
 * @Author: YKH
 * @Date: 2022-05-27 09:01:24
 * @LastEditTime: 2022-07-07 14:53:14
 * @Description: 
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\service\game_complete_hand.ts
 */
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from 'midwayjs-cool-core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { ShuyouGameCompleteHandEntity } from '../entity/game_complete_hand';
import { ILogger } from '@midwayjs/logger';
import { Utils } from '../../../comm/utils';
/**
 * 手动完成处理
 */
@Provide()
export class ShuyouGameCompleteHandService extends BaseService {

  @InjectEntityModel(ShuyouGameCompleteHandEntity)
  ShuyouGameCompleteHandEntity: Repository<ShuyouGameCompleteHandEntity>;

  @Inject()
  logger: ILogger;

  @Inject()
  utils: Utils;

  /**
  * 重载接口,修改或新增
  */
  async addOrUpdate(param) {
    // console.log(param)
    if (typeof (param.price_hand) == "undefined" || param.price_hand == 'null' || param.price_hand == null) {
      param.price_hand = 0
    }
    if (typeof (param.amount_hand) == "undefined" || param.amount_hand == 'null' || param.amount_hand == null) {
      param.amount_hand = 0
    }
    if (typeof (param.complete_pay_hand) == "undefined" || param.complete_pay_hand == 'null' || param.complete_pay_hand == null) {
      param.complete_pay_hand = 0
    }
    if (typeof (param.pay_mode_hand) == "undefined" || param.pay_mode_hand == 'null' || param.pay_mode_hand == null) {
      param.pay_mode_hand = 4
    }
    if (typeof (param.is_same_hand) == "undefined" || param.is_same_hand == 'null' || param.is_same_hand == null) {
      param.is_same_hand = 0
    }
    if (typeof (param.is_wt_hand) == "undefined" || param.is_wt_hand == 'null' || param.is_wt_hand == null) {
      param.is_wt_hand = 0
    }
    if (typeof (param.is_xjf_hand) == "undefined" || param.is_xjf_hand == 'null' || param.is_xjf_hand == null) {
      param.is_xjf_hand = 0
    }
    if (typeof (param.is_pay_hand) == "undefined" || param.is_pay_hand == 'null' || param.is_pay_hand == null) {
      param.is_pay_hand = 1
    }
    if (typeof (param.complete_new_hand) == "undefined" || param.complete_new_hand == 'null' || param.complete_new_hand == null) {
      param.complete_new_hand = 0
    }
    if (typeof (param.complete_lc_hand) == "undefined" || param.complete_lc_hand == 'null' || param.complete_lc_hand == null) {
      param.complete_lc_hand = 0
    }
    const exists = await this.ShuyouGameCompleteHandEntity.findOne({
      task_id: param.task_id,
      new_date: param.new_date
    });
    param.createTime = new Date()
    param.updateTime = new Date()
    if (param.is_same_hand == 0) {
      param.amount_hand = parseFloat(param.price_hand) * parseFloat(param.complete_pay_hand)
    }
    if (!_.isEmpty(exists)) {   //修改
      let object = {
        complete_new_hand: param.complete_new_hand,
        complete_lc_hand: param.complete_lc_hand,
        is_pay_hand: param.is_pay_hand,
        complete_pay_hand: param.complete_pay_hand,
        price_hand: param.price_hand,
        amount_hand: param.amount_hand,
        pay_mode_hand: param.pay_mode_hand,
        other_pay_hand: param.other_pay_hand,
        is_same_hand: param.is_same_hand,
        account_hand: param.account_hand,
        is_wt_hand: param.is_wt_hand,
        is_xjf_hand: param.is_xjf_hand
      }
      const updateResult = await this.ShuyouGameCompleteHandEntity
        .createQueryBuilder()
        .update()
        .set(object)
        .where('task_id = :task_id and new_date = :new_date', {
          task_id: param.task_id,
          new_date: param.new_date
        })
        .execute();
      if (updateResult.affected >= 1) {
        // throw new CoolCommException('修改成功~');
        // console.log('修改成功~')
      } else {
        // throw new CoolCommException('修改成功~');
        console.log('修改失败~')
      }
    } else {
      //新增
      let object = {
        task_id: param.task_id,
        new_date: param.new_date,
        complete_new_hand: param.complete_new_hand,
        complete_lc_hand: param.complete_lc_hand,
        is_pay_hand: param.is_pay_hand,
        complete_pay_hand: param.complete_pay_hand,
        price_hand: param.price_hand,
        amount_hand: param.amount_hand,
        pay_mode_hand: param.pay_mode_hand,
        other_pay_hand: param.other_pay_hand,
        is_same_hand: param.is_same_hand,
        account_hand: param.account_hand,
        is_wt_hand: param.is_wt_hand,
        is_xjf_hand: param.is_xjf_hand
      }
      await this.ShuyouGameCompleteHandEntity.insert(object)

    }
  }


}
