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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.applyForm.CoreApplyForm;
import com.yida.data.common.core.entity.applyForm.CoreApplyFormApprovalProcess;
import com.yida.data.customForm.dto.apply.ApplyFormSelectPageDTO;
import com.yida.data.customForm.dto.apply.SaveFormProcessDTO;
import com.yida.data.customForm.vo.apply.ApplyFormInfoVO;
import com.yida.data.customForm.vo.apply.ApplyFormProcessSelectVO;
import com.yida.data.customForm.vo.apply.ApplyFormSelectPageVO;
import com.yida.data.form.apply.service.CoreApplyFormApprovalProcessService;
import com.yida.data.form.apply.service.CoreApplyFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 申请类表单管理 Controller
 *
 * @author ZYJ
 * @date 2023/11/14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/applyFrom")
@Api(tags = "后台--申请类表单管理")
public class CoreApplyFormController {

    private final CoreApplyFormService coreApplyFormService;
    private final CoreApplyFormApprovalProcessService coreApplyFormApprovalProcessService;

    @ApiOperation("查询申请表单列表数据")
    @PostMapping("/listApplyFormPage")
    public ResultBean<IPage<ApplyFormSelectPageVO>> listApplyFormPage(ApplyFormSelectPageDTO dto) {
        IPage<ApplyFormSelectPageVO> page = coreApplyFormService.listApplyFormPage(dto);
        return ResultBean.buildSuccess(page);
    }

    @ApiOperation("查询申请表单详情")
    @GetMapping("/getApplyFormInfo")
    public ResultBean<ApplyFormInfoVO> getApplyFormInfo(
            @RequestParam @ApiParam(value = "表单id", required = true) Long id) {
        return ResultBean.buildSuccess(coreApplyFormService.getApplyFormInfo(id));
    }

    @ApiOperation("删除申请表单")
    @GetMapping("/deleteApplyForm")
    public ResultBean<String> deleteApplyForm(
            @RequestParam @ApiParam(value = "表单id", required = true) Long id) {
        coreApplyFormService.deleteApplyForm(id);
        return ResultBean.buildSuccess();
    }

    @ApiOperation(value = "保存申请表单", consumes = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saveApplyForm", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResultBean<Long> saveApplyForm(@RequestBody CoreApplyForm applyForm) {
        return ResultBean.buildSuccess(coreApplyFormService.saveApplyForm(applyForm));
    }

    @ApiOperation("修改申请表单状态")
    @GetMapping("updateEnableStatus")
    public ResultBean<String> updateEnableStatus(
            @ApiParam(value = "表单id", required = true) @RequestParam Long formId,
            @ApiParam(value = "状态. 0为停用，1为启用", required = true) @RequestParam Integer enableStatus) {
        coreApplyFormService.updateEnableStatus(formId, enableStatus);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询表单对应的审核流程数据")
    @GetMapping("/listFormApprovalProcess")
    public ResultBean<List<ApplyFormProcessSelectVO>> listFormApprovalProcess(
            @ApiParam(value = "表单id", required = true) @RequestParam Long formId) {
        return ResultBean.buildSuccess(coreApplyFormApprovalProcessService.listFormApprovalProcess(formId));
    }

    @ApiOperation("保存表单审核流程数据")
    @PostMapping("/saveFormApprovalProcess")
    public ResultBean<String> saveFormApprovalProcess(@RequestBody SaveFormProcessDTO dto) {
        coreApplyFormApprovalProcessService.saveApplyFormApprovalProcess(dto.getFormId() , dto.getList());
        return ResultBean.buildSuccess();
    }
}
