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.DictionarySampleTypePageReqVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleTypeRespVO;
import com.zt.plat.module.qms.business.dic.controller.vo.DictionarySampleTypeSaveReqVO;
import com.zt.plat.module.qms.business.dic.dal.dataobject.DictionarySampleTypeDO;
import com.zt.plat.module.qms.business.dic.service.DictionarySampleTypeService;
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-type")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class DictionarySampleTypeController implements BusinessControllerMarker {


    @Resource
    private DictionarySampleTypeService dictionarySampleTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建样品类型字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:create')")
    public CommonResult<DictionarySampleTypeRespVO> createDictionarySampleType(@Valid @RequestBody DictionarySampleTypeSaveReqVO createReqVO) {
        return success(dictionarySampleTypeService.createDictionarySampleType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新样品类型字典")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:update')")
    public CommonResult<Boolean> updateDictionarySampleType(@Valid @RequestBody DictionarySampleTypeSaveReqVO updateReqVO) {
        dictionarySampleTypeService.updateDictionarySampleType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除样品类型字典")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:delete')")
    public CommonResult<Boolean> deleteDictionarySampleType(@RequestParam("id") Long id) {
        dictionarySampleTypeService.deleteDictionarySampleType(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除样品类型字典")
                //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:delete')")
    public CommonResult<Boolean> deleteDictionarySampleTypeList(@RequestBody BatchDeleteReqVO req) {
        dictionarySampleTypeService.deleteDictionarySampleTypeListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得样品类型字典")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:query')")
    public CommonResult<DictionarySampleTypeRespVO> getDictionarySampleType(@RequestParam("id") Long id) {
        DictionarySampleTypeDO dictionarySampleType = dictionarySampleTypeService.getDictionarySampleType(id);
        return success(BeanUtils.toBean(dictionarySampleType, DictionarySampleTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得样品类型字典分页")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:query')")
    public CommonResult<PageResult<DictionarySampleTypeRespVO>> getDictionarySampleTypePage(@Valid DictionarySampleTypePageReqVO pageReqVO) {
        PageResult<DictionarySampleTypeDO> pageResult = dictionarySampleTypeService.getDictionarySampleTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DictionarySampleTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出样品类型字典 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:dictionary-sample-type:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDictionarySampleTypeExcel(@Valid DictionarySampleTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DictionarySampleTypeDO> list = dictionarySampleTypeService.getDictionarySampleTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "样品类型字典.xls", "数据", DictionarySampleTypeRespVO.class,
                        BeanUtils.toBean(list, DictionarySampleTypeRespVO.class));
    }

}