package com.iformall.controller;

import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxMsgValidationcode;
import com.iformall.enums.EnumAssignTagsTrigger;
import com.iformall.service.*;
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;

import java.util.Date;

@RestController
@RequestMapping("/api/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;

    @Autowired
    private WxCUserService wxCUserService;

    @Autowired
    private WxCUserTagsService wxCUserTagsService;

    @GetMapping("sendvalidationcode")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "type", value = "场景", dataType = "Integer", paramType = "query", required = true)})
    public ResultData sendvalidationcode(String phone, Integer type) {
        WxCUser user = getUser();
        WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode();
        wxMsgValidationcode.setTenantId(user.getTenantId());
        wxMsgValidationcode.setPhone(phone);
        wxMsgValidationcode.setType(type);
        wxMsgValidationcode.setAppid(user.getAppId());
        return wxMsgValidationcodeService.sendvalidationcode(wxMsgValidationcode);
    }

    @GetMapping("hasvalidationcode")
    @ApiImplicitParams({
            @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)})
    public ResultData hasvalidationcode(String phone, Integer type, String code) {
        WxCUser user = getUser();
        WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode();
        wxMsgValidationcode.setTenantId(user.getTenantId());
        wxMsgValidationcode.setPhone(phone);
        wxMsgValidationcode.setType(type);
        wxMsgValidationcode.setCode(code);
        wxMsgValidationcode.setAppid(user.getAppId());
        ResultData rd = wxMsgValidationcodeService.hasvalidationcode(wxMsgValidationcode);
        if (rd.code == Result.SUCCESS) {
            // 更新 WxCUser
            user.setVerifyCodePhone(phone);
            user.setUpdateDate(new Date());
            wxCUserService.saveOrUpdate(user);

            // 更新到basicinfo
            saveToBasicInfo(user);
            wxCUserTagsService.triggerAssignTags(EnumAssignTagsTrigger.ASSIGN_TAGS_TRIGGER_PHONE,user);
        }
        return rd;
    }


}
