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");
//已邮寄 1080
module.exports.mailed=async (params)=>{
  console.log("已邮寄 1080");
  let obj = await common.delivererVerification(params);
  if(obj.status==-1){return obj;}
  let _apply = obj._apply;
  let invoiceData = {}, applyData = {}, delivererData = {};
  //更改发票表信息
  invoiceData.status = common.trim(params.nextStatus);
  invoiceData.mailNo = common.trim(params.mailNo);
  invoiceData.id = common.trim(params.id);
  //更改申请表信息
  applyData.status = common.trim(params.nextStatus);
  applyData.id = common.trim(params.id);
  //交付商
  let _deliverer = await delivererDao.findOne({ id: _apply.delivererId });
  delivererData.delivererMailNo = params.mailNo;
  delivererData.id = _deliverer.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();
}