<?php

namespace addons\shopro\notification\wallet;

use addons\shopro\notification\Notification;
use addons\shopro\notification\traits\Notification as NotificationTrait;

/**
 * 积分变动通知
 */
class ScoreChange extends Notification
{
    use NotificationTrait;

    // 队列延迟时间，必须继承 ShouldQueue 接口
    public $delay = 0;

    public $receiver_type = 'user';      // 接收人:user=用户

    // 消息类型  Notification::$notificationType
    public $notification_type = 'shop';

    // 发送类型
    public $event = 'score_change';

    // 额外数据
    public $data = [];

    public $template = [
        'MessageDefaultContent' => '您的积分发生变化，变动类型：{event_name}，变动数量：{amount}，请及时查看',
        // 'WechatOfficialAccount' => [
        //     'temp_no' => 'OPENTM415437052',
        //     'fields' => [
        //         // 'first' => '您的积分发生变动，请及时查看',
        //         // 'keyword1' => 'event_name',                               // 交易类型
        //         // 'keyword2' => 'amount',                               // 交易金额
        //         // 'keyword3' => 'create_date',                               // 交易时间
        //         // 'keyword4' => 'score',                               // 账户余额
        //         // 'remark' => '您的积分发生变动，请及时查看',
        //         [
        //             "template_field" => "first",
        //             "value" => '您的积分发生变动，请及时查看',
        //         ],
        //         [
        //             "name" => "交易类型",
        //             "field" => "event_name",
        //             "template_field" => "keyword1",
        //         ],
        //         [
        //             "name" => "交易金额",
        //             "field" => "amount",
        //             "template_field" => "keyword2",
        //         ],
        //         [
        //             "name" => "交易时间",
        //             "field" => "create_date",
        //             "template_field" => "keyword3",
        //         ],
        //         [
        //             "name" => "账户余额",
        //             "field" => "score",
        //             "template_field" => "keyword4",
        //         ],
        //         [
        //             "template_field" => "remark",
        //             "value" => '您的积分发生变动，请及时查看',
        //         ]
        //     ],
        // ],
        'WechatOfficialAccount' => [
            'temp_no' => false,         // 目前公众号类目模板库，找不到符合条件模板
            'keywords' => [],
            'fields' => [],
        ],
        'WechatMiniProgram' => [
            'category_id' => 670,
            'tid' => '3613',
            'kid' => [8,15,6,7],      // 变动原因,积分变动,积分总额,变动时间
            'scene_desc' => '当积分发生变化时通知用户',     // 申请模板场景描述
            'fields' => [
                // 'thing8' => 'event_name',                               // 变动原因
                // 'number1' => 'amount',                               // 积分变动
                // 'number6' => 'score',                               // 积分总额
                // 'time7' => 'create_date',                               // 变动时间
                [
                    "name" => "变动原因",
                    "field" => "event_name",
                    "template_field" => "thing8",
                ],
                [
                    "name" => "积分变动值",
                    "field" => "amount",
                    "template_field" => "character_string15",
                ],
                [
                    "name" => "积分总额",
                    "field" => "score",
                    "template_field" => "number6",
                ],
                [
                    "name" => "变动时间",
                    "field" => "create_date",
                    "template_field" => "time7",
                ],
            ],
        ]
    ];

    // 返回的字段列表
    public $returnField = [
        'name' => '积分变动通知',
        'channels' => ['Sms', 'Email', 'WechatOfficialAccount', 'WechatMiniProgram'],
        'fields' => [
            ['name' => '消息名称', 'field' => 'template'],
            ['name' => '变动用户', 'field' => 'nickname'],
            ['name' => '用户手机', 'field' => 'mobile'],
            ['name' => '变动类型', 'field' => 'event_name'],
            ['name' => '变动数量', 'field' => 'amount'],
            ['name' => '变动前', 'field' => 'before'],
            ['name' => '变动后', 'field' => 'after'],
            ['name' => '当前积分', 'field' => 'score'],
            ['name' => '备注信息', 'field' => 'memo'],
            ['name' => '变动时间', 'field' => 'create_date'],
        ]
    ];



    /**
     * 组合数据参数
     *
     * @param \think\Model $notifiable
     * @return array
     */
    protected function getData($notifiable)
    {
        $walletLog = $this->data['walletLog'];
        $type = $this->data['type'];

        $data['template'] = $this->returnField['name'];           // 模板名称
        $data['nickname'] = $notifiable['nickname'];
        $data['mobile'] = $notifiable['mobile'];
        $data['event_name'] = '积分变动';
        $data['amount'] = $walletLog['amount'];
        $data['before'] = $walletLog['before'];
        $data['after'] = $walletLog['after'];
        $data['score'] = $walletLog['after'];
        $data['memo'] = $walletLog['event_text'] . ($walletLog['memo'] ? ('-' . $walletLog['memo']) : '');
        $data['create_date'] = $walletLog['createtime'];

        // 统一跳转地址
        $data['jump_url'] = "/pages/user/wallet/score";

        return $data;
    }
}
