package com.xhpc.general.controller;

import com.xhpc.common.api.UserTypeService;
import com.xhpc.common.core.domain.R;
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.common.util.UserTypeUtil;
import com.xhpc.general.constant.AliyunTemplate;
import com.xhpc.general.constant.AliyunTemplateKeyWord;
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;
    @Autowired
    private UserTypeService userTypeService;

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

        String signatureName = null;
        String templateId = null;

        signatureName = "小华充电";
        templateId = "SMS_226786362";
        if(phone !=null || "".equals(phone)){
            if(!UserTypeUtil.COMMUNIT.equals(phone.substring(0,2)) &&  !UserTypeUtil.CUSTOMERS.equals(phone.substring(0,2))){
                //C端用户
                return xhpcSmsService.getLogonPhoneCode(phone, signatureName, templateId,null);
            }else if (UserTypeUtil.COMMUNIT.equals(phone.substring(0,2)) || UserTypeUtil.CUSTOMERS.equals(phone.substring(0,2))){
                R user = userTypeService.getUser(phone, null, null, null,tenantId);
                if(user !=null && user.getData() !=null){
                    Map<String, Object> map = (Map<String, Object>)user.getData();
                    return xhpcSmsService.getLogonPhoneCode(map.get("phone").toString(), signatureName, templateId,map.get("account").toString());
                }
            }
        }
        return AjaxResult.error("1003", "请输入正确的手机号或账号");
    }

    @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(AliyunTemplateKeyWord.DIRECT_STREAM_PILE_STOP_CHARGE)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.DIRECT_STREAM_PILE_STOP_CHARGE;
        } else if (content.contains(AliyunTemplateKeyWord.INTERFLOW_STREAM_PILE_STOP_CHARGE)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.INTERFLOW_STREAM_PILE_STOP_CHARGE;
        } else if (content.contains(AliyunTemplateKeyWord.CHARGE_MONEY_INSUFFICIENT)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.CHARGE_MONEY_INSUFFICIENT;
        } else if (content.contains(AliyunTemplateKeyWord.CHARGE_FULL)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.CHARGE_FULL;
        } else if (content.contains(AliyunTemplateKeyWord.REFUND_FAIL)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.REFUND_FAIL;
        }

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

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