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 { ShuyouReasonIncompleteEntity } from '../entity/reason_incomplete';
import { Utils } from '../../../comm/utils';

/**
 * 游戏目标
 */
@Provide()
export class ShuyouReasonIncompleteService extends BaseService {

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

  @Inject()
  utils: Utils;

  /**
   * 重载新增接口
   */
  async add(param) {
    // console.log(param)
    const exists = await this.ShuyouReasonIncompleteEntity.findOne({
      task_id: param.task_id,
      new_date: param.new_date
    });
    param.createTime = new Date()
    param.updateTime = new Date()
    if (!_.isEmpty(exists)) {
      if (typeof (param.incomplete_new) == "undefined") {
        param.incomplete_new = exists.incomplete_new
      }
      if (typeof (param.incomplete_pay) == "undefined") {
        param.incomplete_pay = exists.incomplete_pay
      }
      if (typeof (param.incomplete_active) == "undefined") {
        param.incomplete_active = exists.incomplete_active
      }
      let addObj = {
        incomplete_new: param.incomplete_new,
        incomplete_pay: param.incomplete_pay,
        incomplete_active: param.incomplete_active,
        type_id: param.type_id,
        reason: param.reason,
        solution: param.solution,
        is_write: param.is_write
      }
      // throw new CoolCommException('该任务原因已添加过，你可以点击修改~');
      const updateResult = await this.ShuyouReasonIncompleteEntity
        .createQueryBuilder()
        .update()
        .set(addObj)
        .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) {
        return { code: 1, message: '修改成功!' }
      } else {
        return { code: -1, message: '修改失败!' }
      }
    } else {
      // console.log(param)
      await super.add(param);
      return { code: 1, message: '添加成功!' }
    }


  }
  /**
     * 重载修改接口
     */
  async update(param) {
    // console.log(param)
    param.is_write = 1
    const exists = await this.ShuyouReasonIncompleteEntity.findOne({
      task_id: param.task_id,
      new_date: param.new_date
    });
    param.createTime = new Date()
    param.updateTime = new Date()
    if (!_.isEmpty(exists)) {
      if (typeof (param.incomplete_new) == "undefined") {
        param.incomplete_new = exists.incomplete_new
      }
      if (typeof (param.incomplete_pay) == "undefined") {
        param.incomplete_pay = exists.incomplete_pay
      }
      if (typeof (param.incomplete_active) == "undefined") {
        param.incomplete_active = exists.incomplete_active
      }
      let addObj = {
        incomplete_new: param.incomplete_new,
        incomplete_pay: param.incomplete_pay,
        incomplete_active: param.incomplete_active,
        type_id: param.type_id,
        reason: param.reason,
        solution: param.solution,
        is_write: param.is_write
      }
      const updateResult = await this.ShuyouReasonIncompleteEntity
        .createQueryBuilder()
        .update()
        .set(addObj)
        .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 {
      throw new CoolCommException('未查询到数据，无法修改~');
    }


  }
  /**
  * 重写分页查询
  * @param query
  */
  async page(query) {
    const { directorName, new_date, task_id, task_name } = query;
    const sql = `
          SELECT
            a.id,a.createTime,a.updateTime,a.task_id,a.task_name,a.directorName,a.new_date,a.reason,a.solution,a.is_write,
            a.incomplete_new,a.incomplete_pay,a.incomplete_active,
            b.name AS type_id
          FROM
          shuyou_reason_incomplete a
            LEFT JOIN shuyou_problem_type b ON a.type_id = b.id
          WHERE 1 = 1
            ${this.setSql(task_id, 'and a.task_id = ?', [task_id])}
            ${this.setSql(new_date, 'and a.new_date >= ? and a.new_date <= ?', [this.utils.formatDate(new Date(new_date[0])), this.utils.formatDate(new Date(new_date[1]))])}
            ${this.setSql(task_name, 'and a.task_name LIKE ?', [`%${task_name}%`])}
            ${this.setSql(directorName, 'and a.directorName LIKE ?', [`%${directorName}%`])}
        `;
    // console.log(sql)
    return this.sqlRenderPage(sql, query);
  }


}
