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.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundPageReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundRespVO;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialInventoryOutboundSaveReqVO;
import com.zt.plat.module.qms.resource.material.controller.vo.export.MaterialConsumeStatisticsExportVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialInventoryOutboundDO;
import com.zt.plat.module.qms.resource.material.service.MaterialInventoryOutboundService;
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-outbound")
@Validated
@FileUploadController(source = "qms.materialinventoryoutbound")
@DeptDataPermissionIgnore(enable = "true")
public class MaterialInventoryOutboundController extends AbstractFileUploadController implements BusinessControllerMarker{

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

    @Resource
    private MaterialInventoryOutboundService materialInventoryOutboundService;

    @PostMapping("/add")
    @Operation(summary = "新增物料出库")
//    @PreAuthorize("@ss.hasPermission('qms:material-inventory-outbound:create')")
    public CommonResult<MaterialInventoryOutboundRespVO> createMaterialInventoryOutbound(@Valid @RequestBody MaterialInventoryOutboundSaveReqVO createReqVO) {
        return success(materialInventoryOutboundService.createMaterialInventoryOutbound(createReqVO));
    }

//    @PutMapping("/update")
    @Operation(summary = "更新物料出库")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-outbound:update')")
    public CommonResult<Boolean> updateMaterialInventoryOutbound(@Valid @RequestBody MaterialInventoryOutboundSaveReqVO updateReqVO) {
        materialInventoryOutboundService.updateMaterialInventoryOutbound(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得物料出库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-outbound:query')")
    public CommonResult<MaterialInventoryOutboundRespVO> getMaterialInventoryOutbound(@RequestParam("id") Long id) {
        MaterialInventoryOutboundDO materialInventoryOutbound = materialInventoryOutboundService.getMaterialInventoryOutbound(id);
        return success(BeanUtils.toBean(materialInventoryOutbound, MaterialInventoryOutboundRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得物料出库分页")
//    @PreAuthorize("@ss.hasPermission('qms:material-inventory-outbound:query')")
    public CommonResult<PageResult<MaterialInventoryOutboundRespVO>> getMaterialInventoryOutboundPage(@Valid MaterialInventoryOutboundPageReqVO pageReqVO) {
        PageResult<MaterialInventoryOutboundDO> pageResult = materialInventoryOutboundService.getMaterialInventoryOutboundPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialInventoryOutboundRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出物料出库 Excel")
    @PreAuthorize("@ss.hasPermission('qms:material-inventory-outbound:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialInventoryOutboundExcel(@Valid MaterialInventoryOutboundPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialInventoryOutboundDO> list = materialInventoryOutboundService.getMaterialInventoryOutboundPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "物料出库.xls", "数据", MaterialInventoryOutboundRespVO.class,
                        BeanUtils.toBean(list, MaterialInventoryOutboundRespVO.class));
    }

    @GetMapping("/consume-statistics")
    @Operation(summary = "按部门消耗统计")
    public CommonResult<List<MaterialInventoryOutboundRespVO>> getUsageOutboundStatisticsByDept(@Valid MaterialInventoryOutboundPageReqVO reqVO) {
        List<MaterialInventoryOutboundRespVO> outboundRespVOS = materialInventoryOutboundService.getUsageOutboundStatisticsByDept(reqVO);
        return success(outboundRespVOS);
    }

    @GetMapping("/consume-statistics/export-excel")
    @Operation(summary = "导出按部门消耗统计 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUsageOutboundStatisticsExcel(@Valid MaterialInventoryOutboundPageReqVO reqVO,
                                                   HttpServletResponse response) throws IOException {
        List<MaterialInventoryOutboundRespVO> list = materialInventoryOutboundService.getUsageOutboundStatisticsByDept(reqVO);
        // 导出 Excel
        ExcelUtils.write(response, "物料消耗统计.xls", "数据", MaterialConsumeStatisticsExportVO.class,
                BeanUtils.toBean(list, MaterialConsumeStatisticsExportVO.class));

    }

}