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.DictionaryProjectPageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryProjectRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryProjectSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryProjectDO;
import com.zt.plat.module.qms.business.dic.service.DictionaryProjectService;
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-project")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class DictionaryProjectController implements BusinessControllerMarker {


    @Resource
    private DictionaryProjectService dictionaryProjectService;

    @PostMapping("/create")
    @Operation(summary = "创建检测项目字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:create')")
    public CommonResult<DictionaryProjectRespVO> createDictionaryProject(@Valid @RequestBody DictionaryProjectSaveReqVO createReqVO) {
        return success(dictionaryProjectService.createDictionaryProject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测项目字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:update')")
    public CommonResult<Boolean> updateDictionaryProject(@Valid @RequestBody DictionaryProjectSaveReqVO updateReqVO) {
        dictionaryProjectService.updateDictionaryProject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检测项目字典")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:delete')")
    public CommonResult<Boolean> deleteDictionaryProject(@RequestParam("id") Long id) {
        dictionaryProjectService.deleteDictionaryProject(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除检测项目字典")
                //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:delete')")
    public CommonResult<Boolean> deleteDictionaryProjectList(@RequestBody BatchDeleteReqVO req) {
        dictionaryProjectService.deleteDictionaryProjectListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检测项目字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:query')")
    public CommonResult<DictionaryProjectRespVO> getDictionaryProject(@RequestParam("id") Long id) {
        DictionaryProjectDO dictionaryProject = dictionaryProjectService.getDictionaryProject(id);
        return success(BeanUtils.toBean(dictionaryProject, DictionaryProjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测项目字典分页")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:query')")
    public CommonResult<PageResult<DictionaryProjectRespVO>> getDictionaryProjectPage(@Valid DictionaryProjectPageReqVO pageReqVO) {
        PageResult<DictionaryProjectDO> pageResult = dictionaryProjectService.getDictionaryProjectPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictionaryProjectRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得检测项目字典列表")
    public CommonResult<?> getDictionaryProjectList(DictionaryProjectPageReqVO reqVO) {
    	List<DictionaryProjectDO> list = dictionaryProjectService.getDictionaryProjectList(reqVO);
        return success(BeanUtils.toBean(list, DictionaryProjectRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测项目字典 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-project:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictionaryProjectExcel(@Valid DictionaryProjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictionaryProjectDO> list = dictionaryProjectService.getDictionaryProjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测项目字典.xls", "数据", DictionaryProjectRespVO.class,
                        BeanUtils.toBean(list, DictionaryProjectRespVO.class));
    }

}