
package com.tmerclub.cloud.biz.controller.supplier;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Maps;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.supplier.feign.SupplierUserFeignClient;
import com.tmerclub.cloud.biz.dto.SendAndCheckSmsDTO;
import com.tmerclub.cloud.biz.service.SmsLogService;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.SendTypeEnum;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.PrincipalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/6/10 14:34
 */
@RestController("supplierSmsController")
@RequestMapping("/s/sms")
@Tag(name = "supplier-短信接口")
public class SmsController {

    @Autowired
    private SmsLogService smsLogService;
    @DubboReference
    private AccountFeignClient accountFeignClient;
    @DubboReference
    private SupplierUserFeignClient supplierUserFeignClient;
    public static final String CHECK_UPDATE_SMS_FLAG = "checkUpdatePwdSmsFlag";
    public static final String UPDATE_PWD_SMS_FLAG = "updatePwdSmsFlag";


    @PutMapping("/sendWithdrawCode")
    @Operation(summary = "给供应商手机号发送申请提现验证码", description = "给供应商手机号发送申请提现验证码，返回供应商手机号")
    public ServerResponseEntity<String> sendWithdrawCode() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        ServerResponseEntity<AuthAccountVO> supplierInfoRes = accountFeignClient.getAccountInfoByTenantId(shopId, sysType);
        if (!supplierInfoRes.isSuccess()) {
            throw new LuckException("服务器繁忙");
        }
        smsLogService.sendSmsCode(SendTypeEnum.VALID, supplierInfoRes.getData().getPhone(), Maps.newHashMap());
        return ServerResponseEntity.success(supplierInfoRes.getData().getPhone());
    }

    @PutMapping("/sendAuthShopUserCode")
    @Operation(summary = "发送认证店铺员工/子账号验证码", description = "发送认证店铺员工/子账号验证码")
    public ServerResponseEntity<Void> sendAuthShopUserCode(@RequestBody SendAndCheckSmsDTO sendAndCheckSmsDTO) {
        String phone = sendAndCheckSmsDTO.getMobile();
        if (StrUtil.isBlank(phone) || !PrincipalUtil.isMobile(phone)) {
            throw new LuckException("手机号格式不正确");
        }
        smsLogService.sendSmsCode(SendTypeEnum.VALID, phone, Maps.newHashMap());
        return ServerResponseEntity.success();
    }

    /**
     * 发送忘记密码验证码接口
     */
    @PostMapping("/ua/sendUpdatePwdCode")
    @Operation(summary = "发送忘记密码验证码接口", description = "发送忘记密码验证码接口")
    public ServerResponseEntity<Void> sendUpdatePwdCode(@RequestBody SendAndCheckSmsDTO sendSmsParam) {
        if (Objects.equals(supplierUserFeignClient.countAccoutByMobile(sendSmsParam.getMobile()).getData(), 0)) {
            throw new LuckException("该手机号码未注册");
        }
        // 每个手机号每分钟只能发十个注册的验证码，免得接口被利用
        smsLogService.sendSmsCode(SendTypeEnum.UPDATE_PASSWORD, sendSmsParam.getMobile(), Maps.newHashMap());
        return ServerResponseEntity.success();
    }

    @PutMapping("/ua/checkUpdatePwd")
    @Operation(summary = "校验用户忘记密码时的验证码", description = "校验验证码返回校验成功的标识")
    public ServerResponseEntity<String> checkUpdatePwd(@Valid @RequestBody SendAndCheckSmsDTO sendAndCheckSmsDto) {
        // 每个ip每分钟只能发十个注册的验证码，免得接口被利用
        if (!smsLogService.checkValidCode(sendAndCheckSmsDto.getMobile(), sendAndCheckSmsDto.getValidCode(), SendTypeEnum.UPDATE_PASSWORD)) {
            // 验证码有误或已过期
            return ServerResponseEntity.fail(ResponseEnum.VERIFICATION_CODE_ERROR);
        }
        String checkUpdatePwdSmsFlag = IdUtil.simpleUUID();
        RedisUtil.set(CHECK_UPDATE_SMS_FLAG + checkUpdatePwdSmsFlag, sendAndCheckSmsDto.getMobile(), 600);
        return ServerResponseEntity.success(checkUpdatePwdSmsFlag);
    }

}
