package com.zt.plat.module.base.controller.admin.base;

import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
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.excel.core.util.ExcelUtils;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialInfomationPageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialInfomationRespVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialInfomationSaveReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialInfomationSimplePageReqVO;
import com.zt.plat.module.base.controller.admin.base.vo.MaterialInfomationSimpleRespVO;
import com.zt.plat.module.base.service.base.MaterialInfomationService;
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("/base/material-infomation")
@Validated
public class MaterialInfomationController {


    @Resource
    private MaterialInfomationService materialInfomationService;

/*    @PostMapping("/create")
    @Operation(summary = "创建物料信息")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:create')")
    public CommonResult<MaterialInfomationRespVO> createMaterialInfomation(@Valid @RequestBody MaterialInfomationSaveReqVO createReqVO) {
        return success(materialInfomationService.createMaterialInfomation(createReqVO));
    }*/

    @Operation(summary = "快捷添加实物物料")
    @PostMapping("/creat-quickly")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:create')")
    public CommonResult<String> createMaterialInfomationQuickly(@Valid @RequestBody MaterialInfomationSaveReqVO createReqVO) {
        String result = materialInfomationService.createMaterialInfomationQuickly(createReqVO);
        return success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料信息")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:update')")
    public CommonResult<Boolean> updateMaterialInfomation(@Valid @RequestBody MaterialInfomationSaveReqVO updateReqVO) {
        materialInfomationService.updateMaterialInfomation(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:material-infomation:delete')")
    public CommonResult<Boolean> deleteMaterialInfomation(@RequestParam("id") Long id) {
        materialInfomationService.deleteMaterialInfomation(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料信息")
                @PreAuthorize("@ss.hasPermission('base:material-infomation:delete')")
    public CommonResult<Boolean> deleteMaterialInfomationList(@RequestBody BatchDeleteReqVO req) {
        materialInfomationService.deleteMaterialInfomationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:query')")
    public CommonResult<MaterialInfomationRespVO> getMaterialInfomation(@RequestParam("id") Long id) {
    MaterialInfomationRespVO materialInfomation = materialInfomationService.getMaterialInfomation(id);
    return success(materialInfomation);
    }

    @GetMapping("/list-by-ids")
    @Operation(summary = "按 ID 批量获得物料信息")
    @Parameter(name = "ids", description = "编号集合", required = true)
    @PreAuthorize("@ss.hasPermission('base:material-infomation:query')")
    public CommonResult<List<MaterialInfomationRespVO>> getMaterialInfomationListByIds(@RequestParam("ids") List<Long> ids) {
        return success(materialInfomationService.getMaterialInfomationListByIds(ids));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料信息分页")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:query')")
    public CommonResult<PageResult<MaterialInfomationRespVO>> getMaterialInfomationPage(@Valid MaterialInfomationPageReqVO pageReqVO) {
    PageResult<MaterialInfomationRespVO> pageResult = materialInfomationService.getMaterialInfomationPage(pageReqVO);
    return success(pageResult);
    }

    @GetMapping("/simple-page")
    @Operation(summary = "获得物料信息精简分页")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:query')")
    public CommonResult<PageResult<MaterialInfomationSimpleRespVO>> getMaterialInfomationSimplePage(@Valid MaterialInfomationSimplePageReqVO pageReqVO) {
        return success(materialInfomationService.getMaterialInfomationSimplePage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料信息 Excel")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialInfomationExcel(@Valid MaterialInfomationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    List<MaterialInfomationRespVO> list = materialInfomationService.getMaterialInfomationPage(pageReqVO).getList();
    // 导出 Excel
    ExcelUtils.write(response, "物料信息.xls", "数据", MaterialInfomationRespVO.class,
            list);
    }

    @GetMapping("/getOneTest")
    @Operation(summary = "测试获取生产版本")
    @PreAuthorize("@ss.hasPermission('base:material-infomation:query')")
    public CommonResult<String> getTest() {
        String getOneTest = materialInfomationService.getOneTest();
        return success(getOneTest);
    }

}