package com.qmth.distributed.print.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.distributed.print.business.entity.BasicMessage;
import com.qmth.teachcloud.common.enums.MessageEnum;
import com.qmth.distributed.print.business.service.BasicMessageService;
import com.qmth.teachcloud.common.annotation.OperationLogDetail;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.EnumResult;
import com.qmth.teachcloud.common.util.Result;
import com.qmth.teachcloud.common.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;

/**
 * <p>
 * 短信发送日志 前端控制器
 * </p>
 *
 * @author xf
 */
@Api(tags = "短信发送日志Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_BASIC + "/message")
@Validated
public class BasicMessageController {

    @Resource
    private BasicMessageService basicMessageService;

    /**
     * 查询短信类型
     *
     * @return
     */
    @ApiOperation(value = "查询短信类型")
    @RequestMapping(value = "/list_message_types", method = RequestMethod.POST)
    public Result listType() {
        List<EnumResult> list = basicMessageService.listMessageTypes();
        return ResultUtil.ok(list);
    }

    /**
     * 查询
     *
     * @param messageType
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "查询")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result list(@RequestParam(value = "messageType", required = false) MessageEnum messageType,
                       @RequestParam(value = "sendStatus", required = false) Boolean sendStatus,
                       @RequestParam(value = "mobileNumber", required = false) String mobileNumber,
                       @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                       @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<BasicMessage> messageIPage = basicMessageService.listByMessageType(messageType, sendStatus, mobileNumber, pageNumber, pageSize);
        return ResultUtil.ok(messageIPage);
    }

    /**
     * 短信失败重发
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "短信失败重发")
    @RequestMapping(value = "/resend", method = RequestMethod.POST)
    @OperationLogDetail
    public Result list(@RequestParam(value = "id", required = false) Long id) {
        basicMessageService.resendSmsTaskById(id);
        return ResultUtil.ok(true, null);
    }
}

