<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 服务通知（微信公众号的模板消息）
 */
namespace app\allwin\widget;
use app\common\facade\WechatMp;
use Exception;

class Inform{

    /**
     * 买单通知
     * @param array $param
     * $param['uid']        = 用户ID;
     * $param['store_name'] = 好店名称;
     * $param['amount']     = 付款金额;
     * $param['order_no']   = 订单号;
     * $param['miniapp_id'] = 所属应用ID;
     * $param['tplmsg']     = 模板ID;
     * @return void
     */
    public static function order(array $param){
        $uid[] = $param['uid'];
        //给好店收银员发通知
        $worker = model('StoreWorker')->where(['store_id' => $param['store_id'],'is_cashier' => 1])->field('uid')->select()->toArray();
        if(!empty($worker)){
            $uid = array_merge(array_column($worker,'uid'),$uid);
        }
        $user = model('SystemUser')->where(['id' => $uid])->field('official_uid')->select();
        if(empty($user)){
           return;
        }
        //用户类别
        $wechat = WechatMp::isTypes($param['miniapp_id']);
        if(empty($wechat)){
            return;
        }
        try {
            $wechat = $wechat->template_message;
            foreach ($user as $value) {
                if(!empty($value['official_uid'])){
                    $wechat->send([
                        'touser'      => $value['official_uid'],
                        'template_id' => $param['tplmsg'],
                        'data' => [
                            'first'    => '微信买单已到账',
                            'keyword1' => $param['order_no'],  //订单编号
                            'keyword2' => ['value'=>'￥'.$param['amount'],'color' => '#FF0000'],
                            'keyword3' => $param['store_name'],  //消费门店
                            'keyword4' => date('Y-m-d H:i:s'),  //消费时间
                        ],
                    ]);
                }
            }
        }catch (Exception $e) {
            return;
        }
    }
}