package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.CmsOrderService;
import com.gxwebsoft.cms.entity.CmsOrder;
import com.gxwebsoft.cms.param.CmsOrderParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 网站订单控制器
 *
 * @author 科技小王子
 * @since 2026-01-27 13:03:25
 */
@Tag(name = "网站订单管理")
@RestController
@RequestMapping("/api/cms/cms-order")
public class CmsOrderController extends BaseController {
    @Resource
    private CmsOrderService cmsOrderService;

    @PreAuthorize("hasAuthority('cms:cmsOrder:list')")
    @Operation(summary = "分页查询网站订单")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsOrder>> page(CmsOrderParam param) {
        // 使用关联查询
        return success(cmsOrderService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:list')")
    @Operation(summary = "查询全部网站订单")
    @GetMapping()
    public ApiResult<List<CmsOrder>> list(CmsOrderParam param) {
        // 使用关联查询
        return success(cmsOrderService.listRel(param));
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:list')")
    @Operation(summary = "根据id查询网站订单")
    @GetMapping("/{id}")
    public ApiResult<CmsOrder> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(cmsOrderService.getByIdRel(id));
    }

    @Operation(summary = "添加网站订单")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsOrder cmsOrder) {

        if (cmsOrderService.save(cmsOrder)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:update')")
    @OperationLog
    @Operation(summary = "修改网站订单")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsOrder cmsOrder) {
        if (cmsOrderService.updateById(cmsOrder)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:remove')")
    @OperationLog
    @Operation(summary = "删除网站订单")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsOrderService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:save')")
    @OperationLog
    @Operation(summary = "批量添加网站订单")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<CmsOrder> list) {
        if (cmsOrderService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:update')")
    @OperationLog
    @Operation(summary = "批量修改网站订单")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<CmsOrder> batchParam) {
        if (batchParam.update(cmsOrderService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsOrder:remove')")
    @OperationLog
    @Operation(summary = "批量删除网站订单")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (cmsOrderService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
