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.ErpProductiveVersionPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveVersionRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveVersionSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpProductiveVersionDO;
import com.zt.plat.module.erp.service.erp.ErpProductiveVersionService;
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-productive-version")
@Validated
public class ErpProductiveVersionController {


    @Resource
    private ErpProductiveVersionService erpProductiveVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP生产版本")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:create')")
    public CommonResult<ErpProductiveVersionRespVO> createErpProductiveVersion(@Valid @RequestBody ErpProductiveVersionSaveReqVO createReqVO) {
        return success(erpProductiveVersionService.createErpProductiveVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP生产版本")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:update')")
    public CommonResult<Boolean> updateErpProductiveVersion(@Valid @RequestBody ErpProductiveVersionSaveReqVO updateReqVO) {
        erpProductiveVersionService.updateErpProductiveVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP生产版本")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:delete')")
    public CommonResult<Boolean> deleteErpProductiveVersion(@RequestParam("id") Long id) {
        erpProductiveVersionService.deleteErpProductiveVersion(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP生产版本")
                @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:delete')")
    public CommonResult<Boolean> deleteErpProductiveVersionList(@RequestBody BatchDeleteReqVO req) {
        erpProductiveVersionService.deleteErpProductiveVersionListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP生产版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:query')")
    public CommonResult<ErpProductiveVersionRespVO> getErpProductiveVersion(@RequestParam("id") Long id) {
        ErpProductiveVersionDO erpProductiveVersion = erpProductiveVersionService.getErpProductiveVersion(id);
        return success(BeanUtils.toBean(erpProductiveVersion, ErpProductiveVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP生产版本分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:query')")
    public CommonResult<PageResult<ErpProductiveVersionRespVO>> getErpProductiveVersionPage(@Valid ErpProductiveVersionPageReqVO pageReqVO) {
        PageResult<ErpProductiveVersionDO> pageResult = erpProductiveVersionService.getErpProductiveVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpProductiveVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP生产版本 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpProductiveVersionExcel(@Valid ErpProductiveVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProductiveVersionDO> list = erpProductiveVersionService.getErpProductiveVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP生产版本.xls", "数据", ErpProductiveVersionRespVO.class,
                        BeanUtils.toBean(list, ErpProductiveVersionRespVO.class));
    }

    @PostMapping("/getErpProductiveVersionTask")
    @Operation(summary = "定时获得erp更新生产版本")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:sync')")
    public CommonResult<Boolean> getErpProductiveVersionTask() {
        erpProductiveVersionService.callErpRfcInterface();
        return success(true);
    }

    @PutMapping("/isEnable")
    @Operation(summary = "启用ERP生产版本,相同工厂、物料，只能启用一个")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-version:update')")
    public CommonResult<Boolean> enableErpProductiveVersion(@Valid @RequestBody ErpProductiveVersionSaveReqVO updateReqVO) {
        erpProductiveVersionService.enableErpProductiveVersion(updateReqVO);
        return success(true);
    }
}