package com.zt.plat.module.qms.resource.material.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.resource.material.controller.vo.MaterialInventoryCheckItemPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckItemRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryCheckItemSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryCheckItemDO;
import com.zt.plat.module.qms.resource.material.service.MaterialInventoryCheckItemService;
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/material-inventory-check-item")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialInventoryCheckItemController implements BusinessControllerMarker {


    @Resource
    private MaterialInventoryCheckItemService materialInventoryCheckItemService;

    @PostMapping("/create")
    @Operation(summary = "创建库存盘点项")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:create')")
    public CommonResult<MaterialInventoryCheckItemRespVO> createMaterialInventoryCheckItem(@Valid @RequestBody MaterialInventoryCheckItemSaveReqVO createReqVO) {
        return success(materialInventoryCheckItemService.createMaterialInventoryCheckItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存盘点项")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:update')")
    public CommonResult<Boolean> updateMaterialInventoryCheckItem(@Valid @RequestBody MaterialInventoryCheckItemSaveReqVO updateReqVO) {
        materialInventoryCheckItemService.updateMaterialInventoryCheckItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存盘点项")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:delete')")
    public CommonResult<Boolean> deleteMaterialInventoryCheckItem(@RequestParam("id") Long id) {
        materialInventoryCheckItemService.deleteMaterialInventoryCheckItem(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除库存盘点项")
                @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:delete')")
    public CommonResult<Boolean> deleteMaterialInventoryCheckItemList(@RequestBody BatchDeleteReqVO req) {
        materialInventoryCheckItemService.deleteMaterialInventoryCheckItemListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存盘点项")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:query')")
    public CommonResult<MaterialInventoryCheckItemRespVO> getMaterialInventoryCheckItem(@RequestParam("id") Long id) {
        MaterialInventoryCheckItemDO materialInventoryCheckItem = materialInventoryCheckItemService.getMaterialInventoryCheckItem(id);
        return success(BeanUtils.toBean(materialInventoryCheckItem, MaterialInventoryCheckItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得库存盘点项分页")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:query')")
    public CommonResult<PageResult<MaterialInventoryCheckItemRespVO>> getMaterialInventoryCheckItemPage(@Valid MaterialInventoryCheckItemPageReqVO pageReqVO) {
        PageResult<MaterialInventoryCheckItemDO> pageResult = materialInventoryCheckItemService.getMaterialInventoryCheckItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialInventoryCheckItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存盘点项 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-check-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialInventoryCheckItemExcel(@Valid MaterialInventoryCheckItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialInventoryCheckItemDO> list = materialInventoryCheckItemService.getMaterialInventoryCheckItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存盘点项.xls", "数据", MaterialInventoryCheckItemRespVO.class,
                        BeanUtils.toBean(list, MaterialInventoryCheckItemRespVO.class));
    }

}