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 businessmenPage(params) {
    let currentPage = Number(params.currentPage || 1);
    let pageSize = Number(params.pageSize || 10);

    let bd_id = this.trim(params.bd_id);
    let bd_path = this.trim(params.bd_path);
    let bdPath = this.trim(params.bdPath);
    let ostatuses = params.ostatuses;

    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;