const system = require("../../../system");
const applyDao = system.getObject("db.invoice.applyDao");
const invoiceDao = system.getObject("db.invoice.invoiceDao");
const delivererDao = system.getObject("db.invoice.delivererDao");
const common = require("./common");
//交付商关闭 1040
module.exports.delivererClose = async function (params) {
  console.log("交付商关闭 1040");
  let obj = await common.delivererVerification(params);
  if (obj.status == -1) { return obj; }
  let _apply = obj._apply;
  let _invoice = obj._invoice;
  //更改发票表信息
  _invoice.status = common.trim(params.nextStatus);
  //添加拒绝原因
  let delivererData = {
    breakReason: common.trim(params.breakReason) || "",
    id: _apply.delivererId
  };
  //更改申请表信息
  _apply.status = "1020";
  _apply.delivererId = null;
  let applyData = {};
  applyData.id = _apply.id;
  applyData.status = "1020";
  applyData.delivererId = '';

  let invoiceData = {};
  invoiceData.status = "1020";
  invoiceData.id=_apply.id;

  await delivererDao.db.transaction(async (t) => {
    //更新申请表状态
    await applyDao.update(applyData, t);
    //更新发票表状态
    await invoiceDao.update(invoiceData, t);
    //更新拒绝原因
    await delivererDao.update(delivererData, t);
  }).catch(error => {
    return system.getResult(null, `系统错误 错误信息 ${error}`);
  });
  return system.getResultSuccess();
}