package com.xhpc.general.controller;

import com.xhpc.common.core.web.controller.BaseController;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.page.TableDataInfo;
import com.xhpc.general.service.IXhpcSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yuyang
 * @date 2021/8/1 23:42
 */
@RestController
@RequestMapping("/sms")
public class XhpcSmsController extends BaseController {

    @Autowired
    private IXhpcSmsService xhpcSmsService;

    /**
     * 注册获取手机号验证码
     */
    @GetMapping(value = "/getLogonPhoneCode")
    public AjaxResult getLogonPhoneCode(@RequestParam("phone") String phone) {

        String signatureName = null;
        String templateId = null;

        signatureName = "小华充电";
        templateId = "SMS_226786362";

        return xhpcSmsService.getLogonPhoneCode(phone, signatureName, templateId);
    }

    @GetMapping(value = "/getList")
    public TableDataInfo getList(Integer status, String phone) {

        startPage();
        List<Map<String, Object>> list = xhpcSmsService.getList(status, phone);
        return getDataTable(list);
    }

    @PostMapping(value = "/send")
    public void send(@RequestBody Map<String, String> paramMap) {


        String signatureName = null;
        String templateId = null;

        //方便使用PostMan调用接口测试

        String phone = paramMap.get("phone");
        String content = paramMap.get("content");
        paramMap.remove("phone");
        paramMap.remove("content");
        //判断内容是什么，调用相应的模板
        if (content.contains("电量为")) {
            signatureName = "小华充电";
            templateId = "SMS_226945037";
        } else if (content.contains("总费用为")) {
            signatureName = "小华充电";
            templateId = "SMS_226786374";
        } else if (content.contains("余额小于")) {
            signatureName = "小华充电";
            templateId = "SMS_226935795";
        } else if (content.contains("设定的SOC")) {
            signatureName = "小华充电";
            templateId = "SMS_227005968";
        }

        xhpcSmsService.sendNotice(phone, signatureName, templateId, paramMap);
    }

    @GetMapping(value = "/getAliyunShortMessageInfo")
    public AjaxResult getAliyunShortMessageInfo() {
        Object messageInfo = xhpcSmsService.getAliyunShortMessageInfo();
        return AjaxResult.success(messageInfo);
    }
}
