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.ErpAssetPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpAssetRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpAssetSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpAssetDO;
import com.zt.plat.module.erp.service.erp.ErpAssetService;
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-asset")
@Validated
public class ErpAssetController {


    @Resource
    private ErpAssetService erpAssetService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP资产卡片")
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:create')")
    public CommonResult<ErpAssetRespVO> createErpAsset(@Valid @RequestBody ErpAssetSaveReqVO createReqVO) {
        return success(erpAssetService.createErpAsset(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP资产卡片")
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:update')")
    public CommonResult<Boolean> updateErpAsset(@Valid @RequestBody ErpAssetSaveReqVO updateReqVO) {
        erpAssetService.updateErpAsset(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP资产卡片")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:delete')")
    public CommonResult<Boolean> deleteErpAsset(@RequestParam("id") Long id) {
        erpAssetService.deleteErpAsset(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP资产卡片")
                @PreAuthorize("@ss.hasPermission('sply:erp-asset:delete')")
    public CommonResult<Boolean> deleteErpAssetList(@RequestBody BatchDeleteReqVO req) {
        erpAssetService.deleteErpAssetListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP资产卡片")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:query')")
    public CommonResult<ErpAssetRespVO> getErpAsset(@RequestParam("id") Long id) {
        ErpAssetDO erpAsset = erpAssetService.getErpAsset(id);
        return success(BeanUtils.toBean(erpAsset, ErpAssetRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP资产卡片分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:query')")
    public CommonResult<PageResult<ErpAssetRespVO>> getErpAssetPage(@Valid ErpAssetPageReqVO pageReqVO) {
        PageResult<ErpAssetDO> pageResult = erpAssetService.getErpAssetPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpAssetRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP资产卡片 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpAssetExcel(@Valid ErpAssetPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpAssetDO> list = erpAssetService.getErpAssetPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP资产卡片.xls", "数据", ErpAssetRespVO.class,
                        BeanUtils.toBean(list, ErpAssetRespVO.class));
    }

    @PostMapping("/getErpAssetTask")
    @Operation(summary = "定时获得erp更新资产卡片")
    @PreAuthorize("@ss.hasPermission('sply:erp-asset:sync')")
    public CommonResult<Boolean> getErpAssetTask() {
        erpAssetService.callErpRfcInterface();
        return success(true);
    }
}