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.ErpPurchaseOrganizationPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpPurchaseOrganizationRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpPurchaseOrganizationSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpPurchaseOrganizationDO;
import com.zt.plat.module.erp.service.erp.ErpPurchaseOrganizationService;
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-purchase-organization")
@Validated
public class ErpPurchaseOrganizationController {


    @Resource
    private ErpPurchaseOrganizationService erpPurchaseOrganizationService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP采购组织")
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:create')")
    public CommonResult<ErpPurchaseOrganizationRespVO> createErpPurchaseOrganization(@Valid @RequestBody ErpPurchaseOrganizationSaveReqVO createReqVO) {
        return success(erpPurchaseOrganizationService.createErpPurchaseOrganization(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP采购组织")
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:update')")
    public CommonResult<Boolean> updateErpPurchaseOrganization(@Valid @RequestBody ErpPurchaseOrganizationSaveReqVO updateReqVO) {
        erpPurchaseOrganizationService.updateErpPurchaseOrganization(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP采购组织")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:delete')")
    public CommonResult<Boolean> deleteErpPurchaseOrganization(@RequestParam("id") Long id) {
        erpPurchaseOrganizationService.deleteErpPurchaseOrganization(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP采购组织")
                @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:delete')")
    public CommonResult<Boolean> deleteErpPurchaseOrganizationList(@RequestBody BatchDeleteReqVO req) {
        erpPurchaseOrganizationService.deleteErpPurchaseOrganizationListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP采购组织")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:query')")
    public CommonResult<ErpPurchaseOrganizationRespVO> getErpPurchaseOrganization(@RequestParam("id") Long id) {
        ErpPurchaseOrganizationDO erpPurchaseOrganization = erpPurchaseOrganizationService.getErpPurchaseOrganization(id);
        return success(BeanUtils.toBean(erpPurchaseOrganization, ErpPurchaseOrganizationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP采购组织分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:query')")
    public CommonResult<PageResult<ErpPurchaseOrganizationRespVO>> getErpPurchaseOrganizationPage(@Valid ErpPurchaseOrganizationPageReqVO pageReqVO) {
        PageResult<ErpPurchaseOrganizationDO> pageResult = erpPurchaseOrganizationService.getErpPurchaseOrganizationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpPurchaseOrganizationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP采购组织 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpPurchaseOrganizationExcel(@Valid ErpPurchaseOrganizationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpPurchaseOrganizationDO> list = erpPurchaseOrganizationService.getErpPurchaseOrganizationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP采购组织.xls", "数据", ErpPurchaseOrganizationRespVO.class,
                        BeanUtils.toBean(list, ErpPurchaseOrganizationRespVO.class));
    }

    @PostMapping("/getErpPurchaseOrganizationTask")
    @Operation(summary = "定时获得erp更新采购组织")
    @PreAuthorize("@ss.hasPermission('sply:erp-purchase-organization:sync')")
    public CommonResult<Boolean> getErpPurchaseOrganizationTask() {
        erpPurchaseOrganizationService.callErpRfcInterface();
        return success(true);
    }

}