<?php

namespace app\admin\controller\sms;

use app\model\Member;
use app\model\MemberBenefit;
use app\model\MemberQuota;
use support\Redis;
use support\Request;
use Hhink\WebmanSms\Sms;

class SmsController
{
    const KEY_PREFIX = 'SMS:CODE:';

    /**
     * @Desc 核销验证码
     * @Author Gorden
     * @Date 2024/6/4 8:46
     *
     * @param Request $request
     * @return \support\Response
     */
    public function sendSmsCodeByQuota(Request $request)
    {
        $memberId = $request->post('member_id', []);
        $componentId = $request->post('component_id', []);
        $nbr = $request->post('nbr');

        if (!$memberId || !$componentId) {
            return json_fail("参数异常");
        }

        $quotas = MemberQuota::where('join_quota_member_id', $memberId[0])
            ->where('join_member_rule_added_component_id', $componentId[0])
            ->where('member_quota_status', 'PENDING')
            ->limit($nbr)
            ->get()
            ->toArray();
        if (count($quotas) < intval($nbr)) {
            return json_fail("可核销数量不足");
        }

        try {
            $member = Member::find(current($memberId));
            $mobile = $member->member_mobile;

            $code = random_string(6, 'number');
            $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
            Redis::set($key, $code);
            Redis::expire($key, 600);

            Sms::app()->send($mobile, [
                'template' => 'SMS_469030007',
                'data' => [
                    'code' => $code
                ],
            ]);

        } catch (\Exception $e) {
            dump($e->getMessage());
            return json_fail("短信发送失败，请稍后重试");
        }


        return json_success('success');
    }
    
    public function sendSmsCodeByBenefit(Request $request)
    {
        $memberId = $request->post('member_id', '');
        $benefitId = $request->post('benefit_id', '');
        $nbr = $request->post('nbr');

        if (!$memberId || !$benefitId) {
            return json_fail("参数异常");
        }
        $benefit = MemberBenefit::where('member_benefit_id', $benefitId)
            ->where('join_benefit_member_id', $memberId)
            ->first();
        if (!$benefit) {
            return json_fail("权益不存在");
        }

        if (($benefit->member_benefit_limit_count - $benefit->member_benefit_used_count) < intval($nbr)) {
            return json_fail("可核销数量不足");
        }

        try {
            $member = Member::find($memberId);
            $mobile = $member->member_mobile;

            $code = random_string(6, 'number');
            $key = self::KEY_PREFIX . 'QUOTA:' . $mobile;
            Redis::set($key, $code);
            Redis::expire($key, 600);

            Sms::app()->send($mobile, [
                'template' => 'SMS_469030007',
                'data' => [
                    'code' => $code
                ],
            ]);

        } catch (\Exception $e) {
            dump($e->getMessage());
            return json_fail("短信发送失败，请稍后重试");
        }


        return json_success('success');
    }

    
    public function sendSmsCodeByWriteOff(Request $request)
    {
        $memberId = $request->post('member_id', '');
        
        try {
            $member = Member::find($memberId);
            $mobile = $member->member_mobile;

            $code = random_string(6, 'number');
            $key = self::KEY_PREFIX . 'WRITE_OFF:' . $mobile;
            Redis::set($key, $code);
            Redis::expire($key, 600);

            Sms::app()->send($mobile, [
                'template' => 'SMS_469030007',
                'data' => [
                    'code' => $code
                ],
            ]);

        } catch (\Exception $e) {
            dump($e->getMessage());
            return json_fail("短信发送失败，请稍后重试");
        }


        return json_success('success');
    }

    public function sendSmsByOrderPay(Request $request)
    {
        $memberId = $request->post('member_id', '');
        
        try {
            $member = Member::find($memberId);
            $mobile = $member->member_mobile;

            $code = random_string(6, 'number');
            $key = self::KEY_PREFIX . 'ORDER_PAY:' . $mobile;
            Redis::set($key, $code);
            Redis::expire($key, 600);

            Sms::app()->send($mobile, [
                'template' => 'SMS_469030007',
                'data' => [
                    'code' => $code
                ],
            ]);

        } catch (\Exception $e) {
            dump($e->getMessage());
            return json_fail("短信发送失败，请稍后重试");
        }


        return json_success('success');
    }
    
    /**
     * 餐饮核销验证码
     */
    public function dishesWriteOffCode(Request $request)
    {
        $memberId = $request->post('member_id', '');
        
        try {
            $member = Member::find($memberId);
            $mobile = $member->member_mobile;

            $code = random_string(6, 'number');
            $key = self::KEY_PREFIX . 'DISHES:' . $mobile;
            Redis::set($key, $code);
            Redis::expire($key, 600);

            Sms::app()->send($mobile, [
                'template' => 'SMS_469030007',
                'data' => [
                    'code' => $code
                ],
            ]);

        } catch (\Exception $e) {
            dump($e->getMessage());
            return json_fail("短信发送失败，请稍后重试");
        }


        return json_success('success');
    }
}