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.ErpCostcenterPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCostcenterRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpCostcenterSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpCostcenterDO;
import com.zt.plat.module.erp.service.erp.ErpCostcenterService;
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-costcenter")
@Validated
public class ErpCostcenterController {


    @Resource
    private ErpCostcenterService erpCostcenterService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP成本中心")
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:create')")
    public CommonResult<ErpCostcenterRespVO> createErpCostcenter(@Valid @RequestBody ErpCostcenterSaveReqVO createReqVO) {
        return success(erpCostcenterService.createErpCostcenter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP成本中心")
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:update')")
    public CommonResult<Boolean> updateErpCostcenter(@Valid @RequestBody ErpCostcenterSaveReqVO updateReqVO) {
        erpCostcenterService.updateErpCostcenter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP成本中心")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:delete')")
    public CommonResult<Boolean> deleteErpCostcenter(@RequestParam("id") Long id) {
        erpCostcenterService.deleteErpCostcenter(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP成本中心")
                @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:delete')")
    public CommonResult<Boolean> deleteErpCostcenterList(@RequestBody BatchDeleteReqVO req) {
        erpCostcenterService.deleteErpCostcenterListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP成本中心")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:query')")
    public CommonResult<ErpCostcenterRespVO> getErpCostcenter(@RequestParam("id") Long id) {
        ErpCostcenterDO erpCostcenter = erpCostcenterService.getErpCostcenter(id);
        return success(BeanUtils.toBean(erpCostcenter, ErpCostcenterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP成本中心分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:query')")
    public CommonResult<PageResult<ErpCostcenterRespVO>> getErpCostcenterPage(@Valid ErpCostcenterPageReqVO pageReqVO) {
        PageResult<ErpCostcenterDO> pageResult = erpCostcenterService.getErpCostcenterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpCostcenterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP成本中心 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpCostcenterExcel(@Valid ErpCostcenterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpCostcenterDO> list = erpCostcenterService.getErpCostcenterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP成本中心.xls", "数据", ErpCostcenterRespVO.class,
                        BeanUtils.toBean(list, ErpCostcenterRespVO.class));
    }

    @PostMapping("/getErpCostcenterTask")
    @Operation(summary = "定时获得erp更新成本中心")
    @PreAuthorize("@ss.hasPermission('sply:erp-costcenter:sync')")
    public CommonResult<Boolean> getErpCostcenterTask() {
        erpCostcenterService.callErpRfcInterface();
        return success(true);
    }


}