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.DictionaryParameterPageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryParameterRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionaryParameterSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionaryParameterDO;
import com.zt.plat.module.qms.business.dic.service.DictionaryParameterService;
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-parameter")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class DictionaryParameterController implements BusinessControllerMarker {


    @Resource
    private DictionaryParameterService dictionaryParameterService;

    @PostMapping("/create")
    @Operation(summary = "创建检测参数字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-parameter:create')")
    public CommonResult<DictionaryParameterRespVO> createDictionaryParameter(@Valid @RequestBody DictionaryParameterSaveReqVO createReqVO) {
        return success(dictionaryParameterService.createDictionaryParameter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测参数字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-parameter:update')")
    public CommonResult<Boolean> updateDictionaryParameter(@Valid @RequestBody DictionaryParameterSaveReqVO updateReqVO) {
        dictionaryParameterService.updateDictionaryParameter(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得检测参数字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-parameter:query')")
    public CommonResult<DictionaryParameterRespVO> getDictionaryParameter(@RequestParam("id") Long id) {
        DictionaryParameterDO dictionaryParameter = dictionaryParameterService.getDictionaryParameter(id);
        return success(BeanUtils.toBean(dictionaryParameter, DictionaryParameterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测参数字典分页")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-parameter:query')")
    public CommonResult<PageResult<DictionaryParameterRespVO>> getDictionaryParameterPage(@Valid DictionaryParameterPageReqVO pageReqVO) {
        PageResult<DictionaryParameterDO> pageResult = dictionaryParameterService.getDictionaryParameterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictionaryParameterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测参数字典 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-parameter:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictionaryParameterExcel(@Valid DictionaryParameterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictionaryParameterDO> list = dictionaryParameterService.getDictionaryParameterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测参数字典.xls", "数据", DictionaryParameterRespVO.class,
                        BeanUtils.toBean(list, DictionaryParameterRespVO.class));
    }

}