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

import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckDetailRespVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckPageReqVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckRespVO;
import com.zt.plat.module.base.controller.admin.erpmonthproductiveplancheck.vo.ErpMonthProductivePlanCheckSaveReqVO;
import com.zt.plat.module.base.dal.dataobject.erpmonthproductiveplancheck.ErpMonthProductivePlanCheckDO;
import com.zt.plat.module.base.service.erpmonthproductiveplancheck.ErpMonthProductivePlanCheckService;
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.constraints.*;
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 = "管理后台 - ERP月度产品计划核对")
@RestController
@RequestMapping("/base/erp-month-productive-plan-check")
@Validated
public class ErpMonthProductivePlanCheckController implements BusinessControllerMarker {


    @Resource
    private ErpMonthProductivePlanCheckService erpMonthProductivePlanCheckService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP月度产品计划核对")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:create')")
    public CommonResult<ErpMonthProductivePlanCheckRespVO> createErpMonthProductivePlanCheck(@Valid @RequestBody ErpMonthProductivePlanCheckSaveReqVO createReqVO) {
        return success(erpMonthProductivePlanCheckService.createErpMonthProductivePlanCheck(createReqVO));
    }

    @PostMapping("/create-batch")
    @Operation(summary = "创建ERP月度产品计划核对")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:create')")
    public CommonResult<?> createBatchErpMonthProductivePlanCheck(@Valid @RequestBody List<ErpMonthProductivePlanCheckSaveReqVO> createReqVOList) {
        erpMonthProductivePlanCheckService.createBatchErpMonthProductivePlanCheck(createReqVOList);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP月度产品计划核对")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:update')")
    public CommonResult<Boolean> updateErpMonthProductivePlanCheck(@Valid @RequestBody ErpMonthProductivePlanCheckSaveReqVO updateReqVO) {
        erpMonthProductivePlanCheckService.updateErpMonthProductivePlanCheck(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP月度产品计划核对")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:delete')")
    public CommonResult<Boolean> deleteErpMonthProductivePlanCheck(@RequestParam("id") Long id) {
        erpMonthProductivePlanCheckService.deleteErpMonthProductivePlanCheck(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP月度产品计划核对")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:delete')")
    public CommonResult<Boolean> deleteErpMonthProductivePlanCheckList(@RequestBody BatchDeleteReqVO req) {
        erpMonthProductivePlanCheckService.deleteErpMonthProductivePlanCheckListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP月度产品计划核对")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:query')")
    public CommonResult<ErpMonthProductivePlanCheckRespVO> getErpMonthProductivePlanCheck(@RequestParam("id") Long id) {
        ErpMonthProductivePlanCheckDO erpMonthProductivePlanCheck = erpMonthProductivePlanCheckService.getErpMonthProductivePlanCheck(id);
        ErpMonthProductivePlanCheckRespVO respVO = BeanUtils.toBean(erpMonthProductivePlanCheck, ErpMonthProductivePlanCheckRespVO.class);
        if (respVO != null) {
            respVO.setDetails(erpMonthProductivePlanCheckService.getErpMonthProductivePlanCheckDetail(id));
        }
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP月度产品计划核对分页")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:query')")
    public CommonResult<PageResult<ErpMonthProductivePlanCheckRespVO>> getErpMonthProductivePlanCheckPage(@Valid ErpMonthProductivePlanCheckPageReqVO pageReqVO) {
        PageResult<ErpMonthProductivePlanCheckDO> pageResult = erpMonthProductivePlanCheckService.getErpMonthProductivePlanCheckPage(pageReqVO);
        PageResult<ErpMonthProductivePlanCheckRespVO> result = BeanUtils.toBean(pageResult, ErpMonthProductivePlanCheckRespVO.class);
        if (result != null && result.getList() != null && !result.getList().isEmpty()){
            result.getList().forEach(erpMonthProductivePlanCheckRespVO
                    -> erpMonthProductivePlanCheckRespVO.setDetails(erpMonthProductivePlanCheckService.getErpMonthProductivePlanCheckDetail(erpMonthProductivePlanCheckRespVO.getId())));
        }
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP月度产品计划核对 Excel")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpMonthProductivePlanCheckExcel(@Valid ErpMonthProductivePlanCheckPageReqVO pageReqVO,
                                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpMonthProductivePlanCheckDO> list = erpMonthProductivePlanCheckService.getErpMonthProductivePlanCheckPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP月度产品计划核对.xls", "数据", ErpMonthProductivePlanCheckRespVO.class,
                BeanUtils.toBean(list, ErpMonthProductivePlanCheckRespVO.class));
    }

    //根据id查询明细详情
    @GetMapping("/get-detail")
    @Operation(summary = "获得ERP月度产品计划核对明细详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:erp-month-productive-plan-check:query')")
    public CommonResult<List<ErpMonthProductivePlanCheckDetailRespVO>> getErpMonthProductivePlanCheckDetail(@RequestParam("id") Long id) {
        return success(erpMonthProductivePlanCheckService.getErpMonthProductivePlanCheckDetail(id));
    }
}