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.MaterialInventoryInboundDetailPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryInboundDetailSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryInboundDetailDO;
import com.zt.plat.module.qms.resource.material.service.MaterialInventoryInboundDetailService;
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-detail")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialInventoryInboundDetailController implements BusinessControllerMarker {


    @Resource
    private MaterialInventoryInboundDetailService materialInventoryInboundDetailService;

//    @PostMapping("/create")
    @Operation(summary = "创建入库明细,验收入库、盘盈入库等")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:create')")
    public CommonResult<MaterialInventoryInboundDetailRespVO> createMaterialInventoryInboundDetail(@Valid @RequestBody MaterialInventoryInboundDetailSaveReqVO createReqVO) {
        return success(materialInventoryInboundDetailService.createMaterialInventoryInboundDetail(createReqVO));
    }

//    @PutMapping("/update")
    @Operation(summary = "更新入库明细,验收入库、盘盈入库等")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:update')")
    public CommonResult<Boolean> updateMaterialInventoryInboundDetail(@Valid @RequestBody MaterialInventoryInboundDetailSaveReqVO updateReqVO) {
        materialInventoryInboundDetailService.updateMaterialInventoryInboundDetail(updateReqVO);
        return success(true);
    }

//    @DeleteMapping("/delete")
    @Operation(summary = "删除入库明细,验收入库、盘盈入库等")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:delete')")
    public CommonResult<Boolean> deleteMaterialInventoryInboundDetail(@RequestParam("id") Long id) {
        materialInventoryInboundDetailService.deleteMaterialInventoryInboundDetail(id);
        return success(true);
    }

//    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除入库明细,验收入库、盘盈入库等")
                @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:delete')")
    public CommonResult<Boolean> deleteMaterialInventoryInboundDetailList(@RequestBody BatchDeleteReqVO req) {
        materialInventoryInboundDetailService.deleteMaterialInventoryInboundDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得入库明细,验收入库、盘盈入库等")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:query')")
    public CommonResult<MaterialInventoryInboundDetailRespVO> getMaterialInventoryInboundDetail(@RequestParam("id") Long id) {
        MaterialInventoryInboundDetailDO materialInventoryInboundDetail = materialInventoryInboundDetailService.getMaterialInventoryInboundDetail(id);
        return success(BeanUtils.toBean(materialInventoryInboundDetail, MaterialInventoryInboundDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得入库明细,验收入库、盘盈入库等分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:query')")
    public CommonResult<PageResult<MaterialInventoryInboundDetailRespVO>> getMaterialInventoryInboundDetailPage(@Valid MaterialInventoryInboundDetailPageReqVO pageReqVO) {
        PageResult<MaterialInventoryInboundDetailRespVO> pageResult = materialInventoryInboundDetailService.getInventoryInboundDetailPageWithMaterialInfo(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "根据入库单id获取物料列表")
    @Parameter(name = "inboundId", description = "入库单id", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:query')")
    public CommonResult<List<MaterialInventoryInboundDetailRespVO>> getMaterialInfoListByInboundId(@RequestParam("inboundId") Long inboundId) {
        List<MaterialInventoryInboundDetailRespVO> materialInfoList = materialInventoryInboundDetailService.getMaterialInfoListByInboundId(inboundId);
        return success(materialInfoList);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出入库明细,验收入库、盘盈入库等 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-inbound-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialInventoryInboundDetailExcel(@Valid MaterialInventoryInboundDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialInventoryInboundDetailDO> list = materialInventoryInboundDetailService.getMaterialInventoryInboundDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "入库明细,验收入库、盘盈入库等.xls", "数据", MaterialInventoryInboundDetailRespVO.class,
                        BeanUtils.toBean(list, MaterialInventoryInboundDetailRespVO.class));
    }

}