<?php

/**
 * Class that operate on table 'onlinetempstoredetail'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2019-11-13 10:55
 */
class OnlinetempstoredetailMySqlExtDAO extends OnlinetempstoredetailMySqlDAO {

    public function insertOrUpdateOnDuplicate($obj) {//quantity += value
        //must have UNIQUE constraint on the "combination" of the two columns
        //ALTER TABLE onlinetempstoredetail ADD CONSTRAINT uniquecombination UNIQUE(`storeid`,`productid`,`sizeid`,`colorid`)
        $sql = "INSERT INTO onlinetempstoredetail (storeid, productid, sizeid, colorid, quantity, edited, sysdate, userid)
                VALUES ($obj->storeid, $obj->productid, $obj->sizeid, $obj->colorid, $obj->quantity,$obj->edited,'" . $obj->sysdate . "',$obj->userid)
                ON DUPLICATE KEY UPDATE quantity = quantity+$obj->quantity, edited = $obj->edited, sysdate = '" . $obj->sysdate . "', userid = $obj->userid";
        $sqlQuery = new SqlQuery($sql);
        return $this->executeUpdate($sqlQuery);
    }

    public function insertOrUpdateOnDuplicate2($obj) {//quantity = value
        //must have UNIQUE constraint on the "combination" of the two columns
        //ALTER TABLE onlinetempstoredetail ADD CONSTRAINT uniquecombination UNIQUE(`storeid`,`productid`,`sizeid`,`colorid`)
        $sql = "INSERT INTO onlinetempstoredetail (storeid, productid, sizeid, colorid, quantity, edited, sysdate, userid)
                VALUES ($obj->storeid, $obj->productid, $obj->sizeid, $obj->colorid, $obj->quantity,$obj->edited,'" . $obj->sysdate . "',$obj->userid)
                ON DUPLICATE KEY UPDATE quantity = $obj->quantity, edited = $obj->edited, sysdate = '" . $obj->sysdate . "', userid = $obj->userid";
        $sqlQuery = new SqlQuery($sql);
        return $this->executeUpdate($sqlQuery);
    }

    public function queryAllSimpleForCurl($sqlquery = '') {//get quantity from stores"storedetail" not only quantity changed that is in"onlinetempstoredetail"
        $sql = 'SELECT onlinetempstoredetail.*,SUM(storedetail.productquantity) AS quantity
		FROM onlinetempstoredetail
                left JOIN storedetail ON storedetail.productid = onlinetempstoredetail.productid
                where sizeid = 0 and colorid = 0 ' . $sqlquery . '
                group by productid,sizeid,colorid
                FOR UPDATE'; //where sizeid = 0 and colorid = 0 as we didinot handle size color in insert in shop until now
        //echo $sql;
        $sqlQuery = new SqlQuery($sql);

        return $this->getList($sqlQuery);
    }

    public function TRUNCATE() {
        $sql = "TRUNCATE onlinetempstoredetail";
        $sqlQuery = new SqlQuery($sql);
        return $this->executeUpdate($sqlQuery);
    }

}

?>