var system = require("../../../system")
const CtlBase = require("../../ctlms.base");
var moment = require('moment');

class InvoiceCtl extends CtlBase {
  constructor() {
    super();
    this.invoiceSve = system.getObject("service.invoice.invoiceSve");
    this.userSve = system.getObject("service.uc.userSve");
    this.INVOICE_TYPE = ["10", "20", "30"];
  }

  /**
   * 业务进度字典
   * @param {*} pobj
   */
  async processDics(pobj, pobj2, req) {
    try {
      return await this.invoiceSve.processDics(pobj);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, `接口错误 错误信息 ${error}`);
    }
  }

  /**
   * 发票申请列表-平台(管理员)
   * @param {*} pobj
   */
  async invoicePage(pobj, pobj2, req) {
    try {
      this.doTimeCondition(pobj, ["createTimeBegin", "createTimeEnd"]);
      return await this.invoiceSve.invoicePage(pobj);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, `接口错误 错误信息 ${error}`);
    }
  }

  /**
   * 发票申请列表-业务员
   * @param {*} pobj
   */
  async myInvoicePage(pobj, pobj2, req) {
    try {
      pobj.bd_path = req.loginUser.orgpath;
      this.doTimeCondition(pobj, ["createTimeBegin", "createTimeEnd"]);
      return await this.invoiceSve.invoicePage(pobj);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, `接口错误 错误信息 ${error}`);
    }
  }

  /**
   * 发票申请
   * @param {*} pobj
   */
  async invoiceApply(pobj, pobj2, req) {
    try {
      pobj.invoice_amount = system.y2f(pobj.invoice_amount);
      pobj.additional_tax = system.y2f(pobj.additional_tax);
      pobj.value_added_tax = system.y2f(pobj.value_added_tax);
      pobj.personal_invoice_tax = system.y2f(pobj.personal_invoice_tax);
      pobj.invoice_content = system.y2f(pobj.invoice_content);

      return await this.invoiceSve.invoiceApply(pobj);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, `接口错误 错误信息 ${error}`);
    }
  }

  /**
   * 进度处理
   * @param {*} pobj
   */
  async handleStatus(pobj, pobj2, req) {
    try {

      if (pobj.status == "1010") { //平台分配业务员
        //1.获取业务员的基本id和bd_path
        let _user = await this.userSve.queryById({
          id: pobj.bd_id
        });
        if (!_user || _user.status != 0) {
          return system.getResult(null, `业务员不存在`);
        }
        if (!_user.data || !_user.data.orgpath) {
          return system.getResult(null, `业务员信息错误 请联系管理员`);
        }
        pobj.bd_path = _user.data.orgpath;
      } else if (pobj.status == "1030") { //交付商分配
        pobj.deliver_delive = system.y2f(pobj.deliver_delive);
      }
      let res = await this.invoiceSve.handleStatus(pobj);

      return res;
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, `接口错误 错误信息 ${error}`);
    }
  }

  /**
   * 根据ID查详细信息
   * @param {*} pobj
   */
  async invoice(pobj, pobj2, req) {
    try {
      return await this.invoiceSve.invoice(pobj);
    } catch (error) {
      console.log(error);
      return system.getResultFail(500, `接口错误 错误信息 ${error}`);
    }
  }

  /**
   * 发票推送
   * @param {*} pobj
   * @param {*} pobj2
   * @param {*} req
   */
  async uploadDetail(pobj, pobj2, req) {
    if (!pobj.invoice_id) {
      return system.getResult(null, `参数错误 发票ID 不能为空`);
    }
    let res = await this.invoiceSve.uploadDetail(pobj.invoice_id);
    return res;
  }


  /**
   * 发票试算接口
   * @param {*} pobj
   * @param {*} query
   * @param {*} req
   */
  async calcInvoice(pobj, query, req) {
    let res = [],
      params = {};
    for (let item of pobj) {
      if (!item.credit_code) {
        return system.getResult(null, `参数错误 统一社会信用代码不能为空`);
      }
      if (!item.invoiced_time) {
        return system.getResult(null, `参数错误 发票申请时间不能为空`);
      }
      if (this.INVOICE_TYPE.indexOf(item.invoice_type) == -1) {
        return system.getResult(null, `参数错误 发票类型错误`);
      }
      let params = {
        "credit_code": item.credit_code,
        "invoiced_time": moment(item.invoiced_time).format("YYYY-MM-DD hh:mm:ss"),
        "invoice_type": item.invoice_type,
        "invoice_amount": item.invoice_amount
      }
      try {
        let rs = await this.invoiceSve.calcInvoice(params);
        if (rs.status !== 0) {
          res.push({
            credit_code: item.credit_code,
            error: 1,
            msg: rs.msg
          });
          continue;
        }
        let data = rs.data;
        res.push(data);
      } catch (error) {
        console.log(error);
        res.push({
          credit_code: item.credit_code,
          error: 1,
          msg: "试算异常，" + error.message
        });
      }
    }
    return system.getResult(res);
  }

  /**
   * 平台推送发票
   * @param {*} params 
   */
  async pushInvoiceToDeliver(params){
    return this.invoiceSve.pushInvoiceToDeliver(params);
  }

}

module.exports = InvoiceCtl;