const system = require("../../../system");
const ServiceBase = require("../../sve.base");
class ProductpriceService extends ServiceBase {
  constructor() {
    super("product", ServiceBase.getDaoName(ProductpriceService));
  }
  /**
   * @param {*} pname 产品名称
   * @param {*} spname 服务商名称
   */
  async findRegionsByProductName(productname, spname) {
    let ps = await this.dao.model.findAll({
      where: {
        sptags: { [this.db.Op.like]: "%" + spname + "%" },
        pname: { [this.db.Op.like]: "%" + productname + "%" },
      }, attributes: ['pname'], raw: true
    })
    //查询出地区
    let rs = await this.db.models.region.findAll({ attributes: ['code', 'name'], raw: true })
    //获取地区JSON 对象
    let psrtn = []
    let cacheregions={}
    ps.forEach(r => {
      let rpathstr = r.pname.split("~")[1]
      let rpatharray = rpathstr.split("/")
      let lstindex = rpatharray.length - 1
      let regionName = rpatharray[lstindex]
      let regioninfo = {}
      regioninfo["label"] = regionName
      //按照名字去取地区编码
      let areainfos = rs.filter(f => {
        if (f.name.indexOf(regionName)>=0) {
          return true
        } else {
          return false
        }
      })
      let codeval=String(areainfos[0].code).padEnd(6,'0')
      if(areainfos[0].name.indexOf("天津") ||areainfos[0].name.indexOf("北京") || areainfos[0].name.indexOf("上海") || areainfos[0].name.indexOf("重庆")){
        codeval=String(areainfos[0].code+"01").padEnd(6,'0')
      }
      if(!cacheregions[codeval]){
        cacheregions[codeval]=regioninfo
        regioninfo["code"] = codeval
        psrtn.push(regioninfo)
      }
    })
    return psrtn
  }
  async updownProduct(uid) {
    let p = await this.dao.findById(uid)
    p.isEnabled = !p.isEnabled
    await p.save()
    return {}
  }
}
module.exports = ProductpriceService;

// let p=new ProductpriceService()
// p.findRegionsByProductName("个体户注册","gao").then((res)=>{
//   console.log(res)
// })