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.ErpProductiveOrderPageReqVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveOrderRespVO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveOrderSaveReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpProductiveOrderDO;
import com.zt.plat.module.erp.service.erp.ErpProductiveOrderService;
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-order")
@Validated
public class ErpProductiveOrderController {


    @Resource
    private ErpProductiveOrderService erpProductiveOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP生产订单")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-order:create')")
    public CommonResult<ErpProductiveOrderRespVO> createErpProductiveOrder(@Valid @RequestBody ErpProductiveOrderSaveReqVO createReqVO) {
        return success(erpProductiveOrderService.createErpProductiveOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP生产订单")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-order:update')")
    public CommonResult<Boolean> updateErpProductiveOrder(@Valid @RequestBody ErpProductiveOrderSaveReqVO updateReqVO) {
        erpProductiveOrderService.updateErpProductiveOrder(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得ERP生产订单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-order:query')")
    public CommonResult<ErpProductiveOrderRespVO> getErpProductiveOrder(@RequestParam("id") Long id) {
        ErpProductiveOrderDO erpProductiveOrder = erpProductiveOrderService.getErpProductiveOrder(id);
        return success(BeanUtils.toBean(erpProductiveOrder, ErpProductiveOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP生产订单分页")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-order:query')")
    public CommonResult<PageResult<ErpProductiveOrderRespVO>> getErpProductiveOrderPage(@Valid ErpProductiveOrderPageReqVO pageReqVO) {
        PageResult<ErpProductiveOrderDO> pageResult = erpProductiveOrderService.getErpProductiveOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ErpProductiveOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP生产订单 Excel")
    @PreAuthorize("@ss.hasPermission('sply:erp-productive-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportErpProductiveOrderExcel(@Valid ErpProductiveOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpProductiveOrderDO> list = erpProductiveOrderService.getErpProductiveOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP生产订单.xls", "数据", ErpProductiveOrderRespVO.class,
                        BeanUtils.toBean(list, ErpProductiveOrderRespVO.class));
    }

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

}