const system = require("../../../system");
const ServiceBase = require("../../sve.base")
/**
 * 个体户
 */
class ObusinessmenService extends ServiceBase {
  constructor() {
    super("order", ServiceBase.getDaoName(ObusinessmenService));
  }

  /**
   * 根据ID查记录
   * @param {*} params 
   * @Id 个体户ID
   */
  async queryObusinessmen(params) {
    try {
      let obj = await this.findById(this.trim(params.id));
      return system.getResult(obj);
    } catch (error) {
      console.log(error);
      return system.getResult(`系统错误 错误信息 ${error}`);
    }
  }

  /**
   * 签约
   */
  async signing(params) {
    if (!params.hasOwnProperty('credit_code') && !params.credit_code) {
      return system.getResult(null, `参数错误 同意社会信用代码不能为空`);
    }
    if (!params.hasOwnProperty('cost_rate')) {
      return system.getResult(null, `参数错误 核定成本费用率不能为空`);
    }
    if (!params.hasOwnProperty('add_value_up_type') && !params.add_value_up_type) {
      return system.getResult(null, `参数错误 增值税累计类型不能为空`);
    }
    if (!params.hasOwnProperty('tax_up_type')) {
      return system.getResult(null, `参数错误 个税累计类型不能为空`);
    }
    if (!params.hasOwnProperty('service_begin_time')) {
      return system.getResult(null, `参数错误 服务开始时间不能为空`);
    }
    if (!params.hasOwnProperty('service_end_time')) {
      return system.getResult(null, `参数错误 服务结束时间不能为空`);
    }
    if (!params.hasOwnProperty('service_rate') && !params.service_rate) {
      return system.getResult(null, `参数错误 服务费比例不能为空`);
    }
    if (!params.hasOwnProperty('tax_rate') && !params.tax_rate) {
      return system.getResult(null, `参数错误 含税价百分比不能为空`);
    }

    let _obusinessmen = await this.dao.model.findOne({where:{
      credit_code:this.trim(params.credit_code)
    }});
    if(!_obusinessmen){
      return system.getResult(null,`个体户不存在`);
    }
    _obusinessmen.cost_rate = this.trim(params.cost_rate);
    _obusinessmen.add_value_up_type = this.trim(params.add_value_up_type);
    _obusinessmen.tax_up_type = this.trim(params.tax_up_type);
    _obusinessmen.service_begin_time = this.trim(params.service_begin_time);
    _obusinessmen.service_end_time = this.trim(params.service_end_time);
    _obusinessmen.service_rate = this.trim(params.service_rate);
    _obusinessmen.tax_rate = this.trim(params.tax_rate);
    _obusinessmen.sign_time = new Date();
    _obusinessmen.sign_notes = this.trim(params.sign_notes);
    try {
      await _obusinessmen.save();
      return system.getResultSuccess();
    } catch (error) {
      console.log(error);
      return system.getResult(null,`系统错误 错误信息${error}`);
    }
  }

  /**
   * 建账
   * @param {*} params 
   */
  async createAccount(params){
    if (!params.hasOwnProperty('credit_code') && !params.credit_code) {
      return system.getResult(null, `参数错误 同意社会信用代码不能为空`);
    }
    let _obusinessmen = await this.dao.model.findOne({where:{
      credit_code:this.trim(params.credit_code)
    }});
    if(!_obusinessmen){
      return system.getResult(null,`个体户不存在`);
    }
    _obusinessmen.is_create_account = true;
    try {
      await _obusinessmen.save();
      return system.getResultSuccess();
    } catch (error) {
      console.log(error);
      system.getResult(null,`系统错误 错误信息${error}`);
    }
  }


  async businessmenPage(params) {
    let currentPage = Number(params.currentPage || 1);
    let pageSize = Number(params.pageSize || 10);

    let order_id = this.trim(params.order_id);
    let name = this.trim(params.name);
    let bd_id = this.trim(params.bd_id);
    let bd_path = this.trim(params.bd_path);

    let legal_name = this.trim(params.legal_name);
    let legal_mobile = this.trim(params.legal_mobile);
    let legal_idcard = this.trim(params.legal_idcard);
    let credit_code = this.trim(params.credit_code);



    var where = {};

    var orderby = [
      ["id", 'desc']
    ];

    if (bdId) {
      where.merchant_id = bdId;
    }
    if (bdPath && bdPath.length > 0) {
      where.id = {
        [this.db.Op.like]: bdPath + "%"
      }
    }
    if (userId) {
      where.userId = userId;
    }
    if (ostatuses && ostatuses.length > 0) {
      where.ostatus = {
        [this.db.Op.in]: ostatuses
      }
    }

    this.addWhereTime(where, 'created_at', params.createdBegin, params.createdEnd);

    var page = await this.getPageList(currentPage, pageSize, where, orderby, null);
    if (page && page.rows) {
      for (var row of page.rows) {
        await this.dao.setRowCodeName(row, "status");
        await this.dao.setRowCodeName(row, "ostatus");
        await this.dao.setRowCodeName(row, "deliverType");

        this.handleDate(row, ["created_at", "assignTime"], null, -8);
      }
    }
    return system.getResultSuccess(page);
  }
}

module.exports = ObusinessmenService;