const system = require("../../../system");
const Dao = require("../../dao.base");
class ProducttypeDao extends Dao {
  constructor() {
    super(Dao.getModelName(ProducttypeDao));
  }
  async findAndCountAll(req) {
    var params = {
      // company_id: req.actionBody.company_id
      company_id: 10
    };
    var returnRes = {
      results: {rows:[],count:[]}
    };
    var dataCount = "select count(1) as dataCount from mc_product_type  where deleted_at is null and p_id = 0 and company_id = :company_id ";
    var sql = "select * from mc_product_type  where deleted_at is null and p_id = 0 and company_id = :company_id ";
    var childData = "select  *,count(1) as childCount from mc_product_type where p_id !=0 and company_id = :company_id group by p_id";

    var list = await this.customQuery(sql, params);
    returnRes.results.rows = list;
    
    var tmpResultCount = await this.customQuery(dataCount, params);
    returnRes.results.count = tmpResultCount && tmpResultCount.length > 0 ? tmpResultCount[0].dataCount : 0;

    var childrenData = await this.customQuery(childData, params);
    for(var i=0;i<childrenData.length;i++){
      returnRes.results.rows[i].childCount = childrenData && childrenData[i].childCount > 0 ? childrenData[i].childCount : 0;
    }
    console.log(returnRes)
    return returnRes;
  }
}
module.exports = ProducttypeDao;
