package com.usky.oa.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.oa.domain.OaFormDefinition;
import com.usky.oa.service.OaFormDefinitionService;
import com.usky.oa.service.enums.OaBuiltInDocument;
import com.usky.oa.service.utils.OaDefinitionDocumentInfo;
import com.usky.oa.service.vo.OaFormNameResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 表单定义表 前端控制器
 * </p>
 *
 * @author fu
 * @since 2024-12-10
 */
@RestController
@RequestMapping("/oaFormDefinition")
public class OaFormDefinitionController {

    @Autowired
    private OaFormDefinitionService oaFormDefinitionService;

    // 新增、修改表单
    @Log(title = "新增/修改表单", businessType = BusinessType.INSERT)
    @PostMapping("/addForm")
    public void addForm(@RequestBody OaFormDefinition oaFormDefinition) {
        oaFormDefinitionService.addForm(oaFormDefinition);
    }

    // 表单状态修改（启用、停用）
    @Log(title = "启停用表单", businessType = BusinessType.UPDATE)
    @GetMapping("/updateStatus")
    public void updateStatus(@RequestParam(value = "formId") Integer formId,
                             @RequestParam(value = "formStatus") Integer formStatus) {
        oaFormDefinitionService.validUpdateStatus(formId, formStatus);
    }

    // 删除表单
    @Log(title = "删除表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/delForm/{formId}")
    public void delForm(@PathVariable Integer formId) {
        oaFormDefinitionService.delForm(formId);
    }

    // 表单管理-分页
    @GetMapping("/manageForm")
    public ApiResult<CommonPage<OaFormDefinition>> manageForm(@RequestParam(value = "formName", required = false) String formName,
                                                              @RequestParam(value = "formId", required = false) Integer formId,
                                                              @RequestParam(value = "formGroup", required = false) Integer formGroup,
                                                              @RequestParam(value = "formStatus", required = false) Integer formStatus,
                                                              @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        return ApiResult.success(oaFormDefinitionService.pageList(formId, formName, formGroup, formStatus, pageNum, pageSize));
    }

    // 内置单据枚举列表
    @GetMapping("/builtInFormEnums")
    public List<OaDefinitionDocumentInfo> getBuiltInDocumentList(String sign, String name) {
        return Arrays.stream(OaBuiltInDocument.values())
                .map(doc -> new OaDefinitionDocumentInfo(doc.getSign(), doc.getName()))
                .filter(docInfo -> (sign == null || docInfo.getSign().equals(sign))
                        && (name == null || docInfo.getName().contains(name)))
                .collect(Collectors.toList());
    }

    // 我的申请-依据权限展示表单
    @GetMapping("/myForm")
    public List<OaFormDefinition> myForm(String formSign) {
        return oaFormDefinitionService.selectByScope(formSign);
    }

    // 表单名称列表
    @GetMapping("/getFormName")
    public List<OaFormNameResponseVO> getFormName(@RequestParam(value = "formSign", required = false) String formSign,
                                                  @RequestParam(value = "formName", required = false) String formName,
                                                  @RequestParam(value = "formId", required = false) Integer formId) {
        return oaFormDefinitionService.getFormName(formSign, formName, formId);
    }


}

