package com.iformall.controller;

import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.po.WxMerchantBUser;
import com.iformall.domain.po.WxMsgValidationcode;
import com.iformall.enums.*;
import com.iformall.service.WxMerchantBUserService;
import com.iformall.service.WxMerchantService;
import com.iformall.service.WxMsgValidationcodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wxMsgValidationcode")
@Api(description="短信验证相关接口")
public class WxMsgValidationcodeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMsgValidationcodeService wxMsgValidationcodeService;

    @Autowired
    private WxMerchantBUserService wxMerchantBUserService;

    @Autowired
    private WxMerchantService wxMerchantService;



    @GetMapping("sendvalidationcode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "场景", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "appid", value = "appid", dataType = "String", paramType = "query", required = true)})
    public ResultData sendvalidationcode(String phone, String type, String appid) {

        WxMerchantBUser userQ = new WxMerchantBUser();
        userQ.setAppId(appid);
        userQ.setPhone(phone);
        userQ.setStatus(EnumMerchantBUserStatus.VALID.getCode());

        WxMerchantBUser user=wxMerchantBUserService.getBUserByAppId(userQ);
        if (user==null) {
            logger.error("B端用户不存在, phone: " + phone);
            return new ResultData(ErrorCode.USER_IS_EMPTY);
        }

        WxMerchant merchant = wxMerchantService.getById(user.getMerchantId());
        if (merchant==null) {
            logger.error("B端所在商户不存在, id: " + user.getMerchantId());
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_FOUND);
        }

        if(merchant.getStatus().equals(EnumMerchantStatus.NOT_VALID.getCode())){
            logger.error("B端所在商户已停用, id: " + merchant.getId());
            return new ResultData(ErrorCode.MERCHANT_INFO_NOT_VALID);
        }

        WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode();
        wxMsgValidationcode.setTenantId(user.getTenantId());
        wxMsgValidationcode.setPhone(phone);
        wxMsgValidationcode.setType(EnumMsgModel.VALIDATION_CODE.getCode());
        wxMsgValidationcode.setAppid(appid);
        return wxMsgValidationcodeService.sendvalidationcode(wxMsgValidationcode);
    }

    @GetMapping("hasvalidationcode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "场景", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "appid", value = "appid", dataType = "String", paramType = "query", required = true)})
    public ResultData hasvalidationcode(String tenantId, String phone, Integer type, String code, String appid) {
        WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode();
        wxMsgValidationcode.setTenantId(tenantId);
        wxMsgValidationcode.setPhone(phone);
        wxMsgValidationcode.setType(EnumMsgModel.VALIDATION_CODE.getCode());
        wxMsgValidationcode.setCode(code);
        wxMsgValidationcode.setAppid(appid);
        return wxMsgValidationcodeService.hasvalidationcode(wxMsgValidationcode);
    }


}
