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.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
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.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.core.constant.DataTypeConstant;
import com.zt.plat.module.qms.resource.material.controller.vo.*;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialProductPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialProductRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialProductSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialProductDO;
import com.zt.plat.module.qms.resource.material.service.MaterialProductService;
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-product")
@Validated
@FileUploadController(source = "resource.materialproduct")
public class MaterialProductController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = MaterialProductController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
        setFileUploadInfo(annotation);
        }
    }

    @Resource
    private MaterialProductService materialProductService;

    @PostMapping("/saveData")
    @Operation(summary = "保存数据")
    public CommonResult<MaterialProductRespVO> saveData(@Valid @RequestBody MaterialProductSaveReqVO createReqVO) {
        return materialProductService.saveData(createReqVO);
    }

    @PostMapping("/saveClassify")
    @Operation(summary = "保存分类")
    public CommonResult<MaterialProductRespVO> saveClassify(@Valid @RequestBody MaterialProductSaveReqVO createReqVO) {
        return materialProductService.saveCategory(createReqVO);
    }

    @GetMapping("/getTreeData")
    @Operation(summary = "查询分类树")
    public CommonResult<List<MaterialProductRespVO>> getTreeData() {
        List<MaterialProductDO> list = materialProductService.getTreeData(DataTypeConstant.DATA_TYPE_CATEGORY);
        return success(BeanUtils.toBean(list, MaterialProductRespVO.class));
    }

    @GetMapping("/getProductTreeData")
    @Operation(summary = "获取分类和产品树")
    public CommonResult<List<MaterialProductRespVO>> getProductTreeData() {
        List<MaterialProductDO> list = materialProductService.getTreeData("");
        return success(BeanUtils.toBean(list, MaterialProductRespVO.class));
    }
    
    @PostMapping("/create")
    @Operation(summary = "创建物料大类")
    @PreAuthorize("@ss.hasPermission('resource:material-product:create')")
    public CommonResult<MaterialProductRespVO> createMaterialProduct(@Valid @RequestBody MaterialProductSaveReqVO createReqVO) {
        return success(materialProductService.createMaterialProduct(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新物料大类")
    @PreAuthorize("@ss.hasPermission('resource:material-product:update')")
    public CommonResult<Boolean> updateMaterialProduct(@Valid @RequestBody MaterialProductSaveReqVO updateReqVO) {
        materialProductService.updateMaterialProduct(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除物料大类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('resource:material-product:delete')")
    public CommonResult<Boolean> deleteMaterialProduct(@RequestParam("id") Long id) {
        materialProductService.deleteMaterialProduct(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除物料大类")
                @PreAuthorize("@ss.hasPermission('resource:material-product:delete')")
    public CommonResult<Boolean> deleteMaterialProductList(@RequestBody BatchDeleteReqVO req) {
        materialProductService.deleteMaterialProductListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得物料大类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('resource:material-product:query')")
    public CommonResult<MaterialProductRespVO> getMaterialProduct(@RequestParam("id") Long id) {
        MaterialProductDO materialProduct = materialProductService.getMaterialProduct(id);
        return success(BeanUtils.toBean(materialProduct, MaterialProductRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料大类分页")
    @PreAuthorize("@ss.hasPermission('resource:material-product:query')")
    public CommonResult<PageResult<MaterialProductRespVO>> getMaterialProductPage(@Valid MaterialProductPageReqVO pageReqVO) {
        PageResult<MaterialProductDO> pageResult = materialProductService.getMaterialProductPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialProductRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料大类 Excel")
    @PreAuthorize("@ss.hasPermission('resource:material-product:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialProductExcel(@Valid MaterialProductPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialProductDO> list = materialProductService.getMaterialProductPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料大类.xls", "数据", MaterialProductRespVO.class,
                        BeanUtils.toBean(list, MaterialProductRespVO.class));
    }

}