package com.iformall.controller.msg;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxMsgValidationcode;
import com.iformall.service.WxMsgValidationcodeService;
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.*;

@RestController
@RequestMapping("wxMsgValidationcode")
public class WxMsgValidationcodeController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMsgValidationcodeService wxMsgValidationcodeService;

    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "消息验证码-列表")
    public ResultData list(@ModelAttribute WxMsgValidationcode wxMsgValidationcode, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::list");
        if (null == wxMsgValidationcode) wxMsgValidationcode = new WxMsgValidationcode();
        final PageInfo<WxMsgValidationcode> page = wxMsgValidationcodeService.listAsPage(wxMsgValidationcode, pageNum, pageSize);
        return new ResultData(page);
    }

    @PostMapping("add")
    @SystemControllerLog(description = "消息验证码-添加")
    public ResultData add(@RequestBody WxMsgValidationcode wxMsgValidationcode) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::add");
        //Assert.notNull(wxMsgValidationcode.getName(), "角色名不能为空");
        //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
        wxMsgValidationcodeService.saveOrUpdate(wxMsgValidationcode);
        return new ResultData();
    }

    @PostMapping("update")
    @SystemControllerLog(description = "消息验证码-更新")
    public ResultData update(@RequestBody WxMsgValidationcode wxMsgValidationcode) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::update");
        wxMsgValidationcodeService.saveOrUpdate(wxMsgValidationcode);
        return new ResultData();
    }

    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "消息验证码-删除")
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::update");
        wxMsgValidationcodeService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "消息验证码-查询")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxMsgValidationcodeService.getById(id));
    }


    @GetMapping("sendvalidationcode")
    @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 = "appid", value = "appid", dataType = "String", paramType = "query", required = true)})
    public ResultData sendvalidationcode(String tenantId, String phone, Integer type, String appid) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::sendvalidationcode");
        WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode();
        wxMsgValidationcode.setTenantId(tenantId);
        wxMsgValidationcode.setPhone(phone);
        wxMsgValidationcode.setType(type);
        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) {
        logger.debug("[" + getIpAddr() + "] WxMsgValidationcodeController::hasvalidationcode");
        WxMsgValidationcode wxMsgValidationcode = new WxMsgValidationcode();
        wxMsgValidationcode.setTenantId(tenantId);
        wxMsgValidationcode.setPhone(phone);
        wxMsgValidationcode.setType(type);
        wxMsgValidationcode.setCode(code);
        wxMsgValidationcode.setAppid(appid);
        return wxMsgValidationcodeService.hasvalidationcode(wxMsgValidationcode);
    }


}
