package com.zt.plat.module.qms.resource.material.controller.admin;

import com.zt.plat.framework.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialBatchPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialBatchRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialBatchSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialBatchDO;
import com.zt.plat.module.qms.resource.material.service.MaterialBatchService;
import com.zt.plat.module.qms.resource.material.valid.AddGroup;
import com.zt.plat.module.qms.resource.material.valid.UpdateGroup;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

import com.zt.plat.framework.excel.core.util.ExcelUtils;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 物料批次")
@RestController
@RequestMapping("/qms/resource/material-batch")
@Validated
@DeptDataPermissionIgnore(enable = "true")
public class MaterialBatchController implements BusinessControllerMarker {


    @Resource
    private MaterialBatchService materialBatchService;

    @PostMapping("/create")
    @Operation(summary = "创建物料批次")
//    @PreAuthorize("@ss.hasPermission('qms:material-batch:create')")
    public CommonResult<MaterialBatchRespVO> createMaterialBatch(@Validated(AddGroup.class) @RequestBody MaterialBatchSaveReqVO createReqVO) {
        return success(materialBatchService.createMaterialBatch(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料批次")
//    @PreAuthorize("@ss.hasPermission('qms:material-batch:update')")
    public CommonResult<Boolean> updateMaterialBatch(@Validated(UpdateGroup.class) @RequestBody MaterialBatchSaveReqVO updateReqVO) {
        materialBatchService.updateMaterialBatch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料批次")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('qms:material-batch:delete')")
    public CommonResult<Boolean> deleteMaterialBatch(@RequestParam("id") Long id) {
        materialBatchService.deleteMaterialBatch(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料批次")
    @PreAuthorize("@ss.hasPermission('qms:material-batch:delete')")
    public CommonResult<Boolean> deleteMaterialBatchList(@RequestBody BatchDeleteReqVO req) {
        materialBatchService.deleteMaterialBatchListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料批次")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('qms:material-batch:query')")
    public CommonResult<MaterialBatchRespVO> getMaterialBatch(@RequestParam("id") Long id) {
        MaterialBatchDO materialBatch = materialBatchService.getMaterialBatch(id);
        return success(BeanUtils.toBean(materialBatch, MaterialBatchRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料批次分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-batch:query')")
    public CommonResult<PageResult<MaterialBatchRespVO>> getMaterialBatchPage(@Valid MaterialBatchPageReqVO pageReqVO) {
        PageResult<MaterialBatchRespVO> pageResult = materialBatchService.getBatchGongPageWithPdtInfo(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料批次 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-batch:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialBatchExcel(@Valid MaterialBatchPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialBatchDO> list = materialBatchService.getMaterialBatchPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料批次.xls", "数据", MaterialBatchRespVO.class,
                        BeanUtils.toBean(list, MaterialBatchRespVO.class));
    }

    @PostMapping("/assign-gongduan")
    @Operation(summary = "批次工段拆分")
//    @PreAuthorize("@ss.hasPermission('qms:material-batch:create')")
    public CommonResult<List<MaterialBatchRespVO>> assignMaterialBatchGongduan(@Valid @RequestBody List<MaterialBatchSaveReqVO> createReqVOs) {
        return success(materialBatchService.assignMaterialBatchGongduan(createReqVOs));
    }

    @PutMapping("/submit")
    @Operation(summary = "提交物料批次")
    @Parameter(name = "id", required = true)
    public CommonResult<Boolean> submitMaterialBatch(@RequestParam("id") Long id) {

        return success(materialBatchService.submitMaterialBatch(id));
    }

    @GetMapping("gongduan-page")
    @Operation(summary = "获取工段列表")
    @Parameter(name = "id", description = "物料批次 id", required = true, example = "1054")
    public CommonResult<List<MaterialBatchRespVO>> getMaterialBatchGongduanList(@RequestParam("id") Long id) {
        List<MaterialBatchDO> list = materialBatchService.getGongduanListByBatIds(Collections.singletonList(id));
        return success(BeanUtils.toBean(list, MaterialBatchRespVO.class));
    }

}