const system=require("../../system");
const Dao=require("../dao.base");
class OrderDao extends Dao{
  constructor(){
    super(Dao.getModelName(OrderDao));
  }
  extraWhere(obj,w){
    console.log("---------------------------------------------------------------------------------zhangjiao");
    console.log(obj);
     if(obj.codepath && obj.codepath!=""){
       if(obj.codepath.indexOf("allorder")<0 && obj.codepath.indexOf("allcalctm")<0 && obj.codepath.indexOf("allcrorder")<0　&& obj.codepath.indexOf("myinvoiceapply")<0　){
         //如果是我的订单，查询条件是订单等于当前登录人的onlyCode
          w["shopOnlyCode"]=obj.onlyCode;
       }
      //  isSubProduct
       if(obj.codepath.indexOf("autosubmitorder")>0){
        w["isSubProduct"]=1;
       }
       if(obj.codepath.indexOf("myinvoiceapply")>0){
         w["onlyCode"]=obj.onlyCode;
         w["invoiceApply"]="notapply";
         w[this.db.Op.and]=[
               {[this.db.Op.or]:[{orderStatus:'4'},{orderStatus:'32'}]},
               {[this.db.Op.or]:[{orderType:'1'},{orderType:'2'},{orderType:'4'},{orderType:'16'}]},
             ];
       }
       if(obj.codepath.indexOf("myorderhookreference")>0){
         //合伙人自助交付，挂接时只能看到自己店铺的订单
         w["shopOnlyCode"]=obj.onlyCode;
       }
       if(obj.codepath.indexOf("allcrorder")>0 || obj.codepath.indexOf("copyrightorder")>0){
         w["itemType"]="ic";
         if(obj.codepath.indexOf("copyrightorder")>0  ){
            w["shopOnlyCode"]=obj.onlyCode;
         }
       }
       if(obj.codepath.indexOf("allpatentorder")>0 || obj.codepath.indexOf("patentorder")>0){
         w["itemType"]="pa";
         if(obj.codepath.indexOf("patentorder")>0  ){
            w["shopOnlyCode"]=obj.onlyCode;
         }
       }

     }
     return w;
  }
  extraModelFilter(){
    return {"key":"include","value":[{model:this.db.models.tmcustomerinfo}]};
  }
  async updatestatus(oid,status,t){
    var order= await this.model.findById(oid);
    order=order.dataValues;
    order.orderStatus=status;
    var result=await this.update(order,t);
    return result;
  }
  async getOrderStatus(oid){
    var order= await this.findById(oid);
    return order.orderStatus;
  }
  async findOneByOrderNum(orderNum){
    var app=  await this.model.findOne({where:{orderNum:orderNum}});
    return app;
  }
  async findByPayUserId(payUserId){
    return this.model.findAll({where:{payUserId:payUserId},order:[["created_at","DESC"]]});
  }
}
module.exports=OrderDao;
