var system = require("../../../system");
const http = require("http");
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const moment = require("moment");
class BizOptCtl extends CtlBase {
  constructor() {
    super("bizchance", CtlBase.getServiceName(BizOptCtl));
  }
  async findAndCountAll(pobj, qobj, req) {
    //设置查询条件
    const rs = await this.service.findAndCountAll(pobj);
    if (rs.results && rs.results.rows) {
      let result = [];
      for (let val of rs.results.rows) {
        val.company_name = val.business_info.company;
        val.customer_number = val.business_info.contactsPhone;
        val.customer_name = val.business_info.contactsName;
        val.updated_at = moment(val.updated_at).format('YYYY-MM-DD HH:mm:ss');
        val.created_at = moment(val.created_at).format('YYYY-MM-DD HH:mm:ss');
        val.schemeStatus = val["scheme.scheme_status"];
        val.source_name = system.SOURCENAME[val.source_name] ? system.SOURCENAME[val.source_name] : val.source_name;
        result.push(val);
      }
      rs.results.rows = result;
    }
    return system.getResult(rs);
  }

  async findBizAndSheme(pobj, qobj, req) {
    if (!pobj.id) {
      return system.getResult(null, "id  can not be empty，100290");
    }
    const rs = await this.service.findBizAndSheme(pobj);
    return system.getResult(rs);
  }

  async closeBiz(pobj, qobj, req) {
    if (!pobj.bizId) {
      return system.getResult(null, "bizId  can not be empty，100290");
    }
    if (!pobj.close_reason) {
      return system.getResult(null, "close_reason  can not be empty，100290");
    }
    try {
      await this.service.closeBiz(pobj);
      return system.getResultSuccess();
    } catch (err) {
      return system.getResult(null, err.message);
    }

  }
}
module.exports = BizOptCtl;
