const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
const appconfig = system.getSysConfig();
const fs = require("fs")
class ProductService extends ServiceBase {
  constructor() {
    super("product", ServiceBase.getDaoName(ProductService));
    this.priceDao = system.getObject("db.product.productpriceDao")
  }
  async create(p) {
    if (!p.name || p.name == "") {
      p.name = p.regionpath + "~" + p.productcatpath
    }
    let stragetyids = p.stragetyids
    var self = this;
    return this.db.transaction(async function (t) {
      p.stragetyids=p.stragetyids.join(",")
      let pnew = await self.dao.create(p, t)
      let productprices = []
      stragetyids.forEach(stragetyid => {
        let pps = {
          product_id: pnew.id,
          pricestrategy_id: stragetyid,
        }
        productprices.push(pps)
      })
      await self.priceDao.bulkCreate(productprices, t)
      return pnew;
    });
  }
}
module.exports = ProductService;
// (async ()=>{
//   let u=new AppService();
//  //  let x=await u.cregister("jiangong")
//  //  console.log(x)
//  // let x=await u.cunregister("jiangong")
//  //  console.log(x)
//   // let t=await u.cmakejwt()
//   // console.log(t)
//   //let ux=await u.cjsonregister(AppService.newRouteUrl("test-service2"),{name:"test-service2",hosts:["ttest1.com"]})
//   //let ux=await u.cjsonregister(AppService.newServiceUrl(),{name:"test-service3",url:"http://zhichan.gongsibao.com"})
//   //let ux=await u.cdel(AppService.routeUrl("test-service2"))
//   //let ux=await u.cdel(AppService.serviceUrl("test-service2"))
//   //  let ux=await u.create({name:"test4-service",backend:"zhichan-service",domainName:"domain.com"})
//   //   console.log(ux);
//   // let delrtn=await u.delete({id:2,name:"test4-service"})
//   // console.log(delrtn);
// })()