const system = require("../../../system");
const Dao = require("../../dao.base");
class OorderdeliverDao extends Dao {
  constructor() {
    super(Dao.getModelName(OorderdeliverDao));
  }

  /**
   * 条件查询所有符合条件的产品
   * @param {*} params 
   */
  async listByIds(ids){
    if(!ids || ids.length == 0) {
      return [];
    }
    let sql = [];
    sql.push(`SELECT * FROM o_order_deliver WHERE deliver_id in (:deliverIdList)`);
    
    return await this.customQuery(sql.join(" "), {deliverIdList: ids});
  }

  async mapByIds(params) {
    let list = await this.listByIds(params);
    var result = {};
    for(let item of list) {
      result[item.deliver_id] = item;
    }
    return result;
  }

    /**
   * 订单管理列表 分页总数查询
   * @param {*} params 
   */
  async ordersdeliverCount(params){
    let sql = [];
    sql.push(`SELECT COUNT(1) AS orderCount FROM o_order_deliver b INNER JOIN o_order a on
    b.order_id=a.id WHERE  b.deliver_id = '${params.deliver_id}' `);
    this.setOrderCount(sql,params);
    return await this.customQuery(sql.join(" "), params);
  }

  /**
   * 订单列表 分页
   * @param {*} params 
   */
  async findOrders(params){
    let sql = [];
    sql.push(`SELECT a.id,b.created_at,a.service_items,a.status,b.deliver_divide,b.operator_id,b.completed_at,
    b.deliver_name FROM o_order_deliver b INNER JOIN o_order a on
    b.order_id=a.id WHERE  b.deliver_id = '${params.deliver_id}' `);
    this.setOrderCount(sql,params);
    sql.push(` order by id desc `);
    sql.push(`limit ${params.startRow} ,${params.pageSize}`);
    return await this.customQuery(sql.join(" "), params);
  }

  /**
   * 订单管理列表查询条件参数设置
   * @param {*} params 
   */
  async setOrderCount(sql,params){
    if(params.id){
      sql.push(`AND b.id = :id `);
    }
    if(params.operator_id){
      sql.push(`AND b.operator_id = :operator_id `);
    }
    if(params.status){
      sql.push(`AND a.status = :status `);
    }
    if (params.createdBegin) {
      sql.push(`AND created_at >= :createdBegin `);
    }
    if (params.createdEnd) {
      sql.push(`AND created_at <= :createdEnd`);
    }

    
  }



}
module.exports = OorderdeliverDao;