package com.zt.plat.module.qms.common.data.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.common.data.controller.vo.*;
import com.zt.plat.module.qms.common.data.controller.vo.DataFormPageReqVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataFormRespVO;
import com.zt.plat.module.qms.common.data.controller.vo.DataFormSaveReqVO;
import com.zt.plat.module.qms.common.data.dal.dataobject.DataFormDO;
import com.zt.plat.module.qms.common.data.service.DataFormService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 通用数据")
@RestController
@RequestMapping("/qms/common/data/data-form")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.dataform")
public class DataFormController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = DataFormController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private DataFormService dataFormService;


    @PostMapping("/createProcessInstance")
    @Operation(summary = "示例代码：发起流程")
    public CommonResult<DataFormRespVO> createProcessInstance(@Valid @RequestBody DataFormSaveReqVO entity) {
        return success(dataFormService.createProcessInstance(entity));
    }

    @PostMapping("/create")
    @Operation(summary = "创建通用数据")
    public CommonResult<DataFormRespVO> createDataForm(@Valid @RequestBody DataFormSaveReqVO createReqVO) {
        return success(dataFormService.createDataForm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新通用数据")
    @PreAuthorize("@ss.hasPermission('qms:data-form:update')")
    public CommonResult<Boolean> updateDataForm(@Valid @RequestBody DataFormSaveReqVO updateReqVO) {
        dataFormService.updateDataForm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除通用数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:data-form:delete')")
    public CommonResult<Boolean> deleteDataForm(@RequestParam("id") Long id) {
        dataFormService.deleteDataForm(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除通用数据")
                @PreAuthorize("@ss.hasPermission('qms:data-form:delete')")
    public CommonResult<Boolean> deleteDataFormList(@RequestBody BatchDeleteReqVO req) {
        dataFormService.deleteDataFormListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得通用数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<DataFormRespVO> getDataForm(@RequestParam("id") Long id) {
        DataFormDO dataForm = dataFormService.getDataForm(id);
        return success(BeanUtils.toBean(dataForm, DataFormRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得通用数据分页")
    public CommonResult<PageResult<DataFormRespVO>> getDataFormPage(@Valid DataFormPageReqVO pageReqVO) {
        PageResult<DataFormDO> pageResult = dataFormService.getDataFormPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataFormRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出通用数据 Excel")
    @PreAuthorize("@ss.hasPermission('qms:data-form:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataFormExcel(@Valid DataFormPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataFormDO> list = dataFormService.getDataFormPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "通用数据.xls", "数据", DataFormRespVO.class,
                        BeanUtils.toBean(list, DataFormRespVO.class));
    }

}