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

  /**
   * 条件查询所有符合条件的产品
   * @param {*} params 
   */
  async orderIdsByDeliverId(deliver_id){
    if(!deliver_id) {
      return [];
    }
    let sql = `SELECT order_id FROM o_order_deliver WHERE deliver_id = :deliver_id`;
    let list = await this.customQuery(sql, {deliver_id: deliver_id});
    let rs = [];
    if (!list) {
      return rs;
    }
    for (let item of list) {
      rs.push(item.order_id);
    }
    return rs;
  }
  /**
   * 条件查询所有符合条件的产品
   * @param {*} params
   */
  async orderIdsByOperatorId(operator_id){
    if(!operator_id) {
      return [];
    }
    let sql = `SELECT order_id FROM o_order_deliver WHERE operator_id = :operator_id`;
    let list = await this.customQuery(sql, {operator_id: operator_id});
    let rs = [];
    if (!list) {
      return rs;
    }
    for (let item of list) {
      rs.push(item.order_id);
    }
    return rs;
  }

  async listByIds(ids){
    if(!ids || ids.length == 0) {
      return [];
    }
    let sql =  `SELECT * FROM o_order_deliver WHERE id in (:deliverIdList)`;
    return await this.customQuery(sql, {deliverIdList: ids});
  }

  async mapByIds(params) {
    let list = await this.listByIds(params);
    var result = {};
    for(let item of list) {
      result[item.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,
    a.desc, a.service_remark,
    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;