package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreVerificationCode;
import shop.alien.store.service.StoreVerificationCodeService;

/**
 * 短信验证码 CRUD
 */
@Api(tags = {"二期-验证码管理"})
@Slf4j
@ApiSort(30)
@CrossOrigin
@RestController
@RequestMapping("/storeVerificationCode")
@RequiredArgsConstructor
public class StoreVerificationCodeController {

    private final StoreVerificationCodeService storeVerificationCodeService;

    @ApiOperation("分页查询验证码记录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "appType", value = "端区分(0:用户,1:商家,2:律师端)", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<StoreVerificationCode>> page(@RequestParam(value = "phone", required = false) String phone,
                                                @RequestParam(value = "appType", required = false) Integer appType,
                                                @RequestParam(value = "businessType", required = false) Integer businessType,
                                                @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        log.info("StoreVerificationCodeController.page?phone={}&appType={}&businessType={}&pageNum={}&pageSize={}",
                phone, appType, businessType, pageNum, pageSize);
        return R.data(storeVerificationCodeService.pageList(phone, appType, businessType, pageNum, pageSize));
    }

    @ApiOperation("根据ID获取验证码记录")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "path", required = true)
    @GetMapping("/{id}")
    public R<StoreVerificationCode> detail(@PathVariable("id") Integer id) {
        log.info("StoreVerificationCodeController.detail?id={}", id);
        return R.data(storeVerificationCodeService.getById(id));
    }

    @ApiOperation("新增验证码记录")
    @ApiOperationSupport(order = 3)
    @PostMapping
    public R<StoreVerificationCode> create(@RequestBody StoreVerificationCode request) {
        log.info("StoreVerificationCodeController.create?request={}", request);
        return R.data(storeVerificationCodeService.create(request));
    }

    @ApiOperation("更新验证码记录")
    @ApiOperationSupport(order = 4)
    @PutMapping
    public R<StoreVerificationCode> update(@RequestBody StoreVerificationCode request) {
        log.info("StoreVerificationCodeController.update?request={}", request);
        return R.data(storeVerificationCodeService.updateRecord(request));
    }

    @ApiOperation("删除验证码记录")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParam(name = "id", value = "主键id", dataType = "Integer", paramType = "path", required = true)
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable("id") Integer id) {
        log.info("StoreVerificationCodeController.delete?id={}", id);
        return R.data(storeVerificationCodeService.removeRecord(id));
    }
}

