const system=require("../../system");
const ServiceBase=require("../sve.base")
const settings=require("../../../config/settings")
class AttachmentService extends ServiceBase{
  constructor(){
     super(ServiceBase.getDaoName(AttachmentService));
     this.accountDao=system.getObject("db.accountDao");
     this.productDao=system.getObject("db.productDao");
     this.tradeDao=system.getObject("db.tradeDao");
  }

  async create(attachmentObj){
    var self=this;
    var productId=attachmentObj.productId;
    var product=await this.productDao.model.findOne({where:{id:productId}});
    var user=attachmentObj.user;
    var account =attachmentObj.account;
      return this.db.transaction(async function (t){
          var tradeObj={username:user.nickName,tradeDate:new Date(),status:"settled",baoAmount:product.price,tradeType:"consume"};
          var trade= await self.tradeDao.create2(user,account,tradeObj,t);
          return self.dao.create(trade,attachmentObj,t);
      });
  }
}
module.exports=AttachmentService;
