<?php
/**
 * ElasticSearch 初始化脚本
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2019/2/23
 * Time: 2:04 PM
 */

namespace Jobs\Controller;

use Mall\Framework\Factory;
use Mall\Framework\Core\Request;

class CESinit
{
    /**
     * Search Demo
     */
    public function search()
    {
        $data = Request::params();
        $c = $data['b'] ?: 1;

        switch ($c) {
            // 添加wms日志索引库
            case 1 :
                $mappings = array(
                    'mappings' => array(
                        "_default_" => [
                            "properties" => [
                                'userId' => ['type' => 'integer'],
                                "userName" => [
                                    "type" => "text",
                                    "store" => false,
                                    "analyzer" => "ik_max_word",
                                    "search_analyzer" => "ik_max_word",
                                    "boost" => 10
                                ],
                                'object' => ['type' => 'keyword'],
                                'actionType' => ['type' => 'keyword'],
                                "logContent" => [
                                    "type" => "text",
                                    "store" => false,
                                    "analyzer" => "ik_max_word",
                                    "search_analyzer" => "ik_max_word",
                                    "boost" => 5
                                ],
                                'time' => ['type' => 'integer'],
                            ]
                        ]
                    )
                );

                $searchServer = Factory::search('wms');
                $result = $searchServer->createBase('dev_wms_operation_log', $mappings);
                if ($result && !isset($result['error'])) {
                    echo 'dev_wms_operation_log init done.';
                    return true;
                } else {
                    print_r($result['error']);
                    return false;
                }

                break;
            // 删除wms索引库
            case 2:
                $searchServer = Factory::search('wms');
                $searchServer->setIndex('dev_wms_operation_log');
                $a = $searchServer->deleteBase('dev_wms_operation_log');
                print_r($a);
                break;
            // 添加oms订单状态日志索引库
            case 3 :
                $mappings = array(
                    'mappings' => array(
                        "_default_" => [
                            "properties" => [
                                'orderNo' => ['type' => 'keyword'],
                                'status' => ['type' => 'integer'],
                                'operationUserName' => ['type' => 'keyword'],
                                'time' => ['type' => 'integer'],
                            ]
                        ]
                    )
                );

                $searchServer = Factory::search('wms');
                $result = $searchServer->createBase('dev_oms_orderstatus_log', $mappings);
                if ($result && !isset($result['error'])) {
                    echo 'dev_oms_orderstatus_log init done.';
                    return true;
                } else {
                    print_r($result['error']);
                    return false;
                }

                break;
            // 删除oms订单状态索引库
            case 4:
                $searchServer = Factory::search('wms');
                $searchServer->setIndex('dev_oms_orderstatus_log');
                $a = $searchServer->deleteBase('dev_oms_orderstatus_log');
                print_r($a);
                break;
            // 添加capital日志索引库
            case 5 :
                $mappings = array(
                    'mappings' => array(
                        "_default_" => [
                            "properties" => [
                                'userId' => ['type' => 'integer'],
                                "userName" => [
                                    "type" => "text",
                                ],
                                'object' => ['type' => 'keyword'],
                                'actionType' => ['type' => 'keyword'],
                                "logContent" => [
                                    "type" => "text",
                                    "store" => false,
                                    "analyzer" => "ik_max_word",
                                    "search_analyzer" => "ik_max_word",
                                    "boost" => 5
                                ],
                                'time' => ['type' => 'integer'],
                            ]
                        ]
                    )
                );

                $searchServer = Factory::search('capital');
                $result = $searchServer->createBase('dev_capital_operation_log', $mappings);
                if ($result && !isset($result['error'])) {
                    echo 'dev_capital_operation_log init done.';
                    return true;
                } else {
                    print_r($result['error']);
                    return false;
                }

                break;
            // 删除wms索引库
            case 6:
                $searchServer = Factory::search('capital');
                $searchServer->setIndex('dev_capital_operation_log');
                $a = $searchServer->deleteBase('dev_capital_operation_log');
                print_r($a);
                break;
            // 添加oms日志索引库
            case 7 :
                $mappings = array(
                    'mappings' => array(
                        "_default_" => [
                            "properties" => [
                                'userId' => ['type' => 'integer'],
                                "userName" => [
                                    "type" => "text",
                                ],
                                'object' => ['type' => 'keyword'],
                                'actionType' => ['type' => 'keyword'],
                                "logContent" => [
                                    "type" => "text",
                                    "store" => false,
                                    "analyzer" => "ik_max_word",
                                    "search_analyzer" => "ik_max_word",
                                    "boost" => 5
                                ],
                                'time' => ['type' => 'integer'],
                            ]
                        ]
                    )
                );

                $searchServer = Factory::search('oms');
                $result = $searchServer->createBase('dev_oms_operation_log', $mappings);
                if ($result && !isset($result['error'])) {
                    echo 'dev_oms_operation_log init done.';
                    return true;
                } else {
                    print_r($result['error']);
                    return false;
                }

                break;
            // 删除wms索引库
            case 8:
                $searchServer = Factory::search('oms');
                $searchServer->setIndex('dev_oms_operation_log');
                $a = $searchServer->deleteBase('dev_oms_operation_log');
                print_r($a);
                break;

        }
    }
}