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

package com.codvision.workflow.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.commoncore.common.ResponseEntity;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.workflow.entity.Questionnaire;
import com.codvision.workflow.service.QuestionnaireService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author codvision
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/questionnaire")
@Api(tags = "动态表单应用管理模块")
public class QuestionnaireController {

    private final QuestionnaireService service;

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

    @PostMapping("/add")
    @ApiOperation("添加")
    public ResponseEntity<String> add(@Validated @RequestBody Questionnaire questionnaire) {
        questionnaire.setUuid(IdUtil.fastSimpleUUID());
        service.save(questionnaire);
        return new ResponseEntity<String>().ok();
    }

    @PostMapping("/update")
    @ApiOperation("编辑")
    public ResponseEntity<String> update(@RequestBody Questionnaire questionnaire) {
        ResponseEntity<String> responseEntity = new ResponseEntity<>();
        if (questionnaire.getId() == null) {
            throw new BusinessException(30002);
        }
        service.updateById(questionnaire);
        return responseEntity.ok();
    }

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