const system = require("../../../system");
const applyDao = system.getObject("db.invoice.applyDao");
const invoiceDao = system.getObject("db.invoice.invoiceDao");
const is = system.getObject("util.invoiceStatus");
const invoiceStatus = is.status;
    
/**
 * 平台状态验证
 */
module.exports.applyVerification=async (params) =>{
 let _apply = await applyDao.findOne({
   id: trim(params.id)
 });
 if (!_apply) {
   return system.getResult(null, `此发票不存在`);
 }
 //获取当前状态 的对象
 let _status = invoiceStatus[_apply.status];
 //如果不符合状态则退出
 if (params.nextStatus == "1300" || params.nextStatus == "1040" || params.nextStatus == "1010") {
   return _apply;
 }
 if (!_status && _status.next != params.nextStatus) {
   let name = invoiceStatus[invoiceStatus[_apply.status].next].name;
   return system.getResult(null, `更新状态错误，提示：当前状态的下一个状态是 ${name}`);
 }
 return _apply;
}


/**
 * 交付商转该验证
 */
module.exports.delivererVerification=async (params)=> {
  let _apply = await applyDao.findOne({ id: params.id });
  let _invoice = await invoiceDao.findOne({ id: params.id });
  if (!_invoice) { return system.getResult(null, `此发票不存在`); }
  //获取当前状态 的对象
  let _status = invoiceStatus[_invoice.status];
  //如果不符合状态则退出
  if (params.nextStatus == "1300" || params.nextStatus == "1040" || params.nextStatus == "1010") {
    let obj = {
      _apply: _apply,
      _invoice: _invoice
    };
    return obj;
  } else if (!_status && _status.dstatus != params.nextStatus) {
    let name = invoiceStatus[invoiceStatus[_invoice.status].dstatus].name;
    return system.getResult(null, `更新状态错误，提示：当前状态的下一个状态是 ${name}`);
  }
  let obj = {
    _apply: _apply,
    _invoice: _invoice
  };
  return obj;
}

function trim(o) {
  if(!o) {
    return "";
  }
  return o.toString().trim();
}

module.exports.trim=(o)=>{
  if(!o) {
    return "";
  }
  return o.toString().trim();
}
