<?php

namespace common\models;

use yii\db\ActiveRecord;
use yii\db\Exception;
use yii\db\Expression;

/**
 * This is the model class for table "order_info".
 *
 * @property integer $id
 * @property integer $order_id
 * @property integer $prod_id
 * @property integer $count
 * @property string $unit_price
 * @property string $total price
 * @property integer $delete_flag
 */
class OrderInfo extends ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'order_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['order_id', 'prod_id', 'count', 'delete_flag'], 'integer'],
            [['unit_price', 'total_price','commission'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'order_id' => 'Order ID',
            'prod_id' => 'Prod ID',
            'count' => 'Count',
            'unit_price' => 'Unit Price',
            'total_price' => 'Total Price',
            'delete_flag' => 'Delete Flag',
        ];
    }

    /**
     * Function Description:插入子订单
     * Function Name: insertInfo
     * @param $prod_arr
     * @param $oderId
     *
     * @return bool
     *
     * @author 娄梦宁
     */
    public function insertInfo($prod_arr, $oderId)
    {
        $sql = "INSERT into order_info(order_id, total_price, count, unit_price, prod_id,commission)
            SELECT {$oderId}, prod_price*{$prod_arr['prod_count']} price,
            {$prod_arr['prod_count']},prod_price,prod_id,commission
            from prod_main
            where prod_id ={$prod_arr['prod_id']}";
        $res = $this->getDb()->createCommand($sql)->execute();
        if (!$res) {
            return false;
        }
        return true;
    }

    /**
     * Function Description:获取订单佣金
     * Function Name: insertInfo
     * @param $orderId int
     * @return int
     * @author 娄梦宁
     */
    public function getOrderCommission($orderId)
    {
        $select = [
            new Expression("IFNULL(SUM(b.commission*a.count),0) 'commission'"),
            'order_id'
        ];
        try{
            $result =self::find()->select($select)
                ->from(self::tableName().' as a')
                ->innerJoin(ProdMain::tableName().' as b','a.prod_id = b.prod_id')
                ->where(['=','a.order_id',$orderId])
                ->asArray()
                ->one();
        }catch (Exception $e) {
            $result['commission'] = 0;
        }
        return $result['commission'];
    }

    /**
     * Des:获取产品数组
     * Name: getFreeWalkProdArr
     * @param $order_id
     * @return array
     * @author 倪宗锋
     */
    public function geProdArr($order_id){
        $select = [
            'prod_id'=> 'b.bus_id',
            'prod_name'=> 'b.prod_name',
            'prod_num' => 'a.count'
        ];
        $where = ['=','a.order_id', $order_id];
        $getProdArr  = self::find()->select($select)
            ->from(self::tableName().' a')
            ->innerJoin(ProdMain::tableName().' b','a.prod_id = b.prod_id')
            ->where($where)
            ->asArray(true)
            ->all();
        return $getProdArr;
    }
}
