<?php

namespace app\extra\basic;

use app\model\system\SystemConfig;
use Overtrue\EasySms\Strategies\OrderStrategy;

class Service
{

    /**
     * @var null
     */
    protected $mode = null;

    /**
     * 短信配置
     * @param int $type 1 系统配置 2 代理配置
     * @return array
     */
    protected function smsConfig(int $type = 1): array
    {
        $sms = (new SystemConfig)->where("type","sms")->column("value","name");
        return [
            "config" => [
                'enable' => true,
                'timeout' => 5.0,
                "default"   => [
                    "strategy"  => OrderStrategy::class,
                    "gateways"  => [$sms['sms_type']]
                ],
                "gateways"  => [
                    'errorlog' => [
                        'file' => runtime_path().'/tmp/easy-sms.log',
                    ],
                    'aliyun' => [
                        'access_key_id' => $sms['AccessKeyId'],
                        'access_key_secret' => $sms['AccessKeySecret'],
                        'sign_name' => trim($sms['sign']),
                    ],
                ]
            ],
            "template"  => $sms['login']
        ];
    }
    /**
     * 默认排序筛选
     * @param array $param
     * @param string $prefix
     * @param array $filter
     * @return string[]
     */
    public function defaultSort(array $param = [],string $prefix = "",array $filter = []): array
    {
        if (!empty($prefix)) $prefix = $prefix.".";
        if (isset($param['order']) && $param['order'] == 'descending'){
            $orderBy = ["{$prefix}{$param['field']}" => "desc"];
        } else if (isset($param['order']) && $param['order'] == 'ascending'){
            $orderBy = ["{$prefix}{$param['field']}" => "asc"];
        } else {
            $orderBy = ["{$prefix}create_at" => "desc"];
        }
        return $orderBy;
    }


    /**
     * @param array $param 参数
     * @param array $filter 过滤器
     * @param string $prefix 链表前缀
     * @param string $join 链表表名
     * @param string $field 字段
     * @param string $forKey 链表首字母
     * @param string $localKey 被链表首字母
     */
    protected function searchVal(array $param = [],array $filter = [],string $prefix = "",string $join = "",string $field = "",string $forKey = "",string $localKey = "")
    {
        $orderBy = $this->defaultSort($param,$prefix);
        $commonFilter = [];
        // 起止时间
        if (!empty($param['create'])) {
            $times = between_time($param['create']);
            $start = date('Y-m-d',$times['start_time']);
            $end = date('Y-m-d',($times['end_time'] + 86400));
            $commonFilter[] = ['create_at', '>=', $start ];
            $commonFilter[] = ['create_at', '<', $end ];
        }
        $filter = array_merge($filter,$commonFilter);
        if (!empty($join)) {
            $this->mode =  $this->mode->alias('a')->join("{$join} {$prefix}","a.{$forKey} = {$prefix}.{$localKey}")->field($field);
        }
        return $this->mode->order($orderBy)->where($filter);
    }

}