const req = require('../../utils/request')
// let Parse = getApp().Parse;
const company = getApp().globalData.company

async function getStores(params) {
  let {
    val,
    uid,
    skip,
    limit
  } = params
  let wh1 = val ? `AND (store."storeName" LIKE '%` + val + "%'" + 'OR store."storeAddress" LIKE ' + "'%" + val + "%')" : ``
  let sql = `SELECT * ,
  ("perCapita" * 1) price,
  (SELECT room."price" FROM "ShopRoom" room 
   WHERE room."shop" = store."objectId"
   AND room."isDeleted" IS NOT TRUE
   AND room."isEnabled" = TRUE
   ORDER BY room."price" ASC LIMIT 1
  ) "minPrice",
  (SELECT clt."isCollect" FROM "DramaShopCollect" clt 
   WHERE clt."homestayStore" = store."objectId" 
   AND clt."isCollect" = TRUE
   AND clt."isDeleted" IS NOT TRUE
   AND clt."user" = '${uid}'
   LIMIT 1 
  ) "iscollect"
  FROM "ShopStore" store
  WHERE "company" = '${company}'
  AND "type" = 'stay'
  AND "isShow" = 'true'
  AND "isDeleted" IS NOT TRUE
  ${wh1}
  ORDER BY "iscollect"
  OFFSET ${skip ?? 0} LIMIT ${limit ?? 10}`
  let res = await req.customSQL(sql);
  return res
}
module.exports = {
  getStores
};