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)){
            String firstChar = phone.substring(0, 1);
            if(firstChar.matches("[1-9]")){
                //C端用户
                return xhpcSmsService.getLogonPhoneCode(phone, signatureName, templateId,null);
            }else if (UserTypeUtil.COMMUNIT.equals(phone.substring(0,1)) || UserTypeUtil.CUSTOMERS.equals(phone.substring(0,1))|| UserTypeUtil.MOTORCADE_USER.equals(phone.substring(0,1))|| UserTypeUtil.MOTORCADE_PERSON_IN_CHARGE.equals(phone.substring(0,1))){
                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) {

        System.out.println("============发送短信=======paramMap==========="+paramMap.toString());
        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.OPERATOR_CODE)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.VALIDATE_CODE;
        }else 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.REFUND_FAIL)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.REFUND_FAIL;
        }else if (content.contains(AliyunTemplateKeyWord.CHARGING_MODEL)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.CHARGING_MODEL;
        } else if(content.contains(AliyunTemplateKeyWord.WORK_ORDER_CREATED)){
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.WORK_ORDER_CREATED;
        } else if (content.contains(AliyunTemplateKeyWord.TENANT_EXPIRING_15D)){
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.TENANT_EXPIRING_15D;
        } else if (content.contains(AliyunTemplateKeyWord.OPERATOR_BANK_BIND)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.OPERATOR_BANK_BIND;
        } else if (content.contains(AliyunTemplateKeyWord.OPERATOR_CASHED)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.OPERATOR_CASHED;
        }else if (content.contains(AliyunTemplateKeyWord.OPERATOR_ORDER_REFUND)) {
            signatureName = AliyunTemplate.SIGNATURE_OREDER_NAME;
            templateId = AliyunTemplate.OPERATOR_ORDER_REFUND;
        }else if (content.contains(AliyunTemplateKeyWord.OPERATOR_PLACEHOLDER_ORDER)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.OPERATOR_PLACEHOLDER_ORDER;
        }else if (content.contains(AliyunTemplateKeyWord.OPERATOR_PLACEHOLDER_ORDER_MONEY)) {
            signatureName = AliyunTemplate.SIGNATURE_NAME;
            templateId = AliyunTemplate.OPERATOR_PLACEHOLDER_ORDER_MONEY;
        }

//        else if (content.contains(AliyunTemplateKeyWord.CHARGE_FULL)) {
//            signatureName = AliyunTemplate.SIGNATURE_NAME;
//            templateId = AliyunTemplate.CHARGE_FULL;
//        }
        if(!"SMS_227005968".equals(templateId)){
            xhpcSmsService.sendNotice(phone, signatureName, templateId, paramMap);
        }
    }

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