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.ErpInternalOrderPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpInternalOrderRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpInternalOrderSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpInternalOrderDO;
import com.zt.plat.module.erp.service.erp.ErpInternalOrderService;
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-internal-order")
@Validated
public class ErpInternalOrderController {


    @Resource
    private ErpInternalOrderService erpInternalOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP内部订单")
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:create')")
    public CommonResult<ErpInternalOrderRespVO> createErpInternalOrder(@Valid @RequestBody ErpInternalOrderSaveReqVO createReqVO) {
        return success(erpInternalOrderService.createErpInternalOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP内部订单")
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:update')")
    public CommonResult<Boolean> updateErpInternalOrder(@Valid @RequestBody ErpInternalOrderSaveReqVO updateReqVO) {
        erpInternalOrderService.updateErpInternalOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP内部订单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:delete')")
    public CommonResult<Boolean> deleteErpInternalOrder(@RequestParam("id") Long id) {
        erpInternalOrderService.deleteErpInternalOrder(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除ERP内部订单")
                @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:delete')")
    public CommonResult<Boolean> deleteErpInternalOrderList(@RequestBody BatchDeleteReqVO req) {
        erpInternalOrderService.deleteErpInternalOrderListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP内部订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:query')")
    public CommonResult<ErpInternalOrderRespVO> getErpInternalOrder(@RequestParam("id") Long id) {
        ErpInternalOrderDO erpInternalOrder = erpInternalOrderService.getErpInternalOrder(id);
        return success(BeanUtils.toBean(erpInternalOrder, ErpInternalOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP内部订单分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:query')")
    public CommonResult<PageResult<ErpInternalOrderRespVO>> getErpInternalOrderPage(@Valid ErpInternalOrderPageReqVO pageReqVO) {
        PageResult<ErpInternalOrderDO> pageResult = erpInternalOrderService.getErpInternalOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpInternalOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP内部订单 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpInternalOrderExcel(@Valid ErpInternalOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpInternalOrderDO> list = erpInternalOrderService.getErpInternalOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP内部订单.xls", "数据", ErpInternalOrderRespVO.class,
                        BeanUtils.toBean(list, ErpInternalOrderRespVO.class));
    }

    @PostMapping("/getErpInternalOrderTask")
    @Operation(summary = "定时获得erp更新内部订单数据")
    @PreAuthorize("@ss.hasPermission('sply:erp-internal-order:sync')")
    public CommonResult<Boolean> getErpInternalOrderTask() {
        erpInternalOrderService.callErpRfcInterface();
        return success(true);
    }

}