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

  async countByParams(params) {
    var sql = [];
    sql.push("SELECT");
    sql.push("COUNT(1) as total");
    sql.push("FROM `invoice` t1");
    sql.push("INNER JOIN `invoice_apply` t2 ON t1.`id` = t2.`id`");
    sql.push("WHERE 1 = 1 and t2.status in('1070','1080','1090')");

    this.setCondition(params, sql);

    var counts = await this.customQuery(sql.join(" "), params);
    if(!counts || counts.length == 0) {
      return 0;
    }
    return counts[0].total || 0;
  }

  async pageByParams(params, startRow, pageSize) {
    var sql = [];
    sql.push("SELECT");
    sql.push("t1.*");
    sql.push("FROM `invoice` t1");
    sql.push("INNER JOIN `invoice_apply` t2 ON t1.`id` = t2.`id`");
    sql.push("WHERE 1 = 1 ");

    this.setCondition(params, sql);

    sql.push("ORDER BY id DESC");
    sql.push("LIMIT :startRow, :pageSize");
    params.startRow = startRow || 0;
    params.pageSize = pageSize || 10; 
    return await this.customQuery(sql.join(" "), params);
  }

  setCondition(params, sql) {
    var complateTax = params.complateTax;
    if (complateTax != "-1") {
      params.complateTax = Number(complateTax);
      sql.push("AND t1.`complate_tax` = :complateTax");
    }

    // invoiceTimeBegin   2019-01-01  2019-01-01 00:00:00
    // invoiceTimeEnd     2019-11-01  2019-11-01 23:59:59
    if(params.invoiceTimeBegin) {
      sql.push("AND t1.`invoice_time` >= :invoiceTimeBegin");
    }
    if(params.invoiceTimeEnd) {
      sql.push("AND t1.`invoice_time` <= :invoiceTimeEnd");
    }
    if(params.invoiceTime){
      sql.push("AND t1.`invoice_time` >= :invoiceTime");
    }

    if(params.applyNo) {
      sql.push("AND t2.`apply_no` = :applyNo");
    }

    if(params.redStatus) {
      sql.push("AND t1.`red_status` = :redStatus");
    }

    if (params.type) {
      sql.push("AND t2.`type` = :type");
    }
    if(params.statuses && params.statuses.length > 0){
      sql.push("AND t2.`status` in (:statuses)");
    }
  }


  /**
   * 红冲列表总计
   * @param {*} params 
   */
  async countRedRushListByParams(params){
    var sql = [];

    sql.push(`select count(1) as count from invoice_apply a inner join invoice_deliverer b on a.deliverer_id =b.id 
    inner join invoice c on b.invoice_id =c.id where a.id = c.id  `);

    if (params.delivererId) {
      sql.push(` AND b.deliverer_id = :delivererId`);
    }
    if (params.applyNo) {
      sql.push(` AND a.apply_no = :applyNo`);
    }
    if (params.invoiceNo) {
      sql.push(` AND c.invoice_no = :invoiceNo`);
    }
    if (params.startTime) {
      sql.push(`and a.invoice_time >= :startTime`);
    }
    if (params.endTime) {
      sql.push(`and a.invoice_time <= :endTime`);
    }
    if(params.invoiceTime){
      sql.push(`and a.invoice_time >= :invoiceTime`);
    }
    if (params.type) {
      sql.push(`and a.type = :type`);
    }
    if (params.redStatus) {
      sql.push(`and c.red_status = :redStatus`);
    }
    if (params.status) {
      sql.push(`and c.status = :status`);
    }
    return await this.customQuery(sql.join(" "), params);
  }

  /**
   * 红冲列表
   * @param {*} params 
   */
  async redRushListByParams(params){
    var sql = [];
    sql.push(`select a.apply_no,a.type,a.invoice_amount,a.invoice_time,c.status,
    a.merchant_name,a.businessmen_name,a.id,b.deliverer_id from invoice_apply a 
    inner join invoice_deliverer b on a.deliverer_id =b.id 
    inner join invoice c on b.invoice_id =c.id where a.id = c.id  `);

    if (params.delivererId) {
      sql.push(` AND b.deliverer_id = :delivererId`);
    }
    if (params.applyNo) {
      sql.push(` AND a.apply_no = :applyNo`);
    }
    if (params.startTime) {
      sql.push(`and a.invoice_time >= :startTime`);
    }
    if (params.endTime) {
      sql.push(`and a.invoice_time <= :endTime`);
    }
    if(params.invoiceTime){
      sql.push(`and a.invoice_time >= :invoiceTime`);
    }
    if (params.type) {
      sql.push(`and a.type = :type`);
    }
    if (params.redStatus) {
      sql.push(`and c.red_status = :redStatus`);
    }
    sql.push("ORDER BY c.id DESC");
    sql.push("LIMIT :statRow, :pageSize");
    return await this.customQuery(sql.join(" "), params);
  }
}
module.exports = IInvoiceDao;