package com.yida.data.form.customForm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.customform.CoreCustomForm;
import com.yida.data.common.core.entity.customform.CoreCustomFormGroup;
import com.yida.data.form.customForm.service.CoreCustomFormGroupService;
import com.yida.data.form.customForm.service.CoreCustomFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表单组 Controller
 *
 * @author ccl
 * @date 2021-11-05 15:41:03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("coreCustomFormGroup")
@RequiredArgsConstructor
@Api(tags = "后台--表单分组")
public class CoreCustomFormGroupController {

    private final CoreCustomFormGroupService formGroupService;

    private final CoreCustomFormService customFormService;

    @GetMapping("findGroupPageList")
    @ApiOperation("分页查询表单组")
    public ResultBean<Page<CoreCustomFormGroup>> findGroupPageList(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                                                   @RequestParam(required = false) String groupName){


        return ResultBean.buildSuccess(formGroupService.findGroupPageList(pageNum,pageSize,groupName));
    }

    @PostMapping("saveOrUpdateGroup")
    @ApiOperation("新增表单组")
    public ResultBean saveOrUpdateGroup(@RequestBody CoreCustomFormGroup formGroup){
        formGroupService.saveOrUpdate(formGroup);
        return ResultBean.buildSuccess();
    }

    @PostMapping("removeFormGroup")
    @ApiOperation("批量删除表单组")
    public ResultBean removeFormGroup(@RequestBody List<Long> ids){
        formGroupService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }

    @GetMapping("updateFormGroup")
    @ApiOperation("将表单分到表单组")
    public ResultBean updateFormGroup(@RequestParam @ApiParam("表单组id") Long id,
                                      @RequestParam @ApiParam("组 id") Long groupId){
        CoreCustomForm coreCustomForm = new CoreCustomForm();
        coreCustomForm.setId(id);
        coreCustomForm.setGroupId(groupId);
        customFormService.updateById(coreCustomForm);
        return ResultBean.buildSuccess();
    }
}
