const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
class ProductService extends ServiceBase {
  constructor() {
    super("aggregation", ServiceBase.getDaoName(ProductService));
    this.producttypeDao = system.getObject('db.aggregation.producttypeDao')
  }
  async create(pobj) {
    let code = await this.getBusUid("p");
    pobj.code = code;
    delete pobj["id"];
    return system.getResultSuccess(this.dao.create(pobj));
  }


  async update(pobj) {
    var functionName1 = "productTypeFirst";
    await this.redisdel.redisInfo(functionName1);
    var functionName2 = "productTypeSecend";
    await this.redisdel.redisInfo(functionName2);
    var functionName3 = "productList";
    await this.redisdel.redisInfo(functionName3);
    var functionName4 = "productDetail";
    await this.redisdel.redisInfo(functionName4);
    return system.getResultSuccess(this.dao.update(pobj));
  }

  /**
   * 获取某一类下的产品
   * @create rxs
   * @param pobj
   * @returns {Promise<void>}
   */
  async getProductsByType(pobj) {
    const type = await this.producttypeDao.findOne({ code: pobj.typeName }, []);
    if (!type) {
      return system.getResultFail(-1, '获取产品类型数据失败');
    }
    let products = await this.dao.model.findAll({
      attributes: ["code", "name"],
      where: { product_type_id: type.id, is_enabled: 1 }, raw: true
    });
    return system.getResult(products)
  }
}
module.exports = ProductService;