package com.zt.plat.module.qms.business.reportdoc.controller.admin;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
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.framework.security.core.LoginUser;
import com.zt.plat.framework.security.core.util.SecurityFrameworkUtils;
import com.zt.plat.module.qms.business.config.controller.vo.ConfigPermissionRespVO;
import com.zt.plat.module.qms.business.config.dal.dataobject.ConfigPermissionDO;
import com.zt.plat.module.qms.business.config.service.ConfigPermissionService;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.*;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentTypeDO;
import com.zt.plat.module.qms.business.reportdoc.service.ReportDocumentTypeService;
import com.zt.plat.module.qms.enums.QmsPermissionConstant;
import com.zt.plat.module.system.api.permission.PermissionApi;
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 java.util.Set;

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/report-document-type")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class ReportDocumentTypeController implements BusinessControllerMarker {


    @Resource private ReportDocumentTypeService reportDocumentTypeService;
    @Resource private ConfigPermissionService configPermissionService;
    @Resource private PermissionApi permissionApi;

    @PostMapping("/save")
    @Operation(summary = "更新报告类型配置")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:update')")
    public CommonResult<String> updateReportDocumentType(@Valid @RequestBody ReportDocumentTypeSaveReqVO updateReqVO) {
        return reportDocumentTypeService.save(updateReqVO);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报告类型配置")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:delete')")
    public CommonResult<Boolean> deleteReportDocumentType(@RequestParam("id") Long id) {
        reportDocumentTypeService.deleteReportDocumentType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报告类型配置")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:delete')")
    public CommonResult<Boolean> deleteReportDocumentTypeList(@RequestBody BatchDeleteReqVO req) {
        reportDocumentTypeService.deleteReportDocumentTypeListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报告类型配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:query')")
    public CommonResult<ReportDocumentTypeRespVO> getReportDocumentType(@RequestParam("id") Long id) {
        ReportDocumentTypeDO reportDocumentTypeDO = reportDocumentTypeService.getReportDocumentType(id);
        Long mainId = reportDocumentTypeDO.getMainId();
        List<ConfigPermissionDO> permissionList = configPermissionService.getPermissionList(mainId, QmsPermissionConstant.REPORT_DOCUMENT_TYPE_START, "");
        List<ConfigPermissionRespVO> permissionRespVOList= BeanUtils.toBean(permissionList, ConfigPermissionRespVO.class);
        ReportDocumentTypeRespVO reportDocumentTypeRespVO = BeanUtils.toBean(reportDocumentTypeDO, ReportDocumentTypeRespVO.class);
        reportDocumentTypeRespVO.setPermissionList(permissionRespVOList);
        return success(reportDocumentTypeRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得报告类型配置分页")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:query')")
    public CommonResult<PageResult<ReportDocumentTypeRespVO>> getReportDocumentTypePage(@Valid ReportDocumentTypePageReqVO pageReqVO) {
        PageResult<ReportDocumentTypeDO> pageResult = reportDocumentTypeService.getReportDocumentTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportDocumentTypeRespVO.class));
    }

    @GetMapping("/selectListWithPermission")
    @Operation(summary = "安权限查询报告类型")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:query')")
    public CommonResult<List<ReportDocumentTypeRespVO>> selectListWithPermission(@Valid ReportDocumentTypePageReqVO pageReqVO) {
        pageReqVO.setPermissionFilterFlag("1");
        pageReqVO.setSrcPermissionType(QmsPermissionConstant.REPORT_DOCUMENT_TYPE_START);

        //查询权限
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        Long userId = loginUser.getId();
        CommonResult<Set<Long>> roleRet = permissionApi.getUserRoleIdListByUserId(userId);
        Set<Long> roleIds = roleRet.getData();
        pageReqVO.setRoleIds(roleIds);
        List<ReportDocumentTypeDO> list = reportDocumentTypeService.selectListWithPermission(pageReqVO);
        return success(BeanUtils.toBean(list, ReportDocumentTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报告类型配置 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportDocumentTypeExcel(@Valid ReportDocumentTypePageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportDocumentTypeDO> list = reportDocumentTypeService.getReportDocumentTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报告类型配置.xls", "数据", ReportDocumentTypeRespVO.class,
                BeanUtils.toBean(list, ReportDocumentTypeRespVO.class));
    }

}