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 getImgList(pobj) {
    let res = await this.dao.findAndCountAll(pobj);
    return system.getResultSuccess(res);
  }
  async createImginfo(pobj) {
    let code = await this.getBusUid("img");
    pobj.imginfo.code = code;
    if (pobj.imginfo.company_id === undefined) {
      pobj.imginfo.company_id = 10;
    }
    let res = await this.dao.create(pobj.imginfo);
    return system.getResultSuccess(res);
  }
  async create(pobj) {
    let code = await this.getBusUid("img");
    pobj.code = code;
    if (!pobj.pic_url) {
      return system.getResultFail(-123, "图片不能为空");
    }
    return system.getResultSuccess(this.dao.create(pobj));
  }


  async update(pobj) {
    let whereParams = {
      id: pobj.id
    }
    let res = await this.dao.findOne(whereParams,[]);
    
    if(!pobj.id){
      return system.getResultFail(-124, "未知图片信息");
    }
    if(!pobj.pic_url){
      pobj.pic_url = res.pic_url;
    }
    if(!pobj.pic_size){
      pobj.pic_size = res.pic_size;
    }
    if(!pobj.name){
      pobj.name = res.name;
    }
    return system.getResultSuccess(this.dao.update(pobj));
  }

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