const common = require("./common");
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");
//审核通过 1070
module.exports.auditSuccessForDeliverer=async function (params){
  console.log("审核通过 1070");
  let _apply =await common.applyVerification(params);
  if(_apply.status==-1){return _apply;}

  let delivererData = {},
    applyData = {},
    invoiceData = {};
  let _deliverer = await delivererDao.findOne({
    id: _apply.delivererId
  });

  delivererData.auditContent = common.trim(params.auditContent);
  delivererData.delivererContent = common.trim(params.delivererContent);
  delivererData.mailAddr = common.trim(params.mailAddr);
  delivererData.mailMobile = common.trim(params.mailMobile);
  delivererData.mailTo = common.trim(params.mailTo);
  delivererData.mailEmail = common.trim(params.mailEmail);
  delivererData.id = common.trim(_deliverer.id);
  applyData.status = common.trim(params.nextStatus);
  applyData.id = common.trim(params.id);
  invoiceData.status = common.trim(params.nextStatus);
  invoiceData.id = common.trim(params.id);

  await applyDao.db.transaction(async (t) => {
    //更新deliverer信息
    await delivererDao.update(delivererData, t);
    //更新 申请单和发票单
    await applyDao.update(applyData, t);
    await invoiceDao.update(invoiceData, t);
    if (_apply.parentId) {
      await invoiceDao.update({ id: _apply.parentId, redStatus: '4' }, t);
    }
  }).catch(error => {
    return system.getResult(null, `系统错误 错误信息 ${error}`);
  });
  return system.getResultSuccess();
}