const Parse = getApp().Parse;
const company = getApp().globalData.company
const date = require("../../utils/date");
const reqSql = require("../utils/request")

function getShopPost(id) {
  return new Promise((resolve, reject) => {
    let post = new Parse.Query("ShopOrder")
    post.equalTo("goods", id)
    post.equalTo("status", 800)
    post.count().then(res => {
      console.log(res);
      resolve(res)
    })
  })
}

async function getOrders(id, limit, length) {
  let sql = `select sp.user,sp.status,sp.images,sp.score,sp.open,sp.content,sp."updatedAt",
    us.avatar,us.nickname
    from "ShopOrder" as sp
    join "_User" as us
    on sp.user = us."objectId"
    where sp.company = '${company}'
    and sp.goods = '${id}' 
    and sp.status = 800
    order by sp."updatedAt" DESC
    limit ${limit ? limit : 200} 
    offset ${length}
    `
  let res = await reqSql.customSQL(sql)
  let orders = res.reduce((arr, item) => {
    item.date = date.formatTime("YYYY-mm-dd", item.updatedAt)
    arr.push(item)
    return arr
  }, [])
  console.log(orders);
  return orders
}

module.exports = {
  getShopPost,
  getOrders
}