<?php

namespace App\Models\marketing\mysql;

use Api\PhpUtils\Mysql\MysqlBase;

class Marketing extends MysqlBase
{
    const TABLE_NAME = 'marketing';
    const CONFIG_INDEX = 'marketing';
    const PRIMARY_KEY = 'marketing_id';

    const MARKETING_TYPE_FENXIAO = 1;
    const MARKETING_TYPE_TUANGOU = 2;
    const MARKETING_TYPE_MIAOSHA = 3;
    const MARKETING_TYPE_PINDAN = 4;
    const MARKETING_TYPE_LIEBIAN = 5;
    const MARKETING_TYPE_TUANZHANG = 6;

    const ONLINE_STATUS_QIDONG = 1;
    const ONLINE_STATUS_GUANBI = 2;
    const ONLINE_STATUS_DAOQI = 3;

    public static $onlineStatusDesc = [
        self::ONLINE_STATUS_QIDONG => "启动",
        self::ONLINE_STATUS_GUANBI => "关闭",
        self::ONLINE_STATUS_DAOQI => "到期",
    ];

    const COMMISSION_MODE_RATE = 1;
    const COMMISSION_MODE_FIXED = 2;

    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 getMarketingList($data, $colums = [])
    {
        if (empty($colums)) {
            $colums = '*';
        }
        return self::select($colums, $data);
    }

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

        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'];
            $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'];
        }

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

        return $data;
    }

    /**
     * 判断活动是否在进行中
     * @param $marketingId
     * @return bool
     */
    public static function isGoing($marketingId)
    {
        if (empty($marketingId)) {
            return false;
        }
        $marketing = self::getRecord(["marketing_id" => $marketingId]);
        return !empty($marketing) && $marketing["online_status"] == self::ONLINE_STATUS_QIDONG;
    }
}
