/*
 * Copyright (c) 2023 codvision.com All Rights Reserved.
 */

package com.codvision.workflow.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.workflow.entity.QuestionnaireModule;
import com.codvision.workflow.service.QuestionnaireModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author codvision
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/questionnaireModule")
@Api(tags = "动态表单设置模块")
public class QuestionnaireModuleController {

    private final QuestionnaireModuleService service;

    @PostMapping("/getPageResult")
    @ApiOperation("分页列表")
    public ResponseEntity<IPage<QuestionnaireModule>> getPageResult(@RequestBody QueryPage<Map<String, Object>> queryPage) {
        return new ResponseEntity<IPage<QuestionnaireModule>>().data(service.getPageResult(queryPage));
    }

    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<QuestionnaireModule> add(@RequestBody QuestionnaireModule questionnaireModule) {
        service.save(questionnaireModule);
        return new ResponseEntity<QuestionnaireModule>().back(200, "成功", questionnaireModule);
    }

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody List<QuestionnaireModule> questionnaireModules) {
        service.batchUpdate(questionnaireModules);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestParam(value = "id") Integer id) {
        service.removeById(id);
        return new ResponseEntity<String>().ok();
    }

    @ApiOperation(value = "校验某个问卷是否已经被用户在浏览器上填报过")
    @PostMapping("/checkFillIn")
    public ResponseEntity<Integer> checkFillIn(@ApiParam(value = "应用uuid") @RequestParam(value = "uuid", required = false, defaultValue = "") String uuid,
                                               @ApiParam(value = "浏览器id") @RequestParam(value = "webId", required = false, defaultValue = "") String webId) {
        ResponseEntity<Integer> responseEntity = new ResponseEntity<>();
        return responseEntity.back(200, "成功", service.checkFillIn(uuid, webId));
    }
}
