package com.zt.plat.module.qms.business.dic.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.module.qms.business.dic.controller.vo.DictionarySampleFlowNodePageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleFlowNodeRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleFlowNodeSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionarySampleFlowNodeDO;
import com.zt.plat.module.qms.business.dic.service.DictionarySampleFlowNodeService;
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/dictionary-sample-flow-node")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class DictionarySampleFlowNodeController implements BusinessControllerMarker {


    @Resource
    private DictionarySampleFlowNodeService dictionarySampleFlowNodeService;

    @PostMapping("/create")
    @Operation(summary = "创建样品流程节点字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:create')")
    public CommonResult<DictionarySampleFlowNodeRespVO> createDictionarySampleFlowNode(@Valid @RequestBody DictionarySampleFlowNodeSaveReqVO createReqVO) {
        return success(dictionarySampleFlowNodeService.createDictionarySampleFlowNode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品流程节点字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:update')")
    public CommonResult<Boolean> updateDictionarySampleFlowNode(@Valid @RequestBody DictionarySampleFlowNodeSaveReqVO updateReqVO) {
        dictionarySampleFlowNodeService.updateDictionarySampleFlowNode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品流程节点字典")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:delete')")
    public CommonResult<Boolean> deleteDictionarySampleFlowNode(@RequestParam("id") Long id) {
        dictionarySampleFlowNodeService.deleteDictionarySampleFlowNode(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品流程节点字典")
                //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:delete')")
    public CommonResult<Boolean> deleteDictionarySampleFlowNodeList(@RequestBody BatchDeleteReqVO req) {
        dictionarySampleFlowNodeService.deleteDictionarySampleFlowNodeListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品流程节点字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:query')")
    public CommonResult<DictionarySampleFlowNodeRespVO> getDictionarySampleFlowNode(@RequestParam("id") Long id) {
        DictionarySampleFlowNodeDO dictionarySampleFlowNode = dictionarySampleFlowNodeService.getDictionarySampleFlowNode(id);
        return success(BeanUtils.toBean(dictionarySampleFlowNode, DictionarySampleFlowNodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品流程节点字典分页")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:query')")
    public CommonResult<PageResult<DictionarySampleFlowNodeRespVO>> getDictionarySampleFlowNodePage(@Valid DictionarySampleFlowNodePageReqVO pageReqVO) {
        PageResult<DictionarySampleFlowNodeDO> pageResult = dictionarySampleFlowNodeService.getDictionarySampleFlowNodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictionarySampleFlowNodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品流程节点字典 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-flow-node:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictionarySampleFlowNodeExcel(@Valid DictionarySampleFlowNodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictionarySampleFlowNodeDO> list = dictionarySampleFlowNodeService.getDictionarySampleFlowNodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品流程节点字典.xls", "数据", DictionarySampleFlowNodeRespVO.class,
                        BeanUtils.toBean(list, DictionarySampleFlowNodeRespVO.class));
    }

}