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.MaterialInventoryInboundPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryInboundDO;
import com.zt.plat.module.qms.resource.material.service.MaterialInventoryInboundService;
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/resource/material-inventory-inbound")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialInventoryInboundController implements BusinessControllerMarker {


    @Resource
    private MaterialInventoryInboundService materialInventoryInboundService;

    @PostMapping("/add")
    @Operation(summary = "物料入库")
//    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound:create')")
    public CommonResult<MaterialInventoryInboundRespVO> createMaterialInventoryInbound(@Valid @RequestBody MaterialInventoryInboundSaveReqVO createReqVO) {
        return success(materialInventoryInboundService.createMaterialInventoryInbound(createReqVO));
    }

//    @DeleteMapping("/delete")
    @Operation(summary = "删除入库")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound:delete')")
    public CommonResult<Boolean> deleteMaterialInventoryInbound(@RequestParam("id") Long id) {
        materialInventoryInboundService.deleteMaterialInventoryInbound(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除入库")
                @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound:delete')")
    public CommonResult<Boolean> deleteMaterialInventoryInboundList(@RequestBody BatchDeleteReqVO req) {
        materialInventoryInboundService.deleteMaterialInventoryInboundListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound:query')")
    public CommonResult<MaterialInventoryInboundRespVO> getMaterialInventoryInbound(@RequestParam("id") Long id) {
        MaterialInventoryInboundDO materialInventoryInbound = materialInventoryInboundService.getMaterialInventoryInbound(id);
        return success(BeanUtils.toBean(materialInventoryInbound, MaterialInventoryInboundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入库分页")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound:query')")
    public CommonResult<PageResult<MaterialInventoryInboundRespVO>> getMaterialInventoryInboundPage(@Valid MaterialInventoryInboundPageReqVO pageReqVO) {
        PageResult<MaterialInventoryInboundDO> pageResult = materialInventoryInboundService.getMaterialInventoryInboundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialInventoryInboundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入库 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialInventoryInboundExcel(@Valid MaterialInventoryInboundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialInventoryInboundDO> list = materialInventoryInboundService.getMaterialInventoryInboundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入库.xls", "数据", MaterialInventoryInboundRespVO.class,
                        BeanUtils.toBean(list, MaterialInventoryInboundRespVO.class));
    }

}