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.MaterialInfomationPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInfomationRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInfomationSaveReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInfomationDO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialProductDO;
import com.zt.plat.module.qms.resource.material.service.MaterialInfomationService;
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-infomation")
@Validated
@FileUploadController(source = "resource.materialinfomation")
public class MaterialInfomationController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource private MaterialInfomationService materialInfomationService;
    @Resource private MaterialProductService materialProductService;

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

    @PutMapping("/update")
    @Operation(summary = "更新物料实例")
    @PreAuthorize("@ss.hasPermission('resource: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('resource: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('resource: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('resource:material-infomation:query')")
    public CommonResult<MaterialInfomationRespVO> getMaterialInfomation(@RequestParam("id") Long id) {
        MaterialInfomationDO materialInfomation = materialInfomationService.getMaterialInfomation(id);
        return success(BeanUtils.toBean(materialInfomation, MaterialInfomationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料实例分页")
    @PreAuthorize("@ss.hasPermission('resource:material-infomation:query')")
    public CommonResult<PageResult<MaterialInfomationRespVO>> getMaterialInfomationPage(@Valid MaterialInfomationPageReqVO pageReqVO) {

        Long productId = pageReqVO.getProductId();
        if(productId != null){
            List<MaterialProductDO> productDOList = materialProductService.listByIdPath(productId, DataTypeConstant.DATA_TYPE_DATA);
            List<Long> productIds = productDOList.stream().map(MaterialProductDO::getId).toList();
            pageReqVO.setProductIds(productIds);
            pageReqVO.setProductId(null);
        }

        PageResult<MaterialInfomationDO> pageResult = materialInfomationService.getMaterialInfomationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialInfomationRespVO.class));
    }

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

}