package com.zt.plat.module.qms.resource.record.controller.admin.recordassign;

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.resource.record.controller.admin.recordassign.vo.RecordAssignPageReqVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordassign.vo.RecordAssignRespVO;
import com.zt.plat.module.qms.resource.record.controller.admin.recordassign.vo.RecordAssignSaveReqVO;
import com.zt.plat.module.qms.resource.record.dal.dataobject.recordassign.RecordAssignDO;
import com.zt.plat.module.qms.resource.record.service.recordassign.RecordAssignService;
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/resource/record-assign")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.recordassign")
public class RecordAssignController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private RecordAssignService recordAssignService;

    @PostMapping("/create")
    @Operation(summary = "创建文件记录分发号")
//    @PreAuthorize("@ss.hasPermission('qms:record-assign:create')")
    public CommonResult<RecordAssignRespVO> createRecordAssign(@Valid @RequestBody RecordAssignSaveReqVO createReqVO) {
        return success(recordAssignService.createRecordAssign(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新文件记录分发号")
//    @PreAuthorize("@ss.hasPermission('qms:record-assign:update')")
    public CommonResult<Boolean> updateRecordAssign(@Valid @RequestBody RecordAssignSaveReqVO updateReqVO) {
        recordAssignService.updateRecordAssign(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除文件记录分发号")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:record-assign:delete')")
    public CommonResult<Boolean> deleteRecordAssign(@RequestParam("id") Long id) {
        recordAssignService.deleteRecordAssign(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除文件记录分发号")
//                @PreAuthorize("@ss.hasPermission('qms:record-assign:delete')")
    public CommonResult<Boolean> deleteRecordAssignList(@RequestBody BatchDeleteReqVO req) {
        recordAssignService.deleteRecordAssignListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得文件记录分发号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:record-assign:query')")
    public CommonResult<RecordAssignRespVO> getRecordAssign(@RequestParam("id") Long id) {
        RecordAssignDO recordAssign = recordAssignService.getRecordAssign(id);
        return success(BeanUtils.toBean(recordAssign, RecordAssignRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得文件记录分发号")
//    @PreAuthorize("@ss.hasPermission('qms:record-assign:query')")
    public CommonResult<PageResult<RecordAssignRespVO>> getRecordAssignPage(@Valid RecordAssignPageReqVO pageReqVO) {
        PageResult<RecordAssignDO> pageResult = recordAssignService.getRecordAssignPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordAssignRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出文件记录分发号")
//    @PreAuthorize("@ss.hasPermission('qms:record-assign:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordAssignExcel(@Valid RecordAssignPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordAssignDO> list = recordAssignService.getRecordAssignPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "文件记录分发号,记录文件分发与线下对应的分发号对应，且标记到目标人物.xls", "数据", RecordAssignRespVO.class,
                        BeanUtils.toBean(list, RecordAssignRespVO.class));
    }

}