<?php

namespace App\Models\marketing\mysql;

use Api\PhpUtils\Mysql\MysqlBase;

class MarketingGoods extends MysqlBase
{
    const TABLE_NAME = 'marketing_goods';
    const CONFIG_INDEX = 'marketing';
    const PRIMARY_KEY = 'id';

    public static function marketingGoodsList($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::select($colums, $where);
    }

    public static function getRecord($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::get($colums, $where);
    }
    public static function getRecordMaster($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::selectMaster($colums, $where);
    }

    public static function insertRecord($colums)
    {
        return self::insert($colums);
    }

    public static function updateRecord($colums, $where)
    {
        return self::update($colums, $where);
    }

    public static function save($data, $where = [])
    {
        if (empty($where)) {
            return self::insert($data);
        }
        return self::update($data, $where);
    }

    public static function deleteRecord($where)
    {
        return self::delete($where);
    }

    public static function getRecords($where, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }

        if (!empty($where['marketing_name'])) {
            $where['marketing_name'] = $where['marketing_name'];
        }

        if (!empty($where)) {
            $data = self::formatWhere($where);
        }
        
        if (!empty($where['LIMIT'])) {
            $data['LIMIT'] = $where['LIMIT'];
        }

        if (!empty($where['ORDER'])) {
            $data['ORDER'] = $where['ORDER'];
        }
        
        return self::select($colums, $data);
    }


    public static function getCount($where, $columns = "*")
    {
        if (!empty($where)) {
            $where = self::formatWhere($where);
        }
        
        if (empty($columns)) {
            $columns = '*';
        }
        return self::count($columns, $where);
    }

    public static function formatWhere($where)
    {
        $data = [];
       
        if (!empty($where['marketing_name'])) {
            $data['marketing_name[~]'] = $where['marketing_name'];
        }

        if (!empty($where['marketing_id'])) {
            $data['marketing_id'] = $where['marketing_id'];
        }

        if (!empty($where['online_status'])) {
            $data['online_status'] = $where['online_status'];
        }

        return $data;
    }
}
