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 `i_invoice` t1");
    sql.push("WHERE 1 = 1 ");

    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) {
    var sql = [];
    sql.push("SELECT");
    sql.push("t1.*");
    sql.push("FROM `i_invoice` t1");
    sql.push("WHERE 1 = 1 ");

    this.setCondition(params, sql);

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

  setCondition(params, sql) {
    if(params.apply_no) {
      sql.push("AND t1.`apply_no` = :apply_no");
    }
    if(params.status) {
      sql.push("AND t1.`status` = :status");
    }
    if(params.statuses && params.statuses.length > 0){
      sql.push("AND t1.`status` in (:statuses)");
    }
    if(params.invoice_type) {
      sql.push("AND t1.`invoice_type` = :invoice_type");
    }
    if(params.createTimeBegin) {
      sql.push("AND t1.`created_at` >= :createTimeBegin");
    }
    if(params.createTimeEnd) {
      sql.push("AND t1.`created_at` <= :createTimeEnd");
    }

    if(params.red_status) {
      sql.push("AND t1.`red_status` = :red_status");
    }
    if(params.deliver_id) {
      sql.push("AND t1.`deliver_id` = :deliver_id");
    }
    if(params.bd_id) {
      sql.push("AND t1.`bd_id` = :bd_id");
    }
    if(params.bd_path) {
      params.bd_path_like = params.bd_path + "%";
      sql.push("AND t1.`bd_path` LIKE :bd_path_like");
    }
    if(params.invoice_number) {
      sql.push("AND t1.`invoice_number`=:invoice_number");
    }
    if(params.credit_code_array){
      sql.push("AND t1.`from_credit_code` in(:credit_code_array)");
    }
  }

  async findByChannelAndApplyNo(channel_id,apply_no){
    let sql =`select count(1) as total from i_invoice where channel_id = "${channel_id}" and apply_no = "${apply_no}"`;
    var counts = await this.customQuery(sql);
    return counts[0];
  }

  //计算附加税累计
  async accumulatedAddValueAmountByTime(businessmen_credit_code,beginTime,endTime){
    let sql =`select SUM(t1.additional_tax) AS additional_tax_total,SUM(t1.value_added_tax) AS value_added_tax_total,SUM(t1.invoice_amount) as invoice_amount_total from i_invoice t1 
    where t1.businessmen_credit_code = :businessmen_credit_code and t1.apply_time>= :beginTime and t1.apply_time <=:endTime`;
    let res= await this.customQuery(sql,{businessmen_credit_code:businessmen_credit_code,beginTime:beginTime,endTime:endTime});
    if(res.length==0){
      return {additional_tax_total:0,value_added_tax_total:0,invoice_amount_total:0};
    }
    return {additional_tax_total:res[0].additional_tax_total || 0 ,value_added_tax_total:res[0].value_added_tax_total || 0,invoice_amount_total:res[0].invoice_amount_total || 0};
  }


  /**
   * 计算个税累计
   * @param {*} beginTime 
   * @param {*} endTime 
   */
  async accumulatedTaxAmountByTime(businessmen_credit_code,beginTime,endTime){
    let sql =`select SUM(t1.personal_invoice_tax) AS personal_invoice_tax_total,SUM(t1.invoice_amount) as personal_invoice_amount_total  from i_invoice t1 
    where t1.businessmen_credit_code = :businessmen_credit_code and t1.apply_time>= :beginTime and t1.apply_time <= :endTime`;
    let res = await this.customQuery(sql,{businessmen_credit_code:businessmen_credit_code,beginTime:beginTime,endTime:endTime});
    if(!res || res.length == 0) {
      return  {personal_invoice_tax_total: 0,personal_invoice_amount_total: 0};
    }
    return {personal_invoice_tax_total:res[0].personal_invoice_tax_total || 0,personal_invoice_amount_total:res[0].personal_invoice_amount_total || 0};
  }

  async countByParamsBycreditCode(params){
    var sql = [];
    sql.push("SELECT count(1) as count");
    sql.push("FROM `saas_invoice` t1  INNER JOIN `saas_invoice_apply` t2 ON t1.id = t2.id ");
    sql.push("WHERE 1 = 1 AND t1.`from_credit_code` in(:credit_code_array)");
    if(params.from_name){
      sql.push("AND t1.`from_name` = :from_name");
    }
    if(params.status){
      sql.push("AND t2.`status` = :status")
    }
    if(params.statusArray){
      sql.push("AND t2.`status` in (:statusArray)")
    }
    let list =  await this.customQuery(sql.join(" "), params);
    if(list.length ==0){
      return {count: 0};
    }
    return list[0];
  }

  /**
   * 根据统一社会信用代码获取发票
   * @param params
   * @returns {Promise<*>}
   */
  async invoiceBycreditCode(params){
    var sql = [];
    sql.push("SELECT t1.from_name,t1.to_name,t1.to_credit_code,t1.to_addr,t1.to_mobile,t1.to_bank,t1.to_account,t1.invoice_time,t1.invoice_img, t1.invoice_amount,t2.created_at,t2.status  FROM `saas_invoice` t1 ");
    sql.push("INNER JOIN `saas_invoice_apply` t2 ON t1.id = t2.id ");
    sql.push("WHERE 1 = 1 AND t1.`from_credit_code` in(:credit_code_array)");
    if(params.from_name){
      sql.push("AND t1.`from_name` = :from_name");
    }
    if(params.status){
      sql.push("AND t2.`status` = :status")
    }
    if(params.statusArray){
      sql.push("AND t2.`status` in (:statusArray)")
    }
    sql.push("ORDER BY t1.invoice_time DESC");
    sql.push("LIMIT :startRow, :pageSize");
    return await this.customQuery(sql.join(" "), params);
  }
}
module.exports = IInvoiceDao;