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

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.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessDetailPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessDetailRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProcessDetailSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpProcessDetailDO;
import com.zt.plat.module.erp.service.erp.ErpProcessDetailService;
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 = "管理后台 - ERP工艺路线明细")
@RestController
@RequestMapping("/base/erp-process-detail")
@Validated
public class ErpProcessDetailController {


    @Resource
    private ErpProcessDetailService erpProcessDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP工艺路线明细")
    @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:create')")
    public CommonResult<ErpProcessDetailRespVO> createErpProcessDetail(@Valid @RequestBody ErpProcessDetailSaveReqVO createReqVO) {
        return success(erpProcessDetailService.createErpProcessDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP工艺路线明细")
    @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:update')")
    public CommonResult<Boolean> updateErpProcessDetail(@Valid @RequestBody ErpProcessDetailSaveReqVO updateReqVO) {
        erpProcessDetailService.updateErpProcessDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP工艺路线明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:delete')")
    public CommonResult<Boolean> deleteErpProcessDetail(@RequestParam("id") Long id) {
        erpProcessDetailService.deleteErpProcessDetail(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP工艺路线明细")
                @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:delete')")
    public CommonResult<Boolean> deleteErpProcessDetailList(@RequestBody BatchDeleteReqVO req) {
        erpProcessDetailService.deleteErpProcessDetailListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP工艺路线明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:query')")
    public CommonResult<ErpProcessDetailRespVO> getErpProcessDetail(@RequestParam("id") Long id) {
        ErpProcessDetailDO erpProcessDetail = erpProcessDetailService.getErpProcessDetail(id);
        return success(BeanUtils.toBean(erpProcessDetail, ErpProcessDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP工艺路线明细分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:query')")
    public CommonResult<PageResult<ErpProcessDetailRespVO>> getErpProcessDetailPage(@Valid ErpProcessDetailPageReqVO pageReqVO) {
        PageResult<ErpProcessDetailDO> pageResult = erpProcessDetailService.getErpProcessDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpProcessDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP工艺路线明细 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-process-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpProcessDetailExcel(@Valid ErpProcessDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProcessDetailDO> list = erpProcessDetailService.getErpProcessDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP工艺路线明细.xls", "数据", ErpProcessDetailRespVO.class,
                        BeanUtils.toBean(list, ErpProcessDetailRespVO.class));
    }

}